blob: 672dee881d6d02595cd4270722b9ce485e519166 [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 Trushkin2ea1ab12011-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;
G.Balajicddf3912011-11-26 21:59:32 +0400753 safi_t safi;
754
paul718e3742002-12-13 20:15:29 +0000755
756 /* Get input stream. */
757 s = client->ibuf;
758
759 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000760 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
761
paul718e3742002-12-13 20:15:29 +0000762 /* Type, flags, message. */
763 rib->type = stream_getc (s);
764 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000765 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400766 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000767 rib->uptime = time (NULL);
768
769 /* IPv4 prefix. */
770 memset (&p, 0, sizeof (struct prefix_ipv4));
771 p.family = AF_INET;
772 p.prefixlen = stream_getc (s);
773 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
774
775 /* Nexthop parse. */
776 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
777 {
778 nexthop_num = stream_getc (s);
779
780 for (i = 0; i < nexthop_num; i++)
781 {
782 nexthop_type = stream_getc (s);
783
784 switch (nexthop_type)
785 {
786 case ZEBRA_NEXTHOP_IFINDEX:
787 ifindex = stream_getl (s);
788 nexthop_ifindex_add (rib, ifindex);
789 break;
790 case ZEBRA_NEXTHOP_IFNAME:
791 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000792 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000793 break;
794 case ZEBRA_NEXTHOP_IPV4:
795 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000796 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000797 break;
798 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000799 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000800 break;
paul595db7f2003-05-25 21:35:06 +0000801 case ZEBRA_NEXTHOP_BLACKHOLE:
802 nexthop_blackhole_add (rib);
803 break;
paul718e3742002-12-13 20:15:29 +0000804 }
805 }
806 }
807
808 /* Distance. */
809 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
810 rib->distance = stream_getc (s);
811
812 /* Metric. */
813 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
814 rib->metric = stream_getl (s);
815
Paul Jakma171eee32006-07-27 16:11:02 +0000816 /* Table */
817 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400818 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000819 return 0;
paul718e3742002-12-13 20:15:29 +0000820}
821
822/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000823static int
paul718e3742002-12-13 20:15:29 +0000824zread_ipv4_delete (struct zserv *client, u_short length)
825{
826 int i;
827 struct stream *s;
828 struct zapi_ipv4 api;
829 struct in_addr nexthop;
830 unsigned long ifindex;
831 struct prefix_ipv4 p;
832 u_char nexthop_num;
833 u_char nexthop_type;
834 u_char ifname_len;
835
836 s = client->ibuf;
837 ifindex = 0;
838 nexthop.s_addr = 0;
839
840 /* Type, flags, message. */
841 api.type = stream_getc (s);
842 api.flags = stream_getc (s);
843 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400844 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000845
846 /* IPv4 prefix. */
847 memset (&p, 0, sizeof (struct prefix_ipv4));
848 p.family = AF_INET;
849 p.prefixlen = stream_getc (s);
850 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
851
852 /* Nexthop, ifindex, distance, metric. */
853 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
854 {
855 nexthop_num = stream_getc (s);
856
857 for (i = 0; i < nexthop_num; i++)
858 {
859 nexthop_type = stream_getc (s);
860
861 switch (nexthop_type)
862 {
863 case ZEBRA_NEXTHOP_IFINDEX:
864 ifindex = stream_getl (s);
865 break;
866 case ZEBRA_NEXTHOP_IFNAME:
867 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000868 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000869 break;
870 case ZEBRA_NEXTHOP_IPV4:
871 nexthop.s_addr = stream_get_ipv4 (s);
872 break;
873 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000874 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000875 break;
876 }
877 }
878 }
879
880 /* Distance. */
881 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
882 api.distance = stream_getc (s);
883 else
884 api.distance = 0;
885
886 /* Metric. */
887 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
888 api.metric = stream_getl (s);
889 else
890 api.metric = 0;
891
892 rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400893 client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +0000894 return 0;
paul718e3742002-12-13 20:15:29 +0000895}
896
897/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000898static int
paul718e3742002-12-13 20:15:29 +0000899zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
900{
901 struct in_addr addr;
902
903 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000904 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000905}
906
907/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000908static int
paul718e3742002-12-13 20:15:29 +0000909zread_ipv4_import_lookup (struct zserv *client, u_short length)
910{
911 struct prefix_ipv4 p;
912
913 p.family = AF_INET;
914 p.prefixlen = stream_getc (client->ibuf);
915 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
916
ajs719e9742005-02-28 20:52:15 +0000917 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000918}
919
920#ifdef HAVE_IPV6
921/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000922static int
paul718e3742002-12-13 20:15:29 +0000923zread_ipv6_add (struct zserv *client, u_short length)
924{
925 int i;
926 struct stream *s;
927 struct zapi_ipv6 api;
928 struct in6_addr nexthop;
929 unsigned long ifindex;
930 struct prefix_ipv6 p;
931
932 s = client->ibuf;
933 ifindex = 0;
934 memset (&nexthop, 0, sizeof (struct in6_addr));
935
936 /* Type, flags, message. */
937 api.type = stream_getc (s);
938 api.flags = stream_getc (s);
939 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +0400940 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000941
942 /* IPv4 prefix. */
943 memset (&p, 0, sizeof (struct prefix_ipv6));
944 p.family = AF_INET6;
945 p.prefixlen = stream_getc (s);
946 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
947
948 /* Nexthop, ifindex, distance, metric. */
949 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
950 {
951 u_char nexthop_type;
952
953 api.nexthop_num = stream_getc (s);
954 for (i = 0; i < api.nexthop_num; i++)
955 {
956 nexthop_type = stream_getc (s);
957
958 switch (nexthop_type)
959 {
960 case ZEBRA_NEXTHOP_IPV6:
961 stream_get (&nexthop, s, 16);
962 break;
963 case ZEBRA_NEXTHOP_IFINDEX:
964 ifindex = stream_getl (s);
965 break;
966 }
967 }
968 }
969
970 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
971 api.distance = stream_getc (s);
972 else
973 api.distance = 0;
974
975 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
976 api.metric = stream_getl (s);
977 else
978 api.metric = 0;
979
980 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100981 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400982 api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +0000983 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100984 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400985 api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +0000986 return 0;
paul718e3742002-12-13 20:15:29 +0000987}
988
989/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000990static int
paul718e3742002-12-13 20:15:29 +0000991zread_ipv6_delete (struct zserv *client, u_short length)
992{
993 int i;
994 struct stream *s;
995 struct zapi_ipv6 api;
996 struct in6_addr nexthop;
997 unsigned long ifindex;
998 struct prefix_ipv6 p;
999
1000 s = client->ibuf;
1001 ifindex = 0;
1002 memset (&nexthop, 0, sizeof (struct in6_addr));
1003
1004 /* Type, flags, message. */
1005 api.type = stream_getc (s);
1006 api.flags = stream_getc (s);
1007 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001008 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001009
1010 /* IPv4 prefix. */
1011 memset (&p, 0, sizeof (struct prefix_ipv6));
1012 p.family = AF_INET6;
1013 p.prefixlen = stream_getc (s);
1014 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1015
1016 /* Nexthop, ifindex, distance, metric. */
1017 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1018 {
1019 u_char nexthop_type;
1020
1021 api.nexthop_num = stream_getc (s);
1022 for (i = 0; i < api.nexthop_num; i++)
1023 {
1024 nexthop_type = stream_getc (s);
1025
1026 switch (nexthop_type)
1027 {
1028 case ZEBRA_NEXTHOP_IPV6:
1029 stream_get (&nexthop, s, 16);
1030 break;
1031 case ZEBRA_NEXTHOP_IFINDEX:
1032 ifindex = stream_getl (s);
1033 break;
1034 }
1035 }
1036 }
1037
1038 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1039 api.distance = stream_getc (s);
1040 else
1041 api.distance = 0;
1042 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1043 api.metric = stream_getl (s);
1044 else
1045 api.metric = 0;
1046
1047 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
G.Balajif768f362011-11-26 22:10:39 +04001048 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
paul718e3742002-12-13 20:15:29 +00001049 else
G.Balajif768f362011-11-26 22:10:39 +04001050 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +00001051 return 0;
paul718e3742002-12-13 20:15:29 +00001052}
1053
ajs719e9742005-02-28 20:52:15 +00001054static int
paul718e3742002-12-13 20:15:29 +00001055zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1056{
1057 struct in6_addr addr;
1058 char buf[BUFSIZ];
1059
1060 stream_get (&addr, client->ibuf, 16);
1061 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1062
ajs719e9742005-02-28 20:52:15 +00001063 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001064}
1065#endif /* HAVE_IPV6 */
1066
hasso18a6dce2004-10-03 18:18:34 +00001067/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001068static int
hasso18a6dce2004-10-03 18:18:34 +00001069zread_router_id_add (struct zserv *client, u_short length)
1070{
1071 struct prefix p;
1072
1073 /* Router-id information is needed. */
1074 client->ridinfo = 1;
1075
1076 router_id_get (&p);
1077
ajs719e9742005-02-28 20:52:15 +00001078 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001079}
1080
1081/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001082static int
hasso18a6dce2004-10-03 18:18:34 +00001083zread_router_id_delete (struct zserv *client, u_short length)
1084{
1085 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001086 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001087}
1088
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001089/* Tie up route-type and client->sock */
1090static void
1091zread_hello (struct zserv *client)
1092{
1093 /* type of protocol (lib/zebra.h) */
1094 u_char proto;
1095 proto = stream_getc (client->ibuf);
1096
1097 /* accept only dynamic routing protocols */
1098 if ((proto < ZEBRA_ROUTE_MAX)
1099 && (proto > ZEBRA_ROUTE_STATIC))
1100 {
1101 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1102 client->sock, zebra_route_string(proto));
1103
1104 /* if route-type was binded by other client */
1105 if (route_type_oaths[proto])
1106 zlog_warn ("sender of %s routes changed %c->%c",
1107 zebra_route_string(proto), route_type_oaths[proto],
1108 client->sock);
1109
1110 route_type_oaths[proto] = client->sock;
1111 }
1112}
1113
1114/* If client sent routes of specific type, zebra removes it
1115 * and returns number of deleted routes.
1116 */
1117static void
1118zebra_score_rib (int client_sock)
1119{
1120 int i;
1121
1122 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1123 if (client_sock == route_type_oaths[i])
1124 {
1125 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1126 client_sock, rib_score_proto (i), zebra_route_string (i));
1127 route_type_oaths[i] = 0;
1128 break;
1129 }
1130}
1131
paul718e3742002-12-13 20:15:29 +00001132/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001133static void
paul718e3742002-12-13 20:15:29 +00001134zebra_client_close (struct zserv *client)
1135{
1136 /* Close file descriptor. */
1137 if (client->sock)
1138 {
1139 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001140 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001141 client->sock = -1;
1142 }
1143
1144 /* Free stream buffers. */
1145 if (client->ibuf)
1146 stream_free (client->ibuf);
1147 if (client->obuf)
1148 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001149 if (client->wb)
1150 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001151
1152 /* Release threads. */
1153 if (client->t_read)
1154 thread_cancel (client->t_read);
1155 if (client->t_write)
1156 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001157 if (client->t_suicide)
1158 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001159
1160 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001161 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001162 XFREE (0, client);
1163}
1164
1165/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001166static void
paul718e3742002-12-13 20:15:29 +00001167zebra_client_create (int sock)
1168{
1169 struct zserv *client;
1170
1171 client = XCALLOC (0, sizeof (struct zserv));
1172
1173 /* Make client input/output buffer. */
1174 client->sock = sock;
1175 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1176 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001177 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001178
1179 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001180 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001181
1182 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001183 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001184
1185 /* Make new read thread. */
1186 zebra_event (ZEBRA_READ, sock, client);
1187}
1188
1189/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001190static int
paul718e3742002-12-13 20:15:29 +00001191zebra_client_read (struct thread *thread)
1192{
1193 int sock;
1194 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001195 size_t already;
paulc1b98002006-01-16 01:54:02 +00001196 uint16_t length, command;
1197 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001198
1199 /* Get thread data. Reset reading thread because I'm running. */
1200 sock = THREAD_FD (thread);
1201 client = THREAD_ARG (thread);
1202 client->t_read = NULL;
1203
ajs719e9742005-02-28 20:52:15 +00001204 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001205 {
ajs719e9742005-02-28 20:52:15 +00001206 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001207 return -1;
1208 }
ajs719e9742005-02-28 20:52:15 +00001209
1210 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001211 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001212 {
ajs57a14772005-04-10 15:01:56 +00001213 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001214 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001215 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001216 (nbyte == -1))
1217 {
1218 if (IS_ZEBRA_DEBUG_EVENT)
1219 zlog_debug ("connection closed socket [%d]", sock);
1220 zebra_client_close (client);
1221 return -1;
1222 }
ajs57a14772005-04-10 15:01:56 +00001223 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001224 {
1225 /* Try again later. */
1226 zebra_event (ZEBRA_READ, sock, client);
1227 return 0;
1228 }
ajs57a14772005-04-10 15:01:56 +00001229 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001230 }
1231
1232 /* Reset to read from the beginning of the incoming packet. */
1233 stream_set_getp(client->ibuf, 0);
1234
paulc1b98002006-01-16 01:54:02 +00001235 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001236 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001237 marker = stream_getc (client->ibuf);
1238 version = stream_getc (client->ibuf);
1239 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001240
paulc1b98002006-01-16 01:54:02 +00001241 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1242 {
1243 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1244 __func__, sock, marker, version);
1245 zebra_client_close (client);
1246 return -1;
1247 }
ajs719e9742005-02-28 20:52:15 +00001248 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001249 {
ajs57a14772005-04-10 15:01:56 +00001250 zlog_warn("%s: socket %d message length %u is less than header size %d",
1251 __func__, sock, length, ZEBRA_HEADER_SIZE);
1252 zebra_client_close (client);
1253 return -1;
1254 }
1255 if (length > STREAM_SIZE(client->ibuf))
1256 {
1257 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1258 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001259 zebra_client_close (client);
1260 return -1;
1261 }
1262
paul718e3742002-12-13 20:15:29 +00001263 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001264 if (already < length)
paul718e3742002-12-13 20:15:29 +00001265 {
ajs57a14772005-04-10 15:01:56 +00001266 ssize_t nbyte;
1267 if (((nbyte = stream_read_try (client->ibuf, sock,
1268 length-already)) == 0) ||
1269 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001270 {
1271 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001272 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001273 zebra_client_close (client);
1274 return -1;
1275 }
ajs57a14772005-04-10 15:01:56 +00001276 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001277 {
1278 /* Try again later. */
1279 zebra_event (ZEBRA_READ, sock, client);
1280 return 0;
1281 }
paul718e3742002-12-13 20:15:29 +00001282 }
1283
ajs719e9742005-02-28 20:52:15 +00001284 length -= ZEBRA_HEADER_SIZE;
1285
paul718e3742002-12-13 20:15:29 +00001286 /* Debug packet information. */
1287 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001288 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001289
1290 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001291 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001292 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001293
1294 switch (command)
1295 {
hasso18a6dce2004-10-03 18:18:34 +00001296 case ZEBRA_ROUTER_ID_ADD:
1297 zread_router_id_add (client, length);
1298 break;
1299 case ZEBRA_ROUTER_ID_DELETE:
1300 zread_router_id_delete (client, length);
1301 break;
paul718e3742002-12-13 20:15:29 +00001302 case ZEBRA_INTERFACE_ADD:
1303 zread_interface_add (client, length);
1304 break;
1305 case ZEBRA_INTERFACE_DELETE:
1306 zread_interface_delete (client, length);
1307 break;
1308 case ZEBRA_IPV4_ROUTE_ADD:
1309 zread_ipv4_add (client, length);
1310 break;
1311 case ZEBRA_IPV4_ROUTE_DELETE:
1312 zread_ipv4_delete (client, length);
1313 break;
1314#ifdef HAVE_IPV6
1315 case ZEBRA_IPV6_ROUTE_ADD:
1316 zread_ipv6_add (client, length);
1317 break;
1318 case ZEBRA_IPV6_ROUTE_DELETE:
1319 zread_ipv6_delete (client, length);
1320 break;
1321#endif /* HAVE_IPV6 */
1322 case ZEBRA_REDISTRIBUTE_ADD:
1323 zebra_redistribute_add (command, client, length);
1324 break;
1325 case ZEBRA_REDISTRIBUTE_DELETE:
1326 zebra_redistribute_delete (command, client, length);
1327 break;
1328 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1329 zebra_redistribute_default_add (command, client, length);
1330 break;
1331 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1332 zebra_redistribute_default_delete (command, client, length);
1333 break;
1334 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1335 zread_ipv4_nexthop_lookup (client, length);
1336 break;
1337#ifdef HAVE_IPV6
1338 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1339 zread_ipv6_nexthop_lookup (client, length);
1340 break;
1341#endif /* HAVE_IPV6 */
1342 case ZEBRA_IPV4_IMPORT_LOOKUP:
1343 zread_ipv4_import_lookup (client, length);
1344 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001345 case ZEBRA_HELLO:
1346 zread_hello (client);
1347 break;
paul718e3742002-12-13 20:15:29 +00001348 default:
1349 zlog_info ("Zebra received unknown command %d", command);
1350 break;
1351 }
1352
ajs719e9742005-02-28 20:52:15 +00001353 if (client->t_suicide)
1354 {
1355 /* No need to wait for thread callback, just kill immediately. */
1356 zebra_client_close(client);
1357 return -1;
1358 }
1359
paul718e3742002-12-13 20:15:29 +00001360 stream_reset (client->ibuf);
1361 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001362 return 0;
1363}
1364
paul718e3742002-12-13 20:15:29 +00001365
1366/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001367static int
paul718e3742002-12-13 20:15:29 +00001368zebra_accept (struct thread *thread)
1369{
1370 int accept_sock;
1371 int client_sock;
1372 struct sockaddr_in client;
1373 socklen_t len;
1374
1375 accept_sock = THREAD_FD (thread);
1376
ajs719e9742005-02-28 20:52:15 +00001377 /* Reregister myself. */
1378 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1379
paul718e3742002-12-13 20:15:29 +00001380 len = sizeof (struct sockaddr_in);
1381 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1382
1383 if (client_sock < 0)
1384 {
ajs6099b3b2004-11-20 02:06:59 +00001385 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001386 return -1;
1387 }
1388
paulccf35572003-03-01 11:42:20 +00001389 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001390 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001391
paul718e3742002-12-13 20:15:29 +00001392 /* Create new zebra client. */
1393 zebra_client_create (client_sock);
1394
paul718e3742002-12-13 20:15:29 +00001395 return 0;
1396}
1397
paulb9df2d22004-05-09 09:09:59 +00001398#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001399/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001400static void
paul718e3742002-12-13 20:15:29 +00001401zebra_serv ()
1402{
1403 int ret;
1404 int accept_sock;
1405 struct sockaddr_in addr;
1406
1407 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1408
1409 if (accept_sock < 0)
1410 {
paul3d1dc852005-04-05 00:45:23 +00001411 zlog_warn ("Can't create zserv stream socket: %s",
1412 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001413 zlog_warn ("zebra can't provice full functionality due to above error");
1414 return;
1415 }
1416
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001417 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001418 memset (&addr, 0, sizeof (struct sockaddr_in));
1419 addr.sin_family = AF_INET;
1420 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001421#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001422 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001423#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001424 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1425
1426 sockopt_reuseaddr (accept_sock);
1427 sockopt_reuseport (accept_sock);
1428
pauledd7c242003-06-04 13:59:38 +00001429 if ( zserv_privs.change(ZPRIVS_RAISE) )
1430 zlog (NULL, LOG_ERR, "Can't raise privileges");
1431
paul718e3742002-12-13 20:15:29 +00001432 ret = bind (accept_sock, (struct sockaddr *)&addr,
1433 sizeof (struct sockaddr_in));
1434 if (ret < 0)
1435 {
paul3d1dc852005-04-05 00:45:23 +00001436 zlog_warn ("Can't bind to stream socket: %s",
1437 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001438 zlog_warn ("zebra can't provice full functionality due to above error");
1439 close (accept_sock); /* Avoid sd leak. */
1440 return;
1441 }
pauledd7c242003-06-04 13:59:38 +00001442
1443 if ( zserv_privs.change(ZPRIVS_LOWER) )
1444 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001445
1446 ret = listen (accept_sock, 1);
1447 if (ret < 0)
1448 {
paul3d1dc852005-04-05 00:45:23 +00001449 zlog_warn ("Can't listen to stream socket: %s",
1450 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001451 zlog_warn ("zebra can't provice full functionality due to above error");
1452 close (accept_sock); /* Avoid sd leak. */
1453 return;
1454 }
1455
1456 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1457}
paulb9df2d22004-05-09 09:09:59 +00001458#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001459
1460/* For sockaddr_un. */
1461#include <sys/un.h>
1462
1463/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001464static void
hassofce954f2004-10-07 20:29:24 +00001465zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001466{
1467 int ret;
1468 int sock, len;
1469 struct sockaddr_un serv;
1470 mode_t old_mask;
1471
1472 /* First of all, unlink existing socket */
1473 unlink (path);
1474
1475 /* Set umask */
1476 old_mask = umask (0077);
1477
1478 /* Make UNIX domain socket. */
1479 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1480 if (sock < 0)
1481 {
paul3d1dc852005-04-05 00:45:23 +00001482 zlog_warn ("Can't create zserv unix socket: %s",
1483 safe_strerror (errno));
1484 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001485 return;
1486 }
1487
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001488 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1489
paul718e3742002-12-13 20:15:29 +00001490 /* Make server socket. */
1491 memset (&serv, 0, sizeof (struct sockaddr_un));
1492 serv.sun_family = AF_UNIX;
1493 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001494#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001495 len = serv.sun_len = SUN_LEN(&serv);
1496#else
1497 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001498#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001499
1500 ret = bind (sock, (struct sockaddr *) &serv, len);
1501 if (ret < 0)
1502 {
paul3d1dc852005-04-05 00:45:23 +00001503 zlog_warn ("Can't bind to unix socket %s: %s",
1504 path, safe_strerror (errno));
1505 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001506 close (sock);
1507 return;
1508 }
1509
1510 ret = listen (sock, 5);
1511 if (ret < 0)
1512 {
paul3d1dc852005-04-05 00:45:23 +00001513 zlog_warn ("Can't listen to unix socket %s: %s",
1514 path, safe_strerror (errno));
1515 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001516 close (sock);
1517 return;
1518 }
1519
1520 umask (old_mask);
1521
1522 zebra_event (ZEBRA_SERV, sock, NULL);
1523}
1524
paul718e3742002-12-13 20:15:29 +00001525
paulb9df2d22004-05-09 09:09:59 +00001526static void
paul718e3742002-12-13 20:15:29 +00001527zebra_event (enum event event, int sock, struct zserv *client)
1528{
1529 switch (event)
1530 {
1531 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001532 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001533 break;
1534 case ZEBRA_READ:
1535 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001536 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001537 break;
1538 case ZEBRA_WRITE:
1539 /**/
1540 break;
1541 }
1542}
1543
1544/* Display default rtm_table for all clients. */
1545DEFUN (show_table,
1546 show_table_cmd,
1547 "show table",
1548 SHOW_STR
1549 "default routing table to use for all clients\n")
1550{
paulb21b19c2003-06-15 01:28:29 +00001551 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001552 VTY_NEWLINE);
1553 return CMD_SUCCESS;
1554}
1555
1556DEFUN (config_table,
1557 config_table_cmd,
1558 "table TABLENO",
1559 "Configure target kernel routing table\n"
1560 "TABLE integer\n")
1561{
paulb21b19c2003-06-15 01:28:29 +00001562 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001563 return CMD_SUCCESS;
1564}
1565
hasso647e4f12003-05-25 11:43:52 +00001566DEFUN (ip_forwarding,
1567 ip_forwarding_cmd,
1568 "ip forwarding",
1569 IP_STR
1570 "Turn on IP forwarding")
1571{
1572 int ret;
1573
1574 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001575 if (ret == 0)
1576 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001577
hasso647e4f12003-05-25 11:43:52 +00001578 if (ret == 0)
1579 {
1580 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1581 return CMD_WARNING;
1582 }
1583
1584 return CMD_SUCCESS;
1585}
1586
paul718e3742002-12-13 20:15:29 +00001587DEFUN (no_ip_forwarding,
1588 no_ip_forwarding_cmd,
1589 "no ip forwarding",
1590 NO_STR
1591 IP_STR
1592 "Turn off IP forwarding")
1593{
1594 int ret;
1595
1596 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001597 if (ret != 0)
1598 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001599
paul718e3742002-12-13 20:15:29 +00001600 if (ret != 0)
1601 {
1602 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1603 return CMD_WARNING;
1604 }
1605
1606 return CMD_SUCCESS;
1607}
1608
1609/* This command is for debugging purpose. */
1610DEFUN (show_zebra_client,
1611 show_zebra_client_cmd,
1612 "show zebra client",
1613 SHOW_STR
1614 "Zebra information"
1615 "Client information")
1616{
hasso52dc7ee2004-09-23 19:18:23 +00001617 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001618 struct zserv *client;
1619
paul1eb8ef22005-04-07 07:30:20 +00001620 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1621 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1622
paul718e3742002-12-13 20:15:29 +00001623 return CMD_SUCCESS;
1624}
1625
1626/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001627static int
paul718e3742002-12-13 20:15:29 +00001628config_write_table (struct vty *vty)
1629{
paulb21b19c2003-06-15 01:28:29 +00001630 if (zebrad.rtm_table_default)
1631 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001632 VTY_NEWLINE);
1633 return 0;
1634}
1635
1636/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001637static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001638{
1639 TABLE_NODE,
1640 "", /* This node has no interface. */
1641 1
1642};
1643
1644/* Only display ip forwarding is enabled or not. */
1645DEFUN (show_ip_forwarding,
1646 show_ip_forwarding_cmd,
1647 "show ip forwarding",
1648 SHOW_STR
1649 IP_STR
1650 "IP forwarding status\n")
1651{
1652 int ret;
1653
1654 ret = ipforward ();
1655
1656 if (ret == 0)
1657 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1658 else
1659 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1660 return CMD_SUCCESS;
1661}
1662
1663#ifdef HAVE_IPV6
1664/* Only display ipv6 forwarding is enabled or not. */
1665DEFUN (show_ipv6_forwarding,
1666 show_ipv6_forwarding_cmd,
1667 "show ipv6 forwarding",
1668 SHOW_STR
1669 "IPv6 information\n"
1670 "Forwarding status\n")
1671{
1672 int ret;
1673
1674 ret = ipforward_ipv6 ();
1675
1676 switch (ret)
1677 {
1678 case -1:
1679 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1680 break;
1681 case 0:
1682 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1683 break;
1684 case 1:
1685 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1686 break;
1687 default:
1688 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1689 break;
1690 }
1691 return CMD_SUCCESS;
1692}
1693
hasso55906722004-02-11 22:42:16 +00001694DEFUN (ipv6_forwarding,
1695 ipv6_forwarding_cmd,
1696 "ipv6 forwarding",
1697 IPV6_STR
1698 "Turn on IPv6 forwarding")
1699{
1700 int ret;
1701
hasso41d3fc92004-04-06 11:59:00 +00001702 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001703 if (ret == 0)
1704 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001705
hasso41d3fc92004-04-06 11:59:00 +00001706 if (ret == 0)
1707 {
hasso55906722004-02-11 22:42:16 +00001708 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1709 return CMD_WARNING;
1710 }
1711
1712 return CMD_SUCCESS;
1713}
1714
paul718e3742002-12-13 20:15:29 +00001715DEFUN (no_ipv6_forwarding,
1716 no_ipv6_forwarding_cmd,
1717 "no ipv6 forwarding",
1718 NO_STR
hasso55906722004-02-11 22:42:16 +00001719 IPV6_STR
1720 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001721{
1722 int ret;
1723
hasso41d3fc92004-04-06 11:59:00 +00001724 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001725 if (ret != 0)
1726 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001727
paul718e3742002-12-13 20:15:29 +00001728 if (ret != 0)
1729 {
1730 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1731 return CMD_WARNING;
1732 }
1733
1734 return CMD_SUCCESS;
1735}
1736
1737#endif /* HAVE_IPV6 */
1738
1739/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001740static int
paul718e3742002-12-13 20:15:29 +00001741config_write_forwarding (struct vty *vty)
1742{
hasso18a6dce2004-10-03 18:18:34 +00001743 /* FIXME: Find better place for that. */
1744 router_id_write (vty);
1745
paul3e0b3a52004-08-23 18:58:32 +00001746 if (ipforward ())
1747 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001748#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001749 if (ipforward_ipv6 ())
1750 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001751#endif /* HAVE_IPV6 */
1752 vty_out (vty, "!%s", VTY_NEWLINE);
1753 return 0;
1754}
1755
1756/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001757static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001758{
1759 FORWARDING_NODE,
1760 "", /* This node has no interface. */
1761 1
1762};
1763
1764
1765/* Initialisation of zebra and installation of commands. */
1766void
paula1ac18c2005-06-28 17:17:12 +00001767zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001768{
1769 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001770 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001771
paul718e3742002-12-13 20:15:29 +00001772 /* Install configuration write function. */
1773 install_node (&table_node, config_write_table);
1774 install_node (&forwarding_node, config_write_forwarding);
1775
1776 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1777 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001778 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001779 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1780 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1781
1782#ifdef HAVE_NETLINK
1783 install_element (VIEW_NODE, &show_table_cmd);
1784 install_element (ENABLE_NODE, &show_table_cmd);
1785 install_element (CONFIG_NODE, &config_table_cmd);
1786#endif /* HAVE_NETLINK */
1787
1788#ifdef HAVE_IPV6
1789 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1790 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001791 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001792 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1793#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001794
1795 /* Route-map */
1796 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001797}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001798
1799/* Make zebra server socket, wiping any existing one (see bug #403). */
1800void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001801zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001802{
1803#ifdef HAVE_TCP_ZEBRA
1804 zebra_serv ();
1805#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001806 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001807#endif /* HAVE_TCP_ZEBRA */
1808}