blob: 872ddb85ee3f54540db968d263871d4019fecf1c [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))
hassobe61c4e2005-08-27 06:05:47 +00001002 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0, api.metric,
1003 api.distance);
paul718e3742002-12-13 20:15:29 +00001004 else
hassobe61c4e2005-08-27 06:05:47 +00001005 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0, api.metric,
1006 api.distance);
ajs719e9742005-02-28 20:52:15 +00001007 return 0;
paul718e3742002-12-13 20:15:29 +00001008}
1009
1010/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001011static int
paul718e3742002-12-13 20:15:29 +00001012zread_ipv6_delete (struct zserv *client, u_short length)
1013{
1014 int i;
1015 struct stream *s;
1016 struct zapi_ipv6 api;
1017 struct in6_addr nexthop;
1018 unsigned long ifindex;
1019 struct prefix_ipv6 p;
1020
1021 s = client->ibuf;
1022 ifindex = 0;
1023 memset (&nexthop, 0, sizeof (struct in6_addr));
1024
1025 /* Type, flags, message. */
1026 api.type = stream_getc (s);
1027 api.flags = stream_getc (s);
1028 api.message = stream_getc (s);
1029
1030 /* IPv4 prefix. */
1031 memset (&p, 0, sizeof (struct prefix_ipv6));
1032 p.family = AF_INET6;
1033 p.prefixlen = stream_getc (s);
1034 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1035
1036 /* Nexthop, ifindex, distance, metric. */
1037 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1038 {
1039 u_char nexthop_type;
1040
1041 api.nexthop_num = stream_getc (s);
1042 for (i = 0; i < api.nexthop_num; i++)
1043 {
1044 nexthop_type = stream_getc (s);
1045
1046 switch (nexthop_type)
1047 {
1048 case ZEBRA_NEXTHOP_IPV6:
1049 stream_get (&nexthop, s, 16);
1050 break;
1051 case ZEBRA_NEXTHOP_IFINDEX:
1052 ifindex = stream_getl (s);
1053 break;
1054 }
1055 }
1056 }
1057
1058 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1059 api.distance = stream_getc (s);
1060 else
1061 api.distance = 0;
1062 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1063 api.metric = stream_getl (s);
1064 else
1065 api.metric = 0;
1066
1067 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1068 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1069 else
1070 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001071 return 0;
paul718e3742002-12-13 20:15:29 +00001072}
1073
ajs719e9742005-02-28 20:52:15 +00001074static int
paul718e3742002-12-13 20:15:29 +00001075zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1076{
1077 struct in6_addr addr;
1078 char buf[BUFSIZ];
1079
1080 stream_get (&addr, client->ibuf, 16);
1081 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1082
ajs719e9742005-02-28 20:52:15 +00001083 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001084}
1085#endif /* HAVE_IPV6 */
1086
hasso18a6dce2004-10-03 18:18:34 +00001087/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001088static int
hasso18a6dce2004-10-03 18:18:34 +00001089zread_router_id_add (struct zserv *client, u_short length)
1090{
1091 struct prefix p;
1092
1093 /* Router-id information is needed. */
1094 client->ridinfo = 1;
1095
1096 router_id_get (&p);
1097
ajs719e9742005-02-28 20:52:15 +00001098 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001099}
1100
1101/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001102static int
hasso18a6dce2004-10-03 18:18:34 +00001103zread_router_id_delete (struct zserv *client, u_short length)
1104{
1105 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001106 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001107}
1108
paul718e3742002-12-13 20:15:29 +00001109/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001110static void
paul718e3742002-12-13 20:15:29 +00001111zebra_client_close (struct zserv *client)
1112{
1113 /* Close file descriptor. */
1114 if (client->sock)
1115 {
1116 close (client->sock);
1117 client->sock = -1;
1118 }
1119
1120 /* Free stream buffers. */
1121 if (client->ibuf)
1122 stream_free (client->ibuf);
1123 if (client->obuf)
1124 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001125 if (client->wb)
1126 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001127
1128 /* Release threads. */
1129 if (client->t_read)
1130 thread_cancel (client->t_read);
1131 if (client->t_write)
1132 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001133 if (client->t_suicide)
1134 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001135
1136 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001137 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001138 XFREE (0, client);
1139}
1140
1141/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001142static void
paul718e3742002-12-13 20:15:29 +00001143zebra_client_create (int sock)
1144{
1145 struct zserv *client;
1146
1147 client = XCALLOC (0, sizeof (struct zserv));
1148
1149 /* Make client input/output buffer. */
1150 client->sock = sock;
1151 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1152 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001153 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001154
1155 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001156 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001157
1158 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001159 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001160
1161 /* Make new read thread. */
1162 zebra_event (ZEBRA_READ, sock, client);
1163}
1164
1165/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001166static int
paul718e3742002-12-13 20:15:29 +00001167zebra_client_read (struct thread *thread)
1168{
1169 int sock;
1170 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001171 size_t already;
paul718e3742002-12-13 20:15:29 +00001172 u_short length;
1173 u_char command;
1174
1175 /* Get thread data. Reset reading thread because I'm running. */
1176 sock = THREAD_FD (thread);
1177 client = THREAD_ARG (thread);
1178 client->t_read = NULL;
1179
ajs719e9742005-02-28 20:52:15 +00001180 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001181 {
ajs719e9742005-02-28 20:52:15 +00001182 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001183 return -1;
1184 }
ajs719e9742005-02-28 20:52:15 +00001185
1186 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001187 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001188 {
ajs57a14772005-04-10 15:01:56 +00001189 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001190 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001191 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001192 (nbyte == -1))
1193 {
1194 if (IS_ZEBRA_DEBUG_EVENT)
1195 zlog_debug ("connection closed socket [%d]", sock);
1196 zebra_client_close (client);
1197 return -1;
1198 }
ajs57a14772005-04-10 15:01:56 +00001199 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001200 {
1201 /* Try again later. */
1202 zebra_event (ZEBRA_READ, sock, client);
1203 return 0;
1204 }
ajs57a14772005-04-10 15:01:56 +00001205 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001206 }
1207
1208 /* Reset to read from the beginning of the incoming packet. */
1209 stream_set_getp(client->ibuf, 0);
1210
paul718e3742002-12-13 20:15:29 +00001211 length = stream_getw (client->ibuf);
1212 command = stream_getc (client->ibuf);
1213
ajs719e9742005-02-28 20:52:15 +00001214 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001215 {
ajs57a14772005-04-10 15:01:56 +00001216 zlog_warn("%s: socket %d message length %u is less than header size %d",
1217 __func__, sock, length, ZEBRA_HEADER_SIZE);
1218 zebra_client_close (client);
1219 return -1;
1220 }
1221 if (length > STREAM_SIZE(client->ibuf))
1222 {
1223 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1224 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001225 zebra_client_close (client);
1226 return -1;
1227 }
1228
paul718e3742002-12-13 20:15:29 +00001229 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001230 if (already < length)
paul718e3742002-12-13 20:15:29 +00001231 {
ajs57a14772005-04-10 15:01:56 +00001232 ssize_t nbyte;
1233 if (((nbyte = stream_read_try (client->ibuf, sock,
1234 length-already)) == 0) ||
1235 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001236 {
1237 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001238 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001239 zebra_client_close (client);
1240 return -1;
1241 }
ajs57a14772005-04-10 15:01:56 +00001242 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001243 {
1244 /* Try again later. */
1245 zebra_event (ZEBRA_READ, sock, client);
1246 return 0;
1247 }
paul718e3742002-12-13 20:15:29 +00001248 }
1249
ajs719e9742005-02-28 20:52:15 +00001250 length -= ZEBRA_HEADER_SIZE;
1251
paul718e3742002-12-13 20:15:29 +00001252 /* Debug packet information. */
1253 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001254 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001255
1256 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001257 zlog_debug ("zebra message received [%s] %d",
paul718e3742002-12-13 20:15:29 +00001258 zebra_command_str[command], length);
1259
1260 switch (command)
1261 {
hasso18a6dce2004-10-03 18:18:34 +00001262 case ZEBRA_ROUTER_ID_ADD:
1263 zread_router_id_add (client, length);
1264 break;
1265 case ZEBRA_ROUTER_ID_DELETE:
1266 zread_router_id_delete (client, length);
1267 break;
paul718e3742002-12-13 20:15:29 +00001268 case ZEBRA_INTERFACE_ADD:
1269 zread_interface_add (client, length);
1270 break;
1271 case ZEBRA_INTERFACE_DELETE:
1272 zread_interface_delete (client, length);
1273 break;
1274 case ZEBRA_IPV4_ROUTE_ADD:
1275 zread_ipv4_add (client, length);
1276 break;
1277 case ZEBRA_IPV4_ROUTE_DELETE:
1278 zread_ipv4_delete (client, length);
1279 break;
1280#ifdef HAVE_IPV6
1281 case ZEBRA_IPV6_ROUTE_ADD:
1282 zread_ipv6_add (client, length);
1283 break;
1284 case ZEBRA_IPV6_ROUTE_DELETE:
1285 zread_ipv6_delete (client, length);
1286 break;
1287#endif /* HAVE_IPV6 */
1288 case ZEBRA_REDISTRIBUTE_ADD:
1289 zebra_redistribute_add (command, client, length);
1290 break;
1291 case ZEBRA_REDISTRIBUTE_DELETE:
1292 zebra_redistribute_delete (command, client, length);
1293 break;
1294 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1295 zebra_redistribute_default_add (command, client, length);
1296 break;
1297 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1298 zebra_redistribute_default_delete (command, client, length);
1299 break;
1300 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1301 zread_ipv4_nexthop_lookup (client, length);
1302 break;
1303#ifdef HAVE_IPV6
1304 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1305 zread_ipv6_nexthop_lookup (client, length);
1306 break;
1307#endif /* HAVE_IPV6 */
1308 case ZEBRA_IPV4_IMPORT_LOOKUP:
1309 zread_ipv4_import_lookup (client, length);
1310 break;
1311 default:
1312 zlog_info ("Zebra received unknown command %d", command);
1313 break;
1314 }
1315
ajs719e9742005-02-28 20:52:15 +00001316 if (client->t_suicide)
1317 {
1318 /* No need to wait for thread callback, just kill immediately. */
1319 zebra_client_close(client);
1320 return -1;
1321 }
1322
paul718e3742002-12-13 20:15:29 +00001323 stream_reset (client->ibuf);
1324 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001325 return 0;
1326}
1327
paul718e3742002-12-13 20:15:29 +00001328
1329/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001330static int
paul718e3742002-12-13 20:15:29 +00001331zebra_accept (struct thread *thread)
1332{
1333 int accept_sock;
1334 int client_sock;
1335 struct sockaddr_in client;
1336 socklen_t len;
1337
1338 accept_sock = THREAD_FD (thread);
1339
ajs719e9742005-02-28 20:52:15 +00001340 /* Reregister myself. */
1341 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1342
paul718e3742002-12-13 20:15:29 +00001343 len = sizeof (struct sockaddr_in);
1344 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1345
1346 if (client_sock < 0)
1347 {
ajs6099b3b2004-11-20 02:06:59 +00001348 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001349 return -1;
1350 }
1351
paulccf35572003-03-01 11:42:20 +00001352 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001353 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001354
paul718e3742002-12-13 20:15:29 +00001355 /* Create new zebra client. */
1356 zebra_client_create (client_sock);
1357
paul718e3742002-12-13 20:15:29 +00001358 return 0;
1359}
1360
paulb9df2d22004-05-09 09:09:59 +00001361#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001362/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001363static void
paul718e3742002-12-13 20:15:29 +00001364zebra_serv ()
1365{
1366 int ret;
1367 int accept_sock;
1368 struct sockaddr_in addr;
1369
1370 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1371
1372 if (accept_sock < 0)
1373 {
paul3d1dc852005-04-05 00:45:23 +00001374 zlog_warn ("Can't create zserv stream socket: %s",
1375 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001376 zlog_warn ("zebra can't provice full functionality due to above error");
1377 return;
1378 }
1379
1380 memset (&addr, 0, sizeof (struct sockaddr_in));
1381 addr.sin_family = AF_INET;
1382 addr.sin_port = htons (ZEBRA_PORT);
1383#ifdef HAVE_SIN_LEN
1384 addr.sin_len = sizeof (struct sockaddr_in);
1385#endif /* HAVE_SIN_LEN */
1386 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1387
1388 sockopt_reuseaddr (accept_sock);
1389 sockopt_reuseport (accept_sock);
1390
pauledd7c242003-06-04 13:59:38 +00001391 if ( zserv_privs.change(ZPRIVS_RAISE) )
1392 zlog (NULL, LOG_ERR, "Can't raise privileges");
1393
paul718e3742002-12-13 20:15:29 +00001394 ret = bind (accept_sock, (struct sockaddr *)&addr,
1395 sizeof (struct sockaddr_in));
1396 if (ret < 0)
1397 {
paul3d1dc852005-04-05 00:45:23 +00001398 zlog_warn ("Can't bind to stream socket: %s",
1399 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001400 zlog_warn ("zebra can't provice full functionality due to above error");
1401 close (accept_sock); /* Avoid sd leak. */
1402 return;
1403 }
pauledd7c242003-06-04 13:59:38 +00001404
1405 if ( zserv_privs.change(ZPRIVS_LOWER) )
1406 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001407
1408 ret = listen (accept_sock, 1);
1409 if (ret < 0)
1410 {
paul3d1dc852005-04-05 00:45:23 +00001411 zlog_warn ("Can't listen to stream socket: %s",
1412 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001413 zlog_warn ("zebra can't provice full functionality due to above error");
1414 close (accept_sock); /* Avoid sd leak. */
1415 return;
1416 }
1417
1418 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1419}
paulb9df2d22004-05-09 09:09:59 +00001420#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001421
1422/* For sockaddr_un. */
1423#include <sys/un.h>
1424
1425/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001426static void
hassofce954f2004-10-07 20:29:24 +00001427zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001428{
1429 int ret;
1430 int sock, len;
1431 struct sockaddr_un serv;
1432 mode_t old_mask;
1433
1434 /* First of all, unlink existing socket */
1435 unlink (path);
1436
1437 /* Set umask */
1438 old_mask = umask (0077);
1439
1440 /* Make UNIX domain socket. */
1441 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1442 if (sock < 0)
1443 {
paul3d1dc852005-04-05 00:45:23 +00001444 zlog_warn ("Can't create zserv unix socket: %s",
1445 safe_strerror (errno));
1446 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001447 return;
1448 }
1449
1450 /* Make server socket. */
1451 memset (&serv, 0, sizeof (struct sockaddr_un));
1452 serv.sun_family = AF_UNIX;
1453 strncpy (serv.sun_path, path, strlen (path));
1454#ifdef HAVE_SUN_LEN
1455 len = serv.sun_len = SUN_LEN(&serv);
1456#else
1457 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1458#endif /* HAVE_SUN_LEN */
1459
1460 ret = bind (sock, (struct sockaddr *) &serv, len);
1461 if (ret < 0)
1462 {
paul3d1dc852005-04-05 00:45:23 +00001463 zlog_warn ("Can't bind to unix socket %s: %s",
1464 path, safe_strerror (errno));
1465 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001466 close (sock);
1467 return;
1468 }
1469
1470 ret = listen (sock, 5);
1471 if (ret < 0)
1472 {
paul3d1dc852005-04-05 00:45:23 +00001473 zlog_warn ("Can't listen to unix socket %s: %s",
1474 path, safe_strerror (errno));
1475 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001476 close (sock);
1477 return;
1478 }
1479
1480 umask (old_mask);
1481
1482 zebra_event (ZEBRA_SERV, sock, NULL);
1483}
1484
paul718e3742002-12-13 20:15:29 +00001485
paulb9df2d22004-05-09 09:09:59 +00001486static void
paul718e3742002-12-13 20:15:29 +00001487zebra_event (enum event event, int sock, struct zserv *client)
1488{
1489 switch (event)
1490 {
1491 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001492 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001493 break;
1494 case ZEBRA_READ:
1495 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001496 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001497 break;
1498 case ZEBRA_WRITE:
1499 /**/
1500 break;
1501 }
1502}
1503
1504/* Display default rtm_table for all clients. */
1505DEFUN (show_table,
1506 show_table_cmd,
1507 "show table",
1508 SHOW_STR
1509 "default routing table to use for all clients\n")
1510{
paulb21b19c2003-06-15 01:28:29 +00001511 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001512 VTY_NEWLINE);
1513 return CMD_SUCCESS;
1514}
1515
1516DEFUN (config_table,
1517 config_table_cmd,
1518 "table TABLENO",
1519 "Configure target kernel routing table\n"
1520 "TABLE integer\n")
1521{
paulb21b19c2003-06-15 01:28:29 +00001522 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001523 return CMD_SUCCESS;
1524}
1525
hasso647e4f12003-05-25 11:43:52 +00001526DEFUN (ip_forwarding,
1527 ip_forwarding_cmd,
1528 "ip forwarding",
1529 IP_STR
1530 "Turn on IP forwarding")
1531{
1532 int ret;
1533
1534 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001535 if (ret == 0)
1536 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001537
hasso647e4f12003-05-25 11:43:52 +00001538 if (ret == 0)
1539 {
1540 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1541 return CMD_WARNING;
1542 }
1543
1544 return CMD_SUCCESS;
1545}
1546
paul718e3742002-12-13 20:15:29 +00001547DEFUN (no_ip_forwarding,
1548 no_ip_forwarding_cmd,
1549 "no ip forwarding",
1550 NO_STR
1551 IP_STR
1552 "Turn off IP forwarding")
1553{
1554 int ret;
1555
1556 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001557 if (ret != 0)
1558 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001559
paul718e3742002-12-13 20:15:29 +00001560 if (ret != 0)
1561 {
1562 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1563 return CMD_WARNING;
1564 }
1565
1566 return CMD_SUCCESS;
1567}
1568
1569/* This command is for debugging purpose. */
1570DEFUN (show_zebra_client,
1571 show_zebra_client_cmd,
1572 "show zebra client",
1573 SHOW_STR
1574 "Zebra information"
1575 "Client information")
1576{
hasso52dc7ee2004-09-23 19:18:23 +00001577 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001578 struct zserv *client;
1579
paul1eb8ef22005-04-07 07:30:20 +00001580 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1581 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1582
paul718e3742002-12-13 20:15:29 +00001583 return CMD_SUCCESS;
1584}
1585
1586/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001587static int
paul718e3742002-12-13 20:15:29 +00001588config_write_table (struct vty *vty)
1589{
paulb21b19c2003-06-15 01:28:29 +00001590 if (zebrad.rtm_table_default)
1591 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001592 VTY_NEWLINE);
1593 return 0;
1594}
1595
1596/* table node for routing tables. */
1597struct cmd_node table_node =
1598{
1599 TABLE_NODE,
1600 "", /* This node has no interface. */
1601 1
1602};
1603
1604/* Only display ip forwarding is enabled or not. */
1605DEFUN (show_ip_forwarding,
1606 show_ip_forwarding_cmd,
1607 "show ip forwarding",
1608 SHOW_STR
1609 IP_STR
1610 "IP forwarding status\n")
1611{
1612 int ret;
1613
1614 ret = ipforward ();
1615
1616 if (ret == 0)
1617 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1618 else
1619 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1620 return CMD_SUCCESS;
1621}
1622
1623#ifdef HAVE_IPV6
1624/* Only display ipv6 forwarding is enabled or not. */
1625DEFUN (show_ipv6_forwarding,
1626 show_ipv6_forwarding_cmd,
1627 "show ipv6 forwarding",
1628 SHOW_STR
1629 "IPv6 information\n"
1630 "Forwarding status\n")
1631{
1632 int ret;
1633
1634 ret = ipforward_ipv6 ();
1635
1636 switch (ret)
1637 {
1638 case -1:
1639 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1640 break;
1641 case 0:
1642 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1643 break;
1644 case 1:
1645 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1646 break;
1647 default:
1648 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1649 break;
1650 }
1651 return CMD_SUCCESS;
1652}
1653
hasso55906722004-02-11 22:42:16 +00001654DEFUN (ipv6_forwarding,
1655 ipv6_forwarding_cmd,
1656 "ipv6 forwarding",
1657 IPV6_STR
1658 "Turn on IPv6 forwarding")
1659{
1660 int ret;
1661
hasso41d3fc92004-04-06 11:59:00 +00001662 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001663 if (ret == 0)
1664 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001665
hasso41d3fc92004-04-06 11:59:00 +00001666 if (ret == 0)
1667 {
hasso55906722004-02-11 22:42:16 +00001668 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1669 return CMD_WARNING;
1670 }
1671
1672 return CMD_SUCCESS;
1673}
1674
paul718e3742002-12-13 20:15:29 +00001675DEFUN (no_ipv6_forwarding,
1676 no_ipv6_forwarding_cmd,
1677 "no ipv6 forwarding",
1678 NO_STR
hasso55906722004-02-11 22:42:16 +00001679 IPV6_STR
1680 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001681{
1682 int ret;
1683
hasso41d3fc92004-04-06 11:59:00 +00001684 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001685 if (ret != 0)
1686 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001687
paul718e3742002-12-13 20:15:29 +00001688 if (ret != 0)
1689 {
1690 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1691 return CMD_WARNING;
1692 }
1693
1694 return CMD_SUCCESS;
1695}
1696
1697#endif /* HAVE_IPV6 */
1698
1699/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001700static int
paul718e3742002-12-13 20:15:29 +00001701config_write_forwarding (struct vty *vty)
1702{
hasso18a6dce2004-10-03 18:18:34 +00001703 /* FIXME: Find better place for that. */
1704 router_id_write (vty);
1705
paul3e0b3a52004-08-23 18:58:32 +00001706 if (ipforward ())
1707 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001708#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001709 if (ipforward_ipv6 ())
1710 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001711#endif /* HAVE_IPV6 */
1712 vty_out (vty, "!%s", VTY_NEWLINE);
1713 return 0;
1714}
1715
1716/* table node for routing tables. */
1717struct cmd_node forwarding_node =
1718{
1719 FORWARDING_NODE,
1720 "", /* This node has no interface. */
1721 1
1722};
1723
1724
1725/* Initialisation of zebra and installation of commands. */
1726void
paula1ac18c2005-06-28 17:17:12 +00001727zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001728{
1729 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001730 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001731
paul718e3742002-12-13 20:15:29 +00001732 /* Make zebra server socket. */
1733#ifdef HAVE_TCP_ZEBRA
1734 zebra_serv ();
1735#else
1736 zebra_serv_un (ZEBRA_SERV_PATH);
1737#endif /* HAVE_TCP_ZEBRA */
1738
1739 /* Install configuration write function. */
1740 install_node (&table_node, config_write_table);
1741 install_node (&forwarding_node, config_write_forwarding);
1742
1743 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1744 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001745 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001746 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1747 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1748
1749#ifdef HAVE_NETLINK
1750 install_element (VIEW_NODE, &show_table_cmd);
1751 install_element (ENABLE_NODE, &show_table_cmd);
1752 install_element (CONFIG_NODE, &config_table_cmd);
1753#endif /* HAVE_NETLINK */
1754
1755#ifdef HAVE_IPV6
1756 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1757 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001758 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001759 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1760#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001761}