blob: 2330135a24d6bd502de1aa7c78315d3d6a33cdb7 [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
ajs719e9742005-02-28 20:52:15 +000067static int
68zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000069{
ajs719e9742005-02-28 20:52:15 +000070 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000071
ajs719e9742005-02-28 20:52:15 +000072 client->t_write = NULL;
73 if (client->t_suicide)
74 {
75 zebra_client_close(client);
76 return -1;
77 }
78 switch (buffer_flush_available(client->wb, client->sock))
79 {
80 case BUFFER_ERROR:
81 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
82 "closing", __func__, client->sock);
83 zebra_client_close(client);
84 break;
85 case BUFFER_PENDING:
86 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
87 client, client->sock);
88 break;
89 case BUFFER_EMPTY:
90 break;
91 }
92 return 0;
paulccf35572003-03-01 11:42:20 +000093}
94
ajs719e9742005-02-28 20:52:15 +000095static int
96zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +000097{
ajs719e9742005-02-28 20:52:15 +000098 if (client->t_suicide)
99 return -1;
100 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
101 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000102 {
ajs719e9742005-02-28 20:52:15 +0000103 case BUFFER_ERROR:
104 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
105 __func__, client->sock);
106 /* Schedule a delayed close since many of the functions that call this
107 one do not check the return code. They do not allow for the
108 possibility that an I/O error may have caused the client to be
109 deleted. */
110 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
111 client, 0);
112 return -1;
ajs719e9742005-02-28 20:52:15 +0000113 case BUFFER_EMPTY:
114 THREAD_OFF(client->t_write);
115 break;
116 case BUFFER_PENDING:
117 THREAD_WRITE_ON(zebrad.master, client->t_write,
118 zserv_flush_data, client, client->sock);
119 break;
paulccf35572003-03-01 11:42:20 +0000120 }
paulccf35572003-03-01 11:42:20 +0000121 return 0;
122}
123
paulc1b98002006-01-16 01:54:02 +0000124static void
125zserv_create_header (struct stream *s, uint16_t cmd)
126{
127 /* length placeholder, caller can update */
128 stream_putw (s, ZEBRA_HEADER_SIZE);
129 stream_putc (s, ZEBRA_HEADER_MARKER);
130 stream_putc (s, ZSERV_VERSION);
131 stream_putw (s, cmd);
132}
133
paul718e3742002-12-13 20:15:29 +0000134/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000135/*
136 * This function is called in the following situations:
137 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
138 * from the client.
139 * - at startup, when zebra figures out the available interfaces
140 * - when an interface is added (where support for
141 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
142 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
143 * received)
144 */
paul718e3742002-12-13 20:15:29 +0000145int
146zsend_interface_add (struct zserv *client, struct interface *ifp)
147{
148 struct stream *s;
149
150 /* Check this client need interface information. */
151 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000152 return 0;
paul718e3742002-12-13 20:15:29 +0000153
154 s = client->obuf;
155 stream_reset (s);
156
paul718e3742002-12-13 20:15:29 +0000157 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000158 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
paul718e3742002-12-13 20:15:29 +0000159
160 /* Interface information. */
161 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
162 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000163 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000164 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000165 stream_putl (s, ifp->metric);
166 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000167 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000168 stream_putl (s, ifp->bandwidth);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000169#ifdef HAVE_STRUCT_SOCKADDR_DL
paul718e3742002-12-13 20:15:29 +0000170 stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
171#else
172 stream_putl (s, ifp->hw_addr_len);
173 if (ifp->hw_addr_len)
174 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000175#endif /* HAVE_STRUCT_SOCKADDR_DL */
paul718e3742002-12-13 20:15:29 +0000176
177 /* Write packet size. */
178 stream_putw_at (s, 0, stream_get_endp (s));
179
ajs719e9742005-02-28 20:52:15 +0000180 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000181}
182
183/* Interface deletion from zebra daemon. */
184int
185zsend_interface_delete (struct zserv *client, struct interface *ifp)
186{
187 struct stream *s;
188
189 /* Check this client need interface information. */
190 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000191 return 0;
paul718e3742002-12-13 20:15:29 +0000192
193 s = client->obuf;
194 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000195
196 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
197
paul718e3742002-12-13 20:15:29 +0000198 /* Interface information. */
paul718e3742002-12-13 20:15:29 +0000199 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
200 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000201 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000202 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000203 stream_putl (s, ifp->metric);
204 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000205 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000206 stream_putl (s, ifp->bandwidth);
207
208 /* Write packet length. */
209 stream_putw_at (s, 0, stream_get_endp (s));
210
ajs719e9742005-02-28 20:52:15 +0000211 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000212}
213
paulb9df2d22004-05-09 09:09:59 +0000214/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
215 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
216 *
217 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
218 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
219 * from the client, after the ZEBRA_INTERFACE_ADD has been
220 * sent from zebra to the client
221 * - redistribute new address info to all clients in the following situations
222 * - at startup, when zebra figures out the available interfaces
223 * - when an interface is added (where support for
224 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
225 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
226 * received)
227 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
228 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
229 * - when an RTM_NEWADDR message is received from the kernel,
230 *
231 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
232 *
233 * zsend_interface_address(DELETE)
234 * ^
235 * |
236 * zebra_interface_address_delete_update
237 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000238 * | | if_delete_update
239 * | |
paulb9df2d22004-05-09 09:09:59 +0000240 * ip_address_uninstall connected_delete_ipv4
241 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
242 * ^ ^
243 * | |
244 * | RTM_NEWADDR on routing/netlink socket
245 * |
246 * vty commands:
247 * "no ip address A.B.C.D/M [label LINE]"
248 * "no ip address A.B.C.D/M secondary"
249 * ["no ipv6 address X:X::X:X/M"]
250 *
251 */
paul718e3742002-12-13 20:15:29 +0000252int
paulb9df2d22004-05-09 09:09:59 +0000253zsend_interface_address (int cmd, struct zserv *client,
254 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000255{
256 int blen;
257 struct stream *s;
258 struct prefix *p;
259
260 /* Check this client need interface information. */
261 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000262 return 0;
paul718e3742002-12-13 20:15:29 +0000263
264 s = client->obuf;
265 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000266
267 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000268 stream_putl (s, ifp->ifindex);
269
270 /* Interface address flag. */
271 stream_putc (s, ifc->flags);
272
273 /* Prefix information. */
274 p = ifc->address;
275 stream_putc (s, p->family);
276 blen = prefix_blen (p);
277 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000278
279 /*
280 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
281 * but zebra_interface_address_delete_read() in the gnu version
282 * expects to find it
283 */
paul718e3742002-12-13 20:15:29 +0000284 stream_putc (s, p->prefixlen);
285
286 /* Destination. */
287 p = ifc->destination;
288 if (p)
289 stream_put (s, &p->u.prefix, blen);
290 else
291 stream_put (s, NULL, blen);
292
293 /* Write packet size. */
294 stream_putw_at (s, 0, stream_get_endp (s));
295
ajs719e9742005-02-28 20:52:15 +0000296 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000297}
298
paulb9df2d22004-05-09 09:09:59 +0000299/*
300 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
301 * ZEBRA_INTERFACE_DOWN.
302 *
303 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
304 * the clients in one of 2 situations:
305 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
306 * - a vty command modifying the bandwidth of an interface is received.
307 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
308 */
paul718e3742002-12-13 20:15:29 +0000309int
paulb9df2d22004-05-09 09:09:59 +0000310zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000311{
312 struct stream *s;
313
314 /* Check this client need interface information. */
315 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000316 return 0;
paul718e3742002-12-13 20:15:29 +0000317
318 s = client->obuf;
319 stream_reset (s);
320
paulc1b98002006-01-16 01:54:02 +0000321 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000322
323 /* Interface information. */
324 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
325 stream_putl (s, ifp->ifindex);
paul2e3b2e42002-12-13 21:03:13 +0000326 stream_putc (s, ifp->status);
paulc77d4542006-01-11 01:59:04 +0000327 stream_putq (s, ifp->flags);
paul718e3742002-12-13 20:15:29 +0000328 stream_putl (s, ifp->metric);
329 stream_putl (s, ifp->mtu);
paulb9df2d22004-05-09 09:09:59 +0000330 stream_putl (s, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000331 stream_putl (s, ifp->bandwidth);
332
333 /* Write packet size. */
334 stream_putw_at (s, 0, stream_get_endp (s));
335
ajs719e9742005-02-28 20:52:15 +0000336 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000337}
338
paulb9df2d22004-05-09 09:09:59 +0000339/*
340 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
341 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
342 * situations:
343 * - when the client starts up, and requests default information
344 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
345 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
346 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
347 * - when the zebra server redistributes routes after it updates its rib
348 *
349 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
350 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
351 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
352 * - deleted from zebra's rib, and this info
353 * has to be redistributed to the clients
354 *
355 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
356 * zebra server when the client wants to tell the zebra server to add a
357 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
358 * same message being sent back and forth, this function and
359 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
360 * duplication.
361 */
paul718e3742002-12-13 20:15:29 +0000362int
paulb9df2d22004-05-09 09:09:59 +0000363zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
364 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000365{
366 int psize;
367 struct stream *s;
368 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000369 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000370 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000371 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000372
paul718e3742002-12-13 20:15:29 +0000373 s = client->obuf;
374 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000375
376 zserv_create_header (s, cmd);
377
378 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000379 stream_putc (s, rib->type);
380 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000381
382 /* marker for message flags field */
383 messmark = stream_get_endp (s);
384 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000385
386 /* Prefix. */
387 psize = PSIZE (p->prefixlen);
388 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000389 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000390
paulb9df2d22004-05-09 09:09:59 +0000391 /*
392 * XXX The message format sent by zebra below does not match the format
393 * of the corresponding message expected by the zebra server
394 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
395 * (is there a bug on the client side if more than one segment is sent?)
396 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
397 * is hard-coded.
398 */
paul718e3742002-12-13 20:15:29 +0000399 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000400
paul718e3742002-12-13 20:15:29 +0000401 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
402 {
403 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000404 {
paul1dcb5172005-05-31 08:38:50 +0000405 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
406 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
407
408 if (nhnummark == 0)
409 {
410 nhnummark = stream_get_endp (s);
411 stream_putc (s, 1); /* placeholder */
412 }
413
paulb9df2d22004-05-09 09:09:59 +0000414 nhnum++;
paul718e3742002-12-13 20:15:29 +0000415
paulb9df2d22004-05-09 09:09:59 +0000416 switch(nexthop->type)
417 {
418 case NEXTHOP_TYPE_IPV4:
419 case NEXTHOP_TYPE_IPV4_IFINDEX:
420 stream_put_in_addr (s, &nexthop->gate.ipv4);
421 break;
422#ifdef HAVE_IPV6
423 case NEXTHOP_TYPE_IPV6:
424 case NEXTHOP_TYPE_IPV6_IFINDEX:
425 case NEXTHOP_TYPE_IPV6_IFNAME:
426 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
427 break;
428#endif
429 default:
430 if (cmd == ZEBRA_IPV4_ROUTE_ADD
431 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
432 {
433 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000434 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000435 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
436 }
437 else
438 {
439 struct in6_addr empty;
440 memset (&empty, 0, sizeof (struct in6_addr));
441 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
442 }
443 }
paul718e3742002-12-13 20:15:29 +0000444
paulb9df2d22004-05-09 09:09:59 +0000445 /* Interface index. */
446 stream_putc (s, 1);
447 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000448
paulb9df2d22004-05-09 09:09:59 +0000449 break;
450 }
paul718e3742002-12-13 20:15:29 +0000451 }
452
453 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700454 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000455 {
vincentfbf5d032005-09-29 11:25:50 +0000456 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
457 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000458 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
459 stream_putl (s, rib->metric);
460 }
461
462 /* write real message flags value */
463 stream_putc_at (s, messmark, zapi_flags);
464
paulb9df2d22004-05-09 09:09:59 +0000465 /* Write next-hop number */
466 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000467 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000468
paul718e3742002-12-13 20:15:29 +0000469 /* Write packet size. */
470 stream_putw_at (s, 0, stream_get_endp (s));
471
ajs719e9742005-02-28 20:52:15 +0000472 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000473}
474
paul718e3742002-12-13 20:15:29 +0000475#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000476static int
paul718e3742002-12-13 20:15:29 +0000477zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
478{
479 struct stream *s;
480 struct rib *rib;
481 unsigned long nump;
482 u_char num;
483 struct nexthop *nexthop;
484
485 /* Lookup nexthop. */
486 rib = rib_match_ipv6 (addr);
487
488 /* Get output stream. */
489 s = client->obuf;
490 stream_reset (s);
491
492 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000493 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000494 stream_put (s, &addr, 16);
495
496 if (rib)
497 {
498 stream_putl (s, rib->metric);
499 num = 0;
paul9985f832005-02-09 15:51:56 +0000500 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000501 stream_putc (s, 0);
502 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
503 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
504 {
505 stream_putc (s, nexthop->type);
506 switch (nexthop->type)
507 {
508 case ZEBRA_NEXTHOP_IPV6:
509 stream_put (s, &nexthop->gate.ipv6, 16);
510 break;
511 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
512 case ZEBRA_NEXTHOP_IPV6_IFNAME:
513 stream_put (s, &nexthop->gate.ipv6, 16);
514 stream_putl (s, nexthop->ifindex);
515 break;
516 case ZEBRA_NEXTHOP_IFINDEX:
517 case ZEBRA_NEXTHOP_IFNAME:
518 stream_putl (s, nexthop->ifindex);
519 break;
hassofa2b17e2004-03-04 17:45:00 +0000520 default:
521 /* do nothing */
522 break;
paul718e3742002-12-13 20:15:29 +0000523 }
524 num++;
525 }
526 stream_putc_at (s, nump, num);
527 }
528 else
529 {
530 stream_putl (s, 0);
531 stream_putc (s, 0);
532 }
533
534 stream_putw_at (s, 0, stream_get_endp (s));
535
ajs719e9742005-02-28 20:52:15 +0000536 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000537}
538#endif /* HAVE_IPV6 */
539
paulb9df2d22004-05-09 09:09:59 +0000540static int
paul718e3742002-12-13 20:15:29 +0000541zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
542{
543 struct stream *s;
544 struct rib *rib;
545 unsigned long nump;
546 u_char num;
547 struct nexthop *nexthop;
548
549 /* Lookup nexthop. */
550 rib = rib_match_ipv4 (addr);
551
552 /* Get output stream. */
553 s = client->obuf;
554 stream_reset (s);
555
556 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000557 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000558 stream_put_in_addr (s, &addr);
559
560 if (rib)
561 {
562 stream_putl (s, rib->metric);
563 num = 0;
paul9985f832005-02-09 15:51:56 +0000564 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000565 stream_putc (s, 0);
566 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
567 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
568 {
569 stream_putc (s, nexthop->type);
570 switch (nexthop->type)
571 {
572 case ZEBRA_NEXTHOP_IPV4:
573 stream_put_in_addr (s, &nexthop->gate.ipv4);
574 break;
575 case ZEBRA_NEXTHOP_IFINDEX:
576 case ZEBRA_NEXTHOP_IFNAME:
577 stream_putl (s, nexthop->ifindex);
578 break;
hassofa2b17e2004-03-04 17:45:00 +0000579 default:
580 /* do nothing */
581 break;
paul718e3742002-12-13 20:15:29 +0000582 }
583 num++;
584 }
585 stream_putc_at (s, nump, num);
586 }
587 else
588 {
589 stream_putl (s, 0);
590 stream_putc (s, 0);
591 }
592
593 stream_putw_at (s, 0, stream_get_endp (s));
594
ajs719e9742005-02-28 20:52:15 +0000595 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000596}
597
paulb9df2d22004-05-09 09:09:59 +0000598static int
paul718e3742002-12-13 20:15:29 +0000599zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
600{
601 struct stream *s;
602 struct rib *rib;
603 unsigned long nump;
604 u_char num;
605 struct nexthop *nexthop;
606
607 /* Lookup nexthop. */
608 rib = rib_lookup_ipv4 (p);
609
610 /* Get output stream. */
611 s = client->obuf;
612 stream_reset (s);
613
614 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000615 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000616 stream_put_in_addr (s, &p->prefix);
617
618 if (rib)
619 {
620 stream_putl (s, rib->metric);
621 num = 0;
paul9985f832005-02-09 15:51:56 +0000622 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000623 stream_putc (s, 0);
624 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
625 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
626 {
627 stream_putc (s, nexthop->type);
628 switch (nexthop->type)
629 {
630 case ZEBRA_NEXTHOP_IPV4:
631 stream_put_in_addr (s, &nexthop->gate.ipv4);
632 break;
633 case ZEBRA_NEXTHOP_IFINDEX:
634 case ZEBRA_NEXTHOP_IFNAME:
635 stream_putl (s, nexthop->ifindex);
636 break;
hassofa2b17e2004-03-04 17:45:00 +0000637 default:
638 /* do nothing */
639 break;
paul718e3742002-12-13 20:15:29 +0000640 }
641 num++;
642 }
643 stream_putc_at (s, nump, num);
644 }
645 else
646 {
647 stream_putl (s, 0);
648 stream_putc (s, 0);
649 }
650
651 stream_putw_at (s, 0, stream_get_endp (s));
652
ajs719e9742005-02-28 20:52:15 +0000653 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000654}
655
hasso18a6dce2004-10-03 18:18:34 +0000656/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
657int
658zsend_router_id_update (struct zserv *client, struct prefix *p)
659{
660 struct stream *s;
661 int blen;
662
663 /* Check this client need interface information. */
664 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000665 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000666
667 s = client->obuf;
668 stream_reset (s);
669
hasso18a6dce2004-10-03 18:18:34 +0000670 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000671 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000672
673 /* Prefix information. */
674 stream_putc (s, p->family);
675 blen = prefix_blen (p);
676 stream_put (s, &p->u.prefix, blen);
677 stream_putc (s, p->prefixlen);
678
679 /* Write packet size. */
680 stream_putw_at (s, 0, stream_get_endp (s));
681
ajs719e9742005-02-28 20:52:15 +0000682 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000683}
684
paul718e3742002-12-13 20:15:29 +0000685/* Register zebra server interface information. Send current all
686 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000687static int
paul718e3742002-12-13 20:15:29 +0000688zread_interface_add (struct zserv *client, u_short length)
689{
paul1eb8ef22005-04-07 07:30:20 +0000690 struct listnode *ifnode, *ifnnode;
691 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000692 struct interface *ifp;
693 struct connected *c;
694
695 /* Interface information is needed. */
696 client->ifinfo = 1;
697
paul1eb8ef22005-04-07 07:30:20 +0000698 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000699 {
paul718e3742002-12-13 20:15:29 +0000700 /* Skip pseudo interface. */
701 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
702 continue;
703
ajs719e9742005-02-28 20:52:15 +0000704 if (zsend_interface_add (client, ifp) < 0)
705 return -1;
paul718e3742002-12-13 20:15:29 +0000706
paul1eb8ef22005-04-07 07:30:20 +0000707 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000708 {
ajs719e9742005-02-28 20:52:15 +0000709 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
710 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
711 ifp, c) < 0))
712 return -1;
paul718e3742002-12-13 20:15:29 +0000713 }
714 }
ajs719e9742005-02-28 20:52:15 +0000715 return 0;
paul718e3742002-12-13 20:15:29 +0000716}
717
718/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000719static int
paul718e3742002-12-13 20:15:29 +0000720zread_interface_delete (struct zserv *client, u_short length)
721{
722 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000723 return 0;
paul718e3742002-12-13 20:15:29 +0000724}
725
726/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000727/*
728 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
729 * add kernel route.
730 */
ajs719e9742005-02-28 20:52:15 +0000731static int
paul718e3742002-12-13 20:15:29 +0000732zread_ipv4_add (struct zserv *client, u_short length)
733{
734 int i;
735 struct rib *rib;
736 struct prefix_ipv4 p;
737 u_char message;
738 struct in_addr nexthop;
739 u_char nexthop_num;
740 u_char nexthop_type;
741 struct stream *s;
742 unsigned int ifindex;
743 u_char ifname_len;
744
745 /* Get input stream. */
746 s = client->ibuf;
747
748 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000749 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
750
paul718e3742002-12-13 20:15:29 +0000751 /* Type, flags, message. */
752 rib->type = stream_getc (s);
Josh Baileyaf56d402012-03-21 18:47:51 -0700753 /* Update client's route type if it is not done yet. */
754 /* It is done here since only zread_ipv4/6_add() and
755 * zread_ipv4/6_delete() decode Zebra messages and retrieve
756 * route types. */
757 if (client->route_type == ZEBRA_ROUTE_MAX)
758 client->route_type = rib->type;
759
paul718e3742002-12-13 20:15:29 +0000760 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000761 message = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000762 rib->uptime = time (NULL);
763
764 /* IPv4 prefix. */
765 memset (&p, 0, sizeof (struct prefix_ipv4));
766 p.family = AF_INET;
767 p.prefixlen = stream_getc (s);
768 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
769
770 /* Nexthop parse. */
771 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
772 {
773 nexthop_num = stream_getc (s);
774
775 for (i = 0; i < nexthop_num; i++)
776 {
777 nexthop_type = stream_getc (s);
778
779 switch (nexthop_type)
780 {
781 case ZEBRA_NEXTHOP_IFINDEX:
782 ifindex = stream_getl (s);
783 nexthop_ifindex_add (rib, ifindex);
784 break;
785 case ZEBRA_NEXTHOP_IFNAME:
786 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000787 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000788 break;
789 case ZEBRA_NEXTHOP_IPV4:
790 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000791 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000792 break;
793 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000794 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000795 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700796 case ZEBRA_NEXTHOP_BLACKHOLE:
797 nexthop_blackhole_add (rib);
798 break;
799 }
paul718e3742002-12-13 20:15:29 +0000800 }
801 }
802
803 /* Distance. */
804 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
805 rib->distance = stream_getc (s);
806
807 /* Metric. */
808 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
809 rib->metric = stream_getl (s);
810
Paul Jakma171eee32006-07-27 16:11:02 +0000811 /* Table */
812 rib->table=zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +0000813 rib_add_ipv4_multipath (&p, rib);
ajs719e9742005-02-28 20:52:15 +0000814 return 0;
paul718e3742002-12-13 20:15:29 +0000815}
816
817/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000818static int
paul718e3742002-12-13 20:15:29 +0000819zread_ipv4_delete (struct zserv *client, u_short length)
820{
821 int i;
822 struct stream *s;
823 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700824 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000825 unsigned long ifindex;
826 struct prefix_ipv4 p;
827 u_char nexthop_num;
828 u_char nexthop_type;
829 u_char ifname_len;
830
831 s = client->ibuf;
832 ifindex = 0;
833 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700834 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000835
836 /* Type, flags, message. */
837 api.type = stream_getc (s);
838 api.flags = stream_getc (s);
839 api.message = stream_getc (s);
840
841 /* IPv4 prefix. */
842 memset (&p, 0, sizeof (struct prefix_ipv4));
843 p.family = AF_INET;
844 p.prefixlen = stream_getc (s);
845 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
846
847 /* Nexthop, ifindex, distance, metric. */
848 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
849 {
850 nexthop_num = stream_getc (s);
851
852 for (i = 0; i < nexthop_num; i++)
853 {
854 nexthop_type = stream_getc (s);
855
856 switch (nexthop_type)
857 {
858 case ZEBRA_NEXTHOP_IFINDEX:
859 ifindex = stream_getl (s);
860 break;
861 case ZEBRA_NEXTHOP_IFNAME:
862 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000863 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000864 break;
865 case ZEBRA_NEXTHOP_IPV4:
866 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700867 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000868 break;
869 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000870 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000871 break;
872 }
873 }
874 }
875
876 /* Distance. */
877 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
878 api.distance = stream_getc (s);
879 else
880 api.distance = 0;
881
882 /* Metric. */
883 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
884 api.metric = stream_getl (s);
885 else
886 api.metric = 0;
887
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700888 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
paul718e3742002-12-13 20:15:29 +0000889 client->rtm_table);
ajs719e9742005-02-28 20:52:15 +0000890 return 0;
paul718e3742002-12-13 20:15:29 +0000891}
892
893/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000894static int
paul718e3742002-12-13 20:15:29 +0000895zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
896{
897 struct in_addr addr;
898
899 addr.s_addr = stream_get_ipv4 (client->ibuf);
ajs719e9742005-02-28 20:52:15 +0000900 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000901}
902
903/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000904static int
paul718e3742002-12-13 20:15:29 +0000905zread_ipv4_import_lookup (struct zserv *client, u_short length)
906{
907 struct prefix_ipv4 p;
908
909 p.family = AF_INET;
910 p.prefixlen = stream_getc (client->ibuf);
911 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
912
ajs719e9742005-02-28 20:52:15 +0000913 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000914}
915
916#ifdef HAVE_IPV6
917/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000918static int
paul718e3742002-12-13 20:15:29 +0000919zread_ipv6_add (struct zserv *client, u_short length)
920{
921 int i;
922 struct stream *s;
923 struct zapi_ipv6 api;
924 struct in6_addr nexthop;
925 unsigned long ifindex;
926 struct prefix_ipv6 p;
927
928 s = client->ibuf;
929 ifindex = 0;
930 memset (&nexthop, 0, sizeof (struct in6_addr));
931
932 /* Type, flags, message. */
933 api.type = stream_getc (s);
Josh Baileyaf56d402012-03-21 18:47:51 -0700934 /* Update the route type of the client.
935 * Same as in zread_ipv4_add(). */
936 if (client->route_type == ZEBRA_ROUTE_MAX)
937 client->route_type = api.type;
938
paul718e3742002-12-13 20:15:29 +0000939 api.flags = stream_getc (s);
940 api.message = stream_getc (s);
941
942 /* IPv4 prefix. */
943 memset (&p, 0, sizeof (struct prefix_ipv6));
944 p.family = AF_INET6;
945 p.prefixlen = stream_getc (s);
946 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
947
948 /* Nexthop, ifindex, distance, metric. */
949 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
950 {
951 u_char nexthop_type;
952
953 api.nexthop_num = stream_getc (s);
954 for (i = 0; i < api.nexthop_num; i++)
955 {
956 nexthop_type = stream_getc (s);
957
958 switch (nexthop_type)
959 {
960 case ZEBRA_NEXTHOP_IPV6:
961 stream_get (&nexthop, s, 16);
962 break;
963 case ZEBRA_NEXTHOP_IFINDEX:
964 ifindex = stream_getl (s);
965 break;
966 }
967 }
968 }
969
970 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
971 api.distance = stream_getc (s);
972 else
973 api.distance = 0;
974
975 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
976 api.metric = stream_getl (s);
977 else
978 api.metric = 0;
979
980 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100981 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000982 api.distance);
paul718e3742002-12-13 20:15:29 +0000983 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100984 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
hassobe61c4e2005-08-27 06:05:47 +0000985 api.distance);
ajs719e9742005-02-28 20:52:15 +0000986 return 0;
paul718e3742002-12-13 20:15:29 +0000987}
988
989/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000990static int
paul718e3742002-12-13 20:15:29 +0000991zread_ipv6_delete (struct zserv *client, u_short length)
992{
993 int i;
994 struct stream *s;
995 struct zapi_ipv6 api;
996 struct in6_addr nexthop;
997 unsigned long ifindex;
998 struct prefix_ipv6 p;
999
1000 s = client->ibuf;
1001 ifindex = 0;
1002 memset (&nexthop, 0, sizeof (struct in6_addr));
1003
1004 /* Type, flags, message. */
1005 api.type = stream_getc (s);
1006 api.flags = stream_getc (s);
1007 api.message = stream_getc (s);
1008
1009 /* IPv4 prefix. */
1010 memset (&p, 0, sizeof (struct prefix_ipv6));
1011 p.family = AF_INET6;
1012 p.prefixlen = stream_getc (s);
1013 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1014
1015 /* Nexthop, ifindex, distance, metric. */
1016 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1017 {
1018 u_char nexthop_type;
1019
1020 api.nexthop_num = stream_getc (s);
1021 for (i = 0; i < api.nexthop_num; i++)
1022 {
1023 nexthop_type = stream_getc (s);
1024
1025 switch (nexthop_type)
1026 {
1027 case ZEBRA_NEXTHOP_IPV6:
1028 stream_get (&nexthop, s, 16);
1029 break;
1030 case ZEBRA_NEXTHOP_IFINDEX:
1031 ifindex = stream_getl (s);
1032 break;
1033 }
1034 }
1035 }
1036
1037 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1038 api.distance = stream_getc (s);
1039 else
1040 api.distance = 0;
1041 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1042 api.metric = stream_getl (s);
1043 else
1044 api.metric = 0;
1045
1046 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001047 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table);
paul718e3742002-12-13 20:15:29 +00001048 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001049 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table);
ajs719e9742005-02-28 20:52:15 +00001050 return 0;
paul718e3742002-12-13 20:15:29 +00001051}
1052
ajs719e9742005-02-28 20:52:15 +00001053static int
paul718e3742002-12-13 20:15:29 +00001054zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1055{
1056 struct in6_addr addr;
1057 char buf[BUFSIZ];
1058
1059 stream_get (&addr, client->ibuf, 16);
1060 printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
1061
ajs719e9742005-02-28 20:52:15 +00001062 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001063}
1064#endif /* HAVE_IPV6 */
1065
hasso18a6dce2004-10-03 18:18:34 +00001066/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001067static int
hasso18a6dce2004-10-03 18:18:34 +00001068zread_router_id_add (struct zserv *client, u_short length)
1069{
1070 struct prefix p;
1071
1072 /* Router-id information is needed. */
1073 client->ridinfo = 1;
1074
1075 router_id_get (&p);
1076
ajs719e9742005-02-28 20:52:15 +00001077 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001078}
1079
1080/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001081static int
hasso18a6dce2004-10-03 18:18:34 +00001082zread_router_id_delete (struct zserv *client, u_short length)
1083{
1084 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001085 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001086}
1087
paul718e3742002-12-13 20:15:29 +00001088/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001089static void
paul718e3742002-12-13 20:15:29 +00001090zebra_client_close (struct zserv *client)
1091{
Josh Baileyaf56d402012-03-21 18:47:51 -07001092 struct stream *s;
1093
1094 /* Sweep all routes learned from the client first. */
1095 rib_sweep_client_route(client);
1096 /* Reset the route type. It may not be necessary since the
1097 * whole client will be freed. */
1098 client->route_type = ZEBRA_ROUTE_MAX;
1099
paul718e3742002-12-13 20:15:29 +00001100 /* Close file descriptor. */
1101 if (client->sock)
1102 {
1103 close (client->sock);
1104 client->sock = -1;
1105 }
1106
1107 /* Free stream buffers. */
1108 if (client->ibuf)
1109 stream_free (client->ibuf);
1110 if (client->obuf)
1111 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001112 if (client->wb)
1113 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001114
1115 /* Release threads. */
1116 if (client->t_read)
1117 thread_cancel (client->t_read);
1118 if (client->t_write)
1119 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001120 if (client->t_suicide)
1121 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001122
1123 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001124 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001125 XFREE (0, client);
1126}
1127
1128/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001129static void
paul718e3742002-12-13 20:15:29 +00001130zebra_client_create (int sock)
1131{
1132 struct zserv *client;
1133
1134 client = XCALLOC (0, sizeof (struct zserv));
1135
1136 /* Make client input/output buffer. */
1137 client->sock = sock;
Josh Baileyaf56d402012-03-21 18:47:51 -07001138 /* Set the default route type to ZEBRA_ROUTE_MAX; it will be updated
1139 * once new routes are received. */
1140 client->route_type = ZEBRA_ROUTE_MAX;
paul718e3742002-12-13 20:15:29 +00001141 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1142 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001143 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001144
1145 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001146 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001147
1148 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001149 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001150
1151 /* Make new read thread. */
1152 zebra_event (ZEBRA_READ, sock, client);
1153}
1154
1155/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001156static int
paul718e3742002-12-13 20:15:29 +00001157zebra_client_read (struct thread *thread)
1158{
1159 int sock;
1160 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001161 size_t already;
paulc1b98002006-01-16 01:54:02 +00001162 uint16_t length, command;
1163 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001164
1165 /* Get thread data. Reset reading thread because I'm running. */
1166 sock = THREAD_FD (thread);
1167 client = THREAD_ARG (thread);
1168 client->t_read = NULL;
1169
ajs719e9742005-02-28 20:52:15 +00001170 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001171 {
ajs719e9742005-02-28 20:52:15 +00001172 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001173 return -1;
1174 }
ajs719e9742005-02-28 20:52:15 +00001175
1176 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001177 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001178 {
ajs57a14772005-04-10 15:01:56 +00001179 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001180 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001181 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001182 (nbyte == -1))
1183 {
1184 if (IS_ZEBRA_DEBUG_EVENT)
1185 zlog_debug ("connection closed socket [%d]", sock);
1186 zebra_client_close (client);
1187 return -1;
1188 }
ajs57a14772005-04-10 15:01:56 +00001189 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001190 {
1191 /* Try again later. */
1192 zebra_event (ZEBRA_READ, sock, client);
1193 return 0;
1194 }
ajs57a14772005-04-10 15:01:56 +00001195 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001196 }
1197
1198 /* Reset to read from the beginning of the incoming packet. */
1199 stream_set_getp(client->ibuf, 0);
1200
paulc1b98002006-01-16 01:54:02 +00001201 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001202 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001203 marker = stream_getc (client->ibuf);
1204 version = stream_getc (client->ibuf);
1205 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001206
paulc1b98002006-01-16 01:54:02 +00001207 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1208 {
1209 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1210 __func__, sock, marker, version);
1211 zebra_client_close (client);
1212 return -1;
1213 }
ajs719e9742005-02-28 20:52:15 +00001214 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001215 {
ajs57a14772005-04-10 15:01:56 +00001216 zlog_warn("%s: socket %d message length %u is less than header size %d",
1217 __func__, sock, length, ZEBRA_HEADER_SIZE);
1218 zebra_client_close (client);
1219 return -1;
1220 }
1221 if (length > STREAM_SIZE(client->ibuf))
1222 {
1223 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1224 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001225 zebra_client_close (client);
1226 return -1;
1227 }
1228
paul718e3742002-12-13 20:15:29 +00001229 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001230 if (already < length)
paul718e3742002-12-13 20:15:29 +00001231 {
ajs57a14772005-04-10 15:01:56 +00001232 ssize_t nbyte;
1233 if (((nbyte = stream_read_try (client->ibuf, sock,
1234 length-already)) == 0) ||
1235 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001236 {
1237 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001238 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001239 zebra_client_close (client);
1240 return -1;
1241 }
ajs57a14772005-04-10 15:01:56 +00001242 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001243 {
1244 /* Try again later. */
1245 zebra_event (ZEBRA_READ, sock, client);
1246 return 0;
1247 }
paul718e3742002-12-13 20:15:29 +00001248 }
1249
ajs719e9742005-02-28 20:52:15 +00001250 length -= ZEBRA_HEADER_SIZE;
1251
paul718e3742002-12-13 20:15:29 +00001252 /* Debug packet information. */
1253 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001254 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001255
1256 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001257 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001258 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001259
1260 switch (command)
1261 {
hasso18a6dce2004-10-03 18:18:34 +00001262 case ZEBRA_ROUTER_ID_ADD:
1263 zread_router_id_add (client, length);
1264 break;
1265 case ZEBRA_ROUTER_ID_DELETE:
1266 zread_router_id_delete (client, length);
1267 break;
paul718e3742002-12-13 20:15:29 +00001268 case ZEBRA_INTERFACE_ADD:
1269 zread_interface_add (client, length);
1270 break;
1271 case ZEBRA_INTERFACE_DELETE:
1272 zread_interface_delete (client, length);
1273 break;
1274 case ZEBRA_IPV4_ROUTE_ADD:
1275 zread_ipv4_add (client, length);
1276 break;
1277 case ZEBRA_IPV4_ROUTE_DELETE:
1278 zread_ipv4_delete (client, length);
1279 break;
1280#ifdef HAVE_IPV6
1281 case ZEBRA_IPV6_ROUTE_ADD:
1282 zread_ipv6_add (client, length);
1283 break;
1284 case ZEBRA_IPV6_ROUTE_DELETE:
1285 zread_ipv6_delete (client, length);
1286 break;
1287#endif /* HAVE_IPV6 */
1288 case ZEBRA_REDISTRIBUTE_ADD:
1289 zebra_redistribute_add (command, client, length);
1290 break;
1291 case ZEBRA_REDISTRIBUTE_DELETE:
1292 zebra_redistribute_delete (command, client, length);
1293 break;
1294 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1295 zebra_redistribute_default_add (command, client, length);
1296 break;
1297 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1298 zebra_redistribute_default_delete (command, client, length);
1299 break;
1300 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1301 zread_ipv4_nexthop_lookup (client, length);
1302 break;
1303#ifdef HAVE_IPV6
1304 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1305 zread_ipv6_nexthop_lookup (client, length);
1306 break;
1307#endif /* HAVE_IPV6 */
1308 case ZEBRA_IPV4_IMPORT_LOOKUP:
1309 zread_ipv4_import_lookup (client, length);
1310 break;
1311 default:
1312 zlog_info ("Zebra received unknown command %d", command);
1313 break;
1314 }
1315
ajs719e9742005-02-28 20:52:15 +00001316 if (client->t_suicide)
1317 {
1318 /* No need to wait for thread callback, just kill immediately. */
1319 zebra_client_close(client);
1320 return -1;
1321 }
1322
paul718e3742002-12-13 20:15:29 +00001323 stream_reset (client->ibuf);
1324 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001325 return 0;
1326}
1327
paul718e3742002-12-13 20:15:29 +00001328
1329/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001330static int
paul718e3742002-12-13 20:15:29 +00001331zebra_accept (struct thread *thread)
1332{
1333 int accept_sock;
1334 int client_sock;
1335 struct sockaddr_in client;
1336 socklen_t len;
1337
1338 accept_sock = THREAD_FD (thread);
1339
ajs719e9742005-02-28 20:52:15 +00001340 /* Reregister myself. */
1341 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1342
paul718e3742002-12-13 20:15:29 +00001343 len = sizeof (struct sockaddr_in);
1344 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1345
1346 if (client_sock < 0)
1347 {
ajs6099b3b2004-11-20 02:06:59 +00001348 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001349 return -1;
1350 }
1351
paulccf35572003-03-01 11:42:20 +00001352 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001353 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001354
paul718e3742002-12-13 20:15:29 +00001355 /* Create new zebra client. */
1356 zebra_client_create (client_sock);
1357
paul718e3742002-12-13 20:15:29 +00001358 return 0;
1359}
1360
paulb9df2d22004-05-09 09:09:59 +00001361#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001362/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001363static void
paul718e3742002-12-13 20:15:29 +00001364zebra_serv ()
1365{
1366 int ret;
1367 int accept_sock;
1368 struct sockaddr_in addr;
1369
1370 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1371
1372 if (accept_sock < 0)
1373 {
paul3d1dc852005-04-05 00:45:23 +00001374 zlog_warn ("Can't create zserv stream socket: %s",
1375 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001376 zlog_warn ("zebra can't provice full functionality due to above error");
1377 return;
1378 }
1379
1380 memset (&addr, 0, sizeof (struct sockaddr_in));
1381 addr.sin_family = AF_INET;
1382 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001383#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001384 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001385#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001386 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1387
1388 sockopt_reuseaddr (accept_sock);
1389 sockopt_reuseport (accept_sock);
1390
pauledd7c242003-06-04 13:59:38 +00001391 if ( zserv_privs.change(ZPRIVS_RAISE) )
1392 zlog (NULL, LOG_ERR, "Can't raise privileges");
1393
paul718e3742002-12-13 20:15:29 +00001394 ret = bind (accept_sock, (struct sockaddr *)&addr,
1395 sizeof (struct sockaddr_in));
1396 if (ret < 0)
1397 {
paul3d1dc852005-04-05 00:45:23 +00001398 zlog_warn ("Can't bind to stream socket: %s",
1399 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001400 zlog_warn ("zebra can't provice full functionality due to above error");
1401 close (accept_sock); /* Avoid sd leak. */
1402 return;
1403 }
pauledd7c242003-06-04 13:59:38 +00001404
1405 if ( zserv_privs.change(ZPRIVS_LOWER) )
1406 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001407
1408 ret = listen (accept_sock, 1);
1409 if (ret < 0)
1410 {
paul3d1dc852005-04-05 00:45:23 +00001411 zlog_warn ("Can't listen to stream socket: %s",
1412 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001413 zlog_warn ("zebra can't provice full functionality due to above error");
1414 close (accept_sock); /* Avoid sd leak. */
1415 return;
1416 }
1417
1418 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1419}
paulb9df2d22004-05-09 09:09:59 +00001420#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001421
1422/* For sockaddr_un. */
1423#include <sys/un.h>
1424
1425/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001426static void
hassofce954f2004-10-07 20:29:24 +00001427zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001428{
1429 int ret;
1430 int sock, len;
1431 struct sockaddr_un serv;
1432 mode_t old_mask;
1433
1434 /* First of all, unlink existing socket */
1435 unlink (path);
1436
1437 /* Set umask */
1438 old_mask = umask (0077);
1439
1440 /* Make UNIX domain socket. */
1441 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1442 if (sock < 0)
1443 {
paul3d1dc852005-04-05 00:45:23 +00001444 zlog_warn ("Can't create zserv unix socket: %s",
1445 safe_strerror (errno));
1446 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001447 return;
1448 }
1449
1450 /* Make server socket. */
1451 memset (&serv, 0, sizeof (struct sockaddr_un));
1452 serv.sun_family = AF_UNIX;
1453 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001454#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001455 len = serv.sun_len = SUN_LEN(&serv);
1456#else
1457 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001458#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001459
1460 ret = bind (sock, (struct sockaddr *) &serv, len);
1461 if (ret < 0)
1462 {
paul3d1dc852005-04-05 00:45:23 +00001463 zlog_warn ("Can't bind to unix socket %s: %s",
1464 path, safe_strerror (errno));
1465 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001466 close (sock);
1467 return;
1468 }
1469
1470 ret = listen (sock, 5);
1471 if (ret < 0)
1472 {
paul3d1dc852005-04-05 00:45:23 +00001473 zlog_warn ("Can't listen to unix socket %s: %s",
1474 path, safe_strerror (errno));
1475 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001476 close (sock);
1477 return;
1478 }
1479
1480 umask (old_mask);
1481
1482 zebra_event (ZEBRA_SERV, sock, NULL);
1483}
1484
paul718e3742002-12-13 20:15:29 +00001485
paulb9df2d22004-05-09 09:09:59 +00001486static void
paul718e3742002-12-13 20:15:29 +00001487zebra_event (enum event event, int sock, struct zserv *client)
1488{
1489 switch (event)
1490 {
1491 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001492 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001493 break;
1494 case ZEBRA_READ:
1495 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001496 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001497 break;
1498 case ZEBRA_WRITE:
1499 /**/
1500 break;
1501 }
1502}
1503
1504/* Display default rtm_table for all clients. */
1505DEFUN (show_table,
1506 show_table_cmd,
1507 "show table",
1508 SHOW_STR
1509 "default routing table to use for all clients\n")
1510{
paulb21b19c2003-06-15 01:28:29 +00001511 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001512 VTY_NEWLINE);
1513 return CMD_SUCCESS;
1514}
1515
1516DEFUN (config_table,
1517 config_table_cmd,
1518 "table TABLENO",
1519 "Configure target kernel routing table\n"
1520 "TABLE integer\n")
1521{
paulb21b19c2003-06-15 01:28:29 +00001522 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001523 return CMD_SUCCESS;
1524}
1525
hasso647e4f12003-05-25 11:43:52 +00001526DEFUN (ip_forwarding,
1527 ip_forwarding_cmd,
1528 "ip forwarding",
1529 IP_STR
1530 "Turn on IP forwarding")
1531{
1532 int ret;
1533
1534 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001535 if (ret == 0)
1536 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001537
hasso647e4f12003-05-25 11:43:52 +00001538 if (ret == 0)
1539 {
1540 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1541 return CMD_WARNING;
1542 }
1543
1544 return CMD_SUCCESS;
1545}
1546
paul718e3742002-12-13 20:15:29 +00001547DEFUN (no_ip_forwarding,
1548 no_ip_forwarding_cmd,
1549 "no ip forwarding",
1550 NO_STR
1551 IP_STR
1552 "Turn off IP forwarding")
1553{
1554 int ret;
1555
1556 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001557 if (ret != 0)
1558 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001559
paul718e3742002-12-13 20:15:29 +00001560 if (ret != 0)
1561 {
1562 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1563 return CMD_WARNING;
1564 }
1565
1566 return CMD_SUCCESS;
1567}
1568
1569/* This command is for debugging purpose. */
1570DEFUN (show_zebra_client,
1571 show_zebra_client_cmd,
1572 "show zebra client",
1573 SHOW_STR
1574 "Zebra information"
1575 "Client information")
1576{
hasso52dc7ee2004-09-23 19:18:23 +00001577 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001578 struct zserv *client;
1579
paul1eb8ef22005-04-07 07:30:20 +00001580 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1581 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1582
paul718e3742002-12-13 20:15:29 +00001583 return CMD_SUCCESS;
1584}
1585
1586/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001587static int
paul718e3742002-12-13 20:15:29 +00001588config_write_table (struct vty *vty)
1589{
paulb21b19c2003-06-15 01:28:29 +00001590 if (zebrad.rtm_table_default)
1591 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001592 VTY_NEWLINE);
1593 return 0;
1594}
1595
1596/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001597static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001598{
1599 TABLE_NODE,
1600 "", /* This node has no interface. */
1601 1
1602};
1603
1604/* Only display ip forwarding is enabled or not. */
1605DEFUN (show_ip_forwarding,
1606 show_ip_forwarding_cmd,
1607 "show ip forwarding",
1608 SHOW_STR
1609 IP_STR
1610 "IP forwarding status\n")
1611{
1612 int ret;
1613
1614 ret = ipforward ();
1615
1616 if (ret == 0)
1617 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1618 else
1619 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1620 return CMD_SUCCESS;
1621}
1622
1623#ifdef HAVE_IPV6
1624/* Only display ipv6 forwarding is enabled or not. */
1625DEFUN (show_ipv6_forwarding,
1626 show_ipv6_forwarding_cmd,
1627 "show ipv6 forwarding",
1628 SHOW_STR
1629 "IPv6 information\n"
1630 "Forwarding status\n")
1631{
1632 int ret;
1633
1634 ret = ipforward_ipv6 ();
1635
1636 switch (ret)
1637 {
1638 case -1:
1639 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1640 break;
1641 case 0:
1642 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1643 break;
1644 case 1:
1645 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1646 break;
1647 default:
1648 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1649 break;
1650 }
1651 return CMD_SUCCESS;
1652}
1653
hasso55906722004-02-11 22:42:16 +00001654DEFUN (ipv6_forwarding,
1655 ipv6_forwarding_cmd,
1656 "ipv6 forwarding",
1657 IPV6_STR
1658 "Turn on IPv6 forwarding")
1659{
1660 int ret;
1661
hasso41d3fc92004-04-06 11:59:00 +00001662 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001663 if (ret == 0)
1664 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001665
hasso41d3fc92004-04-06 11:59:00 +00001666 if (ret == 0)
1667 {
hasso55906722004-02-11 22:42:16 +00001668 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1669 return CMD_WARNING;
1670 }
1671
1672 return CMD_SUCCESS;
1673}
1674
paul718e3742002-12-13 20:15:29 +00001675DEFUN (no_ipv6_forwarding,
1676 no_ipv6_forwarding_cmd,
1677 "no ipv6 forwarding",
1678 NO_STR
hasso55906722004-02-11 22:42:16 +00001679 IPV6_STR
1680 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001681{
1682 int ret;
1683
hasso41d3fc92004-04-06 11:59:00 +00001684 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001685 if (ret != 0)
1686 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001687
paul718e3742002-12-13 20:15:29 +00001688 if (ret != 0)
1689 {
1690 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1691 return CMD_WARNING;
1692 }
1693
1694 return CMD_SUCCESS;
1695}
1696
1697#endif /* HAVE_IPV6 */
1698
1699/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001700static int
paul718e3742002-12-13 20:15:29 +00001701config_write_forwarding (struct vty *vty)
1702{
hasso18a6dce2004-10-03 18:18:34 +00001703 /* FIXME: Find better place for that. */
1704 router_id_write (vty);
1705
paul3e0b3a52004-08-23 18:58:32 +00001706 if (ipforward ())
1707 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001708#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001709 if (ipforward_ipv6 ())
1710 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001711#endif /* HAVE_IPV6 */
1712 vty_out (vty, "!%s", VTY_NEWLINE);
1713 return 0;
1714}
1715
1716/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001717static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001718{
1719 FORWARDING_NODE,
1720 "", /* This node has no interface. */
1721 1
1722};
1723
1724
1725/* Initialisation of zebra and installation of commands. */
1726void
paula1ac18c2005-06-28 17:17:12 +00001727zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001728{
1729 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001730 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001731
paul718e3742002-12-13 20:15:29 +00001732 /* Install configuration write function. */
1733 install_node (&table_node, config_write_table);
1734 install_node (&forwarding_node, config_write_forwarding);
1735
1736 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1737 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001738 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001739 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1740 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1741
1742#ifdef HAVE_NETLINK
1743 install_element (VIEW_NODE, &show_table_cmd);
1744 install_element (ENABLE_NODE, &show_table_cmd);
1745 install_element (CONFIG_NODE, &config_table_cmd);
1746#endif /* HAVE_NETLINK */
1747
1748#ifdef HAVE_IPV6
1749 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1750 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001751 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001752 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1753#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001754
1755 /* Route-map */
1756 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001757}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001758
1759/* Make zebra server socket, wiping any existing one (see bug #403). */
1760void
1761zebra_zserv_socket_init (void)
1762{
1763#ifdef HAVE_TCP_ZEBRA
1764 zebra_serv ();
1765#else
1766 zebra_serv_un (ZEBRA_SERV_PATH);
1767#endif /* HAVE_TCP_ZEBRA */
1768}