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/dict.h"
41 #include "isisd/isis_constants.h"
42 #include "isisd/isis_common.h"
43 #include "isisd/isis_flags.h"
44 #include "isisd/isis_misc.h"
45 #include "isisd/isis_circuit.h"
46 #include "isisd/isisd.h"
47 #include "isisd/isis_circuit.h"
48 #include "isisd/isis_csm.h"
49 #include "isisd/isis_lsp.h"
50 #include "isisd/isis_route.h"
51 #include "isisd/isis_zebra.h"
52 #include "isisd/isis_te.h"
54 struct zclient
*zclient
= NULL
;
56 /* Router-id update message from zebra. */
57 static int isis_router_id_update_zebra(int command
, struct zclient
*zclient
,
58 zebra_size_t length
, vrf_id_t vrf_id
)
60 struct isis_area
*area
;
61 struct listnode
*node
;
62 struct prefix router_id
;
65 * If ISIS TE is enable, TE Router ID is set through specific command.
66 * See mpls_te_router_addr() command in isis_te.c
68 if (IS_MPLS_TE(isisMplsTE
))
71 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
72 if (isis
->router_id
== router_id
.u
.prefix4
.s_addr
)
75 isis
->router_id
= router_id
.u
.prefix4
.s_addr
;
76 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
77 if (listcount(area
->area_addrs
) > 0)
78 lsp_regenerate_schedule(area
, area
->is_type
, 0);
83 static int isis_zebra_if_add(int command
, struct zclient
*zclient
,
84 zebra_size_t length
, vrf_id_t vrf_id
)
86 struct interface
*ifp
;
88 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
90 if (isis
->debugs
& DEBUG_ZEBRA
)
92 "Zebra I/F add: %s index %d flags %ld metric %d mtu %d",
93 ifp
->name
, ifp
->ifindex
, (long)ifp
->flags
, ifp
->metric
,
96 if (if_is_operative(ifp
))
97 isis_csm_state_change(IF_UP_FROM_Z
, circuit_scan_by_ifp(ifp
),
103 static int isis_zebra_if_del(int command
, struct zclient
*zclient
,
104 zebra_size_t length
, vrf_id_t vrf_id
)
106 struct interface
*ifp
;
110 ifp
= zebra_interface_state_read(s
, vrf_id
);
115 if (if_is_operative(ifp
))
116 zlog_warn("Zebra: got delete of %s, but interface is still up",
119 if (isis
->debugs
& DEBUG_ZEBRA
)
121 "Zebra I/F delete: %s index %d flags %ld metric %d mtu %d",
122 ifp
->name
, ifp
->ifindex
, (long)ifp
->flags
, ifp
->metric
,
125 isis_csm_state_change(IF_DOWN_FROM_Z
, circuit_scan_by_ifp(ifp
), ifp
);
127 /* Cannot call if_delete because we should retain the pseudo interface
128 in case there is configuration info attached to it. */
129 if_delete_retain(ifp
);
131 if_set_index(ifp
, IFINDEX_INTERNAL
);
136 static int isis_zebra_if_state_up(int command
, struct zclient
*zclient
,
137 zebra_size_t length
, vrf_id_t vrf_id
)
139 struct interface
*ifp
;
141 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
146 isis_csm_state_change(IF_UP_FROM_Z
, circuit_scan_by_ifp(ifp
), ifp
);
151 static int isis_zebra_if_state_down(int command
, struct zclient
*zclient
,
152 zebra_size_t length
, vrf_id_t vrf_id
)
154 struct interface
*ifp
;
155 struct isis_circuit
*circuit
;
157 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
162 circuit
= isis_csm_state_change(IF_DOWN_FROM_Z
,
163 circuit_scan_by_ifp(ifp
), ifp
);
165 SET_FLAG(circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
);
170 static int isis_zebra_if_address_add(int command
, struct zclient
*zclient
,
171 zebra_size_t length
, vrf_id_t vrf_id
)
175 char buf
[PREFIX2STR_BUFFER
];
177 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
178 zclient
->ibuf
, vrf_id
);
185 prefix2str(p
, buf
, sizeof(buf
));
187 if (p
->family
== AF_INET
)
188 zlog_debug("connected IP address %s", buf
);
189 if (p
->family
== AF_INET6
)
190 zlog_debug("connected IPv6 address %s", buf
);
191 #endif /* EXTREME_DEBUG */
192 if (if_is_operative(c
->ifp
))
193 isis_circuit_add_addr(circuit_scan_by_ifp(c
->ifp
), c
);
198 static int isis_zebra_if_address_del(int command
, struct zclient
*client
,
199 zebra_size_t length
, vrf_id_t vrf_id
)
202 struct interface
*ifp
;
205 char buf
[PREFIX2STR_BUFFER
];
206 #endif /* EXTREME_DEBUG */
208 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
209 zclient
->ibuf
, vrf_id
);
218 prefix2str(p
, buf
, sizeof(buf
));
220 if (p
->family
== AF_INET
)
221 zlog_debug("disconnected IP address %s", buf
);
222 if (p
->family
== AF_INET6
)
223 zlog_debug("disconnected IPv6 address %s", buf
);
224 #endif /* EXTREME_DEBUG */
226 if (if_is_operative(ifp
))
227 isis_circuit_del_addr(circuit_scan_by_ifp(ifp
), c
);
233 static int isis_zebra_link_params(int command
, struct zclient
*zclient
,
236 struct interface
*ifp
;
238 ifp
= zebra_interface_link_params_read(zclient
->ibuf
);
244 isis_mpls_te_update(ifp
);
249 static void isis_zebra_route_add_route(struct prefix
*prefix
,
250 struct prefix_ipv6
*src_p
,
251 struct isis_route_info
*route_info
)
253 struct zapi_route api
;
254 struct zapi_nexthop
*api_nh
;
255 struct isis_nexthop
*nexthop
;
256 struct isis_nexthop6
*nexthop6
;
257 struct listnode
*node
;
260 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
263 memset(&api
, 0, sizeof(api
));
264 api
.vrf_id
= VRF_DEFAULT
;
265 api
.type
= ZEBRA_ROUTE_ISIS
;
266 api
.safi
= SAFI_UNICAST
;
267 api
.prefix
= *prefix
;
268 if (src_p
&& src_p
->prefixlen
) {
269 api
.src_prefix
= *src_p
;
270 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
272 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
273 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
274 api
.metric
= route_info
->cost
;
276 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
277 api
.distance
= route_info
->depth
;
281 switch (prefix
->family
) {
283 for (ALL_LIST_ELEMENTS_RO(route_info
->nexthops
, node
,
285 if (count
>= MULTIPATH_NUM
)
287 api_nh
= &api
.nexthops
[count
];
288 api_nh
->vrf_id
= VRF_DEFAULT
;
289 /* FIXME: can it be ? */
290 if (nexthop
->ip
.s_addr
!= INADDR_ANY
) {
291 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
292 api_nh
->gate
.ipv4
= nexthop
->ip
;
294 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
296 api_nh
->ifindex
= nexthop
->ifindex
;
301 for (ALL_LIST_ELEMENTS_RO(route_info
->nexthops6
, node
,
303 if (count
>= MULTIPATH_NUM
)
305 if (!IN6_IS_ADDR_LINKLOCAL(&nexthop6
->ip6
)
306 && !IN6_IS_ADDR_UNSPECIFIED(&nexthop6
->ip6
)) {
310 api_nh
= &api
.nexthops
[count
];
311 api_nh
->vrf_id
= VRF_DEFAULT
;
312 api_nh
->gate
.ipv6
= nexthop6
->ip6
;
313 api_nh
->ifindex
= nexthop6
->ifindex
;
314 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
322 api
.nexthop_num
= count
;
324 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
325 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
326 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
329 static void isis_zebra_route_del_route(struct prefix
*prefix
,
330 struct prefix_ipv6
*src_p
,
331 struct isis_route_info
*route_info
)
333 struct zapi_route api
;
335 if (!CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
338 memset(&api
, 0, sizeof(api
));
339 api
.vrf_id
= VRF_DEFAULT
;
340 api
.type
= ZEBRA_ROUTE_ISIS
;
341 api
.safi
= SAFI_UNICAST
;
342 api
.prefix
= *prefix
;
343 if (src_p
&& src_p
->prefixlen
) {
344 api
.src_prefix
= *src_p
;
345 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
348 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
349 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
352 void isis_zebra_route_update(struct prefix
*prefix
,
353 struct prefix_ipv6
*src_p
,
354 struct isis_route_info
*route_info
)
356 if (zclient
->sock
< 0)
359 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
360 isis_zebra_route_add_route(prefix
, src_p
, route_info
);
362 isis_zebra_route_del_route(prefix
, src_p
, route_info
);
365 static int isis_zebra_read(int command
, struct zclient
*zclient
,
366 zebra_size_t length
, vrf_id_t vrf_id
)
368 struct zapi_route api
;
370 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
374 * Avoid advertising a false default reachability. (A default
375 * route installed by IS-IS gets redistributed from zebra back
376 * into IS-IS causing us to start advertising default reachabity
377 * without this check)
379 if (api
.prefix
.prefixlen
== 0
380 && api
.src_prefix
.prefixlen
== 0
381 && api
.type
== ZEBRA_ROUTE_ISIS
) {
382 command
= ZEBRA_REDISTRIBUTE_ROUTE_DEL
;
385 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
386 isis_redist_add(api
.type
, &api
.prefix
, &api
.src_prefix
,
387 api
.distance
, api
.metric
);
389 isis_redist_delete(api
.type
, &api
.prefix
, &api
.src_prefix
);
394 int isis_distribute_list_update(int routetype
)
399 void isis_zebra_redistribute_set(afi_t afi
, int type
)
401 if (type
== DEFAULT_ROUTE
)
402 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
403 zclient
, VRF_DEFAULT
);
405 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
409 void isis_zebra_redistribute_unset(afi_t afi
, int type
)
411 if (type
== DEFAULT_ROUTE
)
412 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
413 zclient
, VRF_DEFAULT
);
415 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
416 type
, 0, VRF_DEFAULT
);
419 static void isis_zebra_connected(struct zclient
*zclient
)
421 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
424 void isis_zebra_init(struct thread_master
*master
)
426 zclient
= zclient_new_notify(master
, &zclient_options_default
);
427 zclient_init(zclient
, ZEBRA_ROUTE_ISIS
, 0, &isisd_privs
);
428 zclient
->zebra_connected
= isis_zebra_connected
;
429 zclient
->router_id_update
= isis_router_id_update_zebra
;
430 zclient
->interface_add
= isis_zebra_if_add
;
431 zclient
->interface_delete
= isis_zebra_if_del
;
432 zclient
->interface_up
= isis_zebra_if_state_up
;
433 zclient
->interface_down
= isis_zebra_if_state_down
;
434 zclient
->interface_address_add
= isis_zebra_if_address_add
;
435 zclient
->interface_address_delete
= isis_zebra_if_address_del
;
436 zclient
->interface_link_params
= isis_zebra_link_params
;
437 zclient
->redistribute_route_add
= isis_zebra_read
;
438 zclient
->redistribute_route_del
= isis_zebra_read
;
443 void isis_zebra_stop(void)
445 zclient_stop(zclient
);
446 zclient_free(zclient
);