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"
41 /* All information about zebra. */
42 struct zclient
*zclient
= NULL
;
43 struct in_addr router_id_zebra
;
45 /* Router-id update message from zebra. */
47 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
)
49 struct prefix router_id
;
50 struct listnode
*node
, *nnode
;
53 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
55 if (BGP_DEBUG(zebra
, ZEBRA
))
58 prefix2str(&router_id
, buf
, sizeof(buf
));
59 zlog_debug("Zebra rcvd: router id update %s", buf
);
62 router_id_zebra
= router_id
.u
.prefix4
;
64 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
66 if (!bgp
->router_id_static
.s_addr
)
67 bgp_router_id_set (bgp
, &router_id
.u
.prefix4
);
73 /* Inteface addition message from zebra. */
75 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
77 struct interface
*ifp
;
79 ifp
= zebra_interface_add_read (zclient
->ibuf
);
81 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
82 zlog_debug("Zebra rcvd: interface add %s", ifp
->name
);
88 bgp_interface_delete (int command
, struct zclient
*zclient
,
92 struct interface
*ifp
;
95 ifp
= zebra_interface_state_read (s
);
96 ifp
->ifindex
= IFINDEX_INTERNAL
;
98 if (BGP_DEBUG(zebra
, ZEBRA
))
99 zlog_debug("Zebra rcvd: interface delete %s", ifp
->name
);
105 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
)
108 struct interface
*ifp
;
110 struct listnode
*node
, *nnode
;
113 ifp
= zebra_interface_state_read (s
);
118 if (BGP_DEBUG(zebra
, ZEBRA
))
119 zlog_debug("Zebra rcvd: interface %s up", ifp
->name
);
121 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
122 bgp_connected_add (c
);
128 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
)
131 struct interface
*ifp
;
133 struct listnode
*node
, *nnode
;
136 ifp
= zebra_interface_state_read (s
);
140 if (BGP_DEBUG(zebra
, ZEBRA
))
141 zlog_debug("Zebra rcvd: interface %s down", ifp
->name
);
143 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
144 bgp_connected_delete (c
);
146 /* Fast external-failover (Currently IPv4 only) */
148 struct listnode
*mnode
;
151 struct interface
*peer_if
;
153 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
155 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
158 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
163 if (peer
->su
.sa
.sa_family
== AF_INET
)
164 peer_if
= if_lookup_by_ipv4 (&peer
->su
.sin
.sin_addr
);
169 BGP_EVENT_ADD (peer
, BGP_Stop
);
178 bgp_interface_address_add (int command
, struct zclient
*zclient
,
181 struct connected
*ifc
;
183 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
);
188 if (BGP_DEBUG(zebra
, ZEBRA
))
191 prefix2str(ifc
->address
, buf
, sizeof(buf
));
192 zlog_debug("Zebra rcvd: interface %s address add %s",
193 ifc
->ifp
->name
, buf
);
196 if (if_is_operative (ifc
->ifp
))
197 bgp_connected_add (ifc
);
203 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
206 struct connected
*ifc
;
208 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
);
213 if (BGP_DEBUG(zebra
, ZEBRA
))
216 prefix2str(ifc
->address
, buf
, sizeof(buf
));
217 zlog_debug("Zebra rcvd: interface %s address delete %s",
218 ifc
->ifp
->name
, buf
);
221 if (if_is_operative (ifc
->ifp
))
222 bgp_connected_delete (ifc
);
224 connected_free (ifc
);
229 /* Zebra route add and delete treatment. */
231 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
)
234 struct zapi_ipv4 api
;
235 struct in_addr nexthop
;
236 struct prefix_ipv4 p
;
241 /* Type, flags, message. */
242 api
.type
= stream_getc (s
);
243 api
.flags
= stream_getc (s
);
244 api
.message
= stream_getc (s
);
247 memset (&p
, 0, sizeof (struct prefix_ipv4
));
249 p
.prefixlen
= stream_getc (s
);
250 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
252 /* Nexthop, ifindex, distance, metric. */
253 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
255 api
.nexthop_num
= stream_getc (s
);
256 nexthop
.s_addr
= stream_get_ipv4 (s
);
258 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
260 api
.ifindex_num
= stream_getc (s
);
261 stream_getl (s
); /* ifindex, unused */
263 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
264 api
.distance
= stream_getc (s
);
265 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
266 api
.metric
= stream_getl (s
);
270 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
272 if (BGP_DEBUG(zebra
, ZEBRA
))
274 char buf
[2][INET_ADDRSTRLEN
];
275 zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u",
276 zebra_route_string(api
.type
),
277 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
279 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
282 bgp_redistribute_add((struct prefix
*)&p
, &nexthop
, NULL
,
283 api
.metric
, api
.type
);
287 if (BGP_DEBUG(zebra
, ZEBRA
))
289 char buf
[2][INET_ADDRSTRLEN
];
290 zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d "
291 "nexthop %s metric %u",
292 zebra_route_string(api
.type
),
293 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
295 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
298 bgp_redistribute_delete((struct prefix
*)&p
, api
.type
);
305 /* Zebra route add and delete treatment. */
307 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
)
310 struct zapi_ipv6 api
;
311 struct in6_addr nexthop
;
312 struct prefix_ipv6 p
;
315 memset (&nexthop
, 0, sizeof (struct in6_addr
));
317 /* Type, flags, message. */
318 api
.type
= stream_getc (s
);
319 api
.flags
= stream_getc (s
);
320 api
.message
= stream_getc (s
);
323 memset (&p
, 0, sizeof (struct prefix_ipv6
));
325 p
.prefixlen
= stream_getc (s
);
326 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
328 /* Nexthop, ifindex, distance, metric. */
329 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
331 api
.nexthop_num
= stream_getc (s
);
332 stream_get (&nexthop
, s
, 16);
334 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
336 api
.ifindex_num
= stream_getc (s
);
337 stream_getl (s
); /* ifindex, unused */
339 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
340 api
.distance
= stream_getc (s
);
343 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
344 api
.metric
= stream_getl (s
);
348 /* Simply ignore link-local address. */
349 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
352 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
354 if (BGP_DEBUG(zebra
, ZEBRA
))
356 char buf
[2][INET6_ADDRSTRLEN
];
357 zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u",
358 zebra_route_string(api
.type
),
359 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
361 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
364 bgp_redistribute_add ((struct prefix
*)&p
, NULL
, &nexthop
,
365 api
.metric
, api
.type
);
369 if (BGP_DEBUG(zebra
, ZEBRA
))
371 char buf
[2][INET6_ADDRSTRLEN
];
372 zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
373 "nexthop %s metric %u",
374 zebra_route_string(api
.type
),
375 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
377 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
380 bgp_redistribute_delete ((struct prefix
*) &p
, api
.type
);
385 #endif /* HAVE_IPV6 */
388 if_lookup_by_ipv4 (struct in_addr
*addr
)
390 struct listnode
*ifnode
;
391 struct listnode
*cnode
;
392 struct interface
*ifp
;
393 struct connected
*connected
;
394 struct prefix_ipv4 p
;
399 p
.prefixlen
= IPV4_MAX_BITLEN
;
401 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
403 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
405 cp
= connected
->address
;
407 if (cp
->family
== AF_INET
)
408 if (prefix_match (cp
, (struct prefix
*)&p
))
416 if_lookup_by_ipv4_exact (struct in_addr
*addr
)
418 struct listnode
*ifnode
;
419 struct listnode
*cnode
;
420 struct interface
*ifp
;
421 struct connected
*connected
;
424 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
426 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
428 cp
= connected
->address
;
430 if (cp
->family
== AF_INET
)
431 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
440 if_lookup_by_ipv6 (struct in6_addr
*addr
)
442 struct listnode
*ifnode
;
443 struct listnode
*cnode
;
444 struct interface
*ifp
;
445 struct connected
*connected
;
446 struct prefix_ipv6 p
;
451 p
.prefixlen
= IPV6_MAX_BITLEN
;
453 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
455 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
457 cp
= connected
->address
;
459 if (cp
->family
== AF_INET6
)
460 if (prefix_match (cp
, (struct prefix
*)&p
))
468 if_lookup_by_ipv6_exact (struct in6_addr
*addr
)
470 struct listnode
*ifnode
;
471 struct listnode
*cnode
;
472 struct interface
*ifp
;
473 struct connected
*connected
;
476 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
478 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
480 cp
= connected
->address
;
482 if (cp
->family
== AF_INET6
)
483 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
491 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
493 struct listnode
*cnode
;
494 struct connected
*connected
;
497 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
499 cp
= connected
->address
;
501 if (cp
->family
== AF_INET6
)
502 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
504 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
512 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
514 struct listnode
*cnode
;
515 struct connected
*connected
;
518 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
520 cp
= connected
->address
;
522 if (cp
->family
== AF_INET6
)
523 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
525 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
531 #endif /* HAVE_IPV6 */
534 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
535 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
538 struct interface
*ifp
= NULL
;
540 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
547 if (local
->sa
.sa_family
== AF_INET
)
549 nexthop
->v4
= local
->sin
.sin_addr
;
550 ifp
= if_lookup_by_ipv4 (&local
->sin
.sin_addr
);
553 if (local
->sa
.sa_family
== AF_INET6
)
555 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
558 ifp
= if_lookup_by_index (if_nametoindex (peer
->ifname
));
561 ifp
= if_lookup_by_ipv6 (&local
->sin6
.sin6_addr
);
563 #endif /* HAVE_IPV6 */
570 /* IPv4 connection. */
571 if (local
->sa
.sa_family
== AF_INET
)
575 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
577 /* There is no global nexthop. */
579 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
581 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
582 #endif /* HAVE_IPV6 */
586 /* IPv6 connection. */
587 if (local
->sa
.sa_family
== AF_INET6
)
589 struct interface
*direct
= NULL
;
591 /* IPv4 nexthop. I don't care about it. */
592 if (peer
->local_id
.s_addr
)
593 nexthop
->v4
= peer
->local_id
;
596 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
598 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
601 /* If directory connected set link-local address. */
602 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
);
604 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
607 /* Link-local address. */
609 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
611 /* If there is no global address. Set link-local address as
612 global. I know this break RFC specification... */
614 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
617 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
622 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
623 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
))
624 peer
->shared_network
= 1;
626 peer
->shared_network
= 0;
628 /* KAME stack specific treatment. */
630 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
631 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
633 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
635 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
636 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
638 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
641 #endif /* HAVE_IPV6 */
646 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
)
652 if (zclient
->sock
< 0)
655 if (! zclient
->redist
[ZEBRA_ROUTE_BGP
])
661 if (peer_sort (peer
) == BGP_PEER_IBGP
|| peer_sort (peer
) == BGP_PEER_CONFED
)
663 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
664 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
667 if ((peer_sort (peer
) == BGP_PEER_EBGP
&& peer
->ttl
!= 1)
668 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
))
669 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
671 if (p
->family
== AF_INET
)
673 struct zapi_ipv4 api
;
674 struct in_addr
*nexthop
;
677 nexthop
= &info
->attr
->nexthop
;
679 api
.type
= ZEBRA_ROUTE_BGP
;
681 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
683 api
.nexthop
= &nexthop
;
685 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
686 api
.metric
= info
->attr
->med
;
688 distance
= bgp_distance_apply (p
, info
, bgp
);
692 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
693 api
.distance
= distance
;
696 if (BGP_DEBUG(zebra
, ZEBRA
))
698 char buf
[2][INET_ADDRSTRLEN
];
699 zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u",
700 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
702 inet_ntop(AF_INET
, nexthop
, buf
[1], sizeof(buf
[1])),
706 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD
, zclient
,
707 (struct prefix_ipv4
*) p
, &api
);
710 /* We have to think about a IPv6 link-local address curse. */
711 if (p
->family
== AF_INET6
)
713 unsigned int ifindex
;
714 struct in6_addr
*nexthop
;
715 struct zapi_ipv6 api
;
720 assert (info
->attr
->extra
);
722 /* Only global address nexthop exists. */
723 if (info
->attr
->extra
->mp_nexthop_len
== 16)
724 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
726 /* If both global and link-local address present. */
727 if (info
->attr
->extra
->mp_nexthop_len
== 32)
729 /* Workaround for Cisco's nexthop bug. */
730 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
731 && peer
->su_remote
->sa
.sa_family
== AF_INET6
)
732 nexthop
= &peer
->su_remote
->sin6
.sin6_addr
;
734 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
736 if (info
->peer
->nexthop
.ifp
)
737 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
743 if (IN6_IS_ADDR_LINKLOCAL (nexthop
) && ! ifindex
)
745 if (info
->peer
->ifname
)
746 ifindex
= if_nametoindex (info
->peer
->ifname
);
747 else if (info
->peer
->nexthop
.ifp
)
748 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
751 /* Make Zebra API structure. */
753 api
.type
= ZEBRA_ROUTE_BGP
;
755 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
757 api
.nexthop
= &nexthop
;
758 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
760 api
.ifindex
= &ifindex
;
761 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
762 api
.metric
= info
->attr
->med
;
764 if (BGP_DEBUG(zebra
, ZEBRA
))
766 char buf
[2][INET6_ADDRSTRLEN
];
767 zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u",
768 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
770 inet_ntop(AF_INET6
, nexthop
, buf
[1], sizeof(buf
[1])),
774 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD
, zclient
,
775 (struct prefix_ipv6
*) p
, &api
);
777 #endif /* HAVE_IPV6 */
781 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
)
786 if (zclient
->sock
< 0)
789 if (! zclient
->redist
[ZEBRA_ROUTE_BGP
])
795 if (peer_sort (peer
) == BGP_PEER_IBGP
)
797 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
798 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
801 if ((peer_sort (peer
) == BGP_PEER_EBGP
&& peer
->ttl
!= 1)
802 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
))
803 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
805 if (p
->family
== AF_INET
)
807 struct zapi_ipv4 api
;
808 struct in_addr
*nexthop
;
811 nexthop
= &info
->attr
->nexthop
;
813 api
.type
= ZEBRA_ROUTE_BGP
;
815 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
817 api
.nexthop
= &nexthop
;
819 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
820 api
.metric
= info
->attr
->med
;
822 if (BGP_DEBUG(zebra
, ZEBRA
))
824 char buf
[2][INET_ADDRSTRLEN
];
825 zlog_debug("Zebra send: IPv4 route delete %s/%d nexthop %s metric %u",
826 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
828 inet_ntop(AF_INET
, nexthop
, buf
[1], sizeof(buf
[1])),
832 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
833 (struct prefix_ipv4
*) p
, &api
);
836 /* We have to think about a IPv6 link-local address curse. */
837 if (p
->family
== AF_INET6
)
839 struct zapi_ipv6 api
;
840 unsigned int ifindex
;
841 struct in6_addr
*nexthop
;
843 assert (info
->attr
->extra
);
848 /* Only global address nexthop exists. */
849 if (info
->attr
->extra
->mp_nexthop_len
== 16)
850 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
852 /* If both global and link-local address present. */
853 if (info
->attr
->extra
->mp_nexthop_len
== 32)
855 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
856 if (info
->peer
->nexthop
.ifp
)
857 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
863 if (IN6_IS_ADDR_LINKLOCAL (nexthop
) && ! ifindex
)
864 if (info
->peer
->ifname
)
865 ifindex
= if_nametoindex (info
->peer
->ifname
);
868 api
.type
= ZEBRA_ROUTE_BGP
;
870 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
872 api
.nexthop
= &nexthop
;
873 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
875 api
.ifindex
= &ifindex
;
876 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
877 api
.metric
= info
->attr
->med
;
879 if (BGP_DEBUG(zebra
, ZEBRA
))
881 char buf
[2][INET6_ADDRSTRLEN
];
882 zlog_debug("Zebra send: IPv6 route delete %s/%d nexthop %s metric %u",
883 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
885 inet_ntop(AF_INET6
, nexthop
, buf
[1], sizeof(buf
[1])),
889 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
890 (struct prefix_ipv6
*) p
, &api
);
892 #endif /* HAVE_IPV6 */
895 /* Other routes redistribution into BGP. */
897 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
)
899 /* Set flag to BGP instance. */
900 bgp
->redist
[afi
][type
] = 1;
902 /* Return if already redistribute flag is set. */
903 if (zclient
->redist
[type
])
906 zclient
->redist
[type
] = 1;
908 /* Return if zebra connection is not established. */
909 if (zclient
->sock
< 0)
912 if (BGP_DEBUG(zebra
, ZEBRA
))
913 zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type
));
915 /* Send distribute add message to zebra. */
916 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, type
);
921 /* Redistribute with route-map specification. */
923 bgp_redistribute_rmap_set (struct bgp
*bgp
, afi_t afi
, int type
,
926 if (bgp
->rmap
[afi
][type
].name
927 && (strcmp (bgp
->rmap
[afi
][type
].name
, name
) == 0))
930 if (bgp
->rmap
[afi
][type
].name
)
931 free (bgp
->rmap
[afi
][type
].name
);
932 bgp
->rmap
[afi
][type
].name
= strdup (name
);
933 bgp
->rmap
[afi
][type
].map
= route_map_lookup_by_name (name
);
938 /* Redistribute with metric specification. */
940 bgp_redistribute_metric_set (struct bgp
*bgp
, afi_t afi
, int type
,
943 if (bgp
->redist_metric_flag
[afi
][type
]
944 && bgp
->redist_metric
[afi
][type
] == metric
)
947 bgp
->redist_metric_flag
[afi
][type
] = 1;
948 bgp
->redist_metric
[afi
][type
] = metric
;
953 /* Unset redistribution. */
955 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
)
957 /* Unset flag from BGP instance. */
958 bgp
->redist
[afi
][type
] = 0;
960 /* Unset route-map. */
961 if (bgp
->rmap
[afi
][type
].name
)
962 free (bgp
->rmap
[afi
][type
].name
);
963 bgp
->rmap
[afi
][type
].name
= NULL
;
964 bgp
->rmap
[afi
][type
].map
= NULL
;
967 bgp
->redist_metric_flag
[afi
][type
] = 0;
968 bgp
->redist_metric
[afi
][type
] = 0;
970 /* Return if zebra connection is disabled. */
971 if (! zclient
->redist
[type
])
973 zclient
->redist
[type
] = 0;
975 if (bgp
->redist
[AFI_IP
][type
] == 0
976 && bgp
->redist
[AFI_IP6
][type
] == 0
977 && zclient
->sock
>= 0)
979 /* Send distribute delete message to zebra. */
980 if (BGP_DEBUG(zebra
, ZEBRA
))
981 zlog_debug("Zebra send: redistribute delete %s",
982 zebra_route_string(type
));
983 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, type
);
986 /* Withdraw redistributed routes from current BGP's routing table. */
987 bgp_redistribute_withdraw (bgp
, afi
, type
);
992 /* Unset redistribution route-map configuration. */
994 bgp_redistribute_routemap_unset (struct bgp
*bgp
, afi_t afi
, int type
)
996 if (! bgp
->rmap
[afi
][type
].name
)
999 /* Unset route-map. */
1000 free (bgp
->rmap
[afi
][type
].name
);
1001 bgp
->rmap
[afi
][type
].name
= NULL
;
1002 bgp
->rmap
[afi
][type
].map
= NULL
;
1007 /* Unset redistribution metric configuration. */
1009 bgp_redistribute_metric_unset (struct bgp
*bgp
, afi_t afi
, int type
)
1011 if (! bgp
->redist_metric_flag
[afi
][type
])
1015 bgp
->redist_metric_flag
[afi
][type
] = 0;
1016 bgp
->redist_metric
[afi
][type
] = 0;
1022 bgp_zclient_reset (void)
1024 zclient_reset (zclient
);
1028 bgp_zebra_init (void)
1030 /* Set default values. */
1031 zclient
= zclient_new ();
1032 zclient_init (zclient
, ZEBRA_ROUTE_BGP
);
1033 zclient
->router_id_update
= bgp_router_id_update
;
1034 zclient
->interface_add
= bgp_interface_add
;
1035 zclient
->interface_delete
= bgp_interface_delete
;
1036 zclient
->interface_address_add
= bgp_interface_address_add
;
1037 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1038 zclient
->ipv4_route_add
= zebra_read_ipv4
;
1039 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
1040 zclient
->interface_up
= bgp_interface_up
;
1041 zclient
->interface_down
= bgp_interface_down
;
1043 zclient
->ipv6_route_add
= zebra_read_ipv6
;
1044 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
1045 #endif /* HAVE_IPV6 */
1047 /* Interface related init. */