2 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "sockunion.h"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_zebra.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_mpath.h"
44 #include "bgpd/bgp_nexthop.h"
45 #include "bgpd/bgp_nht.h"
46 #include "bgpd/bgp_bfd.h"
48 /* All information about zebra. */
49 struct zclient
*zclient
= NULL
;
50 struct in_addr router_id_zebra
;
52 /* Growable buffer for nexthops sent to zebra */
53 struct stream
*bgp_nexthop_buf
= NULL
;
54 struct stream
*bgp_ifindices_buf
= NULL
;
56 /* These array buffers are used in making a copy of the attributes for
57 route-map apply. Arrays are being used here to minimize mallocs and
58 frees for the temporary copy of the attributes.
59 Given the zapi api expects the nexthop buffer to contain pointer to
60 pointers for nexthops, we couldnt have used a single nexthop variable
61 on the stack, hence we had two options:
62 1. maintain a linked-list and free it after zapi_*_route call
63 2. use an array to avoid number of mallocs.
64 Number of supported next-hops are finite, use of arrays should be ok. */
65 struct attr attr_cp
[BGP_MAXIMUM_MAXPATHS
];
66 struct attr_extra attr_extra_cp
[BGP_MAXIMUM_MAXPATHS
];
69 /* Once per address-family initialization of the attribute array */
70 #define BGP_INFO_ATTR_BUF_INIT()\
72 memset(attr_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr));\
73 memset(attr_extra_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr_extra));\
77 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
79 *info_dst = *info_src; \
80 assert(attr_index != BGP_MAXIMUM_MAXPATHS);\
81 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
82 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
83 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
84 info_dst->attr = &attr_cp[attr_index]; \
88 #define BGP_INFO_ATTR_BUF_FREE(info) \
90 bgp_attr_deep_free(info->attr); \
93 /* Router-id update message from zebra. */
95 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
98 struct prefix router_id
;
99 struct listnode
*node
, *nnode
;
102 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
104 if (BGP_DEBUG (zebra
, ZEBRA
))
107 prefix2str(&router_id
, buf
, sizeof(buf
));
108 zlog_debug("Zebra rcvd: router id update %s", buf
);
111 router_id_zebra
= router_id
.u
.prefix4
;
113 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
115 if (!bgp
->router_id_static
.s_addr
)
116 bgp_router_id_set (bgp
, &router_id
.u
.prefix4
);
122 /* Nexthop update message from zebra. */
124 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
125 zebra_size_t length
, vrf_id_t vrf_id
)
127 bgp_parse_nexthop_update(command
, vrf_id
);
132 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
133 zebra_size_t length
, vrf_id_t vrf_id
)
135 bgp_parse_nexthop_update(command
, vrf_id
);
140 bgp_start_interface_nbrs (struct interface
*ifp
)
142 struct listnode
*node
, *nnode
, *mnode
;
146 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
148 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
151 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
152 peer
->status
!= Established
)
154 if (peer_active(peer
))
155 BGP_EVENT_ADD (peer
, BGP_Stop
);
156 BGP_EVENT_ADD (peer
, BGP_Start
);
163 bgp_nbr_connected_add (struct nbr_connected
*ifc
)
165 struct listnode
*node
;
166 struct connected
*connected
;
167 struct interface
*ifp
;
170 /* Kick-off the FSM for any relevant peers only if there is a
171 * valid local address on the interface.
174 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
176 p
= connected
->address
;
177 if (p
->family
== AF_INET6
&&
178 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
184 bgp_start_interface_nbrs (ifp
);
188 bgp_nbr_connected_delete (struct nbr_connected
*ifc
, int del
)
190 struct listnode
*node
, *nnode
, *mnode
;
193 struct interface
*ifp
;
195 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
197 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
199 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
201 BGP_EVENT_ADD (peer
, BGP_Stop
);
205 /* Free neighbor also, if we're asked to. */
209 listnode_delete (ifp
->nbr_connected
, ifc
);
210 nbr_connected_free (ifc
);
214 /* Inteface addition message from zebra. */
216 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
219 struct interface
*ifp
;
221 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
223 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
224 zlog_debug("Zebra rcvd: interface add %s", ifp
->name
);
230 bgp_interface_delete (int command
, struct zclient
*zclient
,
231 zebra_size_t length
, vrf_id_t vrf_id
)
234 struct interface
*ifp
;
237 ifp
= zebra_interface_state_read (s
, vrf_id
);
238 ifp
->ifindex
= IFINDEX_INTERNAL
;
240 if (BGP_DEBUG (zebra
, ZEBRA
))
241 zlog_debug("Zebra rcvd: interface delete %s", ifp
->name
);
247 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
251 struct interface
*ifp
;
253 struct nbr_connected
*nc
;
254 struct listnode
*node
, *nnode
;
257 ifp
= zebra_interface_state_read (s
, vrf_id
);
262 if (BGP_DEBUG (zebra
, ZEBRA
))
263 zlog_debug("Zebra rcvd: interface %s up", ifp
->name
);
265 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
266 bgp_connected_add (c
);
268 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
269 bgp_nbr_connected_add (nc
);
275 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
279 struct interface
*ifp
;
281 struct nbr_connected
*nc
;
282 struct listnode
*node
, *nnode
;
285 ifp
= zebra_interface_state_read (s
, vrf_id
);
289 if (BGP_DEBUG (zebra
, ZEBRA
))
290 zlog_debug("Zebra rcvd: interface %s down", ifp
->name
);
292 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
293 bgp_connected_delete (c
);
295 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
296 bgp_nbr_connected_delete (nc
, 1);
298 /* Fast external-failover */
300 struct listnode
*mnode
;
304 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
306 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
309 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
311 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
314 if (ifp
== peer
->nexthop
.ifp
)
315 BGP_EVENT_ADD (peer
, BGP_Stop
);
324 bgp_interface_address_add (int command
, struct zclient
*zclient
,
325 zebra_size_t length
, vrf_id_t vrf_id
)
327 struct connected
*ifc
;
329 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
334 if (bgp_debug_zebra(ifc
->address
))
337 prefix2str(ifc
->address
, buf
, sizeof(buf
));
338 zlog_debug("Zebra rcvd: interface %s address add %s",
339 ifc
->ifp
->name
, buf
);
342 if (if_is_operative (ifc
->ifp
))
344 bgp_connected_add (ifc
);
345 /* If we have learnt of any neighbors on this interface,
346 * check to kick off any BGP interface-based neighbors,
347 * but only if this is a link-local address.
349 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
350 !list_isempty(ifc
->ifp
->nbr_connected
))
351 bgp_start_interface_nbrs (ifc
->ifp
);
358 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
359 zebra_size_t length
, vrf_id_t vrf_id
)
361 struct connected
*ifc
;
363 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
368 if (bgp_debug_zebra(ifc
->address
))
371 prefix2str(ifc
->address
, buf
, sizeof(buf
));
372 zlog_debug("Zebra rcvd: interface %s address delete %s",
373 ifc
->ifp
->name
, buf
);
376 if (if_is_operative (ifc
->ifp
))
377 bgp_connected_delete (ifc
);
379 connected_free (ifc
);
385 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
386 zebra_size_t length
, vrf_id_t vrf_id
)
388 struct nbr_connected
*ifc
= NULL
;
390 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
395 if (bgp_debug_zebra(ifc
->address
))
398 prefix2str(ifc
->address
, buf
, sizeof(buf
));
399 zlog_debug("Zebra rcvd: interface %s nbr address add %s",
400 ifc
->ifp
->name
, buf
);
403 if (if_is_operative (ifc
->ifp
))
404 bgp_nbr_connected_add (ifc
);
410 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
411 zebra_size_t length
, vrf_id_t vrf_id
)
413 struct nbr_connected
*ifc
= NULL
;
415 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
420 if (bgp_debug_zebra(ifc
->address
))
423 prefix2str(ifc
->address
, buf
, sizeof(buf
));
424 zlog_debug("Zebra rcvd: interface %s nbr address delete %s",
425 ifc
->ifp
->name
, buf
);
428 if (if_is_operative (ifc
->ifp
))
429 bgp_nbr_connected_delete (ifc
, 0);
431 nbr_connected_free (ifc
);
436 /* Zebra route add and delete treatment. */
438 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
442 struct zapi_ipv4 api
;
443 struct in_addr nexthop
;
444 struct prefix_ipv4 p
;
445 unsigned int ifindex
;
451 /* Type, flags, message. */
452 api
.type
= stream_getc (s
);
453 api
.instance
= stream_getw (s
);
454 api
.flags
= stream_getc (s
);
455 api
.message
= stream_getc (s
);
458 memset (&p
, 0, sizeof (struct prefix_ipv4
));
460 p
.prefixlen
= stream_getc (s
);
461 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
463 /* Nexthop, ifindex, distance, metric. */
464 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
466 api
.nexthop_num
= stream_getc (s
);
467 nexthop
.s_addr
= stream_get_ipv4 (s
);
470 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
472 api
.ifindex_num
= stream_getc (s
);
473 ifindex
= stream_getl (s
); /* ifindex, unused */
480 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
481 api
.distance
= stream_getc (s
);
483 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
484 api
.metric
= stream_getl (s
);
488 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
489 api
.tag
= stream_getw (s
);
493 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
495 if (bgp_debug_zebra((struct prefix
*)&p
))
497 char buf
[2][INET_ADDRSTRLEN
];
498 zlog_debug("Zebra rcvd: IPv4 route add %s[%d] %s/%d nexthop %s metric %u tag %d",
499 zebra_route_string(api
.type
), api
.instance
,
500 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
502 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
508 * The ADD message is actually an UPDATE and there is no explicit DEL
509 * for a prior redistributed route, if any. So, perform an implicit
510 * DEL processing for the same redistributed route from any other
513 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
516 bgp_redistribute_delete((struct prefix
*)&p
, i
, api
.instance
);
519 /* Now perform the add/update. */
520 bgp_redistribute_add((struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
521 api
.metric
, api
.type
, api
.instance
, api
.tag
);
523 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
525 if (bgp_debug_zebra((struct prefix
*)&p
))
527 char buf
[2][INET_ADDRSTRLEN
];
528 zlog_debug("Zebra rcvd: IPv4 route delete %s[%d] %s/%d "
529 "nexthop %s metric %u tag %d",
530 zebra_route_string(api
.type
), api
.instance
,
531 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
533 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
537 bgp_redistribute_delete((struct prefix
*)&p
, api
.type
, api
.instance
);
544 /* Zebra route add and delete treatment. */
546 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
550 struct zapi_ipv6 api
;
551 struct in6_addr nexthop
;
552 struct prefix_ipv6 p
;
553 unsigned int ifindex
;
557 memset (&nexthop
, 0, sizeof (struct in6_addr
));
559 /* Type, flags, message. */
560 api
.type
= stream_getc (s
);
561 api
.instance
= stream_getw (s
);
562 api
.flags
= stream_getc (s
);
563 api
.message
= stream_getc (s
);
566 memset (&p
, 0, sizeof (struct prefix_ipv6
));
568 p
.prefixlen
= stream_getc (s
);
569 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
571 /* Nexthop, ifindex, distance, metric. */
572 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
574 api
.nexthop_num
= stream_getc (s
);
575 stream_get (&nexthop
, s
, 16);
578 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
580 api
.ifindex_num
= stream_getc (s
);
581 ifindex
= stream_getl (s
); /* ifindex, unused */
588 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
589 api
.distance
= stream_getc (s
);
593 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
594 api
.metric
= stream_getl (s
);
598 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
599 api
.tag
= stream_getw (s
);
603 /* Simply ignore link-local address. */
604 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
607 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
609 if (bgp_debug_zebra((struct prefix
*)&p
))
611 char buf
[2][INET6_ADDRSTRLEN
];
612 zlog_debug("Zebra rcvd: IPv6 route add %s[%d] %s/%d nexthop %s metric %u tag %d",
613 zebra_route_string(api
.type
), api
.instance
,
614 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
616 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
622 * The ADD message is actually an UPDATE and there is no explicit DEL
623 * for a prior redistributed route, if any. So, perform an implicit
624 * DEL processing for the same redistributed route from any other
627 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
630 bgp_redistribute_delete((struct prefix
*)&p
, i
, api
.instance
);
633 bgp_redistribute_add ((struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
634 api
.metric
, api
.type
, api
.instance
, api
.tag
);
636 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
638 if (bgp_debug_zebra((struct prefix
*)&p
))
640 char buf
[2][INET6_ADDRSTRLEN
];
641 zlog_debug("Zebra rcvd: IPv6 route delete %s[%d] %s/%d "
642 "nexthop %s metric %u tag %d",
643 zebra_route_string(api
.type
), api
.instance
,
644 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
646 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
650 bgp_redistribute_delete ((struct prefix
*) &p
, api
.type
, api
.instance
);
655 #endif /* HAVE_IPV6 */
658 if_lookup_by_ipv4 (struct in_addr
*addr
)
660 struct listnode
*ifnode
;
661 struct listnode
*cnode
;
662 struct interface
*ifp
;
663 struct connected
*connected
;
664 struct prefix_ipv4 p
;
669 p
.prefixlen
= IPV4_MAX_BITLEN
;
671 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
673 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
675 cp
= connected
->address
;
677 if (cp
->family
== AF_INET
)
678 if (prefix_match (cp
, (struct prefix
*)&p
))
686 if_lookup_by_ipv4_exact (struct in_addr
*addr
)
688 struct listnode
*ifnode
;
689 struct listnode
*cnode
;
690 struct interface
*ifp
;
691 struct connected
*connected
;
694 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
696 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
698 cp
= connected
->address
;
700 if (cp
->family
== AF_INET
)
701 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
710 if_lookup_by_ipv6 (struct in6_addr
*addr
, unsigned int ifindex
)
712 struct listnode
*ifnode
;
713 struct listnode
*cnode
;
714 struct interface
*ifp
;
715 struct connected
*connected
;
716 struct prefix_ipv6 p
;
721 p
.prefixlen
= IPV6_MAX_BITLEN
;
723 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
725 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
727 cp
= connected
->address
;
729 if (cp
->family
== AF_INET6
)
730 if (prefix_match (cp
, (struct prefix
*)&p
))
732 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
.s6_addr32
[0]))
734 if (ifindex
== ifp
->ifindex
)
746 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, unsigned int ifindex
)
748 struct listnode
*ifnode
;
749 struct listnode
*cnode
;
750 struct interface
*ifp
;
751 struct connected
*connected
;
754 for (ALL_LIST_ELEMENTS_RO (iflist
, ifnode
, ifp
))
756 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
758 cp
= connected
->address
;
760 if (cp
->family
== AF_INET6
)
761 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
763 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
765 if (ifindex
== ifp
->ifindex
)
777 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
779 struct listnode
*cnode
;
780 struct connected
*connected
;
783 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
785 cp
= connected
->address
;
787 if (cp
->family
== AF_INET6
)
788 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
790 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
798 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
800 struct listnode
*cnode
;
801 struct connected
*connected
;
804 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
806 cp
= connected
->address
;
808 if (cp
->family
== AF_INET6
)
809 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
811 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
817 #endif /* HAVE_IPV6 */
820 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
822 struct listnode
*cnode
;
823 struct connected
*connected
;
826 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
828 cp
= connected
->address
;
829 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
831 *addr
= cp
->u
.prefix4
;
839 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
840 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
843 struct interface
*ifp
= NULL
;
845 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
852 if (local
->sa
.sa_family
== AF_INET
)
854 nexthop
->v4
= local
->sin
.sin_addr
;
856 ifp
= if_lookup_by_name (peer
->update_if
);
858 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
);
861 if (local
->sa
.sa_family
== AF_INET6
)
863 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
865 if (peer
->conf_if
|| peer
->ifname
)
866 ifp
= if_lookup_by_index (if_nametoindex (peer
->conf_if
? peer
->conf_if
: peer
->ifname
));
868 else if (peer
->update_if
)
869 ifp
= if_lookup_by_name (peer
->update_if
);
871 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
872 local
->sin6
.sin6_scope_id
);
874 #endif /* HAVE_IPV6 */
881 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
882 if (local
->sa
.sa_family
== AF_INET
)
886 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
888 /* There is no global nexthop. */
890 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
892 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
893 #endif /* HAVE_IPV6 */
897 /* IPv6 connection, fetch and store IPv4 local address if any. */
898 if (local
->sa
.sa_family
== AF_INET6
)
900 struct interface
*direct
= NULL
;
903 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
904 if (!ret
&& peer
->local_id
.s_addr
)
905 nexthop
->v4
= peer
->local_id
;
908 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
910 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
913 /* If directory connected set link-local address. */
914 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
915 remote
->sin6
.sin6_scope_id
);
917 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
920 /* Link-local address. */
922 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
924 /* If there is no global address. Set link-local address as
925 global. I know this break RFC specification... */
926 /* In this scenario, the expectation for interop is that the
927 * network admin would use a route-map to specify the global
931 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
933 /* Always set the link-local address */
934 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
939 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
940 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
))
941 peer
->shared_network
= 1;
943 peer
->shared_network
= 0;
945 /* KAME stack specific treatment. */
947 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
948 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
950 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
952 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
953 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
955 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
958 #endif /* HAVE_IPV6 */
960 /* If we have identified the local interface, there is no error for now. */
964 static struct in6_addr
*
965 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
967 struct in6_addr
*nexthop
= NULL
;
969 /* Only global address nexthop exists. */
970 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
971 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
973 /* If both global and link-local address present. */
974 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
976 /* Workaround for Cisco's nexthop bug. */
977 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
978 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
979 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
981 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
988 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
989 struct bgp_info
*info
)
991 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
994 if (bgp_debug_zebra(p
))
996 if (p
->family
== AF_INET
)
998 char buf
[2][INET_ADDRSTRLEN
];
999 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1000 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1002 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1005 if (p
->family
== AF_INET6
)
1007 char buf
[2][INET6_ADDRSTRLEN
];
1008 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1009 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1011 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1019 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1020 afi_t afi
, safi_t safi
)
1025 struct bgp_info
*mpinfo
;
1026 size_t oldsize
, newsize
;
1027 u_int32_t nhcount
, metric
;
1028 struct bgp_info local_info
;
1029 struct bgp_info
*info_cp
= &local_info
;
1032 if (zclient
->sock
< 0)
1035 if ((p
->family
== AF_INET
&&
1036 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], VRF_DEFAULT
))
1037 || (p
->family
== AF_INET6
&&
1038 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], VRF_DEFAULT
)))
1041 if (bgp
->main_zebra_update_hold
)
1047 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1048 tag
= info
->attr
->extra
->tag
;
1052 /* When we create an aggregate route we must also install a Null0 route in
1054 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1055 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1057 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1058 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1060 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1061 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1064 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1065 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1066 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1068 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1070 nhcount
= 1 + bgp_info_mpath_count (info
);
1072 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1074 struct zapi_ipv4 api
;
1075 struct in_addr
*nexthop
;
1076 char buf
[2][INET_ADDRSTRLEN
];
1077 int valid_nh_count
= 0;
1079 /* resize nexthop buffer size if necessary */
1080 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1081 (sizeof (struct in_addr
*) * nhcount
))
1083 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1084 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1085 if (newsize
== oldsize
)
1087 zlog_err ("can't resize nexthop buffer");
1091 stream_reset (bgp_nexthop_buf
);
1094 /* Metric is currently based on the best-path only. */
1095 metric
= info
->attr
->med
;
1097 if (bgp
->table_map
[afi
][safi
].name
)
1099 BGP_INFO_ATTR_BUF_INIT();
1101 /* Copy info and attributes, so the route-map apply doesn't modify the
1103 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1104 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1106 metric
= info_cp
->attr
->med
;
1107 nexthop
= &info_cp
->attr
->nexthop
;
1109 if (info_cp
->attr
->extra
)
1110 tag
= info_cp
->attr
->extra
->tag
;
1112 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1116 nexthop
= &info
->attr
->nexthop
;
1121 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1125 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1126 mpinfo
= bgp_info_mpath_next (mpinfo
))
1130 if (bgp
->table_map
[afi
][safi
].name
)
1132 /* Copy info and attributes, so the route-map apply doesn't modify the
1134 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1135 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1136 nexthop
= &info_cp
->attr
->nexthop
;
1137 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1141 nexthop
= &mpinfo
->attr
->nexthop
;
1144 if (nexthop
== NULL
)
1147 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1151 api
.vrf_id
= VRF_DEFAULT
;
1153 api
.type
= ZEBRA_ROUTE_BGP
;
1157 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1159 /* Note that this currently only applies to Null0 routes for aggregates.
1160 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1161 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1162 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1164 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1165 api
.nexthop_num
= 0;
1167 api
.nexthop_num
= valid_nh_count
;
1169 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1170 api
.ifindex_num
= 0;
1171 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1172 api
.metric
= metric
;
1176 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1180 distance
= bgp_distance_apply (p
, info
, bgp
);
1184 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1185 api
.distance
= distance
;
1188 if (bgp_debug_zebra(p
))
1191 zlog_debug("Zebra send: IPv4 route %s %s/%d metric %u tag %d"
1192 " count %d", (valid_nh_count
? "add":"delete"),
1193 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1194 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1195 for (i
= 0; i
< api
.nexthop_num
; i
++)
1196 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1197 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1200 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1201 zclient
, (struct prefix_ipv4
*) p
, &api
);
1205 /* We have to think about a IPv6 link-local address curse. */
1206 if (p
->family
== AF_INET6
||
1207 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1209 unsigned int ifindex
;
1210 struct in6_addr
*nexthop
;
1211 struct zapi_ipv6 api
;
1212 int valid_nh_count
= 0;
1213 char buf
[2][INET6_ADDRSTRLEN
];
1215 /* resize nexthop buffer size if necessary */
1216 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1217 (sizeof (struct in6_addr
*) * nhcount
))
1219 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1220 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1221 if (newsize
== oldsize
)
1223 zlog_err ("can't resize nexthop buffer");
1227 stream_reset (bgp_nexthop_buf
);
1229 /* resize ifindices buffer size if necessary */
1230 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1231 (sizeof (unsigned int) * nhcount
))
1233 newsize
= (sizeof (unsigned int) * nhcount
);
1234 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1235 if (newsize
== oldsize
)
1237 zlog_err ("can't resize nexthop buffer");
1241 stream_reset (bgp_ifindices_buf
);
1246 assert (info
->attr
->extra
);
1248 /* Metric is currently based on the best-path only. */
1249 metric
= info
->attr
->med
;
1251 if (bgp
->table_map
[afi
][safi
].name
)
1253 BGP_INFO_ATTR_BUF_INIT();
1255 /* Copy info and attributes, so the route-map apply doesn't modify the
1257 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1258 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1260 metric
= info_cp
->attr
->med
;
1261 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1263 if (info_cp
->attr
->extra
)
1264 tag
= info_cp
->attr
->extra
->tag
;
1266 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1270 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1275 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1276 if (info
->peer
->nexthop
.ifp
)
1277 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1281 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1282 ifindex
= if_nametoindex (info
->peer
->conf_if
? info
->peer
->conf_if
: info
->peer
->ifname
);
1283 else if (info
->peer
->nexthop
.ifp
)
1284 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1286 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1287 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1291 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1292 mpinfo
= bgp_info_mpath_next (mpinfo
))
1297 if (bgp
->table_map
[afi
][safi
].name
)
1299 /* Copy info and attributes, so the route-map apply doesn't modify the
1301 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1302 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1303 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1304 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1308 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1311 if (nexthop
== NULL
)
1314 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1315 if (mpinfo
->peer
->nexthop
.ifp
)
1316 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1320 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1321 ifindex
= if_nametoindex (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
: mpinfo
->peer
->ifname
);
1322 else if (mpinfo
->peer
->nexthop
.ifp
)
1323 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1328 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1329 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1333 /* Make Zebra API structure. */
1334 api
.vrf_id
= VRF_DEFAULT
;
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_ipv6_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 :: 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 in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1353 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1354 api
.ifindex_num
= valid_nh_count
;
1355 api
.ifindex
= (unsigned int *)STREAM_DATA (bgp_ifindices_buf
);
1356 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1357 api
.metric
= metric
;
1361 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1365 if (p
->family
== AF_INET
)
1367 if (bgp_debug_zebra(p
))
1370 zlog_debug("Zebra send: IPv4 route %s %s/%d metric %u tag %d",
1371 valid_nh_count
? "add" : "delete",
1372 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1373 p
->prefixlen
, api
.metric
, api
.tag
);
1374 for (i
= 0; i
< api
.nexthop_num
; i
++)
1375 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1376 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1380 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1381 zclient
, (struct prefix_ipv4
*) p
,
1382 (struct zapi_ipv6
*)&api
);
1384 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1385 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1389 if (bgp_debug_zebra(p
))
1392 zlog_debug("Zebra send: IPv6 route %s %s/%d metric %u tag %d",
1393 valid_nh_count
? "add" : "delete",
1394 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1395 p
->prefixlen
, api
.metric
, api
.tag
);
1396 for (i
= 0; i
< api
.nexthop_num
; i
++)
1397 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1398 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1401 zapi_ipv6_route (valid_nh_count
?
1402 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1403 zclient
, (struct prefix_ipv6
*) p
, &api
);
1406 #endif /* HAVE_IPV6 */
1409 /* Announce all routes of a table to zebra */
1411 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1413 struct bgp_node
*rn
;
1414 struct bgp_table
*table
;
1415 struct bgp_info
*ri
;
1417 table
= bgp
->rib
[afi
][safi
];
1420 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1421 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1422 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1423 && ri
->type
== ZEBRA_ROUTE_BGP
1424 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1425 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1429 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1434 if (zclient
->sock
< 0)
1437 if ((p
->family
== AF_INET
&&
1438 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], VRF_DEFAULT
))
1439 || (p
->family
== AF_INET6
&&
1440 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], VRF_DEFAULT
)))
1447 if (peer
->sort
== BGP_PEER_IBGP
)
1449 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1450 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1453 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1454 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1455 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1456 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1458 if (p
->family
== AF_INET
)
1460 struct zapi_ipv4 api
;
1462 api
.vrf_id
= VRF_DEFAULT
;
1465 api
.type
= ZEBRA_ROUTE_BGP
;
1469 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1470 api
.nexthop_num
= 0;
1472 api
.ifindex_num
= 0;
1473 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1474 api
.metric
= info
->attr
->med
;
1476 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1478 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1479 api
.tag
= info
->attr
->extra
->tag
;
1482 if (bgp_debug_zebra(p
))
1484 char buf
[2][INET_ADDRSTRLEN
];
1485 zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u tag %d",
1486 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1487 p
->prefixlen
, api
.metric
, api
.tag
);
1490 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1491 (struct prefix_ipv4
*) p
, &api
);
1494 /* We have to think about a IPv6 link-local address curse. */
1495 if (p
->family
== AF_INET6
)
1497 struct zapi_ipv6 api
;
1499 assert (info
->attr
->extra
);
1501 api
.vrf_id
= VRF_DEFAULT
;
1503 api
.type
= ZEBRA_ROUTE_BGP
;
1507 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1508 api
.nexthop_num
= 0;
1510 api
.ifindex_num
= 0;
1511 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1512 api
.metric
= info
->attr
->med
;
1514 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1516 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1517 api
.tag
= info
->attr
->extra
->tag
;
1520 if (bgp_debug_zebra(p
))
1522 char buf
[2][INET6_ADDRSTRLEN
];
1523 zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u tag %d",
1524 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1525 p
->prefixlen
, api
.metric
, api
.tag
);
1528 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1529 (struct prefix_ipv6
*) p
, &api
);
1531 #endif /* HAVE_IPV6 */
1534 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1536 struct list
*red_list
;
1537 struct listnode
*node
;
1538 struct bgp_redist
*red
;
1540 red_list
= bgp
->redist
[afi
][type
];
1544 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1545 if (red
->instance
== instance
)
1552 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1554 struct list
*red_list
;
1555 struct bgp_redist
*red
;
1557 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1561 if (!bgp
->redist
[afi
][type
])
1562 bgp
->redist
[afi
][type
] = list_new();
1564 red_list
= bgp
->redist
[afi
][type
];
1565 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1566 red
->instance
= instance
;
1568 listnode_add(red_list
, red
);
1574 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1576 struct bgp_redist
*red
;
1578 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1582 listnode_delete(bgp
->redist
[afi
][type
], red
);
1583 if (!bgp
->redist
[afi
][type
]->count
)
1585 list_free(bgp
->redist
[afi
][type
]);
1586 bgp
->redist
[afi
][type
] = NULL
;
1591 /* Other routes redistribution into BGP. */
1593 bgp_redistribute_set (afi_t afi
, int type
, u_short instance
)
1596 /* Return if already redistribute flag is set. */
1599 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1602 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1606 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], VRF_DEFAULT
))
1609 vrf_bitmap_set (zclient
->redist
[afi
][type
], VRF_DEFAULT
);
1612 /* Return if zebra connection is not established. */
1613 if (zclient
->sock
< 0)
1616 if (BGP_DEBUG (zebra
, ZEBRA
))
1617 zlog_debug("Zebra send: redistribute add afi %d %s %d", afi
,
1618 zebra_route_string(type
), instance
);
1620 /* Send distribute add message to zebra. */
1621 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1622 instance
, VRF_DEFAULT
);
1628 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1630 /* Return if zebra connection is not established. */
1631 if (zclient
->sock
< 0)
1634 if (BGP_DEBUG (zebra
, ZEBRA
))
1635 zlog_debug("Zebra send: redistribute delete/add afi %d %s %d", afi
,
1636 zebra_route_string(type
), instance
);
1638 /* Send distribute add message to zebra. */
1639 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1640 instance
, VRF_DEFAULT
);
1641 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1642 instance
, VRF_DEFAULT
);
1647 /* Redistribute with route-map specification. */
1649 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1652 && (strcmp (red
->rmap
.name
, name
) == 0))
1656 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1657 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1658 red
->rmap
.map
= route_map_lookup_by_name (name
);
1663 /* Redistribute with metric specification. */
1665 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1666 int type
, u_int32_t metric
)
1668 struct bgp_node
*rn
;
1669 struct bgp_info
*ri
;
1671 if (red
->redist_metric_flag
1672 && red
->redist_metric
== metric
)
1675 red
->redist_metric_flag
= 1;
1676 red
->redist_metric
= metric
;
1678 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
)) {
1679 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1680 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&& ri
->type
== type
&&
1681 ri
->instance
== red
->instance
) {
1682 ri
->attr
->med
= red
->redist_metric
;
1683 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1684 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1692 /* Unset redistribution. */
1694 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1696 struct bgp_redist
*red
;
1698 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1702 /* Unset route-map. */
1704 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1705 red
->rmap
.name
= NULL
;
1706 red
->rmap
.map
= NULL
;
1709 red
->redist_metric_flag
= 0;
1710 red
->redist_metric
= 0;
1712 bgp_redist_del(bgp
, afi
, type
, instance
);
1714 /* Return if zebra connection is disabled. */
1717 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1719 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1723 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], VRF_DEFAULT
))
1725 vrf_bitmap_unset (zclient
->redist
[afi
][type
], VRF_DEFAULT
);
1728 if (zclient
->sock
>= 0)
1730 /* Send distribute delete message to zebra. */
1731 if (BGP_DEBUG (zebra
, ZEBRA
))
1732 zlog_debug("Zebra send: redistribute delete afi %d %s %d",
1733 afi
, zebra_route_string(type
), instance
);
1734 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1738 /* Withdraw redistributed routes from current BGP's routing table. */
1739 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1745 bgp_zclient_reset (void)
1747 zclient_reset (zclient
);
1751 bgp_zebra_connected (struct zclient
*zclient
)
1753 zclient_send_requests (zclient
, VRF_DEFAULT
);
1755 bgp_nht_register_all();
1759 bgp_zebra_init (struct thread_master
*master
)
1761 /* Set default values. */
1762 zclient
= zclient_new (master
);
1763 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
1764 zclient
->zebra_connected
= bgp_zebra_connected
;
1765 zclient
->router_id_update
= bgp_router_id_update
;
1766 zclient
->interface_add
= bgp_interface_add
;
1767 zclient
->interface_delete
= bgp_interface_delete
;
1768 zclient
->interface_address_add
= bgp_interface_address_add
;
1769 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1770 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
1771 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
1772 zclient
->ipv4_route_add
= zebra_read_ipv4
;
1773 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
1774 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
1775 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
1776 zclient
->interface_up
= bgp_interface_up
;
1777 zclient
->interface_down
= bgp_interface_down
;
1779 zclient
->ipv6_route_add
= zebra_read_ipv6
;
1780 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
1781 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
1782 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
1783 #endif /* HAVE_IPV6 */
1784 zclient
->nexthop_update
= bgp_read_nexthop_update
;
1785 zclient
->import_check_update
= bgp_read_import_check_update
;
1787 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
1788 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);