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"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_nexthop.h"
41 #include "bgpd/bgp_zebra.h"
42 #include "bgpd/bgp_fsm.h"
43 #include "bgpd/bgp_debug.h"
44 #include "bgpd/bgp_mpath.h"
45 #include "bgpd/bgp_nexthop.h"
46 #include "bgpd/bgp_nht.h"
48 /* All information about zebra. */
49 struct zclient
*zclient
= NULL
;
51 /* Growable buffer for nexthops sent to zebra */
52 struct stream
*bgp_nexthop_buf
= NULL
;
53 struct stream
*bgp_ifindices_buf
= NULL
;
55 /* These array buffers are used in making a copy of the attributes for
56 route-map apply. Arrays are being used here to minimize mallocs and
57 frees for the temporary copy of the attributes.
58 Given the zapi api expects the nexthop buffer to contain pointer to
59 pointers for nexthops, we couldnt have used a single nexthop variable
60 on the stack, hence we had two options:
61 1. maintain a linked-list and free it after zapi_*_route call
62 2. use an array to avoid number of mallocs.
63 Number of supported next-hops are finite, use of arrays should be ok. */
64 struct attr attr_cp
[MULTIPATH_NUM
];
65 struct attr_extra attr_extra_cp
[MULTIPATH_NUM
];
68 /* Once per address-family initialization of the attribute array */
69 #define BGP_INFO_ATTR_BUF_INIT()\
71 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
72 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
76 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
78 *info_dst = *info_src; \
79 assert(attr_index != MULTIPATH_NUM);\
80 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
81 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
82 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
83 info_dst->attr = &attr_cp[attr_index]; \
87 #define BGP_INFO_ATTR_BUF_FREE(info) \
89 bgp_attr_deep_free(info->attr); \
93 /* Can we install into zebra? */
95 bgp_install_info_to_zebra (struct bgp
*bgp
)
97 if (zclient
->sock
<= 0)
100 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
106 /* Router-id update message from zebra. */
108 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
111 struct prefix router_id
;
113 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
115 if (BGP_DEBUG (zebra
, ZEBRA
))
117 char buf
[PREFIX2STR_BUFFER
];
118 prefix2str(&router_id
, buf
, sizeof(buf
));
119 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
122 bgp_router_id_zebra_bump (vrf_id
, &router_id
);
126 /* Nexthop update message from zebra. */
128 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
129 zebra_size_t length
, vrf_id_t vrf_id
)
131 bgp_parse_nexthop_update(command
, vrf_id
);
136 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
137 zebra_size_t length
, vrf_id_t vrf_id
)
139 bgp_parse_nexthop_update(command
, vrf_id
);
143 /* Set or clear interface on which unnumbered neighbor is configured. This
144 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
148 bgp_update_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
,
149 struct interface
*upd_ifp
)
151 struct listnode
*node
, *nnode
;
154 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
157 (strcmp (peer
->conf_if
, ifp
->name
) == 0))
162 bgp_zebra_initiate_radv (bgp
, peer
);
166 bgp_zebra_terminate_radv (bgp
, peer
);
174 bgp_start_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
)
176 struct listnode
*node
, *nnode
;
179 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
182 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
183 peer
->status
!= Established
)
185 if (peer_active(peer
))
186 BGP_EVENT_ADD (peer
, BGP_Stop
);
187 BGP_EVENT_ADD (peer
, BGP_Start
);
193 bgp_nbr_connected_add (struct bgp
*bgp
, struct nbr_connected
*ifc
)
195 struct listnode
*node
;
196 struct connected
*connected
;
197 struct interface
*ifp
;
200 /* Kick-off the FSM for any relevant peers only if there is a
201 * valid local address on the interface.
204 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
206 p
= connected
->address
;
207 if (p
->family
== AF_INET6
&&
208 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
214 bgp_start_interface_nbrs (bgp
, ifp
);
218 bgp_nbr_connected_delete (struct bgp
*bgp
, struct nbr_connected
*ifc
, int del
)
220 struct listnode
*node
, *nnode
;
222 struct interface
*ifp
;
224 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
226 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
228 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
229 BGP_EVENT_ADD (peer
, BGP_Stop
);
232 /* Free neighbor also, if we're asked to. */
236 listnode_delete (ifp
->nbr_connected
, ifc
);
237 nbr_connected_free (ifc
);
241 /* Inteface addition message from zebra. */
243 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
246 struct interface
*ifp
;
249 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
250 if (!ifp
) // unexpected
253 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
254 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
256 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
260 bgp_update_interface_nbrs (bgp
, ifp
, ifp
);
265 bgp_interface_delete (int command
, struct zclient
*zclient
,
266 zebra_size_t length
, vrf_id_t vrf_id
)
269 struct interface
*ifp
;
273 ifp
= zebra_interface_state_read (s
, vrf_id
);
274 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
277 ifp
->ifindex
= IFINDEX_DELETED
;
279 if (BGP_DEBUG (zebra
, ZEBRA
))
280 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
282 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
286 bgp_update_interface_nbrs (bgp
, ifp
, NULL
);
291 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
295 struct interface
*ifp
;
297 struct nbr_connected
*nc
;
298 struct listnode
*node
, *nnode
;
302 ifp
= zebra_interface_state_read (s
, vrf_id
);
307 if (BGP_DEBUG (zebra
, ZEBRA
))
308 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
310 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
314 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
315 bgp_connected_add (bgp
, c
);
317 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
318 bgp_nbr_connected_add (bgp
, nc
);
324 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
328 struct interface
*ifp
;
330 struct nbr_connected
*nc
;
331 struct listnode
*node
, *nnode
;
335 ifp
= zebra_interface_state_read (s
, vrf_id
);
339 if (BGP_DEBUG (zebra
, ZEBRA
))
340 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
342 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
346 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
347 bgp_connected_delete (bgp
, c
);
349 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
350 bgp_nbr_connected_delete (bgp
, nc
, 1);
352 /* Fast external-failover */
356 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
359 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
361 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
364 if (ifp
== peer
->nexthop
.ifp
)
366 BGP_EVENT_ADD (peer
, BGP_Stop
);
367 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
376 bgp_interface_address_add (int command
, struct zclient
*zclient
,
377 zebra_size_t length
, vrf_id_t vrf_id
)
379 struct connected
*ifc
;
381 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
386 if (bgp_debug_zebra(ifc
->address
))
388 char buf
[PREFIX2STR_BUFFER
];
389 prefix2str(ifc
->address
, buf
, sizeof(buf
));
390 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
391 vrf_id
, ifc
->ifp
->name
, buf
);
394 if (if_is_operative (ifc
->ifp
))
398 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
402 bgp_connected_add (bgp
, ifc
);
403 /* If we have learnt of any neighbors on this interface,
404 * check to kick off any BGP interface-based neighbors,
405 * but only if this is a link-local address.
407 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
408 !list_isempty(ifc
->ifp
->nbr_connected
))
409 bgp_start_interface_nbrs (bgp
, ifc
->ifp
);
416 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
417 zebra_size_t length
, vrf_id_t vrf_id
)
419 struct connected
*ifc
;
422 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
427 if (bgp_debug_zebra(ifc
->address
))
429 char buf
[PREFIX2STR_BUFFER
];
430 prefix2str(ifc
->address
, buf
, sizeof(buf
));
431 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
432 vrf_id
, ifc
->ifp
->name
, buf
);
435 if (if_is_operative (ifc
->ifp
))
437 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
439 bgp_connected_delete (bgp
, ifc
);
442 connected_free (ifc
);
448 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
449 zebra_size_t length
, vrf_id_t vrf_id
)
451 struct nbr_connected
*ifc
= NULL
;
454 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
459 if (bgp_debug_zebra(ifc
->address
))
461 char buf
[PREFIX2STR_BUFFER
];
462 prefix2str(ifc
->address
, buf
, sizeof(buf
));
463 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
464 vrf_id
, ifc
->ifp
->name
, buf
);
467 if (if_is_operative (ifc
->ifp
))
469 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
471 bgp_nbr_connected_add (bgp
, ifc
);
478 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
479 zebra_size_t length
, vrf_id_t vrf_id
)
481 struct nbr_connected
*ifc
= NULL
;
484 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
489 if (bgp_debug_zebra(ifc
->address
))
491 char buf
[PREFIX2STR_BUFFER
];
492 prefix2str(ifc
->address
, buf
, sizeof(buf
));
493 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
494 vrf_id
, ifc
->ifp
->name
, buf
);
497 if (if_is_operative (ifc
->ifp
))
499 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
501 bgp_nbr_connected_delete (bgp
, ifc
, 0);
504 nbr_connected_free (ifc
);
509 /* VRF update for an interface. */
511 bgp_interface_vrf_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
514 struct interface
*ifp
;
517 struct nbr_connected
*nc
;
518 struct listnode
*node
, *nnode
;
521 ifp
= zebra_interface_vrf_update_read (zclient
->ibuf
, vrf_id
, &new_vrf_id
);
525 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
526 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
527 vrf_id
, ifp
->name
, new_vrf_id
);
529 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
533 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
534 bgp_connected_delete (bgp
, c
);
536 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
537 bgp_nbr_connected_delete (bgp
, nc
, 1);
539 /* Fast external-failover */
543 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
546 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
548 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
551 if (ifp
== peer
->nexthop
.ifp
)
552 BGP_EVENT_ADD (peer
, BGP_Stop
);
556 if_update_vrf (ifp
, ifp
->name
, strlen (ifp
->name
), new_vrf_id
);
558 bgp
= bgp_lookup_by_vrf_id (new_vrf_id
);
562 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
563 bgp_connected_add (bgp
, c
);
565 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
566 bgp_nbr_connected_add (bgp
, nc
);
570 /* Zebra route add and delete treatment. */
572 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
576 struct zapi_ipv4 api
;
577 struct in_addr nexthop
;
578 struct prefix_ipv4 p
;
579 unsigned int ifindex
;
583 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
590 /* Type, flags, message. */
591 api
.type
= stream_getc (s
);
592 api
.instance
= stream_getw (s
);
593 api
.flags
= stream_getc (s
);
594 api
.message
= stream_getc (s
);
597 memset (&p
, 0, sizeof (struct prefix_ipv4
));
599 p
.prefixlen
= stream_getc (s
);
600 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
602 /* Nexthop, ifindex, distance, metric. */
603 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
605 api
.nexthop_num
= stream_getc (s
);
606 nexthop
.s_addr
= stream_get_ipv4 (s
);
609 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
611 api
.ifindex_num
= stream_getc (s
);
612 ifindex
= stream_getl (s
); /* ifindex, unused */
619 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
620 api
.distance
= stream_getc (s
);
622 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
623 api
.metric
= stream_getl (s
);
627 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
628 api
.tag
= stream_getw (s
);
632 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
634 if (bgp_debug_zebra((struct prefix
*)&p
))
636 char buf
[2][INET_ADDRSTRLEN
];
637 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
639 zebra_route_string(api
.type
), api
.instance
,
640 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
642 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
648 * The ADD message is actually an UPDATE and there is no explicit DEL
649 * for a prior redistributed route, if any. So, perform an implicit
650 * DEL processing for the same redistributed route from any other
653 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
656 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
659 /* Now perform the add/update. */
660 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
661 api
.metric
, api
.type
, api
.instance
, api
.tag
);
663 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
665 if (bgp_debug_zebra((struct prefix
*)&p
))
667 char buf
[2][INET_ADDRSTRLEN
];
668 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
669 "nexthop %s metric %u tag %d",
671 zebra_route_string(api
.type
), api
.instance
,
672 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
674 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
678 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
, api
.instance
);
685 /* Zebra route add and delete treatment. */
687 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
691 struct zapi_ipv6 api
;
692 struct in6_addr nexthop
;
693 struct prefix_ipv6 p
;
694 unsigned int ifindex
;
698 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
703 memset (&nexthop
, 0, sizeof (struct in6_addr
));
705 /* Type, flags, message. */
706 api
.type
= stream_getc (s
);
707 api
.instance
= stream_getw (s
);
708 api
.flags
= stream_getc (s
);
709 api
.message
= stream_getc (s
);
712 memset (&p
, 0, sizeof (struct prefix_ipv6
));
714 p
.prefixlen
= stream_getc (s
);
715 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
717 /* Nexthop, ifindex, distance, metric. */
718 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
720 api
.nexthop_num
= stream_getc (s
);
721 stream_get (&nexthop
, s
, 16);
724 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
726 api
.ifindex_num
= stream_getc (s
);
727 ifindex
= stream_getl (s
); /* ifindex, unused */
734 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
735 api
.distance
= stream_getc (s
);
739 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
740 api
.metric
= stream_getl (s
);
744 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
745 api
.tag
= stream_getw (s
);
749 /* Simply ignore link-local address. */
750 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
753 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
755 if (bgp_debug_zebra((struct prefix
*)&p
))
757 char buf
[2][INET6_ADDRSTRLEN
];
758 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
760 zebra_route_string(api
.type
), api
.instance
,
761 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
763 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
769 * The ADD message is actually an UPDATE and there is no explicit DEL
770 * for a prior redistributed route, if any. So, perform an implicit
771 * DEL processing for the same redistributed route from any other
774 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
777 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
780 bgp_redistribute_add (bgp
, (struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
781 api
.metric
, api
.type
, api
.instance
, api
.tag
);
783 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
785 if (bgp_debug_zebra((struct prefix
*)&p
))
787 char buf
[2][INET6_ADDRSTRLEN
];
788 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
789 "nexthop %s metric %u tag %d",
791 zebra_route_string(api
.type
), api
.instance
,
792 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
794 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
798 bgp_redistribute_delete (bgp
, (struct prefix
*) &p
, api
.type
, api
.instance
);
803 #endif /* HAVE_IPV6 */
806 if_lookup_by_ipv4 (struct in_addr
*addr
, vrf_id_t vrf_id
)
808 struct listnode
*ifnode
;
809 struct listnode
*cnode
;
810 struct interface
*ifp
;
811 struct connected
*connected
;
812 struct prefix_ipv4 p
;
817 p
.prefixlen
= IPV4_MAX_BITLEN
;
819 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
821 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
823 cp
= connected
->address
;
825 if (cp
->family
== AF_INET
)
826 if (prefix_match (cp
, (struct prefix
*)&p
))
834 if_lookup_by_ipv4_exact (struct in_addr
*addr
, vrf_id_t vrf_id
)
836 struct listnode
*ifnode
;
837 struct listnode
*cnode
;
838 struct interface
*ifp
;
839 struct connected
*connected
;
842 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
844 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
846 cp
= connected
->address
;
848 if (cp
->family
== AF_INET
)
849 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
858 if_lookup_by_ipv6 (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
860 struct listnode
*ifnode
;
861 struct listnode
*cnode
;
862 struct interface
*ifp
;
863 struct connected
*connected
;
864 struct prefix_ipv6 p
;
869 p
.prefixlen
= IPV6_MAX_BITLEN
;
871 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
873 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
875 cp
= connected
->address
;
877 if (cp
->family
== AF_INET6
)
878 if (prefix_match (cp
, (struct prefix
*)&p
))
880 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
882 if (ifindex
== ifp
->ifindex
)
894 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
896 struct listnode
*ifnode
;
897 struct listnode
*cnode
;
898 struct interface
*ifp
;
899 struct connected
*connected
;
902 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
904 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
906 cp
= connected
->address
;
908 if (cp
->family
== AF_INET6
)
909 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
911 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
913 if (ifindex
== ifp
->ifindex
)
925 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
927 struct listnode
*cnode
;
928 struct connected
*connected
;
931 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
933 cp
= connected
->address
;
935 if (cp
->family
== AF_INET6
)
936 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
938 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
946 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
948 struct listnode
*cnode
;
949 struct connected
*connected
;
952 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
954 cp
= connected
->address
;
956 if (cp
->family
== AF_INET6
)
957 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
959 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
965 #endif /* HAVE_IPV6 */
968 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
970 struct listnode
*cnode
;
971 struct connected
*connected
;
974 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
976 cp
= connected
->address
;
977 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
979 *addr
= cp
->u
.prefix4
;
987 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
988 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
991 struct interface
*ifp
= NULL
;
993 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
1000 if (local
->sa
.sa_family
== AF_INET
)
1002 nexthop
->v4
= local
->sin
.sin_addr
;
1003 if (peer
->update_if
)
1004 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1006 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
, peer
->bgp
->vrf_id
);
1009 if (local
->sa
.sa_family
== AF_INET6
)
1011 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1013 if (peer
->conf_if
|| peer
->ifname
)
1014 ifp
= if_lookup_by_name_vrf (peer
->conf_if
? peer
->conf_if
: peer
->ifname
, peer
->bgp
->vrf_id
);
1016 else if (peer
->update_if
)
1017 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1019 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
1020 local
->sin6
.sin6_scope_id
,
1023 #endif /* HAVE_IPV6 */
1030 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1031 if (local
->sa
.sa_family
== AF_INET
)
1035 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1039 /* There is no global nexthop. Use link-local address as both the
1040 * global and link-local nexthop. In this scenario, the expectation
1041 * for interop is that the network admin would use a route-map to
1042 * specify the global IPv6 nexthop.
1044 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
1045 memcpy (&nexthop
->v6_local
, &nexthop
->v6_global
,
1049 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1051 if (if_lookup_by_ipv4 (&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1052 peer
->shared_network
= 1;
1054 peer
->shared_network
= 0;
1055 #endif /* HAVE_IPV6 */
1059 /* IPv6 connection, fetch and store IPv4 local address if any. */
1060 if (local
->sa
.sa_family
== AF_INET6
)
1062 struct interface
*direct
= NULL
;
1065 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1066 if (!ret
&& peer
->local_id
.s_addr
)
1067 nexthop
->v4
= peer
->local_id
;
1070 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1072 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1075 /* If directory connected set link-local address. */
1076 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
1077 remote
->sin6
.sin6_scope_id
, peer
->bgp
->vrf_id
);
1079 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1082 /* Link-local address. */
1084 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1086 /* If there is no global address. Set link-local address as
1087 global. I know this break RFC specification... */
1088 /* In this scenario, the expectation for interop is that the
1089 * network admin would use a route-map to specify the global
1093 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1095 /* Always set the link-local address */
1096 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1100 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
1101 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
,
1103 peer
->shared_network
= 1;
1105 peer
->shared_network
= 0;
1108 /* KAME stack specific treatment. */
1110 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
1111 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
1113 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
1115 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
1116 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
1118 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
1121 #endif /* HAVE_IPV6 */
1123 /* If we have identified the local interface, there is no error for now. */
1127 static struct in6_addr
*
1128 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
1130 struct in6_addr
*nexthop
= NULL
;
1132 /* Only global address nexthop exists. */
1133 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1134 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1136 /* If both global and link-local address present. */
1137 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1139 /* Check if route-map is set to prefer global over link-local */
1140 if (info
->attr
->extra
->mp_nexthop_prefer_global
)
1141 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1144 /* Workaround for Cisco's nexthop bug. */
1145 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
1146 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1147 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
1149 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1157 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
1158 struct bgp_info
*info
)
1160 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1163 if (bgp_debug_zebra(p
))
1165 if (p
->family
== AF_INET
)
1167 char buf
[2][INET_ADDRSTRLEN
];
1168 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1169 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1171 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1174 if (p
->family
== AF_INET6
)
1176 char buf
[2][INET6_ADDRSTRLEN
];
1177 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1178 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1180 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1188 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1189 afi_t afi
, safi_t safi
)
1194 struct bgp_info
*mpinfo
;
1195 size_t oldsize
, newsize
;
1196 u_int32_t nhcount
, metric
;
1197 struct bgp_info local_info
;
1198 struct bgp_info
*info_cp
= &local_info
;
1201 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1202 * know of this instance.
1204 if (!bgp_install_info_to_zebra (bgp
))
1207 if ((p
->family
== AF_INET
&&
1208 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
))
1209 || (p
->family
== AF_INET6
&&
1210 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
)))
1213 if (bgp
->main_zebra_update_hold
)
1219 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1220 tag
= info
->attr
->extra
->tag
;
1224 /* When we create an aggregate route we must also install a Null0 route in
1226 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1227 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1229 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1230 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1232 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1233 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1236 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1237 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1238 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1240 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1242 nhcount
= 1 + bgp_info_mpath_count (info
);
1244 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1246 struct zapi_ipv4 api
;
1247 struct in_addr
*nexthop
;
1248 char buf
[2][INET_ADDRSTRLEN
];
1249 int valid_nh_count
= 0;
1251 /* resize nexthop buffer size if necessary */
1252 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1253 (sizeof (struct in_addr
*) * nhcount
))
1255 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1256 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1257 if (newsize
== oldsize
)
1259 zlog_err ("can't resize nexthop buffer");
1263 stream_reset (bgp_nexthop_buf
);
1266 /* Metric is currently based on the best-path only. */
1267 metric
= info
->attr
->med
;
1269 if (bgp
->table_map
[afi
][safi
].name
)
1271 BGP_INFO_ATTR_BUF_INIT();
1273 /* Copy info and attributes, so the route-map apply doesn't modify the
1275 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1276 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1278 metric
= info_cp
->attr
->med
;
1279 nexthop
= &info_cp
->attr
->nexthop
;
1281 if (info_cp
->attr
->extra
)
1282 tag
= info_cp
->attr
->extra
->tag
;
1284 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1288 nexthop
= &info
->attr
->nexthop
;
1293 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1297 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1298 mpinfo
= bgp_info_mpath_next (mpinfo
))
1302 if (bgp
->table_map
[afi
][safi
].name
)
1304 /* Copy info and attributes, so the route-map apply doesn't modify the
1306 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1307 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1308 nexthop
= &info_cp
->attr
->nexthop
;
1309 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1313 nexthop
= &mpinfo
->attr
->nexthop
;
1316 if (nexthop
== NULL
)
1319 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1323 api
.vrf_id
= bgp
->vrf_id
;
1325 api
.type
= ZEBRA_ROUTE_BGP
;
1329 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1331 /* Note that this currently only applies to Null0 routes for aggregates.
1332 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1333 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1334 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1336 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1337 api
.nexthop_num
= 0;
1339 api
.nexthop_num
= valid_nh_count
;
1341 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1342 api
.ifindex_num
= 0;
1343 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1344 api
.metric
= metric
;
1349 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1353 distance
= bgp_distance_apply (p
, info
, bgp
);
1357 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1358 api
.distance
= distance
;
1361 if (bgp_debug_zebra(p
))
1364 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d"
1365 " count %d", (valid_nh_count
? "add":"delete"),
1367 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1368 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1369 for (i
= 0; i
< api
.nexthop_num
; i
++)
1370 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1371 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1374 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1375 zclient
, (struct prefix_ipv4
*) p
, &api
);
1379 /* We have to think about a IPv6 link-local address curse. */
1380 if (p
->family
== AF_INET6
||
1381 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1383 unsigned int ifindex
;
1384 struct in6_addr
*nexthop
;
1385 struct zapi_ipv6 api
;
1386 int valid_nh_count
= 0;
1387 char buf
[2][INET6_ADDRSTRLEN
];
1389 /* resize nexthop buffer size if necessary */
1390 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1391 (sizeof (struct in6_addr
*) * nhcount
))
1393 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1394 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1395 if (newsize
== oldsize
)
1397 zlog_err ("can't resize nexthop buffer");
1401 stream_reset (bgp_nexthop_buf
);
1403 /* resize ifindices buffer size if necessary */
1404 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1405 (sizeof (unsigned int) * nhcount
))
1407 newsize
= (sizeof (unsigned int) * nhcount
);
1408 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1409 if (newsize
== oldsize
)
1411 zlog_err ("can't resize nexthop buffer");
1415 stream_reset (bgp_ifindices_buf
);
1420 assert (info
->attr
->extra
);
1422 /* Metric is currently based on the best-path only. */
1423 metric
= info
->attr
->med
;
1425 if (bgp
->table_map
[afi
][safi
].name
)
1427 BGP_INFO_ATTR_BUF_INIT();
1429 /* Copy info and attributes, so the route-map apply doesn't modify the
1431 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1432 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1434 metric
= info_cp
->attr
->med
;
1435 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1437 if (info_cp
->attr
->extra
)
1438 tag
= info_cp
->attr
->extra
->tag
;
1440 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1444 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1449 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1450 if (info
->peer
->nexthop
.ifp
)
1451 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1455 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1456 ifindex
= if_nametoindex (info
->peer
->conf_if
? info
->peer
->conf_if
: info
->peer
->ifname
);
1457 else if (info
->peer
->nexthop
.ifp
)
1458 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1460 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1461 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1465 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1466 mpinfo
= bgp_info_mpath_next (mpinfo
))
1471 if (bgp
->table_map
[afi
][safi
].name
)
1473 /* Copy info and attributes, so the route-map apply doesn't modify the
1475 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1476 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1477 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1478 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1482 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1485 if (nexthop
== NULL
)
1488 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1489 if (mpinfo
->peer
->nexthop
.ifp
)
1490 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1494 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1495 ifindex
= ifname2ifindex (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
: mpinfo
->peer
->ifname
);
1496 else if (mpinfo
->peer
->nexthop
.ifp
)
1497 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1502 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1503 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1507 /* Make Zebra API structure. */
1508 api
.vrf_id
= bgp
->vrf_id
;
1510 api
.type
= ZEBRA_ROUTE_BGP
;
1514 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1516 /* Note that this currently only applies to Null0 routes for aggregates.
1517 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1518 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1519 * do not want to also encode the :: nexthop for the aggregate route.
1521 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1522 api
.nexthop_num
= 0;
1524 api
.nexthop_num
= valid_nh_count
;
1526 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1527 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1528 api
.ifindex_num
= valid_nh_count
;
1529 api
.ifindex
= (unsigned int *)STREAM_DATA (bgp_ifindices_buf
);
1530 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1531 api
.metric
= metric
;
1536 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1540 if (p
->family
== AF_INET
)
1542 if (bgp_debug_zebra(p
))
1545 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d",
1546 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1547 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1548 p
->prefixlen
, api
.metric
, api
.tag
);
1549 for (i
= 0; i
< api
.nexthop_num
; i
++)
1550 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1551 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1555 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1556 zclient
, (struct prefix_ipv4
*) p
,
1557 (struct zapi_ipv6
*)&api
);
1559 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1560 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1564 if (bgp_debug_zebra(p
))
1567 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %d",
1568 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1569 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1570 p
->prefixlen
, api
.metric
, api
.tag
);
1571 for (i
= 0; i
< api
.nexthop_num
; i
++)
1572 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1573 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1576 zapi_ipv6_route (valid_nh_count
?
1577 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1578 zclient
, (struct prefix_ipv6
*) p
, &api
);
1581 #endif /* HAVE_IPV6 */
1584 /* Announce all routes of a table to zebra */
1586 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1588 struct bgp_node
*rn
;
1589 struct bgp_table
*table
;
1590 struct bgp_info
*ri
;
1592 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1593 * know of this instance.
1595 if (!bgp_install_info_to_zebra (bgp
))
1598 table
= bgp
->rib
[afi
][safi
];
1601 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1602 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1603 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1604 && ri
->type
== ZEBRA_ROUTE_BGP
1605 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1606 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1610 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1618 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1619 * know of this instance.
1621 if (!bgp_install_info_to_zebra (peer
->bgp
))
1624 if ((p
->family
== AF_INET
&&
1625 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
))
1626 || (p
->family
== AF_INET6
&&
1627 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
)))
1632 if (peer
->sort
== BGP_PEER_IBGP
)
1634 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1635 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1638 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1639 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1640 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1641 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1643 if (p
->family
== AF_INET
)
1645 struct zapi_ipv4 api
;
1647 api
.vrf_id
= peer
->bgp
->vrf_id
;
1650 api
.type
= ZEBRA_ROUTE_BGP
;
1654 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1655 api
.nexthop_num
= 0;
1657 api
.ifindex_num
= 0;
1658 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1659 api
.metric
= info
->attr
->med
;
1662 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1664 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1665 api
.tag
= info
->attr
->extra
->tag
;
1668 if (bgp_debug_zebra(p
))
1670 char buf
[2][INET_ADDRSTRLEN
];
1671 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %d",
1673 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1674 p
->prefixlen
, api
.metric
, api
.tag
);
1677 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1678 (struct prefix_ipv4
*) p
, &api
);
1681 /* We have to think about a IPv6 link-local address curse. */
1682 if (p
->family
== AF_INET6
)
1684 struct zapi_ipv6 api
;
1686 assert (info
->attr
->extra
);
1688 api
.vrf_id
= peer
->bgp
->vrf_id
;
1690 api
.type
= ZEBRA_ROUTE_BGP
;
1694 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1695 api
.nexthop_num
= 0;
1697 api
.ifindex_num
= 0;
1698 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1699 api
.metric
= info
->attr
->med
;
1702 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1704 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1705 api
.tag
= info
->attr
->extra
->tag
;
1708 if (bgp_debug_zebra(p
))
1710 char buf
[2][INET6_ADDRSTRLEN
];
1711 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %d",
1713 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1714 p
->prefixlen
, api
.metric
, api
.tag
);
1717 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1718 (struct prefix_ipv6
*) p
, &api
);
1720 #endif /* HAVE_IPV6 */
1723 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1725 struct list
*red_list
;
1726 struct listnode
*node
;
1727 struct bgp_redist
*red
;
1729 red_list
= bgp
->redist
[afi
][type
];
1733 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1734 if (red
->instance
== instance
)
1741 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1743 struct list
*red_list
;
1744 struct bgp_redist
*red
;
1746 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1750 if (!bgp
->redist
[afi
][type
])
1751 bgp
->redist
[afi
][type
] = list_new();
1753 red_list
= bgp
->redist
[afi
][type
];
1754 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1755 red
->instance
= instance
;
1757 listnode_add(red_list
, red
);
1763 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1765 struct bgp_redist
*red
;
1767 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1771 listnode_delete(bgp
->redist
[afi
][type
], red
);
1772 if (!bgp
->redist
[afi
][type
]->count
)
1774 list_free(bgp
->redist
[afi
][type
]);
1775 bgp
->redist
[afi
][type
] = NULL
;
1780 /* Other routes redistribution into BGP. */
1782 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1785 /* Return if already redistribute flag is set. */
1788 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1791 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1795 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1798 vrf_bitmap_set (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1801 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1802 * know of this instance.
1804 if (!bgp_install_info_to_zebra (bgp
))
1807 if (BGP_DEBUG (zebra
, ZEBRA
))
1808 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1810 zebra_route_string(type
), instance
);
1812 /* Send distribute add message to zebra. */
1813 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1814 instance
, bgp
->vrf_id
);
1820 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1822 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1823 * know of this instance.
1825 if (!bgp_install_info_to_zebra (bgp
))
1828 if (BGP_DEBUG (zebra
, ZEBRA
))
1829 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1831 zebra_route_string(type
), instance
);
1833 /* Send distribute add message to zebra. */
1834 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1835 instance
, bgp
->vrf_id
);
1836 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1837 instance
, bgp
->vrf_id
);
1842 /* Redistribute with route-map specification. */
1844 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1847 && (strcmp (red
->rmap
.name
, name
) == 0))
1851 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1852 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1853 red
->rmap
.map
= route_map_lookup_by_name (name
);
1858 /* Redistribute with metric specification. */
1860 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1861 int type
, u_int32_t metric
)
1863 struct bgp_node
*rn
;
1864 struct bgp_info
*ri
;
1866 if (red
->redist_metric_flag
1867 && red
->redist_metric
== metric
)
1870 red
->redist_metric_flag
= 1;
1871 red
->redist_metric
= metric
;
1873 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
))
1875 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1877 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&&
1879 ri
->instance
== red
->instance
)
1881 struct attr
*old_attr
;
1882 struct attr new_attr
;
1883 struct attr_extra new_extra
;
1885 new_attr
.extra
= &new_extra
;
1886 bgp_attr_dup (&new_attr
, ri
->attr
);
1887 new_attr
.med
= red
->redist_metric
;
1888 old_attr
= ri
->attr
;
1889 ri
->attr
= bgp_attr_intern (&new_attr
);
1890 bgp_attr_unintern (&old_attr
);
1892 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1893 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1901 /* Unset redistribution. */
1903 bgp_redistribute_unreg (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1905 struct bgp_redist
*red
;
1907 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1911 /* Return if zebra connection is disabled. */
1914 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1916 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1920 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1922 vrf_bitmap_unset (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1925 if (bgp_install_info_to_zebra (bgp
))
1927 /* Send distribute delete message to zebra. */
1928 if (BGP_DEBUG (zebra
, ZEBRA
))
1929 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1930 bgp
->vrf_id
, afi
, zebra_route_string(type
), instance
);
1931 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1935 /* Withdraw redistributed routes from current BGP's routing table. */
1936 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1941 /* Unset redistribution. */
1943 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1945 struct bgp_redist
*red
;
1947 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1951 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1953 /* Unset route-map. */
1955 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1956 red
->rmap
.name
= NULL
;
1957 red
->rmap
.map
= NULL
;
1960 red
->redist_metric_flag
= 0;
1961 red
->redist_metric
= 0;
1963 bgp_redist_del(bgp
, afi
, type
, instance
);
1968 /* Update redistribute vrf bitmap during triggers like
1969 restart networking or delete/add VRFs */
1971 bgp_update_redist_vrf_bitmaps (struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1976 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1977 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1978 if (vrf_bitmap_check (zclient
->redist
[afi
][i
], old_vrf_id
))
1980 vrf_bitmap_unset (zclient
->redist
[afi
][i
], old_vrf_id
);
1981 vrf_bitmap_set (zclient
->redist
[afi
][i
], bgp
->vrf_id
);
1987 bgp_zclient_reset (void)
1989 zclient_reset (zclient
);
1992 /* Register this instance with Zebra. Invoked upon connect (for
1993 * default instance) and when other VRFs are learnt (or created and
1997 bgp_zebra_instance_register (struct bgp
*bgp
)
1999 /* Don't try to register if we're not connected to Zebra */
2000 if (!zclient
|| zclient
->sock
< 0)
2003 if (BGP_DEBUG (zebra
, ZEBRA
))
2004 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2006 /* Register for router-id, interfaces, redistributed routes. */
2007 zclient_send_reg_requests (zclient
, bgp
->vrf_id
);
2010 /* Deregister this instance with Zebra. Invoked upon the instance
2011 * being deleted (default or VRF) and it is already registered.
2014 bgp_zebra_instance_deregister (struct bgp
*bgp
)
2016 /* Don't try to deregister if we're not connected to Zebra */
2017 if (zclient
->sock
< 0)
2020 if (BGP_DEBUG (zebra
, ZEBRA
))
2021 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2023 /* Deregister for router-id, interfaces, redistributed routes. */
2024 zclient_send_dereg_requests (zclient
, bgp
->vrf_id
);
2028 bgp_zebra_initiate_radv (struct bgp
*bgp
, struct peer
*peer
)
2030 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2032 /* Don't try to initiate if we're not connected to Zebra */
2033 if (zclient
->sock
< 0)
2036 if (BGP_DEBUG (zebra
, ZEBRA
))
2037 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2039 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 1, ra_interval
);
2043 bgp_zebra_terminate_radv (struct bgp
*bgp
, struct peer
*peer
)
2045 /* Don't try to terminate if we're not connected to Zebra */
2046 if (zclient
->sock
< 0)
2049 if (BGP_DEBUG (zebra
, ZEBRA
))
2050 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2052 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
2055 /* BGP has established connection with Zebra. */
2057 bgp_zebra_connected (struct zclient
*zclient
)
2061 /* At this point, we may or may not have BGP instances configured, but
2062 * we're only interested in the default VRF (others wouldn't have learnt
2063 * the VRF from Zebra yet.)
2065 bgp
= bgp_get_default();
2069 bgp_zebra_instance_register (bgp
);
2071 /* Send the client registration */
2072 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2074 /* TODO - What if we have peers and networks configured, do we have to
2081 bgp_zebra_init (struct thread_master
*master
)
2083 /* Set default values. */
2084 zclient
= zclient_new (master
);
2085 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
2086 zclient
->zebra_connected
= bgp_zebra_connected
;
2087 zclient
->router_id_update
= bgp_router_id_update
;
2088 zclient
->interface_add
= bgp_interface_add
;
2089 zclient
->interface_delete
= bgp_interface_delete
;
2090 zclient
->interface_address_add
= bgp_interface_address_add
;
2091 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2092 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2093 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
2094 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2095 zclient
->ipv4_route_add
= zebra_read_ipv4
;
2096 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
2097 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2098 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2099 zclient
->interface_up
= bgp_interface_up
;
2100 zclient
->interface_down
= bgp_interface_down
;
2101 zclient
->ipv6_route_add
= zebra_read_ipv6
;
2102 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
2103 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2104 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2105 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2106 zclient
->import_check_update
= bgp_read_import_check_update
;
2108 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
2109 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);
2113 bgp_zebra_destroy(void)
2115 if (zclient
== NULL
)
2117 zclient_stop(zclient
);
2118 zclient_free(zclient
);