2 * RIPngd and zebra interface.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 #include "ripngd/ripngd.h"
35 #include "ripngd/ripng_debug.h"
37 /* All information about zebra. */
38 struct zclient *zclient = NULL;
40 /* Send ECMP routes to zebra. */
42 ripng_zebra_ipv6_send (struct route_node *rp, u_char cmd)
44 static struct in6_addr **nexthops = NULL;
45 static ifindex_t *ifindexes = NULL;
46 static unsigned int nexthops_len = 0;
48 struct list *list = (struct list *)rp->info;
50 struct listnode *listnode = NULL;
51 struct ripng_info *rinfo = NULL;
54 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_RIPNG], VRF_DEFAULT))
56 api.vrf_id = VRF_DEFAULT;
57 api.type = ZEBRA_ROUTE_RIPNG;
60 api.safi = SAFI_UNICAST;
62 if (nexthops_len < listcount (list))
64 nexthops_len = listcount (list);
65 nexthops = XREALLOC (MTYPE_TMP, nexthops,
66 nexthops_len * sizeof (struct in6_addr *));
67 ifindexes = XREALLOC (MTYPE_TMP, ifindexes,
68 nexthops_len * sizeof (unsigned int));
71 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
72 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
73 for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
75 nexthops[count] = &rinfo->nexthop;
76 ifindexes[count] = rinfo->ifindex;
78 if (cmd == ZEBRA_IPV6_ROUTE_ADD)
79 SET_FLAG (rinfo->flags, RIPNG_RTF_FIB);
81 UNSET_FLAG (rinfo->flags, RIPNG_RTF_FIB);
84 api.nexthop = nexthops;
85 api.nexthop_num = count;
86 api.ifindex = ifindexes;
87 api.ifindex_num = count;
89 rinfo = listgetdata (listhead (list));
91 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
92 api.metric = rinfo->metric;
96 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
100 zapi_ipv6_route (cmd, zclient,
101 (struct prefix_ipv6 *)&rp->p, &api);
103 if (IS_RIPNG_DEBUG_ZEBRA)
106 zlog_debug ("%s: %s/%d nexthops %d",
107 (cmd == ZEBRA_IPV6_ROUTE_ADD) ? \
108 "Install into zebra" : "Delete from zebra",
109 inet6_ntoa (rp->p.u.prefix6), rp->p.prefixlen, count);
111 zlog_debug ("%s: %s/%d",
112 (cmd == ZEBRA_IPV6_ROUTE_ADD) ? \
113 "Install into zebra" : "Delete from zebra",
114 inet6_ntoa (rp->p.u.prefix6), rp->p.prefixlen);
119 /* Add/update ECMP routes to zebra. */
121 ripng_zebra_ipv6_add (struct route_node *rp)
123 ripng_zebra_ipv6_send (rp, ZEBRA_IPV6_ROUTE_ADD);
126 /* Delete ECMP routes from zebra. */
128 ripng_zebra_ipv6_delete (struct route_node *rp)
130 ripng_zebra_ipv6_send (rp, ZEBRA_IPV6_ROUTE_DELETE);
133 /* Zebra route add and delete treatment. */
135 ripng_zebra_read_ipv6 (int command, struct zclient *zclient,
136 zebra_size_t length, vrf_id_t vrf_id)
139 struct zapi_ipv6 api;
140 unsigned long ifindex;
141 struct in6_addr nexthop;
142 struct prefix_ipv6 p;
143 unsigned char plength = 0;
147 memset (&nexthop, 0, sizeof (struct in6_addr));
149 /* Type, flags, message. */
150 api.type = stream_getc (s);
151 api.flags = stream_getc (s);
152 api.message = stream_getc (s);
155 memset (&p, 0, sizeof (struct prefix_ipv6));
157 plength = stream_getc (s);
158 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
159 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
161 /* Nexthop, ifindex, distance, metric. */
162 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
164 api.nexthop_num = stream_getc (s);
165 stream_get (&nexthop, s, 16);
167 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
169 api.ifindex_num = stream_getc (s);
170 ifindex = stream_getl (s);
172 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
173 api.distance = stream_getc (s);
176 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
177 api.metric = stream_getl (s);
181 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
182 api.tag = stream_getl (s);
186 if (command == ZEBRA_IPV6_ROUTE_ADD)
187 ripng_redistribute_add (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p,
188 ifindex, &nexthop, api.tag);
190 ripng_redistribute_delete (api.type, RIPNG_ROUTE_REDISTRIBUTE, &p, ifindex);
196 ripng_zclient_reset (void)
198 zclient_reset (zclient);
202 ripng_redistribute_unset (int type)
204 if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
207 vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
209 if (zclient->sock > 0)
210 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
213 ripng_redistribute_withdraw (type);
219 ripng_redistribute_check (int type)
221 return vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT);
225 ripng_redistribute_metric_set (int type, int metric)
227 ripng->route_map[type].metric_config = 1;
228 ripng->route_map[type].metric = metric;
232 ripng_redistribute_metric_unset (int type)
234 ripng->route_map[type].metric_config = 0;
235 ripng->route_map[type].metric = 0;
240 ripng_redistribute_routemap_set (int type, const char *name)
242 if (ripng->route_map[type].name)
243 free (ripng->route_map[type].name);
245 ripng->route_map[type].name = strdup (name);
246 ripng->route_map[type].map = route_map_lookup_by_name (name);
250 ripng_redistribute_routemap_unset (int type)
252 if (ripng->route_map[type].name)
253 free (ripng->route_map[type].name);
255 ripng->route_map[type].name = NULL;
256 ripng->route_map[type].map = NULL;
259 /* Redistribution types */
265 {ZEBRA_ROUTE_KERNEL, 1, "kernel"},
266 {ZEBRA_ROUTE_CONNECT, 1, "connected"},
267 {ZEBRA_ROUTE_STATIC, 1, "static"},
268 {ZEBRA_ROUTE_OSPF6, 1, "ospf6"},
269 {ZEBRA_ROUTE_BGP, 2, "bgp"},
270 {ZEBRA_ROUTE_BABEL, 2, "babel"},
275 ripng_redistribute_clean ()
279 for (i = 0; redist_type[i].str; i++)
281 if (vrf_bitmap_check (zclient->redist[redist_type[i].type], VRF_DEFAULT))
283 if (zclient->sock > 0)
284 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE,
285 zclient, redist_type[i].type,
288 vrf_bitmap_unset (zclient->redist[redist_type[i].type], VRF_DEFAULT);
290 /* Remove the routes from RIPng table. */
291 ripng_redistribute_withdraw (redist_type[i].type);
299 "Enable a routing process\n"
300 "Make connection to zebra daemon\n")
302 vty->node = ZEBRA_NODE;
304 zclient_start (zclient);
308 DEFUN (no_router_zebra,
312 "Disable a routing process\n"
313 "Stop connection to zebra daemon\n")
316 zclient_stop (zclient);
320 DEFUN (ripng_redistribute_ripng,
321 ripng_redistribute_ripng_cmd,
322 "redistribute ripng",
323 "Redistribute information from another routing protocol\n"
326 vrf_bitmap_set (zclient->redist[ZEBRA_ROUTE_RIPNG], VRF_DEFAULT);
330 DEFUN (no_ripng_redistribute_ripng,
331 no_ripng_redistribute_ripng_cmd,
332 "no redistribute ripng",
334 "Redistribute information from another routing protocol\n"
337 vrf_bitmap_unset (zclient->redist[ZEBRA_ROUTE_RIPNG], VRF_DEFAULT);
341 DEFUN (ripng_redistribute_type,
342 ripng_redistribute_type_cmd,
343 "redistribute " QUAGGA_REDIST_STR_RIPNGD,
345 QUAGGA_REDIST_HELP_STR_RIPNGD)
349 type = proto_redistnum(AFI_IP6, argv[0]);
353 vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
357 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
361 DEFUN (no_ripng_redistribute_type,
362 no_ripng_redistribute_type_cmd,
363 "no redistribute " QUAGGA_REDIST_STR_RIPNGD,
366 QUAGGA_REDIST_HELP_STR_RIPNGD)
370 type = proto_redistnum(AFI_IP6, argv[0]);
374 vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
378 ripng_redistribute_metric_unset (type);
379 ripng_redistribute_routemap_unset (type);
380 return ripng_redistribute_unset (type);
384 DEFUN (ripng_redistribute_type_metric,
385 ripng_redistribute_type_metric_cmd,
386 "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
388 QUAGGA_REDIST_HELP_STR_RIPNGD
395 metric = atoi (argv[1]);
396 type = proto_redistnum(AFI_IP6, argv[0]);
400 vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
404 ripng_redistribute_metric_set (type, metric);
405 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
409 ALIAS (no_ripng_redistribute_type,
410 no_ripng_redistribute_type_metric_cmd,
411 "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
414 QUAGGA_REDIST_HELP_STR_RIPNGD
418 DEFUN (ripng_redistribute_type_routemap,
419 ripng_redistribute_type_routemap_cmd,
420 "redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
422 QUAGGA_REDIST_HELP_STR_RIPNGD
423 "Route map reference\n"
424 "Pointer to route-map entries\n")
428 type = proto_redistnum(AFI_IP6, argv[0]);
432 vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
436 ripng_redistribute_routemap_set (type, argv[1]);
437 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
441 ALIAS (no_ripng_redistribute_type,
442 no_ripng_redistribute_type_routemap_cmd,
443 "no redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
446 QUAGGA_REDIST_HELP_STR_RIPNGD
447 "Route map reference\n"
448 "Pointer to route-map entries\n")
450 DEFUN (ripng_redistribute_type_metric_routemap,
451 ripng_redistribute_type_metric_routemap_cmd,
452 "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
454 QUAGGA_REDIST_HELP_STR_RIPNGD
457 "Route map reference\n"
458 "Pointer to route-map entries\n")
463 type = proto_redistnum(AFI_IP6, argv[0]);
464 metric = atoi (argv[1]);
468 vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
472 ripng_redistribute_metric_set (type, metric);
473 ripng_redistribute_routemap_set (type, argv[2]);
474 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
478 ALIAS (no_ripng_redistribute_type,
479 no_ripng_redistribute_type_metric_routemap_cmd,
480 "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
483 QUAGGA_REDIST_HELP_STR_RIPNGD
484 "Route map reference\n"
485 "Pointer to route-map entries\n")
488 ripng_redistribute_write (struct vty *vty, int config_mode)
492 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
493 if (i != zclient->redist_default &&
494 vrf_bitmap_check (zclient->redist[i], VRF_DEFAULT))
498 if (ripng->route_map[i].metric_config)
500 if (ripng->route_map[i].name)
501 vty_out (vty, " redistribute %s metric %d route-map %s%s",
502 zebra_route_string(i), ripng->route_map[i].metric,
503 ripng->route_map[i].name, VTY_NEWLINE);
505 vty_out (vty, " redistribute %s metric %d%s",
506 zebra_route_string(i), ripng->route_map[i].metric,
511 if (ripng->route_map[i].name)
512 vty_out (vty, " redistribute %s route-map %s%s",
513 zebra_route_string(i), ripng->route_map[i].name,
516 vty_out (vty, " redistribute %s%s", zebra_route_string(i),
521 vty_out (vty, " %s", zebra_route_string(i));
525 /* RIPng configuration write function. */
527 zebra_config_write (struct vty *vty)
529 if (! zclient->enable)
531 vty_out (vty, "no router zebra%s", VTY_NEWLINE);
534 else if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_RIPNG], VRF_DEFAULT))
536 vty_out (vty, "router zebra%s", VTY_NEWLINE);
537 vty_out (vty, " no redistribute ripng%s", VTY_NEWLINE);
543 /* Zebra node structure. */
544 static struct cmd_node zebra_node =
547 "%s(config-router)# ",
551 ripng_zebra_connected (struct zclient *zclient)
553 zclient_send_requests (zclient, VRF_DEFAULT);
556 /* Initialize zebra structure and it's commands. */
558 zebra_init (struct thread_master *master)
560 /* Allocate zebra structure. */
561 zclient = zclient_new (master);
562 zclient_init (zclient, ZEBRA_ROUTE_RIPNG);
564 zclient->zebra_connected = ripng_zebra_connected;
565 zclient->interface_up = ripng_interface_up;
566 zclient->interface_down = ripng_interface_down;
567 zclient->interface_add = ripng_interface_add;
568 zclient->interface_delete = ripng_interface_delete;
569 zclient->interface_address_add = ripng_interface_address_add;
570 zclient->interface_address_delete = ripng_interface_address_delete;
571 zclient->ipv6_route_add = ripng_zebra_read_ipv6;
572 zclient->ipv6_route_delete = ripng_zebra_read_ipv6;
574 /* Install zebra node. */
575 install_node (&zebra_node, zebra_config_write);
577 /* Install command element for zebra node. */
578 install_element (CONFIG_NODE, &router_zebra_cmd);
579 install_element (CONFIG_NODE, &no_router_zebra_cmd);
580 install_default (ZEBRA_NODE);
581 install_element (ZEBRA_NODE, &ripng_redistribute_ripng_cmd);
582 install_element (ZEBRA_NODE, &no_ripng_redistribute_ripng_cmd);
584 /* Install command elements to ripng node */
585 install_element (RIPNG_NODE, &ripng_redistribute_type_cmd);
586 install_element (RIPNG_NODE, &ripng_redistribute_type_routemap_cmd);
587 install_element (RIPNG_NODE, &ripng_redistribute_type_metric_cmd);
588 install_element (RIPNG_NODE, &ripng_redistribute_type_metric_routemap_cmd);
589 install_element (RIPNG_NODE, &no_ripng_redistribute_type_cmd);
590 install_element (RIPNG_NODE, &no_ripng_redistribute_type_routemap_cmd);
591 install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_cmd);
592 install_element (RIPNG_NODE, &no_ripng_redistribute_type_metric_routemap_cmd);