2 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "sockunion.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_zebra.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_debug.h"
45 #include "bgpd/bgp_mpath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_nht.h"
48 #include "bgpd/bgp_bfd.h"
50 /* All information about zebra. */
51 struct zclient
*zclient
= NULL
;
53 /* Growable buffer for nexthops sent to zebra */
54 struct stream
*bgp_nexthop_buf
= NULL
;
55 struct stream
*bgp_ifindices_buf
= NULL
;
57 /* These array buffers are used in making a copy of the attributes for
58 route-map apply. Arrays are being used here to minimize mallocs and
59 frees for the temporary copy of the attributes.
60 Given the zapi api expects the nexthop buffer to contain pointer to
61 pointers for nexthops, we couldnt have used a single nexthop variable
62 on the stack, hence we had two options:
63 1. maintain a linked-list and free it after zapi_*_route call
64 2. use an array to avoid number of mallocs.
65 Number of supported next-hops are finite, use of arrays should be ok. */
66 struct attr attr_cp
[MULTIPATH_NUM
];
67 struct attr_extra attr_extra_cp
[MULTIPATH_NUM
];
70 /* Once per address-family initialization of the attribute array */
71 #define BGP_INFO_ATTR_BUF_INIT()\
73 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
74 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
78 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
80 *info_dst = *info_src; \
81 assert(attr_index != MULTIPATH_NUM);\
82 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
83 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
84 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
85 info_dst->attr = &attr_cp[attr_index]; \
89 #define BGP_INFO_ATTR_BUF_FREE(info) \
91 bgp_attr_deep_free(info->attr); \
95 /* Can we install into zebra? */
97 bgp_install_info_to_zebra (struct bgp
*bgp
)
99 if (zclient
->sock
<= 0)
102 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
108 /* Router-id update message from zebra. */
110 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
113 struct prefix router_id
;
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 bgp_router_id_zebra_bump (vrf_id
, &router_id
);
128 /* Nexthop update message from zebra. */
130 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
131 zebra_size_t length
, vrf_id_t vrf_id
)
133 bgp_parse_nexthop_update(command
, vrf_id
);
138 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
139 zebra_size_t length
, vrf_id_t vrf_id
)
141 bgp_parse_nexthop_update(command
, vrf_id
);
145 /* Set or clear interface on which unnumbered neighbor is configured. This
146 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
150 bgp_update_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
,
151 struct interface
*upd_ifp
)
153 struct listnode
*node
, *nnode
;
156 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
159 (strcmp (peer
->conf_if
, ifp
->name
) == 0))
164 bgp_zebra_initiate_radv (bgp
, peer
);
168 bgp_zebra_terminate_radv (bgp
, peer
);
176 bgp_start_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
)
178 struct listnode
*node
, *nnode
;
181 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
184 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
185 peer
->status
!= Established
)
187 if (peer_active(peer
))
188 BGP_EVENT_ADD (peer
, BGP_Stop
);
189 BGP_EVENT_ADD (peer
, BGP_Start
);
195 bgp_nbr_connected_add (struct bgp
*bgp
, struct nbr_connected
*ifc
)
197 struct listnode
*node
;
198 struct connected
*connected
;
199 struct interface
*ifp
;
202 /* Kick-off the FSM for any relevant peers only if there is a
203 * valid local address on the interface.
206 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
208 p
= connected
->address
;
209 if (p
->family
== AF_INET6
&&
210 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
216 bgp_start_interface_nbrs (bgp
, ifp
);
220 bgp_nbr_connected_delete (struct bgp
*bgp
, struct nbr_connected
*ifc
, int del
)
222 struct listnode
*node
, *nnode
;
224 struct interface
*ifp
;
226 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
228 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
230 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
231 BGP_EVENT_ADD (peer
, BGP_Stop
);
234 /* Free neighbor also, if we're asked to. */
238 listnode_delete (ifp
->nbr_connected
, ifc
);
239 nbr_connected_free (ifc
);
243 /* Inteface addition message from zebra. */
245 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
248 struct interface
*ifp
;
251 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
252 if (!ifp
) // unexpected
255 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
256 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
258 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
262 bgp_update_interface_nbrs (bgp
, ifp
, ifp
);
267 bgp_interface_delete (int command
, struct zclient
*zclient
,
268 zebra_size_t length
, vrf_id_t vrf_id
)
271 struct interface
*ifp
;
275 ifp
= zebra_interface_state_read (s
, vrf_id
);
276 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
279 ifp
->ifindex
= IFINDEX_DELETED
;
281 if (BGP_DEBUG (zebra
, ZEBRA
))
282 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
284 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
288 bgp_update_interface_nbrs (bgp
, ifp
, NULL
);
293 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
297 struct interface
*ifp
;
299 struct nbr_connected
*nc
;
300 struct listnode
*node
, *nnode
;
304 ifp
= zebra_interface_state_read (s
, vrf_id
);
309 if (BGP_DEBUG (zebra
, ZEBRA
))
310 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
312 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
316 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
317 bgp_connected_add (bgp
, c
);
319 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
320 bgp_nbr_connected_add (bgp
, nc
);
326 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
330 struct interface
*ifp
;
332 struct nbr_connected
*nc
;
333 struct listnode
*node
, *nnode
;
337 ifp
= zebra_interface_state_read (s
, vrf_id
);
341 if (BGP_DEBUG (zebra
, ZEBRA
))
342 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
344 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
348 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
349 bgp_connected_delete (bgp
, c
);
351 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
352 bgp_nbr_connected_delete (bgp
, nc
, 1);
354 /* Fast external-failover */
358 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
361 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
363 #if defined(HAVE_CUMULUS)
364 /* Take down directly connected EBGP peers as well as 1-hop BFD
365 * tracked (directly connected) IBGP peers.
367 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1) &&
368 (!peer
->bfd_info
|| bgp_bfd_is_peer_multihop(peer
)))
370 /* Take down directly connected EBGP peers */
371 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
375 if (ifp
== peer
->nexthop
.ifp
)
377 BGP_EVENT_ADD (peer
, BGP_Stop
);
378 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
387 bgp_interface_address_add (int command
, struct zclient
*zclient
,
388 zebra_size_t length
, vrf_id_t vrf_id
)
390 struct connected
*ifc
;
392 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
397 if (bgp_debug_zebra(ifc
->address
))
399 char buf
[PREFIX2STR_BUFFER
];
400 prefix2str(ifc
->address
, buf
, sizeof(buf
));
401 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
402 vrf_id
, ifc
->ifp
->name
, buf
);
405 if (if_is_operative (ifc
->ifp
))
409 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
413 bgp_connected_add (bgp
, ifc
);
414 /* If we have learnt of any neighbors on this interface,
415 * check to kick off any BGP interface-based neighbors,
416 * but only if this is a link-local address.
418 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
419 !list_isempty(ifc
->ifp
->nbr_connected
))
420 bgp_start_interface_nbrs (bgp
, ifc
->ifp
);
427 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
428 zebra_size_t length
, vrf_id_t vrf_id
)
430 struct connected
*ifc
;
433 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
438 if (bgp_debug_zebra(ifc
->address
))
440 char buf
[PREFIX2STR_BUFFER
];
441 prefix2str(ifc
->address
, buf
, sizeof(buf
));
442 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
443 vrf_id
, ifc
->ifp
->name
, buf
);
446 if (if_is_operative (ifc
->ifp
))
448 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
450 bgp_connected_delete (bgp
, ifc
);
453 connected_free (ifc
);
459 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
460 zebra_size_t length
, vrf_id_t vrf_id
)
462 struct nbr_connected
*ifc
= NULL
;
465 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
470 if (bgp_debug_zebra(ifc
->address
))
472 char buf
[PREFIX2STR_BUFFER
];
473 prefix2str(ifc
->address
, buf
, sizeof(buf
));
474 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
475 vrf_id
, ifc
->ifp
->name
, buf
);
478 if (if_is_operative (ifc
->ifp
))
480 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
482 bgp_nbr_connected_add (bgp
, ifc
);
489 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
490 zebra_size_t length
, vrf_id_t vrf_id
)
492 struct nbr_connected
*ifc
= NULL
;
495 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
500 if (bgp_debug_zebra(ifc
->address
))
502 char buf
[PREFIX2STR_BUFFER
];
503 prefix2str(ifc
->address
, buf
, sizeof(buf
));
504 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
505 vrf_id
, ifc
->ifp
->name
, buf
);
508 if (if_is_operative (ifc
->ifp
))
510 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
512 bgp_nbr_connected_delete (bgp
, ifc
, 0);
515 nbr_connected_free (ifc
);
520 /* VRF update for an interface. */
522 bgp_interface_vrf_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
525 struct interface
*ifp
;
528 struct nbr_connected
*nc
;
529 struct listnode
*node
, *nnode
;
532 ifp
= zebra_interface_vrf_update_read (zclient
->ibuf
, vrf_id
, &new_vrf_id
);
536 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
537 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
538 vrf_id
, ifp
->name
, new_vrf_id
);
540 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
544 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
545 bgp_connected_delete (bgp
, c
);
547 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
548 bgp_nbr_connected_delete (bgp
, nc
, 1);
550 /* Fast external-failover */
554 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
557 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
559 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
562 if (ifp
== peer
->nexthop
.ifp
)
563 BGP_EVENT_ADD (peer
, BGP_Stop
);
567 if_update_vrf (ifp
, ifp
->name
, strlen (ifp
->name
), new_vrf_id
);
569 bgp
= bgp_lookup_by_vrf_id (new_vrf_id
);
573 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
574 bgp_connected_add (bgp
, c
);
576 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
577 bgp_nbr_connected_add (bgp
, nc
);
581 /* Zebra route add and delete treatment. */
583 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
587 struct zapi_ipv4 api
;
588 struct in_addr nexthop
;
589 struct prefix_ipv4 p
;
590 unsigned int ifindex
;
594 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
601 /* Type, flags, message. */
602 api
.type
= stream_getc (s
);
603 api
.instance
= stream_getw (s
);
604 api
.flags
= stream_getc (s
);
605 api
.message
= stream_getc (s
);
608 memset (&p
, 0, sizeof (struct prefix_ipv4
));
610 p
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc (s
));
611 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
613 /* Nexthop, ifindex, distance, metric. */
614 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
616 api
.nexthop_num
= stream_getc (s
);
617 nexthop
.s_addr
= stream_get_ipv4 (s
);
620 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
622 api
.ifindex_num
= stream_getc (s
);
623 ifindex
= stream_getl (s
); /* ifindex, unused */
630 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
631 api
.distance
= stream_getc (s
);
633 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
634 api
.metric
= stream_getl (s
);
638 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
639 api
.tag
= stream_getw (s
);
643 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
645 if (bgp_debug_zebra((struct prefix
*)&p
))
647 char buf
[2][INET_ADDRSTRLEN
];
648 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
650 zebra_route_string(api
.type
), api
.instance
,
651 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
653 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
659 * The ADD message is actually an UPDATE and there is no explicit DEL
660 * for a prior redistributed route, if any. So, perform an implicit
661 * DEL processing for the same redistributed route from any other
664 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
667 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
670 /* Now perform the add/update. */
671 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
672 api
.metric
, api
.type
, api
.instance
, api
.tag
);
674 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
676 if (bgp_debug_zebra((struct prefix
*)&p
))
678 char buf
[2][INET_ADDRSTRLEN
];
679 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
680 "nexthop %s metric %u tag %d",
682 zebra_route_string(api
.type
), api
.instance
,
683 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
685 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
689 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
, api
.instance
);
696 /* Zebra route add and delete treatment. */
698 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
702 struct zapi_ipv6 api
;
703 struct in6_addr nexthop
;
704 struct prefix_ipv6 p
;
705 unsigned int ifindex
;
709 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
714 memset (&nexthop
, 0, sizeof (struct in6_addr
));
716 /* Type, flags, message. */
717 api
.type
= stream_getc (s
);
718 api
.instance
= stream_getw (s
);
719 api
.flags
= stream_getc (s
);
720 api
.message
= stream_getc (s
);
723 memset (&p
, 0, sizeof (struct prefix_ipv6
));
725 p
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc (s
));
726 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
728 /* Nexthop, ifindex, distance, metric. */
729 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
731 api
.nexthop_num
= stream_getc (s
);
732 stream_get (&nexthop
, s
, 16);
735 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
737 api
.ifindex_num
= stream_getc (s
);
738 ifindex
= stream_getl (s
); /* ifindex, unused */
745 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
746 api
.distance
= stream_getc (s
);
750 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
751 api
.metric
= stream_getl (s
);
755 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
756 api
.tag
= stream_getw (s
);
760 /* Simply ignore link-local address. */
761 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
764 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
766 if (bgp_debug_zebra((struct prefix
*)&p
))
768 char buf
[2][INET6_ADDRSTRLEN
];
769 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
771 zebra_route_string(api
.type
), api
.instance
,
772 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
774 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
780 * The ADD message is actually an UPDATE and there is no explicit DEL
781 * for a prior redistributed route, if any. So, perform an implicit
782 * DEL processing for the same redistributed route from any other
785 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
788 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
791 bgp_redistribute_add (bgp
, (struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
792 api
.metric
, api
.type
, api
.instance
, api
.tag
);
794 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
796 if (bgp_debug_zebra((struct prefix
*)&p
))
798 char buf
[2][INET6_ADDRSTRLEN
];
799 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
800 "nexthop %s metric %u tag %d",
802 zebra_route_string(api
.type
), api
.instance
,
803 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
805 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
809 bgp_redistribute_delete (bgp
, (struct prefix
*) &p
, api
.type
, api
.instance
);
814 #endif /* HAVE_IPV6 */
817 if_lookup_by_ipv4 (struct in_addr
*addr
, vrf_id_t vrf_id
)
819 struct listnode
*ifnode
;
820 struct listnode
*cnode
;
821 struct interface
*ifp
;
822 struct connected
*connected
;
823 struct prefix_ipv4 p
;
828 p
.prefixlen
= IPV4_MAX_BITLEN
;
830 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
832 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
834 cp
= connected
->address
;
836 if (cp
->family
== AF_INET
)
837 if (prefix_match (cp
, (struct prefix
*)&p
))
845 if_lookup_by_ipv4_exact (struct in_addr
*addr
, vrf_id_t vrf_id
)
847 struct listnode
*ifnode
;
848 struct listnode
*cnode
;
849 struct interface
*ifp
;
850 struct connected
*connected
;
853 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
855 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
857 cp
= connected
->address
;
859 if (cp
->family
== AF_INET
)
860 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
869 if_lookup_by_ipv6 (struct in6_addr
*addr
, ifindex_t ifindex
, vrf_id_t vrf_id
)
871 struct listnode
*ifnode
;
872 struct listnode
*cnode
;
873 struct interface
*ifp
;
874 struct connected
*connected
;
875 struct prefix_ipv6 p
;
880 p
.prefixlen
= IPV6_MAX_BITLEN
;
882 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
884 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
886 cp
= connected
->address
;
888 if (cp
->family
== AF_INET6
)
889 if (prefix_match (cp
, (struct prefix
*)&p
))
891 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
893 if (ifindex
== ifp
->ifindex
)
905 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, ifindex_t ifindex
, vrf_id_t vrf_id
)
907 struct listnode
*ifnode
;
908 struct listnode
*cnode
;
909 struct interface
*ifp
;
910 struct connected
*connected
;
913 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
915 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
917 cp
= connected
->address
;
919 if (cp
->family
== AF_INET6
)
920 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
922 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
924 if (ifindex
== ifp
->ifindex
)
936 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
938 struct listnode
*cnode
;
939 struct connected
*connected
;
942 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
944 cp
= connected
->address
;
946 if (cp
->family
== AF_INET6
)
947 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
949 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
957 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
959 struct listnode
*cnode
;
960 struct connected
*connected
;
963 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
965 cp
= connected
->address
;
967 if (cp
->family
== AF_INET6
)
968 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
970 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
976 #endif /* HAVE_IPV6 */
979 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
981 struct listnode
*cnode
;
982 struct connected
*connected
;
985 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
987 cp
= connected
->address
;
988 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
990 *addr
= cp
->u
.prefix4
;
998 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
999 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
1002 struct interface
*ifp
= NULL
;
1004 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
1011 if (local
->sa
.sa_family
== AF_INET
)
1013 nexthop
->v4
= local
->sin
.sin_addr
;
1014 if (peer
->update_if
)
1015 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1017 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
, peer
->bgp
->vrf_id
);
1020 if (local
->sa
.sa_family
== AF_INET6
)
1022 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1024 if (peer
->conf_if
|| peer
->ifname
)
1025 ifp
= if_lookup_by_name_vrf (peer
->conf_if
? peer
->conf_if
: peer
->ifname
, peer
->bgp
->vrf_id
);
1027 else if (peer
->update_if
)
1028 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1030 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
1031 local
->sin6
.sin6_scope_id
,
1034 #endif /* HAVE_IPV6 */
1041 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1042 if (local
->sa
.sa_family
== AF_INET
)
1046 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1050 /* There is no global nexthop. Use link-local address as both the
1051 * global and link-local nexthop. In this scenario, the expectation
1052 * for interop is that the network admin would use a route-map to
1053 * specify the global IPv6 nexthop.
1055 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
1056 memcpy (&nexthop
->v6_local
, &nexthop
->v6_global
,
1060 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1062 if (if_lookup_by_ipv4 (&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1063 peer
->shared_network
= 1;
1065 peer
->shared_network
= 0;
1066 #endif /* HAVE_IPV6 */
1070 /* IPv6 connection, fetch and store IPv4 local address if any. */
1071 if (local
->sa
.sa_family
== AF_INET6
)
1073 struct interface
*direct
= NULL
;
1076 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1077 if (!ret
&& peer
->local_id
.s_addr
)
1078 nexthop
->v4
= peer
->local_id
;
1081 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1083 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1086 /* If directory connected set link-local address. */
1087 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
1088 remote
->sin6
.sin6_scope_id
, peer
->bgp
->vrf_id
);
1090 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1093 /* Link-local address. */
1095 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1097 /* If there is no global address. Set link-local address as
1098 global. I know this break RFC specification... */
1099 /* In this scenario, the expectation for interop is that the
1100 * network admin would use a route-map to specify the global
1104 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1106 /* Always set the link-local address */
1107 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1111 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
1112 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
,
1114 peer
->shared_network
= 1;
1116 peer
->shared_network
= 0;
1119 /* KAME stack specific treatment. */
1121 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
1122 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
1124 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
1126 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
1127 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
1129 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
1132 #endif /* HAVE_IPV6 */
1134 /* If we have identified the local interface, there is no error for now. */
1138 static struct in6_addr
*
1139 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
1141 struct in6_addr
*nexthop
= NULL
;
1143 /* Only global address nexthop exists. */
1144 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1145 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1147 /* If both global and link-local address present. */
1148 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1150 /* Check if route-map is set to prefer global over link-local */
1151 if (info
->attr
->extra
->mp_nexthop_prefer_global
)
1152 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1155 /* Workaround for Cisco's nexthop bug. */
1156 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
1157 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1158 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
1160 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1168 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
1169 struct bgp_info
*info
)
1171 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1174 if (bgp_debug_zebra(p
))
1176 if (p
->family
== AF_INET
)
1178 char buf
[2][INET_ADDRSTRLEN
];
1179 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1180 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1182 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1185 if (p
->family
== AF_INET6
)
1187 char buf
[2][INET6_ADDRSTRLEN
];
1188 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1189 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1191 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1199 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1200 afi_t afi
, safi_t safi
)
1205 struct bgp_info
*mpinfo
;
1206 size_t oldsize
, newsize
;
1207 u_int32_t nhcount
, metric
;
1208 struct bgp_info local_info
;
1209 struct bgp_info
*info_cp
= &local_info
;
1212 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1213 * know of this instance.
1215 if (!bgp_install_info_to_zebra (bgp
))
1218 if ((p
->family
== AF_INET
&&
1219 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
))
1220 || (p
->family
== AF_INET6
&&
1221 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
)))
1224 if (bgp
->main_zebra_update_hold
)
1230 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1231 tag
= info
->attr
->extra
->tag
;
1235 /* When we create an aggregate route we must also install a Null0 route in
1237 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1238 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1240 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1241 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1243 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1244 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1247 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1248 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1249 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1251 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1253 nhcount
= 1 + bgp_info_mpath_count (info
);
1255 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1257 struct zapi_ipv4 api
;
1258 struct in_addr
*nexthop
;
1259 char buf
[2][INET_ADDRSTRLEN
];
1260 int valid_nh_count
= 0;
1262 /* resize nexthop buffer size if necessary */
1263 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1264 (sizeof (struct in_addr
*) * nhcount
))
1266 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1267 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1268 if (newsize
== oldsize
)
1270 zlog_err ("can't resize nexthop buffer");
1274 stream_reset (bgp_nexthop_buf
);
1277 /* Metric is currently based on the best-path only. */
1278 metric
= info
->attr
->med
;
1280 if (bgp
->table_map
[afi
][safi
].name
)
1282 BGP_INFO_ATTR_BUF_INIT();
1284 /* Copy info and attributes, so the route-map apply doesn't modify the
1286 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1287 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1289 metric
= info_cp
->attr
->med
;
1290 nexthop
= &info_cp
->attr
->nexthop
;
1292 if (info_cp
->attr
->extra
)
1293 tag
= info_cp
->attr
->extra
->tag
;
1295 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1299 nexthop
= &info
->attr
->nexthop
;
1304 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1308 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1309 mpinfo
= bgp_info_mpath_next (mpinfo
))
1313 if (bgp
->table_map
[afi
][safi
].name
)
1315 /* Copy info and attributes, so the route-map apply doesn't modify the
1317 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1318 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1319 nexthop
= &info_cp
->attr
->nexthop
;
1320 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1324 nexthop
= &mpinfo
->attr
->nexthop
;
1327 if (nexthop
== NULL
)
1330 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1334 api
.vrf_id
= bgp
->vrf_id
;
1336 api
.type
= ZEBRA_ROUTE_BGP
;
1340 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1342 /* Note that this currently only applies to Null0 routes for aggregates.
1343 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1344 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1345 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1347 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1348 api
.nexthop_num
= 0;
1350 api
.nexthop_num
= valid_nh_count
;
1352 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1353 api
.ifindex_num
= 0;
1354 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1355 api
.metric
= metric
;
1360 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1364 distance
= bgp_distance_apply (p
, info
, bgp
);
1368 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1369 api
.distance
= distance
;
1372 if (bgp_debug_zebra(p
))
1375 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d"
1376 " count %d", (valid_nh_count
? "add":"delete"),
1378 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1379 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1380 for (i
= 0; i
< api
.nexthop_num
; i
++)
1381 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1382 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1385 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1386 zclient
, (struct prefix_ipv4
*) p
, &api
);
1390 /* We have to think about a IPv6 link-local address curse. */
1391 if (p
->family
== AF_INET6
||
1392 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1395 struct in6_addr
*nexthop
;
1396 struct zapi_ipv6 api
;
1397 int valid_nh_count
= 0;
1398 char buf
[2][INET6_ADDRSTRLEN
];
1400 /* resize nexthop buffer size if necessary */
1401 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1402 (sizeof (struct in6_addr
*) * nhcount
))
1404 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1405 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1406 if (newsize
== oldsize
)
1408 zlog_err ("can't resize nexthop buffer");
1412 stream_reset (bgp_nexthop_buf
);
1414 /* resize ifindices buffer size if necessary */
1415 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1416 (sizeof (unsigned int) * nhcount
))
1418 newsize
= (sizeof (unsigned int) * nhcount
);
1419 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1420 if (newsize
== oldsize
)
1422 zlog_err ("can't resize nexthop buffer");
1426 stream_reset (bgp_ifindices_buf
);
1431 assert (info
->attr
->extra
);
1433 /* Metric is currently based on the best-path only. */
1434 metric
= info
->attr
->med
;
1436 if (bgp
->table_map
[afi
][safi
].name
)
1438 BGP_INFO_ATTR_BUF_INIT();
1440 /* Copy info and attributes, so the route-map apply doesn't modify the
1442 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1443 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1445 metric
= info_cp
->attr
->med
;
1446 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1448 if (info_cp
->attr
->extra
)
1449 tag
= info_cp
->attr
->extra
->tag
;
1451 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1455 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1460 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1461 if (info
->peer
->nexthop
.ifp
)
1462 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1466 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1467 ifindex
= if_nametoindex (info
->peer
->conf_if
? info
->peer
->conf_if
: info
->peer
->ifname
);
1468 else if (info
->peer
->nexthop
.ifp
)
1469 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1471 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1472 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1476 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1477 mpinfo
= bgp_info_mpath_next (mpinfo
))
1482 if (bgp
->table_map
[afi
][safi
].name
)
1484 /* Copy info and attributes, so the route-map apply doesn't modify the
1486 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1487 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1488 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1489 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1493 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1496 if (nexthop
== NULL
)
1499 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1500 if (mpinfo
->peer
->nexthop
.ifp
)
1501 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1505 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1506 ifindex
= ifname2ifindex (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
: mpinfo
->peer
->ifname
);
1507 else if (mpinfo
->peer
->nexthop
.ifp
)
1508 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1513 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1514 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1518 /* Make Zebra API structure. */
1519 api
.vrf_id
= bgp
->vrf_id
;
1521 api
.type
= ZEBRA_ROUTE_BGP
;
1525 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1527 /* Note that this currently only applies to Null0 routes for aggregates.
1528 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1529 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1530 * do not want to also encode the :: nexthop for the aggregate route.
1532 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1533 api
.nexthop_num
= 0;
1535 api
.nexthop_num
= valid_nh_count
;
1537 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1538 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1539 api
.ifindex_num
= valid_nh_count
;
1540 api
.ifindex
= (ifindex_t
*)STREAM_DATA (bgp_ifindices_buf
);
1541 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1542 api
.metric
= metric
;
1547 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1551 if (p
->family
== AF_INET
)
1553 if (bgp_debug_zebra(p
))
1556 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d",
1557 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1558 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1559 p
->prefixlen
, api
.metric
, api
.tag
);
1560 for (i
= 0; i
< api
.nexthop_num
; i
++)
1561 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1562 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1566 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1567 zclient
, (struct prefix_ipv4
*) p
,
1568 (struct zapi_ipv6
*)&api
);
1570 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1571 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1575 if (bgp_debug_zebra(p
))
1578 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %d",
1579 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1580 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1581 p
->prefixlen
, api
.metric
, api
.tag
);
1582 for (i
= 0; i
< api
.nexthop_num
; i
++)
1583 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1584 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1587 zapi_ipv6_route (valid_nh_count
?
1588 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1589 zclient
, (struct prefix_ipv6
*) p
, &api
);
1592 #endif /* HAVE_IPV6 */
1595 /* Announce all routes of a table to zebra */
1597 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1599 struct bgp_node
*rn
;
1600 struct bgp_table
*table
;
1601 struct bgp_info
*ri
;
1603 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1604 * know of this instance.
1606 if (!bgp_install_info_to_zebra (bgp
))
1609 table
= bgp
->rib
[afi
][safi
];
1612 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1613 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1614 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1615 && ri
->type
== ZEBRA_ROUTE_BGP
1616 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1617 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1621 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1629 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1630 * know of this instance.
1632 if (!bgp_install_info_to_zebra (peer
->bgp
))
1635 if ((p
->family
== AF_INET
&&
1636 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
))
1637 || (p
->family
== AF_INET6
&&
1638 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
)))
1643 if (peer
->sort
== BGP_PEER_IBGP
)
1645 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1646 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1649 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1650 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1651 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1652 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1654 if (p
->family
== AF_INET
)
1656 struct zapi_ipv4 api
;
1658 api
.vrf_id
= peer
->bgp
->vrf_id
;
1661 api
.type
= ZEBRA_ROUTE_BGP
;
1665 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1666 api
.nexthop_num
= 0;
1668 api
.ifindex_num
= 0;
1669 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1670 api
.metric
= info
->attr
->med
;
1673 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1675 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1676 api
.tag
= info
->attr
->extra
->tag
;
1679 if (bgp_debug_zebra(p
))
1681 char buf
[2][INET_ADDRSTRLEN
];
1682 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %d",
1684 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1685 p
->prefixlen
, api
.metric
, api
.tag
);
1688 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1689 (struct prefix_ipv4
*) p
, &api
);
1692 /* We have to think about a IPv6 link-local address curse. */
1693 if (p
->family
== AF_INET6
)
1695 struct zapi_ipv6 api
;
1697 assert (info
->attr
->extra
);
1699 api
.vrf_id
= peer
->bgp
->vrf_id
;
1701 api
.type
= ZEBRA_ROUTE_BGP
;
1705 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1706 api
.nexthop_num
= 0;
1708 api
.ifindex_num
= 0;
1709 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1710 api
.metric
= info
->attr
->med
;
1713 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1715 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1716 api
.tag
= info
->attr
->extra
->tag
;
1719 if (bgp_debug_zebra(p
))
1721 char buf
[2][INET6_ADDRSTRLEN
];
1722 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %d",
1724 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1725 p
->prefixlen
, api
.metric
, api
.tag
);
1728 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1729 (struct prefix_ipv6
*) p
, &api
);
1731 #endif /* HAVE_IPV6 */
1734 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1736 struct list
*red_list
;
1737 struct listnode
*node
;
1738 struct bgp_redist
*red
;
1740 red_list
= bgp
->redist
[afi
][type
];
1744 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1745 if (red
->instance
== instance
)
1752 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1754 struct list
*red_list
;
1755 struct bgp_redist
*red
;
1757 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1761 if (!bgp
->redist
[afi
][type
])
1762 bgp
->redist
[afi
][type
] = list_new();
1764 red_list
= bgp
->redist
[afi
][type
];
1765 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1766 red
->instance
= instance
;
1768 listnode_add(red_list
, red
);
1774 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1776 struct bgp_redist
*red
;
1778 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1782 listnode_delete(bgp
->redist
[afi
][type
], red
);
1783 if (!bgp
->redist
[afi
][type
]->count
)
1785 list_free(bgp
->redist
[afi
][type
]);
1786 bgp
->redist
[afi
][type
] = NULL
;
1791 /* Other routes redistribution into BGP. */
1793 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1796 /* Return if already redistribute flag is set. */
1799 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1802 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1806 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1809 vrf_bitmap_set (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1812 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1813 * know of this instance.
1815 if (!bgp_install_info_to_zebra (bgp
))
1818 if (BGP_DEBUG (zebra
, ZEBRA
))
1819 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1821 zebra_route_string(type
), instance
);
1823 /* Send distribute add message to zebra. */
1824 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1825 instance
, bgp
->vrf_id
);
1831 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1833 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1834 * know of this instance.
1836 if (!bgp_install_info_to_zebra (bgp
))
1839 if (BGP_DEBUG (zebra
, ZEBRA
))
1840 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1842 zebra_route_string(type
), instance
);
1844 /* Send distribute add message to zebra. */
1845 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1846 instance
, bgp
->vrf_id
);
1847 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1848 instance
, bgp
->vrf_id
);
1853 /* Redistribute with route-map specification. */
1855 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1858 && (strcmp (red
->rmap
.name
, name
) == 0))
1862 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1863 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1864 red
->rmap
.map
= route_map_lookup_by_name (name
);
1869 /* Redistribute with metric specification. */
1871 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1872 int type
, u_int32_t metric
)
1874 struct bgp_node
*rn
;
1875 struct bgp_info
*ri
;
1877 if (red
->redist_metric_flag
1878 && red
->redist_metric
== metric
)
1881 red
->redist_metric_flag
= 1;
1882 red
->redist_metric
= metric
;
1884 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
))
1886 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1888 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&&
1890 ri
->instance
== red
->instance
)
1892 struct attr
*old_attr
;
1893 struct attr new_attr
;
1894 struct attr_extra new_extra
;
1896 new_attr
.extra
= &new_extra
;
1897 bgp_attr_dup (&new_attr
, ri
->attr
);
1898 new_attr
.med
= red
->redist_metric
;
1899 old_attr
= ri
->attr
;
1900 ri
->attr
= bgp_attr_intern (&new_attr
);
1901 bgp_attr_unintern (&old_attr
);
1903 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1904 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1912 /* Unset redistribution. */
1914 bgp_redistribute_unreg (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1916 struct bgp_redist
*red
;
1918 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1922 /* Return if zebra connection is disabled. */
1925 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1927 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1931 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1933 vrf_bitmap_unset (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1936 if (bgp_install_info_to_zebra (bgp
))
1938 /* Send distribute delete message to zebra. */
1939 if (BGP_DEBUG (zebra
, ZEBRA
))
1940 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1941 bgp
->vrf_id
, afi
, zebra_route_string(type
), instance
);
1942 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1946 /* Withdraw redistributed routes from current BGP's routing table. */
1947 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1952 /* Unset redistribution. */
1954 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1956 struct bgp_redist
*red
;
1958 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1962 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1964 /* Unset route-map. */
1966 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1967 red
->rmap
.name
= NULL
;
1968 red
->rmap
.map
= NULL
;
1971 red
->redist_metric_flag
= 0;
1972 red
->redist_metric
= 0;
1974 bgp_redist_del(bgp
, afi
, type
, instance
);
1979 /* Update redistribute vrf bitmap during triggers like
1980 restart networking or delete/add VRFs */
1982 bgp_update_redist_vrf_bitmaps (struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1987 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1988 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1989 if (vrf_bitmap_check (zclient
->redist
[afi
][i
], old_vrf_id
))
1991 vrf_bitmap_unset (zclient
->redist
[afi
][i
], old_vrf_id
);
1992 vrf_bitmap_set (zclient
->redist
[afi
][i
], bgp
->vrf_id
);
1998 bgp_zclient_reset (void)
2000 zclient_reset (zclient
);
2003 /* Register this instance with Zebra. Invoked upon connect (for
2004 * default instance) and when other VRFs are learnt (or created and
2008 bgp_zebra_instance_register (struct bgp
*bgp
)
2010 /* Don't try to register if we're not connected to Zebra */
2011 if (!zclient
|| zclient
->sock
< 0)
2014 if (BGP_DEBUG (zebra
, ZEBRA
))
2015 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2017 /* Register for router-id, interfaces, redistributed routes. */
2018 zclient_send_reg_requests (zclient
, bgp
->vrf_id
);
2021 /* Deregister this instance with Zebra. Invoked upon the instance
2022 * being deleted (default or VRF) and it is already registered.
2025 bgp_zebra_instance_deregister (struct bgp
*bgp
)
2027 /* Don't try to deregister if we're not connected to Zebra */
2028 if (zclient
->sock
< 0)
2031 if (BGP_DEBUG (zebra
, ZEBRA
))
2032 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2034 /* Deregister for router-id, interfaces, redistributed routes. */
2035 zclient_send_dereg_requests (zclient
, bgp
->vrf_id
);
2039 bgp_zebra_initiate_radv (struct bgp
*bgp
, struct peer
*peer
)
2041 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2043 /* Don't try to initiate if we're not connected to Zebra */
2044 if (zclient
->sock
< 0)
2047 if (BGP_DEBUG (zebra
, ZEBRA
))
2048 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2050 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 1, ra_interval
);
2054 bgp_zebra_terminate_radv (struct bgp
*bgp
, struct peer
*peer
)
2056 /* Don't try to terminate if we're not connected to Zebra */
2057 if (zclient
->sock
< 0)
2060 if (BGP_DEBUG (zebra
, ZEBRA
))
2061 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2063 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
2066 /* BGP has established connection with Zebra. */
2068 bgp_zebra_connected (struct zclient
*zclient
)
2072 /* At this point, we may or may not have BGP instances configured, but
2073 * we're only interested in the default VRF (others wouldn't have learnt
2074 * the VRF from Zebra yet.)
2076 bgp
= bgp_get_default();
2080 bgp_zebra_instance_register (bgp
);
2082 /* Send the client registration */
2083 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2085 /* TODO - What if we have peers and networks configured, do we have to
2092 bgp_zebra_init (struct thread_master
*master
)
2094 /* Set default values. */
2095 zclient
= zclient_new (master
);
2096 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
2097 zclient
->zebra_connected
= bgp_zebra_connected
;
2098 zclient
->router_id_update
= bgp_router_id_update
;
2099 zclient
->interface_add
= bgp_interface_add
;
2100 zclient
->interface_delete
= bgp_interface_delete
;
2101 zclient
->interface_address_add
= bgp_interface_address_add
;
2102 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2103 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2104 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
2105 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2106 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2107 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2108 zclient
->interface_up
= bgp_interface_up
;
2109 zclient
->interface_down
= bgp_interface_down
;
2110 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2111 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2112 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2113 zclient
->import_check_update
= bgp_read_import_check_update
;
2115 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
2116 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);
2120 bgp_zebra_destroy(void)
2122 if (zclient
== NULL
)
2124 zclient_stop(zclient
);
2125 zclient_free(zclient
);