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
32 #include "ripngd/ripngd.h"
34 /* All information about zebra. */
35 struct zclient
*zclient
= NULL
;
37 /* Callback prototypes for zebra client service. */
38 int ripng_interface_up (int, struct zclient
*, zebra_size_t
);
39 int ripng_interface_down (int, struct zclient
*, zebra_size_t
);
40 int ripng_interface_add (int, struct zclient
*, zebra_size_t
);
41 int ripng_interface_delete (int, struct zclient
*, zebra_size_t
);
42 int ripng_interface_address_add (int, struct zclient
*, zebra_size_t
);
43 int ripng_interface_address_delete (int, struct zclient
*, zebra_size_t
);
46 ripng_zebra_ipv6_add (struct prefix_ipv6
*p
, struct in6_addr
*nexthop
,
51 if (zclient
->redist
[ZEBRA_ROUTE_RIPNG
])
53 api
.type
= ZEBRA_ROUTE_RIPNG
;
56 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
58 api
.nexthop
= &nexthop
;
59 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
61 api
.ifindex
= &ifindex
;
63 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD
, zclient
, p
, &api
);
68 ripng_zebra_ipv6_delete (struct prefix_ipv6
*p
, struct in6_addr
*nexthop
,
73 if (zclient
->redist
[ZEBRA_ROUTE_RIPNG
])
75 api
.type
= ZEBRA_ROUTE_RIPNG
;
78 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
80 api
.nexthop
= &nexthop
;
81 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
83 api
.ifindex
= &ifindex
;
85 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
, p
, &api
);
89 /* Zebra route add and delete treatment. */
91 ripng_zebra_read_ipv6 (int command
, struct zclient
*zclient
,
96 unsigned long ifindex
;
97 struct in6_addr nexthop
;
102 memset (&nexthop
, 0, sizeof (struct in6_addr
));
104 /* Type, flags, message. */
105 api
.type
= stream_getc (s
);
106 api
.flags
= stream_getc (s
);
107 api
.message
= stream_getc (s
);
110 memset (&p
, 0, sizeof (struct prefix_ipv6
));
112 p
.prefixlen
= stream_getc (s
);
113 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
115 /* Nexthop, ifindex, distance, metric. */
116 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
118 api
.nexthop_num
= stream_getc (s
);
119 stream_get (&nexthop
, s
, 16);
121 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
123 api
.ifindex_num
= stream_getc (s
);
124 ifindex
= stream_getl (s
);
126 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
127 api
.distance
= stream_getc (s
);
130 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
131 api
.metric
= stream_getl (s
);
135 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
136 ripng_redistribute_add (api
.type
, RIPNG_ROUTE_REDISTRIBUTE
, &p
, ifindex
, &nexthop
);
138 ripng_redistribute_delete (api
.type
, RIPNG_ROUTE_REDISTRIBUTE
, &p
, ifindex
);
144 ripng_zclient_reset ()
146 zclient_reset (zclient
);
150 ripng_redistribute_unset (int type
)
152 if (! zclient
->redist
[type
])
155 zclient
->redist
[type
] = 0;
157 if (zclient
->sock
> 0)
158 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
->sock
, type
);
160 ripng_redistribute_withdraw (type
);
166 ripng_redistribute_check (int type
)
168 return (zclient
->redist
[type
]);
172 ripng_redistribute_metric_set (int type
, int metric
)
174 ripng
->route_map
[type
].metric_config
= 1;
175 ripng
->route_map
[type
].metric
= metric
;
179 ripng_redistribute_metric_unset (int type
)
181 ripng
->route_map
[type
].metric_config
= 0;
182 ripng
->route_map
[type
].metric
= 0;
187 ripng_redistribute_routemap_set (int type
, char *name
)
189 if (ripng
->route_map
[type
].name
)
190 free (ripng
->route_map
[type
].name
);
192 ripng
->route_map
[type
].name
= strdup (name
);
193 ripng
->route_map
[type
].map
= route_map_lookup_by_name (name
);
197 ripng_redistribute_routemap_unset (int type
)
199 if (ripng
->route_map
[type
].name
)
200 free (ripng
->route_map
[type
].name
);
202 ripng
->route_map
[type
].name
= NULL
;
203 ripng
->route_map
[type
].map
= NULL
;
206 /* Redistribution types */
212 {ZEBRA_ROUTE_KERNEL
, 1, "kernel"},
213 {ZEBRA_ROUTE_CONNECT
, 1, "connected"},
214 {ZEBRA_ROUTE_STATIC
, 1, "static"},
215 {ZEBRA_ROUTE_OSPF6
, 1, "ospf6"},
216 {ZEBRA_ROUTE_BGP
, 1, "bgp"},
221 ripng_redistribute_clean ()
225 for (i
= 0; redist_type
[i
].str
; i
++)
227 if (zclient
->redist
[redist_type
[i
].type
])
229 if (zclient
->sock
> 0)
230 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
,
231 zclient
->sock
, redist_type
[i
].type
);
233 zclient
->redist
[redist_type
[i
].type
] = 0;
235 /* Remove the routes from RIPng table. */
236 ripng_redistribute_withdraw (redist_type
[i
].type
);
244 "Enable a routing process\n"
245 "Make connection to zebra daemon\n")
247 vty
->node
= ZEBRA_NODE
;
249 zclient_start (zclient
);
253 DEFUN (no_router_zebra
,
257 "Disable a routing process\n"
258 "Stop connection to zebra daemon\n")
261 zclient_stop (zclient
);
265 DEFUN (ripng_redistribute_ripng
,
266 ripng_redistribute_ripng_cmd
,
267 "redistribute ripng",
268 "Redistribute information from another routing protocol\n"
271 zclient
->redist
[ZEBRA_ROUTE_RIPNG
] = 1;
275 DEFUN (no_ripng_redistribute_ripng
,
276 no_ripng_redistribute_ripng_cmd
,
277 "no redistribute ripng",
279 "Redistribute information from another routing protocol\n"
282 zclient
->redist
[ZEBRA_ROUTE_RIPNG
] = 0;
286 DEFUN (ripng_redistribute_type
,
287 ripng_redistribute_type_cmd
,
288 "redistribute (kernel|connected|static|ospf6|bgp)",
289 "Redistribute information from another routing protocol\n"
293 "Open Shortest Path First (OSPFv3)\n"
294 "Border Gateway Protocol (BGP)\n")
298 for(i
= 0; redist_type
[i
].str
; i
++)
300 if (strncmp (redist_type
[i
].str
, argv
[0],
301 redist_type
[i
].str_min_len
) == 0)
303 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, redist_type
[i
].type
);
308 vty_out(vty
, "Invalid type %s%s", argv
[0],
314 DEFUN (no_ripng_redistribute_type
,
315 no_ripng_redistribute_type_cmd
,
316 "no redistribute (kernel|connected|static|ospf6|bgp)",
318 "Redistribute information from another routing protocol\n"
322 "Open Shortest Path First (OSPFv3)\n"
323 "Border Gateway Protocol (BGP)\n")
327 for (i
= 0; redist_type
[i
].str
; i
++)
329 if (strncmp(redist_type
[i
].str
, argv
[0],
330 redist_type
[i
].str_min_len
) == 0)
332 ripng_redistribute_metric_unset (redist_type
[i
].type
);
333 ripng_redistribute_routemap_unset (redist_type
[i
].type
);
334 return ripng_redistribute_unset (redist_type
[i
].type
);
338 vty_out(vty
, "Invalid type %s%s", argv
[0],
345 DEFUN (ripng_redistribute_type_metric
,
346 ripng_redistribute_type_metric_cmd
,
347 "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
348 "Redistribute information from another routing protocol\n"
352 "Open Shortest Path First (OSPFv3)\n"
353 "Border Gateway Protocol (BGP)\n"
360 metric
= atoi (argv
[1]);
362 for (i
= 0; redist_type
[i
].str
; i
++) {
363 if (strncmp(redist_type
[i
].str
, argv
[0],
364 redist_type
[i
].str_min_len
) == 0)
366 ripng_redistribute_metric_set (redist_type
[i
].type
, metric
);
367 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, redist_type
[i
].type
);
372 vty_out(vty
, "Invalid type %s%s", argv
[0],
378 ALIAS (no_ripng_redistribute_type
,
379 no_ripng_redistribute_type_metric_cmd
,
380 "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
382 "Redistribute information from another routing protocol\n"
386 "Open Shortest Path First (OSPFv3)\n"
387 "Border Gateway Protocol (BGP)\n"
391 DEFUN (ripng_redistribute_type_routemap
,
392 ripng_redistribute_type_routemap_cmd
,
393 "redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
394 "Redistribute information from another routing protocol\n"
398 "Open Shortest Path First (OSPFv3)\n"
399 "Border Gateway Protocol (BGP)\n"
400 "Route map reference\n"
401 "Pointer to route-map entries\n")
405 for (i
= 0; redist_type
[i
].str
; i
++) {
406 if (strncmp(redist_type
[i
].str
, argv
[0],
407 redist_type
[i
].str_min_len
) == 0)
409 ripng_redistribute_routemap_set (redist_type
[i
].type
, argv
[1]);
410 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, redist_type
[i
].type
);
415 vty_out(vty
, "Invalid type %s%s", argv
[0],
421 ALIAS (no_ripng_redistribute_type
,
422 no_ripng_redistribute_type_routemap_cmd
,
423 "no redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
425 "Redistribute information from another routing protocol\n"
429 "Open Shortest Path First (OSPFv3)\n"
430 "Border Gateway Protocol (BGP)\n"
431 "Route map reference\n"
432 "Pointer to route-map entries\n")
434 DEFUN (ripng_redistribute_type_metric_routemap
,
435 ripng_redistribute_type_metric_routemap_cmd
,
436 "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
437 "Redistribute information from another routing protocol\n"
441 "Open Shortest Path First (OSPFv3)\n"
442 "Border Gateway Protocol (BGP)\n"
445 "Route map reference\n"
446 "Pointer to route-map entries\n")
451 metric
= atoi (argv
[1]);
453 for (i
= 0; redist_type
[i
].str
; i
++) {
454 if (strncmp(redist_type
[i
].str
, argv
[0],
455 redist_type
[i
].str_min_len
) == 0)
457 ripng_redistribute_metric_set (redist_type
[i
].type
, metric
);
458 ripng_redistribute_routemap_set (redist_type
[i
].type
, argv
[2]);
459 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
, redist_type
[i
].type
);
464 vty_out(vty
, "Invalid type %s%s", argv
[0],
470 ALIAS (no_ripng_redistribute_type
,
471 no_ripng_redistribute_type_metric_routemap_cmd
,
472 "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
474 "Redistribute information from another routing protocol\n"
478 "Open Shortest Path First (OSPFv3)\n"
479 "Border Gateway Protocol (BGP)\n"
480 "Route map reference\n"
481 "Pointer to route-map entries\n")
484 ripng_redistribute_write (struct vty
*vty
, int config_mode
)
487 const char *str
[] = { "system", "kernel", "connected", "static", "rip",
488 "ripng", "ospf", "ospf6", "isis", "bgp"};
490 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
491 if (i
!= zclient
->redist_default
&& zclient
->redist
[i
])
495 if (ripng
->route_map
[i
].metric_config
)
497 if (ripng
->route_map
[i
].name
)
498 vty_out (vty
, " redistribute %s metric %d route-map %s%s",
499 str
[i
], ripng
->route_map
[i
].metric
,
500 ripng
->route_map
[i
].name
, VTY_NEWLINE
);
502 vty_out (vty
, " redistribute %s metric %d%s",
503 str
[i
], ripng
->route_map
[i
].metric
, VTY_NEWLINE
);
507 if (ripng
->route_map
[i
].name
)
508 vty_out (vty
, " redistribute %s route-map %s%s",
509 str
[i
], ripng
->route_map
[i
].name
, VTY_NEWLINE
);
511 vty_out (vty
, " redistribute %s%s", str
[i
], VTY_NEWLINE
);
515 vty_out (vty
, " %s", str
[i
]);
519 /* RIPng configuration write function. */
521 zebra_config_write (struct vty
*vty
)
523 if (! zclient
->enable
)
525 vty_out (vty
, "no router zebra%s", VTY_NEWLINE
);
528 else if (! zclient
->redist
[ZEBRA_ROUTE_RIPNG
])
530 vty_out (vty
, "router zebra%s", VTY_NEWLINE
);
531 vty_out (vty
, " no redistribute ripng%s", VTY_NEWLINE
);
537 /* Zebra node structure. */
538 struct cmd_node zebra_node
=
541 "%s(config-router)# ",
544 /* Initialize zebra structure and it's commands. */
548 /* Allocate zebra structure. */
549 zclient
= zclient_new ();
550 zclient_init (zclient
, ZEBRA_ROUTE_RIPNG
);
552 zclient
->interface_up
= ripng_interface_up
;
553 zclient
->interface_down
= ripng_interface_down
;
554 zclient
->interface_add
= ripng_interface_add
;
555 zclient
->interface_delete
= ripng_interface_delete
;
556 zclient
->interface_address_add
= ripng_interface_address_add
;
557 zclient
->interface_address_delete
= ripng_interface_address_delete
;
558 zclient
->ipv6_route_add
= ripng_zebra_read_ipv6
;
559 zclient
->ipv6_route_delete
= ripng_zebra_read_ipv6
;
561 /* Install zebra node. */
562 install_node (&zebra_node
, zebra_config_write
);
564 /* Install command element for zebra node. */
565 install_element (CONFIG_NODE
, &router_zebra_cmd
);
566 install_element (CONFIG_NODE
, &no_router_zebra_cmd
);
567 install_default (ZEBRA_NODE
);
568 install_element (ZEBRA_NODE
, &ripng_redistribute_ripng_cmd
);
569 install_element (ZEBRA_NODE
, &no_ripng_redistribute_ripng_cmd
);
571 /* Install command elements to ripng node */
572 install_element (RIPNG_NODE
, &ripng_redistribute_type_cmd
);
573 install_element (RIPNG_NODE
, &ripng_redistribute_type_routemap_cmd
);
574 install_element (RIPNG_NODE
, &ripng_redistribute_type_metric_cmd
);
575 install_element (RIPNG_NODE
, &ripng_redistribute_type_metric_routemap_cmd
);
576 install_element (RIPNG_NODE
, &no_ripng_redistribute_type_cmd
);
577 install_element (RIPNG_NODE
, &no_ripng_redistribute_type_routemap_cmd
);
578 install_element (RIPNG_NODE
, &no_ripng_redistribute_type_metric_cmd
);
579 install_element (RIPNG_NODE
, &no_ripng_redistribute_type_metric_routemap_cmd
);