blob: 8aaaea6f1310f673e807eb49162dbad089890d69 [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
Josh Bailey51d4ef82012-03-21 17:13:39 -0700143static void
144zserv_encode_interface (struct stream *s, struct interface *ifp)
145{
146 /* Interface information. */
147 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
148 stream_putl (s, ifp->ifindex);
149 stream_putc (s, ifp->status);
150 stream_putq (s, ifp->flags);
151 stream_putl (s, ifp->metric);
152 stream_putl (s, ifp->mtu);
153 stream_putl (s, ifp->mtu6);
154 stream_putl (s, ifp->bandwidth);
155#ifdef HAVE_STRUCT_SOCKADDR_DL
David Lamparterca3ccd82012-09-26 14:52:39 +0200156 stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage));
Josh Bailey51d4ef82012-03-21 17:13:39 -0700157#else
158 stream_putl (s, ifp->hw_addr_len);
159 if (ifp->hw_addr_len)
160 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
161#endif /* HAVE_STRUCT_SOCKADDR_DL */
162
163 /* Write packet size. */
164 stream_putw_at (s, 0, stream_get_endp (s));
165}
166
paul718e3742002-12-13 20:15:29 +0000167/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000168/*
169 * This function is called in the following situations:
170 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
171 * from the client.
172 * - at startup, when zebra figures out the available interfaces
173 * - when an interface is added (where support for
174 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
175 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
176 * received)
177 */
paul718e3742002-12-13 20:15:29 +0000178int
179zsend_interface_add (struct zserv *client, struct interface *ifp)
180{
181 struct stream *s;
182
183 /* Check this client need interface information. */
184 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000185 return 0;
paul718e3742002-12-13 20:15:29 +0000186
187 s = client->obuf;
188 stream_reset (s);
189
paulc1b98002006-01-16 01:54:02 +0000190 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700191 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000192
ajs719e9742005-02-28 20:52:15 +0000193 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000194}
195
196/* Interface deletion from zebra daemon. */
197int
198zsend_interface_delete (struct zserv *client, struct interface *ifp)
199{
200 struct stream *s;
201
202 /* Check this client need interface information. */
203 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000204 return 0;
paul718e3742002-12-13 20:15:29 +0000205
206 s = client->obuf;
207 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000208
Josh Bailey51d4ef82012-03-21 17:13:39 -0700209 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
210 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000211
ajs719e9742005-02-28 20:52:15 +0000212 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000213}
214
paulb9df2d22004-05-09 09:09:59 +0000215/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
216 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
217 *
218 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
219 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
220 * from the client, after the ZEBRA_INTERFACE_ADD has been
221 * sent from zebra to the client
222 * - redistribute new address info to all clients in the following situations
223 * - at startup, when zebra figures out the available interfaces
224 * - when an interface is added (where support for
225 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
226 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
227 * received)
228 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
229 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
230 * - when an RTM_NEWADDR message is received from the kernel,
231 *
232 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
233 *
234 * zsend_interface_address(DELETE)
235 * ^
236 * |
237 * zebra_interface_address_delete_update
238 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000239 * | | if_delete_update
240 * | |
paulb9df2d22004-05-09 09:09:59 +0000241 * ip_address_uninstall connected_delete_ipv4
242 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
243 * ^ ^
244 * | |
245 * | RTM_NEWADDR on routing/netlink socket
246 * |
247 * vty commands:
248 * "no ip address A.B.C.D/M [label LINE]"
249 * "no ip address A.B.C.D/M secondary"
250 * ["no ipv6 address X:X::X:X/M"]
251 *
252 */
paul718e3742002-12-13 20:15:29 +0000253int
paulb9df2d22004-05-09 09:09:59 +0000254zsend_interface_address (int cmd, struct zserv *client,
255 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000256{
257 int blen;
258 struct stream *s;
259 struct prefix *p;
260
261 /* Check this client need interface information. */
262 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000263 return 0;
paul718e3742002-12-13 20:15:29 +0000264
265 s = client->obuf;
266 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000267
268 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000269 stream_putl (s, ifp->ifindex);
270
271 /* Interface address flag. */
272 stream_putc (s, ifc->flags);
273
274 /* Prefix information. */
275 p = ifc->address;
276 stream_putc (s, p->family);
277 blen = prefix_blen (p);
278 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000279
280 /*
281 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
282 * but zebra_interface_address_delete_read() in the gnu version
283 * expects to find it
284 */
paul718e3742002-12-13 20:15:29 +0000285 stream_putc (s, p->prefixlen);
286
287 /* Destination. */
288 p = ifc->destination;
289 if (p)
290 stream_put (s, &p->u.prefix, blen);
291 else
292 stream_put (s, NULL, blen);
293
294 /* Write packet size. */
295 stream_putw_at (s, 0, stream_get_endp (s));
296
ajs719e9742005-02-28 20:52:15 +0000297 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000298}
299
paulb9df2d22004-05-09 09:09:59 +0000300/*
301 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
302 * ZEBRA_INTERFACE_DOWN.
303 *
304 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
305 * the clients in one of 2 situations:
306 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
307 * - a vty command modifying the bandwidth of an interface is received.
308 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
309 */
paul718e3742002-12-13 20:15:29 +0000310int
paulb9df2d22004-05-09 09:09:59 +0000311zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000312{
313 struct stream *s;
314
315 /* Check this client need interface information. */
316 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000317 return 0;
paul718e3742002-12-13 20:15:29 +0000318
319 s = client->obuf;
320 stream_reset (s);
321
paulc1b98002006-01-16 01:54:02 +0000322 zserv_create_header (s, cmd);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700323 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000324
ajs719e9742005-02-28 20:52:15 +0000325 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000326}
327
paulb9df2d22004-05-09 09:09:59 +0000328/*
329 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
330 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
331 * situations:
332 * - when the client starts up, and requests default information
333 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
334 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
335 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
336 * - when the zebra server redistributes routes after it updates its rib
337 *
338 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
339 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
340 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
341 * - deleted from zebra's rib, and this info
342 * has to be redistributed to the clients
343 *
344 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
345 * zebra server when the client wants to tell the zebra server to add a
346 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
347 * same message being sent back and forth, this function and
348 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
349 * duplication.
350 */
paul718e3742002-12-13 20:15:29 +0000351int
paulb9df2d22004-05-09 09:09:59 +0000352zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
353 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000354{
355 int psize;
356 struct stream *s;
357 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000358 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000359 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000360 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000361
paul718e3742002-12-13 20:15:29 +0000362 s = client->obuf;
363 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000364
365 zserv_create_header (s, cmd);
366
367 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000368 stream_putc (s, rib->type);
369 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000370
371 /* marker for message flags field */
372 messmark = stream_get_endp (s);
373 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000374
375 /* Prefix. */
376 psize = PSIZE (p->prefixlen);
377 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000378 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000379
paulb9df2d22004-05-09 09:09:59 +0000380 /*
381 * XXX The message format sent by zebra below does not match the format
382 * of the corresponding message expected by the zebra server
383 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
384 * (is there a bug on the client side if more than one segment is sent?)
385 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
386 * is hard-coded.
387 */
paul718e3742002-12-13 20:15:29 +0000388 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000389
paul718e3742002-12-13 20:15:29 +0000390 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
391 {
392 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000393 {
paul1dcb5172005-05-31 08:38:50 +0000394 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
395 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
396
397 if (nhnummark == 0)
398 {
399 nhnummark = stream_get_endp (s);
400 stream_putc (s, 1); /* placeholder */
401 }
402
paulb9df2d22004-05-09 09:09:59 +0000403 nhnum++;
paul718e3742002-12-13 20:15:29 +0000404
paulb9df2d22004-05-09 09:09:59 +0000405 switch(nexthop->type)
406 {
407 case NEXTHOP_TYPE_IPV4:
408 case NEXTHOP_TYPE_IPV4_IFINDEX:
409 stream_put_in_addr (s, &nexthop->gate.ipv4);
410 break;
411#ifdef HAVE_IPV6
412 case NEXTHOP_TYPE_IPV6:
413 case NEXTHOP_TYPE_IPV6_IFINDEX:
414 case NEXTHOP_TYPE_IPV6_IFNAME:
415 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
416 break;
417#endif
418 default:
419 if (cmd == ZEBRA_IPV4_ROUTE_ADD
420 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
421 {
422 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000423 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000424 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
425 }
426 else
427 {
428 struct in6_addr empty;
429 memset (&empty, 0, sizeof (struct in6_addr));
430 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
431 }
432 }
paul718e3742002-12-13 20:15:29 +0000433
paulb9df2d22004-05-09 09:09:59 +0000434 /* Interface index. */
435 stream_putc (s, 1);
436 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000437
paulb9df2d22004-05-09 09:09:59 +0000438 break;
439 }
paul718e3742002-12-13 20:15:29 +0000440 }
441
442 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700443 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000444 {
vincentfbf5d032005-09-29 11:25:50 +0000445 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
446 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000447 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
448 stream_putl (s, rib->metric);
449 }
450
451 /* write real message flags value */
452 stream_putc_at (s, messmark, zapi_flags);
453
paulb9df2d22004-05-09 09:09:59 +0000454 /* Write next-hop number */
455 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000456 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000457
paul718e3742002-12-13 20:15:29 +0000458 /* Write packet size. */
459 stream_putw_at (s, 0, stream_get_endp (s));
460
ajs719e9742005-02-28 20:52:15 +0000461 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000462}
463
paul718e3742002-12-13 20:15:29 +0000464#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000465static int
paul718e3742002-12-13 20:15:29 +0000466zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
467{
468 struct stream *s;
469 struct rib *rib;
470 unsigned long nump;
471 u_char num;
472 struct nexthop *nexthop;
473
474 /* Lookup nexthop. */
475 rib = rib_match_ipv6 (addr);
476
477 /* Get output stream. */
478 s = client->obuf;
479 stream_reset (s);
480
481 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000482 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000483 stream_put (s, &addr, 16);
484
485 if (rib)
486 {
487 stream_putl (s, rib->metric);
488 num = 0;
paul9985f832005-02-09 15:51:56 +0000489 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000490 stream_putc (s, 0);
491 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
492 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
493 {
494 stream_putc (s, nexthop->type);
495 switch (nexthop->type)
496 {
497 case ZEBRA_NEXTHOP_IPV6:
498 stream_put (s, &nexthop->gate.ipv6, 16);
499 break;
500 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
501 case ZEBRA_NEXTHOP_IPV6_IFNAME:
502 stream_put (s, &nexthop->gate.ipv6, 16);
503 stream_putl (s, nexthop->ifindex);
504 break;
505 case ZEBRA_NEXTHOP_IFINDEX:
506 case ZEBRA_NEXTHOP_IFNAME:
507 stream_putl (s, nexthop->ifindex);
508 break;
hassofa2b17e2004-03-04 17:45:00 +0000509 default:
510 /* do nothing */
511 break;
paul718e3742002-12-13 20:15:29 +0000512 }
513 num++;
514 }
515 stream_putc_at (s, nump, num);
516 }
517 else
518 {
519 stream_putl (s, 0);
520 stream_putc (s, 0);
521 }
522
523 stream_putw_at (s, 0, stream_get_endp (s));
524
ajs719e9742005-02-28 20:52:15 +0000525 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000526}
527#endif /* HAVE_IPV6 */
528
paulb9df2d22004-05-09 09:09:59 +0000529static int
paul718e3742002-12-13 20:15:29 +0000530zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
531{
532 struct stream *s;
533 struct rib *rib;
534 unsigned long nump;
535 u_char num;
536 struct nexthop *nexthop;
537
538 /* Lookup nexthop. */
539 rib = rib_match_ipv4 (addr);
540
541 /* Get output stream. */
542 s = client->obuf;
543 stream_reset (s);
544
545 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000546 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000547 stream_put_in_addr (s, &addr);
548
549 if (rib)
550 {
551 stream_putl (s, rib->metric);
552 num = 0;
paul9985f832005-02-09 15:51:56 +0000553 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000554 stream_putc (s, 0);
555 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
556 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
557 {
558 stream_putc (s, nexthop->type);
559 switch (nexthop->type)
560 {
561 case ZEBRA_NEXTHOP_IPV4:
562 stream_put_in_addr (s, &nexthop->gate.ipv4);
563 break;
564 case ZEBRA_NEXTHOP_IFINDEX:
565 case ZEBRA_NEXTHOP_IFNAME:
566 stream_putl (s, nexthop->ifindex);
567 break;
hassofa2b17e2004-03-04 17:45:00 +0000568 default:
569 /* do nothing */
570 break;
paul718e3742002-12-13 20:15:29 +0000571 }
572 num++;
573 }
574 stream_putc_at (s, nump, num);
575 }
576 else
577 {
578 stream_putl (s, 0);
579 stream_putc (s, 0);
580 }
581
582 stream_putw_at (s, 0, stream_get_endp (s));
583
ajs719e9742005-02-28 20:52:15 +0000584 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000585}
586
paulb9df2d22004-05-09 09:09:59 +0000587static int
paul718e3742002-12-13 20:15:29 +0000588zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
589{
590 struct stream *s;
591 struct rib *rib;
592 unsigned long nump;
593 u_char num;
594 struct nexthop *nexthop;
595
596 /* Lookup nexthop. */
597 rib = rib_lookup_ipv4 (p);
598
599 /* Get output stream. */
600 s = client->obuf;
601 stream_reset (s);
602
603 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000604 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000605 stream_put_in_addr (s, &p->prefix);
606
607 if (rib)
608 {
609 stream_putl (s, rib->metric);
610 num = 0;
paul9985f832005-02-09 15:51:56 +0000611 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000612 stream_putc (s, 0);
613 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
614 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
615 {
616 stream_putc (s, nexthop->type);
617 switch (nexthop->type)
618 {
619 case ZEBRA_NEXTHOP_IPV4:
620 stream_put_in_addr (s, &nexthop->gate.ipv4);
621 break;
622 case ZEBRA_NEXTHOP_IFINDEX:
623 case ZEBRA_NEXTHOP_IFNAME:
624 stream_putl (s, nexthop->ifindex);
625 break;
hassofa2b17e2004-03-04 17:45:00 +0000626 default:
627 /* do nothing */
628 break;
paul718e3742002-12-13 20:15:29 +0000629 }
630 num++;
631 }
632 stream_putc_at (s, nump, num);
633 }
634 else
635 {
636 stream_putl (s, 0);
637 stream_putc (s, 0);
638 }
639
640 stream_putw_at (s, 0, stream_get_endp (s));
641
ajs719e9742005-02-28 20:52:15 +0000642 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000643}
644
hasso18a6dce2004-10-03 18:18:34 +0000645/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
646int
647zsend_router_id_update (struct zserv *client, struct prefix *p)
648{
649 struct stream *s;
650 int blen;
651
652 /* Check this client need interface information. */
653 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000654 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000655
656 s = client->obuf;
657 stream_reset (s);
658
hasso18a6dce2004-10-03 18:18:34 +0000659 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000660 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000661
662 /* Prefix information. */
663 stream_putc (s, p->family);
664 blen = prefix_blen (p);
665 stream_put (s, &p->u.prefix, blen);
666 stream_putc (s, p->prefixlen);
667
668 /* Write packet size. */
669 stream_putw_at (s, 0, stream_get_endp (s));
670
ajs719e9742005-02-28 20:52:15 +0000671 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000672}
673
paul718e3742002-12-13 20:15:29 +0000674/* Register zebra server interface information. Send current all
675 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000676static int
paul718e3742002-12-13 20:15:29 +0000677zread_interface_add (struct zserv *client, u_short length)
678{
paul1eb8ef22005-04-07 07:30:20 +0000679 struct listnode *ifnode, *ifnnode;
680 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000681 struct interface *ifp;
682 struct connected *c;
683
684 /* Interface information is needed. */
685 client->ifinfo = 1;
686
paul1eb8ef22005-04-07 07:30:20 +0000687 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000688 {
paul718e3742002-12-13 20:15:29 +0000689 /* Skip pseudo interface. */
690 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
691 continue;
692
ajs719e9742005-02-28 20:52:15 +0000693 if (zsend_interface_add (client, ifp) < 0)
694 return -1;
paul718e3742002-12-13 20:15:29 +0000695
paul1eb8ef22005-04-07 07:30:20 +0000696 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000697 {
ajs719e9742005-02-28 20:52:15 +0000698 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
699 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
700 ifp, c) < 0))
701 return -1;
paul718e3742002-12-13 20:15:29 +0000702 }
703 }
ajs719e9742005-02-28 20:52:15 +0000704 return 0;
paul718e3742002-12-13 20:15:29 +0000705}
706
707/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000708static int
paul718e3742002-12-13 20:15:29 +0000709zread_interface_delete (struct zserv *client, u_short length)
710{
711 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000712 return 0;
paul718e3742002-12-13 20:15:29 +0000713}
714
715/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000716/*
717 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
718 * add kernel route.
719 */
ajs719e9742005-02-28 20:52:15 +0000720static int
paul718e3742002-12-13 20:15:29 +0000721zread_ipv4_add (struct zserv *client, u_short length)
722{
723 int i;
724 struct rib *rib;
725 struct prefix_ipv4 p;
726 u_char message;
727 struct in_addr nexthop;
728 u_char nexthop_num;
729 u_char nexthop_type;
730 struct stream *s;
731 unsigned int ifindex;
732 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400733 safi_t safi;
734
paul718e3742002-12-13 20:15:29 +0000735
736 /* Get input stream. */
737 s = client->ibuf;
738
739 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000740 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
741
paul718e3742002-12-13 20:15:29 +0000742 /* Type, flags, message. */
743 rib->type = stream_getc (s);
744 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000745 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400746 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000747 rib->uptime = time (NULL);
748
749 /* IPv4 prefix. */
750 memset (&p, 0, sizeof (struct prefix_ipv4));
751 p.family = AF_INET;
752 p.prefixlen = stream_getc (s);
753 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
754
755 /* Nexthop parse. */
756 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
757 {
758 nexthop_num = stream_getc (s);
759
760 for (i = 0; i < nexthop_num; i++)
761 {
762 nexthop_type = stream_getc (s);
763
764 switch (nexthop_type)
765 {
766 case ZEBRA_NEXTHOP_IFINDEX:
767 ifindex = stream_getl (s);
768 nexthop_ifindex_add (rib, ifindex);
769 break;
770 case ZEBRA_NEXTHOP_IFNAME:
771 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000772 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000773 break;
774 case ZEBRA_NEXTHOP_IPV4:
775 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000776 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000777 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200778 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
779 nexthop.s_addr = stream_get_ipv4 (s);
780 ifindex = stream_getl (s);
781 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
782 break;
paul718e3742002-12-13 20:15:29 +0000783 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000784 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000785 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700786 case ZEBRA_NEXTHOP_BLACKHOLE:
787 nexthop_blackhole_add (rib);
788 break;
789 }
paul718e3742002-12-13 20:15:29 +0000790 }
791 }
792
793 /* Distance. */
794 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
795 rib->distance = stream_getc (s);
796
797 /* Metric. */
798 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
799 rib->metric = stream_getl (s);
800
Paul Jakma171eee32006-07-27 16:11:02 +0000801 /* Table */
802 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400803 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000804 return 0;
paul718e3742002-12-13 20:15:29 +0000805}
806
807/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000808static int
paul718e3742002-12-13 20:15:29 +0000809zread_ipv4_delete (struct zserv *client, u_short length)
810{
811 int i;
812 struct stream *s;
813 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700814 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000815 unsigned long ifindex;
816 struct prefix_ipv4 p;
817 u_char nexthop_num;
818 u_char nexthop_type;
819 u_char ifname_len;
820
821 s = client->ibuf;
822 ifindex = 0;
823 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700824 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000825
826 /* Type, flags, message. */
827 api.type = stream_getc (s);
828 api.flags = stream_getc (s);
829 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400830 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000831
832 /* IPv4 prefix. */
833 memset (&p, 0, sizeof (struct prefix_ipv4));
834 p.family = AF_INET;
835 p.prefixlen = stream_getc (s);
836 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
837
838 /* Nexthop, ifindex, distance, metric. */
839 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
840 {
841 nexthop_num = stream_getc (s);
842
843 for (i = 0; i < nexthop_num; i++)
844 {
845 nexthop_type = stream_getc (s);
846
847 switch (nexthop_type)
848 {
849 case ZEBRA_NEXTHOP_IFINDEX:
850 ifindex = stream_getl (s);
851 break;
852 case ZEBRA_NEXTHOP_IFNAME:
853 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000854 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000855 break;
856 case ZEBRA_NEXTHOP_IPV4:
857 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700858 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000859 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200860 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
861 nexthop.s_addr = stream_get_ipv4 (s);
862 ifindex = stream_getl (s);
863 break;
paul718e3742002-12-13 20:15:29 +0000864 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000865 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000866 break;
867 }
868 }
869 }
870
871 /* Distance. */
872 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
873 api.distance = stream_getc (s);
874 else
875 api.distance = 0;
876
877 /* Metric. */
878 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
879 api.metric = stream_getl (s);
880 else
881 api.metric = 0;
882
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700883 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400884 client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +0000885 return 0;
paul718e3742002-12-13 20:15:29 +0000886}
887
888/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000889static int
paul718e3742002-12-13 20:15:29 +0000890zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
891{
892 struct in_addr addr;
893
894 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000895 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000896}
897
898/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000899static int
paul718e3742002-12-13 20:15:29 +0000900zread_ipv4_import_lookup (struct zserv *client, u_short length)
901{
902 struct prefix_ipv4 p;
903
904 p.family = AF_INET;
905 p.prefixlen = stream_getc (client->ibuf);
906 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
907
ajs719e9742005-02-28 20:52:15 +0000908 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000909}
910
911#ifdef HAVE_IPV6
912/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000913static int
paul718e3742002-12-13 20:15:29 +0000914zread_ipv6_add (struct zserv *client, u_short length)
915{
916 int i;
917 struct stream *s;
918 struct zapi_ipv6 api;
919 struct in6_addr nexthop;
920 unsigned long ifindex;
921 struct prefix_ipv6 p;
922
923 s = client->ibuf;
924 ifindex = 0;
925 memset (&nexthop, 0, sizeof (struct in6_addr));
926
927 /* Type, flags, message. */
928 api.type = stream_getc (s);
929 api.flags = stream_getc (s);
930 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +0400931 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000932
933 /* IPv4 prefix. */
934 memset (&p, 0, sizeof (struct prefix_ipv6));
935 p.family = AF_INET6;
936 p.prefixlen = stream_getc (s);
937 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
938
939 /* Nexthop, ifindex, distance, metric. */
940 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
941 {
942 u_char nexthop_type;
943
944 api.nexthop_num = stream_getc (s);
945 for (i = 0; i < api.nexthop_num; i++)
946 {
947 nexthop_type = stream_getc (s);
948
949 switch (nexthop_type)
950 {
951 case ZEBRA_NEXTHOP_IPV6:
952 stream_get (&nexthop, s, 16);
953 break;
954 case ZEBRA_NEXTHOP_IFINDEX:
955 ifindex = stream_getl (s);
956 break;
957 }
958 }
959 }
960
961 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
962 api.distance = stream_getc (s);
963 else
964 api.distance = 0;
965
966 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
967 api.metric = stream_getl (s);
968 else
969 api.metric = 0;
970
971 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100972 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400973 api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +0000974 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100975 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400976 api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +0000977 return 0;
paul718e3742002-12-13 20:15:29 +0000978}
979
980/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000981static int
paul718e3742002-12-13 20:15:29 +0000982zread_ipv6_delete (struct zserv *client, u_short length)
983{
984 int i;
985 struct stream *s;
986 struct zapi_ipv6 api;
987 struct in6_addr nexthop;
988 unsigned long ifindex;
989 struct prefix_ipv6 p;
990
991 s = client->ibuf;
992 ifindex = 0;
993 memset (&nexthop, 0, sizeof (struct in6_addr));
994
995 /* Type, flags, message. */
996 api.type = stream_getc (s);
997 api.flags = stream_getc (s);
998 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +0400999 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001000
1001 /* IPv4 prefix. */
1002 memset (&p, 0, sizeof (struct prefix_ipv6));
1003 p.family = AF_INET6;
1004 p.prefixlen = stream_getc (s);
1005 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1006
1007 /* Nexthop, ifindex, distance, metric. */
1008 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1009 {
1010 u_char nexthop_type;
1011
1012 api.nexthop_num = stream_getc (s);
1013 for (i = 0; i < api.nexthop_num; i++)
1014 {
1015 nexthop_type = stream_getc (s);
1016
1017 switch (nexthop_type)
1018 {
1019 case ZEBRA_NEXTHOP_IPV6:
1020 stream_get (&nexthop, s, 16);
1021 break;
1022 case ZEBRA_NEXTHOP_IFINDEX:
1023 ifindex = stream_getl (s);
1024 break;
1025 }
1026 }
1027 }
1028
1029 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1030 api.distance = stream_getc (s);
1031 else
1032 api.distance = 0;
1033 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1034 api.metric = stream_getl (s);
1035 else
1036 api.metric = 0;
1037
1038 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
G.Balajif768f362011-11-26 22:10:39 +04001039 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
paul718e3742002-12-13 20:15:29 +00001040 else
G.Balajif768f362011-11-26 22:10:39 +04001041 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +00001042 return 0;
paul718e3742002-12-13 20:15:29 +00001043}
1044
ajs719e9742005-02-28 20:52:15 +00001045static int
paul718e3742002-12-13 20:15:29 +00001046zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1047{
1048 struct in6_addr addr;
1049 char buf[BUFSIZ];
1050
1051 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001052 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1053 zlog_debug("%s: looking up %s", __func__,
1054 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001055
ajs719e9742005-02-28 20:52:15 +00001056 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001057}
1058#endif /* HAVE_IPV6 */
1059
hasso18a6dce2004-10-03 18:18:34 +00001060/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001061static int
hasso18a6dce2004-10-03 18:18:34 +00001062zread_router_id_add (struct zserv *client, u_short length)
1063{
1064 struct prefix p;
1065
1066 /* Router-id information is needed. */
1067 client->ridinfo = 1;
1068
1069 router_id_get (&p);
1070
ajs719e9742005-02-28 20:52:15 +00001071 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001072}
1073
1074/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001075static int
hasso18a6dce2004-10-03 18:18:34 +00001076zread_router_id_delete (struct zserv *client, u_short length)
1077{
1078 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001079 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001080}
1081
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001082/* Tie up route-type and client->sock */
1083static void
1084zread_hello (struct zserv *client)
1085{
1086 /* type of protocol (lib/zebra.h) */
1087 u_char proto;
1088 proto = stream_getc (client->ibuf);
1089
1090 /* accept only dynamic routing protocols */
1091 if ((proto < ZEBRA_ROUTE_MAX)
1092 && (proto > ZEBRA_ROUTE_STATIC))
1093 {
1094 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1095 client->sock, zebra_route_string(proto));
1096
1097 /* if route-type was binded by other client */
1098 if (route_type_oaths[proto])
1099 zlog_warn ("sender of %s routes changed %c->%c",
1100 zebra_route_string(proto), route_type_oaths[proto],
1101 client->sock);
1102
1103 route_type_oaths[proto] = client->sock;
1104 }
1105}
1106
1107/* If client sent routes of specific type, zebra removes it
1108 * and returns number of deleted routes.
1109 */
1110static void
1111zebra_score_rib (int client_sock)
1112{
1113 int i;
1114
1115 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1116 if (client_sock == route_type_oaths[i])
1117 {
1118 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1119 client_sock, rib_score_proto (i), zebra_route_string (i));
1120 route_type_oaths[i] = 0;
1121 break;
1122 }
1123}
1124
paul718e3742002-12-13 20:15:29 +00001125/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001126static void
paul718e3742002-12-13 20:15:29 +00001127zebra_client_close (struct zserv *client)
1128{
1129 /* Close file descriptor. */
1130 if (client->sock)
1131 {
1132 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001133 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001134 client->sock = -1;
1135 }
1136
1137 /* Free stream buffers. */
1138 if (client->ibuf)
1139 stream_free (client->ibuf);
1140 if (client->obuf)
1141 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001142 if (client->wb)
1143 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001144
1145 /* Release threads. */
1146 if (client->t_read)
1147 thread_cancel (client->t_read);
1148 if (client->t_write)
1149 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001150 if (client->t_suicide)
1151 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001152
1153 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001154 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001155 XFREE (0, client);
1156}
1157
1158/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001159static void
paul718e3742002-12-13 20:15:29 +00001160zebra_client_create (int sock)
1161{
1162 struct zserv *client;
1163
1164 client = XCALLOC (0, sizeof (struct zserv));
1165
1166 /* Make client input/output buffer. */
1167 client->sock = sock;
1168 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1169 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001170 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001171
1172 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001173 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001174
1175 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001176 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001177
1178 /* Make new read thread. */
1179 zebra_event (ZEBRA_READ, sock, client);
1180}
1181
1182/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001183static int
paul718e3742002-12-13 20:15:29 +00001184zebra_client_read (struct thread *thread)
1185{
1186 int sock;
1187 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001188 size_t already;
paulc1b98002006-01-16 01:54:02 +00001189 uint16_t length, command;
1190 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001191
1192 /* Get thread data. Reset reading thread because I'm running. */
1193 sock = THREAD_FD (thread);
1194 client = THREAD_ARG (thread);
1195 client->t_read = NULL;
1196
ajs719e9742005-02-28 20:52:15 +00001197 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001198 {
ajs719e9742005-02-28 20:52:15 +00001199 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001200 return -1;
1201 }
ajs719e9742005-02-28 20:52:15 +00001202
1203 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001204 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001205 {
ajs57a14772005-04-10 15:01:56 +00001206 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001207 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001208 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001209 (nbyte == -1))
1210 {
1211 if (IS_ZEBRA_DEBUG_EVENT)
1212 zlog_debug ("connection closed socket [%d]", sock);
1213 zebra_client_close (client);
1214 return -1;
1215 }
ajs57a14772005-04-10 15:01:56 +00001216 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001217 {
1218 /* Try again later. */
1219 zebra_event (ZEBRA_READ, sock, client);
1220 return 0;
1221 }
ajs57a14772005-04-10 15:01:56 +00001222 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001223 }
1224
1225 /* Reset to read from the beginning of the incoming packet. */
1226 stream_set_getp(client->ibuf, 0);
1227
paulc1b98002006-01-16 01:54:02 +00001228 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001229 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001230 marker = stream_getc (client->ibuf);
1231 version = stream_getc (client->ibuf);
1232 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001233
paulc1b98002006-01-16 01:54:02 +00001234 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1235 {
1236 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1237 __func__, sock, marker, version);
1238 zebra_client_close (client);
1239 return -1;
1240 }
ajs719e9742005-02-28 20:52:15 +00001241 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001242 {
ajs57a14772005-04-10 15:01:56 +00001243 zlog_warn("%s: socket %d message length %u is less than header size %d",
1244 __func__, sock, length, ZEBRA_HEADER_SIZE);
1245 zebra_client_close (client);
1246 return -1;
1247 }
1248 if (length > STREAM_SIZE(client->ibuf))
1249 {
1250 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1251 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001252 zebra_client_close (client);
1253 return -1;
1254 }
1255
paul718e3742002-12-13 20:15:29 +00001256 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001257 if (already < length)
paul718e3742002-12-13 20:15:29 +00001258 {
ajs57a14772005-04-10 15:01:56 +00001259 ssize_t nbyte;
1260 if (((nbyte = stream_read_try (client->ibuf, sock,
1261 length-already)) == 0) ||
1262 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001263 {
1264 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001265 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001266 zebra_client_close (client);
1267 return -1;
1268 }
ajs57a14772005-04-10 15:01:56 +00001269 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001270 {
1271 /* Try again later. */
1272 zebra_event (ZEBRA_READ, sock, client);
1273 return 0;
1274 }
paul718e3742002-12-13 20:15:29 +00001275 }
1276
ajs719e9742005-02-28 20:52:15 +00001277 length -= ZEBRA_HEADER_SIZE;
1278
paul718e3742002-12-13 20:15:29 +00001279 /* Debug packet information. */
1280 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001281 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001282
1283 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001284 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001285 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001286
1287 switch (command)
1288 {
hasso18a6dce2004-10-03 18:18:34 +00001289 case ZEBRA_ROUTER_ID_ADD:
1290 zread_router_id_add (client, length);
1291 break;
1292 case ZEBRA_ROUTER_ID_DELETE:
1293 zread_router_id_delete (client, length);
1294 break;
paul718e3742002-12-13 20:15:29 +00001295 case ZEBRA_INTERFACE_ADD:
1296 zread_interface_add (client, length);
1297 break;
1298 case ZEBRA_INTERFACE_DELETE:
1299 zread_interface_delete (client, length);
1300 break;
1301 case ZEBRA_IPV4_ROUTE_ADD:
1302 zread_ipv4_add (client, length);
1303 break;
1304 case ZEBRA_IPV4_ROUTE_DELETE:
1305 zread_ipv4_delete (client, length);
1306 break;
1307#ifdef HAVE_IPV6
1308 case ZEBRA_IPV6_ROUTE_ADD:
1309 zread_ipv6_add (client, length);
1310 break;
1311 case ZEBRA_IPV6_ROUTE_DELETE:
1312 zread_ipv6_delete (client, length);
1313 break;
1314#endif /* HAVE_IPV6 */
1315 case ZEBRA_REDISTRIBUTE_ADD:
1316 zebra_redistribute_add (command, client, length);
1317 break;
1318 case ZEBRA_REDISTRIBUTE_DELETE:
1319 zebra_redistribute_delete (command, client, length);
1320 break;
1321 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1322 zebra_redistribute_default_add (command, client, length);
1323 break;
1324 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1325 zebra_redistribute_default_delete (command, client, length);
1326 break;
1327 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1328 zread_ipv4_nexthop_lookup (client, length);
1329 break;
1330#ifdef HAVE_IPV6
1331 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1332 zread_ipv6_nexthop_lookup (client, length);
1333 break;
1334#endif /* HAVE_IPV6 */
1335 case ZEBRA_IPV4_IMPORT_LOOKUP:
1336 zread_ipv4_import_lookup (client, length);
1337 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001338 case ZEBRA_HELLO:
1339 zread_hello (client);
1340 break;
paul718e3742002-12-13 20:15:29 +00001341 default:
1342 zlog_info ("Zebra received unknown command %d", command);
1343 break;
1344 }
1345
ajs719e9742005-02-28 20:52:15 +00001346 if (client->t_suicide)
1347 {
1348 /* No need to wait for thread callback, just kill immediately. */
1349 zebra_client_close(client);
1350 return -1;
1351 }
1352
paul718e3742002-12-13 20:15:29 +00001353 stream_reset (client->ibuf);
1354 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001355 return 0;
1356}
1357
paul718e3742002-12-13 20:15:29 +00001358
1359/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001360static int
paul718e3742002-12-13 20:15:29 +00001361zebra_accept (struct thread *thread)
1362{
1363 int accept_sock;
1364 int client_sock;
1365 struct sockaddr_in client;
1366 socklen_t len;
1367
1368 accept_sock = THREAD_FD (thread);
1369
ajs719e9742005-02-28 20:52:15 +00001370 /* Reregister myself. */
1371 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1372
paul718e3742002-12-13 20:15:29 +00001373 len = sizeof (struct sockaddr_in);
1374 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1375
1376 if (client_sock < 0)
1377 {
ajs6099b3b2004-11-20 02:06:59 +00001378 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001379 return -1;
1380 }
1381
paulccf35572003-03-01 11:42:20 +00001382 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001383 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001384
paul718e3742002-12-13 20:15:29 +00001385 /* Create new zebra client. */
1386 zebra_client_create (client_sock);
1387
paul718e3742002-12-13 20:15:29 +00001388 return 0;
1389}
1390
paulb9df2d22004-05-09 09:09:59 +00001391#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001392/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001393static void
paul718e3742002-12-13 20:15:29 +00001394zebra_serv ()
1395{
1396 int ret;
1397 int accept_sock;
1398 struct sockaddr_in addr;
1399
1400 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1401
1402 if (accept_sock < 0)
1403 {
paul3d1dc852005-04-05 00:45:23 +00001404 zlog_warn ("Can't create zserv stream socket: %s",
1405 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001406 zlog_warn ("zebra can't provice full functionality due to above error");
1407 return;
1408 }
1409
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001410 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001411 memset (&addr, 0, sizeof (struct sockaddr_in));
1412 addr.sin_family = AF_INET;
1413 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001414#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001415 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001416#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001417 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1418
1419 sockopt_reuseaddr (accept_sock);
1420 sockopt_reuseport (accept_sock);
1421
pauledd7c242003-06-04 13:59:38 +00001422 if ( zserv_privs.change(ZPRIVS_RAISE) )
1423 zlog (NULL, LOG_ERR, "Can't raise privileges");
1424
paul718e3742002-12-13 20:15:29 +00001425 ret = bind (accept_sock, (struct sockaddr *)&addr,
1426 sizeof (struct sockaddr_in));
1427 if (ret < 0)
1428 {
paul3d1dc852005-04-05 00:45:23 +00001429 zlog_warn ("Can't bind to stream socket: %s",
1430 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001431 zlog_warn ("zebra can't provice full functionality due to above error");
1432 close (accept_sock); /* Avoid sd leak. */
1433 return;
1434 }
pauledd7c242003-06-04 13:59:38 +00001435
1436 if ( zserv_privs.change(ZPRIVS_LOWER) )
1437 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001438
1439 ret = listen (accept_sock, 1);
1440 if (ret < 0)
1441 {
paul3d1dc852005-04-05 00:45:23 +00001442 zlog_warn ("Can't listen to stream socket: %s",
1443 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001444 zlog_warn ("zebra can't provice full functionality due to above error");
1445 close (accept_sock); /* Avoid sd leak. */
1446 return;
1447 }
1448
1449 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1450}
paulb9df2d22004-05-09 09:09:59 +00001451#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001452
1453/* For sockaddr_un. */
1454#include <sys/un.h>
1455
1456/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001457static void
hassofce954f2004-10-07 20:29:24 +00001458zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001459{
1460 int ret;
1461 int sock, len;
1462 struct sockaddr_un serv;
1463 mode_t old_mask;
1464
1465 /* First of all, unlink existing socket */
1466 unlink (path);
1467
1468 /* Set umask */
1469 old_mask = umask (0077);
1470
1471 /* Make UNIX domain socket. */
1472 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1473 if (sock < 0)
1474 {
paul3d1dc852005-04-05 00:45:23 +00001475 zlog_warn ("Can't create zserv unix socket: %s",
1476 safe_strerror (errno));
1477 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001478 return;
1479 }
1480
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001481 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1482
paul718e3742002-12-13 20:15:29 +00001483 /* Make server socket. */
1484 memset (&serv, 0, sizeof (struct sockaddr_un));
1485 serv.sun_family = AF_UNIX;
1486 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001487#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001488 len = serv.sun_len = SUN_LEN(&serv);
1489#else
1490 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001491#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001492
1493 ret = bind (sock, (struct sockaddr *) &serv, len);
1494 if (ret < 0)
1495 {
paul3d1dc852005-04-05 00:45:23 +00001496 zlog_warn ("Can't bind to unix socket %s: %s",
1497 path, safe_strerror (errno));
1498 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001499 close (sock);
1500 return;
1501 }
1502
1503 ret = listen (sock, 5);
1504 if (ret < 0)
1505 {
paul3d1dc852005-04-05 00:45:23 +00001506 zlog_warn ("Can't listen to unix socket %s: %s",
1507 path, safe_strerror (errno));
1508 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001509 close (sock);
1510 return;
1511 }
1512
1513 umask (old_mask);
1514
1515 zebra_event (ZEBRA_SERV, sock, NULL);
1516}
1517
paul718e3742002-12-13 20:15:29 +00001518
paulb9df2d22004-05-09 09:09:59 +00001519static void
paul718e3742002-12-13 20:15:29 +00001520zebra_event (enum event event, int sock, struct zserv *client)
1521{
1522 switch (event)
1523 {
1524 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001525 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001526 break;
1527 case ZEBRA_READ:
1528 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001529 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001530 break;
1531 case ZEBRA_WRITE:
1532 /**/
1533 break;
1534 }
1535}
1536
1537/* Display default rtm_table for all clients. */
1538DEFUN (show_table,
1539 show_table_cmd,
1540 "show table",
1541 SHOW_STR
1542 "default routing table to use for all clients\n")
1543{
paulb21b19c2003-06-15 01:28:29 +00001544 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001545 VTY_NEWLINE);
1546 return CMD_SUCCESS;
1547}
1548
1549DEFUN (config_table,
1550 config_table_cmd,
1551 "table TABLENO",
1552 "Configure target kernel routing table\n"
1553 "TABLE integer\n")
1554{
paulb21b19c2003-06-15 01:28:29 +00001555 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001556 return CMD_SUCCESS;
1557}
1558
hasso647e4f12003-05-25 11:43:52 +00001559DEFUN (ip_forwarding,
1560 ip_forwarding_cmd,
1561 "ip forwarding",
1562 IP_STR
1563 "Turn on IP forwarding")
1564{
1565 int ret;
1566
1567 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001568 if (ret == 0)
1569 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001570
hasso647e4f12003-05-25 11:43:52 +00001571 if (ret == 0)
1572 {
1573 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1574 return CMD_WARNING;
1575 }
1576
1577 return CMD_SUCCESS;
1578}
1579
paul718e3742002-12-13 20:15:29 +00001580DEFUN (no_ip_forwarding,
1581 no_ip_forwarding_cmd,
1582 "no ip forwarding",
1583 NO_STR
1584 IP_STR
1585 "Turn off IP forwarding")
1586{
1587 int ret;
1588
1589 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001590 if (ret != 0)
1591 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001592
paul718e3742002-12-13 20:15:29 +00001593 if (ret != 0)
1594 {
1595 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1596 return CMD_WARNING;
1597 }
1598
1599 return CMD_SUCCESS;
1600}
1601
1602/* This command is for debugging purpose. */
1603DEFUN (show_zebra_client,
1604 show_zebra_client_cmd,
1605 "show zebra client",
1606 SHOW_STR
1607 "Zebra information"
1608 "Client information")
1609{
hasso52dc7ee2004-09-23 19:18:23 +00001610 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001611 struct zserv *client;
1612
paul1eb8ef22005-04-07 07:30:20 +00001613 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1614 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1615
paul718e3742002-12-13 20:15:29 +00001616 return CMD_SUCCESS;
1617}
1618
1619/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001620static int
paul718e3742002-12-13 20:15:29 +00001621config_write_table (struct vty *vty)
1622{
paulb21b19c2003-06-15 01:28:29 +00001623 if (zebrad.rtm_table_default)
1624 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001625 VTY_NEWLINE);
1626 return 0;
1627}
1628
1629/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001630static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001631{
1632 TABLE_NODE,
1633 "", /* This node has no interface. */
1634 1
1635};
1636
1637/* Only display ip forwarding is enabled or not. */
1638DEFUN (show_ip_forwarding,
1639 show_ip_forwarding_cmd,
1640 "show ip forwarding",
1641 SHOW_STR
1642 IP_STR
1643 "IP forwarding status\n")
1644{
1645 int ret;
1646
1647 ret = ipforward ();
1648
1649 if (ret == 0)
1650 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1651 else
1652 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1653 return CMD_SUCCESS;
1654}
1655
1656#ifdef HAVE_IPV6
1657/* Only display ipv6 forwarding is enabled or not. */
1658DEFUN (show_ipv6_forwarding,
1659 show_ipv6_forwarding_cmd,
1660 "show ipv6 forwarding",
1661 SHOW_STR
1662 "IPv6 information\n"
1663 "Forwarding status\n")
1664{
1665 int ret;
1666
1667 ret = ipforward_ipv6 ();
1668
1669 switch (ret)
1670 {
1671 case -1:
1672 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1673 break;
1674 case 0:
1675 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1676 break;
1677 case 1:
1678 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1679 break;
1680 default:
1681 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1682 break;
1683 }
1684 return CMD_SUCCESS;
1685}
1686
hasso55906722004-02-11 22:42:16 +00001687DEFUN (ipv6_forwarding,
1688 ipv6_forwarding_cmd,
1689 "ipv6 forwarding",
1690 IPV6_STR
1691 "Turn on IPv6 forwarding")
1692{
1693 int ret;
1694
hasso41d3fc92004-04-06 11:59:00 +00001695 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001696 if (ret == 0)
1697 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001698
hasso41d3fc92004-04-06 11:59:00 +00001699 if (ret == 0)
1700 {
hasso55906722004-02-11 22:42:16 +00001701 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1702 return CMD_WARNING;
1703 }
1704
1705 return CMD_SUCCESS;
1706}
1707
paul718e3742002-12-13 20:15:29 +00001708DEFUN (no_ipv6_forwarding,
1709 no_ipv6_forwarding_cmd,
1710 "no ipv6 forwarding",
1711 NO_STR
hasso55906722004-02-11 22:42:16 +00001712 IPV6_STR
1713 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001714{
1715 int ret;
1716
hasso41d3fc92004-04-06 11:59:00 +00001717 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001718 if (ret != 0)
1719 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001720
paul718e3742002-12-13 20:15:29 +00001721 if (ret != 0)
1722 {
1723 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1724 return CMD_WARNING;
1725 }
1726
1727 return CMD_SUCCESS;
1728}
1729
1730#endif /* HAVE_IPV6 */
1731
1732/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001733static int
paul718e3742002-12-13 20:15:29 +00001734config_write_forwarding (struct vty *vty)
1735{
hasso18a6dce2004-10-03 18:18:34 +00001736 /* FIXME: Find better place for that. */
1737 router_id_write (vty);
1738
paul3e0b3a52004-08-23 18:58:32 +00001739 if (ipforward ())
1740 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001741#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001742 if (ipforward_ipv6 ())
1743 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001744#endif /* HAVE_IPV6 */
1745 vty_out (vty, "!%s", VTY_NEWLINE);
1746 return 0;
1747}
1748
1749/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001750static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001751{
1752 FORWARDING_NODE,
1753 "", /* This node has no interface. */
1754 1
1755};
1756
1757
1758/* Initialisation of zebra and installation of commands. */
1759void
paula1ac18c2005-06-28 17:17:12 +00001760zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001761{
1762 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001763 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001764
paul718e3742002-12-13 20:15:29 +00001765 /* Install configuration write function. */
1766 install_node (&table_node, config_write_table);
1767 install_node (&forwarding_node, config_write_forwarding);
1768
1769 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1770 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001771 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001772 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1773 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1774
1775#ifdef HAVE_NETLINK
1776 install_element (VIEW_NODE, &show_table_cmd);
1777 install_element (ENABLE_NODE, &show_table_cmd);
1778 install_element (CONFIG_NODE, &config_table_cmd);
1779#endif /* HAVE_NETLINK */
1780
1781#ifdef HAVE_IPV6
1782 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1783 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001784 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001785 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1786#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001787
1788 /* Route-map */
1789 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001790}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001791
1792/* Make zebra server socket, wiping any existing one (see bug #403). */
1793void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001794zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001795{
1796#ifdef HAVE_TCP_ZEBRA
1797 zebra_serv ();
1798#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001799 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001800#endif /* HAVE_TCP_ZEBRA */
1801}