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"
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_route.h"
36 #include "bgpd/bgp_attr.h"
37 #include "bgpd/bgp_nexthop.h"
38 #include "bgpd/bgp_zebra.h"
39 #include "bgpd/bgp_fsm.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_mpath.h"
42 #include "bgpd/bgp_nexthop.h"
43 #include "bgpd/bgp_nht.h"
45 /* All information about zebra. */
46 struct zclient *zclient = NULL;
47 struct in_addr router_id_zebra;
49 /* Growable buffer for nexthops sent to zebra */
50 struct stream *bgp_nexthop_buf = NULL;
51 struct stream *bgp_ifindices_buf = NULL;
53 int zclient_num_connects;
55 /* Router-id update message from zebra. */
57 bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
60 struct prefix router_id;
62 zebra_router_id_update_read(zclient->ibuf,&router_id);
64 if (BGP_DEBUG(zebra, ZEBRA))
67 prefix2str(&router_id, buf, sizeof(buf));
68 zlog_debug("Zebra rcvd: router id update %s", buf);
71 router_id_zebra = router_id.u.prefix4;
73 bgp_router_id_zebra_bump ();
77 /* Nexthop update message from zebra. */
79 bgp_read_nexthop_update (int command, struct zclient *zclient,
80 zebra_size_t length, vrf_id_t vrf_id)
82 bgp_parse_nexthop_update();
86 /* Inteface addition message from zebra. */
88 bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
91 struct interface *ifp;
93 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
95 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
96 zlog_debug("Zebra rcvd: interface add %s", ifp->name);
102 bgp_interface_delete (int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
106 struct interface *ifp;
109 ifp = zebra_interface_state_read (s, vrf_id);
113 ifp->ifindex = IFINDEX_INTERNAL;
115 if (BGP_DEBUG(zebra, ZEBRA))
116 zlog_debug("Zebra rcvd: interface delete %s", ifp->name);
122 bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
126 struct interface *ifp;
128 struct listnode *node, *nnode;
131 ifp = zebra_interface_state_read (s, vrf_id);
136 if (BGP_DEBUG(zebra, ZEBRA))
137 zlog_debug("Zebra rcvd: interface %s up", ifp->name);
139 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
140 bgp_connected_add (c);
146 bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
150 struct interface *ifp;
152 struct listnode *node, *nnode;
155 ifp = zebra_interface_state_read (s, vrf_id);
159 if (BGP_DEBUG(zebra, ZEBRA))
160 zlog_debug("Zebra rcvd: interface %s down", ifp->name);
162 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
163 bgp_connected_delete (c);
165 /* Fast external-failover */
167 struct listnode *mnode;
171 for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
173 if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
176 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
178 if (peer->gtsm_hops != 1 && peer_ttl (peer) != 1)
180 if (ifp == peer->nexthop.ifp)
181 BGP_EVENT_ADD (peer, BGP_Stop);
190 bgp_interface_address_add (int command, struct zclient *zclient,
191 zebra_size_t length, vrf_id_t vrf_id)
193 struct connected *ifc;
195 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
200 if (BGP_DEBUG(zebra, ZEBRA))
203 prefix2str(ifc->address, buf, sizeof(buf));
204 zlog_debug("Zebra rcvd: interface %s address add %s",
205 ifc->ifp->name, buf);
208 if (if_is_operative (ifc->ifp))
209 bgp_connected_add (ifc);
215 bgp_interface_address_delete (int command, struct zclient *zclient,
216 zebra_size_t length, vrf_id_t vrf_id)
218 struct connected *ifc;
220 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
225 if (BGP_DEBUG(zebra, ZEBRA))
228 prefix2str(ifc->address, buf, sizeof(buf));
229 zlog_debug("Zebra rcvd: interface %s address delete %s",
230 ifc->ifp->name, buf);
233 if (if_is_operative (ifc->ifp))
234 bgp_connected_delete (ifc);
236 connected_free (ifc);
241 /* Zebra route add and delete treatment. */
243 zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
247 struct zapi_ipv4 api;
248 struct in_addr nexthop;
249 struct prefix_ipv4 p;
250 unsigned char plength = 0;
255 /* Type, flags, message. */
256 api.type = stream_getc (s);
257 api.flags = stream_getc (s);
258 api.message = stream_getc (s);
261 memset (&p, 0, sizeof (struct prefix_ipv4));
263 plength = stream_getc (s);
264 p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
265 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
267 /* Nexthop, ifindex, distance, metric. */
268 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
270 api.nexthop_num = stream_getc (s);
271 nexthop.s_addr = stream_get_ipv4 (s);
273 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
275 api.ifindex_num = stream_getc (s);
276 stream_getl (s); /* ifindex, unused */
278 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
279 api.distance = stream_getc (s);
280 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
281 api.metric = stream_getl (s);
285 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
286 api.tag = stream_getl (s);
290 if (command == ZEBRA_IPV4_ROUTE_ADD)
292 if (BGP_DEBUG(zebra, ZEBRA))
294 char buf[2][INET_ADDRSTRLEN];
295 zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u tag %d",
296 zebra_route_string(api.type),
297 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
299 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
303 bgp_redistribute_add ((struct prefix *)&p, &nexthop, NULL,
304 api.metric, api.type, api.tag);
308 if (BGP_DEBUG(zebra, ZEBRA))
310 char buf[2][INET_ADDRSTRLEN];
311 zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d "
312 "nexthop %s metric %u tag %d",
313 zebra_route_string(api.type),
314 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
316 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
320 bgp_redistribute_delete((struct prefix *)&p, api.type);
326 /* Zebra route add and delete treatment. */
328 zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
332 struct zapi_ipv6 api;
333 struct in6_addr nexthop;
334 struct prefix_ipv6 p;
335 unsigned char plength = 0;
338 memset (&nexthop, 0, sizeof (struct in6_addr));
340 /* Type, flags, message. */
341 api.type = stream_getc (s);
342 api.flags = stream_getc (s);
343 api.message = stream_getc (s);
346 memset (&p, 0, sizeof (struct prefix_ipv6));
348 plength = stream_getc (s);
349 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
350 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
352 /* Nexthop, ifindex, distance, metric. */
353 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
355 api.nexthop_num = stream_getc (s);
356 stream_get (&nexthop, s, 16);
358 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
360 api.ifindex_num = stream_getc (s);
361 stream_getl (s); /* ifindex, unused */
363 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
364 api.distance = stream_getc (s);
367 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
368 api.metric = stream_getl (s);
372 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
373 api.tag = stream_getl (s);
377 /* Simply ignore link-local address. */
378 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
381 if (command == ZEBRA_IPV6_ROUTE_ADD)
383 if (BGP_DEBUG(zebra, ZEBRA))
385 char buf[2][INET6_ADDRSTRLEN];
386 zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u tag %d",
387 zebra_route_string(api.type),
388 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
390 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
394 bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop,
395 api.metric, api.type, api.tag);
399 if (BGP_DEBUG(zebra, ZEBRA))
401 char buf[2][INET6_ADDRSTRLEN];
402 zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
403 "nexthop %s metric %u tag %d",
404 zebra_route_string(api.type),
405 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
407 inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
411 bgp_redistribute_delete ((struct prefix *) &p, api.type);
418 if_lookup_by_ipv4 (struct in_addr *addr)
420 struct listnode *ifnode;
421 struct listnode *cnode;
422 struct interface *ifp;
423 struct connected *connected;
424 struct prefix_ipv4 p;
429 p.prefixlen = IPV4_MAX_BITLEN;
431 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
433 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
435 cp = connected->address;
437 if (cp->family == AF_INET)
438 if (prefix_match (cp, (struct prefix *)&p))
446 if_lookup_by_ipv4_exact (struct in_addr *addr)
448 struct listnode *ifnode;
449 struct listnode *cnode;
450 struct interface *ifp;
451 struct connected *connected;
454 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
456 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
458 cp = connected->address;
460 if (cp->family == AF_INET)
461 if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
469 if_lookup_by_ipv6 (struct in6_addr *addr)
471 struct listnode *ifnode;
472 struct listnode *cnode;
473 struct interface *ifp;
474 struct connected *connected;
475 struct prefix_ipv6 p;
480 p.prefixlen = IPV6_MAX_BITLEN;
482 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
484 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
486 cp = connected->address;
488 if (cp->family == AF_INET6)
489 if (prefix_match (cp, (struct prefix *)&p))
497 if_lookup_by_ipv6_exact (struct in6_addr *addr)
499 struct listnode *ifnode;
500 struct listnode *cnode;
501 struct interface *ifp;
502 struct connected *connected;
505 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
507 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
509 cp = connected->address;
511 if (cp->family == AF_INET6)
512 if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
520 if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
522 struct listnode *cnode;
523 struct connected *connected;
526 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
528 cp = connected->address;
530 if (cp->family == AF_INET6)
531 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
533 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
541 if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
543 struct listnode *cnode;
544 struct connected *connected;
547 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
549 cp = connected->address;
551 if (cp->family == AF_INET6)
552 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
554 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
562 if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
564 struct listnode *cnode;
565 struct connected *connected;
568 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
570 cp = connected->address;
571 if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
573 *addr = cp->u.prefix4;
581 bgp_nexthop_set (union sockunion *local, union sockunion *remote,
582 struct bgp_nexthop *nexthop, struct peer *peer)
585 struct interface *ifp = NULL;
587 memset (nexthop, 0, sizeof (struct bgp_nexthop));
594 if (local->sa.sa_family == AF_INET)
596 nexthop->v4 = local->sin.sin_addr;
598 ifp = if_lookup_by_name (peer->update_if);
600 ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr);
602 if (local->sa.sa_family == AF_INET6)
604 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
607 ifp = if_lookup_by_name (peer->ifname);
609 else if (peer->update_if)
610 ifp = if_lookup_by_name (peer->update_if);
612 ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr);
620 /* IPv4 connection. */
621 if (local->sa.sa_family == AF_INET)
624 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
626 /* There is no global nexthop. */
628 if_get_ipv6_local (ifp, &nexthop->v6_global);
630 if_get_ipv6_local (ifp, &nexthop->v6_local);
633 /* IPv6 connection. */
634 if (local->sa.sa_family == AF_INET6)
636 struct interface *direct = NULL;
639 ret = if_get_ipv4_address(ifp, &nexthop->v4);
640 if (!ret && peer->local_id.s_addr)
641 nexthop->v4 = peer->local_id;
644 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
646 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
649 /* If directory connected set link-local address. */
650 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr);
652 if_get_ipv6_local (ifp, &nexthop->v6_local);
655 /* Link-local address. */
657 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
659 /* If there is no global address. Set link-local address as
660 global. I know this break RFC specification... */
662 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
665 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
670 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
671 if_lookup_by_ipv6 (&remote->sin6.sin6_addr))
672 peer->shared_network = 1;
674 peer->shared_network = 0;
676 /* KAME stack specific treatment. */
678 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
679 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
681 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
683 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
684 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
686 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
693 bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi)
698 struct bgp_info *mpinfo;
699 size_t oldsize, newsize;
703 if (zclient->sock < 0)
706 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
712 if ((info->attr->extra) && (info->attr->extra->tag != 0))
713 tag = info->attr->extra->tag;
715 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
717 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
718 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
721 if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
722 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
723 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
725 nhcount = 1 + bgp_info_mpath_count (info);
727 if (p->family == AF_INET)
729 struct zapi_ipv4 api;
730 struct in_addr *nexthop;
732 /* resize nexthop buffer size if necessary */
733 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
734 (sizeof (struct in_addr *) * nhcount))
736 newsize = (sizeof (struct in_addr *) * nhcount);
737 newsize = stream_resize (bgp_nexthop_buf, newsize);
738 if (newsize == oldsize)
740 zlog_err ("can't resize nexthop buffer");
744 stream_reset (bgp_nexthop_buf);
746 api.vrf_id = VRF_DEFAULT;
748 nexthop = &info->attr->nexthop;
749 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
750 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
751 mpinfo = bgp_info_mpath_next (mpinfo))
753 nexthop = &mpinfo->attr->nexthop;
754 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
757 api.type = ZEBRA_ROUTE_BGP;
760 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
761 api.nexthop_num = nhcount;
762 api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
764 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
765 api.metric = info->attr->med;
769 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
773 distance = bgp_distance_apply (p, info, bgp);
777 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
778 api.distance = distance;
781 if (BGP_DEBUG(zebra, ZEBRA))
784 char buf[2][INET_ADDRSTRLEN];
785 zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u"
787 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
789 inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])),
790 api.metric, api.tag, api.nexthop_num);
791 for (i = 1; i < api.nexthop_num; i++)
792 zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s",
793 i, inet_ntop(AF_INET, api.nexthop[i], buf[1],
797 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient,
798 (struct prefix_ipv4 *) p, &api);
801 /* We have to think about a IPv6 link-local address curse. */
802 if (p->family == AF_INET6)
805 struct in6_addr *nexthop;
806 struct zapi_ipv6 api;
807 int valid_nh_count = 0;
809 /* resize nexthop buffer size if necessary */
810 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
811 (sizeof (struct in6_addr *) * nhcount))
813 newsize = (sizeof (struct in6_addr *) * nhcount);
814 newsize = stream_resize (bgp_nexthop_buf, newsize);
815 if (newsize == oldsize)
817 zlog_err ("can't resize nexthop buffer");
821 stream_reset (bgp_nexthop_buf);
823 /* resize ifindices buffer size if necessary */
824 if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
825 (sizeof (unsigned int) * nhcount))
827 newsize = (sizeof (unsigned int) * nhcount);
828 newsize = stream_resize (bgp_ifindices_buf, newsize);
829 if (newsize == oldsize)
831 zlog_err ("can't resize nexthop buffer");
835 stream_reset (bgp_ifindices_buf);
840 assert (info->attr->extra);
842 /* Only global address nexthop exists. */
843 if (info->attr->extra->mp_nexthop_len == 16)
844 nexthop = &info->attr->extra->mp_nexthop_global;
846 /* If both global and link-local address present. */
847 if (info->attr->extra->mp_nexthop_len == 32)
849 /* Workaround for Cisco's nexthop bug. */
850 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
851 && peer->su_remote->sa.sa_family == AF_INET6)
852 nexthop = &peer->su_remote->sin6.sin6_addr;
854 nexthop = &info->attr->extra->mp_nexthop_local;
856 if (info->peer->nexthop.ifp)
857 ifindex = info->peer->nexthop.ifp->ifindex;
865 if (info->peer->ifname)
866 ifindex = ifname2ifindex (info->peer->ifname);
867 else if (info->peer->nexthop.ifp)
868 ifindex = info->peer->nexthop.ifp->ifindex;
870 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
871 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
874 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
875 mpinfo = bgp_info_mpath_next (mpinfo))
879 /* Only global address nexthop exists. */
880 if (mpinfo->attr->extra->mp_nexthop_len == 16)
881 nexthop = &mpinfo->attr->extra->mp_nexthop_global;
883 /* If both global and link-local address present. */
884 if (mpinfo->attr->extra->mp_nexthop_len == 32)
886 /* Workaround for Cisco's nexthop bug. */
887 if (IN6_IS_ADDR_UNSPECIFIED (&mpinfo->attr->extra->mp_nexthop_global)
888 && mpinfo->peer->su_remote->sa.sa_family == AF_INET6)
890 nexthop = &mpinfo->peer->su_remote->sin6.sin6_addr;
894 nexthop = &mpinfo->attr->extra->mp_nexthop_local;
897 if (mpinfo->peer->nexthop.ifp)
899 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
910 if (mpinfo->peer->ifname)
912 ifindex = if_nametoindex (mpinfo->peer->ifname);
914 else if (mpinfo->peer->nexthop.ifp)
916 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
925 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
926 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
930 /* Make Zebra API structure. */
931 api.vrf_id = VRF_DEFAULT;
933 api.type = ZEBRA_ROUTE_BGP;
936 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
937 api.nexthop_num = valid_nh_count;
938 api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
939 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
940 api.ifindex_num = valid_nh_count;
941 api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf);
942 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
943 api.metric = info->attr->med;
945 distance = ipv6_bgp_distance_apply (p, info, bgp);
949 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
950 api.distance = distance;
955 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
959 if (BGP_DEBUG(zebra, ZEBRA))
961 char buf[2][INET6_ADDRSTRLEN];
962 zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u"
964 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
966 inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])),
967 api.metric, api.tag);
970 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient,
971 (struct prefix_ipv6 *) p, &api);
976 bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
981 if (zclient->sock < 0)
984 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
990 if (peer->sort == BGP_PEER_IBGP)
992 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
993 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
996 if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
997 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
998 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1000 if (p->family == AF_INET)
1002 struct zapi_ipv4 api;
1004 api.vrf_id = VRF_DEFAULT;
1007 api.type = ZEBRA_ROUTE_BGP;
1010 api.nexthop_num = 0;
1011 api.ifindex_num = 0;
1012 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1013 api.metric = info->attr->med;
1015 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1017 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1018 api.tag = info->attr->extra->tag;
1021 if (BGP_DEBUG(zebra, ZEBRA))
1023 char buf[2][INET_ADDRSTRLEN];
1024 zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u tag %d",
1025 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1031 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
1032 (struct prefix_ipv4 *) p, &api);
1035 /* We have to think about a IPv6 link-local address curse. */
1036 if (p->family == AF_INET6)
1038 struct zapi_ipv6 api;
1040 api.vrf_id = VRF_DEFAULT;
1042 api.type = ZEBRA_ROUTE_BGP;
1045 api.nexthop_num = 0;
1046 api.ifindex_num = 0;
1047 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1048 api.metric = info->attr->med;
1050 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1052 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1053 api.tag = info->attr->extra->tag;
1056 if (BGP_DEBUG(zebra, ZEBRA))
1058 char buf[2][INET6_ADDRSTRLEN];
1059 zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u tag %d",
1060 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1066 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
1067 (struct prefix_ipv6 *) p, &api);
1071 /* Other routes redistribution into BGP. */
1073 bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type)
1075 /* Set flag to BGP instance. */
1076 bgp->redist[afi][type] = 1;
1078 /* Return if already redistribute flag is set. */
1079 if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
1082 vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
1084 /* Return if zebra connection is not established. */
1085 if (zclient->sock < 0)
1088 if (BGP_DEBUG(zebra, ZEBRA))
1089 zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type));
1091 /* Send distribute add message to zebra. */
1092 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
1097 /* Redistribute with route-map specification. */
1099 bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type,
1102 if (bgp->rmap[afi][type].name
1103 && (strcmp (bgp->rmap[afi][type].name, name) == 0))
1106 if (bgp->rmap[afi][type].name)
1107 free (bgp->rmap[afi][type].name);
1108 bgp->rmap[afi][type].name = strdup (name);
1109 bgp->rmap[afi][type].map = route_map_lookup_by_name (name);
1114 /* Redistribute with metric specification. */
1116 bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type,
1119 if (bgp->redist_metric_flag[afi][type]
1120 && bgp->redist_metric[afi][type] == metric)
1123 bgp->redist_metric_flag[afi][type] = 1;
1124 bgp->redist_metric[afi][type] = metric;
1129 /* Unset redistribution. */
1131 bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type)
1133 /* Unset flag from BGP instance. */
1134 bgp->redist[afi][type] = 0;
1136 /* Unset route-map. */
1137 if (bgp->rmap[afi][type].name)
1138 free (bgp->rmap[afi][type].name);
1139 bgp->rmap[afi][type].name = NULL;
1140 bgp->rmap[afi][type].map = NULL;
1143 bgp->redist_metric_flag[afi][type] = 0;
1144 bgp->redist_metric[afi][type] = 0;
1146 /* Return if zebra connection is disabled. */
1147 if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
1149 vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
1151 if (bgp->redist[AFI_IP][type] == 0
1152 && bgp->redist[AFI_IP6][type] == 0
1153 && zclient->sock >= 0)
1155 /* Send distribute delete message to zebra. */
1156 if (BGP_DEBUG(zebra, ZEBRA))
1157 zlog_debug("Zebra send: redistribute delete %s",
1158 zebra_route_string(type));
1159 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
1163 /* Withdraw redistributed routes from current BGP's routing table. */
1164 bgp_redistribute_withdraw (bgp, afi, type);
1170 bgp_zclient_reset (void)
1172 zclient_reset (zclient);
1176 bgp_zebra_connected (struct zclient *zclient)
1178 zclient_num_connects++;
1179 zclient_send_requests (zclient, VRF_DEFAULT);
1183 bgp_zebra_init (struct thread_master *master)
1185 zclient_num_connects = 0;
1187 /* Set default values. */
1188 zclient = zclient_new (master);
1189 zclient_init (zclient, ZEBRA_ROUTE_BGP);
1190 zclient->zebra_connected = bgp_zebra_connected;
1191 zclient->router_id_update = bgp_router_id_update;
1192 zclient->interface_add = bgp_interface_add;
1193 zclient->interface_delete = bgp_interface_delete;
1194 zclient->interface_address_add = bgp_interface_address_add;
1195 zclient->interface_address_delete = bgp_interface_address_delete;
1196 zclient->ipv4_route_add = zebra_read_ipv4;
1197 zclient->ipv4_route_delete = zebra_read_ipv4;
1198 zclient->interface_up = bgp_interface_up;
1199 zclient->interface_down = bgp_interface_down;
1200 zclient->ipv6_route_add = zebra_read_ipv6;
1201 zclient->ipv6_route_delete = zebra_read_ipv6;
1202 zclient->nexthop_update = bgp_read_nexthop_update;
1204 bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
1205 bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
1209 bgp_zebra_destroy(void)
1211 if (zclient == NULL)
1213 zclient_stop(zclient);
1214 zclient_free(zclient);
1219 bgp_zebra_num_connects(void)
1221 return zclient_num_connects;