blob: a09563248edfc278cb06a38166c698e3cabadc33 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
ajs634f9ea2005-04-11 15:51:40 +00003 * Copyright (C) 2005 Andrew J. Schorr
paul718e3742002-12-13 20:15:29 +00004 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "stream.h"
ajs634f9ea2005-04-11 15:51:40 +000027#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000028#include "network.h"
29#include "if.h"
30#include "log.h"
31#include "thread.h"
32#include "zclient.h"
33#include "memory.h"
34#include "table.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020035
paul718e3742002-12-13 20:15:29 +000036/* Zebra client events. */
37enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
38
39/* Prototype for event manager. */
40static void zclient_event (enum event, struct zclient *);
41
ajs634f9ea2005-04-11 15:51:40 +000042extern struct thread_master *master;
43
David Lamparterfd8f6eb2015-03-03 08:57:02 +010044const char *zclient_serv_path = NULL;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040045
paul718e3742002-12-13 20:15:29 +000046/* This file local debug flag. */
47int zclient_debug = 0;
David Lamparter6b0655a2014-06-04 06:53:35 +020048
paul718e3742002-12-13 20:15:29 +000049/* Allocate zclient structure. */
50struct zclient *
51zclient_new ()
52{
53 struct zclient *zclient;
Stephen Hemminger393deb92008-08-18 14:13:29 -070054 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
paul718e3742002-12-13 20:15:29 +000055
56 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
57 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs634f9ea2005-04-11 15:51:40 +000058 zclient->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +000059
60 return zclient;
61}
62
Chris Caputo228da422009-07-18 05:44:03 +000063/* This function is only called when exiting, because
ajs634f9ea2005-04-11 15:51:40 +000064 many parts of the code do not check for I/O errors, so they could
65 reference an invalid pointer if the structure was ever freed.
ajs634f9ea2005-04-11 15:51:40 +000066
Chris Caputo228da422009-07-18 05:44:03 +000067 Free zclient structure. */
paul718e3742002-12-13 20:15:29 +000068void
69zclient_free (struct zclient *zclient)
70{
ajs634f9ea2005-04-11 15:51:40 +000071 if (zclient->ibuf)
72 stream_free(zclient->ibuf);
73 if (zclient->obuf)
74 stream_free(zclient->obuf);
75 if (zclient->wb)
76 buffer_free(zclient->wb);
77
paul718e3742002-12-13 20:15:29 +000078 XFREE (MTYPE_ZCLIENT, zclient);
79}
80
81/* Initialize zebra client. Argument redist_default is unwanted
82 redistribute route type. */
83void
84zclient_init (struct zclient *zclient, int redist_default)
85{
86 int i;
87
88 /* Enable zebra client connection by default. */
89 zclient->enable = 1;
90
91 /* Set -1 to the default socket value. */
92 zclient->sock = -1;
93
94 /* Clear redistribution flags. */
95 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
Feng Luc99f3482014-10-16 09:52:36 +080096 zclient->redist[i] = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +000097
98 /* Set unwanted redistribute route. bgpd does not need BGP route
99 redistribution. */
100 zclient->redist_default = redist_default;
paul718e3742002-12-13 20:15:29 +0000101
102 /* Set default-information redistribute to zero. */
Feng Luc99f3482014-10-16 09:52:36 +0800103 zclient->default_information = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +0000104
105 /* Schedule first zclient connection. */
106 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000107 zlog_debug ("zclient start scheduled");
paul718e3742002-12-13 20:15:29 +0000108
109 zclient_event (ZCLIENT_SCHEDULE, zclient);
110}
111
112/* Stop zebra client services. */
113void
114zclient_stop (struct zclient *zclient)
115{
116 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000117 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000118
119 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000120 THREAD_OFF(zclient->t_read);
121 THREAD_OFF(zclient->t_connect);
122 THREAD_OFF(zclient->t_write);
123
124 /* Reset streams. */
125 stream_reset(zclient->ibuf);
126 stream_reset(zclient->obuf);
127
128 /* Empty the write buffer. */
129 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000130
131 /* Close socket. */
132 if (zclient->sock >= 0)
133 {
134 close (zclient->sock);
135 zclient->sock = -1;
136 }
137 zclient->fail = 0;
138}
139
140void
141zclient_reset (struct zclient *zclient)
142{
143 zclient_stop (zclient);
144 zclient_init (zclient, zclient->redist_default);
145}
146
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400147#ifdef HAVE_TCP_ZEBRA
148
paul718e3742002-12-13 20:15:29 +0000149/* Make socket to zebra daemon. Return zebra socket. */
Everton Marques1a9352a2014-09-23 14:33:34 -0300150static int
ajs634f9ea2005-04-11 15:51:40 +0000151zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000152{
153 int sock;
154 int ret;
155 struct sockaddr_in serv;
156
157 /* We should think about IPv6 connection. */
158 sock = socket (AF_INET, SOCK_STREAM, 0);
159 if (sock < 0)
160 return -1;
161
162 /* Make server socket. */
163 memset (&serv, 0, sizeof (struct sockaddr_in));
164 serv.sin_family = AF_INET;
165 serv.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000166#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000167 serv.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000168#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000169 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
170
171 /* Connect to zebra. */
172 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
173 if (ret < 0)
174 {
175 close (sock);
176 return -1;
177 }
178 return sock;
179}
180
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400181#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400182
paul718e3742002-12-13 20:15:29 +0000183/* For sockaddr_un. */
184#include <sys/un.h>
185
Everton Marques1a9352a2014-09-23 14:33:34 -0300186static int
hasso8c328f12004-10-05 21:01:23 +0000187zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000188{
189 int ret;
190 int sock, len;
191 struct sockaddr_un addr;
192
193 sock = socket (AF_UNIX, SOCK_STREAM, 0);
194 if (sock < 0)
195 return -1;
196
197 /* Make server socket. */
198 memset (&addr, 0, sizeof (struct sockaddr_un));
199 addr.sun_family = AF_UNIX;
200 strncpy (addr.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000201#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +0000202 len = addr.sun_len = SUN_LEN(&addr);
203#else
204 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000205#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +0000206
207 ret = connect (sock, (struct sockaddr *) &addr, len);
208 if (ret < 0)
209 {
210 close (sock);
211 return -1;
212 }
213 return sock;
214}
215
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400216#endif /* HAVE_TCP_ZEBRA */
217
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400218/**
219 * Connect to zebra daemon.
220 * @param zclient a pointer to zclient structure
221 * @return socket fd just to make sure that connection established
222 * @see zclient_init
223 * @see zclient_new
224 */
225int
226zclient_socket_connect (struct zclient *zclient)
227{
228#ifdef HAVE_TCP_ZEBRA
229 zclient->sock = zclient_socket ();
230#else
Everton Marques1f298942014-08-21 15:47:28 -0300231 zclient->sock = zclient_socket_un (zclient_serv_path_get());
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400232#endif
233 return zclient->sock;
234}
235
ajs634f9ea2005-04-11 15:51:40 +0000236static int
237zclient_failed(struct zclient *zclient)
238{
239 zclient->fail++;
240 zclient_stop(zclient);
241 zclient_event(ZCLIENT_CONNECT, zclient);
242 return -1;
243}
244
245static int
246zclient_flush_data(struct thread *thread)
247{
248 struct zclient *zclient = THREAD_ARG(thread);
249
250 zclient->t_write = NULL;
251 if (zclient->sock < 0)
252 return -1;
253 switch (buffer_flush_available(zclient->wb, zclient->sock))
254 {
255 case BUFFER_ERROR:
256 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
257 __func__, zclient->sock);
258 return zclient_failed(zclient);
259 break;
260 case BUFFER_PENDING:
261 zclient->t_write = thread_add_write(master, zclient_flush_data,
262 zclient, zclient->sock);
263 break;
264 case BUFFER_EMPTY:
265 break;
266 }
267 return 0;
268}
269
paul718e3742002-12-13 20:15:29 +0000270int
ajs634f9ea2005-04-11 15:51:40 +0000271zclient_send_message(struct zclient *zclient)
272{
273 if (zclient->sock < 0)
274 return -1;
275 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
276 stream_get_endp(zclient->obuf)))
277 {
278 case BUFFER_ERROR:
279 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
280 __func__, zclient->sock);
281 return zclient_failed(zclient);
282 break;
283 case BUFFER_EMPTY:
284 THREAD_OFF(zclient->t_write);
285 break;
286 case BUFFER_PENDING:
287 THREAD_WRITE_ON(master, zclient->t_write,
288 zclient_flush_data, zclient, zclient->sock);
289 break;
290 }
291 return 0;
292}
293
pauld2110862006-01-17 17:43:18 +0000294void
Feng Luc99f3482014-10-16 09:52:36 +0800295zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000296{
297 /* length placeholder, caller can update */
298 stream_putw (s, ZEBRA_HEADER_SIZE);
299 stream_putc (s, ZEBRA_HEADER_MARKER);
300 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800301 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000302 stream_putw (s, command);
303}
304
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200305int
306zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
307 u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
308{
309 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
310 return -1;
311
312 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
313 *marker = stream_getc (s);
314 *version = stream_getc (s);
315 *vrf_id = stream_getw (s);
316 *cmd = stream_getw (s);
317
318 if (*size && stream_read (s, sock, *size) != *size)
319 return -1;
320
321 return 0;
322}
323
ajs634f9ea2005-04-11 15:51:40 +0000324/* Send simple Zebra message. */
325static int
Feng Luc99f3482014-10-16 09:52:36 +0800326zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000327{
328 struct stream *s;
329
330 /* Get zclient output buffer. */
331 s = zclient->obuf;
332 stream_reset (s);
333
334 /* Send very simple command only Zebra message. */
Feng Luc99f3482014-10-16 09:52:36 +0800335 zclient_create_header (s, command, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000336
ajs634f9ea2005-04-11 15:51:40 +0000337 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000338}
339
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400340static int
341zebra_hello_send (struct zclient *zclient)
342{
343 struct stream *s;
344
345 if (zclient->redist_default)
346 {
347 s = zclient->obuf;
348 stream_reset (s);
349
Feng Luc99f3482014-10-16 09:52:36 +0800350 /* The VRF ID in the HELLO message is always 0. */
351 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400352 stream_putc (s, zclient->redist_default);
353 stream_putw_at (s, 0, stream_get_endp (s));
354 return zclient_send_message(zclient);
355 }
356
357 return 0;
358}
359
Feng Luc99f3482014-10-16 09:52:36 +0800360/* Send requests to zebra daemon for the information in a VRF. */
361void
362zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
363{
364 int i;
365
366 /* zclient is disabled. */
367 if (! zclient->enable)
368 return;
369
370 /* If not connected to the zebra yet. */
371 if (zclient->sock < 0)
372 return;
373
374 if (zclient_debug)
375 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
376
377 /* We need router-id information. */
378 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
379
380 /* We need interface information. */
381 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
382
383 /* Set unwanted redistribute route. */
384 vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
385
386 /* Flush all redistribute request. */
387 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
388 if (i != zclient->redist_default &&
389 vrf_bitmap_check (zclient->redist[i], vrf_id))
390 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
391
392 /* If default information is needed. */
393 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
394 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
395}
396
paul718e3742002-12-13 20:15:29 +0000397/* Make connection to zebra daemon. */
398int
399zclient_start (struct zclient *zclient)
400{
paul718e3742002-12-13 20:15:29 +0000401 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000402 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000403
404 /* zclient is disabled. */
405 if (! zclient->enable)
406 return 0;
407
408 /* If already connected to the zebra. */
409 if (zclient->sock >= 0)
410 return 0;
411
412 /* Check connect thread. */
413 if (zclient->t_connect)
414 return 0;
415
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400416 if (zclient_socket_connect(zclient) < 0)
paul718e3742002-12-13 20:15:29 +0000417 {
418 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000419 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000420 zclient->fail++;
421 zclient_event (ZCLIENT_CONNECT, zclient);
422 return -1;
423 }
424
ajs634f9ea2005-04-11 15:51:40 +0000425 if (set_nonblocking(zclient->sock) < 0)
426 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
427
paul718e3742002-12-13 20:15:29 +0000428 /* Clear fail count. */
429 zclient->fail = 0;
430 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000431 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000432
433 /* Create read thread. */
434 zclient_event (ZCLIENT_READ, zclient);
435
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400436 zebra_hello_send (zclient);
437
Feng Luc99f3482014-10-16 09:52:36 +0800438 /* Inform the successful connection. */
439 if (zclient->zebra_connected)
440 (*zclient->zebra_connected) (zclient);
paul718e3742002-12-13 20:15:29 +0000441
442 return 0;
443}
444
445/* This function is a wrapper function for calling zclient_start from
446 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000447static int
paul718e3742002-12-13 20:15:29 +0000448zclient_connect (struct thread *t)
449{
450 struct zclient *zclient;
451
452 zclient = THREAD_ARG (t);
453 zclient->t_connect = NULL;
454
455 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000456 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000457
458 return zclient_start (zclient);
459}
David Lamparter6b0655a2014-06-04 06:53:35 +0200460
paul0a589352004-05-08 11:48:26 +0000461 /*
462 * "xdr_encode"-like interface that allows daemon (client) to send
463 * a message to zebra server for a route that needs to be
464 * added/deleted to the kernel. Info about the route is specified
465 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
466 * the info down the zclient socket using the stream_* functions.
467 *
468 * The corresponding read ("xdr_decode") function on the server
469 * side is zread_ipv4_add()/zread_ipv4_delete().
470 *
471 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
472 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
473 * | Length (2) | Command | Route Type |
474 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
475 * | ZEBRA Flags | Message Flags | Prefix length |
476 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
477 * | Destination IPv4 Prefix for route |
478 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 * | Nexthop count |
480 * +-+-+-+-+-+-+-+-+
481 *
482 *
483 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
484 * described, as per the Nexthop count. Each nexthop described as:
485 *
486 * +-+-+-+-+-+-+-+-+
487 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 * | IPv4 Nexthop address or Interface Index number |
490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 *
492 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
493 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
494 * nexthop information is provided, and the message describes a prefix
495 * to blackhole or reject route.
496 *
497 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
498 * byte value.
499 *
500 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
501 * byte value.
502 *
503 * XXX: No attention paid to alignment.
504 */
paul718e3742002-12-13 20:15:29 +0000505int
paul0a589352004-05-08 11:48:26 +0000506zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
507 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000508{
509 int i;
510 int psize;
511 struct stream *s;
512
513 /* Reset stream. */
514 s = zclient->obuf;
515 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800516
517 zclient_create_header (s, cmd, api->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000518
519 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000520 stream_putc (s, api->type);
521 stream_putc (s, api->flags);
522 stream_putc (s, api->message);
G.Balaji5a616c02011-11-26 21:58:42 +0400523 stream_putw (s, api->safi);
paul0a589352004-05-08 11:48:26 +0000524
paul718e3742002-12-13 20:15:29 +0000525 /* Put prefix information. */
526 psize = PSIZE (p->prefixlen);
527 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000528 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000529
530 /* Nexthop, ifindex, distance and metric information. */
531 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
532 {
paul595db7f2003-05-25 21:35:06 +0000533 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
534 {
535 stream_putc (s, 1);
536 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000537 /* XXX assert(api->nexthop_num == 0); */
538 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000539 }
540 else
541 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000542
543 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000544 {
545 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
546 stream_put_in_addr (s, api->nexthop[i]);
547 }
paul718e3742002-12-13 20:15:29 +0000548 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000549 {
550 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
551 stream_putl (s, api->ifindex[i]);
552 }
paul718e3742002-12-13 20:15:29 +0000553 }
554
555 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
556 stream_putc (s, api->distance);
557 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
558 stream_putl (s, api->metric);
559
560 /* Put length at the first point of the stream. */
561 stream_putw_at (s, 0, stream_get_endp (s));
562
ajs634f9ea2005-04-11 15:51:40 +0000563 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000564}
565
566#ifdef HAVE_IPV6
567int
paul0a589352004-05-08 11:48:26 +0000568zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000569 struct zapi_ipv6 *api)
570{
571 int i;
572 int psize;
573 struct stream *s;
574
575 /* Reset stream. */
576 s = zclient->obuf;
577 stream_reset (s);
578
Feng Luc99f3482014-10-16 09:52:36 +0800579 zclient_create_header (s, cmd, api->vrf_id);
paul718e3742002-12-13 20:15:29 +0000580
paulc1b98002006-01-16 01:54:02 +0000581 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000582 stream_putc (s, api->type);
583 stream_putc (s, api->flags);
584 stream_putc (s, api->message);
G.Balajic7ec1792011-11-26 22:04:05 +0400585 stream_putw (s, api->safi);
paul718e3742002-12-13 20:15:29 +0000586
587 /* Put prefix information. */
588 psize = PSIZE (p->prefixlen);
589 stream_putc (s, p->prefixlen);
590 stream_write (s, (u_char *)&p->prefix, psize);
591
592 /* Nexthop, ifindex, distance and metric information. */
593 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
594 {
595 stream_putc (s, api->nexthop_num + api->ifindex_num);
596
597 for (i = 0; i < api->nexthop_num; i++)
598 {
599 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
600 stream_write (s, (u_char *)api->nexthop[i], 16);
601 }
602 for (i = 0; i < api->ifindex_num; i++)
603 {
604 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
605 stream_putl (s, api->ifindex[i]);
606 }
607 }
608
609 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
610 stream_putc (s, api->distance);
611 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
612 stream_putl (s, api->metric);
613
614 /* Put length at the first point of the stream. */
615 stream_putw_at (s, 0, stream_get_endp (s));
616
ajs634f9ea2005-04-11 15:51:40 +0000617 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000618}
paul718e3742002-12-13 20:15:29 +0000619#endif /* HAVE_IPV6 */
620
paul0a589352004-05-08 11:48:26 +0000621/*
622 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
623 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
624 * then set/unset redist[type] in the client handle (a struct zserv) for the
625 * sending client
626 */
paul718e3742002-12-13 20:15:29 +0000627int
Feng Luc99f3482014-10-16 09:52:36 +0800628zebra_redistribute_send (int command, struct zclient *zclient, int type,
629 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000630{
paul718e3742002-12-13 20:15:29 +0000631 struct stream *s;
632
ajs634f9ea2005-04-11 15:51:40 +0000633 s = zclient->obuf;
634 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000635
Feng Luc99f3482014-10-16 09:52:36 +0800636 zclient_create_header (s, command, vrf_id);
paul718e3742002-12-13 20:15:29 +0000637 stream_putc (s, type);
paulc1b98002006-01-16 01:54:02 +0000638
639 stream_putw_at (s, 0, stream_get_endp (s));
640
ajs634f9ea2005-04-11 15:51:40 +0000641 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000642}
643
hasso18a6dce2004-10-03 18:18:34 +0000644/* Router-id update from zebra daemon. */
645void
646zebra_router_id_update_read (struct stream *s, struct prefix *rid)
647{
648 int plen;
649
650 /* Fetch interface address. */
651 rid->family = stream_getc (s);
652
653 plen = prefix_blen (rid);
654 stream_get (&rid->u.prefix, s, plen);
655 rid->prefixlen = stream_getc (s);
656}
657
paul718e3742002-12-13 20:15:29 +0000658/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000659/*
660 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
661 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
662 * 0 1 2 3
663 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
664 * +-+-+-+-+-+-+-+-+
665 * | type |
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * | ifname |
668 * | |
669 * | |
670 * | |
671 * | |
672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 * | ifindex |
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | if_flags |
paulc77d4542006-01-11 01:59:04 +0000676 * | |
paul0a589352004-05-08 11:48:26 +0000677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
678 * | metric |
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * | ifmtu |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 * | ifmtu6 |
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * | bandwidth |
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | sockaddr_dl |
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 */
689
paul718e3742002-12-13 20:15:29 +0000690struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800691zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000692{
693 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000694 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000695
696 /* Read interface name. */
697 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
698
ajsa3491982005-04-02 22:50:38 +0000699 /* Lookup/create interface by name. */
Feng Luc99f3482014-10-16 09:52:36 +0800700 ifp = if_get_by_name_len_vrf (ifname_tmp,
701 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
702 vrf_id);
paul718e3742002-12-13 20:15:29 +0000703
Josh Bailey51d4ef82012-03-21 17:13:39 -0700704 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000705
paul718e3742002-12-13 20:15:29 +0000706 return ifp;
707}
708
paul0a589352004-05-08 11:48:26 +0000709/*
710 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
711 * from zebra server. The format of this message is the same as
712 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
713 * comments for zebra_interface_add_read), except that no sockaddr_dl
714 * is sent at the tail of the message.
715 */
paul718e3742002-12-13 20:15:29 +0000716struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800717zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000718{
719 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000720 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000721
722 /* Read interface name. */
723 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
724
725 /* Lookup this by interface index. */
Feng Luc99f3482014-10-16 09:52:36 +0800726 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
727 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
728 vrf_id);
paul718e3742002-12-13 20:15:29 +0000729
730 /* If such interface does not exist, indicate an error */
731 if (! ifp)
732 return NULL;
733
Josh Bailey51d4ef82012-03-21 17:13:39 -0700734 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000735
736 return ifp;
737}
738
paul0a589352004-05-08 11:48:26 +0000739/*
740 * format of message for address additon is:
741 * 0
742 * 0 1 2 3 4 5 6 7
743 * +-+-+-+-+-+-+-+-+
744 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
745 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
746 * | |
747 * + +
748 * | ifindex |
749 * + +
750 * | |
751 * + +
752 * | |
753 * +-+-+-+-+-+-+-+-+
754 * | ifc_flags | flags for connected address
755 * +-+-+-+-+-+-+-+-+
756 * | addr_family |
757 * +-+-+-+-+-+-+-+-+
758 * | addr... |
759 * : :
760 * | |
761 * +-+-+-+-+-+-+-+-+
762 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
763 * +-+-+-+-+-+-+-+-+
764 * | daddr.. |
765 * : :
766 * | |
767 * +-+-+-+-+-+-+-+-+
768 *
769 */
770
hasso18a6dce2004-10-03 18:18:34 +0000771void
772zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
773{
774 /* Read interface's index. */
775 ifp->ifindex = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000776 ifp->status = stream_getc (s);
hasso18a6dce2004-10-03 18:18:34 +0000777
778 /* Read interface's value. */
paulc77d4542006-01-11 01:59:04 +0000779 ifp->flags = stream_getq (s);
hasso18a6dce2004-10-03 18:18:34 +0000780 ifp->metric = stream_getl (s);
781 ifp->mtu = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000782 ifp->mtu6 = stream_getl (s);
hasso18a6dce2004-10-03 18:18:34 +0000783 ifp->bandwidth = stream_getl (s);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700784#ifdef HAVE_STRUCT_SOCKADDR_DL
David Lamparterca3ccd82012-09-26 14:52:39 +0200785 stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
Josh Bailey51d4ef82012-03-21 17:13:39 -0700786#else
787 ifp->hw_addr_len = stream_getl (s);
788 if (ifp->hw_addr_len)
789 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
790#endif /* HAVE_STRUCT_SOCKADDR_DL */
hasso18a6dce2004-10-03 18:18:34 +0000791}
792
hasso3fb9cd62004-10-19 19:44:43 +0000793static int
794memconstant(const void *s, int c, size_t n)
795{
796 const u_char *p = s;
797
798 while (n-- > 0)
799 if (*p++ != c)
800 return 0;
801 return 1;
802}
803
paul718e3742002-12-13 20:15:29 +0000804struct connected *
Feng Luc99f3482014-10-16 09:52:36 +0800805zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000806{
807 unsigned int ifindex;
808 struct interface *ifp;
809 struct connected *ifc;
paul0a589352004-05-08 11:48:26 +0000810 struct prefix p, d;
paul718e3742002-12-13 20:15:29 +0000811 int family;
812 int plen;
paul0a589352004-05-08 11:48:26 +0000813 u_char ifc_flags;
814
815 memset (&p, 0, sizeof(p));
816 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000817
818 /* Get interface index. */
819 ifindex = stream_getl (s);
820
821 /* Lookup index. */
Feng Luc99f3482014-10-16 09:52:36 +0800822 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
paul718e3742002-12-13 20:15:29 +0000823 if (ifp == NULL)
824 {
paul0a589352004-05-08 11:48:26 +0000825 zlog_warn ("zebra_interface_address_read(%s): "
826 "Can't find interface by ifindex: %d ",
827 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
828 ifindex);
paul718e3742002-12-13 20:15:29 +0000829 return NULL;
830 }
831
832 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000833 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000834
835 /* Fetch interface address. */
836 family = p.family = stream_getc (s);
837
paul0a589352004-05-08 11:48:26 +0000838 plen = prefix_blen (&p);
839 stream_get (&p.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000840 p.prefixlen = stream_getc (s);
841
842 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +0000843 stream_get (&d.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000844 d.family = family;
845
paul0a589352004-05-08 11:48:26 +0000846 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
847 {
hasso3fb9cd62004-10-19 19:44:43 +0000848 /* N.B. NULL destination pointers are encoded as all zeroes */
849 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
850 NULL : &d));
paul0a589352004-05-08 11:48:26 +0000851 if (ifc != NULL)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000852 {
853 ifc->flags = ifc_flags;
854 if (ifc->destination)
855 ifc->destination->prefixlen = ifc->address->prefixlen;
David Lamparter90444ca2014-07-01 16:14:05 +0200856 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
857 {
858 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
Timo Teräs41eb9a42015-05-23 11:08:39 +0300859 char buf[PREFIX_STRLEN];
David Lamparter90444ca2014-07-01 16:14:05 +0200860 zlog_warn("warning: interface %s address %s "
861 "with peer flag set, but no peer address!",
Timo Teräs41eb9a42015-05-23 11:08:39 +0300862 ifp->name,
863 prefix2str (ifc->address, buf, sizeof buf));
David Lamparter90444ca2014-07-01 16:14:05 +0200864 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
865 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000866 }
paul0a589352004-05-08 11:48:26 +0000867 }
868 else
869 {
870 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
871 ifc = connected_delete_by_prefix(ifp, &p);
872 }
paul718e3742002-12-13 20:15:29 +0000873
874 return ifc;
875}
paul0a589352004-05-08 11:48:26 +0000876
David Lamparter6b0655a2014-06-04 06:53:35 +0200877
paul718e3742002-12-13 20:15:29 +0000878/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +0000879static int
paul718e3742002-12-13 20:15:29 +0000880zclient_read (struct thread *thread)
881{
ajs634f9ea2005-04-11 15:51:40 +0000882 size_t already;
paulc1b98002006-01-16 01:54:02 +0000883 uint16_t length, command;
884 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +0800885 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +0000886 struct zclient *zclient;
887
888 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +0000889 zclient = THREAD_ARG (thread);
890 zclient->t_read = NULL;
891
ajs634f9ea2005-04-11 15:51:40 +0000892 /* Read zebra header (if we don't have it already). */
893 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000894 {
ajs634f9ea2005-04-11 15:51:40 +0000895 ssize_t nbyte;
896 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
897 ZEBRA_HEADER_SIZE-already)) == 0) ||
898 (nbyte == -1))
899 {
900 if (zclient_debug)
901 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
902 return zclient_failed(zclient);
903 }
904 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
905 {
906 /* Try again later. */
907 zclient_event (ZCLIENT_READ, zclient);
908 return 0;
909 }
910 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000911 }
912
ajs634f9ea2005-04-11 15:51:40 +0000913 /* Reset to read from the beginning of the incoming packet. */
914 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +0000915
paulc1b98002006-01-16 01:54:02 +0000916 /* Fetch header values. */
paul718e3742002-12-13 20:15:29 +0000917 length = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000918 marker = stream_getc (zclient->ibuf);
919 version = stream_getc (zclient->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +0800920 vrf_id = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000921 command = stream_getw (zclient->ibuf);
922
923 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
924 {
925 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
926 __func__, zclient->sock, marker, version);
927 return zclient_failed(zclient);
928 }
929
ajs634f9ea2005-04-11 15:51:40 +0000930 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000931 {
ajs634f9ea2005-04-11 15:51:40 +0000932 zlog_err("%s: socket %d message length %u is less than %d ",
933 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
934 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +0000935 }
ajs634f9ea2005-04-11 15:51:40 +0000936
937 /* Length check. */
938 if (length > STREAM_SIZE(zclient->ibuf))
939 {
940 struct stream *ns;
941 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
942 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
943 ns = stream_new(length);
944 stream_copy(ns, zclient->ibuf);
945 stream_free (zclient->ibuf);
946 zclient->ibuf = ns;
947 }
paul718e3742002-12-13 20:15:29 +0000948
949 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +0000950 if (already < length)
951 {
952 ssize_t nbyte;
953 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
954 length-already)) == 0) ||
955 (nbyte == -1))
956 {
957 if (zclient_debug)
958 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
959 return zclient_failed(zclient);
960 }
961 if (nbyte != (ssize_t)(length-already))
962 {
963 /* Try again later. */
964 zclient_event (ZCLIENT_READ, zclient);
965 return 0;
966 }
967 }
968
969 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000970
paul0a589352004-05-08 11:48:26 +0000971 if (zclient_debug)
Feng Luc99f3482014-10-16 09:52:36 +0800972 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
paul0a589352004-05-08 11:48:26 +0000973
paul718e3742002-12-13 20:15:29 +0000974 switch (command)
975 {
hasso18a6dce2004-10-03 18:18:34 +0000976 case ZEBRA_ROUTER_ID_UPDATE:
977 if (zclient->router_id_update)
Feng Luc99f3482014-10-16 09:52:36 +0800978 (*zclient->router_id_update) (command, zclient, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000979 break;
paul718e3742002-12-13 20:15:29 +0000980 case ZEBRA_INTERFACE_ADD:
981 if (zclient->interface_add)
Feng Luc99f3482014-10-16 09:52:36 +0800982 (*zclient->interface_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000983 break;
984 case ZEBRA_INTERFACE_DELETE:
985 if (zclient->interface_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800986 (*zclient->interface_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000987 break;
988 case ZEBRA_INTERFACE_ADDRESS_ADD:
989 if (zclient->interface_address_add)
Feng Luc99f3482014-10-16 09:52:36 +0800990 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000991 break;
992 case ZEBRA_INTERFACE_ADDRESS_DELETE:
993 if (zclient->interface_address_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800994 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000995 break;
996 case ZEBRA_INTERFACE_UP:
997 if (zclient->interface_up)
Feng Luc99f3482014-10-16 09:52:36 +0800998 (*zclient->interface_up) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000999 break;
1000 case ZEBRA_INTERFACE_DOWN:
1001 if (zclient->interface_down)
Feng Luc99f3482014-10-16 09:52:36 +08001002 (*zclient->interface_down) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001003 break;
1004 case ZEBRA_IPV4_ROUTE_ADD:
1005 if (zclient->ipv4_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001006 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001007 break;
1008 case ZEBRA_IPV4_ROUTE_DELETE:
1009 if (zclient->ipv4_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001010 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001011 break;
1012 case ZEBRA_IPV6_ROUTE_ADD:
1013 if (zclient->ipv6_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001014 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001015 break;
1016 case ZEBRA_IPV6_ROUTE_DELETE:
1017 if (zclient->ipv6_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001018 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001019 break;
1020 default:
1021 break;
1022 }
1023
ajs634f9ea2005-04-11 15:51:40 +00001024 if (zclient->sock < 0)
1025 /* Connection was closed during packet processing. */
1026 return -1;
1027
paul718e3742002-12-13 20:15:29 +00001028 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +00001029 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +00001030 zclient_event (ZCLIENT_READ, zclient);
1031
1032 return 0;
1033}
1034
1035void
Feng Luc99f3482014-10-16 09:52:36 +08001036zclient_redistribute (int command, struct zclient *zclient, int type,
1037 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001038{
paul718e3742002-12-13 20:15:29 +00001039
paul0a589352004-05-08 11:48:26 +00001040 if (command == ZEBRA_REDISTRIBUTE_ADD)
1041 {
Feng Luc99f3482014-10-16 09:52:36 +08001042 if (vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001043 return;
Feng Luc99f3482014-10-16 09:52:36 +08001044 vrf_bitmap_set (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001045 }
1046 else
1047 {
Feng Luc99f3482014-10-16 09:52:36 +08001048 if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001049 return;
Feng Luc99f3482014-10-16 09:52:36 +08001050 vrf_bitmap_unset (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001051 }
paul718e3742002-12-13 20:15:29 +00001052
1053 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001054 zebra_redistribute_send (command, zclient, type, vrf_id);
paul718e3742002-12-13 20:15:29 +00001055}
1056
paul0a589352004-05-08 11:48:26 +00001057
paul718e3742002-12-13 20:15:29 +00001058void
Feng Luc99f3482014-10-16 09:52:36 +08001059zclient_redistribute_default (int command, struct zclient *zclient,
1060 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001061{
paul718e3742002-12-13 20:15:29 +00001062
paul0a589352004-05-08 11:48:26 +00001063 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1064 {
Feng Luc99f3482014-10-16 09:52:36 +08001065 if (vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001066 return;
Feng Luc99f3482014-10-16 09:52:36 +08001067 vrf_bitmap_set (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001068 }
1069 else
1070 {
Feng Luc99f3482014-10-16 09:52:36 +08001071 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001072 return;
Feng Luc99f3482014-10-16 09:52:36 +08001073 vrf_bitmap_unset (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001074 }
paul718e3742002-12-13 20:15:29 +00001075
1076 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001077 zebra_message_send (zclient, command, vrf_id);
paul718e3742002-12-13 20:15:29 +00001078}
1079
paul718e3742002-12-13 20:15:29 +00001080static void
1081zclient_event (enum event event, struct zclient *zclient)
1082{
1083 switch (event)
1084 {
1085 case ZCLIENT_SCHEDULE:
1086 if (! zclient->t_connect)
1087 zclient->t_connect =
1088 thread_add_event (master, zclient_connect, zclient, 0);
1089 break;
1090 case ZCLIENT_CONNECT:
1091 if (zclient->fail >= 10)
1092 return;
1093 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +00001094 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +00001095 zclient->fail < 3 ? 10 : 60);
1096 if (! zclient->t_connect)
1097 zclient->t_connect =
1098 thread_add_timer (master, zclient_connect, zclient,
1099 zclient->fail < 3 ? 10 : 60);
1100 break;
1101 case ZCLIENT_READ:
1102 zclient->t_read =
1103 thread_add_read (master, zclient_read, zclient, zclient->sock);
1104 break;
1105 }
1106}
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001107
David Lamparterfd8f6eb2015-03-03 08:57:02 +01001108const char *zclient_serv_path_get()
Everton Marques1f298942014-08-21 15:47:28 -03001109{
1110 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
1111}
1112
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001113void
1114zclient_serv_path_set (char *path)
1115{
1116 struct stat sb;
1117
1118 /* reset */
1119 zclient_serv_path = NULL;
1120
1121 /* test if `path' is socket. don't set it otherwise. */
1122 if (stat(path, &sb) == -1)
1123 {
1124 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1125 return;
1126 }
1127
1128 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1129 {
1130 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1131 return;
1132 }
1133
1134 /* it seems that path is unix socket */
1135 zclient_serv_path = path;
1136}
1137