blob: abd99cdc381f0c61508135b37853352f431ed3c0 [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 {
vincentfbf5d032005-09-29 11:25:50 +0000489 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
490 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000491 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
492 stream_putl (s, rib->metric);
493 }
494
495 /* write real message flags value */
496 stream_putc_at (s, messmark, zapi_flags);
497
paulb9df2d22004-05-09 09:09:59 +0000498 /* Write next-hop number */
499 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000500 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000501
paul718e3742002-12-13 20:15:29 +0000502 /* Write packet size. */
503 stream_putw_at (s, 0, stream_get_endp (s));
504
ajs719e9742005-02-28 20:52:15 +0000505 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000506}
507
paul718e3742002-12-13 20:15:29 +0000508#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000509static int
paul718e3742002-12-13 20:15:29 +0000510zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
511{
512 struct stream *s;
513 struct rib *rib;
514 unsigned long nump;
515 u_char num;
516 struct nexthop *nexthop;
517
518 /* Lookup nexthop. */
519 rib = rib_match_ipv6 (addr);
520
521 /* Get output stream. */
522 s = client->obuf;
523 stream_reset (s);
524
525 /* Fill in result. */
526 stream_putw (s, 0);
527 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
528 stream_put (s, &addr, 16);
529
530 if (rib)
531 {
532 stream_putl (s, rib->metric);
533 num = 0;
paul9985f832005-02-09 15:51:56 +0000534 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000535 stream_putc (s, 0);
536 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
537 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
538 {
539 stream_putc (s, nexthop->type);
540 switch (nexthop->type)
541 {
542 case ZEBRA_NEXTHOP_IPV6:
543 stream_put (s, &nexthop->gate.ipv6, 16);
544 break;
545 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
546 case ZEBRA_NEXTHOP_IPV6_IFNAME:
547 stream_put (s, &nexthop->gate.ipv6, 16);
548 stream_putl (s, nexthop->ifindex);
549 break;
550 case ZEBRA_NEXTHOP_IFINDEX:
551 case ZEBRA_NEXTHOP_IFNAME:
552 stream_putl (s, nexthop->ifindex);
553 break;
hassofa2b17e2004-03-04 17:45:00 +0000554 default:
555 /* do nothing */
556 break;
paul718e3742002-12-13 20:15:29 +0000557 }
558 num++;
559 }
560 stream_putc_at (s, nump, num);
561 }
562 else
563 {
564 stream_putl (s, 0);
565 stream_putc (s, 0);
566 }
567
568 stream_putw_at (s, 0, stream_get_endp (s));
569
ajs719e9742005-02-28 20:52:15 +0000570 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000571}
572#endif /* HAVE_IPV6 */
573
paulb9df2d22004-05-09 09:09:59 +0000574static int
paul718e3742002-12-13 20:15:29 +0000575zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
576{
577 struct stream *s;
578 struct rib *rib;
579 unsigned long nump;
580 u_char num;
581 struct nexthop *nexthop;
582
583 /* Lookup nexthop. */
584 rib = rib_match_ipv4 (addr);
585
586 /* Get output stream. */
587 s = client->obuf;
588 stream_reset (s);
589
590 /* Fill in result. */
591 stream_putw (s, 0);
592 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
593 stream_put_in_addr (s, &addr);
594
595 if (rib)
596 {
597 stream_putl (s, rib->metric);
598 num = 0;
paul9985f832005-02-09 15:51:56 +0000599 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000600 stream_putc (s, 0);
601 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
602 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
603 {
604 stream_putc (s, nexthop->type);
605 switch (nexthop->type)
606 {
607 case ZEBRA_NEXTHOP_IPV4:
608 stream_put_in_addr (s, &nexthop->gate.ipv4);
609 break;
610 case ZEBRA_NEXTHOP_IFINDEX:
611 case ZEBRA_NEXTHOP_IFNAME:
612 stream_putl (s, nexthop->ifindex);
613 break;
hassofa2b17e2004-03-04 17:45:00 +0000614 default:
615 /* do nothing */
616 break;
paul718e3742002-12-13 20:15:29 +0000617 }
618 num++;
619 }
620 stream_putc_at (s, nump, num);
621 }
622 else
623 {
624 stream_putl (s, 0);
625 stream_putc (s, 0);
626 }
627
628 stream_putw_at (s, 0, stream_get_endp (s));
629
ajs719e9742005-02-28 20:52:15 +0000630 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000631}
632
paulb9df2d22004-05-09 09:09:59 +0000633static int
paul718e3742002-12-13 20:15:29 +0000634zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
635{
636 struct stream *s;
637 struct rib *rib;
638 unsigned long nump;
639 u_char num;
640 struct nexthop *nexthop;
641
642 /* Lookup nexthop. */
643 rib = rib_lookup_ipv4 (p);
644
645 /* Get output stream. */
646 s = client->obuf;
647 stream_reset (s);
648
649 /* Fill in result. */
650 stream_putw (s, 0);
651 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
652 stream_put_in_addr (s, &p->prefix);
653
654 if (rib)
655 {
656 stream_putl (s, rib->metric);
657 num = 0;
paul9985f832005-02-09 15:51:56 +0000658 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000659 stream_putc (s, 0);
660 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
661 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
662 {
663 stream_putc (s, nexthop->type);
664 switch (nexthop->type)
665 {
666 case ZEBRA_NEXTHOP_IPV4:
667 stream_put_in_addr (s, &nexthop->gate.ipv4);
668 break;
669 case ZEBRA_NEXTHOP_IFINDEX:
670 case ZEBRA_NEXTHOP_IFNAME:
671 stream_putl (s, nexthop->ifindex);
672 break;
hassofa2b17e2004-03-04 17:45:00 +0000673 default:
674 /* do nothing */
675 break;
paul718e3742002-12-13 20:15:29 +0000676 }
677 num++;
678 }
679 stream_putc_at (s, nump, num);
680 }
681 else
682 {
683 stream_putl (s, 0);
684 stream_putc (s, 0);
685 }
686
687 stream_putw_at (s, 0, stream_get_endp (s));
688
ajs719e9742005-02-28 20:52:15 +0000689 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000690}
691
hasso18a6dce2004-10-03 18:18:34 +0000692/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
693int
694zsend_router_id_update (struct zserv *client, struct prefix *p)
695{
696 struct stream *s;
697 int blen;
698
699 /* Check this client need interface information. */
700 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000701 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000702
703 s = client->obuf;
704 stream_reset (s);
705
706 /* Place holder for size. */
707 stream_putw (s, 0);
708
709 /* Message type. */
710 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
711
712 /* Prefix information. */
713 stream_putc (s, p->family);
714 blen = prefix_blen (p);
715 stream_put (s, &p->u.prefix, blen);
716 stream_putc (s, p->prefixlen);
717
718 /* Write packet size. */
719 stream_putw_at (s, 0, stream_get_endp (s));
720
ajs719e9742005-02-28 20:52:15 +0000721 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000722}
723
paul718e3742002-12-13 20:15:29 +0000724/* Register zebra server interface information. Send current all
725 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000726static int
paul718e3742002-12-13 20:15:29 +0000727zread_interface_add (struct zserv *client, u_short length)
728{
paul1eb8ef22005-04-07 07:30:20 +0000729 struct listnode *ifnode, *ifnnode;
730 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000731 struct interface *ifp;
732 struct connected *c;
733
734 /* Interface information is needed. */
735 client->ifinfo = 1;
736
paul1eb8ef22005-04-07 07:30:20 +0000737 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000738 {
paul718e3742002-12-13 20:15:29 +0000739 /* Skip pseudo interface. */
740 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
741 continue;
742
ajs719e9742005-02-28 20:52:15 +0000743 if (zsend_interface_add (client, ifp) < 0)
744 return -1;
paul718e3742002-12-13 20:15:29 +0000745
paul1eb8ef22005-04-07 07:30:20 +0000746 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000747 {
ajs719e9742005-02-28 20:52:15 +0000748 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
749 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
750 ifp, c) < 0))
751 return -1;
paul718e3742002-12-13 20:15:29 +0000752 }
753 }
ajs719e9742005-02-28 20:52:15 +0000754 return 0;
paul718e3742002-12-13 20:15:29 +0000755}
756
757/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000758static int
paul718e3742002-12-13 20:15:29 +0000759zread_interface_delete (struct zserv *client, u_short length)
760{
761 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000762 return 0;
paul718e3742002-12-13 20:15:29 +0000763}
764
765/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000766/*
767 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
768 * add kernel route.
769 */
ajs719e9742005-02-28 20:52:15 +0000770static int
paul718e3742002-12-13 20:15:29 +0000771zread_ipv4_add (struct zserv *client, u_short length)
772{
773 int i;
774 struct rib *rib;
775 struct prefix_ipv4 p;
776 u_char message;
777 struct in_addr nexthop;
778 u_char nexthop_num;
779 u_char nexthop_type;
780 struct stream *s;
781 unsigned int ifindex;
782 u_char ifname_len;
783
784 /* Get input stream. */
785 s = client->ibuf;
786
787 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000788 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
789
paul718e3742002-12-13 20:15:29 +0000790 /* Type, flags, message. */
791 rib->type = stream_getc (s);
792 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000793 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000794 rib->uptime = time (NULL);
795
796 /* IPv4 prefix. */
797 memset (&p, 0, sizeof (struct prefix_ipv4));
798 p.family = AF_INET;
799 p.prefixlen = stream_getc (s);
800 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
801
802 /* Nexthop parse. */
803 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
804 {
805 nexthop_num = stream_getc (s);
806
807 for (i = 0; i < nexthop_num; i++)
808 {
809 nexthop_type = stream_getc (s);
810
811 switch (nexthop_type)
812 {
813 case ZEBRA_NEXTHOP_IFINDEX:
814 ifindex = stream_getl (s);
815 nexthop_ifindex_add (rib, ifindex);
816 break;
817 case ZEBRA_NEXTHOP_IFNAME:
818 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000819 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000820 break;
821 case ZEBRA_NEXTHOP_IPV4:
822 nexthop.s_addr = stream_get_ipv4 (s);
823 nexthop_ipv4_add (rib, &nexthop);
824 break;
825 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000826 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000827 break;
paul595db7f2003-05-25 21:35:06 +0000828 case ZEBRA_NEXTHOP_BLACKHOLE:
829 nexthop_blackhole_add (rib);
830 break;
paul718e3742002-12-13 20:15:29 +0000831 }
832 }
833 }
834
835 /* Distance. */
836 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
837 rib->distance = stream_getc (s);
838
839 /* Metric. */
840 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
841 rib->metric = stream_getl (s);
842
843 rib_add_ipv4_multipath (&p, rib);
ajs719e9742005-02-28 20:52:15 +0000844 return 0;
paul718e3742002-12-13 20:15:29 +0000845}
846
847/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000848static int
paul718e3742002-12-13 20:15:29 +0000849zread_ipv4_delete (struct zserv *client, u_short length)
850{
851 int i;
852 struct stream *s;
853 struct zapi_ipv4 api;
854 struct in_addr nexthop;
855 unsigned long ifindex;
856 struct prefix_ipv4 p;
857 u_char nexthop_num;
858 u_char nexthop_type;
859 u_char ifname_len;
860
861 s = client->ibuf;
862 ifindex = 0;
863 nexthop.s_addr = 0;
864
865 /* Type, flags, message. */
866 api.type = stream_getc (s);
867 api.flags = stream_getc (s);
868 api.message = stream_getc (s);
869
870 /* IPv4 prefix. */
871 memset (&p, 0, sizeof (struct prefix_ipv4));
872 p.family = AF_INET;
873 p.prefixlen = stream_getc (s);
874 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
875
876 /* Nexthop, ifindex, distance, metric. */
877 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
878 {
879 nexthop_num = stream_getc (s);
880
881 for (i = 0; i < nexthop_num; i++)
882 {
883 nexthop_type = stream_getc (s);
884
885 switch (nexthop_type)
886 {
887 case ZEBRA_NEXTHOP_IFINDEX:
888 ifindex = stream_getl (s);
889 break;
890 case ZEBRA_NEXTHOP_IFNAME:
891 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000892 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000893 break;
894 case ZEBRA_NEXTHOP_IPV4:
895 nexthop.s_addr = stream_get_ipv4 (s);
896 break;
897 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000898 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000899 break;
900 }
901 }
902 }
903
904 /* Distance. */
905 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
906 api.distance = stream_getc (s);
907 else
908 api.distance = 0;
909
910 /* Metric. */
911 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
912 api.metric = stream_getl (s);
913 else
914 api.metric = 0;
915
916 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
917 client->rtm_table);
ajs719e9742005-02-28 20:52:15 +0000918 return 0;
paul718e3742002-12-13 20:15:29 +0000919}
920
921/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000922static int
paul718e3742002-12-13 20:15:29 +0000923zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
924{
925 struct in_addr addr;
926
927 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000928 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000929}
930
931/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000932static int
paul718e3742002-12-13 20:15:29 +0000933zread_ipv4_import_lookup (struct zserv *client, u_short length)
934{
935 struct prefix_ipv4 p;
936
937 p.family = AF_INET;
938 p.prefixlen = stream_getc (client->ibuf);
939 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
940
ajs719e9742005-02-28 20:52:15 +0000941 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000942}
943
944#ifdef HAVE_IPV6
945/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000946static int
paul718e3742002-12-13 20:15:29 +0000947zread_ipv6_add (struct zserv *client, u_short length)
948{
949 int i;
950 struct stream *s;
951 struct zapi_ipv6 api;
952 struct in6_addr nexthop;
953 unsigned long ifindex;
954 struct prefix_ipv6 p;
955
956 s = client->ibuf;
957 ifindex = 0;
958 memset (&nexthop, 0, sizeof (struct in6_addr));
959
960 /* Type, flags, message. */
961 api.type = stream_getc (s);
962 api.flags = stream_getc (s);
963 api.message = stream_getc (s);
964
965 /* IPv4 prefix. */
966 memset (&p, 0, sizeof (struct prefix_ipv6));
967 p.family = AF_INET6;
968 p.prefixlen = stream_getc (s);
969 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
970
971 /* Nexthop, ifindex, distance, metric. */
972 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
973 {
974 u_char nexthop_type;
975
976 api.nexthop_num = stream_getc (s);
977 for (i = 0; i < api.nexthop_num; i++)
978 {
979 nexthop_type = stream_getc (s);
980
981 switch (nexthop_type)
982 {
983 case ZEBRA_NEXTHOP_IPV6:
984 stream_get (&nexthop, s, 16);
985 break;
986 case ZEBRA_NEXTHOP_IFINDEX:
987 ifindex = stream_getl (s);
988 break;
989 }
990 }
991 }
992
993 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
994 api.distance = stream_getc (s);
995 else
996 api.distance = 0;
997
998 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
999 api.metric = stream_getl (s);
1000 else
1001 api.metric = 0;
1002
1003 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
hassobe61c4e2005-08-27 06:05:47 +00001004 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0, api.metric,
1005 api.distance);
paul718e3742002-12-13 20:15:29 +00001006 else
hassobe61c4e2005-08-27 06:05:47 +00001007 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0, api.metric,
1008 api.distance);
ajs719e9742005-02-28 20:52:15 +00001009 return 0;
paul718e3742002-12-13 20:15:29 +00001010}
1011
1012/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001013static int
paul718e3742002-12-13 20:15:29 +00001014zread_ipv6_delete (struct zserv *client, u_short length)
1015{
1016 int i;
1017 struct stream *s;
1018 struct zapi_ipv6 api;
1019 struct in6_addr nexthop;
1020 unsigned long ifindex;
1021 struct prefix_ipv6 p;
1022
1023 s = client->ibuf;
1024 ifindex = 0;
1025 memset (&nexthop, 0, sizeof (struct in6_addr));
1026
1027 /* Type, flags, message. */
1028 api.type = stream_getc (s);
1029 api.flags = stream_getc (s);
1030 api.message = stream_getc (s);
1031
1032 /* IPv4 prefix. */
1033 memset (&p, 0, sizeof (struct prefix_ipv6));
1034 p.family = AF_INET6;
1035 p.prefixlen = stream_getc (s);
1036 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1037
1038 /* Nexthop, ifindex, distance, metric. */
1039 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1040 {
1041 u_char nexthop_type;
1042
1043 api.nexthop_num = stream_getc (s);
1044 for (i = 0; i < api.nexthop_num; i++)
1045 {
1046 nexthop_type = stream_getc (s);
1047
1048 switch (nexthop_type)
1049 {
1050 case ZEBRA_NEXTHOP_IPV6:
1051 stream_get (&nexthop, s, 16);
1052 break;
1053 case ZEBRA_NEXTHOP_IFINDEX:
1054 ifindex = stream_getl (s);
1055 break;
1056 }
1057 }
1058 }
1059
1060 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1061 api.distance = stream_getc (s);
1062 else
1063 api.distance = 0;
1064 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1065 api.metric = stream_getl (s);
1066 else
1067 api.metric = 0;
1068
1069 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1070 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1071 else
1072 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001073 return 0;
paul718e3742002-12-13 20:15:29 +00001074}
1075
ajs719e9742005-02-28 20:52:15 +00001076static int
paul718e3742002-12-13 20:15:29 +00001077zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1078{
1079 struct in6_addr addr;
1080 char buf[BUFSIZ];
1081
1082 stream_get (&addr, client->ibuf, 16);
1083 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1084
ajs719e9742005-02-28 20:52:15 +00001085 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001086}
1087#endif /* HAVE_IPV6 */
1088
hasso18a6dce2004-10-03 18:18:34 +00001089/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001090static int
hasso18a6dce2004-10-03 18:18:34 +00001091zread_router_id_add (struct zserv *client, u_short length)
1092{
1093 struct prefix p;
1094
1095 /* Router-id information is needed. */
1096 client->ridinfo = 1;
1097
1098 router_id_get (&p);
1099
ajs719e9742005-02-28 20:52:15 +00001100 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001101}
1102
1103/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001104static int
hasso18a6dce2004-10-03 18:18:34 +00001105zread_router_id_delete (struct zserv *client, u_short length)
1106{
1107 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001108 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001109}
1110
paul718e3742002-12-13 20:15:29 +00001111/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001112static void
paul718e3742002-12-13 20:15:29 +00001113zebra_client_close (struct zserv *client)
1114{
1115 /* Close file descriptor. */
1116 if (client->sock)
1117 {
1118 close (client->sock);
1119 client->sock = -1;
1120 }
1121
1122 /* Free stream buffers. */
1123 if (client->ibuf)
1124 stream_free (client->ibuf);
1125 if (client->obuf)
1126 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001127 if (client->wb)
1128 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001129
1130 /* Release threads. */
1131 if (client->t_read)
1132 thread_cancel (client->t_read);
1133 if (client->t_write)
1134 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001135 if (client->t_suicide)
1136 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001137
1138 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001139 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001140 XFREE (0, client);
1141}
1142
1143/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001144static void
paul718e3742002-12-13 20:15:29 +00001145zebra_client_create (int sock)
1146{
1147 struct zserv *client;
1148
1149 client = XCALLOC (0, sizeof (struct zserv));
1150
1151 /* Make client input/output buffer. */
1152 client->sock = sock;
1153 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1154 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001155 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001156
1157 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001158 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001159
1160 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001161 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001162
1163 /* Make new read thread. */
1164 zebra_event (ZEBRA_READ, sock, client);
1165}
1166
1167/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001168static int
paul718e3742002-12-13 20:15:29 +00001169zebra_client_read (struct thread *thread)
1170{
1171 int sock;
1172 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001173 size_t already;
paul718e3742002-12-13 20:15:29 +00001174 u_short length;
1175 u_char command;
1176
1177 /* Get thread data. Reset reading thread because I'm running. */
1178 sock = THREAD_FD (thread);
1179 client = THREAD_ARG (thread);
1180 client->t_read = NULL;
1181
ajs719e9742005-02-28 20:52:15 +00001182 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001183 {
ajs719e9742005-02-28 20:52:15 +00001184 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001185 return -1;
1186 }
ajs719e9742005-02-28 20:52:15 +00001187
1188 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001189 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001190 {
ajs57a14772005-04-10 15:01:56 +00001191 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001192 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001193 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001194 (nbyte == -1))
1195 {
1196 if (IS_ZEBRA_DEBUG_EVENT)
1197 zlog_debug ("connection closed socket [%d]", sock);
1198 zebra_client_close (client);
1199 return -1;
1200 }
ajs57a14772005-04-10 15:01:56 +00001201 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001202 {
1203 /* Try again later. */
1204 zebra_event (ZEBRA_READ, sock, client);
1205 return 0;
1206 }
ajs57a14772005-04-10 15:01:56 +00001207 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001208 }
1209
1210 /* Reset to read from the beginning of the incoming packet. */
1211 stream_set_getp(client->ibuf, 0);
1212
paul718e3742002-12-13 20:15:29 +00001213 length = stream_getw (client->ibuf);
1214 command = stream_getc (client->ibuf);
1215
ajs719e9742005-02-28 20:52:15 +00001216 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001217 {
ajs57a14772005-04-10 15:01:56 +00001218 zlog_warn("%s: socket %d message length %u is less than header size %d",
1219 __func__, sock, length, ZEBRA_HEADER_SIZE);
1220 zebra_client_close (client);
1221 return -1;
1222 }
1223 if (length > STREAM_SIZE(client->ibuf))
1224 {
1225 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1226 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001227 zebra_client_close (client);
1228 return -1;
1229 }
1230
paul718e3742002-12-13 20:15:29 +00001231 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001232 if (already < length)
paul718e3742002-12-13 20:15:29 +00001233 {
ajs57a14772005-04-10 15:01:56 +00001234 ssize_t nbyte;
1235 if (((nbyte = stream_read_try (client->ibuf, sock,
1236 length-already)) == 0) ||
1237 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001238 {
1239 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001240 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001241 zebra_client_close (client);
1242 return -1;
1243 }
ajs57a14772005-04-10 15:01:56 +00001244 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001245 {
1246 /* Try again later. */
1247 zebra_event (ZEBRA_READ, sock, client);
1248 return 0;
1249 }
paul718e3742002-12-13 20:15:29 +00001250 }
1251
ajs719e9742005-02-28 20:52:15 +00001252 length -= ZEBRA_HEADER_SIZE;
1253
paul718e3742002-12-13 20:15:29 +00001254 /* Debug packet information. */
1255 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001256 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001257
1258 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001259 zlog_debug ("zebra message received [%s] %d",
paul718e3742002-12-13 20:15:29 +00001260 zebra_command_str[command], length);
1261
1262 switch (command)
1263 {
hasso18a6dce2004-10-03 18:18:34 +00001264 case ZEBRA_ROUTER_ID_ADD:
1265 zread_router_id_add (client, length);
1266 break;
1267 case ZEBRA_ROUTER_ID_DELETE:
1268 zread_router_id_delete (client, length);
1269 break;
paul718e3742002-12-13 20:15:29 +00001270 case ZEBRA_INTERFACE_ADD:
1271 zread_interface_add (client, length);
1272 break;
1273 case ZEBRA_INTERFACE_DELETE:
1274 zread_interface_delete (client, length);
1275 break;
1276 case ZEBRA_IPV4_ROUTE_ADD:
1277 zread_ipv4_add (client, length);
1278 break;
1279 case ZEBRA_IPV4_ROUTE_DELETE:
1280 zread_ipv4_delete (client, length);
1281 break;
1282#ifdef HAVE_IPV6
1283 case ZEBRA_IPV6_ROUTE_ADD:
1284 zread_ipv6_add (client, length);
1285 break;
1286 case ZEBRA_IPV6_ROUTE_DELETE:
1287 zread_ipv6_delete (client, length);
1288 break;
1289#endif /* HAVE_IPV6 */
1290 case ZEBRA_REDISTRIBUTE_ADD:
1291 zebra_redistribute_add (command, client, length);
1292 break;
1293 case ZEBRA_REDISTRIBUTE_DELETE:
1294 zebra_redistribute_delete (command, client, length);
1295 break;
1296 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1297 zebra_redistribute_default_add (command, client, length);
1298 break;
1299 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1300 zebra_redistribute_default_delete (command, client, length);
1301 break;
1302 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1303 zread_ipv4_nexthop_lookup (client, length);
1304 break;
1305#ifdef HAVE_IPV6
1306 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1307 zread_ipv6_nexthop_lookup (client, length);
1308 break;
1309#endif /* HAVE_IPV6 */
1310 case ZEBRA_IPV4_IMPORT_LOOKUP:
1311 zread_ipv4_import_lookup (client, length);
1312 break;
1313 default:
1314 zlog_info ("Zebra received unknown command %d", command);
1315 break;
1316 }
1317
ajs719e9742005-02-28 20:52:15 +00001318 if (client->t_suicide)
1319 {
1320 /* No need to wait for thread callback, just kill immediately. */
1321 zebra_client_close(client);
1322 return -1;
1323 }
1324
paul718e3742002-12-13 20:15:29 +00001325 stream_reset (client->ibuf);
1326 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001327 return 0;
1328}
1329
paul718e3742002-12-13 20:15:29 +00001330
1331/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001332static int
paul718e3742002-12-13 20:15:29 +00001333zebra_accept (struct thread *thread)
1334{
1335 int accept_sock;
1336 int client_sock;
1337 struct sockaddr_in client;
1338 socklen_t len;
1339
1340 accept_sock = THREAD_FD (thread);
1341
ajs719e9742005-02-28 20:52:15 +00001342 /* Reregister myself. */
1343 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1344
paul718e3742002-12-13 20:15:29 +00001345 len = sizeof (struct sockaddr_in);
1346 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1347
1348 if (client_sock < 0)
1349 {
ajs6099b3b2004-11-20 02:06:59 +00001350 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001351 return -1;
1352 }
1353
paulccf35572003-03-01 11:42:20 +00001354 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001355 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001356
paul718e3742002-12-13 20:15:29 +00001357 /* Create new zebra client. */
1358 zebra_client_create (client_sock);
1359
paul718e3742002-12-13 20:15:29 +00001360 return 0;
1361}
1362
paulb9df2d22004-05-09 09:09:59 +00001363#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001364/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001365static void
paul718e3742002-12-13 20:15:29 +00001366zebra_serv ()
1367{
1368 int ret;
1369 int accept_sock;
1370 struct sockaddr_in addr;
1371
1372 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1373
1374 if (accept_sock < 0)
1375 {
paul3d1dc852005-04-05 00:45:23 +00001376 zlog_warn ("Can't create zserv stream socket: %s",
1377 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001378 zlog_warn ("zebra can't provice full functionality due to above error");
1379 return;
1380 }
1381
1382 memset (&addr, 0, sizeof (struct sockaddr_in));
1383 addr.sin_family = AF_INET;
1384 addr.sin_port = htons (ZEBRA_PORT);
1385#ifdef HAVE_SIN_LEN
1386 addr.sin_len = sizeof (struct sockaddr_in);
1387#endif /* HAVE_SIN_LEN */
1388 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1389
1390 sockopt_reuseaddr (accept_sock);
1391 sockopt_reuseport (accept_sock);
1392
pauledd7c242003-06-04 13:59:38 +00001393 if ( zserv_privs.change(ZPRIVS_RAISE) )
1394 zlog (NULL, LOG_ERR, "Can't raise privileges");
1395
paul718e3742002-12-13 20:15:29 +00001396 ret = bind (accept_sock, (struct sockaddr *)&addr,
1397 sizeof (struct sockaddr_in));
1398 if (ret < 0)
1399 {
paul3d1dc852005-04-05 00:45:23 +00001400 zlog_warn ("Can't bind to stream socket: %s",
1401 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001402 zlog_warn ("zebra can't provice full functionality due to above error");
1403 close (accept_sock); /* Avoid sd leak. */
1404 return;
1405 }
pauledd7c242003-06-04 13:59:38 +00001406
1407 if ( zserv_privs.change(ZPRIVS_LOWER) )
1408 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001409
1410 ret = listen (accept_sock, 1);
1411 if (ret < 0)
1412 {
paul3d1dc852005-04-05 00:45:23 +00001413 zlog_warn ("Can't listen to stream socket: %s",
1414 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001415 zlog_warn ("zebra can't provice full functionality due to above error");
1416 close (accept_sock); /* Avoid sd leak. */
1417 return;
1418 }
1419
1420 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1421}
paulb9df2d22004-05-09 09:09:59 +00001422#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001423
1424/* For sockaddr_un. */
1425#include <sys/un.h>
1426
1427/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001428static void
hassofce954f2004-10-07 20:29:24 +00001429zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001430{
1431 int ret;
1432 int sock, len;
1433 struct sockaddr_un serv;
1434 mode_t old_mask;
1435
1436 /* First of all, unlink existing socket */
1437 unlink (path);
1438
1439 /* Set umask */
1440 old_mask = umask (0077);
1441
1442 /* Make UNIX domain socket. */
1443 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1444 if (sock < 0)
1445 {
paul3d1dc852005-04-05 00:45:23 +00001446 zlog_warn ("Can't create zserv unix socket: %s",
1447 safe_strerror (errno));
1448 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001449 return;
1450 }
1451
1452 /* Make server socket. */
1453 memset (&serv, 0, sizeof (struct sockaddr_un));
1454 serv.sun_family = AF_UNIX;
1455 strncpy (serv.sun_path, path, strlen (path));
1456#ifdef HAVE_SUN_LEN
1457 len = serv.sun_len = SUN_LEN(&serv);
1458#else
1459 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1460#endif /* HAVE_SUN_LEN */
1461
1462 ret = bind (sock, (struct sockaddr *) &serv, len);
1463 if (ret < 0)
1464 {
paul3d1dc852005-04-05 00:45:23 +00001465 zlog_warn ("Can't bind to unix socket %s: %s",
1466 path, safe_strerror (errno));
1467 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001468 close (sock);
1469 return;
1470 }
1471
1472 ret = listen (sock, 5);
1473 if (ret < 0)
1474 {
paul3d1dc852005-04-05 00:45:23 +00001475 zlog_warn ("Can't listen to unix socket %s: %s",
1476 path, safe_strerror (errno));
1477 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001478 close (sock);
1479 return;
1480 }
1481
1482 umask (old_mask);
1483
1484 zebra_event (ZEBRA_SERV, sock, NULL);
1485}
1486
paul718e3742002-12-13 20:15:29 +00001487
paulb9df2d22004-05-09 09:09:59 +00001488static void
paul718e3742002-12-13 20:15:29 +00001489zebra_event (enum event event, int sock, struct zserv *client)
1490{
1491 switch (event)
1492 {
1493 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001494 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001495 break;
1496 case ZEBRA_READ:
1497 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001498 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001499 break;
1500 case ZEBRA_WRITE:
1501 /**/
1502 break;
1503 }
1504}
1505
1506/* Display default rtm_table for all clients. */
1507DEFUN (show_table,
1508 show_table_cmd,
1509 "show table",
1510 SHOW_STR
1511 "default routing table to use for all clients\n")
1512{
paulb21b19c2003-06-15 01:28:29 +00001513 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001514 VTY_NEWLINE);
1515 return CMD_SUCCESS;
1516}
1517
1518DEFUN (config_table,
1519 config_table_cmd,
1520 "table TABLENO",
1521 "Configure target kernel routing table\n"
1522 "TABLE integer\n")
1523{
paulb21b19c2003-06-15 01:28:29 +00001524 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001525 return CMD_SUCCESS;
1526}
1527
hasso647e4f12003-05-25 11:43:52 +00001528DEFUN (ip_forwarding,
1529 ip_forwarding_cmd,
1530 "ip forwarding",
1531 IP_STR
1532 "Turn on IP forwarding")
1533{
1534 int ret;
1535
1536 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001537 if (ret == 0)
1538 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001539
hasso647e4f12003-05-25 11:43:52 +00001540 if (ret == 0)
1541 {
1542 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1543 return CMD_WARNING;
1544 }
1545
1546 return CMD_SUCCESS;
1547}
1548
paul718e3742002-12-13 20:15:29 +00001549DEFUN (no_ip_forwarding,
1550 no_ip_forwarding_cmd,
1551 "no ip forwarding",
1552 NO_STR
1553 IP_STR
1554 "Turn off IP forwarding")
1555{
1556 int ret;
1557
1558 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001559 if (ret != 0)
1560 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001561
paul718e3742002-12-13 20:15:29 +00001562 if (ret != 0)
1563 {
1564 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1565 return CMD_WARNING;
1566 }
1567
1568 return CMD_SUCCESS;
1569}
1570
1571/* This command is for debugging purpose. */
1572DEFUN (show_zebra_client,
1573 show_zebra_client_cmd,
1574 "show zebra client",
1575 SHOW_STR
1576 "Zebra information"
1577 "Client information")
1578{
hasso52dc7ee2004-09-23 19:18:23 +00001579 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001580 struct zserv *client;
1581
paul1eb8ef22005-04-07 07:30:20 +00001582 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1583 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1584
paul718e3742002-12-13 20:15:29 +00001585 return CMD_SUCCESS;
1586}
1587
1588/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001589static int
paul718e3742002-12-13 20:15:29 +00001590config_write_table (struct vty *vty)
1591{
paulb21b19c2003-06-15 01:28:29 +00001592 if (zebrad.rtm_table_default)
1593 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001594 VTY_NEWLINE);
1595 return 0;
1596}
1597
1598/* table node for routing tables. */
1599struct cmd_node table_node =
1600{
1601 TABLE_NODE,
1602 "", /* This node has no interface. */
1603 1
1604};
1605
1606/* Only display ip forwarding is enabled or not. */
1607DEFUN (show_ip_forwarding,
1608 show_ip_forwarding_cmd,
1609 "show ip forwarding",
1610 SHOW_STR
1611 IP_STR
1612 "IP forwarding status\n")
1613{
1614 int ret;
1615
1616 ret = ipforward ();
1617
1618 if (ret == 0)
1619 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1620 else
1621 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1622 return CMD_SUCCESS;
1623}
1624
1625#ifdef HAVE_IPV6
1626/* Only display ipv6 forwarding is enabled or not. */
1627DEFUN (show_ipv6_forwarding,
1628 show_ipv6_forwarding_cmd,
1629 "show ipv6 forwarding",
1630 SHOW_STR
1631 "IPv6 information\n"
1632 "Forwarding status\n")
1633{
1634 int ret;
1635
1636 ret = ipforward_ipv6 ();
1637
1638 switch (ret)
1639 {
1640 case -1:
1641 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1642 break;
1643 case 0:
1644 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1645 break;
1646 case 1:
1647 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1648 break;
1649 default:
1650 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1651 break;
1652 }
1653 return CMD_SUCCESS;
1654}
1655
hasso55906722004-02-11 22:42:16 +00001656DEFUN (ipv6_forwarding,
1657 ipv6_forwarding_cmd,
1658 "ipv6 forwarding",
1659 IPV6_STR
1660 "Turn on IPv6 forwarding")
1661{
1662 int ret;
1663
hasso41d3fc92004-04-06 11:59:00 +00001664 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001665 if (ret == 0)
1666 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001667
hasso41d3fc92004-04-06 11:59:00 +00001668 if (ret == 0)
1669 {
hasso55906722004-02-11 22:42:16 +00001670 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1671 return CMD_WARNING;
1672 }
1673
1674 return CMD_SUCCESS;
1675}
1676
paul718e3742002-12-13 20:15:29 +00001677DEFUN (no_ipv6_forwarding,
1678 no_ipv6_forwarding_cmd,
1679 "no ipv6 forwarding",
1680 NO_STR
hasso55906722004-02-11 22:42:16 +00001681 IPV6_STR
1682 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001683{
1684 int ret;
1685
hasso41d3fc92004-04-06 11:59:00 +00001686 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001687 if (ret != 0)
1688 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001689
paul718e3742002-12-13 20:15:29 +00001690 if (ret != 0)
1691 {
1692 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1693 return CMD_WARNING;
1694 }
1695
1696 return CMD_SUCCESS;
1697}
1698
1699#endif /* HAVE_IPV6 */
1700
1701/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001702static int
paul718e3742002-12-13 20:15:29 +00001703config_write_forwarding (struct vty *vty)
1704{
hasso18a6dce2004-10-03 18:18:34 +00001705 /* FIXME: Find better place for that. */
1706 router_id_write (vty);
1707
paul3e0b3a52004-08-23 18:58:32 +00001708 if (ipforward ())
1709 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001710#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001711 if (ipforward_ipv6 ())
1712 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001713#endif /* HAVE_IPV6 */
1714 vty_out (vty, "!%s", VTY_NEWLINE);
1715 return 0;
1716}
1717
1718/* table node for routing tables. */
1719struct cmd_node forwarding_node =
1720{
1721 FORWARDING_NODE,
1722 "", /* This node has no interface. */
1723 1
1724};
1725
1726
1727/* Initialisation of zebra and installation of commands. */
1728void
paula1ac18c2005-06-28 17:17:12 +00001729zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001730{
1731 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001732 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001733
paul718e3742002-12-13 20:15:29 +00001734 /* Make zebra server socket. */
1735#ifdef HAVE_TCP_ZEBRA
1736 zebra_serv ();
1737#else
1738 zebra_serv_un (ZEBRA_SERV_PATH);
1739#endif /* HAVE_TCP_ZEBRA */
1740
1741 /* Install configuration write function. */
1742 install_node (&table_node, config_write_table);
1743 install_node (&forwarding_node, config_write_forwarding);
1744
1745 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1746 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001747 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001748 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1749 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1750
1751#ifdef HAVE_NETLINK
1752 install_element (VIEW_NODE, &show_table_cmd);
1753 install_element (ENABLE_NODE, &show_table_cmd);
1754 install_element (CONFIG_NODE, &config_table_cmd);
1755#endif /* HAVE_NETLINK */
1756
1757#ifdef HAVE_IPV6
1758 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1759 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001760 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001761 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1762#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001763}