blob: 310c6ce652799400c116fc6916173584ef1c10c1 [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);
940
941 /* IPv4 prefix. */
942 memset (&p, 0, sizeof (struct prefix_ipv6));
943 p.family = AF_INET6;
944 p.prefixlen = stream_getc (s);
945 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
946
947 /* Nexthop, ifindex, distance, metric. */
948 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
949 {
950 u_char nexthop_type;
951
952 api.nexthop_num = stream_getc (s);
953 for (i = 0; i < api.nexthop_num; i++)
954 {
955 nexthop_type = stream_getc (s);
956
957 switch (nexthop_type)
958 {
959 case ZEBRA_NEXTHOP_IPV6:
960 stream_get (&nexthop, s, 16);
961 break;
962 case ZEBRA_NEXTHOP_IFINDEX:
963 ifindex = stream_getl (s);
964 break;
965 }
966 }
967 }
968
969 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
970 api.distance = stream_getc (s);
971 else
972 api.distance = 0;
973
974 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
975 api.metric = stream_getl (s);
976 else
977 api.metric = 0;
978
979 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100980 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000981 api.distance);
paul718e3742002-12-13 20:15:29 +0000982 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100983 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000984 api.distance);
ajs719e9742005-02-28 20:52:15 +0000985 return 0;
paul718e3742002-12-13 20:15:29 +0000986}
987
988/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000989static int
paul718e3742002-12-13 20:15:29 +0000990zread_ipv6_delete (struct zserv *client, u_short length)
991{
992 int i;
993 struct stream *s;
994 struct zapi_ipv6 api;
995 struct in6_addr nexthop;
996 unsigned long ifindex;
997 struct prefix_ipv6 p;
998
999 s = client->ibuf;
1000 ifindex = 0;
1001 memset (&nexthop, 0, sizeof (struct in6_addr));
1002
1003 /* Type, flags, message. */
1004 api.type = stream_getc (s);
1005 api.flags = stream_getc (s);
1006 api.message = stream_getc (s);
1007
1008 /* IPv4 prefix. */
1009 memset (&p, 0, sizeof (struct prefix_ipv6));
1010 p.family = AF_INET6;
1011 p.prefixlen = stream_getc (s);
1012 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1013
1014 /* Nexthop, ifindex, distance, metric. */
1015 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1016 {
1017 u_char nexthop_type;
1018
1019 api.nexthop_num = stream_getc (s);
1020 for (i = 0; i < api.nexthop_num; i++)
1021 {
1022 nexthop_type = stream_getc (s);
1023
1024 switch (nexthop_type)
1025 {
1026 case ZEBRA_NEXTHOP_IPV6:
1027 stream_get (&nexthop, s, 16);
1028 break;
1029 case ZEBRA_NEXTHOP_IFINDEX:
1030 ifindex = stream_getl (s);
1031 break;
1032 }
1033 }
1034 }
1035
1036 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1037 api.distance = stream_getc (s);
1038 else
1039 api.distance = 0;
1040 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1041 api.metric = stream_getl (s);
1042 else
1043 api.metric = 0;
1044
1045 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001046 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table);
paul718e3742002-12-13 20:15:29 +00001047 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001048 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table);
ajs719e9742005-02-28 20:52:15 +00001049 return 0;
paul718e3742002-12-13 20:15:29 +00001050}
1051
ajs719e9742005-02-28 20:52:15 +00001052static int
paul718e3742002-12-13 20:15:29 +00001053zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1054{
1055 struct in6_addr addr;
1056 char buf[BUFSIZ];
1057
1058 stream_get (&addr, client->ibuf, 16);
1059 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1060
ajs719e9742005-02-28 20:52:15 +00001061 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001062}
1063#endif /* HAVE_IPV6 */
1064
hasso18a6dce2004-10-03 18:18:34 +00001065/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001066static int
hasso18a6dce2004-10-03 18:18:34 +00001067zread_router_id_add (struct zserv *client, u_short length)
1068{
1069 struct prefix p;
1070
1071 /* Router-id information is needed. */
1072 client->ridinfo = 1;
1073
1074 router_id_get (&p);
1075
ajs719e9742005-02-28 20:52:15 +00001076 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001077}
1078
1079/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001080static int
hasso18a6dce2004-10-03 18:18:34 +00001081zread_router_id_delete (struct zserv *client, u_short length)
1082{
1083 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001084 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001085}
1086
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001087/* Tie up route-type and client->sock */
1088static void
1089zread_hello (struct zserv *client)
1090{
1091 /* type of protocol (lib/zebra.h) */
1092 u_char proto;
1093 proto = stream_getc (client->ibuf);
1094
1095 /* accept only dynamic routing protocols */
1096 if ((proto < ZEBRA_ROUTE_MAX)
1097 && (proto > ZEBRA_ROUTE_STATIC))
1098 {
1099 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1100 client->sock, zebra_route_string(proto));
1101
1102 /* if route-type was binded by other client */
1103 if (route_type_oaths[proto])
1104 zlog_warn ("sender of %s routes changed %c->%c",
1105 zebra_route_string(proto), route_type_oaths[proto],
1106 client->sock);
1107
1108 route_type_oaths[proto] = client->sock;
1109 }
1110}
1111
1112/* If client sent routes of specific type, zebra removes it
1113 * and returns number of deleted routes.
1114 */
1115static void
1116zebra_score_rib (int client_sock)
1117{
1118 int i;
1119
1120 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1121 if (client_sock == route_type_oaths[i])
1122 {
1123 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1124 client_sock, rib_score_proto (i), zebra_route_string (i));
1125 route_type_oaths[i] = 0;
1126 break;
1127 }
1128}
1129
paul718e3742002-12-13 20:15:29 +00001130/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001131static void
paul718e3742002-12-13 20:15:29 +00001132zebra_client_close (struct zserv *client)
1133{
1134 /* Close file descriptor. */
1135 if (client->sock)
1136 {
1137 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001138 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001139 client->sock = -1;
1140 }
1141
1142 /* Free stream buffers. */
1143 if (client->ibuf)
1144 stream_free (client->ibuf);
1145 if (client->obuf)
1146 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001147 if (client->wb)
1148 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001149
1150 /* Release threads. */
1151 if (client->t_read)
1152 thread_cancel (client->t_read);
1153 if (client->t_write)
1154 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001155 if (client->t_suicide)
1156 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001157
1158 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001159 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001160 XFREE (0, client);
1161}
1162
1163/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001164static void
paul718e3742002-12-13 20:15:29 +00001165zebra_client_create (int sock)
1166{
1167 struct zserv *client;
1168
1169 client = XCALLOC (0, sizeof (struct zserv));
1170
1171 /* Make client input/output buffer. */
1172 client->sock = sock;
1173 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1174 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001175 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001176
1177 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001178 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001179
1180 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001181 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001182
1183 /* Make new read thread. */
1184 zebra_event (ZEBRA_READ, sock, client);
1185}
1186
1187/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001188static int
paul718e3742002-12-13 20:15:29 +00001189zebra_client_read (struct thread *thread)
1190{
1191 int sock;
1192 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001193 size_t already;
paulc1b98002006-01-16 01:54:02 +00001194 uint16_t length, command;
1195 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001196
1197 /* Get thread data. Reset reading thread because I'm running. */
1198 sock = THREAD_FD (thread);
1199 client = THREAD_ARG (thread);
1200 client->t_read = NULL;
1201
ajs719e9742005-02-28 20:52:15 +00001202 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001203 {
ajs719e9742005-02-28 20:52:15 +00001204 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001205 return -1;
1206 }
ajs719e9742005-02-28 20:52:15 +00001207
1208 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001209 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001210 {
ajs57a14772005-04-10 15:01:56 +00001211 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001212 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001213 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001214 (nbyte == -1))
1215 {
1216 if (IS_ZEBRA_DEBUG_EVENT)
1217 zlog_debug ("connection closed socket [%d]", sock);
1218 zebra_client_close (client);
1219 return -1;
1220 }
ajs57a14772005-04-10 15:01:56 +00001221 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001222 {
1223 /* Try again later. */
1224 zebra_event (ZEBRA_READ, sock, client);
1225 return 0;
1226 }
ajs57a14772005-04-10 15:01:56 +00001227 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001228 }
1229
1230 /* Reset to read from the beginning of the incoming packet. */
1231 stream_set_getp(client->ibuf, 0);
1232
paulc1b98002006-01-16 01:54:02 +00001233 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001234 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001235 marker = stream_getc (client->ibuf);
1236 version = stream_getc (client->ibuf);
1237 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001238
paulc1b98002006-01-16 01:54:02 +00001239 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1240 {
1241 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1242 __func__, sock, marker, version);
1243 zebra_client_close (client);
1244 return -1;
1245 }
ajs719e9742005-02-28 20:52:15 +00001246 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001247 {
ajs57a14772005-04-10 15:01:56 +00001248 zlog_warn("%s: socket %d message length %u is less than header size %d",
1249 __func__, sock, length, ZEBRA_HEADER_SIZE);
1250 zebra_client_close (client);
1251 return -1;
1252 }
1253 if (length > STREAM_SIZE(client->ibuf))
1254 {
1255 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1256 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001257 zebra_client_close (client);
1258 return -1;
1259 }
1260
paul718e3742002-12-13 20:15:29 +00001261 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001262 if (already < length)
paul718e3742002-12-13 20:15:29 +00001263 {
ajs57a14772005-04-10 15:01:56 +00001264 ssize_t nbyte;
1265 if (((nbyte = stream_read_try (client->ibuf, sock,
1266 length-already)) == 0) ||
1267 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001268 {
1269 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001270 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001271 zebra_client_close (client);
1272 return -1;
1273 }
ajs57a14772005-04-10 15:01:56 +00001274 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001275 {
1276 /* Try again later. */
1277 zebra_event (ZEBRA_READ, sock, client);
1278 return 0;
1279 }
paul718e3742002-12-13 20:15:29 +00001280 }
1281
ajs719e9742005-02-28 20:52:15 +00001282 length -= ZEBRA_HEADER_SIZE;
1283
paul718e3742002-12-13 20:15:29 +00001284 /* Debug packet information. */
1285 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001286 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001287
1288 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001289 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001290 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001291
1292 switch (command)
1293 {
hasso18a6dce2004-10-03 18:18:34 +00001294 case ZEBRA_ROUTER_ID_ADD:
1295 zread_router_id_add (client, length);
1296 break;
1297 case ZEBRA_ROUTER_ID_DELETE:
1298 zread_router_id_delete (client, length);
1299 break;
paul718e3742002-12-13 20:15:29 +00001300 case ZEBRA_INTERFACE_ADD:
1301 zread_interface_add (client, length);
1302 break;
1303 case ZEBRA_INTERFACE_DELETE:
1304 zread_interface_delete (client, length);
1305 break;
1306 case ZEBRA_IPV4_ROUTE_ADD:
1307 zread_ipv4_add (client, length);
1308 break;
1309 case ZEBRA_IPV4_ROUTE_DELETE:
1310 zread_ipv4_delete (client, length);
1311 break;
1312#ifdef HAVE_IPV6
1313 case ZEBRA_IPV6_ROUTE_ADD:
1314 zread_ipv6_add (client, length);
1315 break;
1316 case ZEBRA_IPV6_ROUTE_DELETE:
1317 zread_ipv6_delete (client, length);
1318 break;
1319#endif /* HAVE_IPV6 */
1320 case ZEBRA_REDISTRIBUTE_ADD:
1321 zebra_redistribute_add (command, client, length);
1322 break;
1323 case ZEBRA_REDISTRIBUTE_DELETE:
1324 zebra_redistribute_delete (command, client, length);
1325 break;
1326 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1327 zebra_redistribute_default_add (command, client, length);
1328 break;
1329 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1330 zebra_redistribute_default_delete (command, client, length);
1331 break;
1332 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1333 zread_ipv4_nexthop_lookup (client, length);
1334 break;
1335#ifdef HAVE_IPV6
1336 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1337 zread_ipv6_nexthop_lookup (client, length);
1338 break;
1339#endif /* HAVE_IPV6 */
1340 case ZEBRA_IPV4_IMPORT_LOOKUP:
1341 zread_ipv4_import_lookup (client, length);
1342 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001343 case ZEBRA_HELLO:
1344 zread_hello (client);
1345 break;
paul718e3742002-12-13 20:15:29 +00001346 default:
1347 zlog_info ("Zebra received unknown command %d", command);
1348 break;
1349 }
1350
ajs719e9742005-02-28 20:52:15 +00001351 if (client->t_suicide)
1352 {
1353 /* No need to wait for thread callback, just kill immediately. */
1354 zebra_client_close(client);
1355 return -1;
1356 }
1357
paul718e3742002-12-13 20:15:29 +00001358 stream_reset (client->ibuf);
1359 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001360 return 0;
1361}
1362
paul718e3742002-12-13 20:15:29 +00001363
1364/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001365static int
paul718e3742002-12-13 20:15:29 +00001366zebra_accept (struct thread *thread)
1367{
1368 int accept_sock;
1369 int client_sock;
1370 struct sockaddr_in client;
1371 socklen_t len;
1372
1373 accept_sock = THREAD_FD (thread);
1374
ajs719e9742005-02-28 20:52:15 +00001375 /* Reregister myself. */
1376 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1377
paul718e3742002-12-13 20:15:29 +00001378 len = sizeof (struct sockaddr_in);
1379 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1380
1381 if (client_sock < 0)
1382 {
ajs6099b3b2004-11-20 02:06:59 +00001383 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001384 return -1;
1385 }
1386
paulccf35572003-03-01 11:42:20 +00001387 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001388 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001389
paul718e3742002-12-13 20:15:29 +00001390 /* Create new zebra client. */
1391 zebra_client_create (client_sock);
1392
paul718e3742002-12-13 20:15:29 +00001393 return 0;
1394}
1395
paulb9df2d22004-05-09 09:09:59 +00001396#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001397/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001398static void
paul718e3742002-12-13 20:15:29 +00001399zebra_serv ()
1400{
1401 int ret;
1402 int accept_sock;
1403 struct sockaddr_in addr;
1404
1405 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1406
1407 if (accept_sock < 0)
1408 {
paul3d1dc852005-04-05 00:45:23 +00001409 zlog_warn ("Can't create zserv stream socket: %s",
1410 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001411 zlog_warn ("zebra can't provice full functionality due to above error");
1412 return;
1413 }
1414
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001415 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001416 memset (&addr, 0, sizeof (struct sockaddr_in));
1417 addr.sin_family = AF_INET;
1418 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001419#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001420 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001421#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001422 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1423
1424 sockopt_reuseaddr (accept_sock);
1425 sockopt_reuseport (accept_sock);
1426
pauledd7c242003-06-04 13:59:38 +00001427 if ( zserv_privs.change(ZPRIVS_RAISE) )
1428 zlog (NULL, LOG_ERR, "Can't raise privileges");
1429
paul718e3742002-12-13 20:15:29 +00001430 ret = bind (accept_sock, (struct sockaddr *)&addr,
1431 sizeof (struct sockaddr_in));
1432 if (ret < 0)
1433 {
paul3d1dc852005-04-05 00:45:23 +00001434 zlog_warn ("Can't bind to stream socket: %s",
1435 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001436 zlog_warn ("zebra can't provice full functionality due to above error");
1437 close (accept_sock); /* Avoid sd leak. */
1438 return;
1439 }
pauledd7c242003-06-04 13:59:38 +00001440
1441 if ( zserv_privs.change(ZPRIVS_LOWER) )
1442 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001443
1444 ret = listen (accept_sock, 1);
1445 if (ret < 0)
1446 {
paul3d1dc852005-04-05 00:45:23 +00001447 zlog_warn ("Can't listen to stream socket: %s",
1448 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001449 zlog_warn ("zebra can't provice full functionality due to above error");
1450 close (accept_sock); /* Avoid sd leak. */
1451 return;
1452 }
1453
1454 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1455}
paulb9df2d22004-05-09 09:09:59 +00001456#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001457
1458/* For sockaddr_un. */
1459#include <sys/un.h>
1460
1461/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001462static void
hassofce954f2004-10-07 20:29:24 +00001463zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001464{
1465 int ret;
1466 int sock, len;
1467 struct sockaddr_un serv;
1468 mode_t old_mask;
1469
1470 /* First of all, unlink existing socket */
1471 unlink (path);
1472
1473 /* Set umask */
1474 old_mask = umask (0077);
1475
1476 /* Make UNIX domain socket. */
1477 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1478 if (sock < 0)
1479 {
paul3d1dc852005-04-05 00:45:23 +00001480 zlog_warn ("Can't create zserv unix socket: %s",
1481 safe_strerror (errno));
1482 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001483 return;
1484 }
1485
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001486 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1487
paul718e3742002-12-13 20:15:29 +00001488 /* Make server socket. */
1489 memset (&serv, 0, sizeof (struct sockaddr_un));
1490 serv.sun_family = AF_UNIX;
1491 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001492#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001493 len = serv.sun_len = SUN_LEN(&serv);
1494#else
1495 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001496#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001497
1498 ret = bind (sock, (struct sockaddr *) &serv, len);
1499 if (ret < 0)
1500 {
paul3d1dc852005-04-05 00:45:23 +00001501 zlog_warn ("Can't bind to unix socket %s: %s",
1502 path, safe_strerror (errno));
1503 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001504 close (sock);
1505 return;
1506 }
1507
1508 ret = listen (sock, 5);
1509 if (ret < 0)
1510 {
paul3d1dc852005-04-05 00:45:23 +00001511 zlog_warn ("Can't listen to unix socket %s: %s",
1512 path, safe_strerror (errno));
1513 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001514 close (sock);
1515 return;
1516 }
1517
1518 umask (old_mask);
1519
1520 zebra_event (ZEBRA_SERV, sock, NULL);
1521}
1522
paul718e3742002-12-13 20:15:29 +00001523
paulb9df2d22004-05-09 09:09:59 +00001524static void
paul718e3742002-12-13 20:15:29 +00001525zebra_event (enum event event, int sock, struct zserv *client)
1526{
1527 switch (event)
1528 {
1529 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001530 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001531 break;
1532 case ZEBRA_READ:
1533 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001534 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001535 break;
1536 case ZEBRA_WRITE:
1537 /**/
1538 break;
1539 }
1540}
1541
1542/* Display default rtm_table for all clients. */
1543DEFUN (show_table,
1544 show_table_cmd,
1545 "show table",
1546 SHOW_STR
1547 "default routing table to use for all clients\n")
1548{
paulb21b19c2003-06-15 01:28:29 +00001549 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001550 VTY_NEWLINE);
1551 return CMD_SUCCESS;
1552}
1553
1554DEFUN (config_table,
1555 config_table_cmd,
1556 "table TABLENO",
1557 "Configure target kernel routing table\n"
1558 "TABLE integer\n")
1559{
paulb21b19c2003-06-15 01:28:29 +00001560 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001561 return CMD_SUCCESS;
1562}
1563
hasso647e4f12003-05-25 11:43:52 +00001564DEFUN (ip_forwarding,
1565 ip_forwarding_cmd,
1566 "ip forwarding",
1567 IP_STR
1568 "Turn on IP forwarding")
1569{
1570 int ret;
1571
1572 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001573 if (ret == 0)
1574 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001575
hasso647e4f12003-05-25 11:43:52 +00001576 if (ret == 0)
1577 {
1578 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1579 return CMD_WARNING;
1580 }
1581
1582 return CMD_SUCCESS;
1583}
1584
paul718e3742002-12-13 20:15:29 +00001585DEFUN (no_ip_forwarding,
1586 no_ip_forwarding_cmd,
1587 "no ip forwarding",
1588 NO_STR
1589 IP_STR
1590 "Turn off IP forwarding")
1591{
1592 int ret;
1593
1594 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001595 if (ret != 0)
1596 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001597
paul718e3742002-12-13 20:15:29 +00001598 if (ret != 0)
1599 {
1600 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1601 return CMD_WARNING;
1602 }
1603
1604 return CMD_SUCCESS;
1605}
1606
1607/* This command is for debugging purpose. */
1608DEFUN (show_zebra_client,
1609 show_zebra_client_cmd,
1610 "show zebra client",
1611 SHOW_STR
1612 "Zebra information"
1613 "Client information")
1614{
hasso52dc7ee2004-09-23 19:18:23 +00001615 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001616 struct zserv *client;
1617
paul1eb8ef22005-04-07 07:30:20 +00001618 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1619 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1620
paul718e3742002-12-13 20:15:29 +00001621 return CMD_SUCCESS;
1622}
1623
1624/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001625static int
paul718e3742002-12-13 20:15:29 +00001626config_write_table (struct vty *vty)
1627{
paulb21b19c2003-06-15 01:28:29 +00001628 if (zebrad.rtm_table_default)
1629 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001630 VTY_NEWLINE);
1631 return 0;
1632}
1633
1634/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001635static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001636{
1637 TABLE_NODE,
1638 "", /* This node has no interface. */
1639 1
1640};
1641
1642/* Only display ip forwarding is enabled or not. */
1643DEFUN (show_ip_forwarding,
1644 show_ip_forwarding_cmd,
1645 "show ip forwarding",
1646 SHOW_STR
1647 IP_STR
1648 "IP forwarding status\n")
1649{
1650 int ret;
1651
1652 ret = ipforward ();
1653
1654 if (ret == 0)
1655 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1656 else
1657 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1658 return CMD_SUCCESS;
1659}
1660
1661#ifdef HAVE_IPV6
1662/* Only display ipv6 forwarding is enabled or not. */
1663DEFUN (show_ipv6_forwarding,
1664 show_ipv6_forwarding_cmd,
1665 "show ipv6 forwarding",
1666 SHOW_STR
1667 "IPv6 information\n"
1668 "Forwarding status\n")
1669{
1670 int ret;
1671
1672 ret = ipforward_ipv6 ();
1673
1674 switch (ret)
1675 {
1676 case -1:
1677 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1678 break;
1679 case 0:
1680 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1681 break;
1682 case 1:
1683 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1684 break;
1685 default:
1686 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1687 break;
1688 }
1689 return CMD_SUCCESS;
1690}
1691
hasso55906722004-02-11 22:42:16 +00001692DEFUN (ipv6_forwarding,
1693 ipv6_forwarding_cmd,
1694 "ipv6 forwarding",
1695 IPV6_STR
1696 "Turn on IPv6 forwarding")
1697{
1698 int ret;
1699
hasso41d3fc92004-04-06 11:59:00 +00001700 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001701 if (ret == 0)
1702 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001703
hasso41d3fc92004-04-06 11:59:00 +00001704 if (ret == 0)
1705 {
hasso55906722004-02-11 22:42:16 +00001706 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1707 return CMD_WARNING;
1708 }
1709
1710 return CMD_SUCCESS;
1711}
1712
paul718e3742002-12-13 20:15:29 +00001713DEFUN (no_ipv6_forwarding,
1714 no_ipv6_forwarding_cmd,
1715 "no ipv6 forwarding",
1716 NO_STR
hasso55906722004-02-11 22:42:16 +00001717 IPV6_STR
1718 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001719{
1720 int ret;
1721
hasso41d3fc92004-04-06 11:59:00 +00001722 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001723 if (ret != 0)
1724 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001725
paul718e3742002-12-13 20:15:29 +00001726 if (ret != 0)
1727 {
1728 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1729 return CMD_WARNING;
1730 }
1731
1732 return CMD_SUCCESS;
1733}
1734
1735#endif /* HAVE_IPV6 */
1736
1737/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001738static int
paul718e3742002-12-13 20:15:29 +00001739config_write_forwarding (struct vty *vty)
1740{
hasso18a6dce2004-10-03 18:18:34 +00001741 /* FIXME: Find better place for that. */
1742 router_id_write (vty);
1743
paul3e0b3a52004-08-23 18:58:32 +00001744 if (ipforward ())
1745 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001746#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001747 if (ipforward_ipv6 ())
1748 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001749#endif /* HAVE_IPV6 */
1750 vty_out (vty, "!%s", VTY_NEWLINE);
1751 return 0;
1752}
1753
1754/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001755static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001756{
1757 FORWARDING_NODE,
1758 "", /* This node has no interface. */
1759 1
1760};
1761
1762
1763/* Initialisation of zebra and installation of commands. */
1764void
paula1ac18c2005-06-28 17:17:12 +00001765zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001766{
1767 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001768 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001769
paul718e3742002-12-13 20:15:29 +00001770 /* Install configuration write function. */
1771 install_node (&table_node, config_write_table);
1772 install_node (&forwarding_node, config_write_forwarding);
1773
1774 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1775 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001776 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001777 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1778 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1779
1780#ifdef HAVE_NETLINK
1781 install_element (VIEW_NODE, &show_table_cmd);
1782 install_element (ENABLE_NODE, &show_table_cmd);
1783 install_element (CONFIG_NODE, &config_table_cmd);
1784#endif /* HAVE_NETLINK */
1785
1786#ifdef HAVE_IPV6
1787 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1788 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001789 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001790 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1791#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001792
1793 /* Route-map */
1794 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001795}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001796
1797/* Make zebra server socket, wiping any existing one (see bug #403). */
1798void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001799zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001800{
1801#ifdef HAVE_TCP_ZEBRA
1802 zebra_serv ();
1803#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001804 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001805#endif /* HAVE_TCP_ZEBRA */
1806}