blob: 1182937800c1081520c96e9d687c59b85542bde4 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
30#include "table.h"
31#include "rib.h"
32#include "network.h"
33#include "sockunion.h"
34#include "log.h"
35#include "zclient.h"
pauledd7c242003-06-04 13:59:38 +000036#include "privs.h"
ajs719e9742005-02-28 20:52:15 +000037#include "network.h"
38#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000039
40#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000041#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000042#include "zebra/redistribute.h"
43#include "zebra/debug.h"
44#include "zebra/ipforward.h"
45
46/* Event list of zebra. */
47enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
48
paulb21b19c2003-06-15 01:28:29 +000049extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000050
paulb9df2d22004-05-09 09:09:59 +000051static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000052
pauledd7c242003-06-04 13:59:38 +000053extern struct zebra_privs_t zserv_privs;
paul718e3742002-12-13 20:15:29 +000054
ajs719e9742005-02-28 20:52:15 +000055static void zebra_client_close (struct zserv *client);
56
57static int
58zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000059{
ajs719e9742005-02-28 20:52:15 +000060 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000061
ajs719e9742005-02-28 20:52:15 +000062 client->t_suicide = NULL;
63 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000064 return 0;
65}
66
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +040067/* When client connects, it sends hello message
68 * with promise to send zebra routes of specific type.
69 * Zebra stores a socket fd of the client into
70 * this array. And use it to clean up routes that
71 * client didn't remove for some reasons after closing
72 * connection.
73 */
74static int route_type_oaths[ZEBRA_ROUTE_MAX];
75
ajs719e9742005-02-28 20:52:15 +000076static int
77zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000078{
ajs719e9742005-02-28 20:52:15 +000079 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000080
ajs719e9742005-02-28 20:52:15 +000081 client->t_write = NULL;
82 if (client->t_suicide)
83 {
84 zebra_client_close(client);
85 return -1;
86 }
87 switch (buffer_flush_available(client->wb, client->sock))
88 {
89 case BUFFER_ERROR:
90 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
91 "closing", __func__, client->sock);
92 zebra_client_close(client);
93 break;
94 case BUFFER_PENDING:
95 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
96 client, client->sock);
97 break;
98 case BUFFER_EMPTY:
99 break;
100 }
101 return 0;
paulccf35572003-03-01 11:42:20 +0000102}
103
ajs719e9742005-02-28 20:52:15 +0000104static int
105zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000106{
ajs719e9742005-02-28 20:52:15 +0000107 if (client->t_suicide)
108 return -1;
109 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
110 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000111 {
ajs719e9742005-02-28 20:52:15 +0000112 case BUFFER_ERROR:
113 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
114 __func__, client->sock);
115 /* Schedule a delayed close since many of the functions that call this
116 one do not check the return code. They do not allow for the
117 possibility that an I/O error may have caused the client to be
118 deleted. */
119 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
120 client, 0);
121 return -1;
ajs719e9742005-02-28 20:52:15 +0000122 case BUFFER_EMPTY:
123 THREAD_OFF(client->t_write);
124 break;
125 case BUFFER_PENDING:
126 THREAD_WRITE_ON(zebrad.master, client->t_write,
127 zserv_flush_data, client, client->sock);
128 break;
paulccf35572003-03-01 11:42:20 +0000129 }
paulccf35572003-03-01 11:42:20 +0000130 return 0;
131}
132
paulc1b98002006-01-16 01:54:02 +0000133static void
134zserv_create_header (struct stream *s, uint16_t cmd)
135{
136 /* length placeholder, caller can update */
137 stream_putw (s, ZEBRA_HEADER_SIZE);
138 stream_putc (s, ZEBRA_HEADER_MARKER);
139 stream_putc (s, ZSERV_VERSION);
140 stream_putw (s, cmd);
141}
142
Josh Bailey51d4ef82012-03-21 17:13:39 -0700143static void
144zserv_encode_interface (struct stream *s, struct interface *ifp)
145{
146 /* Interface information. */
147 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
148 stream_putl (s, ifp->ifindex);
149 stream_putc (s, ifp->status);
150 stream_putq (s, ifp->flags);
151 stream_putl (s, ifp->metric);
152 stream_putl (s, ifp->mtu);
153 stream_putl (s, ifp->mtu6);
154 stream_putl (s, ifp->bandwidth);
155#ifdef HAVE_STRUCT_SOCKADDR_DL
David Lamparterca3ccd82012-09-26 14:52:39 +0200156 stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage));
Josh Bailey51d4ef82012-03-21 17:13:39 -0700157#else
158 stream_putl (s, ifp->hw_addr_len);
159 if (ifp->hw_addr_len)
160 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
161#endif /* HAVE_STRUCT_SOCKADDR_DL */
162
163 /* Write packet size. */
164 stream_putw_at (s, 0, stream_get_endp (s));
165}
166
paul718e3742002-12-13 20:15:29 +0000167/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000168/*
169 * This function is called in the following situations:
170 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
171 * from the client.
172 * - at startup, when zebra figures out the available interfaces
173 * - when an interface is added (where support for
174 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
175 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
176 * received)
177 */
paul718e3742002-12-13 20:15:29 +0000178int
179zsend_interface_add (struct zserv *client, struct interface *ifp)
180{
181 struct stream *s;
182
183 /* Check this client need interface information. */
184 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000185 return 0;
paul718e3742002-12-13 20:15:29 +0000186
187 s = client->obuf;
188 stream_reset (s);
189
paulc1b98002006-01-16 01:54:02 +0000190 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700191 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000192
ajs719e9742005-02-28 20:52:15 +0000193 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000194}
195
196/* Interface deletion from zebra daemon. */
197int
198zsend_interface_delete (struct zserv *client, struct interface *ifp)
199{
200 struct stream *s;
201
202 /* Check this client need interface information. */
203 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000204 return 0;
paul718e3742002-12-13 20:15:29 +0000205
206 s = client->obuf;
207 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000208
Josh Bailey51d4ef82012-03-21 17:13:39 -0700209 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
210 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000211
ajs719e9742005-02-28 20:52:15 +0000212 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000213}
214
paulb9df2d22004-05-09 09:09:59 +0000215/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
216 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
217 *
218 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
219 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
220 * from the client, after the ZEBRA_INTERFACE_ADD has been
221 * sent from zebra to the client
222 * - redistribute new address info to all clients in the following situations
223 * - at startup, when zebra figures out the available interfaces
224 * - when an interface is added (where support for
225 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
226 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
227 * received)
228 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
229 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
230 * - when an RTM_NEWADDR message is received from the kernel,
231 *
232 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
233 *
234 * zsend_interface_address(DELETE)
235 * ^
236 * |
237 * zebra_interface_address_delete_update
238 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000239 * | | if_delete_update
240 * | |
paulb9df2d22004-05-09 09:09:59 +0000241 * ip_address_uninstall connected_delete_ipv4
242 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
243 * ^ ^
244 * | |
245 * | RTM_NEWADDR on routing/netlink socket
246 * |
247 * vty commands:
248 * "no ip address A.B.C.D/M [label LINE]"
249 * "no ip address A.B.C.D/M secondary"
250 * ["no ipv6 address X:X::X:X/M"]
251 *
252 */
paul718e3742002-12-13 20:15:29 +0000253int
paulb9df2d22004-05-09 09:09:59 +0000254zsend_interface_address (int cmd, struct zserv *client,
255 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000256{
257 int blen;
258 struct stream *s;
259 struct prefix *p;
260
261 /* Check this client need interface information. */
262 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000263 return 0;
paul718e3742002-12-13 20:15:29 +0000264
265 s = client->obuf;
266 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000267
268 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000269 stream_putl (s, ifp->ifindex);
270
271 /* Interface address flag. */
272 stream_putc (s, ifc->flags);
273
274 /* Prefix information. */
275 p = ifc->address;
276 stream_putc (s, p->family);
277 blen = prefix_blen (p);
278 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000279
280 /*
281 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
282 * but zebra_interface_address_delete_read() in the gnu version
283 * expects to find it
284 */
paul718e3742002-12-13 20:15:29 +0000285 stream_putc (s, p->prefixlen);
286
287 /* Destination. */
288 p = ifc->destination;
289 if (p)
290 stream_put (s, &p->u.prefix, blen);
291 else
292 stream_put (s, NULL, blen);
293
294 /* Write packet size. */
295 stream_putw_at (s, 0, stream_get_endp (s));
296
ajs719e9742005-02-28 20:52:15 +0000297 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000298}
299
paulb9df2d22004-05-09 09:09:59 +0000300/*
301 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
302 * ZEBRA_INTERFACE_DOWN.
303 *
304 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
305 * the clients in one of 2 situations:
306 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
307 * - a vty command modifying the bandwidth of an interface is received.
308 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
309 */
paul718e3742002-12-13 20:15:29 +0000310int
paulb9df2d22004-05-09 09:09:59 +0000311zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000312{
313 struct stream *s;
314
315 /* Check this client need interface information. */
316 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000317 return 0;
paul718e3742002-12-13 20:15:29 +0000318
319 s = client->obuf;
320 stream_reset (s);
321
paulc1b98002006-01-16 01:54:02 +0000322 zserv_create_header (s, cmd);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700323 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000324
ajs719e9742005-02-28 20:52:15 +0000325 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000326}
327
paulb9df2d22004-05-09 09:09:59 +0000328/*
329 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
330 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
331 * situations:
332 * - when the client starts up, and requests default information
333 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
334 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
335 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
336 * - when the zebra server redistributes routes after it updates its rib
337 *
338 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
339 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
340 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
341 * - deleted from zebra's rib, and this info
342 * has to be redistributed to the clients
343 *
344 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
345 * zebra server when the client wants to tell the zebra server to add a
346 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
347 * same message being sent back and forth, this function and
348 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
349 * duplication.
350 */
paul718e3742002-12-13 20:15:29 +0000351int
paulb9df2d22004-05-09 09:09:59 +0000352zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
353 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000354{
355 int psize;
356 struct stream *s;
357 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000358 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000359 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000360 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000361
paul718e3742002-12-13 20:15:29 +0000362 s = client->obuf;
363 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000364
365 zserv_create_header (s, cmd);
366
367 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000368 stream_putc (s, rib->type);
369 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000370
371 /* marker for message flags field */
372 messmark = stream_get_endp (s);
373 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000374
375 /* Prefix. */
376 psize = PSIZE (p->prefixlen);
377 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000378 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000379
paulb9df2d22004-05-09 09:09:59 +0000380 /*
381 * XXX The message format sent by zebra below does not match the format
382 * of the corresponding message expected by the zebra server
383 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
384 * (is there a bug on the client side if more than one segment is sent?)
385 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
386 * is hard-coded.
387 */
paul718e3742002-12-13 20:15:29 +0000388 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000389
paul718e3742002-12-13 20:15:29 +0000390 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
391 {
392 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paulb9df2d22004-05-09 09:09:59 +0000393 {
paul1dcb5172005-05-31 08:38:50 +0000394 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
395 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
396
397 if (nhnummark == 0)
398 {
399 nhnummark = stream_get_endp (s);
400 stream_putc (s, 1); /* placeholder */
401 }
402
paulb9df2d22004-05-09 09:09:59 +0000403 nhnum++;
paul718e3742002-12-13 20:15:29 +0000404
paulb9df2d22004-05-09 09:09:59 +0000405 switch(nexthop->type)
406 {
407 case NEXTHOP_TYPE_IPV4:
408 case NEXTHOP_TYPE_IPV4_IFINDEX:
409 stream_put_in_addr (s, &nexthop->gate.ipv4);
410 break;
411#ifdef HAVE_IPV6
412 case NEXTHOP_TYPE_IPV6:
413 case NEXTHOP_TYPE_IPV6_IFINDEX:
414 case NEXTHOP_TYPE_IPV6_IFNAME:
415 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
416 break;
417#endif
418 default:
419 if (cmd == ZEBRA_IPV4_ROUTE_ADD
420 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
421 {
422 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000423 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000424 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
425 }
426 else
427 {
428 struct in6_addr empty;
429 memset (&empty, 0, sizeof (struct in6_addr));
430 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
431 }
432 }
paul718e3742002-12-13 20:15:29 +0000433
paulb9df2d22004-05-09 09:09:59 +0000434 /* Interface index. */
435 stream_putc (s, 1);
436 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000437
paulb9df2d22004-05-09 09:09:59 +0000438 break;
439 }
paul718e3742002-12-13 20:15:29 +0000440 }
441
442 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700443 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000444 {
vincentfbf5d032005-09-29 11:25:50 +0000445 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
446 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000447 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
448 stream_putl (s, rib->metric);
449 }
450
451 /* write real message flags value */
452 stream_putc_at (s, messmark, zapi_flags);
453
paulb9df2d22004-05-09 09:09:59 +0000454 /* Write next-hop number */
455 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000456 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000457
paul718e3742002-12-13 20:15:29 +0000458 /* Write packet size. */
459 stream_putw_at (s, 0, stream_get_endp (s));
460
ajs719e9742005-02-28 20:52:15 +0000461 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000462}
463
paul718e3742002-12-13 20:15:29 +0000464#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000465static int
paul718e3742002-12-13 20:15:29 +0000466zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
467{
468 struct stream *s;
469 struct rib *rib;
470 unsigned long nump;
471 u_char num;
472 struct nexthop *nexthop;
473
474 /* Lookup nexthop. */
475 rib = rib_match_ipv6 (addr);
476
477 /* Get output stream. */
478 s = client->obuf;
479 stream_reset (s);
480
481 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000482 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000483 stream_put (s, &addr, 16);
484
485 if (rib)
486 {
487 stream_putl (s, rib->metric);
488 num = 0;
paul9985f832005-02-09 15:51:56 +0000489 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000490 stream_putc (s, 0);
491 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
492 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
493 {
494 stream_putc (s, nexthop->type);
495 switch (nexthop->type)
496 {
497 case ZEBRA_NEXTHOP_IPV6:
498 stream_put (s, &nexthop->gate.ipv6, 16);
499 break;
500 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
501 case ZEBRA_NEXTHOP_IPV6_IFNAME:
502 stream_put (s, &nexthop->gate.ipv6, 16);
503 stream_putl (s, nexthop->ifindex);
504 break;
505 case ZEBRA_NEXTHOP_IFINDEX:
506 case ZEBRA_NEXTHOP_IFNAME:
507 stream_putl (s, nexthop->ifindex);
508 break;
hassofa2b17e2004-03-04 17:45:00 +0000509 default:
510 /* do nothing */
511 break;
paul718e3742002-12-13 20:15:29 +0000512 }
513 num++;
514 }
515 stream_putc_at (s, nump, num);
516 }
517 else
518 {
519 stream_putl (s, 0);
520 stream_putc (s, 0);
521 }
522
523 stream_putw_at (s, 0, stream_get_endp (s));
524
ajs719e9742005-02-28 20:52:15 +0000525 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000526}
527#endif /* HAVE_IPV6 */
528
paulb9df2d22004-05-09 09:09:59 +0000529static int
paul718e3742002-12-13 20:15:29 +0000530zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
531{
532 struct stream *s;
533 struct rib *rib;
534 unsigned long nump;
535 u_char num;
536 struct nexthop *nexthop;
537
538 /* Lookup nexthop. */
539 rib = rib_match_ipv4 (addr);
540
541 /* Get output stream. */
542 s = client->obuf;
543 stream_reset (s);
544
545 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000546 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000547 stream_put_in_addr (s, &addr);
548
549 if (rib)
550 {
Christian Frankebb97e462013-05-25 14:01:35 +0000551 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
552 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000553 stream_putl (s, rib->metric);
554 num = 0;
paul9985f832005-02-09 15:51:56 +0000555 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000556 stream_putc (s, 0);
557 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
558 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
559 {
560 stream_putc (s, nexthop->type);
561 switch (nexthop->type)
562 {
563 case ZEBRA_NEXTHOP_IPV4:
564 stream_put_in_addr (s, &nexthop->gate.ipv4);
565 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000566 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
567 stream_put_in_addr (s, &nexthop->gate.ipv4);
568 stream_putl (s, nexthop->ifindex);
569 break;
paul718e3742002-12-13 20:15:29 +0000570 case ZEBRA_NEXTHOP_IFINDEX:
571 case ZEBRA_NEXTHOP_IFNAME:
572 stream_putl (s, nexthop->ifindex);
573 break;
hassofa2b17e2004-03-04 17:45:00 +0000574 default:
575 /* do nothing */
576 break;
paul718e3742002-12-13 20:15:29 +0000577 }
578 num++;
579 }
580 stream_putc_at (s, nump, num);
581 }
582 else
583 {
Christian Frankebb97e462013-05-25 14:01:35 +0000584 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
585 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000586 stream_putl (s, 0);
587 stream_putc (s, 0);
588 }
589
590 stream_putw_at (s, 0, stream_get_endp (s));
591
ajs719e9742005-02-28 20:52:15 +0000592 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000593}
594
paulb9df2d22004-05-09 09:09:59 +0000595static int
paul718e3742002-12-13 20:15:29 +0000596zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
597{
598 struct stream *s;
599 struct rib *rib;
600 unsigned long nump;
601 u_char num;
602 struct nexthop *nexthop;
603
604 /* Lookup nexthop. */
605 rib = rib_lookup_ipv4 (p);
606
607 /* Get output stream. */
608 s = client->obuf;
609 stream_reset (s);
610
611 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000612 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000613 stream_put_in_addr (s, &p->prefix);
614
615 if (rib)
616 {
617 stream_putl (s, rib->metric);
618 num = 0;
paul9985f832005-02-09 15:51:56 +0000619 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000620 stream_putc (s, 0);
621 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
622 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
623 {
624 stream_putc (s, nexthop->type);
625 switch (nexthop->type)
626 {
627 case ZEBRA_NEXTHOP_IPV4:
628 stream_put_in_addr (s, &nexthop->gate.ipv4);
629 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000630 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
631 stream_put_in_addr (s, &nexthop->gate.ipv4);
632 stream_putl (s, nexthop->ifindex);
633 break;
paul718e3742002-12-13 20:15:29 +0000634 case ZEBRA_NEXTHOP_IFINDEX:
635 case ZEBRA_NEXTHOP_IFNAME:
636 stream_putl (s, nexthop->ifindex);
637 break;
hassofa2b17e2004-03-04 17:45:00 +0000638 default:
639 /* do nothing */
640 break;
paul718e3742002-12-13 20:15:29 +0000641 }
642 num++;
643 }
644 stream_putc_at (s, nump, num);
645 }
646 else
647 {
648 stream_putl (s, 0);
649 stream_putc (s, 0);
650 }
651
652 stream_putw_at (s, 0, stream_get_endp (s));
653
ajs719e9742005-02-28 20:52:15 +0000654 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000655}
656
hasso18a6dce2004-10-03 18:18:34 +0000657/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
658int
659zsend_router_id_update (struct zserv *client, struct prefix *p)
660{
661 struct stream *s;
662 int blen;
663
664 /* Check this client need interface information. */
665 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000666 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000667
668 s = client->obuf;
669 stream_reset (s);
670
hasso18a6dce2004-10-03 18:18:34 +0000671 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000672 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000673
674 /* Prefix information. */
675 stream_putc (s, p->family);
676 blen = prefix_blen (p);
677 stream_put (s, &p->u.prefix, blen);
678 stream_putc (s, p->prefixlen);
679
680 /* Write packet size. */
681 stream_putw_at (s, 0, stream_get_endp (s));
682
ajs719e9742005-02-28 20:52:15 +0000683 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000684}
685
paul718e3742002-12-13 20:15:29 +0000686/* Register zebra server interface information. Send current all
687 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000688static int
paul718e3742002-12-13 20:15:29 +0000689zread_interface_add (struct zserv *client, u_short length)
690{
paul1eb8ef22005-04-07 07:30:20 +0000691 struct listnode *ifnode, *ifnnode;
692 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000693 struct interface *ifp;
694 struct connected *c;
695
696 /* Interface information is needed. */
697 client->ifinfo = 1;
698
paul1eb8ef22005-04-07 07:30:20 +0000699 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000700 {
paul718e3742002-12-13 20:15:29 +0000701 /* Skip pseudo interface. */
702 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
703 continue;
704
ajs719e9742005-02-28 20:52:15 +0000705 if (zsend_interface_add (client, ifp) < 0)
706 return -1;
paul718e3742002-12-13 20:15:29 +0000707
paul1eb8ef22005-04-07 07:30:20 +0000708 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000709 {
ajs719e9742005-02-28 20:52:15 +0000710 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
711 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
712 ifp, c) < 0))
713 return -1;
paul718e3742002-12-13 20:15:29 +0000714 }
715 }
ajs719e9742005-02-28 20:52:15 +0000716 return 0;
paul718e3742002-12-13 20:15:29 +0000717}
718
719/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000720static int
paul718e3742002-12-13 20:15:29 +0000721zread_interface_delete (struct zserv *client, u_short length)
722{
723 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000724 return 0;
paul718e3742002-12-13 20:15:29 +0000725}
726
727/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000728/*
729 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
730 * add kernel route.
731 */
ajs719e9742005-02-28 20:52:15 +0000732static int
paul718e3742002-12-13 20:15:29 +0000733zread_ipv4_add (struct zserv *client, u_short length)
734{
735 int i;
736 struct rib *rib;
737 struct prefix_ipv4 p;
738 u_char message;
739 struct in_addr nexthop;
740 u_char nexthop_num;
741 u_char nexthop_type;
742 struct stream *s;
743 unsigned int ifindex;
744 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400745 safi_t safi;
746
paul718e3742002-12-13 20:15:29 +0000747
748 /* Get input stream. */
749 s = client->ibuf;
750
751 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000752 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
753
paul718e3742002-12-13 20:15:29 +0000754 /* Type, flags, message. */
755 rib->type = stream_getc (s);
756 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000757 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400758 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000759 rib->uptime = time (NULL);
760
761 /* IPv4 prefix. */
762 memset (&p, 0, sizeof (struct prefix_ipv4));
763 p.family = AF_INET;
764 p.prefixlen = stream_getc (s);
765 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
766
767 /* Nexthop parse. */
768 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
769 {
770 nexthop_num = stream_getc (s);
771
772 for (i = 0; i < nexthop_num; i++)
773 {
774 nexthop_type = stream_getc (s);
775
776 switch (nexthop_type)
777 {
778 case ZEBRA_NEXTHOP_IFINDEX:
779 ifindex = stream_getl (s);
780 nexthop_ifindex_add (rib, ifindex);
781 break;
782 case ZEBRA_NEXTHOP_IFNAME:
783 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000784 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000785 break;
786 case ZEBRA_NEXTHOP_IPV4:
787 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000788 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000789 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200790 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
791 nexthop.s_addr = stream_get_ipv4 (s);
792 ifindex = stream_getl (s);
793 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
794 break;
paul718e3742002-12-13 20:15:29 +0000795 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000796 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000797 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700798 case ZEBRA_NEXTHOP_BLACKHOLE:
799 nexthop_blackhole_add (rib);
800 break;
801 }
paul718e3742002-12-13 20:15:29 +0000802 }
803 }
804
805 /* Distance. */
806 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
807 rib->distance = stream_getc (s);
808
809 /* Metric. */
810 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
811 rib->metric = stream_getl (s);
812
Paul Jakma171eee32006-07-27 16:11:02 +0000813 /* Table */
814 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400815 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000816 return 0;
paul718e3742002-12-13 20:15:29 +0000817}
818
819/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000820static int
paul718e3742002-12-13 20:15:29 +0000821zread_ipv4_delete (struct zserv *client, u_short length)
822{
823 int i;
824 struct stream *s;
825 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700826 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000827 unsigned long ifindex;
828 struct prefix_ipv4 p;
829 u_char nexthop_num;
830 u_char nexthop_type;
831 u_char ifname_len;
832
833 s = client->ibuf;
834 ifindex = 0;
835 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700836 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000837
838 /* Type, flags, message. */
839 api.type = stream_getc (s);
840 api.flags = stream_getc (s);
841 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400842 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000843
844 /* IPv4 prefix. */
845 memset (&p, 0, sizeof (struct prefix_ipv4));
846 p.family = AF_INET;
847 p.prefixlen = stream_getc (s);
848 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
849
850 /* Nexthop, ifindex, distance, metric. */
851 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
852 {
853 nexthop_num = stream_getc (s);
854
855 for (i = 0; i < nexthop_num; i++)
856 {
857 nexthop_type = stream_getc (s);
858
859 switch (nexthop_type)
860 {
861 case ZEBRA_NEXTHOP_IFINDEX:
862 ifindex = stream_getl (s);
863 break;
864 case ZEBRA_NEXTHOP_IFNAME:
865 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000866 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000867 break;
868 case ZEBRA_NEXTHOP_IPV4:
869 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700870 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000871 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200872 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
873 nexthop.s_addr = stream_get_ipv4 (s);
874 ifindex = stream_getl (s);
875 break;
paul718e3742002-12-13 20:15:29 +0000876 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000877 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000878 break;
879 }
880 }
881 }
882
883 /* Distance. */
884 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
885 api.distance = stream_getc (s);
886 else
887 api.distance = 0;
888
889 /* Metric. */
890 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
891 api.metric = stream_getl (s);
892 else
893 api.metric = 0;
894
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700895 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400896 client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +0000897 return 0;
paul718e3742002-12-13 20:15:29 +0000898}
899
900/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000901static int
paul718e3742002-12-13 20:15:29 +0000902zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
903{
904 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +0000905 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +0000906
907 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +0000908 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
909 zlog_debug("%s: looking up %s", __func__,
910 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
ajs719e9742005-02-28 20:52:15 +0000911 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000912}
913
914/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000915static int
paul718e3742002-12-13 20:15:29 +0000916zread_ipv4_import_lookup (struct zserv *client, u_short length)
917{
918 struct prefix_ipv4 p;
919
920 p.family = AF_INET;
921 p.prefixlen = stream_getc (client->ibuf);
922 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
923
ajs719e9742005-02-28 20:52:15 +0000924 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +0000925}
926
927#ifdef HAVE_IPV6
928/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +0000929static int
paul718e3742002-12-13 20:15:29 +0000930zread_ipv6_add (struct zserv *client, u_short length)
931{
932 int i;
933 struct stream *s;
934 struct zapi_ipv6 api;
935 struct in6_addr nexthop;
936 unsigned long ifindex;
937 struct prefix_ipv6 p;
938
939 s = client->ibuf;
940 ifindex = 0;
941 memset (&nexthop, 0, sizeof (struct in6_addr));
942
943 /* Type, flags, message. */
944 api.type = stream_getc (s);
945 api.flags = stream_getc (s);
946 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +0400947 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000948
949 /* IPv4 prefix. */
950 memset (&p, 0, sizeof (struct prefix_ipv6));
951 p.family = AF_INET6;
952 p.prefixlen = stream_getc (s);
953 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
954
955 /* Nexthop, ifindex, distance, metric. */
956 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
957 {
958 u_char nexthop_type;
959
960 api.nexthop_num = stream_getc (s);
961 for (i = 0; i < api.nexthop_num; i++)
962 {
963 nexthop_type = stream_getc (s);
964
965 switch (nexthop_type)
966 {
967 case ZEBRA_NEXTHOP_IPV6:
968 stream_get (&nexthop, s, 16);
969 break;
970 case ZEBRA_NEXTHOP_IFINDEX:
971 ifindex = stream_getl (s);
972 break;
973 }
974 }
975 }
976
977 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
978 api.distance = stream_getc (s);
979 else
980 api.distance = 0;
981
982 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
983 api.metric = stream_getl (s);
984 else
985 api.metric = 0;
986
987 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100988 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400989 api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +0000990 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100991 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +0400992 api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +0000993 return 0;
paul718e3742002-12-13 20:15:29 +0000994}
995
996/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000997static int
paul718e3742002-12-13 20:15:29 +0000998zread_ipv6_delete (struct zserv *client, u_short length)
999{
1000 int i;
1001 struct stream *s;
1002 struct zapi_ipv6 api;
1003 struct in6_addr nexthop;
1004 unsigned long ifindex;
1005 struct prefix_ipv6 p;
1006
1007 s = client->ibuf;
1008 ifindex = 0;
1009 memset (&nexthop, 0, sizeof (struct in6_addr));
1010
1011 /* Type, flags, message. */
1012 api.type = stream_getc (s);
1013 api.flags = stream_getc (s);
1014 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001015 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001016
1017 /* IPv4 prefix. */
1018 memset (&p, 0, sizeof (struct prefix_ipv6));
1019 p.family = AF_INET6;
1020 p.prefixlen = stream_getc (s);
1021 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1022
1023 /* Nexthop, ifindex, distance, metric. */
1024 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1025 {
1026 u_char nexthop_type;
1027
1028 api.nexthop_num = stream_getc (s);
1029 for (i = 0; i < api.nexthop_num; i++)
1030 {
1031 nexthop_type = stream_getc (s);
1032
1033 switch (nexthop_type)
1034 {
1035 case ZEBRA_NEXTHOP_IPV6:
1036 stream_get (&nexthop, s, 16);
1037 break;
1038 case ZEBRA_NEXTHOP_IFINDEX:
1039 ifindex = stream_getl (s);
1040 break;
1041 }
1042 }
1043 }
1044
1045 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1046 api.distance = stream_getc (s);
1047 else
1048 api.distance = 0;
1049 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1050 api.metric = stream_getl (s);
1051 else
1052 api.metric = 0;
1053
1054 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
G.Balajif768f362011-11-26 22:10:39 +04001055 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
paul718e3742002-12-13 20:15:29 +00001056 else
G.Balajif768f362011-11-26 22:10:39 +04001057 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +00001058 return 0;
paul718e3742002-12-13 20:15:29 +00001059}
1060
ajs719e9742005-02-28 20:52:15 +00001061static int
paul718e3742002-12-13 20:15:29 +00001062zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1063{
1064 struct in6_addr addr;
1065 char buf[BUFSIZ];
1066
1067 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001068 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1069 zlog_debug("%s: looking up %s", __func__,
1070 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001071
ajs719e9742005-02-28 20:52:15 +00001072 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001073}
1074#endif /* HAVE_IPV6 */
1075
hasso18a6dce2004-10-03 18:18:34 +00001076/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001077static int
hasso18a6dce2004-10-03 18:18:34 +00001078zread_router_id_add (struct zserv *client, u_short length)
1079{
1080 struct prefix p;
1081
1082 /* Router-id information is needed. */
1083 client->ridinfo = 1;
1084
1085 router_id_get (&p);
1086
ajs719e9742005-02-28 20:52:15 +00001087 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001088}
1089
1090/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001091static int
hasso18a6dce2004-10-03 18:18:34 +00001092zread_router_id_delete (struct zserv *client, u_short length)
1093{
1094 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001095 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001096}
1097
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001098/* Tie up route-type and client->sock */
1099static void
1100zread_hello (struct zserv *client)
1101{
1102 /* type of protocol (lib/zebra.h) */
1103 u_char proto;
1104 proto = stream_getc (client->ibuf);
1105
1106 /* accept only dynamic routing protocols */
1107 if ((proto < ZEBRA_ROUTE_MAX)
1108 && (proto > ZEBRA_ROUTE_STATIC))
1109 {
1110 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1111 client->sock, zebra_route_string(proto));
1112
1113 /* if route-type was binded by other client */
1114 if (route_type_oaths[proto])
1115 zlog_warn ("sender of %s routes changed %c->%c",
1116 zebra_route_string(proto), route_type_oaths[proto],
1117 client->sock);
1118
1119 route_type_oaths[proto] = client->sock;
1120 }
1121}
1122
1123/* If client sent routes of specific type, zebra removes it
1124 * and returns number of deleted routes.
1125 */
1126static void
1127zebra_score_rib (int client_sock)
1128{
1129 int i;
1130
1131 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1132 if (client_sock == route_type_oaths[i])
1133 {
1134 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1135 client_sock, rib_score_proto (i), zebra_route_string (i));
1136 route_type_oaths[i] = 0;
1137 break;
1138 }
1139}
1140
paul718e3742002-12-13 20:15:29 +00001141/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001142static void
paul718e3742002-12-13 20:15:29 +00001143zebra_client_close (struct zserv *client)
1144{
1145 /* Close file descriptor. */
1146 if (client->sock)
1147 {
1148 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001149 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001150 client->sock = -1;
1151 }
1152
1153 /* Free stream buffers. */
1154 if (client->ibuf)
1155 stream_free (client->ibuf);
1156 if (client->obuf)
1157 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001158 if (client->wb)
1159 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001160
1161 /* Release threads. */
1162 if (client->t_read)
1163 thread_cancel (client->t_read);
1164 if (client->t_write)
1165 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001166 if (client->t_suicide)
1167 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001168
1169 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001170 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001171 XFREE (0, client);
1172}
1173
1174/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001175static void
paul718e3742002-12-13 20:15:29 +00001176zebra_client_create (int sock)
1177{
1178 struct zserv *client;
1179
1180 client = XCALLOC (0, sizeof (struct zserv));
1181
1182 /* Make client input/output buffer. */
1183 client->sock = sock;
1184 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1185 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001186 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001187
1188 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001189 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001190
1191 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001192 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001193
1194 /* Make new read thread. */
1195 zebra_event (ZEBRA_READ, sock, client);
1196}
1197
1198/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001199static int
paul718e3742002-12-13 20:15:29 +00001200zebra_client_read (struct thread *thread)
1201{
1202 int sock;
1203 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001204 size_t already;
paulc1b98002006-01-16 01:54:02 +00001205 uint16_t length, command;
1206 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001207
1208 /* Get thread data. Reset reading thread because I'm running. */
1209 sock = THREAD_FD (thread);
1210 client = THREAD_ARG (thread);
1211 client->t_read = NULL;
1212
ajs719e9742005-02-28 20:52:15 +00001213 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001214 {
ajs719e9742005-02-28 20:52:15 +00001215 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001216 return -1;
1217 }
ajs719e9742005-02-28 20:52:15 +00001218
1219 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001220 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001221 {
ajs57a14772005-04-10 15:01:56 +00001222 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001223 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001224 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001225 (nbyte == -1))
1226 {
1227 if (IS_ZEBRA_DEBUG_EVENT)
1228 zlog_debug ("connection closed socket [%d]", sock);
1229 zebra_client_close (client);
1230 return -1;
1231 }
ajs57a14772005-04-10 15:01:56 +00001232 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001233 {
1234 /* Try again later. */
1235 zebra_event (ZEBRA_READ, sock, client);
1236 return 0;
1237 }
ajs57a14772005-04-10 15:01:56 +00001238 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001239 }
1240
1241 /* Reset to read from the beginning of the incoming packet. */
1242 stream_set_getp(client->ibuf, 0);
1243
paulc1b98002006-01-16 01:54:02 +00001244 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001245 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001246 marker = stream_getc (client->ibuf);
1247 version = stream_getc (client->ibuf);
1248 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001249
paulc1b98002006-01-16 01:54:02 +00001250 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1251 {
1252 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1253 __func__, sock, marker, version);
1254 zebra_client_close (client);
1255 return -1;
1256 }
ajs719e9742005-02-28 20:52:15 +00001257 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001258 {
ajs57a14772005-04-10 15:01:56 +00001259 zlog_warn("%s: socket %d message length %u is less than header size %d",
1260 __func__, sock, length, ZEBRA_HEADER_SIZE);
1261 zebra_client_close (client);
1262 return -1;
1263 }
1264 if (length > STREAM_SIZE(client->ibuf))
1265 {
1266 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1267 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001268 zebra_client_close (client);
1269 return -1;
1270 }
1271
paul718e3742002-12-13 20:15:29 +00001272 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001273 if (already < length)
paul718e3742002-12-13 20:15:29 +00001274 {
ajs57a14772005-04-10 15:01:56 +00001275 ssize_t nbyte;
1276 if (((nbyte = stream_read_try (client->ibuf, sock,
1277 length-already)) == 0) ||
1278 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001279 {
1280 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001281 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001282 zebra_client_close (client);
1283 return -1;
1284 }
ajs57a14772005-04-10 15:01:56 +00001285 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001286 {
1287 /* Try again later. */
1288 zebra_event (ZEBRA_READ, sock, client);
1289 return 0;
1290 }
paul718e3742002-12-13 20:15:29 +00001291 }
1292
ajs719e9742005-02-28 20:52:15 +00001293 length -= ZEBRA_HEADER_SIZE;
1294
paul718e3742002-12-13 20:15:29 +00001295 /* Debug packet information. */
1296 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001297 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001298
1299 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001300 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001301 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001302
1303 switch (command)
1304 {
hasso18a6dce2004-10-03 18:18:34 +00001305 case ZEBRA_ROUTER_ID_ADD:
1306 zread_router_id_add (client, length);
1307 break;
1308 case ZEBRA_ROUTER_ID_DELETE:
1309 zread_router_id_delete (client, length);
1310 break;
paul718e3742002-12-13 20:15:29 +00001311 case ZEBRA_INTERFACE_ADD:
1312 zread_interface_add (client, length);
1313 break;
1314 case ZEBRA_INTERFACE_DELETE:
1315 zread_interface_delete (client, length);
1316 break;
1317 case ZEBRA_IPV4_ROUTE_ADD:
1318 zread_ipv4_add (client, length);
1319 break;
1320 case ZEBRA_IPV4_ROUTE_DELETE:
1321 zread_ipv4_delete (client, length);
1322 break;
1323#ifdef HAVE_IPV6
1324 case ZEBRA_IPV6_ROUTE_ADD:
1325 zread_ipv6_add (client, length);
1326 break;
1327 case ZEBRA_IPV6_ROUTE_DELETE:
1328 zread_ipv6_delete (client, length);
1329 break;
1330#endif /* HAVE_IPV6 */
1331 case ZEBRA_REDISTRIBUTE_ADD:
1332 zebra_redistribute_add (command, client, length);
1333 break;
1334 case ZEBRA_REDISTRIBUTE_DELETE:
1335 zebra_redistribute_delete (command, client, length);
1336 break;
1337 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1338 zebra_redistribute_default_add (command, client, length);
1339 break;
1340 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1341 zebra_redistribute_default_delete (command, client, length);
1342 break;
1343 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1344 zread_ipv4_nexthop_lookup (client, length);
1345 break;
1346#ifdef HAVE_IPV6
1347 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1348 zread_ipv6_nexthop_lookup (client, length);
1349 break;
1350#endif /* HAVE_IPV6 */
1351 case ZEBRA_IPV4_IMPORT_LOOKUP:
1352 zread_ipv4_import_lookup (client, length);
1353 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001354 case ZEBRA_HELLO:
1355 zread_hello (client);
1356 break;
paul718e3742002-12-13 20:15:29 +00001357 default:
1358 zlog_info ("Zebra received unknown command %d", command);
1359 break;
1360 }
1361
ajs719e9742005-02-28 20:52:15 +00001362 if (client->t_suicide)
1363 {
1364 /* No need to wait for thread callback, just kill immediately. */
1365 zebra_client_close(client);
1366 return -1;
1367 }
1368
paul718e3742002-12-13 20:15:29 +00001369 stream_reset (client->ibuf);
1370 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001371 return 0;
1372}
1373
paul718e3742002-12-13 20:15:29 +00001374
1375/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001376static int
paul718e3742002-12-13 20:15:29 +00001377zebra_accept (struct thread *thread)
1378{
1379 int accept_sock;
1380 int client_sock;
1381 struct sockaddr_in client;
1382 socklen_t len;
1383
1384 accept_sock = THREAD_FD (thread);
1385
ajs719e9742005-02-28 20:52:15 +00001386 /* Reregister myself. */
1387 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1388
paul718e3742002-12-13 20:15:29 +00001389 len = sizeof (struct sockaddr_in);
1390 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1391
1392 if (client_sock < 0)
1393 {
ajs6099b3b2004-11-20 02:06:59 +00001394 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001395 return -1;
1396 }
1397
paulccf35572003-03-01 11:42:20 +00001398 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001399 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001400
paul718e3742002-12-13 20:15:29 +00001401 /* Create new zebra client. */
1402 zebra_client_create (client_sock);
1403
paul718e3742002-12-13 20:15:29 +00001404 return 0;
1405}
1406
paulb9df2d22004-05-09 09:09:59 +00001407#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001408/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001409static void
paul718e3742002-12-13 20:15:29 +00001410zebra_serv ()
1411{
1412 int ret;
1413 int accept_sock;
1414 struct sockaddr_in addr;
1415
1416 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1417
1418 if (accept_sock < 0)
1419 {
paul3d1dc852005-04-05 00:45:23 +00001420 zlog_warn ("Can't create zserv stream socket: %s",
1421 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001422 zlog_warn ("zebra can't provice full functionality due to above error");
1423 return;
1424 }
1425
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001426 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001427 memset (&addr, 0, sizeof (struct sockaddr_in));
1428 addr.sin_family = AF_INET;
1429 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001430#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001431 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001432#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001433 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1434
1435 sockopt_reuseaddr (accept_sock);
1436 sockopt_reuseport (accept_sock);
1437
pauledd7c242003-06-04 13:59:38 +00001438 if ( zserv_privs.change(ZPRIVS_RAISE) )
1439 zlog (NULL, LOG_ERR, "Can't raise privileges");
1440
paul718e3742002-12-13 20:15:29 +00001441 ret = bind (accept_sock, (struct sockaddr *)&addr,
1442 sizeof (struct sockaddr_in));
1443 if (ret < 0)
1444 {
paul3d1dc852005-04-05 00:45:23 +00001445 zlog_warn ("Can't bind to stream socket: %s",
1446 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001447 zlog_warn ("zebra can't provice full functionality due to above error");
1448 close (accept_sock); /* Avoid sd leak. */
1449 return;
1450 }
pauledd7c242003-06-04 13:59:38 +00001451
1452 if ( zserv_privs.change(ZPRIVS_LOWER) )
1453 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001454
1455 ret = listen (accept_sock, 1);
1456 if (ret < 0)
1457 {
paul3d1dc852005-04-05 00:45:23 +00001458 zlog_warn ("Can't listen to stream socket: %s",
1459 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001460 zlog_warn ("zebra can't provice full functionality due to above error");
1461 close (accept_sock); /* Avoid sd leak. */
1462 return;
1463 }
1464
1465 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1466}
paulb9df2d22004-05-09 09:09:59 +00001467#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001468
1469/* For sockaddr_un. */
1470#include <sys/un.h>
1471
1472/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001473static void
hassofce954f2004-10-07 20:29:24 +00001474zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001475{
1476 int ret;
1477 int sock, len;
1478 struct sockaddr_un serv;
1479 mode_t old_mask;
1480
1481 /* First of all, unlink existing socket */
1482 unlink (path);
1483
1484 /* Set umask */
1485 old_mask = umask (0077);
1486
1487 /* Make UNIX domain socket. */
1488 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1489 if (sock < 0)
1490 {
paul3d1dc852005-04-05 00:45:23 +00001491 zlog_warn ("Can't create zserv unix socket: %s",
1492 safe_strerror (errno));
1493 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001494 return;
1495 }
1496
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001497 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1498
paul718e3742002-12-13 20:15:29 +00001499 /* Make server socket. */
1500 memset (&serv, 0, sizeof (struct sockaddr_un));
1501 serv.sun_family = AF_UNIX;
1502 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001503#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001504 len = serv.sun_len = SUN_LEN(&serv);
1505#else
1506 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001507#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001508
1509 ret = bind (sock, (struct sockaddr *) &serv, len);
1510 if (ret < 0)
1511 {
paul3d1dc852005-04-05 00:45:23 +00001512 zlog_warn ("Can't bind to unix socket %s: %s",
1513 path, safe_strerror (errno));
1514 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001515 close (sock);
1516 return;
1517 }
1518
1519 ret = listen (sock, 5);
1520 if (ret < 0)
1521 {
paul3d1dc852005-04-05 00:45:23 +00001522 zlog_warn ("Can't listen to unix socket %s: %s",
1523 path, safe_strerror (errno));
1524 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001525 close (sock);
1526 return;
1527 }
1528
1529 umask (old_mask);
1530
1531 zebra_event (ZEBRA_SERV, sock, NULL);
1532}
1533
paul718e3742002-12-13 20:15:29 +00001534
paulb9df2d22004-05-09 09:09:59 +00001535static void
paul718e3742002-12-13 20:15:29 +00001536zebra_event (enum event event, int sock, struct zserv *client)
1537{
1538 switch (event)
1539 {
1540 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001541 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001542 break;
1543 case ZEBRA_READ:
1544 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001545 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001546 break;
1547 case ZEBRA_WRITE:
1548 /**/
1549 break;
1550 }
1551}
1552
1553/* Display default rtm_table for all clients. */
1554DEFUN (show_table,
1555 show_table_cmd,
1556 "show table",
1557 SHOW_STR
1558 "default routing table to use for all clients\n")
1559{
paulb21b19c2003-06-15 01:28:29 +00001560 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001561 VTY_NEWLINE);
1562 return CMD_SUCCESS;
1563}
1564
1565DEFUN (config_table,
1566 config_table_cmd,
1567 "table TABLENO",
1568 "Configure target kernel routing table\n"
1569 "TABLE integer\n")
1570{
paulb21b19c2003-06-15 01:28:29 +00001571 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001572 return CMD_SUCCESS;
1573}
1574
hasso647e4f12003-05-25 11:43:52 +00001575DEFUN (ip_forwarding,
1576 ip_forwarding_cmd,
1577 "ip forwarding",
1578 IP_STR
1579 "Turn on IP forwarding")
1580{
1581 int ret;
1582
1583 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001584 if (ret == 0)
1585 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001586
hasso647e4f12003-05-25 11:43:52 +00001587 if (ret == 0)
1588 {
1589 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1590 return CMD_WARNING;
1591 }
1592
1593 return CMD_SUCCESS;
1594}
1595
paul718e3742002-12-13 20:15:29 +00001596DEFUN (no_ip_forwarding,
1597 no_ip_forwarding_cmd,
1598 "no ip forwarding",
1599 NO_STR
1600 IP_STR
1601 "Turn off IP forwarding")
1602{
1603 int ret;
1604
1605 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001606 if (ret != 0)
1607 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001608
paul718e3742002-12-13 20:15:29 +00001609 if (ret != 0)
1610 {
1611 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1612 return CMD_WARNING;
1613 }
1614
1615 return CMD_SUCCESS;
1616}
1617
1618/* This command is for debugging purpose. */
1619DEFUN (show_zebra_client,
1620 show_zebra_client_cmd,
1621 "show zebra client",
1622 SHOW_STR
1623 "Zebra information"
1624 "Client information")
1625{
hasso52dc7ee2004-09-23 19:18:23 +00001626 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001627 struct zserv *client;
1628
paul1eb8ef22005-04-07 07:30:20 +00001629 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1630 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1631
paul718e3742002-12-13 20:15:29 +00001632 return CMD_SUCCESS;
1633}
1634
1635/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001636static int
paul718e3742002-12-13 20:15:29 +00001637config_write_table (struct vty *vty)
1638{
paulb21b19c2003-06-15 01:28:29 +00001639 if (zebrad.rtm_table_default)
1640 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001641 VTY_NEWLINE);
1642 return 0;
1643}
1644
1645/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001646static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001647{
1648 TABLE_NODE,
1649 "", /* This node has no interface. */
1650 1
1651};
1652
1653/* Only display ip forwarding is enabled or not. */
1654DEFUN (show_ip_forwarding,
1655 show_ip_forwarding_cmd,
1656 "show ip forwarding",
1657 SHOW_STR
1658 IP_STR
1659 "IP forwarding status\n")
1660{
1661 int ret;
1662
1663 ret = ipforward ();
1664
1665 if (ret == 0)
1666 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1667 else
1668 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1669 return CMD_SUCCESS;
1670}
1671
1672#ifdef HAVE_IPV6
1673/* Only display ipv6 forwarding is enabled or not. */
1674DEFUN (show_ipv6_forwarding,
1675 show_ipv6_forwarding_cmd,
1676 "show ipv6 forwarding",
1677 SHOW_STR
1678 "IPv6 information\n"
1679 "Forwarding status\n")
1680{
1681 int ret;
1682
1683 ret = ipforward_ipv6 ();
1684
1685 switch (ret)
1686 {
1687 case -1:
1688 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1689 break;
1690 case 0:
1691 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1692 break;
1693 case 1:
1694 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1695 break;
1696 default:
1697 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1698 break;
1699 }
1700 return CMD_SUCCESS;
1701}
1702
hasso55906722004-02-11 22:42:16 +00001703DEFUN (ipv6_forwarding,
1704 ipv6_forwarding_cmd,
1705 "ipv6 forwarding",
1706 IPV6_STR
1707 "Turn on IPv6 forwarding")
1708{
1709 int ret;
1710
hasso41d3fc92004-04-06 11:59:00 +00001711 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001712 if (ret == 0)
1713 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001714
hasso41d3fc92004-04-06 11:59:00 +00001715 if (ret == 0)
1716 {
hasso55906722004-02-11 22:42:16 +00001717 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1718 return CMD_WARNING;
1719 }
1720
1721 return CMD_SUCCESS;
1722}
1723
paul718e3742002-12-13 20:15:29 +00001724DEFUN (no_ipv6_forwarding,
1725 no_ipv6_forwarding_cmd,
1726 "no ipv6 forwarding",
1727 NO_STR
hasso55906722004-02-11 22:42:16 +00001728 IPV6_STR
1729 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001730{
1731 int ret;
1732
hasso41d3fc92004-04-06 11:59:00 +00001733 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001734 if (ret != 0)
1735 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001736
paul718e3742002-12-13 20:15:29 +00001737 if (ret != 0)
1738 {
1739 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1740 return CMD_WARNING;
1741 }
1742
1743 return CMD_SUCCESS;
1744}
1745
1746#endif /* HAVE_IPV6 */
1747
1748/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001749static int
paul718e3742002-12-13 20:15:29 +00001750config_write_forwarding (struct vty *vty)
1751{
hasso18a6dce2004-10-03 18:18:34 +00001752 /* FIXME: Find better place for that. */
1753 router_id_write (vty);
1754
paul3e0b3a52004-08-23 18:58:32 +00001755 if (ipforward ())
1756 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001757#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001758 if (ipforward_ipv6 ())
1759 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001760#endif /* HAVE_IPV6 */
1761 vty_out (vty, "!%s", VTY_NEWLINE);
1762 return 0;
1763}
1764
1765/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001766static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001767{
1768 FORWARDING_NODE,
1769 "", /* This node has no interface. */
1770 1
1771};
1772
1773
1774/* Initialisation of zebra and installation of commands. */
1775void
paula1ac18c2005-06-28 17:17:12 +00001776zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001777{
1778 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001779 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001780
paul718e3742002-12-13 20:15:29 +00001781 /* Install configuration write function. */
1782 install_node (&table_node, config_write_table);
1783 install_node (&forwarding_node, config_write_forwarding);
1784
1785 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1786 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001787 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001788 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1789 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1790
1791#ifdef HAVE_NETLINK
1792 install_element (VIEW_NODE, &show_table_cmd);
1793 install_element (ENABLE_NODE, &show_table_cmd);
1794 install_element (CONFIG_NODE, &config_table_cmd);
1795#endif /* HAVE_NETLINK */
1796
1797#ifdef HAVE_IPV6
1798 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1799 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001800 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001801 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1802#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001803
1804 /* Route-map */
1805 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001806}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001807
1808/* Make zebra server socket, wiping any existing one (see bug #403). */
1809void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001810zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001811{
1812#ifdef HAVE_TCP_ZEBRA
1813 zebra_serv ();
1814#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001815 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001816#endif /* HAVE_TCP_ZEBRA */
1817}