blob: ec8cbf2b2a9c08a8cf65e034a429385263de2af8 [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
ajs719e9742005-02-28 20:52:15 +000055static void zebra_client_close (struct zserv *client);
56
57static int
58zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000059{
ajs719e9742005-02-28 20:52:15 +000060 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000061
ajs719e9742005-02-28 20:52:15 +000062 client->t_suicide = NULL;
63 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000064 return 0;
65}
66
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +040067/* When client connects, it sends hello message
68 * with promise to send zebra routes of specific type.
69 * Zebra stores a socket fd of the client into
70 * this array. And use it to clean up routes that
71 * client didn't remove for some reasons after closing
72 * connection.
73 */
74static int route_type_oaths[ZEBRA_ROUTE_MAX];
75
ajs719e9742005-02-28 20:52:15 +000076static int
77zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000078{
ajs719e9742005-02-28 20:52:15 +000079 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000080
ajs719e9742005-02-28 20:52:15 +000081 client->t_write = NULL;
82 if (client->t_suicide)
83 {
84 zebra_client_close(client);
85 return -1;
86 }
87 switch (buffer_flush_available(client->wb, client->sock))
88 {
89 case BUFFER_ERROR:
90 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
91 "closing", __func__, client->sock);
92 zebra_client_close(client);
93 break;
94 case BUFFER_PENDING:
95 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
96 client, client->sock);
97 break;
98 case BUFFER_EMPTY:
99 break;
100 }
101 return 0;
paulccf35572003-03-01 11:42:20 +0000102}
103
ajs719e9742005-02-28 20:52:15 +0000104static int
105zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000106{
ajs719e9742005-02-28 20:52:15 +0000107 if (client->t_suicide)
108 return -1;
109 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
110 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000111 {
ajs719e9742005-02-28 20:52:15 +0000112 case BUFFER_ERROR:
113 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
114 __func__, client->sock);
115 /* Schedule a delayed close since many of the functions that call this
116 one do not check the return code. They do not allow for the
117 possibility that an I/O error may have caused the client to be
118 deleted. */
119 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
120 client, 0);
121 return -1;
ajs719e9742005-02-28 20:52:15 +0000122 case BUFFER_EMPTY:
123 THREAD_OFF(client->t_write);
124 break;
125 case BUFFER_PENDING:
126 THREAD_WRITE_ON(zebrad.master, client->t_write,
127 zserv_flush_data, client, client->sock);
128 break;
paulccf35572003-03-01 11:42:20 +0000129 }
paulccf35572003-03-01 11:42:20 +0000130 return 0;
131}
132
paulc1b98002006-01-16 01:54:02 +0000133static void
134zserv_create_header (struct stream *s, uint16_t cmd)
135{
136 /* length placeholder, caller can update */
137 stream_putw (s, ZEBRA_HEADER_SIZE);
138 stream_putc (s, ZEBRA_HEADER_MARKER);
139 stream_putc (s, ZSERV_VERSION);
140 stream_putw (s, cmd);
141}
142
paul718e3742002-12-13 20:15:29 +0000143/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000144/*
145 * This function is called in the following situations:
146 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
147 * from the client.
148 * - at startup, when zebra figures out the available interfaces
149 * - when an interface is added (where support for
150 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
151 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
152 * received)
153 */
paul718e3742002-12-13 20:15:29 +0000154int
155zsend_interface_add (struct zserv *client, struct interface *ifp)
156{
157 struct stream *s;
158
159 /* Check this client need interface information. */
160 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000161 return 0;
paul718e3742002-12-13 20:15:29 +0000162
163 s = client->obuf;
164 stream_reset (s);
165
paul718e3742002-12-13 20:15:29 +0000166 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000167 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
paul718e3742002-12-13 20:15:29 +0000168
169 /* Interface information. */
170 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
171 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000172 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000173 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000174 stream_putl (s, ifp->metric);
175 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000176 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000177 stream_putl (s, ifp->bandwidth);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000178#ifdef HAVE_STRUCT_SOCKADDR_DL
paul718e3742002-12-13 20:15:29 +0000179 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
180#else
181 stream_putl (s, ifp->hw_addr_len);
182 if (ifp->hw_addr_len)
183 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000184#endif /* HAVE_STRUCT_SOCKADDR_DL */
paul718e3742002-12-13 20:15:29 +0000185
186 /* Write packet size. */
187 stream_putw_at (s, 0, stream_get_endp (s));
188
ajs719e9742005-02-28 20:52:15 +0000189 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000190}
191
192/* Interface deletion from zebra daemon. */
193int
194zsend_interface_delete (struct zserv *client, struct interface *ifp)
195{
196 struct stream *s;
197
198 /* Check this client need interface information. */
199 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000200 return 0;
paul718e3742002-12-13 20:15:29 +0000201
202 s = client->obuf;
203 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000204
205 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
206
paul718e3742002-12-13 20:15:29 +0000207 /* Interface information. */
paul718e3742002-12-13 20:15:29 +0000208 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
209 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000210 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000211 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000212 stream_putl (s, ifp->metric);
213 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000214 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000215 stream_putl (s, ifp->bandwidth);
216
217 /* Write packet length. */
218 stream_putw_at (s, 0, stream_get_endp (s));
219
ajs719e9742005-02-28 20:52:15 +0000220 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000221}
222
paulb9df2d22004-05-09 09:09:59 +0000223/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
224 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
225 *
226 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
227 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
228 * from the client, after the ZEBRA_INTERFACE_ADD has been
229 * sent from zebra to the client
230 * - redistribute new address info to all clients in the following situations
231 * - at startup, when zebra figures out the available interfaces
232 * - when an interface is added (where support for
233 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
234 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
235 * received)
236 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
237 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
238 * - when an RTM_NEWADDR message is received from the kernel,
239 *
240 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
241 *
242 * zsend_interface_address(DELETE)
243 * ^
244 * |
245 * zebra_interface_address_delete_update
246 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000247 * | | if_delete_update
248 * | |
paulb9df2d22004-05-09 09:09:59 +0000249 * ip_address_uninstall connected_delete_ipv4
250 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
251 * ^ ^
252 * | |
253 * | RTM_NEWADDR on routing/netlink socket
254 * |
255 * vty commands:
256 * "no ip address A.B.C.D/M [label LINE]"
257 * "no ip address A.B.C.D/M secondary"
258 * ["no ipv6 address X:X::X:X/M"]
259 *
260 */
paul718e3742002-12-13 20:15:29 +0000261int
paulb9df2d22004-05-09 09:09:59 +0000262zsend_interface_address (int cmd, struct zserv *client,
263 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000264{
265 int blen;
266 struct stream *s;
267 struct prefix *p;
268
269 /* Check this client need interface information. */
270 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000271 return 0;
paul718e3742002-12-13 20:15:29 +0000272
273 s = client->obuf;
274 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000275
276 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000277 stream_putl (s, ifp->ifindex);
278
279 /* Interface address flag. */
280 stream_putc (s, ifc->flags);
281
282 /* Prefix information. */
283 p = ifc->address;
284 stream_putc (s, p->family);
285 blen = prefix_blen (p);
286 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000287
288 /*
289 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
290 * but zebra_interface_address_delete_read() in the gnu version
291 * expects to find it
292 */
paul718e3742002-12-13 20:15:29 +0000293 stream_putc (s, p->prefixlen);
294
295 /* Destination. */
296 p = ifc->destination;
297 if (p)
298 stream_put (s, &p->u.prefix, blen);
299 else
300 stream_put (s, NULL, blen);
301
302 /* Write packet size. */
303 stream_putw_at (s, 0, stream_get_endp (s));
304
ajs719e9742005-02-28 20:52:15 +0000305 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000306}
307
paulb9df2d22004-05-09 09:09:59 +0000308/*
309 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
310 * ZEBRA_INTERFACE_DOWN.
311 *
312 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
313 * the clients in one of 2 situations:
314 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
315 * - a vty command modifying the bandwidth of an interface is received.
316 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
317 */
paul718e3742002-12-13 20:15:29 +0000318int
paulb9df2d22004-05-09 09:09:59 +0000319zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000320{
321 struct stream *s;
322
323 /* Check this client need interface information. */
324 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000325 return 0;
paul718e3742002-12-13 20:15:29 +0000326
327 s = client->obuf;
328 stream_reset (s);
329
paulc1b98002006-01-16 01:54:02 +0000330 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000331
332 /* Interface information. */
333 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
334 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000335 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000336 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000337 stream_putl (s, ifp->metric);
338 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000339 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000340 stream_putl (s, ifp->bandwidth);
341
342 /* Write packet size. */
343 stream_putw_at (s, 0, stream_get_endp (s));
344
ajs719e9742005-02-28 20:52:15 +0000345 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000346}
347
paulb9df2d22004-05-09 09:09:59 +0000348/*
349 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
350 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
351 * situations:
352 * - when the client starts up, and requests default information
353 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
354 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
355 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
356 * - when the zebra server redistributes routes after it updates its rib
357 *
358 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
359 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
360 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
361 * - deleted from zebra's rib, and this info
362 * has to be redistributed to the clients
363 *
364 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
365 * zebra server when the client wants to tell the zebra server to add a
366 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
367 * same message being sent back and forth, this function and
368 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
369 * duplication.
370 */
paul718e3742002-12-13 20:15:29 +0000371int
paulb9df2d22004-05-09 09:09:59 +0000372zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
373 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000374{
375 int psize;
376 struct stream *s;
377 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000378 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000379 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000380 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000381
paul718e3742002-12-13 20:15:29 +0000382 s = client->obuf;
383 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000384
385 zserv_create_header (s, cmd);
386
387 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000388 stream_putc (s, rib->type);
389 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000390
391 /* marker for message flags field */
392 messmark = stream_get_endp (s);
393 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000394
395 /* Prefix. */
396 psize = PSIZE (p->prefixlen);
397 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000398 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000399
paulb9df2d22004-05-09 09:09:59 +0000400 /*
401 * XXX The message format sent by zebra below does not match the format
402 * of the corresponding message expected by the zebra server
403 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
404 * (is there a bug on the client side if more than one segment is sent?)
405 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
406 * is hard-coded.
407 */
paul718e3742002-12-13 20:15:29 +0000408 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000409
paul718e3742002-12-13 20:15:29 +0000410 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
411 {
412 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000413 {
paul1dcb5172005-05-31 08:38:50 +0000414 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
415 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
416
417 if (nhnummark == 0)
418 {
419 nhnummark = stream_get_endp (s);
420 stream_putc (s, 1); /* placeholder */
421 }
422
paulb9df2d22004-05-09 09:09:59 +0000423 nhnum++;
paul718e3742002-12-13 20:15:29 +0000424
paulb9df2d22004-05-09 09:09:59 +0000425 switch(nexthop->type)
426 {
427 case NEXTHOP_TYPE_IPV4:
428 case NEXTHOP_TYPE_IPV4_IFINDEX:
429 stream_put_in_addr (s, &nexthop->gate.ipv4);
430 break;
431#ifdef HAVE_IPV6
432 case NEXTHOP_TYPE_IPV6:
433 case NEXTHOP_TYPE_IPV6_IFINDEX:
434 case NEXTHOP_TYPE_IPV6_IFNAME:
435 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
436 break;
437#endif
438 default:
439 if (cmd == ZEBRA_IPV4_ROUTE_ADD
440 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
441 {
442 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000443 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000444 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
445 }
446 else
447 {
448 struct in6_addr empty;
449 memset (&empty, 0, sizeof (struct in6_addr));
450 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
451 }
452 }
paul718e3742002-12-13 20:15:29 +0000453
paulb9df2d22004-05-09 09:09:59 +0000454 /* Interface index. */
455 stream_putc (s, 1);
456 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000457
paulb9df2d22004-05-09 09:09:59 +0000458 break;
459 }
paul718e3742002-12-13 20:15:29 +0000460 }
461
462 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700463 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000464 {
vincentfbf5d032005-09-29 11:25:50 +0000465 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
466 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000467 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
468 stream_putl (s, rib->metric);
469 }
470
471 /* write real message flags value */
472 stream_putc_at (s, messmark, zapi_flags);
473
paulb9df2d22004-05-09 09:09:59 +0000474 /* Write next-hop number */
475 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000476 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000477
paul718e3742002-12-13 20:15:29 +0000478 /* Write packet size. */
479 stream_putw_at (s, 0, stream_get_endp (s));
480
ajs719e9742005-02-28 20:52:15 +0000481 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000482}
483
paul718e3742002-12-13 20:15:29 +0000484#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000485static int
paul718e3742002-12-13 20:15:29 +0000486zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
487{
488 struct stream *s;
489 struct rib *rib;
490 unsigned long nump;
491 u_char num;
492 struct nexthop *nexthop;
493
494 /* Lookup nexthop. */
495 rib = rib_match_ipv6 (addr);
496
497 /* Get output stream. */
498 s = client->obuf;
499 stream_reset (s);
500
501 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000502 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000503 stream_put (s, &addr, 16);
504
505 if (rib)
506 {
507 stream_putl (s, rib->metric);
508 num = 0;
paul9985f832005-02-09 15:51:56 +0000509 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000510 stream_putc (s, 0);
511 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
512 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
513 {
514 stream_putc (s, nexthop->type);
515 switch (nexthop->type)
516 {
517 case ZEBRA_NEXTHOP_IPV6:
518 stream_put (s, &nexthop->gate.ipv6, 16);
519 break;
520 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
521 case ZEBRA_NEXTHOP_IPV6_IFNAME:
522 stream_put (s, &nexthop->gate.ipv6, 16);
523 stream_putl (s, nexthop->ifindex);
524 break;
525 case ZEBRA_NEXTHOP_IFINDEX:
526 case ZEBRA_NEXTHOP_IFNAME:
527 stream_putl (s, nexthop->ifindex);
528 break;
hassofa2b17e2004-03-04 17:45:00 +0000529 default:
530 /* do nothing */
531 break;
paul718e3742002-12-13 20:15:29 +0000532 }
533 num++;
534 }
535 stream_putc_at (s, nump, num);
536 }
537 else
538 {
539 stream_putl (s, 0);
540 stream_putc (s, 0);
541 }
542
543 stream_putw_at (s, 0, stream_get_endp (s));
544
ajs719e9742005-02-28 20:52:15 +0000545 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000546}
547#endif /* HAVE_IPV6 */
548
paulb9df2d22004-05-09 09:09:59 +0000549static int
paul718e3742002-12-13 20:15:29 +0000550zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
551{
552 struct stream *s;
553 struct rib *rib;
554 unsigned long nump;
555 u_char num;
556 struct nexthop *nexthop;
557
558 /* Lookup nexthop. */
559 rib = rib_match_ipv4 (addr);
560
561 /* Get output stream. */
562 s = client->obuf;
563 stream_reset (s);
564
565 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000566 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000567 stream_put_in_addr (s, &addr);
568
569 if (rib)
570 {
571 stream_putl (s, rib->metric);
572 num = 0;
paul9985f832005-02-09 15:51:56 +0000573 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000574 stream_putc (s, 0);
575 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
576 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
577 {
578 stream_putc (s, nexthop->type);
579 switch (nexthop->type)
580 {
581 case ZEBRA_NEXTHOP_IPV4:
582 stream_put_in_addr (s, &nexthop->gate.ipv4);
583 break;
584 case ZEBRA_NEXTHOP_IFINDEX:
585 case ZEBRA_NEXTHOP_IFNAME:
586 stream_putl (s, nexthop->ifindex);
587 break;
hassofa2b17e2004-03-04 17:45:00 +0000588 default:
589 /* do nothing */
590 break;
paul718e3742002-12-13 20:15:29 +0000591 }
592 num++;
593 }
594 stream_putc_at (s, nump, num);
595 }
596 else
597 {
598 stream_putl (s, 0);
599 stream_putc (s, 0);
600 }
601
602 stream_putw_at (s, 0, stream_get_endp (s));
603
ajs719e9742005-02-28 20:52:15 +0000604 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000605}
606
paulb9df2d22004-05-09 09:09:59 +0000607static int
paul718e3742002-12-13 20:15:29 +0000608zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
609{
610 struct stream *s;
611 struct rib *rib;
612 unsigned long nump;
613 u_char num;
614 struct nexthop *nexthop;
615
616 /* Lookup nexthop. */
617 rib = rib_lookup_ipv4 (p);
618
619 /* Get output stream. */
620 s = client->obuf;
621 stream_reset (s);
622
623 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000624 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000625 stream_put_in_addr (s, &p->prefix);
626
627 if (rib)
628 {
629 stream_putl (s, rib->metric);
630 num = 0;
paul9985f832005-02-09 15:51:56 +0000631 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000632 stream_putc (s, 0);
633 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
634 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
635 {
636 stream_putc (s, nexthop->type);
637 switch (nexthop->type)
638 {
639 case ZEBRA_NEXTHOP_IPV4:
640 stream_put_in_addr (s, &nexthop->gate.ipv4);
641 break;
642 case ZEBRA_NEXTHOP_IFINDEX:
643 case ZEBRA_NEXTHOP_IFNAME:
644 stream_putl (s, nexthop->ifindex);
645 break;
hassofa2b17e2004-03-04 17:45:00 +0000646 default:
647 /* do nothing */
648 break;
paul718e3742002-12-13 20:15:29 +0000649 }
650 num++;
651 }
652 stream_putc_at (s, nump, num);
653 }
654 else
655 {
656 stream_putl (s, 0);
657 stream_putc (s, 0);
658 }
659
660 stream_putw_at (s, 0, stream_get_endp (s));
661
ajs719e9742005-02-28 20:52:15 +0000662 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000663}
664
hasso18a6dce2004-10-03 18:18:34 +0000665/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
666int
667zsend_router_id_update (struct zserv *client, struct prefix *p)
668{
669 struct stream *s;
670 int blen;
671
672 /* Check this client need interface information. */
673 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000674 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000675
676 s = client->obuf;
677 stream_reset (s);
678
hasso18a6dce2004-10-03 18:18:34 +0000679 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000680 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000681
682 /* Prefix information. */
683 stream_putc (s, p->family);
684 blen = prefix_blen (p);
685 stream_put (s, &p->u.prefix, blen);
686 stream_putc (s, p->prefixlen);
687
688 /* Write packet size. */
689 stream_putw_at (s, 0, stream_get_endp (s));
690
ajs719e9742005-02-28 20:52:15 +0000691 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000692}
693
paul718e3742002-12-13 20:15:29 +0000694/* Register zebra server interface information. Send current all
695 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000696static int
paul718e3742002-12-13 20:15:29 +0000697zread_interface_add (struct zserv *client, u_short length)
698{
paul1eb8ef22005-04-07 07:30:20 +0000699 struct listnode *ifnode, *ifnnode;
700 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000701 struct interface *ifp;
702 struct connected *c;
703
704 /* Interface information is needed. */
705 client->ifinfo = 1;
706
paul1eb8ef22005-04-07 07:30:20 +0000707 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000708 {
paul718e3742002-12-13 20:15:29 +0000709 /* Skip pseudo interface. */
710 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
711 continue;
712
ajs719e9742005-02-28 20:52:15 +0000713 if (zsend_interface_add (client, ifp) < 0)
714 return -1;
paul718e3742002-12-13 20:15:29 +0000715
paul1eb8ef22005-04-07 07:30:20 +0000716 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000717 {
ajs719e9742005-02-28 20:52:15 +0000718 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
719 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
720 ifp, c) < 0))
721 return -1;
paul718e3742002-12-13 20:15:29 +0000722 }
723 }
ajs719e9742005-02-28 20:52:15 +0000724 return 0;
paul718e3742002-12-13 20:15:29 +0000725}
726
727/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000728static int
paul718e3742002-12-13 20:15:29 +0000729zread_interface_delete (struct zserv *client, u_short length)
730{
731 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000732 return 0;
paul718e3742002-12-13 20:15:29 +0000733}
734
735/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000736/*
737 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
738 * add kernel route.
739 */
ajs719e9742005-02-28 20:52:15 +0000740static int
paul718e3742002-12-13 20:15:29 +0000741zread_ipv4_add (struct zserv *client, u_short length)
742{
743 int i;
744 struct rib *rib;
745 struct prefix_ipv4 p;
746 u_char message;
747 struct in_addr nexthop;
748 u_char nexthop_num;
749 u_char nexthop_type;
750 struct stream *s;
751 unsigned int ifindex;
752 u_char ifname_len;
753
754 /* Get input stream. */
755 s = client->ibuf;
756
757 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000758 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
759
paul718e3742002-12-13 20:15:29 +0000760 /* Type, flags, message. */
761 rib->type = stream_getc (s);
762 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000763 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000764 rib->uptime = time (NULL);
765
766 /* IPv4 prefix. */
767 memset (&p, 0, sizeof (struct prefix_ipv4));
768 p.family = AF_INET;
769 p.prefixlen = stream_getc (s);
770 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
771
772 /* Nexthop parse. */
773 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
774 {
775 nexthop_num = stream_getc (s);
776
777 for (i = 0; i < nexthop_num; i++)
778 {
779 nexthop_type = stream_getc (s);
780
781 switch (nexthop_type)
782 {
783 case ZEBRA_NEXTHOP_IFINDEX:
784 ifindex = stream_getl (s);
785 nexthop_ifindex_add (rib, ifindex);
786 break;
787 case ZEBRA_NEXTHOP_IFNAME:
788 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000789 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000790 break;
791 case ZEBRA_NEXTHOP_IPV4:
792 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000793 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000794 break;
795 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000796 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000797 break;
paul595db7f2003-05-25 21:35:06 +0000798 case ZEBRA_NEXTHOP_BLACKHOLE:
799 nexthop_blackhole_add (rib);
800 break;
paul718e3742002-12-13 20:15:29 +0000801 }
802 }
803 }
804
805 /* Distance. */
806 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
807 rib->distance = stream_getc (s);
808
809 /* Metric. */
810 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
811 rib->metric = stream_getl (s);
812
Paul Jakma171eee32006-07-27 16:11:02 +0000813 /* Table */
814 rib->table=zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +0000815 rib_add_ipv4_multipath (&p, rib);
ajs719e9742005-02-28 20:52:15 +0000816 return 0;
paul718e3742002-12-13 20:15:29 +0000817}
818
819/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000820static int
paul718e3742002-12-13 20:15:29 +0000821zread_ipv4_delete (struct zserv *client, u_short length)
822{
823 int i;
824 struct stream *s;
825 struct zapi_ipv4 api;
826 struct in_addr nexthop;
827 unsigned long ifindex;
828 struct prefix_ipv4 p;
829 u_char nexthop_num;
830 u_char nexthop_type;
831 u_char ifname_len;
832
833 s = client->ibuf;
834 ifindex = 0;
835 nexthop.s_addr = 0;
836
837 /* Type, flags, message. */
838 api.type = stream_getc (s);
839 api.flags = stream_getc (s);
840 api.message = stream_getc (s);
841
842 /* IPv4 prefix. */
843 memset (&p, 0, sizeof (struct prefix_ipv4));
844 p.family = AF_INET;
845 p.prefixlen = stream_getc (s);
846 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
847
848 /* Nexthop, ifindex, distance, metric. */
849 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
850 {
851 nexthop_num = stream_getc (s);
852
853 for (i = 0; i < nexthop_num; i++)
854 {
855 nexthop_type = stream_getc (s);
856
857 switch (nexthop_type)
858 {
859 case ZEBRA_NEXTHOP_IFINDEX:
860 ifindex = stream_getl (s);
861 break;
862 case ZEBRA_NEXTHOP_IFNAME:
863 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000864 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000865 break;
866 case ZEBRA_NEXTHOP_IPV4:
867 nexthop.s_addr = stream_get_ipv4 (s);
868 break;
869 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000870 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000871 break;
872 }
873 }
874 }
875
876 /* Distance. */
877 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
878 api.distance = stream_getc (s);
879 else
880 api.distance = 0;
881
882 /* Metric. */
883 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
884 api.metric = stream_getl (s);
885 else
886 api.metric = 0;
887
888 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
889 client->rtm_table);
ajs719e9742005-02-28 20:52:15 +0000890 return 0;
paul718e3742002-12-13 20:15:29 +0000891}
892
893/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000894static int
paul718e3742002-12-13 20:15:29 +0000895zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
896{
897 struct in_addr addr;
898
899 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000900 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000901}
902
903/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000904static int
paul718e3742002-12-13 20:15:29 +0000905zread_ipv4_import_lookup (struct zserv *client, u_short length)
906{
907 struct prefix_ipv4 p;
908
909 p.family = AF_INET;
910 p.prefixlen = stream_getc (client->ibuf);
911 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
912
ajs719e9742005-02-28 20:52:15 +0000913 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000914}
915
916#ifdef HAVE_IPV6
917/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000918static int
paul718e3742002-12-13 20:15:29 +0000919zread_ipv6_add (struct zserv *client, u_short length)
920{
921 int i;
922 struct stream *s;
923 struct zapi_ipv6 api;
924 struct in6_addr nexthop;
925 unsigned long ifindex;
926 struct prefix_ipv6 p;
927
928 s = client->ibuf;
929 ifindex = 0;
930 memset (&nexthop, 0, sizeof (struct in6_addr));
931
932 /* Type, flags, message. */
933 api.type = stream_getc (s);
934 api.flags = stream_getc (s);
935 api.message = stream_getc (s);
936
937 /* IPv4 prefix. */
938 memset (&p, 0, sizeof (struct prefix_ipv6));
939 p.family = AF_INET6;
940 p.prefixlen = stream_getc (s);
941 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
942
943 /* Nexthop, ifindex, distance, metric. */
944 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
945 {
946 u_char nexthop_type;
947
948 api.nexthop_num = stream_getc (s);
949 for (i = 0; i < api.nexthop_num; i++)
950 {
951 nexthop_type = stream_getc (s);
952
953 switch (nexthop_type)
954 {
955 case ZEBRA_NEXTHOP_IPV6:
956 stream_get (&nexthop, s, 16);
957 break;
958 case ZEBRA_NEXTHOP_IFINDEX:
959 ifindex = stream_getl (s);
960 break;
961 }
962 }
963 }
964
965 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
966 api.distance = stream_getc (s);
967 else
968 api.distance = 0;
969
970 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
971 api.metric = stream_getl (s);
972 else
973 api.metric = 0;
974
975 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100976 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000977 api.distance);
paul718e3742002-12-13 20:15:29 +0000978 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100979 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000980 api.distance);
ajs719e9742005-02-28 20:52:15 +0000981 return 0;
paul718e3742002-12-13 20:15:29 +0000982}
983
984/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000985static int
paul718e3742002-12-13 20:15:29 +0000986zread_ipv6_delete (struct zserv *client, u_short length)
987{
988 int i;
989 struct stream *s;
990 struct zapi_ipv6 api;
991 struct in6_addr nexthop;
992 unsigned long ifindex;
993 struct prefix_ipv6 p;
994
995 s = client->ibuf;
996 ifindex = 0;
997 memset (&nexthop, 0, sizeof (struct in6_addr));
998
999 /* Type, flags, message. */
1000 api.type = stream_getc (s);
1001 api.flags = stream_getc (s);
1002 api.message = stream_getc (s);
1003
1004 /* IPv4 prefix. */
1005 memset (&p, 0, sizeof (struct prefix_ipv6));
1006 p.family = AF_INET6;
1007 p.prefixlen = stream_getc (s);
1008 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1009
1010 /* Nexthop, ifindex, distance, metric. */
1011 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1012 {
1013 u_char nexthop_type;
1014
1015 api.nexthop_num = stream_getc (s);
1016 for (i = 0; i < api.nexthop_num; i++)
1017 {
1018 nexthop_type = stream_getc (s);
1019
1020 switch (nexthop_type)
1021 {
1022 case ZEBRA_NEXTHOP_IPV6:
1023 stream_get (&nexthop, s, 16);
1024 break;
1025 case ZEBRA_NEXTHOP_IFINDEX:
1026 ifindex = stream_getl (s);
1027 break;
1028 }
1029 }
1030 }
1031
1032 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1033 api.distance = stream_getc (s);
1034 else
1035 api.distance = 0;
1036 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1037 api.metric = stream_getl (s);
1038 else
1039 api.metric = 0;
1040
1041 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001042 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table);
paul718e3742002-12-13 20:15:29 +00001043 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001044 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table);
ajs719e9742005-02-28 20:52:15 +00001045 return 0;
paul718e3742002-12-13 20:15:29 +00001046}
1047
ajs719e9742005-02-28 20:52:15 +00001048static int
paul718e3742002-12-13 20:15:29 +00001049zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1050{
1051 struct in6_addr addr;
1052 char buf[BUFSIZ];
1053
1054 stream_get (&addr, client->ibuf, 16);
1055 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1056
ajs719e9742005-02-28 20:52:15 +00001057 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001058}
1059#endif /* HAVE_IPV6 */
1060
hasso18a6dce2004-10-03 18:18:34 +00001061/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001062static int
hasso18a6dce2004-10-03 18:18:34 +00001063zread_router_id_add (struct zserv *client, u_short length)
1064{
1065 struct prefix p;
1066
1067 /* Router-id information is needed. */
1068 client->ridinfo = 1;
1069
1070 router_id_get (&p);
1071
ajs719e9742005-02-28 20:52:15 +00001072 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001073}
1074
1075/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001076static int
hasso18a6dce2004-10-03 18:18:34 +00001077zread_router_id_delete (struct zserv *client, u_short length)
1078{
1079 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001080 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001081}
1082
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +04001083/* Tie up route-type and client->sock */
1084static void
1085zread_hello (struct zserv *client)
1086{
1087 /* type of protocol (lib/zebra.h) */
1088 u_char proto;
1089 proto = stream_getc (client->ibuf);
1090
1091 /* accept only dynamic routing protocols */
1092 if ((proto < ZEBRA_ROUTE_MAX)
1093 && (proto > ZEBRA_ROUTE_STATIC))
1094 {
1095 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1096 client->sock, zebra_route_string(proto));
1097
1098 /* if route-type was binded by other client */
1099 if (route_type_oaths[proto])
1100 zlog_warn ("sender of %s routes changed %c->%c",
1101 zebra_route_string(proto), route_type_oaths[proto],
1102 client->sock);
1103
1104 route_type_oaths[proto] = client->sock;
1105 }
1106}
1107
1108/* If client sent routes of specific type, zebra removes it
1109 * and returns number of deleted routes.
1110 */
1111static void
1112zebra_score_rib (int client_sock)
1113{
1114 int i;
1115
1116 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1117 if (client_sock == route_type_oaths[i])
1118 {
1119 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1120 client_sock, rib_score_proto (i), zebra_route_string (i));
1121 route_type_oaths[i] = 0;
1122 break;
1123 }
1124}
1125
paul718e3742002-12-13 20:15:29 +00001126/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001127static void
paul718e3742002-12-13 20:15:29 +00001128zebra_client_close (struct zserv *client)
1129{
1130 /* Close file descriptor. */
1131 if (client->sock)
1132 {
1133 close (client->sock);
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +04001134 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001135 client->sock = -1;
1136 }
1137
1138 /* Free stream buffers. */
1139 if (client->ibuf)
1140 stream_free (client->ibuf);
1141 if (client->obuf)
1142 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001143 if (client->wb)
1144 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001145
1146 /* Release threads. */
1147 if (client->t_read)
1148 thread_cancel (client->t_read);
1149 if (client->t_write)
1150 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001151 if (client->t_suicide)
1152 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001153
1154 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001155 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001156 XFREE (0, client);
1157}
1158
1159/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001160static void
paul718e3742002-12-13 20:15:29 +00001161zebra_client_create (int sock)
1162{
1163 struct zserv *client;
1164
1165 client = XCALLOC (0, sizeof (struct zserv));
1166
1167 /* Make client input/output buffer. */
1168 client->sock = sock;
1169 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1170 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001171 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001172
1173 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001174 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001175
1176 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001177 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001178
1179 /* Make new read thread. */
1180 zebra_event (ZEBRA_READ, sock, client);
1181}
1182
1183/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001184static int
paul718e3742002-12-13 20:15:29 +00001185zebra_client_read (struct thread *thread)
1186{
1187 int sock;
1188 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001189 size_t already;
paulc1b98002006-01-16 01:54:02 +00001190 uint16_t length, command;
1191 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001192
1193 /* Get thread data. Reset reading thread because I'm running. */
1194 sock = THREAD_FD (thread);
1195 client = THREAD_ARG (thread);
1196 client->t_read = NULL;
1197
ajs719e9742005-02-28 20:52:15 +00001198 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001199 {
ajs719e9742005-02-28 20:52:15 +00001200 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001201 return -1;
1202 }
ajs719e9742005-02-28 20:52:15 +00001203
1204 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001205 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001206 {
ajs57a14772005-04-10 15:01:56 +00001207 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001208 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001209 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001210 (nbyte == -1))
1211 {
1212 if (IS_ZEBRA_DEBUG_EVENT)
1213 zlog_debug ("connection closed socket [%d]", sock);
1214 zebra_client_close (client);
1215 return -1;
1216 }
ajs57a14772005-04-10 15:01:56 +00001217 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001218 {
1219 /* Try again later. */
1220 zebra_event (ZEBRA_READ, sock, client);
1221 return 0;
1222 }
ajs57a14772005-04-10 15:01:56 +00001223 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001224 }
1225
1226 /* Reset to read from the beginning of the incoming packet. */
1227 stream_set_getp(client->ibuf, 0);
1228
paulc1b98002006-01-16 01:54:02 +00001229 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001230 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001231 marker = stream_getc (client->ibuf);
1232 version = stream_getc (client->ibuf);
1233 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001234
paulc1b98002006-01-16 01:54:02 +00001235 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1236 {
1237 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1238 __func__, sock, marker, version);
1239 zebra_client_close (client);
1240 return -1;
1241 }
ajs719e9742005-02-28 20:52:15 +00001242 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001243 {
ajs57a14772005-04-10 15:01:56 +00001244 zlog_warn("%s: socket %d message length %u is less than header size %d",
1245 __func__, sock, length, ZEBRA_HEADER_SIZE);
1246 zebra_client_close (client);
1247 return -1;
1248 }
1249 if (length > STREAM_SIZE(client->ibuf))
1250 {
1251 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1252 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001253 zebra_client_close (client);
1254 return -1;
1255 }
1256
paul718e3742002-12-13 20:15:29 +00001257 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001258 if (already < length)
paul718e3742002-12-13 20:15:29 +00001259 {
ajs57a14772005-04-10 15:01:56 +00001260 ssize_t nbyte;
1261 if (((nbyte = stream_read_try (client->ibuf, sock,
1262 length-already)) == 0) ||
1263 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001264 {
1265 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001266 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001267 zebra_client_close (client);
1268 return -1;
1269 }
ajs57a14772005-04-10 15:01:56 +00001270 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001271 {
1272 /* Try again later. */
1273 zebra_event (ZEBRA_READ, sock, client);
1274 return 0;
1275 }
paul718e3742002-12-13 20:15:29 +00001276 }
1277
ajs719e9742005-02-28 20:52:15 +00001278 length -= ZEBRA_HEADER_SIZE;
1279
paul718e3742002-12-13 20:15:29 +00001280 /* Debug packet information. */
1281 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001282 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001283
1284 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001285 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001286 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001287
1288 switch (command)
1289 {
hasso18a6dce2004-10-03 18:18:34 +00001290 case ZEBRA_ROUTER_ID_ADD:
1291 zread_router_id_add (client, length);
1292 break;
1293 case ZEBRA_ROUTER_ID_DELETE:
1294 zread_router_id_delete (client, length);
1295 break;
paul718e3742002-12-13 20:15:29 +00001296 case ZEBRA_INTERFACE_ADD:
1297 zread_interface_add (client, length);
1298 break;
1299 case ZEBRA_INTERFACE_DELETE:
1300 zread_interface_delete (client, length);
1301 break;
1302 case ZEBRA_IPV4_ROUTE_ADD:
1303 zread_ipv4_add (client, length);
1304 break;
1305 case ZEBRA_IPV4_ROUTE_DELETE:
1306 zread_ipv4_delete (client, length);
1307 break;
1308#ifdef HAVE_IPV6
1309 case ZEBRA_IPV6_ROUTE_ADD:
1310 zread_ipv6_add (client, length);
1311 break;
1312 case ZEBRA_IPV6_ROUTE_DELETE:
1313 zread_ipv6_delete (client, length);
1314 break;
1315#endif /* HAVE_IPV6 */
1316 case ZEBRA_REDISTRIBUTE_ADD:
1317 zebra_redistribute_add (command, client, length);
1318 break;
1319 case ZEBRA_REDISTRIBUTE_DELETE:
1320 zebra_redistribute_delete (command, client, length);
1321 break;
1322 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1323 zebra_redistribute_default_add (command, client, length);
1324 break;
1325 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1326 zebra_redistribute_default_delete (command, client, length);
1327 break;
1328 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1329 zread_ipv4_nexthop_lookup (client, length);
1330 break;
1331#ifdef HAVE_IPV6
1332 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1333 zread_ipv6_nexthop_lookup (client, length);
1334 break;
1335#endif /* HAVE_IPV6 */
1336 case ZEBRA_IPV4_IMPORT_LOOKUP:
1337 zread_ipv4_import_lookup (client, length);
1338 break;
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +04001339 case ZEBRA_HELLO:
1340 zread_hello (client);
1341 break;
paul718e3742002-12-13 20:15:29 +00001342 default:
1343 zlog_info ("Zebra received unknown command %d", command);
1344 break;
1345 }
1346
ajs719e9742005-02-28 20:52:15 +00001347 if (client->t_suicide)
1348 {
1349 /* No need to wait for thread callback, just kill immediately. */
1350 zebra_client_close(client);
1351 return -1;
1352 }
1353
paul718e3742002-12-13 20:15:29 +00001354 stream_reset (client->ibuf);
1355 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001356 return 0;
1357}
1358
paul718e3742002-12-13 20:15:29 +00001359
1360/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001361static int
paul718e3742002-12-13 20:15:29 +00001362zebra_accept (struct thread *thread)
1363{
1364 int accept_sock;
1365 int client_sock;
1366 struct sockaddr_in client;
1367 socklen_t len;
1368
1369 accept_sock = THREAD_FD (thread);
1370
ajs719e9742005-02-28 20:52:15 +00001371 /* Reregister myself. */
1372 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1373
paul718e3742002-12-13 20:15:29 +00001374 len = sizeof (struct sockaddr_in);
1375 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1376
1377 if (client_sock < 0)
1378 {
ajs6099b3b2004-11-20 02:06:59 +00001379 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001380 return -1;
1381 }
1382
paulccf35572003-03-01 11:42:20 +00001383 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001384 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001385
paul718e3742002-12-13 20:15:29 +00001386 /* Create new zebra client. */
1387 zebra_client_create (client_sock);
1388
paul718e3742002-12-13 20:15:29 +00001389 return 0;
1390}
1391
paulb9df2d22004-05-09 09:09:59 +00001392#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001393/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001394static void
paul718e3742002-12-13 20:15:29 +00001395zebra_serv ()
1396{
1397 int ret;
1398 int accept_sock;
1399 struct sockaddr_in addr;
1400
1401 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1402
1403 if (accept_sock < 0)
1404 {
paul3d1dc852005-04-05 00:45:23 +00001405 zlog_warn ("Can't create zserv stream socket: %s",
1406 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001407 zlog_warn ("zebra can't provice full functionality due to above error");
1408 return;
1409 }
1410
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +04001411 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001412 memset (&addr, 0, sizeof (struct sockaddr_in));
1413 addr.sin_family = AF_INET;
1414 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001415#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001416 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001417#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001418 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1419
1420 sockopt_reuseaddr (accept_sock);
1421 sockopt_reuseport (accept_sock);
1422
pauledd7c242003-06-04 13:59:38 +00001423 if ( zserv_privs.change(ZPRIVS_RAISE) )
1424 zlog (NULL, LOG_ERR, "Can't raise privileges");
1425
paul718e3742002-12-13 20:15:29 +00001426 ret = bind (accept_sock, (struct sockaddr *)&addr,
1427 sizeof (struct sockaddr_in));
1428 if (ret < 0)
1429 {
paul3d1dc852005-04-05 00:45:23 +00001430 zlog_warn ("Can't bind to stream socket: %s",
1431 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001432 zlog_warn ("zebra can't provice full functionality due to above error");
1433 close (accept_sock); /* Avoid sd leak. */
1434 return;
1435 }
pauledd7c242003-06-04 13:59:38 +00001436
1437 if ( zserv_privs.change(ZPRIVS_LOWER) )
1438 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001439
1440 ret = listen (accept_sock, 1);
1441 if (ret < 0)
1442 {
paul3d1dc852005-04-05 00:45:23 +00001443 zlog_warn ("Can't listen to stream socket: %s",
1444 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001445 zlog_warn ("zebra can't provice full functionality due to above error");
1446 close (accept_sock); /* Avoid sd leak. */
1447 return;
1448 }
1449
1450 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1451}
paulb9df2d22004-05-09 09:09:59 +00001452#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001453
1454/* For sockaddr_un. */
1455#include <sys/un.h>
1456
1457/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001458static void
hassofce954f2004-10-07 20:29:24 +00001459zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001460{
1461 int ret;
1462 int sock, len;
1463 struct sockaddr_un serv;
1464 mode_t old_mask;
1465
1466 /* First of all, unlink existing socket */
1467 unlink (path);
1468
1469 /* Set umask */
1470 old_mask = umask (0077);
1471
1472 /* Make UNIX domain socket. */
1473 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1474 if (sock < 0)
1475 {
paul3d1dc852005-04-05 00:45:23 +00001476 zlog_warn ("Can't create zserv unix socket: %s",
1477 safe_strerror (errno));
1478 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001479 return;
1480 }
1481
Vyacheslav Trushkin89e9f822011-12-11 18:48:47 +04001482 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1483
paul718e3742002-12-13 20:15:29 +00001484 /* Make server socket. */
1485 memset (&serv, 0, sizeof (struct sockaddr_un));
1486 serv.sun_family = AF_UNIX;
1487 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001488#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001489 len = serv.sun_len = SUN_LEN(&serv);
1490#else
1491 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001492#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001493
1494 ret = bind (sock, (struct sockaddr *) &serv, len);
1495 if (ret < 0)
1496 {
paul3d1dc852005-04-05 00:45:23 +00001497 zlog_warn ("Can't bind to unix socket %s: %s",
1498 path, safe_strerror (errno));
1499 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001500 close (sock);
1501 return;
1502 }
1503
1504 ret = listen (sock, 5);
1505 if (ret < 0)
1506 {
paul3d1dc852005-04-05 00:45:23 +00001507 zlog_warn ("Can't listen to unix socket %s: %s",
1508 path, safe_strerror (errno));
1509 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001510 close (sock);
1511 return;
1512 }
1513
1514 umask (old_mask);
1515
1516 zebra_event (ZEBRA_SERV, sock, NULL);
1517}
1518
paul718e3742002-12-13 20:15:29 +00001519
paulb9df2d22004-05-09 09:09:59 +00001520static void
paul718e3742002-12-13 20:15:29 +00001521zebra_event (enum event event, int sock, struct zserv *client)
1522{
1523 switch (event)
1524 {
1525 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001526 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001527 break;
1528 case ZEBRA_READ:
1529 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001530 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001531 break;
1532 case ZEBRA_WRITE:
1533 /**/
1534 break;
1535 }
1536}
1537
1538/* Display default rtm_table for all clients. */
1539DEFUN (show_table,
1540 show_table_cmd,
1541 "show table",
1542 SHOW_STR
1543 "default routing table to use for all clients\n")
1544{
paulb21b19c2003-06-15 01:28:29 +00001545 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001546 VTY_NEWLINE);
1547 return CMD_SUCCESS;
1548}
1549
1550DEFUN (config_table,
1551 config_table_cmd,
1552 "table TABLENO",
1553 "Configure target kernel routing table\n"
1554 "TABLE integer\n")
1555{
paulb21b19c2003-06-15 01:28:29 +00001556 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001557 return CMD_SUCCESS;
1558}
1559
hasso647e4f12003-05-25 11:43:52 +00001560DEFUN (ip_forwarding,
1561 ip_forwarding_cmd,
1562 "ip forwarding",
1563 IP_STR
1564 "Turn on IP forwarding")
1565{
1566 int ret;
1567
1568 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001569 if (ret == 0)
1570 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001571
hasso647e4f12003-05-25 11:43:52 +00001572 if (ret == 0)
1573 {
1574 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1575 return CMD_WARNING;
1576 }
1577
1578 return CMD_SUCCESS;
1579}
1580
paul718e3742002-12-13 20:15:29 +00001581DEFUN (no_ip_forwarding,
1582 no_ip_forwarding_cmd,
1583 "no ip forwarding",
1584 NO_STR
1585 IP_STR
1586 "Turn off IP forwarding")
1587{
1588 int ret;
1589
1590 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001591 if (ret != 0)
1592 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001593
paul718e3742002-12-13 20:15:29 +00001594 if (ret != 0)
1595 {
1596 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1597 return CMD_WARNING;
1598 }
1599
1600 return CMD_SUCCESS;
1601}
1602
1603/* This command is for debugging purpose. */
1604DEFUN (show_zebra_client,
1605 show_zebra_client_cmd,
1606 "show zebra client",
1607 SHOW_STR
1608 "Zebra information"
1609 "Client information")
1610{
hasso52dc7ee2004-09-23 19:18:23 +00001611 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001612 struct zserv *client;
1613
paul1eb8ef22005-04-07 07:30:20 +00001614 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1615 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1616
paul718e3742002-12-13 20:15:29 +00001617 return CMD_SUCCESS;
1618}
1619
1620/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001621static int
paul718e3742002-12-13 20:15:29 +00001622config_write_table (struct vty *vty)
1623{
paulb21b19c2003-06-15 01:28:29 +00001624 if (zebrad.rtm_table_default)
1625 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001626 VTY_NEWLINE);
1627 return 0;
1628}
1629
1630/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001631static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001632{
1633 TABLE_NODE,
1634 "", /* This node has no interface. */
1635 1
1636};
1637
1638/* Only display ip forwarding is enabled or not. */
1639DEFUN (show_ip_forwarding,
1640 show_ip_forwarding_cmd,
1641 "show ip forwarding",
1642 SHOW_STR
1643 IP_STR
1644 "IP forwarding status\n")
1645{
1646 int ret;
1647
1648 ret = ipforward ();
1649
1650 if (ret == 0)
1651 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1652 else
1653 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1654 return CMD_SUCCESS;
1655}
1656
1657#ifdef HAVE_IPV6
1658/* Only display ipv6 forwarding is enabled or not. */
1659DEFUN (show_ipv6_forwarding,
1660 show_ipv6_forwarding_cmd,
1661 "show ipv6 forwarding",
1662 SHOW_STR
1663 "IPv6 information\n"
1664 "Forwarding status\n")
1665{
1666 int ret;
1667
1668 ret = ipforward_ipv6 ();
1669
1670 switch (ret)
1671 {
1672 case -1:
1673 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1674 break;
1675 case 0:
1676 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1677 break;
1678 case 1:
1679 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1680 break;
1681 default:
1682 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1683 break;
1684 }
1685 return CMD_SUCCESS;
1686}
1687
hasso55906722004-02-11 22:42:16 +00001688DEFUN (ipv6_forwarding,
1689 ipv6_forwarding_cmd,
1690 "ipv6 forwarding",
1691 IPV6_STR
1692 "Turn on IPv6 forwarding")
1693{
1694 int ret;
1695
hasso41d3fc92004-04-06 11:59:00 +00001696 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001697 if (ret == 0)
1698 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001699
hasso41d3fc92004-04-06 11:59:00 +00001700 if (ret == 0)
1701 {
hasso55906722004-02-11 22:42:16 +00001702 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1703 return CMD_WARNING;
1704 }
1705
1706 return CMD_SUCCESS;
1707}
1708
paul718e3742002-12-13 20:15:29 +00001709DEFUN (no_ipv6_forwarding,
1710 no_ipv6_forwarding_cmd,
1711 "no ipv6 forwarding",
1712 NO_STR
hasso55906722004-02-11 22:42:16 +00001713 IPV6_STR
1714 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001715{
1716 int ret;
1717
hasso41d3fc92004-04-06 11:59:00 +00001718 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001719 if (ret != 0)
1720 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001721
paul718e3742002-12-13 20:15:29 +00001722 if (ret != 0)
1723 {
1724 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1725 return CMD_WARNING;
1726 }
1727
1728 return CMD_SUCCESS;
1729}
1730
1731#endif /* HAVE_IPV6 */
1732
1733/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001734static int
paul718e3742002-12-13 20:15:29 +00001735config_write_forwarding (struct vty *vty)
1736{
hasso18a6dce2004-10-03 18:18:34 +00001737 /* FIXME: Find better place for that. */
1738 router_id_write (vty);
1739
paul3e0b3a52004-08-23 18:58:32 +00001740 if (ipforward ())
1741 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001742#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001743 if (ipforward_ipv6 ())
1744 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001745#endif /* HAVE_IPV6 */
1746 vty_out (vty, "!%s", VTY_NEWLINE);
1747 return 0;
1748}
1749
1750/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001751static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001752{
1753 FORWARDING_NODE,
1754 "", /* This node has no interface. */
1755 1
1756};
1757
1758
1759/* Initialisation of zebra and installation of commands. */
1760void
paula1ac18c2005-06-28 17:17:12 +00001761zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001762{
1763 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001764 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001765
paul718e3742002-12-13 20:15:29 +00001766 /* Install configuration write function. */
1767 install_node (&table_node, config_write_table);
1768 install_node (&forwarding_node, config_write_forwarding);
1769
1770 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1771 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001772 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001773 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1774 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1775
1776#ifdef HAVE_NETLINK
1777 install_element (VIEW_NODE, &show_table_cmd);
1778 install_element (ENABLE_NODE, &show_table_cmd);
1779 install_element (CONFIG_NODE, &config_table_cmd);
1780#endif /* HAVE_NETLINK */
1781
1782#ifdef HAVE_IPV6
1783 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1784 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001785 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001786 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1787#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001788
1789 /* Route-map */
1790 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001791}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001792
1793/* Make zebra server socket, wiping any existing one (see bug #403). */
1794void
Vyacheslav Trushkin271ee732011-11-25 18:51:48 +04001795zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001796{
1797#ifdef HAVE_TCP_ZEBRA
1798 zebra_serv ();
1799#else
Vyacheslav Trushkin271ee732011-11-25 18:51:48 +04001800 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001801#endif /* HAVE_TCP_ZEBRA */
1802}