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; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include "isisd/dict.h"
39 #include "isisd/isis_constants.h"
40 #include "isisd/isis_common.h"
41 #include "isisd/isis_flags.h"
42 #include "isisd/isis_misc.h"
43 #include "isisd/isis_circuit.h"
44 #include "isisd/isis_tlv.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. */
57 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);
84 isis_zebra_if_add (int command, struct zclient *zclient, zebra_size_t length,
87 struct interface *ifp;
89 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
91 if (isis->debugs & DEBUG_ZEBRA)
92 zlog_debug ("Zebra I/F add: %s index %d flags %ld metric %d mtu %d",
93 ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric, ifp->mtu);
95 if (if_is_operative (ifp))
96 isis_csm_state_change (IF_UP_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
102 isis_zebra_if_del (int command, struct zclient *zclient, zebra_size_t length,
105 struct interface *ifp;
109 ifp = zebra_interface_state_read (s, vrf_id);
114 if (if_is_operative (ifp))
115 zlog_warn ("Zebra: got delete of %s, but interface is still up",
118 if (isis->debugs & DEBUG_ZEBRA)
119 zlog_debug ("Zebra I/F delete: %s index %d flags %ld metric %d mtu %d",
120 ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric, ifp->mtu);
122 isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
124 /* Cannot call if_delete because we should retain the pseudo interface
125 in case there is configuration info attached to it. */
126 if_delete_retain(ifp);
128 ifp->ifindex = IFINDEX_INTERNAL;
134 isis_zebra_if_state_up (int command, struct zclient *zclient,
135 zebra_size_t length, vrf_id_t vrf_id)
137 struct interface *ifp;
139 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
144 isis_csm_state_change (IF_UP_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
150 isis_zebra_if_state_down (int command, struct zclient *zclient,
151 zebra_size_t length, vrf_id_t vrf_id)
153 struct interface *ifp;
154 struct isis_circuit *circuit;
156 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
161 circuit = isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp),
164 SET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
170 isis_zebra_if_address_add (int command, struct zclient *zclient,
171 zebra_size_t length, vrf_id_t vrf_id)
177 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
178 zclient->ibuf, vrf_id);
185 prefix2str (p, buf, BUFSIZ);
187 if (p->family == AF_INET)
188 zlog_debug ("connected IP address %s", buf);
190 if (p->family == AF_INET6)
191 zlog_debug ("connected IPv6 address %s", buf);
192 #endif /* HAVE_IPV6 */
193 #endif /* EXTREME_DEBUG */
194 if (if_is_operative (c->ifp))
195 isis_circuit_add_addr (circuit_scan_by_ifp (c->ifp), c);
201 isis_zebra_if_address_del (int command, struct zclient *client,
202 zebra_size_t length, vrf_id_t vrf_id)
205 struct interface *ifp;
209 #endif /* EXTREME_DEBUG */
211 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE,
212 zclient->ibuf, vrf_id);
221 prefix2str (p, buf, BUFSIZ);
223 if (p->family == AF_INET)
224 zlog_debug ("disconnected IP address %s", buf);
226 if (p->family == AF_INET6)
227 zlog_debug ("disconnected IPv6 address %s", buf);
228 #endif /* HAVE_IPV6 */
229 #endif /* EXTREME_DEBUG */
231 if (if_is_operative (ifp))
232 isis_circuit_del_addr (circuit_scan_by_ifp (ifp), c);
239 isis_zebra_link_params (int command, struct zclient *zclient,
242 struct interface *ifp;
244 ifp = zebra_interface_link_params_read (zclient->ibuf);
250 isis_mpls_te_update(ifp);
256 isis_zebra_route_add_ipv4 (struct prefix *prefix,
257 struct isis_route_info *route_info)
259 u_char message, flags;
261 struct stream *stream;
262 struct isis_nexthop *nexthop;
263 struct listnode *node;
265 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
268 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_ISIS], VRF_DEFAULT))
273 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
274 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
276 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
279 stream = zclient->obuf;
280 stream_reset (stream);
281 zclient_create_header (stream, ZEBRA_IPV4_ROUTE_ADD, VRF_DEFAULT);
283 stream_putc (stream, ZEBRA_ROUTE_ISIS);
285 stream_putc (stream, flags);
287 stream_putc (stream, message);
289 stream_putw (stream, SAFI_UNICAST);
290 /* prefix information */
291 psize = PSIZE (prefix->prefixlen);
292 stream_putc (stream, prefix->prefixlen);
293 stream_write (stream, (u_char *) & prefix->u.prefix4, psize);
295 stream_putc (stream, listcount (route_info->nexthops));
297 /* Nexthop, ifindex, distance and metric information */
298 for (ALL_LIST_ELEMENTS_RO (route_info->nexthops, node, nexthop))
300 /* FIXME: can it be ? */
301 if (nexthop->ip.s_addr != INADDR_ANY)
303 stream_putc (stream, ZEBRA_NEXTHOP_IPV4);
304 stream_put_in_addr (stream, &nexthop->ip);
308 stream_putc (stream, ZEBRA_NEXTHOP_IFINDEX);
309 stream_putl (stream, nexthop->ifindex);
313 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
314 stream_putc (stream, route_info->depth);
316 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
317 stream_putl (stream, route_info->cost);
319 stream_putw_at (stream, 0, stream_get_endp (stream));
320 zclient_send_message(zclient);
321 SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
322 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
327 isis_zebra_route_del_ipv4 (struct prefix *prefix,
328 struct isis_route_info *route_info)
330 struct zapi_ipv4 api;
331 struct prefix_ipv4 prefix4;
333 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_ISIS], VRF_DEFAULT))
335 api.vrf_id = VRF_DEFAULT;
336 api.type = ZEBRA_ROUTE_ISIS;
339 api.safi = SAFI_UNICAST;
340 prefix4.family = AF_INET;
341 prefix4.prefixlen = prefix->prefixlen;
342 prefix4.prefix = prefix->u.prefix4;
343 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, &prefix4, &api);
345 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
352 isis_zebra_route_add_ipv6 (struct prefix *prefix,
353 struct isis_route_info *route_info)
355 struct zapi_ipv6 api;
356 struct in6_addr **nexthop_list;
357 ifindex_t *ifindex_list;
358 struct isis_nexthop6 *nexthop6;
360 struct listnode *node;
361 struct prefix_ipv6 prefix6;
363 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
366 api.vrf_id = VRF_DEFAULT;
367 api.type = ZEBRA_ROUTE_ISIS;
370 api.safi = SAFI_UNICAST;
371 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
372 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
373 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
374 api.metric = route_info->cost;
376 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
377 api.distance = route_info->depth;
379 api.nexthop_num = listcount (route_info->nexthops6);
380 api.ifindex_num = listcount (route_info->nexthops6);
382 /* allocate memory for nexthop_list */
383 size = sizeof (struct isis_nexthop6 *) * listcount (route_info->nexthops6);
384 nexthop_list = (struct in6_addr **) XMALLOC (MTYPE_ISIS_TMP, size);
387 zlog_err ("isis_zebra_add_route_ipv6: out of memory!");
391 /* allocate memory for ifindex_list */
392 size = sizeof (unsigned int) * listcount (route_info->nexthops6);
393 ifindex_list = (ifindex_t *) XMALLOC (MTYPE_ISIS_TMP, size);
396 zlog_err ("isis_zebra_add_route_ipv6: out of memory!");
397 XFREE (MTYPE_ISIS_TMP, nexthop_list);
401 /* for each nexthop */
403 for (ALL_LIST_ELEMENTS_RO (route_info->nexthops6, node, nexthop6))
405 if (!IN6_IS_ADDR_LINKLOCAL (&nexthop6->ip6) &&
406 !IN6_IS_ADDR_UNSPECIFIED (&nexthop6->ip6))
413 nexthop_list[i] = &nexthop6->ip6;
414 ifindex_list[i] = nexthop6->ifindex;
418 api.nexthop = nexthop_list;
419 api.ifindex = ifindex_list;
421 if (api.nexthop_num && api.ifindex_num)
423 prefix6.family = AF_INET6;
424 prefix6.prefixlen = prefix->prefixlen;
425 memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
426 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, &prefix6, &api);
427 SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
428 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
431 XFREE (MTYPE_ISIS_TMP, nexthop_list);
432 XFREE (MTYPE_ISIS_TMP, ifindex_list);
438 isis_zebra_route_del_ipv6 (struct prefix *prefix,
439 struct isis_route_info *route_info)
441 struct zapi_ipv6 api;
442 struct in6_addr **nexthop_list;
443 ifindex_t *ifindex_list;
444 struct isis_nexthop6 *nexthop6;
446 struct listnode *node;
447 struct prefix_ipv6 prefix6;
449 if (!CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
452 api.vrf_id = VRF_DEFAULT;
453 api.type = ZEBRA_ROUTE_ISIS;
456 api.safi = SAFI_UNICAST;
457 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
458 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
459 api.nexthop_num = listcount (route_info->nexthops6);
460 api.ifindex_num = listcount (route_info->nexthops6);
462 /* allocate memory for nexthop_list */
463 size = sizeof (struct isis_nexthop6 *) * listcount (route_info->nexthops6);
464 nexthop_list = (struct in6_addr **) XMALLOC (MTYPE_ISIS_TMP, size);
467 zlog_err ("isis_zebra_route_del_ipv6: out of memory!");
471 /* allocate memory for ifindex_list */
472 size = sizeof (unsigned int) * listcount (route_info->nexthops6);
473 ifindex_list = (ifindex_t *) XMALLOC (MTYPE_ISIS_TMP, size);
476 zlog_err ("isis_zebra_route_del_ipv6: out of memory!");
477 XFREE (MTYPE_ISIS_TMP, nexthop_list);
481 /* for each nexthop */
483 for (ALL_LIST_ELEMENTS_RO (route_info->nexthops6, node, nexthop6))
485 if (!IN6_IS_ADDR_LINKLOCAL (&nexthop6->ip6) &&
486 !IN6_IS_ADDR_UNSPECIFIED (&nexthop6->ip6))
493 nexthop_list[i] = &nexthop6->ip6;
494 ifindex_list[i] = nexthop6->ifindex;
498 api.nexthop = nexthop_list;
499 api.ifindex = ifindex_list;
501 if (api.nexthop_num && api.ifindex_num)
503 prefix6.family = AF_INET6;
504 prefix6.prefixlen = prefix->prefixlen;
505 memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
506 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, &prefix6, &api);
507 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
510 XFREE (MTYPE_ISIS_TMP, nexthop_list);
511 XFREE (MTYPE_ISIS_TMP, ifindex_list);
514 #endif /* HAVE_IPV6 */
517 isis_zebra_route_update (struct prefix *prefix,
518 struct isis_route_info *route_info)
520 if (zclient->sock < 0)
523 if (!vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_ISIS], VRF_DEFAULT))
526 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ACTIVE))
528 if (prefix->family == AF_INET)
529 isis_zebra_route_add_ipv4 (prefix, route_info);
531 else if (prefix->family == AF_INET6)
532 isis_zebra_route_add_ipv6 (prefix, route_info);
533 #endif /* HAVE_IPV6 */
537 if (prefix->family == AF_INET)
538 isis_zebra_route_del_ipv4 (prefix, route_info);
540 else if (prefix->family == AF_INET6)
541 isis_zebra_route_del_ipv6 (prefix, route_info);
542 #endif /* HAVE_IPV6 */
548 isis_zebra_read_ipv4 (int command, struct zclient *zclient,
549 zebra_size_t length, vrf_id_t vrf_id)
551 struct stream *stream;
552 struct zapi_ipv4 api;
553 struct prefix_ipv4 p;
554 struct prefix *p_generic = (struct prefix*)&p;
555 unsigned long ifindex __attribute__ ((unused));
556 struct in_addr nexthop __attribute__ ((unused));
557 unsigned char plength = 0;
559 stream = zclient->ibuf;
560 memset(&api, 0, sizeof(api));
561 memset (&p, 0, sizeof (struct prefix_ipv4));
562 memset(&nexthop, 0, sizeof(nexthop));
565 api.type = stream_getc (stream);
566 api.flags = stream_getc (stream);
567 api.message = stream_getc (stream);
570 plength = stream_getc (stream);
571 p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
572 stream_get (&p.prefix, stream, PSIZE (p.prefixlen));
574 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
576 api.nexthop_num = stream_getc (stream);
577 nexthop.s_addr = stream_get_ipv4 (stream);
579 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
581 api.ifindex_num = stream_getc (stream);
582 ifindex = stream_getl (stream);
584 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
585 api.distance = stream_getc (stream);
586 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
587 api.metric = stream_getl (stream);
590 * Avoid advertising a false default reachability. (A default
591 * route installed by IS-IS gets redistributed from zebra back
592 * into IS-IS causing us to start advertising default reachabity
593 * without this check)
595 if (p.prefixlen == 0 && api.type == ZEBRA_ROUTE_ISIS)
596 command = ZEBRA_IPV4_ROUTE_DELETE;
598 if (command == ZEBRA_IPV4_ROUTE_ADD)
599 isis_redist_add(api.type, p_generic, api.distance, api.metric);
601 isis_redist_delete(api.type, p_generic);
607 isis_zebra_read_ipv6 (int command, struct zclient *zclient,
608 zebra_size_t length, vrf_id_t vrf_id)
610 struct stream *stream;
611 struct zapi_ipv6 api;
612 struct prefix_ipv6 p;
613 struct prefix *p_generic = (struct prefix*)&p;
614 struct in6_addr nexthop;
615 unsigned long ifindex __attribute__((unused));
617 stream = zclient->ibuf;
618 memset(&api, 0, sizeof(api));
619 memset(&p, 0, sizeof(struct prefix_ipv6));
620 memset(&nexthop, 0, sizeof(nexthop));
623 api.type = stream_getc(stream);
624 api.flags = stream_getc(stream);
625 api.message = stream_getc(stream);
628 p.prefixlen = stream_getc(stream);
629 stream_get(&p.prefix, stream, PSIZE(p.prefixlen));
631 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP))
633 api.nexthop_num = stream_getc(stream); /* this is always 1 */
634 stream_get(&nexthop, stream, sizeof(nexthop));
636 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX))
638 api.ifindex_num = stream_getc(stream);
639 ifindex = stream_getl(stream);
641 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
642 api.distance = stream_getc(stream);
643 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
644 api.metric = stream_getl(stream);
647 * Avoid advertising a false default reachability. (A default
648 * route installed by IS-IS gets redistributed from zebra back
649 * into IS-IS causing us to start advertising default reachabity
650 * without this check)
652 if (p.prefixlen == 0 && api.type == ZEBRA_ROUTE_ISIS)
653 command = ZEBRA_IPV6_ROUTE_DELETE;
655 if (command == ZEBRA_IPV6_ROUTE_ADD)
656 isis_redist_add(api.type, p_generic, api.distance, api.metric);
658 isis_redist_delete(api.type, p_generic);
664 isis_distribute_list_update (int routetype)
670 isis_zebra_redistribute_set(int type)
672 if (type == DEFAULT_ROUTE)
673 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient, VRF_DEFAULT);
675 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
679 isis_zebra_redistribute_unset(int type)
681 if (type == DEFAULT_ROUTE)
682 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient, VRF_DEFAULT);
684 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, type, VRF_DEFAULT);
688 isis_zebra_connected (struct zclient *zclient)
690 zclient_send_requests (zclient, VRF_DEFAULT);
694 isis_zebra_init (struct thread_master *master)
696 zclient = zclient_new (master);
697 zclient_init (zclient, ZEBRA_ROUTE_ISIS);
698 zclient->zebra_connected = isis_zebra_connected;
699 zclient->router_id_update = isis_router_id_update_zebra;
700 zclient->interface_add = isis_zebra_if_add;
701 zclient->interface_delete = isis_zebra_if_del;
702 zclient->interface_up = isis_zebra_if_state_up;
703 zclient->interface_down = isis_zebra_if_state_down;
704 zclient->interface_address_add = isis_zebra_if_address_add;
705 zclient->interface_address_delete = isis_zebra_if_address_del;
706 zclient->interface_link_params = isis_zebra_link_params;
707 zclient->ipv4_route_add = isis_zebra_read_ipv4;
708 zclient->ipv4_route_delete = isis_zebra_read_ipv4;
710 zclient->ipv6_route_add = isis_zebra_read_ipv6;
711 zclient->ipv6_route_delete = isis_zebra_read_ipv6;
712 #endif /* HAVE_IPV6 */