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"
33 #include "bgpd/bgpd.h"
34 #include "bgpd/bgp_route.h"
35 #include "bgpd/bgp_attr.h"
36 #include "bgpd/bgp_nexthop.h"
37 #include "bgpd/bgp_zebra.h"
38 #include "bgpd/bgp_fsm.h"
39 #include "bgpd/bgp_debug.h"
40 #include "bgpd/bgp_mpath.h"
41 #include "bgpd/bgp_nexthop.h"
43 /* All information about zebra. */
44 struct zclient
*zclient
= NULL
;
45 struct in_addr router_id_zebra
;
47 /* Growable buffer for nexthops sent to zebra */
48 struct stream
*bgp_nexthop_buf
= NULL
;
49 struct stream
*bgp_ifindices_buf
= NULL
;
51 /* These array buffers are used in making a copy of the attributes for
52 route-map apply. Arrays are being used here to minimize mallocs and
53 frees for the temporary copy of the attributes.
54 Given the zapi api expects the nexthop buffer to contain pointer to
55 pointers for nexthops, we couldnt have used a single nexthop variable
56 on the stack, hence we had two options:
57 1. maintain a linked-list and free it after zapi_*_route call
58 2. use an array to avoid number of mallocs.
59 Number of supported next-hops are finite, use of arrays should be ok. */
60 struct attr attr_cp
[BGP_MAXIMUM_MAXPATHS
];
61 struct attr_extra attr_extra_cp
[BGP_MAXIMUM_MAXPATHS
];
64 /* Once per address-family initialization of the attribute array */
65 #define BGP_INFO_ATTR_BUF_INIT()\
67 memset(attr_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr));\
68 memset(attr_extra_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr_extra));\
72 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
74 *info_dst = *info_src; \
75 assert(attr_index != BGP_MAXIMUM_MAXPATHS);\
76 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
77 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
78 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
79 info_dst->attr = &attr_cp[attr_index]; \
83 #define BGP_INFO_ATTR_BUF_FREE(info) \
85 bgp_attr_deep_free(info->attr); \
88 /* Router-id update message from zebra. */
90 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
)
92 struct prefix router_id
;
93 struct listnode
*node
, *nnode
;
96 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
98 if (BGP_DEBUG(zebra
, ZEBRA
))
101 prefix2str(&router_id
, buf
, sizeof(buf
));
102 zlog_debug("Zebra rcvd: router id update %s", buf
);
105 router_id_zebra
= router_id
.u
.prefix4
;
107 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
109 if (!bgp
->router_id_static
.s_addr
)
110 bgp_router_id_set (bgp
, &router_id
.u
.prefix4
);
116 /* Nexthop update message from zebra. */
118 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
121 bgp_parse_nexthop_update();
125 /* Inteface addition message from zebra. */
127 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
129 struct interface
*ifp
;
131 ifp
= zebra_interface_add_read (zclient
->ibuf
);
133 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
134 zlog_debug("Zebra rcvd: interface add %s", ifp
->name
);
140 bgp_interface_delete (int command
, struct zclient
*zclient
,
144 struct interface
*ifp
;
147 ifp
= zebra_interface_state_read (s
);
148 ifp
->ifindex
= IFINDEX_INTERNAL
;
150 if (BGP_DEBUG(zebra
, ZEBRA
))
151 zlog_debug("Zebra rcvd: interface delete %s", ifp
->name
);
157 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
)
160 struct interface
*ifp
;
162 struct listnode
*node
, *nnode
;
165 ifp
= zebra_interface_state_read (s
);
170 if (BGP_DEBUG(zebra
, ZEBRA
))
171 zlog_debug("Zebra rcvd: interface %s up", ifp
->name
);
173 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
174 bgp_connected_add (c
);
180 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
)
183 struct interface
*ifp
;
185 struct listnode
*node
, *nnode
;
188 ifp
= zebra_interface_state_read (s
);
192 if (BGP_DEBUG(zebra
, ZEBRA
))
193 zlog_debug("Zebra rcvd: interface %s down", ifp
->name
);
195 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
196 bgp_connected_delete (c
);
198 /* Fast external-failover */
200 struct listnode
*mnode
;
204 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
206 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
209 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
211 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
214 if (ifp
== peer
->nexthop
.ifp
)
215 BGP_EVENT_ADD (peer
, BGP_Stop
);
224 bgp_interface_address_add (int command
, struct zclient
*zclient
,
227 struct connected
*ifc
;
229 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
);
234 if (BGP_DEBUG(zebra
, ZEBRA
))
237 prefix2str(ifc
->address
, buf
, sizeof(buf
));
238 zlog_debug("Zebra rcvd: interface %s address add %s",
239 ifc
->ifp
->name
, buf
);
242 if (if_is_operative (ifc
->ifp
))
243 bgp_connected_add (ifc
);
249 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
252 struct connected
*ifc
;
254 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
);
259 if (BGP_DEBUG(zebra
, ZEBRA
))
262 prefix2str(ifc
->address
, buf
, sizeof(buf
));
263 zlog_debug("Zebra rcvd: interface %s address delete %s",
264 ifc
->ifp
->name
, buf
);
267 if (if_is_operative (ifc
->ifp
))
268 bgp_connected_delete (ifc
);
270 connected_free (ifc
);
275 /* Zebra route add and delete treatment. */
277 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
)
280 struct zapi_ipv4 api
;
281 struct in_addr nexthop
;
282 struct prefix_ipv4 p
;
287 /* Type, flags, message. */
288 api
.type
= stream_getc (s
);
289 api
.flags
= stream_getc (s
);
290 api
.message
= stream_getc (s
);
293 memset (&p
, 0, sizeof (struct prefix_ipv4
));
295 p
.prefixlen
= stream_getc (s
);
296 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
298 /* Nexthop, ifindex, distance, metric. */
299 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
301 api
.nexthop_num
= stream_getc (s
);
302 nexthop
.s_addr
= stream_get_ipv4 (s
);
304 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
306 api
.ifindex_num
= stream_getc (s
);
307 stream_getl (s
); /* ifindex, unused */
309 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
310 api
.distance
= stream_getc (s
);
311 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
312 api
.metric
= stream_getl (s
);
316 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
318 if (BGP_DEBUG(zebra
, ZEBRA
))
320 char buf
[2][INET_ADDRSTRLEN
];
321 zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u",
322 zebra_route_string(api
.type
),
323 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
325 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
328 bgp_redistribute_add((struct prefix
*)&p
, &nexthop
, NULL
,
329 api
.metric
, api
.type
);
333 if (BGP_DEBUG(zebra
, ZEBRA
))
335 char buf
[2][INET_ADDRSTRLEN
];
336 zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d "
337 "nexthop %s metric %u",
338 zebra_route_string(api
.type
),
339 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
341 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
344 bgp_redistribute_delete((struct prefix
*)&p
, api
.type
);
351 /* Zebra route add and delete treatment. */
353 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
)
356 struct zapi_ipv6 api
;
357 struct in6_addr nexthop
;
358 struct prefix_ipv6 p
;
361 memset (&nexthop
, 0, sizeof (struct in6_addr
));
363 /* Type, flags, message. */
364 api
.type
= stream_getc (s
);
365 api
.flags
= stream_getc (s
);
366 api
.message
= stream_getc (s
);
369 memset (&p
, 0, sizeof (struct prefix_ipv6
));
371 p
.prefixlen
= stream_getc (s
);
372 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
374 /* Nexthop, ifindex, distance, metric. */
375 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
377 api
.nexthop_num
= stream_getc (s
);
378 stream_get (&nexthop
, s
, 16);
380 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
382 api
.ifindex_num
= stream_getc (s
);
383 stream_getl (s
); /* ifindex, unused */
385 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
386 api
.distance
= stream_getc (s
);
389 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
390 api
.metric
= stream_getl (s
);
394 /* Simply ignore link-local address. */
395 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
398 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
400 if (BGP_DEBUG(zebra
, ZEBRA
))
402 char buf
[2][INET6_ADDRSTRLEN
];
403 zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u",
404 zebra_route_string(api
.type
),
405 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
407 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
410 bgp_redistribute_add ((struct prefix
*)&p
, NULL
, &nexthop
,
411 api
.metric
, api
.type
);
415 if (BGP_DEBUG(zebra
, ZEBRA
))
417 char buf
[2][INET6_ADDRSTRLEN
];
418 zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
419 "nexthop %s metric %u",
420 zebra_route_string(api
.type
),
421 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
423 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
426 bgp_redistribute_delete ((struct prefix
*) &p
, api
.type
);
431 #endif /* HAVE_IPV6 */
434 if_lookup_by_ipv4 (struct in_addr
*addr
)
436 struct listnode
*ifnode
;
437 struct listnode
*cnode
;
438 struct interface
*ifp
;
439 struct connected
*connected
;
440 struct prefix_ipv4 p
;
445 p
.prefixlen
= IPV4_MAX_BITLEN
;
447 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
449 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
451 cp
= connected
->address
;
453 if (cp
->family
== AF_INET
)
454 if (prefix_match (cp
, (struct prefix
*)&p
))
462 if_lookup_by_ipv4_exact (struct in_addr
*addr
)
464 struct listnode
*ifnode
;
465 struct listnode
*cnode
;
466 struct interface
*ifp
;
467 struct connected
*connected
;
470 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
472 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
474 cp
= connected
->address
;
476 if (cp
->family
== AF_INET
)
477 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
486 if_lookup_by_ipv6 (struct in6_addr
*addr
)
488 struct listnode
*ifnode
;
489 struct listnode
*cnode
;
490 struct interface
*ifp
;
491 struct connected
*connected
;
492 struct prefix_ipv6 p
;
497 p
.prefixlen
= IPV6_MAX_BITLEN
;
499 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
501 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
503 cp
= connected
->address
;
505 if (cp
->family
== AF_INET6
)
506 if (prefix_match (cp
, (struct prefix
*)&p
))
514 if_lookup_by_ipv6_exact (struct in6_addr
*addr
)
516 struct listnode
*ifnode
;
517 struct listnode
*cnode
;
518 struct interface
*ifp
;
519 struct connected
*connected
;
522 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
524 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
526 cp
= connected
->address
;
528 if (cp
->family
== AF_INET6
)
529 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
537 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
539 struct listnode
*cnode
;
540 struct connected
*connected
;
543 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
545 cp
= connected
->address
;
547 if (cp
->family
== AF_INET6
)
548 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
550 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
558 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
560 struct listnode
*cnode
;
561 struct connected
*connected
;
564 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
566 cp
= connected
->address
;
568 if (cp
->family
== AF_INET6
)
569 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
571 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
577 #endif /* HAVE_IPV6 */
580 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
582 struct listnode
*cnode
;
583 struct connected
*connected
;
586 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
588 cp
= connected
->address
;
589 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
591 *addr
= cp
->u
.prefix4
;
599 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
600 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
603 struct interface
*ifp
= NULL
;
605 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
612 if (local
->sa
.sa_family
== AF_INET
)
614 nexthop
->v4
= local
->sin
.sin_addr
;
615 ifp
= if_lookup_by_ipv4 (&local
->sin
.sin_addr
);
618 if (local
->sa
.sa_family
== AF_INET6
)
620 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
623 ifp
= if_lookup_by_index (if_nametoindex (peer
->ifname
));
626 ifp
= if_lookup_by_ipv6 (&local
->sin6
.sin6_addr
);
628 #endif /* HAVE_IPV6 */
635 /* IPv4 connection. */
636 if (local
->sa
.sa_family
== AF_INET
)
640 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
642 /* There is no global nexthop. */
644 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
646 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
647 #endif /* HAVE_IPV6 */
651 /* IPv6 connection. */
652 if (local
->sa
.sa_family
== AF_INET6
)
654 struct interface
*direct
= NULL
;
657 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
658 if (!ret
&& peer
->local_id
.s_addr
)
659 nexthop
->v4
= peer
->local_id
;
662 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
664 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
667 /* If directory connected set link-local address. */
668 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
);
670 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
673 /* Link-local address. */
675 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
677 /* If there is no global address. Set link-local address as
678 global. I know this break RFC specification... */
680 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
683 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
688 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
689 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
))
690 peer
->shared_network
= 1;
692 peer
->shared_network
= 0;
694 /* KAME stack specific treatment. */
696 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
697 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
699 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
701 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
702 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
704 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
707 #endif /* HAVE_IPV6 */
711 static struct in6_addr
*
712 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
714 struct in6_addr
*nexthop
= NULL
;
716 /* Only global address nexthop exists. */
717 if (info
->attr
->extra
->mp_nexthop_len
== 16)
718 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
720 /* If both global and link-local address present. */
721 if (info
->attr
->extra
->mp_nexthop_len
== 32)
723 /* Workaround for Cisco's nexthop bug. */
724 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
725 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
726 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
728 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
735 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
736 struct bgp_info
*info
)
738 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
741 if (BGP_DEBUG(zebra
, ZEBRA
))
743 if (p
->family
== AF_INET
)
745 char buf
[2][INET_ADDRSTRLEN
];
746 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
747 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
749 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
752 if (p
->family
== AF_INET6
)
754 char buf
[2][INET6_ADDRSTRLEN
];
755 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
756 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
758 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
766 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
767 afi_t afi
, safi_t safi
)
772 struct bgp_info
*mpinfo
;
773 size_t oldsize
, newsize
;
774 u_int32_t nhcount
, metric
;
775 struct bgp_info local_info
;
776 struct bgp_info
*info_cp
= &local_info
;
778 if (zclient
->sock
< 0)
781 if (! zclient
->redist
[ZEBRA_ROUTE_BGP
])
787 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
)
789 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
790 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
793 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
794 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
))
795 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
797 nhcount
= 1 + bgp_info_mpath_count (info
);
799 if (p
->family
== AF_INET
)
801 struct zapi_ipv4 api
;
802 struct in_addr
*nexthop
;
803 char buf
[2][INET_ADDRSTRLEN
];
804 int valid_nh_count
= 0;
806 /* resize nexthop buffer size if necessary */
807 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
808 (sizeof (struct in_addr
*) * nhcount
))
810 newsize
= (sizeof (struct in_addr
*) * nhcount
);
811 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
812 if (newsize
== oldsize
)
814 zlog_err ("can't resize nexthop buffer");
818 stream_reset (bgp_nexthop_buf
);
821 /* Metric is currently based on the best-path only. */
822 metric
= info
->attr
->med
;
824 if (bgp
->table_map
[afi
][safi
].name
)
826 BGP_INFO_ATTR_BUF_INIT();
828 /* Copy info and attributes, so the route-map apply doesn't modify the
830 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
831 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
833 metric
= info_cp
->attr
->med
;
834 nexthop
= &info_cp
->attr
->nexthop
;
836 BGP_INFO_ATTR_BUF_FREE(info_cp
);
840 nexthop
= &info
->attr
->nexthop
;
845 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
849 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
850 mpinfo
= bgp_info_mpath_next (mpinfo
))
854 if (bgp
->table_map
[afi
][safi
].name
)
856 /* Copy info and attributes, so the route-map apply doesn't modify the
858 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
859 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
860 nexthop
= &info_cp
->attr
->nexthop
;
861 BGP_INFO_ATTR_BUF_FREE(info_cp
);
865 nexthop
= &mpinfo
->attr
->nexthop
;
871 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
876 api
.type
= ZEBRA_ROUTE_BGP
;
879 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
880 api
.nexthop_num
= valid_nh_count
;
881 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
883 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
886 distance
= bgp_distance_apply (p
, info
, bgp
);
890 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
891 api
.distance
= distance
;
894 if (BGP_DEBUG(zebra
, ZEBRA
))
897 zlog_debug("Zebra send: IPv4 route %s %s/%d metric %u"
898 " count %d", (valid_nh_count
? "add":"delete"),
899 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
900 p
->prefixlen
, api
.metric
, api
.nexthop_num
);
901 for (i
= 0; i
< api
.nexthop_num
; i
++)
902 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
903 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
906 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
907 zclient
, (struct prefix_ipv4
*) p
, &api
);
911 /* We have to think about a IPv6 link-local address curse. */
912 if (p
->family
== AF_INET6
)
914 unsigned int ifindex
;
915 struct in6_addr
*nexthop
;
916 struct zapi_ipv6 api
;
917 int valid_nh_count
= 0;
918 char buf
[2][INET6_ADDRSTRLEN
];
920 /* resize nexthop buffer size if necessary */
921 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
922 (sizeof (struct in6_addr
*) * nhcount
))
924 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
925 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
926 if (newsize
== oldsize
)
928 zlog_err ("can't resize nexthop buffer");
932 stream_reset (bgp_nexthop_buf
);
934 /* resize ifindices buffer size if necessary */
935 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
936 (sizeof (unsigned int) * nhcount
))
938 newsize
= (sizeof (unsigned int) * nhcount
);
939 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
940 if (newsize
== oldsize
)
942 zlog_err ("can't resize nexthop buffer");
946 stream_reset (bgp_ifindices_buf
);
951 assert (info
->attr
->extra
);
953 /* Metric is currently based on the best-path only. */
954 metric
= info
->attr
->med
;
956 if (bgp
->table_map
[afi
][safi
].name
)
958 BGP_INFO_ATTR_BUF_INIT();
960 /* Copy info and attributes, so the route-map apply doesn't modify the
962 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
963 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
965 metric
= info_cp
->attr
->med
;
966 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
968 BGP_INFO_ATTR_BUF_FREE(info_cp
);
972 nexthop
= bgp_info_to_ipv6_nexthop(info
);
977 if (info
->attr
->extra
->mp_nexthop_len
== 32)
978 if (info
->peer
->nexthop
.ifp
)
979 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
982 if (info
->peer
->ifname
)
983 ifindex
= if_nametoindex (info
->peer
->ifname
);
984 else if (info
->peer
->nexthop
.ifp
)
985 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
987 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
988 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
992 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
993 mpinfo
= bgp_info_mpath_next (mpinfo
))
998 if (bgp
->table_map
[afi
][safi
].name
)
1000 /* Copy info and attributes, so the route-map apply doesn't modify the
1002 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1003 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1004 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1005 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1009 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1012 if (nexthop
== NULL
)
1015 if (mpinfo
->attr
->extra
->mp_nexthop_len
== 32)
1016 if (mpinfo
->peer
->nexthop
.ifp
)
1017 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1020 if (mpinfo
->peer
->ifname
)
1021 ifindex
= if_nametoindex (mpinfo
->peer
->ifname
);
1022 else if (mpinfo
->peer
->nexthop
.ifp
)
1023 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1028 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1029 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1033 /* Make Zebra API structure. */
1035 api
.type
= ZEBRA_ROUTE_BGP
;
1038 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1039 api
.nexthop_num
= valid_nh_count
;
1040 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1041 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1042 api
.ifindex_num
= valid_nh_count
;
1043 api
.ifindex
= (unsigned int *)STREAM_DATA (bgp_ifindices_buf
);
1044 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1045 api
.metric
= metric
;
1047 if (BGP_DEBUG(zebra
, ZEBRA
))
1050 zlog_debug("Zebra send: IPv6 route %s %s/%d metric %u",
1051 valid_nh_count
? "add" : "delete",
1052 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1053 p
->prefixlen
, api
.metric
);
1054 for (i
= 0; i
< api
.nexthop_num
; i
++)
1055 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1056 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1059 zapi_ipv6_route (valid_nh_count
? ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1060 zclient
, (struct prefix_ipv6
*) p
, &api
);
1062 #endif /* HAVE_IPV6 */
1065 /* Announce all routes of a table to zebra */
1067 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1069 struct bgp_node
*rn
;
1070 struct bgp_table
*table
;
1071 struct bgp_info
*ri
;
1073 table
= bgp
->rib
[afi
][safi
];
1075 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1076 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1077 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1078 && ri
->type
== ZEBRA_ROUTE_BGP
1079 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1080 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1084 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1089 if (zclient
->sock
< 0)
1092 if (! zclient
->redist
[ZEBRA_ROUTE_BGP
])
1098 if (peer
->sort
== BGP_PEER_IBGP
)
1100 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1101 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1104 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1105 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
))
1106 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1108 if (p
->family
== AF_INET
)
1110 struct zapi_ipv4 api
;
1111 struct in_addr
*nexthop
;
1114 nexthop
= &info
->attr
->nexthop
;
1116 api
.type
= ZEBRA_ROUTE_BGP
;
1119 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1120 api
.nexthop_num
= 1;
1121 api
.nexthop
= &nexthop
;
1122 api
.ifindex_num
= 0;
1123 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1124 api
.metric
= info
->attr
->med
;
1126 if (BGP_DEBUG(zebra
, ZEBRA
))
1128 char buf
[2][INET_ADDRSTRLEN
];
1129 zlog_debug("Zebra send: IPv4 route delete %s/%d nexthop %s metric %u",
1130 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1132 inet_ntop(AF_INET
, nexthop
, buf
[1], sizeof(buf
[1])),
1136 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1137 (struct prefix_ipv4
*) p
, &api
);
1140 /* We have to think about a IPv6 link-local address curse. */
1141 if (p
->family
== AF_INET6
)
1143 struct zapi_ipv6 api
;
1144 unsigned int ifindex
;
1145 struct in6_addr
*nexthop
;
1147 assert (info
->attr
->extra
);
1152 /* Only global address nexthop exists. */
1153 if (info
->attr
->extra
->mp_nexthop_len
== 16)
1154 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1156 /* If both global and link-local address present. */
1157 if (info
->attr
->extra
->mp_nexthop_len
== 32)
1159 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1160 if (info
->peer
->nexthop
.ifp
)
1161 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1164 if (nexthop
== NULL
)
1167 if (IN6_IS_ADDR_LINKLOCAL (nexthop
) && ! ifindex
)
1168 if (info
->peer
->ifname
)
1169 ifindex
= if_nametoindex (info
->peer
->ifname
);
1172 api
.type
= ZEBRA_ROUTE_BGP
;
1175 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1176 api
.nexthop_num
= 1;
1177 api
.nexthop
= &nexthop
;
1178 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1179 api
.ifindex_num
= 1;
1180 api
.ifindex
= &ifindex
;
1181 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1182 api
.metric
= info
->attr
->med
;
1184 if (BGP_DEBUG(zebra
, ZEBRA
))
1186 char buf
[2][INET6_ADDRSTRLEN
];
1187 zlog_debug("Zebra send: IPv6 route delete %s/%d nexthop %s metric %u",
1188 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1190 inet_ntop(AF_INET6
, nexthop
, buf
[1], sizeof(buf
[1])),
1194 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1195 (struct prefix_ipv6
*) p
, &api
);
1197 #endif /* HAVE_IPV6 */
1200 /* Other routes redistribution into BGP. */
1202 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
)
1204 /* Set flag to BGP instance. */
1205 bgp
->redist
[afi
][type
] = 1;
1207 /* Return if already redistribute flag is set. */
1208 if (zclient
->redist
[type
])
1211 zclient
->redist
[type
] = 1;
1213 /* Return if zebra connection is not established. */
1214 if (zclient
->sock
< 0)
1217 if (BGP_DEBUG(zebra
, ZEBRA
))
1218 zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type
));
1220 /* Send distribute add message to zebra. */
1221 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
1226 /* Redistribute with route-map specification. */
1228 bgp_redistribute_rmap_set (struct bgp
*bgp
, afi_t afi
, int type
,
1231 if (bgp
->rmap
[afi
][type
].name
1232 && (strcmp (bgp
->rmap
[afi
][type
].name
, name
) == 0))
1235 if (bgp
->rmap
[afi
][type
].name
)
1236 free (bgp
->rmap
[afi
][type
].name
);
1237 bgp
->rmap
[afi
][type
].name
= strdup (name
);
1238 bgp
->rmap
[afi
][type
].map
= route_map_lookup_by_name (name
);
1243 /* Redistribute with metric specification. */
1245 bgp_redistribute_metric_set (struct bgp
*bgp
, afi_t afi
, int type
,
1248 if (bgp
->redist_metric_flag
[afi
][type
]
1249 && bgp
->redist_metric
[afi
][type
] == metric
)
1252 bgp
->redist_metric_flag
[afi
][type
] = 1;
1253 bgp
->redist_metric
[afi
][type
] = metric
;
1258 /* Unset redistribution. */
1260 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
)
1262 /* Unset flag from BGP instance. */
1263 bgp
->redist
[afi
][type
] = 0;
1265 /* Unset route-map. */
1266 if (bgp
->rmap
[afi
][type
].name
)
1267 free (bgp
->rmap
[afi
][type
].name
);
1268 bgp
->rmap
[afi
][type
].name
= NULL
;
1269 bgp
->rmap
[afi
][type
].map
= NULL
;
1272 bgp
->redist_metric_flag
[afi
][type
] = 0;
1273 bgp
->redist_metric
[afi
][type
] = 0;
1275 /* Return if zebra connection is disabled. */
1276 if (! zclient
->redist
[type
])
1278 zclient
->redist
[type
] = 0;
1280 if (bgp
->redist
[AFI_IP
][type
] == 0
1281 && bgp
->redist
[AFI_IP6
][type
] == 0
1282 && zclient
->sock
>= 0)
1284 /* Send distribute delete message to zebra. */
1285 if (BGP_DEBUG(zebra
, ZEBRA
))
1286 zlog_debug("Zebra send: redistribute delete %s",
1287 zebra_route_string(type
));
1288 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
1291 /* Withdraw redistributed routes from current BGP's routing table. */
1292 bgp_redistribute_withdraw (bgp
, afi
, type
);
1297 /* Unset redistribution route-map configuration. */
1299 bgp_redistribute_routemap_unset (struct bgp
*bgp
, afi_t afi
, int type
)
1301 if (! bgp
->rmap
[afi
][type
].name
)
1304 /* Unset route-map. */
1305 free (bgp
->rmap
[afi
][type
].name
);
1306 bgp
->rmap
[afi
][type
].name
= NULL
;
1307 bgp
->rmap
[afi
][type
].map
= NULL
;
1312 /* Unset redistribution metric configuration. */
1314 bgp_redistribute_metric_unset (struct bgp
*bgp
, afi_t afi
, int type
)
1316 if (! bgp
->redist_metric_flag
[afi
][type
])
1320 bgp
->redist_metric_flag
[afi
][type
] = 0;
1321 bgp
->redist_metric
[afi
][type
] = 0;
1327 bgp_zclient_reset (void)
1329 zclient_reset (zclient
);
1333 bgp_zebra_init (void)
1335 /* Set default values. */
1336 zclient
= zclient_new ();
1337 zclient_init (zclient
, ZEBRA_ROUTE_BGP
);
1338 zclient
->router_id_update
= bgp_router_id_update
;
1339 zclient
->interface_add
= bgp_interface_add
;
1340 zclient
->interface_delete
= bgp_interface_delete
;
1341 zclient
->interface_address_add
= bgp_interface_address_add
;
1342 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1343 zclient
->ipv4_route_add
= zebra_read_ipv4
;
1344 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
1345 zclient
->interface_up
= bgp_interface_up
;
1346 zclient
->interface_down
= bgp_interface_down
;
1348 zclient
->ipv6_route_add
= zebra_read_ipv6
;
1349 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
1350 #endif /* HAVE_IPV6 */
1351 zclient
->nexthop_update
= bgp_read_nexthop_update
;
1353 /* Interface related init. */
1356 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
1357 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);