blob: eb126feb1538f448ad8ab5dfe9085b53e49c9309 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
30#include "table.h"
31#include "rib.h"
32#include "network.h"
33#include "sockunion.h"
34#include "log.h"
35#include "zclient.h"
pauledd7c242003-06-04 13:59:38 +000036#include "privs.h"
ajs719e9742005-02-28 20:52:15 +000037#include "network.h"
38#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000039
40#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000041#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000042#include "zebra/redistribute.h"
43#include "zebra/debug.h"
44#include "zebra/ipforward.h"
45
46/* Event list of zebra. */
47enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
48
paulb21b19c2003-06-15 01:28:29 +000049extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000050
paulb9df2d22004-05-09 09:09:59 +000051static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000052
pauledd7c242003-06-04 13:59:38 +000053extern struct zebra_privs_t zserv_privs;
paul718e3742002-12-13 20:15:29 +000054
55/* For logging of zebra meesages. */
hassofce954f2004-10-07 20:29:24 +000056static const char *zebra_command_str [] =
paul718e3742002-12-13 20:15:29 +000057{
58 "NULL",
59 "ZEBRA_INTERFACE_ADD",
60 "ZEBRA_INTERFACE_DELETE",
61 "ZEBRA_INTERFACE_ADDRESS_ADD",
62 "ZEBRA_INTERFACE_ADDRESS_DELETE",
63 "ZEBRA_INTERFACE_UP",
64 "ZEBRA_INTERFACE_DOWN",
65 "ZEBRA_IPV4_ROUTE_ADD",
66 "ZEBRA_IPV4_ROUTE_DELETE",
67 "ZEBRA_IPV6_ROUTE_ADD",
68 "ZEBRA_IPV6_ROUTE_DELETE",
69 "ZEBRA_REDISTRIBUTE_ADD",
70 "ZEBRA_REDISTRIBUTE_DELETE",
71 "ZEBRA_REDISTRIBUTE_DEFAULT_ADD",
72 "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE",
73 "ZEBRA_IPV4_NEXTHOP_LOOKUP",
74 "ZEBRA_IPV6_NEXTHOP_LOOKUP",
75 "ZEBRA_IPV4_IMPORT_LOOKUP",
hasso18a6dce2004-10-03 18:18:34 +000076 "ZEBRA_IPV6_IMPORT_LOOKUP",
77 "ZEBRA_ROUTER_ID_ADD",
78 "ZEBRA_ROUTER_ID_DELETE",
79 "ZEBRA_ROUTER_ID_UPDATE"
paul718e3742002-12-13 20:15:29 +000080};
81
ajs719e9742005-02-28 20:52:15 +000082
83static void zebra_client_close (struct zserv *client);
84
85static int
86zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000087{
ajs719e9742005-02-28 20:52:15 +000088 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000089
ajs719e9742005-02-28 20:52:15 +000090 client->t_suicide = NULL;
91 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000092 return 0;
93}
94
ajs719e9742005-02-28 20:52:15 +000095static int
96zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000097{
ajs719e9742005-02-28 20:52:15 +000098 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000099
ajs719e9742005-02-28 20:52:15 +0000100 client->t_write = NULL;
101 if (client->t_suicide)
102 {
103 zebra_client_close(client);
104 return -1;
105 }
106 switch (buffer_flush_available(client->wb, client->sock))
107 {
108 case BUFFER_ERROR:
109 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
110 "closing", __func__, client->sock);
111 zebra_client_close(client);
112 break;
113 case BUFFER_PENDING:
114 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
115 client, client->sock);
116 break;
117 case BUFFER_EMPTY:
118 break;
119 }
120 return 0;
paulccf35572003-03-01 11:42:20 +0000121}
122
ajs719e9742005-02-28 20:52:15 +0000123static int
124zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000125{
ajs719e9742005-02-28 20:52:15 +0000126 if (client->t_suicide)
127 return -1;
128 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
129 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000130 {
ajs719e9742005-02-28 20:52:15 +0000131 case BUFFER_ERROR:
132 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
133 __func__, client->sock);
134 /* Schedule a delayed close since many of the functions that call this
135 one do not check the return code. They do not allow for the
136 possibility that an I/O error may have caused the client to be
137 deleted. */
138 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
139 client, 0);
140 return -1;
141 break;
142 case BUFFER_EMPTY:
143 THREAD_OFF(client->t_write);
144 break;
145 case BUFFER_PENDING:
146 THREAD_WRITE_ON(zebrad.master, client->t_write,
147 zserv_flush_data, client, client->sock);
148 break;
paulccf35572003-03-01 11:42:20 +0000149 }
paulccf35572003-03-01 11:42:20 +0000150 return 0;
151}
152
paul718e3742002-12-13 20:15:29 +0000153/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000154/*
155 * This function is called in the following situations:
156 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
157 * from the client.
158 * - at startup, when zebra figures out the available interfaces
159 * - when an interface is added (where support for
160 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
161 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
162 * received)
163 */
paul718e3742002-12-13 20:15:29 +0000164int
165zsend_interface_add (struct zserv *client, struct interface *ifp)
166{
167 struct stream *s;
168
169 /* Check this client need interface information. */
170 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000171 return 0;
paul718e3742002-12-13 20:15:29 +0000172
173 s = client->obuf;
174 stream_reset (s);
175
176 /* Place holder for size. */
177 stream_putw (s, 0);
178
179 /* Message type. */
180 stream_putc (s, ZEBRA_INTERFACE_ADD);
181
182 /* Interface information. */
183 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
184 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000185 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000186 stream_putl (s, ifp->flags);
187 stream_putl (s, ifp->metric);
188 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000189 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000190 stream_putl (s, ifp->bandwidth);
191#ifdef HAVE_SOCKADDR_DL
192 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
193#else
194 stream_putl (s, ifp->hw_addr_len);
195 if (ifp->hw_addr_len)
196 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
197#endif /* HAVE_SOCKADDR_DL */
198
199 /* Write packet size. */
200 stream_putw_at (s, 0, stream_get_endp (s));
201
ajs719e9742005-02-28 20:52:15 +0000202 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000203}
204
205/* Interface deletion from zebra daemon. */
206int
207zsend_interface_delete (struct zserv *client, struct interface *ifp)
208{
209 struct stream *s;
210
211 /* Check this client need interface information. */
212 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000213 return 0;
paul718e3742002-12-13 20:15:29 +0000214
215 s = client->obuf;
216 stream_reset (s);
217
218 /* Packet length placeholder. */
219 stream_putw (s, 0);
220
221 /* Interface information. */
222 stream_putc (s, ZEBRA_INTERFACE_DELETE);
223 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
224 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000225 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000226 stream_putl (s, ifp->flags);
227 stream_putl (s, ifp->metric);
228 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000229 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000230 stream_putl (s, ifp->bandwidth);
231
232 /* Write packet length. */
233 stream_putw_at (s, 0, stream_get_endp (s));
234
ajs719e9742005-02-28 20:52:15 +0000235 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000236}
237
paulb9df2d22004-05-09 09:09:59 +0000238/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
239 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
240 *
241 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
242 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
243 * from the client, after the ZEBRA_INTERFACE_ADD has been
244 * sent from zebra to the client
245 * - redistribute new address info to all clients in the following situations
246 * - at startup, when zebra figures out the available interfaces
247 * - when an interface is added (where support for
248 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
249 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
250 * received)
251 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
252 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
253 * - when an RTM_NEWADDR message is received from the kernel,
254 *
255 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
256 *
257 * zsend_interface_address(DELETE)
258 * ^
259 * |
260 * zebra_interface_address_delete_update
261 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000262 * | | if_delete_update
263 * | |
paulb9df2d22004-05-09 09:09:59 +0000264 * ip_address_uninstall connected_delete_ipv4
265 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
266 * ^ ^
267 * | |
268 * | RTM_NEWADDR on routing/netlink socket
269 * |
270 * vty commands:
271 * "no ip address A.B.C.D/M [label LINE]"
272 * "no ip address A.B.C.D/M secondary"
273 * ["no ipv6 address X:X::X:X/M"]
274 *
275 */
paul718e3742002-12-13 20:15:29 +0000276int
paulb9df2d22004-05-09 09:09:59 +0000277zsend_interface_address (int cmd, struct zserv *client,
278 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000279{
280 int blen;
281 struct stream *s;
282 struct prefix *p;
283
284 /* Check this client need interface information. */
285 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000286 return 0;
paul718e3742002-12-13 20:15:29 +0000287
288 s = client->obuf;
289 stream_reset (s);
290
291 /* Place holder for size. */
292 stream_putw (s, 0);
293
paulb9df2d22004-05-09 09:09:59 +0000294 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000295 stream_putl (s, ifp->ifindex);
296
297 /* Interface address flag. */
298 stream_putc (s, ifc->flags);
299
300 /* Prefix information. */
301 p = ifc->address;
302 stream_putc (s, p->family);
303 blen = prefix_blen (p);
304 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000305
306 /*
307 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
308 * but zebra_interface_address_delete_read() in the gnu version
309 * expects to find it
310 */
paul718e3742002-12-13 20:15:29 +0000311 stream_putc (s, p->prefixlen);
312
313 /* Destination. */
314 p = ifc->destination;
315 if (p)
316 stream_put (s, &p->u.prefix, blen);
317 else
318 stream_put (s, NULL, blen);
319
320 /* Write packet size. */
321 stream_putw_at (s, 0, stream_get_endp (s));
322
ajs719e9742005-02-28 20:52:15 +0000323 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000324}
325
paulb9df2d22004-05-09 09:09:59 +0000326/*
327 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
328 * ZEBRA_INTERFACE_DOWN.
329 *
330 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
331 * the clients in one of 2 situations:
332 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
333 * - a vty command modifying the bandwidth of an interface is received.
334 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
335 */
paul718e3742002-12-13 20:15:29 +0000336int
paulb9df2d22004-05-09 09:09:59 +0000337zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000338{
339 struct stream *s;
340
341 /* Check this client need interface information. */
342 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000343 return 0;
paul718e3742002-12-13 20:15:29 +0000344
345 s = client->obuf;
346 stream_reset (s);
347
348 /* Place holder for size. */
349 stream_putw (s, 0);
350
351 /* Zebra command. */
paulb9df2d22004-05-09 09:09:59 +0000352 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000353
354 /* Interface information. */
355 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
356 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000357 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000358 stream_putl (s, ifp->flags);
359 stream_putl (s, ifp->metric);
360 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000361 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000362 stream_putl (s, ifp->bandwidth);
363
364 /* Write packet size. */
365 stream_putw_at (s, 0, stream_get_endp (s));
366
ajs719e9742005-02-28 20:52:15 +0000367 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000368}
369
paulb9df2d22004-05-09 09:09:59 +0000370/*
371 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
372 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
373 * situations:
374 * - when the client starts up, and requests default information
375 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
376 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
377 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
378 * - when the zebra server redistributes routes after it updates its rib
379 *
380 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
381 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
382 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
383 * - deleted from zebra's rib, and this info
384 * has to be redistributed to the clients
385 *
386 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
387 * zebra server when the client wants to tell the zebra server to add a
388 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
389 * same message being sent back and forth, this function and
390 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
391 * duplication.
392 */
paul718e3742002-12-13 20:15:29 +0000393int
paulb9df2d22004-05-09 09:09:59 +0000394zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
395 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000396{
397 int psize;
398 struct stream *s;
399 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000400 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000401 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000402 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000403
paul718e3742002-12-13 20:15:29 +0000404 s = client->obuf;
405 stream_reset (s);
406
407 /* Place holder for size. */
408 stream_putw (s, 0);
409
410 /* Put command, type and nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000411 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000412 stream_putc (s, rib->type);
413 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000414
415 /* marker for message flags field */
416 messmark = stream_get_endp (s);
417 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000418
419 /* Prefix. */
420 psize = PSIZE (p->prefixlen);
421 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000422 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000423
paulb9df2d22004-05-09 09:09:59 +0000424 /*
425 * XXX The message format sent by zebra below does not match the format
426 * of the corresponding message expected by the zebra server
427 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
428 * (is there a bug on the client side if more than one segment is sent?)
429 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
430 * is hard-coded.
431 */
paul718e3742002-12-13 20:15:29 +0000432 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000433
paul718e3742002-12-13 20:15:29 +0000434 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
435 {
436 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000437 {
paul1dcb5172005-05-31 08:38:50 +0000438 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
439 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
440
441 if (nhnummark == 0)
442 {
443 nhnummark = stream_get_endp (s);
444 stream_putc (s, 1); /* placeholder */
445 }
446
paulb9df2d22004-05-09 09:09:59 +0000447 nhnum++;
paul718e3742002-12-13 20:15:29 +0000448
paulb9df2d22004-05-09 09:09:59 +0000449 switch(nexthop->type)
450 {
451 case NEXTHOP_TYPE_IPV4:
452 case NEXTHOP_TYPE_IPV4_IFINDEX:
453 stream_put_in_addr (s, &nexthop->gate.ipv4);
454 break;
455#ifdef HAVE_IPV6
456 case NEXTHOP_TYPE_IPV6:
457 case NEXTHOP_TYPE_IPV6_IFINDEX:
458 case NEXTHOP_TYPE_IPV6_IFNAME:
459 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
460 break;
461#endif
462 default:
463 if (cmd == ZEBRA_IPV4_ROUTE_ADD
464 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
465 {
466 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000467 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000468 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
469 }
470 else
471 {
472 struct in6_addr empty;
473 memset (&empty, 0, sizeof (struct in6_addr));
474 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
475 }
476 }
paul718e3742002-12-13 20:15:29 +0000477
paulb9df2d22004-05-09 09:09:59 +0000478 /* Interface index. */
479 stream_putc (s, 1);
480 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000481
paulb9df2d22004-05-09 09:09:59 +0000482 break;
483 }
paul718e3742002-12-13 20:15:29 +0000484 }
485
486 /* Metric */
paul1dcb5172005-05-31 08:38:50 +0000487 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
488 {
489 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
490 stream_putl (s, rib->metric);
491 }
492
493 /* write real message flags value */
494 stream_putc_at (s, messmark, zapi_flags);
495
paulb9df2d22004-05-09 09:09:59 +0000496 /* Write next-hop number */
497 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000498 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000499
paul718e3742002-12-13 20:15:29 +0000500 /* Write packet size. */
501 stream_putw_at (s, 0, stream_get_endp (s));
502
ajs719e9742005-02-28 20:52:15 +0000503 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000504}
505
paul718e3742002-12-13 20:15:29 +0000506#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000507static int
paul718e3742002-12-13 20:15:29 +0000508zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
509{
510 struct stream *s;
511 struct rib *rib;
512 unsigned long nump;
513 u_char num;
514 struct nexthop *nexthop;
515
516 /* Lookup nexthop. */
517 rib = rib_match_ipv6 (addr);
518
519 /* Get output stream. */
520 s = client->obuf;
521 stream_reset (s);
522
523 /* Fill in result. */
524 stream_putw (s, 0);
525 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
526 stream_put (s, &addr, 16);
527
528 if (rib)
529 {
530 stream_putl (s, rib->metric);
531 num = 0;
paul9985f832005-02-09 15:51:56 +0000532 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000533 stream_putc (s, 0);
534 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
535 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
536 {
537 stream_putc (s, nexthop->type);
538 switch (nexthop->type)
539 {
540 case ZEBRA_NEXTHOP_IPV6:
541 stream_put (s, &nexthop->gate.ipv6, 16);
542 break;
543 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
544 case ZEBRA_NEXTHOP_IPV6_IFNAME:
545 stream_put (s, &nexthop->gate.ipv6, 16);
546 stream_putl (s, nexthop->ifindex);
547 break;
548 case ZEBRA_NEXTHOP_IFINDEX:
549 case ZEBRA_NEXTHOP_IFNAME:
550 stream_putl (s, nexthop->ifindex);
551 break;
hassofa2b17e2004-03-04 17:45:00 +0000552 default:
553 /* do nothing */
554 break;
paul718e3742002-12-13 20:15:29 +0000555 }
556 num++;
557 }
558 stream_putc_at (s, nump, num);
559 }
560 else
561 {
562 stream_putl (s, 0);
563 stream_putc (s, 0);
564 }
565
566 stream_putw_at (s, 0, stream_get_endp (s));
567
ajs719e9742005-02-28 20:52:15 +0000568 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000569}
570#endif /* HAVE_IPV6 */
571
paulb9df2d22004-05-09 09:09:59 +0000572static int
paul718e3742002-12-13 20:15:29 +0000573zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
574{
575 struct stream *s;
576 struct rib *rib;
577 unsigned long nump;
578 u_char num;
579 struct nexthop *nexthop;
580
581 /* Lookup nexthop. */
582 rib = rib_match_ipv4 (addr);
583
584 /* Get output stream. */
585 s = client->obuf;
586 stream_reset (s);
587
588 /* Fill in result. */
589 stream_putw (s, 0);
590 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
591 stream_put_in_addr (s, &addr);
592
593 if (rib)
594 {
595 stream_putl (s, rib->metric);
596 num = 0;
paul9985f832005-02-09 15:51:56 +0000597 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000598 stream_putc (s, 0);
599 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
600 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
601 {
602 stream_putc (s, nexthop->type);
603 switch (nexthop->type)
604 {
605 case ZEBRA_NEXTHOP_IPV4:
606 stream_put_in_addr (s, &nexthop->gate.ipv4);
607 break;
608 case ZEBRA_NEXTHOP_IFINDEX:
609 case ZEBRA_NEXTHOP_IFNAME:
610 stream_putl (s, nexthop->ifindex);
611 break;
hassofa2b17e2004-03-04 17:45:00 +0000612 default:
613 /* do nothing */
614 break;
paul718e3742002-12-13 20:15:29 +0000615 }
616 num++;
617 }
618 stream_putc_at (s, nump, num);
619 }
620 else
621 {
622 stream_putl (s, 0);
623 stream_putc (s, 0);
624 }
625
626 stream_putw_at (s, 0, stream_get_endp (s));
627
ajs719e9742005-02-28 20:52:15 +0000628 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000629}
630
paulb9df2d22004-05-09 09:09:59 +0000631static int
paul718e3742002-12-13 20:15:29 +0000632zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
633{
634 struct stream *s;
635 struct rib *rib;
636 unsigned long nump;
637 u_char num;
638 struct nexthop *nexthop;
639
640 /* Lookup nexthop. */
641 rib = rib_lookup_ipv4 (p);
642
643 /* Get output stream. */
644 s = client->obuf;
645 stream_reset (s);
646
647 /* Fill in result. */
648 stream_putw (s, 0);
649 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
650 stream_put_in_addr (s, &p->prefix);
651
652 if (rib)
653 {
654 stream_putl (s, rib->metric);
655 num = 0;
paul9985f832005-02-09 15:51:56 +0000656 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000657 stream_putc (s, 0);
658 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
659 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
660 {
661 stream_putc (s, nexthop->type);
662 switch (nexthop->type)
663 {
664 case ZEBRA_NEXTHOP_IPV4:
665 stream_put_in_addr (s, &nexthop->gate.ipv4);
666 break;
667 case ZEBRA_NEXTHOP_IFINDEX:
668 case ZEBRA_NEXTHOP_IFNAME:
669 stream_putl (s, nexthop->ifindex);
670 break;
hassofa2b17e2004-03-04 17:45:00 +0000671 default:
672 /* do nothing */
673 break;
paul718e3742002-12-13 20:15:29 +0000674 }
675 num++;
676 }
677 stream_putc_at (s, nump, num);
678 }
679 else
680 {
681 stream_putl (s, 0);
682 stream_putc (s, 0);
683 }
684
685 stream_putw_at (s, 0, stream_get_endp (s));
686
ajs719e9742005-02-28 20:52:15 +0000687 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000688}
689
hasso18a6dce2004-10-03 18:18:34 +0000690/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
691int
692zsend_router_id_update (struct zserv *client, struct prefix *p)
693{
694 struct stream *s;
695 int blen;
696
697 /* Check this client need interface information. */
698 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000699 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000700
701 s = client->obuf;
702 stream_reset (s);
703
704 /* Place holder for size. */
705 stream_putw (s, 0);
706
707 /* Message type. */
708 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
709
710 /* Prefix information. */
711 stream_putc (s, p->family);
712 blen = prefix_blen (p);
713 stream_put (s, &p->u.prefix, blen);
714 stream_putc (s, p->prefixlen);
715
716 /* Write packet size. */
717 stream_putw_at (s, 0, stream_get_endp (s));
718
ajs719e9742005-02-28 20:52:15 +0000719 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000720}
721
paul718e3742002-12-13 20:15:29 +0000722/* Register zebra server interface information. Send current all
723 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000724static int
paul718e3742002-12-13 20:15:29 +0000725zread_interface_add (struct zserv *client, u_short length)
726{
paul1eb8ef22005-04-07 07:30:20 +0000727 struct listnode *ifnode, *ifnnode;
728 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000729 struct interface *ifp;
730 struct connected *c;
731
732 /* Interface information is needed. */
733 client->ifinfo = 1;
734
paul1eb8ef22005-04-07 07:30:20 +0000735 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000736 {
paul718e3742002-12-13 20:15:29 +0000737 /* Skip pseudo interface. */
738 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
739 continue;
740
ajs719e9742005-02-28 20:52:15 +0000741 if (zsend_interface_add (client, ifp) < 0)
742 return -1;
paul718e3742002-12-13 20:15:29 +0000743
paul1eb8ef22005-04-07 07:30:20 +0000744 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000745 {
ajs719e9742005-02-28 20:52:15 +0000746 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
747 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
748 ifp, c) < 0))
749 return -1;
paul718e3742002-12-13 20:15:29 +0000750 }
751 }
ajs719e9742005-02-28 20:52:15 +0000752 return 0;
paul718e3742002-12-13 20:15:29 +0000753}
754
755/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000756static int
paul718e3742002-12-13 20:15:29 +0000757zread_interface_delete (struct zserv *client, u_short length)
758{
759 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000760 return 0;
paul718e3742002-12-13 20:15:29 +0000761}
762
763/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000764/*
765 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
766 * add kernel route.
767 */
ajs719e9742005-02-28 20:52:15 +0000768static int
paul718e3742002-12-13 20:15:29 +0000769zread_ipv4_add (struct zserv *client, u_short length)
770{
771 int i;
772 struct rib *rib;
773 struct prefix_ipv4 p;
774 u_char message;
775 struct in_addr nexthop;
776 u_char nexthop_num;
777 u_char nexthop_type;
778 struct stream *s;
779 unsigned int ifindex;
780 u_char ifname_len;
781
782 /* Get input stream. */
783 s = client->ibuf;
784
785 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000786 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
787
paul718e3742002-12-13 20:15:29 +0000788 /* Type, flags, message. */
789 rib->type = stream_getc (s);
790 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000791 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000792 rib->uptime = time (NULL);
793
794 /* IPv4 prefix. */
795 memset (&p, 0, sizeof (struct prefix_ipv4));
796 p.family = AF_INET;
797 p.prefixlen = stream_getc (s);
798 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
799
800 /* Nexthop parse. */
801 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
802 {
803 nexthop_num = stream_getc (s);
804
805 for (i = 0; i < nexthop_num; i++)
806 {
807 nexthop_type = stream_getc (s);
808
809 switch (nexthop_type)
810 {
811 case ZEBRA_NEXTHOP_IFINDEX:
812 ifindex = stream_getl (s);
813 nexthop_ifindex_add (rib, ifindex);
814 break;
815 case ZEBRA_NEXTHOP_IFNAME:
816 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000817 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000818 break;
819 case ZEBRA_NEXTHOP_IPV4:
820 nexthop.s_addr = stream_get_ipv4 (s);
821 nexthop_ipv4_add (rib, &nexthop);
822 break;
823 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000824 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000825 break;
paul595db7f2003-05-25 21:35:06 +0000826 case ZEBRA_NEXTHOP_BLACKHOLE:
827 nexthop_blackhole_add (rib);
828 break;
paul718e3742002-12-13 20:15:29 +0000829 }
830 }
831 }
832
833 /* Distance. */
834 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
835 rib->distance = stream_getc (s);
836
837 /* Metric. */
838 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
839 rib->metric = stream_getl (s);
840
841 rib_add_ipv4_multipath (&p, rib);
ajs719e9742005-02-28 20:52:15 +0000842 return 0;
paul718e3742002-12-13 20:15:29 +0000843}
844
845/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000846static int
paul718e3742002-12-13 20:15:29 +0000847zread_ipv4_delete (struct zserv *client, u_short length)
848{
849 int i;
850 struct stream *s;
851 struct zapi_ipv4 api;
852 struct in_addr nexthop;
853 unsigned long ifindex;
854 struct prefix_ipv4 p;
855 u_char nexthop_num;
856 u_char nexthop_type;
857 u_char ifname_len;
858
859 s = client->ibuf;
860 ifindex = 0;
861 nexthop.s_addr = 0;
862
863 /* Type, flags, message. */
864 api.type = stream_getc (s);
865 api.flags = stream_getc (s);
866 api.message = stream_getc (s);
867
868 /* IPv4 prefix. */
869 memset (&p, 0, sizeof (struct prefix_ipv4));
870 p.family = AF_INET;
871 p.prefixlen = stream_getc (s);
872 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
873
874 /* Nexthop, ifindex, distance, metric. */
875 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
876 {
877 nexthop_num = stream_getc (s);
878
879 for (i = 0; i < nexthop_num; i++)
880 {
881 nexthop_type = stream_getc (s);
882
883 switch (nexthop_type)
884 {
885 case ZEBRA_NEXTHOP_IFINDEX:
886 ifindex = stream_getl (s);
887 break;
888 case ZEBRA_NEXTHOP_IFNAME:
889 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000890 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000891 break;
892 case ZEBRA_NEXTHOP_IPV4:
893 nexthop.s_addr = stream_get_ipv4 (s);
894 break;
895 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000896 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000897 break;
898 }
899 }
900 }
901
902 /* Distance. */
903 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
904 api.distance = stream_getc (s);
905 else
906 api.distance = 0;
907
908 /* Metric. */
909 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
910 api.metric = stream_getl (s);
911 else
912 api.metric = 0;
913
914 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
915 client->rtm_table);
ajs719e9742005-02-28 20:52:15 +0000916 return 0;
paul718e3742002-12-13 20:15:29 +0000917}
918
919/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000920static int
paul718e3742002-12-13 20:15:29 +0000921zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
922{
923 struct in_addr addr;
924
925 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000926 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000927}
928
929/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000930static int
paul718e3742002-12-13 20:15:29 +0000931zread_ipv4_import_lookup (struct zserv *client, u_short length)
932{
933 struct prefix_ipv4 p;
934
935 p.family = AF_INET;
936 p.prefixlen = stream_getc (client->ibuf);
937 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
938
ajs719e9742005-02-28 20:52:15 +0000939 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000940}
941
942#ifdef HAVE_IPV6
943/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000944static int
paul718e3742002-12-13 20:15:29 +0000945zread_ipv6_add (struct zserv *client, u_short length)
946{
947 int i;
948 struct stream *s;
949 struct zapi_ipv6 api;
950 struct in6_addr nexthop;
951 unsigned long ifindex;
952 struct prefix_ipv6 p;
953
954 s = client->ibuf;
955 ifindex = 0;
956 memset (&nexthop, 0, sizeof (struct in6_addr));
957
958 /* Type, flags, message. */
959 api.type = stream_getc (s);
960 api.flags = stream_getc (s);
961 api.message = stream_getc (s);
962
963 /* IPv4 prefix. */
964 memset (&p, 0, sizeof (struct prefix_ipv6));
965 p.family = AF_INET6;
966 p.prefixlen = stream_getc (s);
967 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
968
969 /* Nexthop, ifindex, distance, metric. */
970 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
971 {
972 u_char nexthop_type;
973
974 api.nexthop_num = stream_getc (s);
975 for (i = 0; i < api.nexthop_num; i++)
976 {
977 nexthop_type = stream_getc (s);
978
979 switch (nexthop_type)
980 {
981 case ZEBRA_NEXTHOP_IPV6:
982 stream_get (&nexthop, s, 16);
983 break;
984 case ZEBRA_NEXTHOP_IFINDEX:
985 ifindex = stream_getl (s);
986 break;
987 }
988 }
989 }
990
991 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
992 api.distance = stream_getc (s);
993 else
994 api.distance = 0;
995
996 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
997 api.metric = stream_getl (s);
998 else
999 api.metric = 0;
1000
1001 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1002 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1003 else
1004 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001005 return 0;
paul718e3742002-12-13 20:15:29 +00001006}
1007
1008/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001009static int
paul718e3742002-12-13 20:15:29 +00001010zread_ipv6_delete (struct zserv *client, u_short length)
1011{
1012 int i;
1013 struct stream *s;
1014 struct zapi_ipv6 api;
1015 struct in6_addr nexthop;
1016 unsigned long ifindex;
1017 struct prefix_ipv6 p;
1018
1019 s = client->ibuf;
1020 ifindex = 0;
1021 memset (&nexthop, 0, sizeof (struct in6_addr));
1022
1023 /* Type, flags, message. */
1024 api.type = stream_getc (s);
1025 api.flags = stream_getc (s);
1026 api.message = stream_getc (s);
1027
1028 /* IPv4 prefix. */
1029 memset (&p, 0, sizeof (struct prefix_ipv6));
1030 p.family = AF_INET6;
1031 p.prefixlen = stream_getc (s);
1032 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1033
1034 /* Nexthop, ifindex, distance, metric. */
1035 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1036 {
1037 u_char nexthop_type;
1038
1039 api.nexthop_num = stream_getc (s);
1040 for (i = 0; i < api.nexthop_num; i++)
1041 {
1042 nexthop_type = stream_getc (s);
1043
1044 switch (nexthop_type)
1045 {
1046 case ZEBRA_NEXTHOP_IPV6:
1047 stream_get (&nexthop, s, 16);
1048 break;
1049 case ZEBRA_NEXTHOP_IFINDEX:
1050 ifindex = stream_getl (s);
1051 break;
1052 }
1053 }
1054 }
1055
1056 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1057 api.distance = stream_getc (s);
1058 else
1059 api.distance = 0;
1060 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1061 api.metric = stream_getl (s);
1062 else
1063 api.metric = 0;
1064
1065 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1066 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1067 else
1068 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001069 return 0;
paul718e3742002-12-13 20:15:29 +00001070}
1071
ajs719e9742005-02-28 20:52:15 +00001072static int
paul718e3742002-12-13 20:15:29 +00001073zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1074{
1075 struct in6_addr addr;
1076 char buf[BUFSIZ];
1077
1078 stream_get (&addr, client->ibuf, 16);
1079 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1080
ajs719e9742005-02-28 20:52:15 +00001081 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001082}
1083#endif /* HAVE_IPV6 */
1084
hasso18a6dce2004-10-03 18:18:34 +00001085/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001086static int
hasso18a6dce2004-10-03 18:18:34 +00001087zread_router_id_add (struct zserv *client, u_short length)
1088{
1089 struct prefix p;
1090
1091 /* Router-id information is needed. */
1092 client->ridinfo = 1;
1093
1094 router_id_get (&p);
1095
ajs719e9742005-02-28 20:52:15 +00001096 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001097}
1098
1099/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001100static int
hasso18a6dce2004-10-03 18:18:34 +00001101zread_router_id_delete (struct zserv *client, u_short length)
1102{
1103 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001104 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001105}
1106
paul718e3742002-12-13 20:15:29 +00001107/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001108static void
paul718e3742002-12-13 20:15:29 +00001109zebra_client_close (struct zserv *client)
1110{
1111 /* Close file descriptor. */
1112 if (client->sock)
1113 {
1114 close (client->sock);
1115 client->sock = -1;
1116 }
1117
1118 /* Free stream buffers. */
1119 if (client->ibuf)
1120 stream_free (client->ibuf);
1121 if (client->obuf)
1122 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001123 if (client->wb)
1124 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001125
1126 /* Release threads. */
1127 if (client->t_read)
1128 thread_cancel (client->t_read);
1129 if (client->t_write)
1130 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001131 if (client->t_suicide)
1132 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001133
1134 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001135 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001136 XFREE (0, client);
1137}
1138
1139/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001140static void
paul718e3742002-12-13 20:15:29 +00001141zebra_client_create (int sock)
1142{
1143 struct zserv *client;
1144
1145 client = XCALLOC (0, sizeof (struct zserv));
1146
1147 /* Make client input/output buffer. */
1148 client->sock = sock;
1149 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1150 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001151 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001152
1153 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001154 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001155
1156 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001157 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001158
1159 /* Make new read thread. */
1160 zebra_event (ZEBRA_READ, sock, client);
1161}
1162
1163/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001164static int
paul718e3742002-12-13 20:15:29 +00001165zebra_client_read (struct thread *thread)
1166{
1167 int sock;
1168 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001169 size_t already;
paul718e3742002-12-13 20:15:29 +00001170 u_short length;
1171 u_char command;
1172
1173 /* Get thread data. Reset reading thread because I'm running. */
1174 sock = THREAD_FD (thread);
1175 client = THREAD_ARG (thread);
1176 client->t_read = NULL;
1177
ajs719e9742005-02-28 20:52:15 +00001178 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001179 {
ajs719e9742005-02-28 20:52:15 +00001180 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001181 return -1;
1182 }
ajs719e9742005-02-28 20:52:15 +00001183
1184 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001185 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001186 {
ajs57a14772005-04-10 15:01:56 +00001187 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001188 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001189 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001190 (nbyte == -1))
1191 {
1192 if (IS_ZEBRA_DEBUG_EVENT)
1193 zlog_debug ("connection closed socket [%d]", sock);
1194 zebra_client_close (client);
1195 return -1;
1196 }
ajs57a14772005-04-10 15:01:56 +00001197 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001198 {
1199 /* Try again later. */
1200 zebra_event (ZEBRA_READ, sock, client);
1201 return 0;
1202 }
ajs57a14772005-04-10 15:01:56 +00001203 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001204 }
1205
1206 /* Reset to read from the beginning of the incoming packet. */
1207 stream_set_getp(client->ibuf, 0);
1208
paul718e3742002-12-13 20:15:29 +00001209 length = stream_getw (client->ibuf);
1210 command = stream_getc (client->ibuf);
1211
ajs719e9742005-02-28 20:52:15 +00001212 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001213 {
ajs57a14772005-04-10 15:01:56 +00001214 zlog_warn("%s: socket %d message length %u is less than header size %d",
1215 __func__, sock, length, ZEBRA_HEADER_SIZE);
1216 zebra_client_close (client);
1217 return -1;
1218 }
1219 if (length > STREAM_SIZE(client->ibuf))
1220 {
1221 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1222 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001223 zebra_client_close (client);
1224 return -1;
1225 }
1226
paul718e3742002-12-13 20:15:29 +00001227 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001228 if (already < length)
paul718e3742002-12-13 20:15:29 +00001229 {
ajs57a14772005-04-10 15:01:56 +00001230 ssize_t nbyte;
1231 if (((nbyte = stream_read_try (client->ibuf, sock,
1232 length-already)) == 0) ||
1233 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001234 {
1235 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001236 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001237 zebra_client_close (client);
1238 return -1;
1239 }
ajs57a14772005-04-10 15:01:56 +00001240 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001241 {
1242 /* Try again later. */
1243 zebra_event (ZEBRA_READ, sock, client);
1244 return 0;
1245 }
paul718e3742002-12-13 20:15:29 +00001246 }
1247
ajs719e9742005-02-28 20:52:15 +00001248 length -= ZEBRA_HEADER_SIZE;
1249
paul718e3742002-12-13 20:15:29 +00001250 /* Debug packet information. */
1251 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001252 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001253
1254 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001255 zlog_debug ("zebra message received [%s] %d",
paul718e3742002-12-13 20:15:29 +00001256 zebra_command_str[command], length);
1257
1258 switch (command)
1259 {
hasso18a6dce2004-10-03 18:18:34 +00001260 case ZEBRA_ROUTER_ID_ADD:
1261 zread_router_id_add (client, length);
1262 break;
1263 case ZEBRA_ROUTER_ID_DELETE:
1264 zread_router_id_delete (client, length);
1265 break;
paul718e3742002-12-13 20:15:29 +00001266 case ZEBRA_INTERFACE_ADD:
1267 zread_interface_add (client, length);
1268 break;
1269 case ZEBRA_INTERFACE_DELETE:
1270 zread_interface_delete (client, length);
1271 break;
1272 case ZEBRA_IPV4_ROUTE_ADD:
1273 zread_ipv4_add (client, length);
1274 break;
1275 case ZEBRA_IPV4_ROUTE_DELETE:
1276 zread_ipv4_delete (client, length);
1277 break;
1278#ifdef HAVE_IPV6
1279 case ZEBRA_IPV6_ROUTE_ADD:
1280 zread_ipv6_add (client, length);
1281 break;
1282 case ZEBRA_IPV6_ROUTE_DELETE:
1283 zread_ipv6_delete (client, length);
1284 break;
1285#endif /* HAVE_IPV6 */
1286 case ZEBRA_REDISTRIBUTE_ADD:
1287 zebra_redistribute_add (command, client, length);
1288 break;
1289 case ZEBRA_REDISTRIBUTE_DELETE:
1290 zebra_redistribute_delete (command, client, length);
1291 break;
1292 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1293 zebra_redistribute_default_add (command, client, length);
1294 break;
1295 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1296 zebra_redistribute_default_delete (command, client, length);
1297 break;
1298 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1299 zread_ipv4_nexthop_lookup (client, length);
1300 break;
1301#ifdef HAVE_IPV6
1302 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1303 zread_ipv6_nexthop_lookup (client, length);
1304 break;
1305#endif /* HAVE_IPV6 */
1306 case ZEBRA_IPV4_IMPORT_LOOKUP:
1307 zread_ipv4_import_lookup (client, length);
1308 break;
1309 default:
1310 zlog_info ("Zebra received unknown command %d", command);
1311 break;
1312 }
1313
ajs719e9742005-02-28 20:52:15 +00001314 if (client->t_suicide)
1315 {
1316 /* No need to wait for thread callback, just kill immediately. */
1317 zebra_client_close(client);
1318 return -1;
1319 }
1320
paul718e3742002-12-13 20:15:29 +00001321 stream_reset (client->ibuf);
1322 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001323 return 0;
1324}
1325
paul718e3742002-12-13 20:15:29 +00001326
1327/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001328static int
paul718e3742002-12-13 20:15:29 +00001329zebra_accept (struct thread *thread)
1330{
1331 int accept_sock;
1332 int client_sock;
1333 struct sockaddr_in client;
1334 socklen_t len;
1335
1336 accept_sock = THREAD_FD (thread);
1337
ajs719e9742005-02-28 20:52:15 +00001338 /* Reregister myself. */
1339 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1340
paul718e3742002-12-13 20:15:29 +00001341 len = sizeof (struct sockaddr_in);
1342 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1343
1344 if (client_sock < 0)
1345 {
ajs6099b3b2004-11-20 02:06:59 +00001346 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001347 return -1;
1348 }
1349
paulccf35572003-03-01 11:42:20 +00001350 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001351 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001352
paul718e3742002-12-13 20:15:29 +00001353 /* Create new zebra client. */
1354 zebra_client_create (client_sock);
1355
paul718e3742002-12-13 20:15:29 +00001356 return 0;
1357}
1358
paulb9df2d22004-05-09 09:09:59 +00001359#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001360/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001361static void
paul718e3742002-12-13 20:15:29 +00001362zebra_serv ()
1363{
1364 int ret;
1365 int accept_sock;
1366 struct sockaddr_in addr;
1367
1368 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1369
1370 if (accept_sock < 0)
1371 {
paul3d1dc852005-04-05 00:45:23 +00001372 zlog_warn ("Can't create zserv stream socket: %s",
1373 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001374 zlog_warn ("zebra can't provice full functionality due to above error");
1375 return;
1376 }
1377
1378 memset (&addr, 0, sizeof (struct sockaddr_in));
1379 addr.sin_family = AF_INET;
1380 addr.sin_port = htons (ZEBRA_PORT);
1381#ifdef HAVE_SIN_LEN
1382 addr.sin_len = sizeof (struct sockaddr_in);
1383#endif /* HAVE_SIN_LEN */
1384 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1385
1386 sockopt_reuseaddr (accept_sock);
1387 sockopt_reuseport (accept_sock);
1388
pauledd7c242003-06-04 13:59:38 +00001389 if ( zserv_privs.change(ZPRIVS_RAISE) )
1390 zlog (NULL, LOG_ERR, "Can't raise privileges");
1391
paul718e3742002-12-13 20:15:29 +00001392 ret = bind (accept_sock, (struct sockaddr *)&addr,
1393 sizeof (struct sockaddr_in));
1394 if (ret < 0)
1395 {
paul3d1dc852005-04-05 00:45:23 +00001396 zlog_warn ("Can't bind to stream socket: %s",
1397 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001398 zlog_warn ("zebra can't provice full functionality due to above error");
1399 close (accept_sock); /* Avoid sd leak. */
1400 return;
1401 }
pauledd7c242003-06-04 13:59:38 +00001402
1403 if ( zserv_privs.change(ZPRIVS_LOWER) )
1404 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001405
1406 ret = listen (accept_sock, 1);
1407 if (ret < 0)
1408 {
paul3d1dc852005-04-05 00:45:23 +00001409 zlog_warn ("Can't listen to stream socket: %s",
1410 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001411 zlog_warn ("zebra can't provice full functionality due to above error");
1412 close (accept_sock); /* Avoid sd leak. */
1413 return;
1414 }
1415
1416 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1417}
paulb9df2d22004-05-09 09:09:59 +00001418#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001419
1420/* For sockaddr_un. */
1421#include <sys/un.h>
1422
1423/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001424static void
hassofce954f2004-10-07 20:29:24 +00001425zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001426{
1427 int ret;
1428 int sock, len;
1429 struct sockaddr_un serv;
1430 mode_t old_mask;
1431
1432 /* First of all, unlink existing socket */
1433 unlink (path);
1434
1435 /* Set umask */
1436 old_mask = umask (0077);
1437
1438 /* Make UNIX domain socket. */
1439 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1440 if (sock < 0)
1441 {
paul3d1dc852005-04-05 00:45:23 +00001442 zlog_warn ("Can't create zserv unix socket: %s",
1443 safe_strerror (errno));
1444 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001445 return;
1446 }
1447
1448 /* Make server socket. */
1449 memset (&serv, 0, sizeof (struct sockaddr_un));
1450 serv.sun_family = AF_UNIX;
1451 strncpy (serv.sun_path, path, strlen (path));
1452#ifdef HAVE_SUN_LEN
1453 len = serv.sun_len = SUN_LEN(&serv);
1454#else
1455 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1456#endif /* HAVE_SUN_LEN */
1457
1458 ret = bind (sock, (struct sockaddr *) &serv, len);
1459 if (ret < 0)
1460 {
paul3d1dc852005-04-05 00:45:23 +00001461 zlog_warn ("Can't bind to unix socket %s: %s",
1462 path, safe_strerror (errno));
1463 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001464 close (sock);
1465 return;
1466 }
1467
1468 ret = listen (sock, 5);
1469 if (ret < 0)
1470 {
paul3d1dc852005-04-05 00:45:23 +00001471 zlog_warn ("Can't listen to unix socket %s: %s",
1472 path, safe_strerror (errno));
1473 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001474 close (sock);
1475 return;
1476 }
1477
1478 umask (old_mask);
1479
1480 zebra_event (ZEBRA_SERV, sock, NULL);
1481}
1482
paul718e3742002-12-13 20:15:29 +00001483
paulb9df2d22004-05-09 09:09:59 +00001484static void
paul718e3742002-12-13 20:15:29 +00001485zebra_event (enum event event, int sock, struct zserv *client)
1486{
1487 switch (event)
1488 {
1489 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001490 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001491 break;
1492 case ZEBRA_READ:
1493 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001494 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001495 break;
1496 case ZEBRA_WRITE:
1497 /**/
1498 break;
1499 }
1500}
1501
1502/* Display default rtm_table for all clients. */
1503DEFUN (show_table,
1504 show_table_cmd,
1505 "show table",
1506 SHOW_STR
1507 "default routing table to use for all clients\n")
1508{
paulb21b19c2003-06-15 01:28:29 +00001509 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001510 VTY_NEWLINE);
1511 return CMD_SUCCESS;
1512}
1513
1514DEFUN (config_table,
1515 config_table_cmd,
1516 "table TABLENO",
1517 "Configure target kernel routing table\n"
1518 "TABLE integer\n")
1519{
paulb21b19c2003-06-15 01:28:29 +00001520 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001521 return CMD_SUCCESS;
1522}
1523
hasso647e4f12003-05-25 11:43:52 +00001524DEFUN (ip_forwarding,
1525 ip_forwarding_cmd,
1526 "ip forwarding",
1527 IP_STR
1528 "Turn on IP forwarding")
1529{
1530 int ret;
1531
1532 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001533 if (ret == 0)
1534 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001535
hasso647e4f12003-05-25 11:43:52 +00001536 if (ret == 0)
1537 {
1538 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1539 return CMD_WARNING;
1540 }
1541
1542 return CMD_SUCCESS;
1543}
1544
paul718e3742002-12-13 20:15:29 +00001545DEFUN (no_ip_forwarding,
1546 no_ip_forwarding_cmd,
1547 "no ip forwarding",
1548 NO_STR
1549 IP_STR
1550 "Turn off IP forwarding")
1551{
1552 int ret;
1553
1554 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001555 if (ret != 0)
1556 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001557
paul718e3742002-12-13 20:15:29 +00001558 if (ret != 0)
1559 {
1560 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1561 return CMD_WARNING;
1562 }
1563
1564 return CMD_SUCCESS;
1565}
1566
1567/* This command is for debugging purpose. */
1568DEFUN (show_zebra_client,
1569 show_zebra_client_cmd,
1570 "show zebra client",
1571 SHOW_STR
1572 "Zebra information"
1573 "Client information")
1574{
hasso52dc7ee2004-09-23 19:18:23 +00001575 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001576 struct zserv *client;
1577
paul1eb8ef22005-04-07 07:30:20 +00001578 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1579 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1580
paul718e3742002-12-13 20:15:29 +00001581 return CMD_SUCCESS;
1582}
1583
1584/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001585static int
paul718e3742002-12-13 20:15:29 +00001586config_write_table (struct vty *vty)
1587{
paulb21b19c2003-06-15 01:28:29 +00001588 if (zebrad.rtm_table_default)
1589 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001590 VTY_NEWLINE);
1591 return 0;
1592}
1593
1594/* table node for routing tables. */
1595struct cmd_node table_node =
1596{
1597 TABLE_NODE,
1598 "", /* This node has no interface. */
1599 1
1600};
1601
1602/* Only display ip forwarding is enabled or not. */
1603DEFUN (show_ip_forwarding,
1604 show_ip_forwarding_cmd,
1605 "show ip forwarding",
1606 SHOW_STR
1607 IP_STR
1608 "IP forwarding status\n")
1609{
1610 int ret;
1611
1612 ret = ipforward ();
1613
1614 if (ret == 0)
1615 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1616 else
1617 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1618 return CMD_SUCCESS;
1619}
1620
1621#ifdef HAVE_IPV6
1622/* Only display ipv6 forwarding is enabled or not. */
1623DEFUN (show_ipv6_forwarding,
1624 show_ipv6_forwarding_cmd,
1625 "show ipv6 forwarding",
1626 SHOW_STR
1627 "IPv6 information\n"
1628 "Forwarding status\n")
1629{
1630 int ret;
1631
1632 ret = ipforward_ipv6 ();
1633
1634 switch (ret)
1635 {
1636 case -1:
1637 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1638 break;
1639 case 0:
1640 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1641 break;
1642 case 1:
1643 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1644 break;
1645 default:
1646 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1647 break;
1648 }
1649 return CMD_SUCCESS;
1650}
1651
hasso55906722004-02-11 22:42:16 +00001652DEFUN (ipv6_forwarding,
1653 ipv6_forwarding_cmd,
1654 "ipv6 forwarding",
1655 IPV6_STR
1656 "Turn on IPv6 forwarding")
1657{
1658 int ret;
1659
hasso41d3fc92004-04-06 11:59:00 +00001660 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001661 if (ret == 0)
1662 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001663
hasso41d3fc92004-04-06 11:59:00 +00001664 if (ret == 0)
1665 {
hasso55906722004-02-11 22:42:16 +00001666 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1667 return CMD_WARNING;
1668 }
1669
1670 return CMD_SUCCESS;
1671}
1672
paul718e3742002-12-13 20:15:29 +00001673DEFUN (no_ipv6_forwarding,
1674 no_ipv6_forwarding_cmd,
1675 "no ipv6 forwarding",
1676 NO_STR
hasso55906722004-02-11 22:42:16 +00001677 IPV6_STR
1678 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001679{
1680 int ret;
1681
hasso41d3fc92004-04-06 11:59:00 +00001682 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001683 if (ret != 0)
1684 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001685
paul718e3742002-12-13 20:15:29 +00001686 if (ret != 0)
1687 {
1688 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1689 return CMD_WARNING;
1690 }
1691
1692 return CMD_SUCCESS;
1693}
1694
1695#endif /* HAVE_IPV6 */
1696
1697/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001698static int
paul718e3742002-12-13 20:15:29 +00001699config_write_forwarding (struct vty *vty)
1700{
hasso18a6dce2004-10-03 18:18:34 +00001701 /* FIXME: Find better place for that. */
1702 router_id_write (vty);
1703
paul3e0b3a52004-08-23 18:58:32 +00001704 if (ipforward ())
1705 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001706#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001707 if (ipforward_ipv6 ())
1708 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001709#endif /* HAVE_IPV6 */
1710 vty_out (vty, "!%s", VTY_NEWLINE);
1711 return 0;
1712}
1713
1714/* table node for routing tables. */
1715struct cmd_node forwarding_node =
1716{
1717 FORWARDING_NODE,
1718 "", /* This node has no interface. */
1719 1
1720};
1721
1722
1723/* Initialisation of zebra and installation of commands. */
1724void
paula1ac18c2005-06-28 17:17:12 +00001725zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001726{
1727 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001728 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001729
paul718e3742002-12-13 20:15:29 +00001730 /* Make zebra server socket. */
1731#ifdef HAVE_TCP_ZEBRA
1732 zebra_serv ();
1733#else
1734 zebra_serv_un (ZEBRA_SERV_PATH);
1735#endif /* HAVE_TCP_ZEBRA */
1736
1737 /* Install configuration write function. */
1738 install_node (&table_node, config_write_table);
1739 install_node (&forwarding_node, config_write_forwarding);
1740
1741 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1742 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001743 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001744 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1745 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1746
1747#ifdef HAVE_NETLINK
1748 install_element (VIEW_NODE, &show_table_cmd);
1749 install_element (ENABLE_NODE, &show_table_cmd);
1750 install_element (CONFIG_NODE, &config_table_cmd);
1751#endif /* HAVE_NETLINK */
1752
1753#ifdef HAVE_IPV6
1754 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1755 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001756 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001757 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1758#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001759}