2 * IS-IS Rout(e)ing protocol - isis_zebra.c
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
7 * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "isisd/isis_constants.h"
41 #include "isisd/isis_common.h"
42 #include "isisd/isis_flags.h"
43 #include "isisd/isis_misc.h"
44 #include "isisd/isis_circuit.h"
45 #include "isisd/isisd.h"
46 #include "isisd/isis_circuit.h"
47 #include "isisd/isis_csm.h"
48 #include "isisd/isis_lsp.h"
49 #include "isisd/isis_route.h"
50 #include "isisd/isis_zebra.h"
51 #include "isisd/isis_te.h"
53 struct zclient
*zclient
= NULL
;
55 /* Router-id update message from zebra. */
56 static int isis_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
58 struct isis_area
*area
;
59 struct listnode
*node
;
60 struct prefix router_id
;
62 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
63 if (isis
->router_id
== router_id
.u
.prefix4
.s_addr
)
66 isis
->router_id
= router_id
.u
.prefix4
.s_addr
;
67 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
68 if (listcount(area
->area_addrs
) > 0)
69 lsp_regenerate_schedule(area
, area
->is_type
, 0);
74 static int isis_zebra_if_add(ZAPI_CALLBACK_ARGS
)
76 struct interface
*ifp
;
78 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
80 if (if_is_operative(ifp
))
81 isis_csm_state_change(IF_UP_FROM_Z
, circuit_scan_by_ifp(ifp
),
87 static int isis_zebra_if_del(ZAPI_CALLBACK_ARGS
)
89 struct interface
*ifp
;
93 ifp
= zebra_interface_state_read(s
, vrf_id
);
98 if (if_is_operative(ifp
))
99 zlog_warn("Zebra: got delete of %s, but interface is still up",
102 isis_csm_state_change(IF_DOWN_FROM_Z
, circuit_scan_by_ifp(ifp
), ifp
);
104 /* Cannot call if_delete because we should retain the pseudo interface
105 in case there is configuration info attached to it. */
106 if_delete_retain(ifp
);
108 if_set_index(ifp
, IFINDEX_INTERNAL
);
113 static int isis_zebra_if_state_up(ZAPI_CALLBACK_ARGS
)
115 struct interface
*ifp
;
117 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
122 isis_csm_state_change(IF_UP_FROM_Z
, circuit_scan_by_ifp(ifp
), ifp
);
127 static int isis_zebra_if_state_down(ZAPI_CALLBACK_ARGS
)
129 struct interface
*ifp
;
130 struct isis_circuit
*circuit
;
132 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
137 circuit
= isis_csm_state_change(IF_DOWN_FROM_Z
,
138 circuit_scan_by_ifp(ifp
), ifp
);
140 SET_FLAG(circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
);
145 static int isis_zebra_if_address_add(ZAPI_CALLBACK_ARGS
)
149 char buf
[PREFIX2STR_BUFFER
];
151 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
152 zclient
->ibuf
, vrf_id
);
159 prefix2str(p
, buf
, sizeof(buf
));
161 if (p
->family
== AF_INET
)
162 zlog_debug("connected IP address %s", buf
);
163 if (p
->family
== AF_INET6
)
164 zlog_debug("connected IPv6 address %s", buf
);
165 #endif /* EXTREME_DEBUG */
166 if (if_is_operative(c
->ifp
))
167 isis_circuit_add_addr(circuit_scan_by_ifp(c
->ifp
), c
);
172 static int isis_zebra_if_address_del(ZAPI_CALLBACK_ARGS
)
175 struct interface
*ifp
;
178 char buf
[PREFIX2STR_BUFFER
];
179 #endif /* EXTREME_DEBUG */
181 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
182 zclient
->ibuf
, vrf_id
);
191 prefix2str(p
, buf
, sizeof(buf
));
193 if (p
->family
== AF_INET
)
194 zlog_debug("disconnected IP address %s", buf
);
195 if (p
->family
== AF_INET6
)
196 zlog_debug("disconnected IPv6 address %s", buf
);
197 #endif /* EXTREME_DEBUG */
199 if (if_is_operative(ifp
))
200 isis_circuit_del_addr(circuit_scan_by_ifp(ifp
), c
);
206 static int isis_zebra_link_params(ZAPI_CALLBACK_ARGS
)
208 struct interface
*ifp
;
210 ifp
= zebra_interface_link_params_read(zclient
->ibuf
, vrf_id
);
216 isis_mpls_te_update(ifp
);
221 static void isis_zebra_route_add_route(struct prefix
*prefix
,
222 struct prefix_ipv6
*src_p
,
223 struct isis_route_info
*route_info
)
225 struct zapi_route api
;
226 struct zapi_nexthop
*api_nh
;
227 struct isis_nexthop
*nexthop
;
228 struct isis_nexthop6
*nexthop6
;
229 struct listnode
*node
;
232 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
235 memset(&api
, 0, sizeof(api
));
236 api
.vrf_id
= VRF_DEFAULT
;
237 api
.type
= PROTO_TYPE
;
238 api
.safi
= SAFI_UNICAST
;
239 api
.prefix
= *prefix
;
240 if (src_p
&& src_p
->prefixlen
) {
241 api
.src_prefix
= *src_p
;
242 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
244 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
245 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
246 api
.metric
= route_info
->cost
;
248 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
249 api
.distance
= route_info
->depth
;
253 switch (prefix
->family
) {
255 for (ALL_LIST_ELEMENTS_RO(route_info
->nexthops
, node
,
257 if (count
>= MULTIPATH_NUM
)
259 api_nh
= &api
.nexthops
[count
];
261 api_nh
->onlink
= true;
262 api_nh
->vrf_id
= VRF_DEFAULT
;
263 /* FIXME: can it be ? */
264 if (nexthop
->ip
.s_addr
!= INADDR_ANY
) {
265 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
266 api_nh
->gate
.ipv4
= nexthop
->ip
;
268 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
270 api_nh
->ifindex
= nexthop
->ifindex
;
275 for (ALL_LIST_ELEMENTS_RO(route_info
->nexthops6
, node
,
277 if (count
>= MULTIPATH_NUM
)
279 if (!IN6_IS_ADDR_LINKLOCAL(&nexthop6
->ip6
)
280 && !IN6_IS_ADDR_UNSPECIFIED(&nexthop6
->ip6
)) {
284 api_nh
= &api
.nexthops
[count
];
286 api_nh
->onlink
= true;
287 api_nh
->vrf_id
= VRF_DEFAULT
;
288 api_nh
->gate
.ipv6
= nexthop6
->ip6
;
289 api_nh
->ifindex
= nexthop6
->ifindex
;
290 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
298 api
.nexthop_num
= count
;
300 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
301 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
302 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
305 static void isis_zebra_route_del_route(struct prefix
*prefix
,
306 struct prefix_ipv6
*src_p
,
307 struct isis_route_info
*route_info
)
309 struct zapi_route api
;
311 if (!CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
314 memset(&api
, 0, sizeof(api
));
315 api
.vrf_id
= VRF_DEFAULT
;
316 api
.type
= PROTO_TYPE
;
317 api
.safi
= SAFI_UNICAST
;
318 api
.prefix
= *prefix
;
319 if (src_p
&& src_p
->prefixlen
) {
320 api
.src_prefix
= *src_p
;
321 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
324 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
325 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
328 void isis_zebra_route_update(struct prefix
*prefix
,
329 struct prefix_ipv6
*src_p
,
330 struct isis_route_info
*route_info
)
332 if (zclient
->sock
< 0)
335 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
336 isis_zebra_route_add_route(prefix
, src_p
, route_info
);
338 isis_zebra_route_del_route(prefix
, src_p
, route_info
);
341 static int isis_zebra_read(ZAPI_CALLBACK_ARGS
)
343 struct zapi_route api
;
345 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
348 if (api
.prefix
.family
== AF_INET6
349 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
353 * Avoid advertising a false default reachability. (A default
354 * route installed by IS-IS gets redistributed from zebra back
355 * into IS-IS causing us to start advertising default reachabity
356 * without this check)
358 if (api
.prefix
.prefixlen
== 0
359 && api
.src_prefix
.prefixlen
== 0
360 && api
.type
== PROTO_TYPE
) {
361 cmd
= ZEBRA_REDISTRIBUTE_ROUTE_DEL
;
364 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
365 isis_redist_add(api
.type
, &api
.prefix
, &api
.src_prefix
,
366 api
.distance
, api
.metric
);
368 isis_redist_delete(api
.type
, &api
.prefix
, &api
.src_prefix
);
373 int isis_distribute_list_update(int routetype
)
378 void isis_zebra_redistribute_set(afi_t afi
, int type
)
380 if (type
== DEFAULT_ROUTE
)
381 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
382 zclient
, afi
, VRF_DEFAULT
);
384 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
388 void isis_zebra_redistribute_unset(afi_t afi
, int type
)
390 if (type
== DEFAULT_ROUTE
)
391 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
392 zclient
, afi
, VRF_DEFAULT
);
394 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
395 type
, 0, VRF_DEFAULT
);
398 static void isis_zebra_connected(struct zclient
*zclient
)
400 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
403 void isis_zebra_init(struct thread_master
*master
)
405 zclient
= zclient_new(master
, &zclient_options_default
);
406 zclient_init(zclient
, PROTO_TYPE
, 0, &isisd_privs
);
407 zclient
->zebra_connected
= isis_zebra_connected
;
408 zclient
->router_id_update
= isis_router_id_update_zebra
;
409 zclient
->interface_add
= isis_zebra_if_add
;
410 zclient
->interface_delete
= isis_zebra_if_del
;
411 zclient
->interface_up
= isis_zebra_if_state_up
;
412 zclient
->interface_down
= isis_zebra_if_state_down
;
413 zclient
->interface_address_add
= isis_zebra_if_address_add
;
414 zclient
->interface_address_delete
= isis_zebra_if_address_del
;
415 zclient
->interface_link_params
= isis_zebra_link_params
;
416 zclient
->redistribute_route_add
= isis_zebra_read
;
417 zclient
->redistribute_route_del
= isis_zebra_read
;
422 void isis_zebra_stop(void)
424 zclient_stop(zclient
);
425 zclient_free(zclient
);