blob: e90fe0194a101f8da509b8f834bd5fc135e96af5 [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. */
paulb9df2d22004-05-09 09:09:59 +0000206/*
207 * This function is only called when support for
208 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
209 * is available. It is not called on Solaris.
210 */
211#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
paul718e3742002-12-13 20:15:29 +0000212int
213zsend_interface_delete (struct zserv *client, struct interface *ifp)
214{
215 struct stream *s;
216
217 /* Check this client need interface information. */
218 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000219 return 0;
paul718e3742002-12-13 20:15:29 +0000220
221 s = client->obuf;
222 stream_reset (s);
223
224 /* Packet length placeholder. */
225 stream_putw (s, 0);
226
227 /* Interface information. */
228 stream_putc (s, ZEBRA_INTERFACE_DELETE);
229 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
230 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000231 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000232 stream_putl (s, ifp->flags);
233 stream_putl (s, ifp->metric);
234 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000235 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000236 stream_putl (s, ifp->bandwidth);
237
238 /* Write packet length. */
239 stream_putw_at (s, 0, stream_get_endp (s));
240
ajs719e9742005-02-28 20:52:15 +0000241 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000242}
paulb9df2d22004-05-09 09:09:59 +0000243#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */
paul718e3742002-12-13 20:15:29 +0000244
paulb9df2d22004-05-09 09:09:59 +0000245/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
246 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
247 *
248 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
249 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
250 * from the client, after the ZEBRA_INTERFACE_ADD has been
251 * sent from zebra to the client
252 * - redistribute new address info to all clients in the following situations
253 * - at startup, when zebra figures out the available interfaces
254 * - when an interface is added (where support for
255 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
256 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
257 * received)
258 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
259 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
260 * - when an RTM_NEWADDR message is received from the kernel,
261 *
262 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
263 *
264 * zsend_interface_address(DELETE)
265 * ^
266 * |
267 * zebra_interface_address_delete_update
268 * ^ ^ ^
269 * | | if_delete_update (not called on
270 * | | Solaris)
271 * ip_address_uninstall connected_delete_ipv4
272 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
273 * ^ ^
274 * | |
275 * | RTM_NEWADDR on routing/netlink socket
276 * |
277 * vty commands:
278 * "no ip address A.B.C.D/M [label LINE]"
279 * "no ip address A.B.C.D/M secondary"
280 * ["no ipv6 address X:X::X:X/M"]
281 *
282 */
paul718e3742002-12-13 20:15:29 +0000283int
paulb9df2d22004-05-09 09:09:59 +0000284zsend_interface_address (int cmd, struct zserv *client,
285 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000286{
287 int blen;
288 struct stream *s;
289 struct prefix *p;
290
291 /* Check this client need interface information. */
292 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000293 return 0;
paul718e3742002-12-13 20:15:29 +0000294
295 s = client->obuf;
296 stream_reset (s);
297
298 /* Place holder for size. */
299 stream_putw (s, 0);
300
paulb9df2d22004-05-09 09:09:59 +0000301 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000302 stream_putl (s, ifp->ifindex);
303
304 /* Interface address flag. */
305 stream_putc (s, ifc->flags);
306
307 /* Prefix information. */
308 p = ifc->address;
309 stream_putc (s, p->family);
310 blen = prefix_blen (p);
311 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000312
313 /*
314 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
315 * but zebra_interface_address_delete_read() in the gnu version
316 * expects to find it
317 */
paul718e3742002-12-13 20:15:29 +0000318 stream_putc (s, p->prefixlen);
319
320 /* Destination. */
321 p = ifc->destination;
322 if (p)
323 stream_put (s, &p->u.prefix, blen);
324 else
325 stream_put (s, NULL, blen);
326
327 /* Write packet size. */
328 stream_putw_at (s, 0, stream_get_endp (s));
329
ajs719e9742005-02-28 20:52:15 +0000330 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000331}
332
paulb9df2d22004-05-09 09:09:59 +0000333/*
334 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
335 * ZEBRA_INTERFACE_DOWN.
336 *
337 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
338 * the clients in one of 2 situations:
339 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
340 * - a vty command modifying the bandwidth of an interface is received.
341 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
342 */
paul718e3742002-12-13 20:15:29 +0000343int
paulb9df2d22004-05-09 09:09:59 +0000344zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000345{
346 struct stream *s;
347
348 /* Check this client need interface information. */
349 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000350 return 0;
paul718e3742002-12-13 20:15:29 +0000351
352 s = client->obuf;
353 stream_reset (s);
354
355 /* Place holder for size. */
356 stream_putw (s, 0);
357
358 /* Zebra command. */
paulb9df2d22004-05-09 09:09:59 +0000359 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000360
361 /* Interface information. */
362 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
363 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000364 stream_putc (s, ifp->status);
paul718e3742002-12-13 20:15:29 +0000365 stream_putl (s, ifp->flags);
366 stream_putl (s, ifp->metric);
367 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000368 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000369 stream_putl (s, ifp->bandwidth);
370
371 /* Write packet size. */
372 stream_putw_at (s, 0, stream_get_endp (s));
373
ajs719e9742005-02-28 20:52:15 +0000374 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000375}
376
paulb9df2d22004-05-09 09:09:59 +0000377/*
378 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
379 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
380 * situations:
381 * - when the client starts up, and requests default information
382 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
383 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
384 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
385 * - when the zebra server redistributes routes after it updates its rib
386 *
387 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
388 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
389 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
390 * - deleted from zebra's rib, and this info
391 * has to be redistributed to the clients
392 *
393 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
394 * zebra server when the client wants to tell the zebra server to add a
395 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
396 * same message being sent back and forth, this function and
397 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
398 * duplication.
399 */
paul718e3742002-12-13 20:15:29 +0000400int
paulb9df2d22004-05-09 09:09:59 +0000401zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
402 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000403{
404 int psize;
405 struct stream *s;
406 struct nexthop *nexthop;
paulb9df2d22004-05-09 09:09:59 +0000407 unsigned long nhnummark = 0;
408 int nhnum = 0;
409 u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX;
410
paul718e3742002-12-13 20:15:29 +0000411 s = client->obuf;
412 stream_reset (s);
413
414 /* Place holder for size. */
415 stream_putw (s, 0);
416
417 /* Put command, type and nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000418 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000419 stream_putc (s, rib->type);
420 stream_putc (s, rib->flags);
paulb9df2d22004-05-09 09:09:59 +0000421
422 /*
423 * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to
424 * send empty nexthop?
425 */
426 if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD)
427 zapi_flags |= ZAPI_MESSAGE_METRIC;
428
429 stream_putc (s, zapi_flags);
paul718e3742002-12-13 20:15:29 +0000430
431 /* Prefix. */
432 psize = PSIZE (p->prefixlen);
433 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000434 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000435
paulb9df2d22004-05-09 09:09:59 +0000436 /*
437 * XXX The message format sent by zebra below does not match the format
438 * of the corresponding message expected by the zebra server
439 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
440 * (is there a bug on the client side if more than one segment is sent?)
441 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
442 * is hard-coded.
443 */
paul718e3742002-12-13 20:15:29 +0000444 /* Nexthop */
445 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
446 {
447 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000448 {
paul9985f832005-02-09 15:51:56 +0000449 nhnummark = stream_get_endp (s);
paulb9df2d22004-05-09 09:09:59 +0000450 stream_putc (s, 1); /* placeholder */
451 nhnum++;
paul718e3742002-12-13 20:15:29 +0000452
paulb9df2d22004-05-09 09:09:59 +0000453 switch(nexthop->type)
454 {
455 case NEXTHOP_TYPE_IPV4:
456 case NEXTHOP_TYPE_IPV4_IFINDEX:
457 stream_put_in_addr (s, &nexthop->gate.ipv4);
458 break;
459#ifdef HAVE_IPV6
460 case NEXTHOP_TYPE_IPV6:
461 case NEXTHOP_TYPE_IPV6_IFINDEX:
462 case NEXTHOP_TYPE_IPV6_IFNAME:
463 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
464 break;
465#endif
466 default:
467 if (cmd == ZEBRA_IPV4_ROUTE_ADD
468 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
469 {
470 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000471 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000472 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
473 }
474 else
475 {
476 struct in6_addr empty;
477 memset (&empty, 0, sizeof (struct in6_addr));
478 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
479 }
480 }
paul718e3742002-12-13 20:15:29 +0000481
paulb9df2d22004-05-09 09:09:59 +0000482 /* Interface index. */
483 stream_putc (s, 1);
484 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000485
paulb9df2d22004-05-09 09:09:59 +0000486 break;
487 }
paul718e3742002-12-13 20:15:29 +0000488 }
489
490 /* Metric */
491 stream_putl (s, rib->metric);
492
paulb9df2d22004-05-09 09:09:59 +0000493 /* Write next-hop number */
494 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000495 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000496
paul718e3742002-12-13 20:15:29 +0000497 /* Write packet size. */
498 stream_putw_at (s, 0, stream_get_endp (s));
499
ajs719e9742005-02-28 20:52:15 +0000500 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000501}
502
paul718e3742002-12-13 20:15:29 +0000503#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000504static int
paul718e3742002-12-13 20:15:29 +0000505zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
506{
507 struct stream *s;
508 struct rib *rib;
509 unsigned long nump;
510 u_char num;
511 struct nexthop *nexthop;
512
513 /* Lookup nexthop. */
514 rib = rib_match_ipv6 (addr);
515
516 /* Get output stream. */
517 s = client->obuf;
518 stream_reset (s);
519
520 /* Fill in result. */
521 stream_putw (s, 0);
522 stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
523 stream_put (s, &addr, 16);
524
525 if (rib)
526 {
527 stream_putl (s, rib->metric);
528 num = 0;
paul9985f832005-02-09 15:51:56 +0000529 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000530 stream_putc (s, 0);
531 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
532 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
533 {
534 stream_putc (s, nexthop->type);
535 switch (nexthop->type)
536 {
537 case ZEBRA_NEXTHOP_IPV6:
538 stream_put (s, &nexthop->gate.ipv6, 16);
539 break;
540 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
541 case ZEBRA_NEXTHOP_IPV6_IFNAME:
542 stream_put (s, &nexthop->gate.ipv6, 16);
543 stream_putl (s, nexthop->ifindex);
544 break;
545 case ZEBRA_NEXTHOP_IFINDEX:
546 case ZEBRA_NEXTHOP_IFNAME:
547 stream_putl (s, nexthop->ifindex);
548 break;
hassofa2b17e2004-03-04 17:45:00 +0000549 default:
550 /* do nothing */
551 break;
paul718e3742002-12-13 20:15:29 +0000552 }
553 num++;
554 }
555 stream_putc_at (s, nump, num);
556 }
557 else
558 {
559 stream_putl (s, 0);
560 stream_putc (s, 0);
561 }
562
563 stream_putw_at (s, 0, stream_get_endp (s));
564
ajs719e9742005-02-28 20:52:15 +0000565 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000566}
567#endif /* HAVE_IPV6 */
568
paulb9df2d22004-05-09 09:09:59 +0000569static int
paul718e3742002-12-13 20:15:29 +0000570zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
571{
572 struct stream *s;
573 struct rib *rib;
574 unsigned long nump;
575 u_char num;
576 struct nexthop *nexthop;
577
578 /* Lookup nexthop. */
579 rib = rib_match_ipv4 (addr);
580
581 /* Get output stream. */
582 s = client->obuf;
583 stream_reset (s);
584
585 /* Fill in result. */
586 stream_putw (s, 0);
587 stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
588 stream_put_in_addr (s, &addr);
589
590 if (rib)
591 {
592 stream_putl (s, rib->metric);
593 num = 0;
paul9985f832005-02-09 15:51:56 +0000594 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000595 stream_putc (s, 0);
596 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
597 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
598 {
599 stream_putc (s, nexthop->type);
600 switch (nexthop->type)
601 {
602 case ZEBRA_NEXTHOP_IPV4:
603 stream_put_in_addr (s, &nexthop->gate.ipv4);
604 break;
605 case ZEBRA_NEXTHOP_IFINDEX:
606 case ZEBRA_NEXTHOP_IFNAME:
607 stream_putl (s, nexthop->ifindex);
608 break;
hassofa2b17e2004-03-04 17:45:00 +0000609 default:
610 /* do nothing */
611 break;
paul718e3742002-12-13 20:15:29 +0000612 }
613 num++;
614 }
615 stream_putc_at (s, nump, num);
616 }
617 else
618 {
619 stream_putl (s, 0);
620 stream_putc (s, 0);
621 }
622
623 stream_putw_at (s, 0, stream_get_endp (s));
624
ajs719e9742005-02-28 20:52:15 +0000625 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000626}
627
paulb9df2d22004-05-09 09:09:59 +0000628static int
paul718e3742002-12-13 20:15:29 +0000629zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
630{
631 struct stream *s;
632 struct rib *rib;
633 unsigned long nump;
634 u_char num;
635 struct nexthop *nexthop;
636
637 /* Lookup nexthop. */
638 rib = rib_lookup_ipv4 (p);
639
640 /* Get output stream. */
641 s = client->obuf;
642 stream_reset (s);
643
644 /* Fill in result. */
645 stream_putw (s, 0);
646 stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
647 stream_put_in_addr (s, &p->prefix);
648
649 if (rib)
650 {
651 stream_putl (s, rib->metric);
652 num = 0;
paul9985f832005-02-09 15:51:56 +0000653 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000654 stream_putc (s, 0);
655 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
656 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
657 {
658 stream_putc (s, nexthop->type);
659 switch (nexthop->type)
660 {
661 case ZEBRA_NEXTHOP_IPV4:
662 stream_put_in_addr (s, &nexthop->gate.ipv4);
663 break;
664 case ZEBRA_NEXTHOP_IFINDEX:
665 case ZEBRA_NEXTHOP_IFNAME:
666 stream_putl (s, nexthop->ifindex);
667 break;
hassofa2b17e2004-03-04 17:45:00 +0000668 default:
669 /* do nothing */
670 break;
paul718e3742002-12-13 20:15:29 +0000671 }
672 num++;
673 }
674 stream_putc_at (s, nump, num);
675 }
676 else
677 {
678 stream_putl (s, 0);
679 stream_putc (s, 0);
680 }
681
682 stream_putw_at (s, 0, stream_get_endp (s));
683
ajs719e9742005-02-28 20:52:15 +0000684 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000685}
686
hasso18a6dce2004-10-03 18:18:34 +0000687/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
688int
689zsend_router_id_update (struct zserv *client, struct prefix *p)
690{
691 struct stream *s;
692 int blen;
693
694 /* Check this client need interface information. */
695 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000696 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000697
698 s = client->obuf;
699 stream_reset (s);
700
701 /* Place holder for size. */
702 stream_putw (s, 0);
703
704 /* Message type. */
705 stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
706
707 /* Prefix information. */
708 stream_putc (s, p->family);
709 blen = prefix_blen (p);
710 stream_put (s, &p->u.prefix, blen);
711 stream_putc (s, p->prefixlen);
712
713 /* Write packet size. */
714 stream_putw_at (s, 0, stream_get_endp (s));
715
ajs719e9742005-02-28 20:52:15 +0000716 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000717}
718
paul718e3742002-12-13 20:15:29 +0000719/* Register zebra server interface information. Send current all
720 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000721static int
paul718e3742002-12-13 20:15:29 +0000722zread_interface_add (struct zserv *client, u_short length)
723{
paul1eb8ef22005-04-07 07:30:20 +0000724 struct listnode *ifnode, *ifnnode;
725 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000726 struct interface *ifp;
727 struct connected *c;
728
729 /* Interface information is needed. */
730 client->ifinfo = 1;
731
paul1eb8ef22005-04-07 07:30:20 +0000732 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000733 {
paul718e3742002-12-13 20:15:29 +0000734 /* Skip pseudo interface. */
735 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
736 continue;
737
ajs719e9742005-02-28 20:52:15 +0000738 if (zsend_interface_add (client, ifp) < 0)
739 return -1;
paul718e3742002-12-13 20:15:29 +0000740
paul1eb8ef22005-04-07 07:30:20 +0000741 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000742 {
ajs719e9742005-02-28 20:52:15 +0000743 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
744 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
745 ifp, c) < 0))
746 return -1;
paul718e3742002-12-13 20:15:29 +0000747 }
748 }
ajs719e9742005-02-28 20:52:15 +0000749 return 0;
paul718e3742002-12-13 20:15:29 +0000750}
751
752/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000753static int
paul718e3742002-12-13 20:15:29 +0000754zread_interface_delete (struct zserv *client, u_short length)
755{
756 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000757 return 0;
paul718e3742002-12-13 20:15:29 +0000758}
759
760/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000761/*
762 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
763 * add kernel route.
764 */
ajs719e9742005-02-28 20:52:15 +0000765static int
paul718e3742002-12-13 20:15:29 +0000766zread_ipv4_add (struct zserv *client, u_short length)
767{
768 int i;
769 struct rib *rib;
770 struct prefix_ipv4 p;
771 u_char message;
772 struct in_addr nexthop;
773 u_char nexthop_num;
774 u_char nexthop_type;
775 struct stream *s;
776 unsigned int ifindex;
777 u_char ifname_len;
778
779 /* Get input stream. */
780 s = client->ibuf;
781
782 /* Allocate new rib. */
783 rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
784 memset (rib, 0, sizeof (struct rib));
785
786 /* Type, flags, message. */
787 rib->type = stream_getc (s);
788 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000789 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000790 rib->uptime = time (NULL);
791
792 /* IPv4 prefix. */
793 memset (&p, 0, sizeof (struct prefix_ipv4));
794 p.family = AF_INET;
795 p.prefixlen = stream_getc (s);
796 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
797
798 /* Nexthop parse. */
799 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
800 {
801 nexthop_num = stream_getc (s);
802
803 for (i = 0; i < nexthop_num; i++)
804 {
805 nexthop_type = stream_getc (s);
806
807 switch (nexthop_type)
808 {
809 case ZEBRA_NEXTHOP_IFINDEX:
810 ifindex = stream_getl (s);
811 nexthop_ifindex_add (rib, ifindex);
812 break;
813 case ZEBRA_NEXTHOP_IFNAME:
814 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000815 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000816 break;
817 case ZEBRA_NEXTHOP_IPV4:
818 nexthop.s_addr = stream_get_ipv4 (s);
819 nexthop_ipv4_add (rib, &nexthop);
820 break;
821 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000822 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000823 break;
paul595db7f2003-05-25 21:35:06 +0000824 case ZEBRA_NEXTHOP_BLACKHOLE:
825 nexthop_blackhole_add (rib);
826 break;
paul718e3742002-12-13 20:15:29 +0000827 }
828 }
829 }
830
831 /* Distance. */
832 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
833 rib->distance = stream_getc (s);
834
835 /* Metric. */
836 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
837 rib->metric = stream_getl (s);
838
839 rib_add_ipv4_multipath (&p, rib);
ajs719e9742005-02-28 20:52:15 +0000840 return 0;
paul718e3742002-12-13 20:15:29 +0000841}
842
843/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000844static int
paul718e3742002-12-13 20:15:29 +0000845zread_ipv4_delete (struct zserv *client, u_short length)
846{
847 int i;
848 struct stream *s;
849 struct zapi_ipv4 api;
850 struct in_addr nexthop;
851 unsigned long ifindex;
852 struct prefix_ipv4 p;
853 u_char nexthop_num;
854 u_char nexthop_type;
855 u_char ifname_len;
856
857 s = client->ibuf;
858 ifindex = 0;
859 nexthop.s_addr = 0;
860
861 /* Type, flags, message. */
862 api.type = stream_getc (s);
863 api.flags = stream_getc (s);
864 api.message = stream_getc (s);
865
866 /* IPv4 prefix. */
867 memset (&p, 0, sizeof (struct prefix_ipv4));
868 p.family = AF_INET;
869 p.prefixlen = stream_getc (s);
870 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
871
872 /* Nexthop, ifindex, distance, metric. */
873 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
874 {
875 nexthop_num = stream_getc (s);
876
877 for (i = 0; i < nexthop_num; i++)
878 {
879 nexthop_type = stream_getc (s);
880
881 switch (nexthop_type)
882 {
883 case ZEBRA_NEXTHOP_IFINDEX:
884 ifindex = stream_getl (s);
885 break;
886 case ZEBRA_NEXTHOP_IFNAME:
887 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000888 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000889 break;
890 case ZEBRA_NEXTHOP_IPV4:
891 nexthop.s_addr = stream_get_ipv4 (s);
892 break;
893 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000894 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000895 break;
896 }
897 }
898 }
899
900 /* Distance. */
901 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
902 api.distance = stream_getc (s);
903 else
904 api.distance = 0;
905
906 /* Metric. */
907 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
908 api.metric = stream_getl (s);
909 else
910 api.metric = 0;
911
912 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
913 client->rtm_table);
ajs719e9742005-02-28 20:52:15 +0000914 return 0;
paul718e3742002-12-13 20:15:29 +0000915}
916
917/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000918static int
paul718e3742002-12-13 20:15:29 +0000919zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
920{
921 struct in_addr addr;
922
923 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000924 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000925}
926
927/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000928static int
paul718e3742002-12-13 20:15:29 +0000929zread_ipv4_import_lookup (struct zserv *client, u_short length)
930{
931 struct prefix_ipv4 p;
932
933 p.family = AF_INET;
934 p.prefixlen = stream_getc (client->ibuf);
935 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
936
ajs719e9742005-02-28 20:52:15 +0000937 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000938}
939
940#ifdef HAVE_IPV6
941/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000942static int
paul718e3742002-12-13 20:15:29 +0000943zread_ipv6_add (struct zserv *client, u_short length)
944{
945 int i;
946 struct stream *s;
947 struct zapi_ipv6 api;
948 struct in6_addr nexthop;
949 unsigned long ifindex;
950 struct prefix_ipv6 p;
951
952 s = client->ibuf;
953 ifindex = 0;
954 memset (&nexthop, 0, sizeof (struct in6_addr));
955
956 /* Type, flags, message. */
957 api.type = stream_getc (s);
958 api.flags = stream_getc (s);
959 api.message = stream_getc (s);
960
961 /* IPv4 prefix. */
962 memset (&p, 0, sizeof (struct prefix_ipv6));
963 p.family = AF_INET6;
964 p.prefixlen = stream_getc (s);
965 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
966
967 /* Nexthop, ifindex, distance, metric. */
968 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
969 {
970 u_char nexthop_type;
971
972 api.nexthop_num = stream_getc (s);
973 for (i = 0; i < api.nexthop_num; i++)
974 {
975 nexthop_type = stream_getc (s);
976
977 switch (nexthop_type)
978 {
979 case ZEBRA_NEXTHOP_IPV6:
980 stream_get (&nexthop, s, 16);
981 break;
982 case ZEBRA_NEXTHOP_IFINDEX:
983 ifindex = stream_getl (s);
984 break;
985 }
986 }
987 }
988
989 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
990 api.distance = stream_getc (s);
991 else
992 api.distance = 0;
993
994 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
995 api.metric = stream_getl (s);
996 else
997 api.metric = 0;
998
999 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1000 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1001 else
1002 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001003 return 0;
paul718e3742002-12-13 20:15:29 +00001004}
1005
1006/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001007static int
paul718e3742002-12-13 20:15:29 +00001008zread_ipv6_delete (struct zserv *client, u_short length)
1009{
1010 int i;
1011 struct stream *s;
1012 struct zapi_ipv6 api;
1013 struct in6_addr nexthop;
1014 unsigned long ifindex;
1015 struct prefix_ipv6 p;
1016
1017 s = client->ibuf;
1018 ifindex = 0;
1019 memset (&nexthop, 0, sizeof (struct in6_addr));
1020
1021 /* Type, flags, message. */
1022 api.type = stream_getc (s);
1023 api.flags = stream_getc (s);
1024 api.message = stream_getc (s);
1025
1026 /* IPv4 prefix. */
1027 memset (&p, 0, sizeof (struct prefix_ipv6));
1028 p.family = AF_INET6;
1029 p.prefixlen = stream_getc (s);
1030 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1031
1032 /* Nexthop, ifindex, distance, metric. */
1033 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1034 {
1035 u_char nexthop_type;
1036
1037 api.nexthop_num = stream_getc (s);
1038 for (i = 0; i < api.nexthop_num; i++)
1039 {
1040 nexthop_type = stream_getc (s);
1041
1042 switch (nexthop_type)
1043 {
1044 case ZEBRA_NEXTHOP_IPV6:
1045 stream_get (&nexthop, s, 16);
1046 break;
1047 case ZEBRA_NEXTHOP_IFINDEX:
1048 ifindex = stream_getl (s);
1049 break;
1050 }
1051 }
1052 }
1053
1054 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1055 api.distance = stream_getc (s);
1056 else
1057 api.distance = 0;
1058 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1059 api.metric = stream_getl (s);
1060 else
1061 api.metric = 0;
1062
1063 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
1064 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
1065 else
1066 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
ajs719e9742005-02-28 20:52:15 +00001067 return 0;
paul718e3742002-12-13 20:15:29 +00001068}
1069
ajs719e9742005-02-28 20:52:15 +00001070static int
paul718e3742002-12-13 20:15:29 +00001071zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1072{
1073 struct in6_addr addr;
1074 char buf[BUFSIZ];
1075
1076 stream_get (&addr, client->ibuf, 16);
1077 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1078
ajs719e9742005-02-28 20:52:15 +00001079 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001080}
1081#endif /* HAVE_IPV6 */
1082
hasso18a6dce2004-10-03 18:18:34 +00001083/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001084static int
hasso18a6dce2004-10-03 18:18:34 +00001085zread_router_id_add (struct zserv *client, u_short length)
1086{
1087 struct prefix p;
1088
1089 /* Router-id information is needed. */
1090 client->ridinfo = 1;
1091
1092 router_id_get (&p);
1093
ajs719e9742005-02-28 20:52:15 +00001094 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001095}
1096
1097/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001098static int
hasso18a6dce2004-10-03 18:18:34 +00001099zread_router_id_delete (struct zserv *client, u_short length)
1100{
1101 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001102 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001103}
1104
paul718e3742002-12-13 20:15:29 +00001105/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001106static void
paul718e3742002-12-13 20:15:29 +00001107zebra_client_close (struct zserv *client)
1108{
1109 /* Close file descriptor. */
1110 if (client->sock)
1111 {
1112 close (client->sock);
1113 client->sock = -1;
1114 }
1115
1116 /* Free stream buffers. */
1117 if (client->ibuf)
1118 stream_free (client->ibuf);
1119 if (client->obuf)
1120 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001121 if (client->wb)
1122 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001123
1124 /* Release threads. */
1125 if (client->t_read)
1126 thread_cancel (client->t_read);
1127 if (client->t_write)
1128 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001129 if (client->t_suicide)
1130 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001131
1132 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001133 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001134 XFREE (0, client);
1135}
1136
1137/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001138static void
paul718e3742002-12-13 20:15:29 +00001139zebra_client_create (int sock)
1140{
1141 struct zserv *client;
1142
1143 client = XCALLOC (0, sizeof (struct zserv));
1144
1145 /* Make client input/output buffer. */
1146 client->sock = sock;
1147 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1148 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001149 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001150
1151 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001152 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001153
1154 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001155 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001156
1157 /* Make new read thread. */
1158 zebra_event (ZEBRA_READ, sock, client);
1159}
1160
1161/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001162static int
paul718e3742002-12-13 20:15:29 +00001163zebra_client_read (struct thread *thread)
1164{
1165 int sock;
1166 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001167 size_t already;
paul718e3742002-12-13 20:15:29 +00001168 u_short length;
1169 u_char command;
1170
1171 /* Get thread data. Reset reading thread because I'm running. */
1172 sock = THREAD_FD (thread);
1173 client = THREAD_ARG (thread);
1174 client->t_read = NULL;
1175
ajs719e9742005-02-28 20:52:15 +00001176 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001177 {
ajs719e9742005-02-28 20:52:15 +00001178 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001179 return -1;
1180 }
ajs719e9742005-02-28 20:52:15 +00001181
1182 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001183 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001184 {
ajs57a14772005-04-10 15:01:56 +00001185 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001186 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001187 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001188 (nbyte == -1))
1189 {
1190 if (IS_ZEBRA_DEBUG_EVENT)
1191 zlog_debug ("connection closed socket [%d]", sock);
1192 zebra_client_close (client);
1193 return -1;
1194 }
ajs57a14772005-04-10 15:01:56 +00001195 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001196 {
1197 /* Try again later. */
1198 zebra_event (ZEBRA_READ, sock, client);
1199 return 0;
1200 }
ajs57a14772005-04-10 15:01:56 +00001201 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001202 }
1203
1204 /* Reset to read from the beginning of the incoming packet. */
1205 stream_set_getp(client->ibuf, 0);
1206
paul718e3742002-12-13 20:15:29 +00001207 length = stream_getw (client->ibuf);
1208 command = stream_getc (client->ibuf);
1209
ajs719e9742005-02-28 20:52:15 +00001210 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001211 {
ajs57a14772005-04-10 15:01:56 +00001212 zlog_warn("%s: socket %d message length %u is less than header size %d",
1213 __func__, sock, length, ZEBRA_HEADER_SIZE);
1214 zebra_client_close (client);
1215 return -1;
1216 }
1217 if (length > STREAM_SIZE(client->ibuf))
1218 {
1219 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1220 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001221 zebra_client_close (client);
1222 return -1;
1223 }
1224
paul718e3742002-12-13 20:15:29 +00001225 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001226 if (already < length)
paul718e3742002-12-13 20:15:29 +00001227 {
ajs57a14772005-04-10 15:01:56 +00001228 ssize_t nbyte;
1229 if (((nbyte = stream_read_try (client->ibuf, sock,
1230 length-already)) == 0) ||
1231 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001232 {
1233 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001234 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001235 zebra_client_close (client);
1236 return -1;
1237 }
ajs57a14772005-04-10 15:01:56 +00001238 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001239 {
1240 /* Try again later. */
1241 zebra_event (ZEBRA_READ, sock, client);
1242 return 0;
1243 }
paul718e3742002-12-13 20:15:29 +00001244 }
1245
ajs719e9742005-02-28 20:52:15 +00001246 length -= ZEBRA_HEADER_SIZE;
1247
paul718e3742002-12-13 20:15:29 +00001248 /* Debug packet information. */
1249 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001250 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001251
1252 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001253 zlog_debug ("zebra message received [%s] %d",
paul718e3742002-12-13 20:15:29 +00001254 zebra_command_str[command], length);
1255
1256 switch (command)
1257 {
hasso18a6dce2004-10-03 18:18:34 +00001258 case ZEBRA_ROUTER_ID_ADD:
1259 zread_router_id_add (client, length);
1260 break;
1261 case ZEBRA_ROUTER_ID_DELETE:
1262 zread_router_id_delete (client, length);
1263 break;
paul718e3742002-12-13 20:15:29 +00001264 case ZEBRA_INTERFACE_ADD:
1265 zread_interface_add (client, length);
1266 break;
1267 case ZEBRA_INTERFACE_DELETE:
1268 zread_interface_delete (client, length);
1269 break;
1270 case ZEBRA_IPV4_ROUTE_ADD:
1271 zread_ipv4_add (client, length);
1272 break;
1273 case ZEBRA_IPV4_ROUTE_DELETE:
1274 zread_ipv4_delete (client, length);
1275 break;
1276#ifdef HAVE_IPV6
1277 case ZEBRA_IPV6_ROUTE_ADD:
1278 zread_ipv6_add (client, length);
1279 break;
1280 case ZEBRA_IPV6_ROUTE_DELETE:
1281 zread_ipv6_delete (client, length);
1282 break;
1283#endif /* HAVE_IPV6 */
1284 case ZEBRA_REDISTRIBUTE_ADD:
1285 zebra_redistribute_add (command, client, length);
1286 break;
1287 case ZEBRA_REDISTRIBUTE_DELETE:
1288 zebra_redistribute_delete (command, client, length);
1289 break;
1290 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1291 zebra_redistribute_default_add (command, client, length);
1292 break;
1293 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1294 zebra_redistribute_default_delete (command, client, length);
1295 break;
1296 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1297 zread_ipv4_nexthop_lookup (client, length);
1298 break;
1299#ifdef HAVE_IPV6
1300 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1301 zread_ipv6_nexthop_lookup (client, length);
1302 break;
1303#endif /* HAVE_IPV6 */
1304 case ZEBRA_IPV4_IMPORT_LOOKUP:
1305 zread_ipv4_import_lookup (client, length);
1306 break;
1307 default:
1308 zlog_info ("Zebra received unknown command %d", command);
1309 break;
1310 }
1311
ajs719e9742005-02-28 20:52:15 +00001312 if (client->t_suicide)
1313 {
1314 /* No need to wait for thread callback, just kill immediately. */
1315 zebra_client_close(client);
1316 return -1;
1317 }
1318
paul718e3742002-12-13 20:15:29 +00001319 stream_reset (client->ibuf);
1320 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001321 return 0;
1322}
1323
paul718e3742002-12-13 20:15:29 +00001324
1325/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001326static int
paul718e3742002-12-13 20:15:29 +00001327zebra_accept (struct thread *thread)
1328{
1329 int accept_sock;
1330 int client_sock;
1331 struct sockaddr_in client;
1332 socklen_t len;
1333
1334 accept_sock = THREAD_FD (thread);
1335
ajs719e9742005-02-28 20:52:15 +00001336 /* Reregister myself. */
1337 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1338
paul718e3742002-12-13 20:15:29 +00001339 len = sizeof (struct sockaddr_in);
1340 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1341
1342 if (client_sock < 0)
1343 {
ajs6099b3b2004-11-20 02:06:59 +00001344 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001345 return -1;
1346 }
1347
paulccf35572003-03-01 11:42:20 +00001348 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001349 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001350
paul718e3742002-12-13 20:15:29 +00001351 /* Create new zebra client. */
1352 zebra_client_create (client_sock);
1353
paul718e3742002-12-13 20:15:29 +00001354 return 0;
1355}
1356
paulb9df2d22004-05-09 09:09:59 +00001357#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001358/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001359static void
paul718e3742002-12-13 20:15:29 +00001360zebra_serv ()
1361{
1362 int ret;
1363 int accept_sock;
1364 struct sockaddr_in addr;
1365
1366 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1367
1368 if (accept_sock < 0)
1369 {
paul3d1dc852005-04-05 00:45:23 +00001370 zlog_warn ("Can't create zserv stream socket: %s",
1371 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001372 zlog_warn ("zebra can't provice full functionality due to above error");
1373 return;
1374 }
1375
1376 memset (&addr, 0, sizeof (struct sockaddr_in));
1377 addr.sin_family = AF_INET;
1378 addr.sin_port = htons (ZEBRA_PORT);
1379#ifdef HAVE_SIN_LEN
1380 addr.sin_len = sizeof (struct sockaddr_in);
1381#endif /* HAVE_SIN_LEN */
1382 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1383
1384 sockopt_reuseaddr (accept_sock);
1385 sockopt_reuseport (accept_sock);
1386
pauledd7c242003-06-04 13:59:38 +00001387 if ( zserv_privs.change(ZPRIVS_RAISE) )
1388 zlog (NULL, LOG_ERR, "Can't raise privileges");
1389
paul718e3742002-12-13 20:15:29 +00001390 ret = bind (accept_sock, (struct sockaddr *)&addr,
1391 sizeof (struct sockaddr_in));
1392 if (ret < 0)
1393 {
paul3d1dc852005-04-05 00:45:23 +00001394 zlog_warn ("Can't bind to stream socket: %s",
1395 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001396 zlog_warn ("zebra can't provice full functionality due to above error");
1397 close (accept_sock); /* Avoid sd leak. */
1398 return;
1399 }
pauledd7c242003-06-04 13:59:38 +00001400
1401 if ( zserv_privs.change(ZPRIVS_LOWER) )
1402 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001403
1404 ret = listen (accept_sock, 1);
1405 if (ret < 0)
1406 {
paul3d1dc852005-04-05 00:45:23 +00001407 zlog_warn ("Can't listen to stream socket: %s",
1408 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001409 zlog_warn ("zebra can't provice full functionality due to above error");
1410 close (accept_sock); /* Avoid sd leak. */
1411 return;
1412 }
1413
1414 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1415}
paulb9df2d22004-05-09 09:09:59 +00001416#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001417
1418/* For sockaddr_un. */
1419#include <sys/un.h>
1420
1421/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001422static void
hassofce954f2004-10-07 20:29:24 +00001423zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001424{
1425 int ret;
1426 int sock, len;
1427 struct sockaddr_un serv;
1428 mode_t old_mask;
1429
1430 /* First of all, unlink existing socket */
1431 unlink (path);
1432
1433 /* Set umask */
1434 old_mask = umask (0077);
1435
1436 /* Make UNIX domain socket. */
1437 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1438 if (sock < 0)
1439 {
paul3d1dc852005-04-05 00:45:23 +00001440 zlog_warn ("Can't create zserv unix socket: %s",
1441 safe_strerror (errno));
1442 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001443 return;
1444 }
1445
1446 /* Make server socket. */
1447 memset (&serv, 0, sizeof (struct sockaddr_un));
1448 serv.sun_family = AF_UNIX;
1449 strncpy (serv.sun_path, path, strlen (path));
1450#ifdef HAVE_SUN_LEN
1451 len = serv.sun_len = SUN_LEN(&serv);
1452#else
1453 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1454#endif /* HAVE_SUN_LEN */
1455
1456 ret = bind (sock, (struct sockaddr *) &serv, len);
1457 if (ret < 0)
1458 {
paul3d1dc852005-04-05 00:45:23 +00001459 zlog_warn ("Can't bind to unix socket %s: %s",
1460 path, safe_strerror (errno));
1461 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001462 close (sock);
1463 return;
1464 }
1465
1466 ret = listen (sock, 5);
1467 if (ret < 0)
1468 {
paul3d1dc852005-04-05 00:45:23 +00001469 zlog_warn ("Can't listen to unix socket %s: %s",
1470 path, safe_strerror (errno));
1471 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001472 close (sock);
1473 return;
1474 }
1475
1476 umask (old_mask);
1477
1478 zebra_event (ZEBRA_SERV, sock, NULL);
1479}
1480
paul718e3742002-12-13 20:15:29 +00001481
paulb9df2d22004-05-09 09:09:59 +00001482static void
paul718e3742002-12-13 20:15:29 +00001483zebra_event (enum event event, int sock, struct zserv *client)
1484{
1485 switch (event)
1486 {
1487 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001488 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001489 break;
1490 case ZEBRA_READ:
1491 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001492 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001493 break;
1494 case ZEBRA_WRITE:
1495 /**/
1496 break;
1497 }
1498}
1499
1500/* Display default rtm_table for all clients. */
1501DEFUN (show_table,
1502 show_table_cmd,
1503 "show table",
1504 SHOW_STR
1505 "default routing table to use for all clients\n")
1506{
paulb21b19c2003-06-15 01:28:29 +00001507 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001508 VTY_NEWLINE);
1509 return CMD_SUCCESS;
1510}
1511
1512DEFUN (config_table,
1513 config_table_cmd,
1514 "table TABLENO",
1515 "Configure target kernel routing table\n"
1516 "TABLE integer\n")
1517{
paulb21b19c2003-06-15 01:28:29 +00001518 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001519 return CMD_SUCCESS;
1520}
1521
hasso647e4f12003-05-25 11:43:52 +00001522DEFUN (ip_forwarding,
1523 ip_forwarding_cmd,
1524 "ip forwarding",
1525 IP_STR
1526 "Turn on IP forwarding")
1527{
1528 int ret;
1529
1530 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001531 if (ret == 0)
1532 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001533
hasso647e4f12003-05-25 11:43:52 +00001534 if (ret == 0)
1535 {
1536 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1537 return CMD_WARNING;
1538 }
1539
1540 return CMD_SUCCESS;
1541}
1542
paul718e3742002-12-13 20:15:29 +00001543DEFUN (no_ip_forwarding,
1544 no_ip_forwarding_cmd,
1545 "no ip forwarding",
1546 NO_STR
1547 IP_STR
1548 "Turn off IP forwarding")
1549{
1550 int ret;
1551
1552 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001553 if (ret != 0)
1554 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001555
paul718e3742002-12-13 20:15:29 +00001556 if (ret != 0)
1557 {
1558 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1559 return CMD_WARNING;
1560 }
1561
1562 return CMD_SUCCESS;
1563}
1564
1565/* This command is for debugging purpose. */
1566DEFUN (show_zebra_client,
1567 show_zebra_client_cmd,
1568 "show zebra client",
1569 SHOW_STR
1570 "Zebra information"
1571 "Client information")
1572{
hasso52dc7ee2004-09-23 19:18:23 +00001573 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001574 struct zserv *client;
1575
paul1eb8ef22005-04-07 07:30:20 +00001576 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1577 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1578
paul718e3742002-12-13 20:15:29 +00001579 return CMD_SUCCESS;
1580}
1581
1582/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001583static int
paul718e3742002-12-13 20:15:29 +00001584config_write_table (struct vty *vty)
1585{
paulb21b19c2003-06-15 01:28:29 +00001586 if (zebrad.rtm_table_default)
1587 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001588 VTY_NEWLINE);
1589 return 0;
1590}
1591
1592/* table node for routing tables. */
1593struct cmd_node table_node =
1594{
1595 TABLE_NODE,
1596 "", /* This node has no interface. */
1597 1
1598};
1599
1600/* Only display ip forwarding is enabled or not. */
1601DEFUN (show_ip_forwarding,
1602 show_ip_forwarding_cmd,
1603 "show ip forwarding",
1604 SHOW_STR
1605 IP_STR
1606 "IP forwarding status\n")
1607{
1608 int ret;
1609
1610 ret = ipforward ();
1611
1612 if (ret == 0)
1613 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1614 else
1615 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1616 return CMD_SUCCESS;
1617}
1618
1619#ifdef HAVE_IPV6
1620/* Only display ipv6 forwarding is enabled or not. */
1621DEFUN (show_ipv6_forwarding,
1622 show_ipv6_forwarding_cmd,
1623 "show ipv6 forwarding",
1624 SHOW_STR
1625 "IPv6 information\n"
1626 "Forwarding status\n")
1627{
1628 int ret;
1629
1630 ret = ipforward_ipv6 ();
1631
1632 switch (ret)
1633 {
1634 case -1:
1635 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1636 break;
1637 case 0:
1638 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1639 break;
1640 case 1:
1641 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1642 break;
1643 default:
1644 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1645 break;
1646 }
1647 return CMD_SUCCESS;
1648}
1649
hasso55906722004-02-11 22:42:16 +00001650DEFUN (ipv6_forwarding,
1651 ipv6_forwarding_cmd,
1652 "ipv6 forwarding",
1653 IPV6_STR
1654 "Turn on IPv6 forwarding")
1655{
1656 int ret;
1657
hasso41d3fc92004-04-06 11:59:00 +00001658 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001659 if (ret == 0)
1660 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001661
hasso41d3fc92004-04-06 11:59:00 +00001662 if (ret == 0)
1663 {
hasso55906722004-02-11 22:42:16 +00001664 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1665 return CMD_WARNING;
1666 }
1667
1668 return CMD_SUCCESS;
1669}
1670
paul718e3742002-12-13 20:15:29 +00001671DEFUN (no_ipv6_forwarding,
1672 no_ipv6_forwarding_cmd,
1673 "no ipv6 forwarding",
1674 NO_STR
hasso55906722004-02-11 22:42:16 +00001675 IPV6_STR
1676 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001677{
1678 int ret;
1679
hasso41d3fc92004-04-06 11:59:00 +00001680 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001681 if (ret != 0)
1682 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001683
paul718e3742002-12-13 20:15:29 +00001684 if (ret != 0)
1685 {
1686 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1687 return CMD_WARNING;
1688 }
1689
1690 return CMD_SUCCESS;
1691}
1692
1693#endif /* HAVE_IPV6 */
1694
1695/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001696static int
paul718e3742002-12-13 20:15:29 +00001697config_write_forwarding (struct vty *vty)
1698{
hasso18a6dce2004-10-03 18:18:34 +00001699 /* FIXME: Find better place for that. */
1700 router_id_write (vty);
1701
paul3e0b3a52004-08-23 18:58:32 +00001702 if (ipforward ())
1703 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001704#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001705 if (ipforward_ipv6 ())
1706 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001707#endif /* HAVE_IPV6 */
1708 vty_out (vty, "!%s", VTY_NEWLINE);
1709 return 0;
1710}
1711
1712/* table node for routing tables. */
1713struct cmd_node forwarding_node =
1714{
1715 FORWARDING_NODE,
1716 "", /* This node has no interface. */
1717 1
1718};
1719
1720
1721/* Initialisation of zebra and installation of commands. */
1722void
1723zebra_init ()
1724{
1725 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001726 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001727
paul718e3742002-12-13 20:15:29 +00001728 /* Make zebra server socket. */
1729#ifdef HAVE_TCP_ZEBRA
1730 zebra_serv ();
1731#else
1732 zebra_serv_un (ZEBRA_SERV_PATH);
1733#endif /* HAVE_TCP_ZEBRA */
1734
1735 /* Install configuration write function. */
1736 install_node (&table_node, config_write_table);
1737 install_node (&forwarding_node, config_write_forwarding);
1738
1739 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1740 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001741 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001742 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1743 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1744
1745#ifdef HAVE_NETLINK
1746 install_element (VIEW_NODE, &show_table_cmd);
1747 install_element (ENABLE_NODE, &show_table_cmd);
1748 install_element (CONFIG_NODE, &config_table_cmd);
1749#endif /* HAVE_NETLINK */
1750
1751#ifdef HAVE_IPV6
1752 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1753 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001754 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001755 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1756#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001757}