blob: 89eb266a18f2b2fa2725c0adf7b8ac6da27f06bf [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"
David Lamparter6b0655a2014-06-04 06:53:35 +020045
paul718e3742002-12-13 20:15:29 +000046/* 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;
David Lamparter6b0655a2014-06-04 06:53:35 +020054
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 {
Christian Frankefa713d92013-07-05 15:35:37 +0000392 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
393 || nexthop_has_fib_child(nexthop))
paulb9df2d22004-05-09 09:09:59 +0000394 {
paul1dcb5172005-05-31 08:38:50 +0000395 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
396 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
397
398 if (nhnummark == 0)
399 {
400 nhnummark = stream_get_endp (s);
401 stream_putc (s, 1); /* placeholder */
402 }
403
paulb9df2d22004-05-09 09:09:59 +0000404 nhnum++;
paul718e3742002-12-13 20:15:29 +0000405
paulb9df2d22004-05-09 09:09:59 +0000406 switch(nexthop->type)
407 {
408 case NEXTHOP_TYPE_IPV4:
409 case NEXTHOP_TYPE_IPV4_IFINDEX:
410 stream_put_in_addr (s, &nexthop->gate.ipv4);
411 break;
412#ifdef HAVE_IPV6
413 case NEXTHOP_TYPE_IPV6:
414 case NEXTHOP_TYPE_IPV6_IFINDEX:
415 case NEXTHOP_TYPE_IPV6_IFNAME:
416 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
417 break;
418#endif
419 default:
420 if (cmd == ZEBRA_IPV4_ROUTE_ADD
421 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
422 {
423 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000424 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000425 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
426 }
427 else
428 {
429 struct in6_addr empty;
430 memset (&empty, 0, sizeof (struct in6_addr));
431 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
432 }
433 }
paul718e3742002-12-13 20:15:29 +0000434
paulb9df2d22004-05-09 09:09:59 +0000435 /* Interface index. */
436 stream_putc (s, 1);
437 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000438
paulb9df2d22004-05-09 09:09:59 +0000439 break;
440 }
paul718e3742002-12-13 20:15:29 +0000441 }
442
443 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700444 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000445 {
vincentfbf5d032005-09-29 11:25:50 +0000446 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
447 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000448 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
449 stream_putl (s, rib->metric);
450 }
451
452 /* write real message flags value */
453 stream_putc_at (s, messmark, zapi_flags);
454
paulb9df2d22004-05-09 09:09:59 +0000455 /* Write next-hop number */
456 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000457 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000458
paul718e3742002-12-13 20:15:29 +0000459 /* Write packet size. */
460 stream_putw_at (s, 0, stream_get_endp (s));
461
ajs719e9742005-02-28 20:52:15 +0000462 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000463}
464
paul718e3742002-12-13 20:15:29 +0000465#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000466static int
paul718e3742002-12-13 20:15:29 +0000467zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
468{
469 struct stream *s;
470 struct rib *rib;
471 unsigned long nump;
472 u_char num;
473 struct nexthop *nexthop;
474
475 /* Lookup nexthop. */
476 rib = rib_match_ipv6 (addr);
477
478 /* Get output stream. */
479 s = client->obuf;
480 stream_reset (s);
481
482 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000483 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000484 stream_put (s, &addr, 16);
485
486 if (rib)
487 {
488 stream_putl (s, rib->metric);
489 num = 0;
paul9985f832005-02-09 15:51:56 +0000490 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000491 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000492 /* Only non-recursive routes are elegible to resolve nexthop we
493 * are looking up. Therefore, we will just iterate over the top
494 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000495 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
496 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
497 {
498 stream_putc (s, nexthop->type);
499 switch (nexthop->type)
500 {
501 case ZEBRA_NEXTHOP_IPV6:
502 stream_put (s, &nexthop->gate.ipv6, 16);
503 break;
504 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
505 case ZEBRA_NEXTHOP_IPV6_IFNAME:
506 stream_put (s, &nexthop->gate.ipv6, 16);
507 stream_putl (s, nexthop->ifindex);
508 break;
509 case ZEBRA_NEXTHOP_IFINDEX:
510 case ZEBRA_NEXTHOP_IFNAME:
511 stream_putl (s, nexthop->ifindex);
512 break;
hassofa2b17e2004-03-04 17:45:00 +0000513 default:
514 /* do nothing */
515 break;
paul718e3742002-12-13 20:15:29 +0000516 }
517 num++;
518 }
519 stream_putc_at (s, nump, num);
520 }
521 else
522 {
523 stream_putl (s, 0);
524 stream_putc (s, 0);
525 }
526
527 stream_putw_at (s, 0, stream_get_endp (s));
528
ajs719e9742005-02-28 20:52:15 +0000529 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000530}
531#endif /* HAVE_IPV6 */
532
paulb9df2d22004-05-09 09:09:59 +0000533static int
paul718e3742002-12-13 20:15:29 +0000534zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
535{
536 struct stream *s;
537 struct rib *rib;
538 unsigned long nump;
539 u_char num;
540 struct nexthop *nexthop;
541
542 /* Lookup nexthop. */
543 rib = rib_match_ipv4 (addr);
544
545 /* Get output stream. */
546 s = client->obuf;
547 stream_reset (s);
548
549 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000550 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000551 stream_put_in_addr (s, &addr);
552
553 if (rib)
554 {
Christian Frankebb97e462013-05-25 14:01:35 +0000555 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
556 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000557 stream_putl (s, rib->metric);
558 num = 0;
paul9985f832005-02-09 15:51:56 +0000559 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000560 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000561 /* Only non-recursive routes are elegible to resolve the nexthop we
562 * are looking up. Therefore, we will just iterate over the top
563 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000564 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
565 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
566 {
567 stream_putc (s, nexthop->type);
568 switch (nexthop->type)
569 {
570 case ZEBRA_NEXTHOP_IPV4:
571 stream_put_in_addr (s, &nexthop->gate.ipv4);
572 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000573 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
574 stream_put_in_addr (s, &nexthop->gate.ipv4);
575 stream_putl (s, nexthop->ifindex);
576 break;
paul718e3742002-12-13 20:15:29 +0000577 case ZEBRA_NEXTHOP_IFINDEX:
578 case ZEBRA_NEXTHOP_IFNAME:
579 stream_putl (s, nexthop->ifindex);
580 break;
hassofa2b17e2004-03-04 17:45:00 +0000581 default:
582 /* do nothing */
583 break;
paul718e3742002-12-13 20:15:29 +0000584 }
585 num++;
586 }
587 stream_putc_at (s, nump, num);
588 }
589 else
590 {
Christian Frankebb97e462013-05-25 14:01:35 +0000591 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
592 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000593 stream_putl (s, 0);
594 stream_putc (s, 0);
595 }
596
597 stream_putw_at (s, 0, stream_get_endp (s));
598
ajs719e9742005-02-28 20:52:15 +0000599 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000600}
601
Everton Marquesbe4fb432014-07-01 15:15:52 -0300602/*
603 Modified version of zsend_ipv4_nexthop_lookup():
604 Query unicast rib if nexthop is not found on mrib.
605 Returns both route metric and protocol distance.
606*/
607static int
608zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr)
609{
610 struct stream *s;
611 struct rib *rib;
612 unsigned long nump;
613 u_char num;
614 struct nexthop *nexthop;
615
616 /* Lookup nexthop. */
617 rib = rib_match_ipv4_safi (addr, SAFI_MULTICAST);
618
619 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
620 zlog_debug("%s: %s mrib entry found.", __func__, rib ? "Matching" : "No matching");
621
622 if (!rib) {
623 /* Retry lookup with unicast rib */
624 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST);
625 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
626 zlog_debug("%s: %s rib entry found.", __func__, rib ? "Matching" : "No matching");
627 }
628
629 /* Get output stream. */
630 s = client->obuf;
631 stream_reset (s);
632
633 /* Fill in result. */
634 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB);
635 stream_put_in_addr (s, &addr);
636
637 if (rib)
638 {
639 stream_putc (s, rib->distance);
640 stream_putl (s, rib->metric);
641 num = 0;
642 nump = stream_get_endp(s); /* remember position for nexthop_num */
643 stream_putc (s, 0); /* reserve room for nexthop_num */
644 /* Only non-recursive routes are elegible to resolve the nexthop we
645 * are looking up. Therefore, we will just iterate over the top
646 * chain of nexthops. */
647 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
648 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
649 {
650 stream_putc (s, nexthop->type);
651 switch (nexthop->type)
652 {
653 case ZEBRA_NEXTHOP_IPV4:
654 stream_put_in_addr (s, &nexthop->gate.ipv4);
655 break;
656 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
657 stream_put_in_addr (s, &nexthop->gate.ipv4);
658 stream_putl (s, nexthop->ifindex);
659 break;
660 case ZEBRA_NEXTHOP_IFINDEX:
661 case ZEBRA_NEXTHOP_IFNAME:
662 stream_putl (s, nexthop->ifindex);
663 break;
664 default:
665 /* do nothing */
666 break;
667 }
668 num++;
669 }
670
671 stream_putc_at (s, nump, num); /* store nexthop_num */
672 }
673 else
674 {
675 stream_putc (s, 0); /* distance */
676 stream_putl (s, 0); /* metric */
677 stream_putc (s, 0); /* nexthop_num */
678 }
679
680 stream_putw_at (s, 0, stream_get_endp (s));
681
682 return zebra_server_send_message(client);
683}
684
paulb9df2d22004-05-09 09:09:59 +0000685static int
paul718e3742002-12-13 20:15:29 +0000686zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
687{
688 struct stream *s;
689 struct rib *rib;
690 unsigned long nump;
691 u_char num;
692 struct nexthop *nexthop;
693
694 /* Lookup nexthop. */
695 rib = rib_lookup_ipv4 (p);
696
697 /* Get output stream. */
698 s = client->obuf;
699 stream_reset (s);
700
701 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000702 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000703 stream_put_in_addr (s, &p->prefix);
704
705 if (rib)
706 {
707 stream_putl (s, rib->metric);
708 num = 0;
paul9985f832005-02-09 15:51:56 +0000709 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000710 stream_putc (s, 0);
711 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Christian Frankefa713d92013-07-05 15:35:37 +0000712 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
713 || nexthop_has_fib_child(nexthop))
paul718e3742002-12-13 20:15:29 +0000714 {
715 stream_putc (s, nexthop->type);
716 switch (nexthop->type)
717 {
718 case ZEBRA_NEXTHOP_IPV4:
719 stream_put_in_addr (s, &nexthop->gate.ipv4);
720 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000721 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
722 stream_put_in_addr (s, &nexthop->gate.ipv4);
723 stream_putl (s, nexthop->ifindex);
724 break;
paul718e3742002-12-13 20:15:29 +0000725 case ZEBRA_NEXTHOP_IFINDEX:
726 case ZEBRA_NEXTHOP_IFNAME:
727 stream_putl (s, nexthop->ifindex);
728 break;
hassofa2b17e2004-03-04 17:45:00 +0000729 default:
730 /* do nothing */
731 break;
paul718e3742002-12-13 20:15:29 +0000732 }
733 num++;
734 }
735 stream_putc_at (s, nump, num);
736 }
737 else
738 {
739 stream_putl (s, 0);
740 stream_putc (s, 0);
741 }
742
743 stream_putw_at (s, 0, stream_get_endp (s));
744
ajs719e9742005-02-28 20:52:15 +0000745 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000746}
David Lamparter6b0655a2014-06-04 06:53:35 +0200747
hasso18a6dce2004-10-03 18:18:34 +0000748/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
749int
750zsend_router_id_update (struct zserv *client, struct prefix *p)
751{
752 struct stream *s;
753 int blen;
754
755 /* Check this client need interface information. */
756 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000757 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000758
759 s = client->obuf;
760 stream_reset (s);
761
hasso18a6dce2004-10-03 18:18:34 +0000762 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000763 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000764
765 /* Prefix information. */
766 stream_putc (s, p->family);
767 blen = prefix_blen (p);
768 stream_put (s, &p->u.prefix, blen);
769 stream_putc (s, p->prefixlen);
770
771 /* Write packet size. */
772 stream_putw_at (s, 0, stream_get_endp (s));
773
ajs719e9742005-02-28 20:52:15 +0000774 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000775}
David Lamparter6b0655a2014-06-04 06:53:35 +0200776
paul718e3742002-12-13 20:15:29 +0000777/* Register zebra server interface information. Send current all
778 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000779static int
paul718e3742002-12-13 20:15:29 +0000780zread_interface_add (struct zserv *client, u_short length)
781{
paul1eb8ef22005-04-07 07:30:20 +0000782 struct listnode *ifnode, *ifnnode;
783 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000784 struct interface *ifp;
785 struct connected *c;
786
787 /* Interface information is needed. */
788 client->ifinfo = 1;
789
paul1eb8ef22005-04-07 07:30:20 +0000790 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000791 {
paul718e3742002-12-13 20:15:29 +0000792 /* Skip pseudo interface. */
793 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
794 continue;
795
ajs719e9742005-02-28 20:52:15 +0000796 if (zsend_interface_add (client, ifp) < 0)
797 return -1;
paul718e3742002-12-13 20:15:29 +0000798
paul1eb8ef22005-04-07 07:30:20 +0000799 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000800 {
ajs719e9742005-02-28 20:52:15 +0000801 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
802 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
803 ifp, c) < 0))
804 return -1;
paul718e3742002-12-13 20:15:29 +0000805 }
806 }
ajs719e9742005-02-28 20:52:15 +0000807 return 0;
paul718e3742002-12-13 20:15:29 +0000808}
809
810/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000811static int
paul718e3742002-12-13 20:15:29 +0000812zread_interface_delete (struct zserv *client, u_short length)
813{
814 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000815 return 0;
paul718e3742002-12-13 20:15:29 +0000816}
817
818/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000819/*
820 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
821 * add kernel route.
822 */
ajs719e9742005-02-28 20:52:15 +0000823static int
paul718e3742002-12-13 20:15:29 +0000824zread_ipv4_add (struct zserv *client, u_short length)
825{
826 int i;
827 struct rib *rib;
828 struct prefix_ipv4 p;
829 u_char message;
830 struct in_addr nexthop;
831 u_char nexthop_num;
832 u_char nexthop_type;
833 struct stream *s;
834 unsigned int ifindex;
835 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400836 safi_t safi;
837
paul718e3742002-12-13 20:15:29 +0000838
839 /* Get input stream. */
840 s = client->ibuf;
841
842 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000843 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
844
paul718e3742002-12-13 20:15:29 +0000845 /* Type, flags, message. */
846 rib->type = stream_getc (s);
847 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000848 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400849 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000850 rib->uptime = time (NULL);
851
852 /* IPv4 prefix. */
853 memset (&p, 0, sizeof (struct prefix_ipv4));
854 p.family = AF_INET;
855 p.prefixlen = stream_getc (s);
856 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
857
858 /* Nexthop parse. */
859 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
860 {
861 nexthop_num = stream_getc (s);
862
863 for (i = 0; i < nexthop_num; i++)
864 {
865 nexthop_type = stream_getc (s);
866
867 switch (nexthop_type)
868 {
869 case ZEBRA_NEXTHOP_IFINDEX:
870 ifindex = stream_getl (s);
871 nexthop_ifindex_add (rib, ifindex);
872 break;
873 case ZEBRA_NEXTHOP_IFNAME:
874 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000875 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000876 break;
877 case ZEBRA_NEXTHOP_IPV4:
878 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000879 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000880 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200881 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
882 nexthop.s_addr = stream_get_ipv4 (s);
883 ifindex = stream_getl (s);
884 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
885 break;
paul718e3742002-12-13 20:15:29 +0000886 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000887 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000888 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700889 case ZEBRA_NEXTHOP_BLACKHOLE:
890 nexthop_blackhole_add (rib);
891 break;
892 }
paul718e3742002-12-13 20:15:29 +0000893 }
894 }
895
896 /* Distance. */
897 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
898 rib->distance = stream_getc (s);
899
900 /* Metric. */
901 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
902 rib->metric = stream_getl (s);
903
Paul Jakma171eee32006-07-27 16:11:02 +0000904 /* Table */
905 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400906 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000907 return 0;
paul718e3742002-12-13 20:15:29 +0000908}
909
910/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000911static int
paul718e3742002-12-13 20:15:29 +0000912zread_ipv4_delete (struct zserv *client, u_short length)
913{
914 int i;
915 struct stream *s;
916 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700917 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000918 unsigned long ifindex;
919 struct prefix_ipv4 p;
920 u_char nexthop_num;
921 u_char nexthop_type;
922 u_char ifname_len;
923
924 s = client->ibuf;
925 ifindex = 0;
926 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700927 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000928
929 /* Type, flags, message. */
930 api.type = stream_getc (s);
931 api.flags = stream_getc (s);
932 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400933 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000934
935 /* IPv4 prefix. */
936 memset (&p, 0, sizeof (struct prefix_ipv4));
937 p.family = AF_INET;
938 p.prefixlen = stream_getc (s);
939 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
940
941 /* Nexthop, ifindex, distance, metric. */
942 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
943 {
944 nexthop_num = stream_getc (s);
945
946 for (i = 0; i < nexthop_num; i++)
947 {
948 nexthop_type = stream_getc (s);
949
950 switch (nexthop_type)
951 {
952 case ZEBRA_NEXTHOP_IFINDEX:
953 ifindex = stream_getl (s);
954 break;
955 case ZEBRA_NEXTHOP_IFNAME:
956 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000957 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000958 break;
959 case ZEBRA_NEXTHOP_IPV4:
960 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700961 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000962 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200963 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
964 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +0000965 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200966 ifindex = stream_getl (s);
967 break;
paul718e3742002-12-13 20:15:29 +0000968 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000969 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000970 break;
971 }
972 }
973 }
974
975 /* Distance. */
976 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
977 api.distance = stream_getc (s);
978 else
979 api.distance = 0;
980
981 /* Metric. */
982 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
983 api.metric = stream_getl (s);
984 else
985 api.metric = 0;
986
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700987 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400988 client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +0000989 return 0;
paul718e3742002-12-13 20:15:29 +0000990}
991
992/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000993static int
paul718e3742002-12-13 20:15:29 +0000994zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
995{
996 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +0000997 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +0000998
999 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001000 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1001 zlog_debug("%s: looking up %s", __func__,
1002 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
ajs719e9742005-02-28 20:52:15 +00001003 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +00001004}
1005
Everton Marquesbe4fb432014-07-01 15:15:52 -03001006/* MRIB Nexthop lookup for IPv4. */
1007static int
1008zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length)
1009{
1010 struct in_addr addr;
1011
1012 addr.s_addr = stream_get_ipv4 (client->ibuf);
1013 return zsend_ipv4_nexthop_lookup_mrib (client, addr);
1014}
1015
paul718e3742002-12-13 20:15:29 +00001016/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001017static int
paul718e3742002-12-13 20:15:29 +00001018zread_ipv4_import_lookup (struct zserv *client, u_short length)
1019{
1020 struct prefix_ipv4 p;
1021
1022 p.family = AF_INET;
1023 p.prefixlen = stream_getc (client->ibuf);
1024 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1025
ajs719e9742005-02-28 20:52:15 +00001026 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +00001027}
1028
1029#ifdef HAVE_IPV6
1030/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001031static int
paul718e3742002-12-13 20:15:29 +00001032zread_ipv6_add (struct zserv *client, u_short length)
1033{
1034 int i;
1035 struct stream *s;
1036 struct zapi_ipv6 api;
1037 struct in6_addr nexthop;
1038 unsigned long ifindex;
1039 struct prefix_ipv6 p;
1040
1041 s = client->ibuf;
1042 ifindex = 0;
1043 memset (&nexthop, 0, sizeof (struct in6_addr));
1044
1045 /* Type, flags, message. */
1046 api.type = stream_getc (s);
1047 api.flags = stream_getc (s);
1048 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001049 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001050
1051 /* IPv4 prefix. */
1052 memset (&p, 0, sizeof (struct prefix_ipv6));
1053 p.family = AF_INET6;
1054 p.prefixlen = stream_getc (s);
1055 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1056
1057 /* Nexthop, ifindex, distance, metric. */
1058 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1059 {
1060 u_char nexthop_type;
1061
1062 api.nexthop_num = stream_getc (s);
1063 for (i = 0; i < api.nexthop_num; i++)
1064 {
1065 nexthop_type = stream_getc (s);
1066
1067 switch (nexthop_type)
1068 {
1069 case ZEBRA_NEXTHOP_IPV6:
1070 stream_get (&nexthop, s, 16);
1071 break;
1072 case ZEBRA_NEXTHOP_IFINDEX:
1073 ifindex = stream_getl (s);
1074 break;
1075 }
1076 }
1077 }
1078
1079 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1080 api.distance = stream_getc (s);
1081 else
1082 api.distance = 0;
1083
1084 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1085 api.metric = stream_getl (s);
1086 else
1087 api.metric = 0;
1088
1089 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001090 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +04001091 api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +00001092 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +01001093 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
G.Balajif768f362011-11-26 22:10:39 +04001094 api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +00001095 return 0;
paul718e3742002-12-13 20:15:29 +00001096}
1097
1098/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001099static int
paul718e3742002-12-13 20:15:29 +00001100zread_ipv6_delete (struct zserv *client, u_short length)
1101{
1102 int i;
1103 struct stream *s;
1104 struct zapi_ipv6 api;
1105 struct in6_addr nexthop;
1106 unsigned long ifindex;
1107 struct prefix_ipv6 p;
1108
1109 s = client->ibuf;
1110 ifindex = 0;
1111 memset (&nexthop, 0, sizeof (struct in6_addr));
1112
1113 /* Type, flags, message. */
1114 api.type = stream_getc (s);
1115 api.flags = stream_getc (s);
1116 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001117 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001118
1119 /* IPv4 prefix. */
1120 memset (&p, 0, sizeof (struct prefix_ipv6));
1121 p.family = AF_INET6;
1122 p.prefixlen = stream_getc (s);
1123 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1124
1125 /* Nexthop, ifindex, distance, metric. */
1126 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1127 {
1128 u_char nexthop_type;
1129
1130 api.nexthop_num = stream_getc (s);
1131 for (i = 0; i < api.nexthop_num; i++)
1132 {
1133 nexthop_type = stream_getc (s);
1134
1135 switch (nexthop_type)
1136 {
1137 case ZEBRA_NEXTHOP_IPV6:
1138 stream_get (&nexthop, s, 16);
1139 break;
1140 case ZEBRA_NEXTHOP_IFINDEX:
1141 ifindex = stream_getl (s);
1142 break;
1143 }
1144 }
1145 }
1146
1147 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1148 api.distance = stream_getc (s);
1149 else
1150 api.distance = 0;
1151 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1152 api.metric = stream_getl (s);
1153 else
1154 api.metric = 0;
1155
1156 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
G.Balajif768f362011-11-26 22:10:39 +04001157 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
paul718e3742002-12-13 20:15:29 +00001158 else
G.Balajif768f362011-11-26 22:10:39 +04001159 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
ajs719e9742005-02-28 20:52:15 +00001160 return 0;
paul718e3742002-12-13 20:15:29 +00001161}
1162
ajs719e9742005-02-28 20:52:15 +00001163static int
paul718e3742002-12-13 20:15:29 +00001164zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1165{
1166 struct in6_addr addr;
1167 char buf[BUFSIZ];
1168
1169 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001170 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1171 zlog_debug("%s: looking up %s", __func__,
1172 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001173
ajs719e9742005-02-28 20:52:15 +00001174 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001175}
1176#endif /* HAVE_IPV6 */
1177
hasso18a6dce2004-10-03 18:18:34 +00001178/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001179static int
hasso18a6dce2004-10-03 18:18:34 +00001180zread_router_id_add (struct zserv *client, u_short length)
1181{
1182 struct prefix p;
1183
1184 /* Router-id information is needed. */
1185 client->ridinfo = 1;
1186
1187 router_id_get (&p);
1188
ajs719e9742005-02-28 20:52:15 +00001189 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001190}
1191
1192/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001193static int
hasso18a6dce2004-10-03 18:18:34 +00001194zread_router_id_delete (struct zserv *client, u_short length)
1195{
1196 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001197 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001198}
1199
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001200/* Tie up route-type and client->sock */
1201static void
1202zread_hello (struct zserv *client)
1203{
1204 /* type of protocol (lib/zebra.h) */
1205 u_char proto;
1206 proto = stream_getc (client->ibuf);
1207
1208 /* accept only dynamic routing protocols */
1209 if ((proto < ZEBRA_ROUTE_MAX)
1210 && (proto > ZEBRA_ROUTE_STATIC))
1211 {
1212 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1213 client->sock, zebra_route_string(proto));
1214
1215 /* if route-type was binded by other client */
1216 if (route_type_oaths[proto])
1217 zlog_warn ("sender of %s routes changed %c->%c",
1218 zebra_route_string(proto), route_type_oaths[proto],
1219 client->sock);
1220
1221 route_type_oaths[proto] = client->sock;
1222 }
1223}
1224
1225/* If client sent routes of specific type, zebra removes it
1226 * and returns number of deleted routes.
1227 */
1228static void
1229zebra_score_rib (int client_sock)
1230{
1231 int i;
1232
1233 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1234 if (client_sock == route_type_oaths[i])
1235 {
1236 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1237 client_sock, rib_score_proto (i), zebra_route_string (i));
1238 route_type_oaths[i] = 0;
1239 break;
1240 }
1241}
1242
paul718e3742002-12-13 20:15:29 +00001243/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001244static void
paul718e3742002-12-13 20:15:29 +00001245zebra_client_close (struct zserv *client)
1246{
1247 /* Close file descriptor. */
1248 if (client->sock)
1249 {
1250 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001251 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001252 client->sock = -1;
1253 }
1254
1255 /* Free stream buffers. */
1256 if (client->ibuf)
1257 stream_free (client->ibuf);
1258 if (client->obuf)
1259 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001260 if (client->wb)
1261 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001262
1263 /* Release threads. */
1264 if (client->t_read)
1265 thread_cancel (client->t_read);
1266 if (client->t_write)
1267 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001268 if (client->t_suicide)
1269 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001270
1271 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001272 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001273 XFREE (0, client);
1274}
1275
1276/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001277static void
paul718e3742002-12-13 20:15:29 +00001278zebra_client_create (int sock)
1279{
1280 struct zserv *client;
1281
1282 client = XCALLOC (0, sizeof (struct zserv));
1283
1284 /* Make client input/output buffer. */
1285 client->sock = sock;
1286 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1287 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001288 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001289
1290 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001291 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001292
1293 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001294 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001295
1296 /* Make new read thread. */
1297 zebra_event (ZEBRA_READ, sock, client);
1298}
1299
1300/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001301static int
paul718e3742002-12-13 20:15:29 +00001302zebra_client_read (struct thread *thread)
1303{
1304 int sock;
1305 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001306 size_t already;
paulc1b98002006-01-16 01:54:02 +00001307 uint16_t length, command;
1308 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001309
1310 /* Get thread data. Reset reading thread because I'm running. */
1311 sock = THREAD_FD (thread);
1312 client = THREAD_ARG (thread);
1313 client->t_read = NULL;
1314
ajs719e9742005-02-28 20:52:15 +00001315 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001316 {
ajs719e9742005-02-28 20:52:15 +00001317 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001318 return -1;
1319 }
ajs719e9742005-02-28 20:52:15 +00001320
1321 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001322 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001323 {
ajs57a14772005-04-10 15:01:56 +00001324 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001325 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001326 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001327 (nbyte == -1))
1328 {
1329 if (IS_ZEBRA_DEBUG_EVENT)
1330 zlog_debug ("connection closed socket [%d]", sock);
1331 zebra_client_close (client);
1332 return -1;
1333 }
ajs57a14772005-04-10 15:01:56 +00001334 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001335 {
1336 /* Try again later. */
1337 zebra_event (ZEBRA_READ, sock, client);
1338 return 0;
1339 }
ajs57a14772005-04-10 15:01:56 +00001340 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001341 }
1342
1343 /* Reset to read from the beginning of the incoming packet. */
1344 stream_set_getp(client->ibuf, 0);
1345
paulc1b98002006-01-16 01:54:02 +00001346 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001347 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001348 marker = stream_getc (client->ibuf);
1349 version = stream_getc (client->ibuf);
1350 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001351
paulc1b98002006-01-16 01:54:02 +00001352 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1353 {
1354 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1355 __func__, sock, marker, version);
1356 zebra_client_close (client);
1357 return -1;
1358 }
ajs719e9742005-02-28 20:52:15 +00001359 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001360 {
ajs57a14772005-04-10 15:01:56 +00001361 zlog_warn("%s: socket %d message length %u is less than header size %d",
1362 __func__, sock, length, ZEBRA_HEADER_SIZE);
1363 zebra_client_close (client);
1364 return -1;
1365 }
1366 if (length > STREAM_SIZE(client->ibuf))
1367 {
1368 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1369 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001370 zebra_client_close (client);
1371 return -1;
1372 }
1373
paul718e3742002-12-13 20:15:29 +00001374 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001375 if (already < length)
paul718e3742002-12-13 20:15:29 +00001376 {
ajs57a14772005-04-10 15:01:56 +00001377 ssize_t nbyte;
1378 if (((nbyte = stream_read_try (client->ibuf, sock,
1379 length-already)) == 0) ||
1380 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001381 {
1382 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001383 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001384 zebra_client_close (client);
1385 return -1;
1386 }
ajs57a14772005-04-10 15:01:56 +00001387 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001388 {
1389 /* Try again later. */
1390 zebra_event (ZEBRA_READ, sock, client);
1391 return 0;
1392 }
paul718e3742002-12-13 20:15:29 +00001393 }
1394
ajs719e9742005-02-28 20:52:15 +00001395 length -= ZEBRA_HEADER_SIZE;
1396
paul718e3742002-12-13 20:15:29 +00001397 /* Debug packet information. */
1398 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001399 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001400
1401 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001402 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001403 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001404
1405 switch (command)
1406 {
hasso18a6dce2004-10-03 18:18:34 +00001407 case ZEBRA_ROUTER_ID_ADD:
1408 zread_router_id_add (client, length);
1409 break;
1410 case ZEBRA_ROUTER_ID_DELETE:
1411 zread_router_id_delete (client, length);
1412 break;
paul718e3742002-12-13 20:15:29 +00001413 case ZEBRA_INTERFACE_ADD:
1414 zread_interface_add (client, length);
1415 break;
1416 case ZEBRA_INTERFACE_DELETE:
1417 zread_interface_delete (client, length);
1418 break;
1419 case ZEBRA_IPV4_ROUTE_ADD:
1420 zread_ipv4_add (client, length);
1421 break;
1422 case ZEBRA_IPV4_ROUTE_DELETE:
1423 zread_ipv4_delete (client, length);
1424 break;
1425#ifdef HAVE_IPV6
1426 case ZEBRA_IPV6_ROUTE_ADD:
1427 zread_ipv6_add (client, length);
1428 break;
1429 case ZEBRA_IPV6_ROUTE_DELETE:
1430 zread_ipv6_delete (client, length);
1431 break;
1432#endif /* HAVE_IPV6 */
1433 case ZEBRA_REDISTRIBUTE_ADD:
1434 zebra_redistribute_add (command, client, length);
1435 break;
1436 case ZEBRA_REDISTRIBUTE_DELETE:
1437 zebra_redistribute_delete (command, client, length);
1438 break;
1439 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1440 zebra_redistribute_default_add (command, client, length);
1441 break;
1442 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1443 zebra_redistribute_default_delete (command, client, length);
1444 break;
1445 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1446 zread_ipv4_nexthop_lookup (client, length);
1447 break;
Everton Marquesbe4fb432014-07-01 15:15:52 -03001448 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
1449 zread_ipv4_nexthop_lookup_mrib (client, length);
1450 break;
paul718e3742002-12-13 20:15:29 +00001451#ifdef HAVE_IPV6
1452 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1453 zread_ipv6_nexthop_lookup (client, length);
1454 break;
1455#endif /* HAVE_IPV6 */
1456 case ZEBRA_IPV4_IMPORT_LOOKUP:
1457 zread_ipv4_import_lookup (client, length);
1458 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001459 case ZEBRA_HELLO:
1460 zread_hello (client);
1461 break;
paul718e3742002-12-13 20:15:29 +00001462 default:
1463 zlog_info ("Zebra received unknown command %d", command);
1464 break;
1465 }
1466
ajs719e9742005-02-28 20:52:15 +00001467 if (client->t_suicide)
1468 {
1469 /* No need to wait for thread callback, just kill immediately. */
1470 zebra_client_close(client);
1471 return -1;
1472 }
1473
paul718e3742002-12-13 20:15:29 +00001474 stream_reset (client->ibuf);
1475 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001476 return 0;
1477}
1478
paul718e3742002-12-13 20:15:29 +00001479
1480/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001481static int
paul718e3742002-12-13 20:15:29 +00001482zebra_accept (struct thread *thread)
1483{
1484 int accept_sock;
1485 int client_sock;
1486 struct sockaddr_in client;
1487 socklen_t len;
1488
1489 accept_sock = THREAD_FD (thread);
1490
ajs719e9742005-02-28 20:52:15 +00001491 /* Reregister myself. */
1492 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1493
paul718e3742002-12-13 20:15:29 +00001494 len = sizeof (struct sockaddr_in);
1495 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1496
1497 if (client_sock < 0)
1498 {
ajs6099b3b2004-11-20 02:06:59 +00001499 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001500 return -1;
1501 }
1502
paulccf35572003-03-01 11:42:20 +00001503 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001504 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001505
paul718e3742002-12-13 20:15:29 +00001506 /* Create new zebra client. */
1507 zebra_client_create (client_sock);
1508
paul718e3742002-12-13 20:15:29 +00001509 return 0;
1510}
1511
paulb9df2d22004-05-09 09:09:59 +00001512#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001513/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001514static void
paul718e3742002-12-13 20:15:29 +00001515zebra_serv ()
1516{
1517 int ret;
1518 int accept_sock;
1519 struct sockaddr_in addr;
1520
1521 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1522
1523 if (accept_sock < 0)
1524 {
paul3d1dc852005-04-05 00:45:23 +00001525 zlog_warn ("Can't create zserv stream socket: %s",
1526 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001527 zlog_warn ("zebra can't provice full functionality due to above error");
1528 return;
1529 }
1530
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001531 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001532 memset (&addr, 0, sizeof (struct sockaddr_in));
1533 addr.sin_family = AF_INET;
1534 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001535#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001536 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001537#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001538 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1539
1540 sockopt_reuseaddr (accept_sock);
1541 sockopt_reuseport (accept_sock);
1542
pauledd7c242003-06-04 13:59:38 +00001543 if ( zserv_privs.change(ZPRIVS_RAISE) )
1544 zlog (NULL, LOG_ERR, "Can't raise privileges");
1545
paul718e3742002-12-13 20:15:29 +00001546 ret = bind (accept_sock, (struct sockaddr *)&addr,
1547 sizeof (struct sockaddr_in));
1548 if (ret < 0)
1549 {
paul3d1dc852005-04-05 00:45:23 +00001550 zlog_warn ("Can't bind to stream socket: %s",
1551 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001552 zlog_warn ("zebra can't provice full functionality due to above error");
1553 close (accept_sock); /* Avoid sd leak. */
1554 return;
1555 }
pauledd7c242003-06-04 13:59:38 +00001556
1557 if ( zserv_privs.change(ZPRIVS_LOWER) )
1558 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001559
1560 ret = listen (accept_sock, 1);
1561 if (ret < 0)
1562 {
paul3d1dc852005-04-05 00:45:23 +00001563 zlog_warn ("Can't listen to stream socket: %s",
1564 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001565 zlog_warn ("zebra can't provice full functionality due to above error");
1566 close (accept_sock); /* Avoid sd leak. */
1567 return;
1568 }
1569
1570 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1571}
paulb9df2d22004-05-09 09:09:59 +00001572#endif /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001573
1574/* For sockaddr_un. */
1575#include <sys/un.h>
1576
1577/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001578static void
hassofce954f2004-10-07 20:29:24 +00001579zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001580{
1581 int ret;
1582 int sock, len;
1583 struct sockaddr_un serv;
1584 mode_t old_mask;
1585
1586 /* First of all, unlink existing socket */
1587 unlink (path);
1588
1589 /* Set umask */
1590 old_mask = umask (0077);
1591
1592 /* Make UNIX domain socket. */
1593 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1594 if (sock < 0)
1595 {
paul3d1dc852005-04-05 00:45:23 +00001596 zlog_warn ("Can't create zserv unix socket: %s",
1597 safe_strerror (errno));
1598 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001599 return;
1600 }
1601
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001602 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1603
paul718e3742002-12-13 20:15:29 +00001604 /* Make server socket. */
1605 memset (&serv, 0, sizeof (struct sockaddr_un));
1606 serv.sun_family = AF_UNIX;
1607 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001608#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001609 len = serv.sun_len = SUN_LEN(&serv);
1610#else
1611 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001612#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001613
1614 ret = bind (sock, (struct sockaddr *) &serv, len);
1615 if (ret < 0)
1616 {
paul3d1dc852005-04-05 00:45:23 +00001617 zlog_warn ("Can't bind to unix socket %s: %s",
1618 path, safe_strerror (errno));
1619 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001620 close (sock);
1621 return;
1622 }
1623
1624 ret = listen (sock, 5);
1625 if (ret < 0)
1626 {
paul3d1dc852005-04-05 00:45:23 +00001627 zlog_warn ("Can't listen to unix socket %s: %s",
1628 path, safe_strerror (errno));
1629 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001630 close (sock);
1631 return;
1632 }
1633
1634 umask (old_mask);
1635
1636 zebra_event (ZEBRA_SERV, sock, NULL);
1637}
David Lamparter6b0655a2014-06-04 06:53:35 +02001638
paul718e3742002-12-13 20:15:29 +00001639
paulb9df2d22004-05-09 09:09:59 +00001640static void
paul718e3742002-12-13 20:15:29 +00001641zebra_event (enum event event, int sock, struct zserv *client)
1642{
1643 switch (event)
1644 {
1645 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001646 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001647 break;
1648 case ZEBRA_READ:
1649 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001650 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001651 break;
1652 case ZEBRA_WRITE:
1653 /**/
1654 break;
1655 }
1656}
David Lamparter6b0655a2014-06-04 06:53:35 +02001657
paul718e3742002-12-13 20:15:29 +00001658/* Display default rtm_table for all clients. */
1659DEFUN (show_table,
1660 show_table_cmd,
1661 "show table",
1662 SHOW_STR
1663 "default routing table to use for all clients\n")
1664{
paulb21b19c2003-06-15 01:28:29 +00001665 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001666 VTY_NEWLINE);
1667 return CMD_SUCCESS;
1668}
1669
1670DEFUN (config_table,
1671 config_table_cmd,
1672 "table TABLENO",
1673 "Configure target kernel routing table\n"
1674 "TABLE integer\n")
1675{
paulb21b19c2003-06-15 01:28:29 +00001676 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001677 return CMD_SUCCESS;
1678}
1679
hasso647e4f12003-05-25 11:43:52 +00001680DEFUN (ip_forwarding,
1681 ip_forwarding_cmd,
1682 "ip forwarding",
1683 IP_STR
1684 "Turn on IP forwarding")
1685{
1686 int ret;
1687
1688 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001689 if (ret == 0)
1690 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001691
hasso647e4f12003-05-25 11:43:52 +00001692 if (ret == 0)
1693 {
1694 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1695 return CMD_WARNING;
1696 }
1697
1698 return CMD_SUCCESS;
1699}
1700
paul718e3742002-12-13 20:15:29 +00001701DEFUN (no_ip_forwarding,
1702 no_ip_forwarding_cmd,
1703 "no ip forwarding",
1704 NO_STR
1705 IP_STR
1706 "Turn off IP forwarding")
1707{
1708 int ret;
1709
1710 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001711 if (ret != 0)
1712 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001713
paul718e3742002-12-13 20:15:29 +00001714 if (ret != 0)
1715 {
1716 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1717 return CMD_WARNING;
1718 }
1719
1720 return CMD_SUCCESS;
1721}
1722
1723/* This command is for debugging purpose. */
1724DEFUN (show_zebra_client,
1725 show_zebra_client_cmd,
1726 "show zebra client",
1727 SHOW_STR
1728 "Zebra information"
1729 "Client information")
1730{
hasso52dc7ee2004-09-23 19:18:23 +00001731 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001732 struct zserv *client;
1733
paul1eb8ef22005-04-07 07:30:20 +00001734 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1735 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1736
paul718e3742002-12-13 20:15:29 +00001737 return CMD_SUCCESS;
1738}
1739
1740/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001741static int
paul718e3742002-12-13 20:15:29 +00001742config_write_table (struct vty *vty)
1743{
paulb21b19c2003-06-15 01:28:29 +00001744 if (zebrad.rtm_table_default)
1745 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001746 VTY_NEWLINE);
1747 return 0;
1748}
1749
1750/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001751static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001752{
1753 TABLE_NODE,
1754 "", /* This node has no interface. */
1755 1
1756};
David Lamparter6b0655a2014-06-04 06:53:35 +02001757
paul718e3742002-12-13 20:15:29 +00001758/* Only display ip forwarding is enabled or not. */
1759DEFUN (show_ip_forwarding,
1760 show_ip_forwarding_cmd,
1761 "show ip forwarding",
1762 SHOW_STR
1763 IP_STR
1764 "IP forwarding status\n")
1765{
1766 int ret;
1767
1768 ret = ipforward ();
1769
1770 if (ret == 0)
1771 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1772 else
1773 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1774 return CMD_SUCCESS;
1775}
1776
1777#ifdef HAVE_IPV6
1778/* Only display ipv6 forwarding is enabled or not. */
1779DEFUN (show_ipv6_forwarding,
1780 show_ipv6_forwarding_cmd,
1781 "show ipv6 forwarding",
1782 SHOW_STR
1783 "IPv6 information\n"
1784 "Forwarding status\n")
1785{
1786 int ret;
1787
1788 ret = ipforward_ipv6 ();
1789
1790 switch (ret)
1791 {
1792 case -1:
1793 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1794 break;
1795 case 0:
1796 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1797 break;
1798 case 1:
1799 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1800 break;
1801 default:
1802 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1803 break;
1804 }
1805 return CMD_SUCCESS;
1806}
1807
hasso55906722004-02-11 22:42:16 +00001808DEFUN (ipv6_forwarding,
1809 ipv6_forwarding_cmd,
1810 "ipv6 forwarding",
1811 IPV6_STR
1812 "Turn on IPv6 forwarding")
1813{
1814 int ret;
1815
hasso41d3fc92004-04-06 11:59:00 +00001816 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001817 if (ret == 0)
1818 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001819
hasso41d3fc92004-04-06 11:59:00 +00001820 if (ret == 0)
1821 {
hasso55906722004-02-11 22:42:16 +00001822 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1823 return CMD_WARNING;
1824 }
1825
1826 return CMD_SUCCESS;
1827}
1828
paul718e3742002-12-13 20:15:29 +00001829DEFUN (no_ipv6_forwarding,
1830 no_ipv6_forwarding_cmd,
1831 "no ipv6 forwarding",
1832 NO_STR
hasso55906722004-02-11 22:42:16 +00001833 IPV6_STR
1834 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001835{
1836 int ret;
1837
hasso41d3fc92004-04-06 11:59:00 +00001838 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001839 if (ret != 0)
1840 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001841
paul718e3742002-12-13 20:15:29 +00001842 if (ret != 0)
1843 {
1844 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1845 return CMD_WARNING;
1846 }
1847
1848 return CMD_SUCCESS;
1849}
1850
1851#endif /* HAVE_IPV6 */
1852
1853/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001854static int
paul718e3742002-12-13 20:15:29 +00001855config_write_forwarding (struct vty *vty)
1856{
hasso18a6dce2004-10-03 18:18:34 +00001857 /* FIXME: Find better place for that. */
1858 router_id_write (vty);
1859
paul3e0b3a52004-08-23 18:58:32 +00001860 if (ipforward ())
1861 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001862#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001863 if (ipforward_ipv6 ())
1864 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001865#endif /* HAVE_IPV6 */
1866 vty_out (vty, "!%s", VTY_NEWLINE);
1867 return 0;
1868}
1869
1870/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001871static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001872{
1873 FORWARDING_NODE,
1874 "", /* This node has no interface. */
1875 1
1876};
1877
David Lamparter6b0655a2014-06-04 06:53:35 +02001878
paul718e3742002-12-13 20:15:29 +00001879/* Initialisation of zebra and installation of commands. */
1880void
paula1ac18c2005-06-28 17:17:12 +00001881zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001882{
1883 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001884 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001885
paul718e3742002-12-13 20:15:29 +00001886 /* Install configuration write function. */
1887 install_node (&table_node, config_write_table);
1888 install_node (&forwarding_node, config_write_forwarding);
1889
1890 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1891 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001892 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001893 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1894 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1895
1896#ifdef HAVE_NETLINK
1897 install_element (VIEW_NODE, &show_table_cmd);
1898 install_element (ENABLE_NODE, &show_table_cmd);
1899 install_element (CONFIG_NODE, &config_table_cmd);
1900#endif /* HAVE_NETLINK */
1901
1902#ifdef HAVE_IPV6
1903 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1904 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001905 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001906 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1907#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001908
1909 /* Route-map */
1910 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001911}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001912
1913/* Make zebra server socket, wiping any existing one (see bug #403). */
1914void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001915zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001916{
1917#ifdef HAVE_TCP_ZEBRA
1918 zebra_serv ();
1919#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001920 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001921#endif /* HAVE_TCP_ZEBRA */
1922}