blob: d25c8d443195cfd5e3502d85ca161c79c13c98f1 [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
David Lamparterfd8f6eb2015-03-03 08:57:02 +010042const char *zclient_serv_path = NULL;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040043
paul718e3742002-12-13 20:15:29 +000044/* This file local debug flag. */
45int zclient_debug = 0;
David Lamparter6b0655a2014-06-04 06:53:35 +020046
paul718e3742002-12-13 20:15:29 +000047/* Allocate zclient structure. */
48struct zclient *
Donald Sharp71252932015-09-24 09:25:19 -040049zclient_new (struct thread_master *master)
paul718e3742002-12-13 20:15:29 +000050{
51 struct zclient *zclient;
Stephen Hemminger393deb92008-08-18 14:13:29 -070052 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
paul718e3742002-12-13 20:15:29 +000053
54 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
55 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs634f9ea2005-04-11 15:51:40 +000056 zclient->wb = buffer_new(0);
Donald Sharp71252932015-09-24 09:25:19 -040057 zclient->master = master;
paul718e3742002-12-13 20:15:29 +000058
59 return zclient;
60}
61
Chris Caputo228da422009-07-18 05:44:03 +000062/* This function is only called when exiting, because
ajs634f9ea2005-04-11 15:51:40 +000063 many parts of the code do not check for I/O errors, so they could
64 reference an invalid pointer if the structure was ever freed.
ajs634f9ea2005-04-11 15:51:40 +000065
Chris Caputo228da422009-07-18 05:44:03 +000066 Free zclient structure. */
paul718e3742002-12-13 20:15:29 +000067void
68zclient_free (struct zclient *zclient)
69{
ajs634f9ea2005-04-11 15:51:40 +000070 if (zclient->ibuf)
71 stream_free(zclient->ibuf);
72 if (zclient->obuf)
73 stream_free(zclient->obuf);
74 if (zclient->wb)
75 buffer_free(zclient->wb);
76
paul718e3742002-12-13 20:15:29 +000077 XFREE (MTYPE_ZCLIENT, zclient);
78}
79
80/* Initialize zebra client. Argument redist_default is unwanted
81 redistribute route type. */
82void
83zclient_init (struct zclient *zclient, int redist_default)
84{
85 int i;
86
87 /* Enable zebra client connection by default. */
88 zclient->enable = 1;
89
90 /* Set -1 to the default socket value. */
91 zclient->sock = -1;
92
93 /* Clear redistribution flags. */
94 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
Feng Luc99f3482014-10-16 09:52:36 +080095 zclient->redist[i] = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +000096
97 /* Set unwanted redistribute route. bgpd does not need BGP route
98 redistribution. */
99 zclient->redist_default = redist_default;
paul718e3742002-12-13 20:15:29 +0000100
101 /* Set default-information redistribute to zero. */
Feng Luc99f3482014-10-16 09:52:36 +0800102 zclient->default_information = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +0000103
104 /* Schedule first zclient connection. */
105 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000106 zlog_debug ("zclient start scheduled");
paul718e3742002-12-13 20:15:29 +0000107
108 zclient_event (ZCLIENT_SCHEDULE, zclient);
109}
110
111/* Stop zebra client services. */
112void
113zclient_stop (struct zclient *zclient)
114{
115 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000116 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000117
118 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000119 THREAD_OFF(zclient->t_read);
120 THREAD_OFF(zclient->t_connect);
121 THREAD_OFF(zclient->t_write);
122
123 /* Reset streams. */
124 stream_reset(zclient->ibuf);
125 stream_reset(zclient->obuf);
126
127 /* Empty the write buffer. */
128 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000129
130 /* Close socket. */
131 if (zclient->sock >= 0)
132 {
133 close (zclient->sock);
134 zclient->sock = -1;
135 }
136 zclient->fail = 0;
137}
138
139void
140zclient_reset (struct zclient *zclient)
141{
142 zclient_stop (zclient);
143 zclient_init (zclient, zclient->redist_default);
144}
145
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400146#ifdef HAVE_TCP_ZEBRA
147
paul718e3742002-12-13 20:15:29 +0000148/* Make socket to zebra daemon. Return zebra socket. */
Everton Marques1a9352a2014-09-23 14:33:34 -0300149static int
ajs634f9ea2005-04-11 15:51:40 +0000150zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000151{
152 int sock;
153 int ret;
154 struct sockaddr_in serv;
155
156 /* We should think about IPv6 connection. */
157 sock = socket (AF_INET, SOCK_STREAM, 0);
158 if (sock < 0)
159 return -1;
160
161 /* Make server socket. */
162 memset (&serv, 0, sizeof (struct sockaddr_in));
163 serv.sin_family = AF_INET;
164 serv.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000165#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000166 serv.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000167#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000168 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
169
170 /* Connect to zebra. */
171 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
172 if (ret < 0)
173 {
174 close (sock);
175 return -1;
176 }
177 return sock;
178}
179
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400180#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400181
paul718e3742002-12-13 20:15:29 +0000182/* For sockaddr_un. */
183#include <sys/un.h>
184
Everton Marques1a9352a2014-09-23 14:33:34 -0300185static int
hasso8c328f12004-10-05 21:01:23 +0000186zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000187{
188 int ret;
189 int sock, len;
190 struct sockaddr_un addr;
191
192 sock = socket (AF_UNIX, SOCK_STREAM, 0);
193 if (sock < 0)
194 return -1;
195
196 /* Make server socket. */
197 memset (&addr, 0, sizeof (struct sockaddr_un));
198 addr.sun_family = AF_UNIX;
199 strncpy (addr.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000200#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +0000201 len = addr.sun_len = SUN_LEN(&addr);
202#else
203 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000204#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +0000205
206 ret = connect (sock, (struct sockaddr *) &addr, len);
207 if (ret < 0)
208 {
209 close (sock);
210 return -1;
211 }
212 return sock;
213}
214
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400215#endif /* HAVE_TCP_ZEBRA */
216
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400217/**
218 * Connect to zebra daemon.
219 * @param zclient a pointer to zclient structure
220 * @return socket fd just to make sure that connection established
221 * @see zclient_init
222 * @see zclient_new
223 */
224int
225zclient_socket_connect (struct zclient *zclient)
226{
227#ifdef HAVE_TCP_ZEBRA
228 zclient->sock = zclient_socket ();
229#else
Everton Marques1f298942014-08-21 15:47:28 -0300230 zclient->sock = zclient_socket_un (zclient_serv_path_get());
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400231#endif
232 return zclient->sock;
233}
234
ajs634f9ea2005-04-11 15:51:40 +0000235static int
236zclient_failed(struct zclient *zclient)
237{
238 zclient->fail++;
239 zclient_stop(zclient);
240 zclient_event(ZCLIENT_CONNECT, zclient);
241 return -1;
242}
243
244static int
245zclient_flush_data(struct thread *thread)
246{
247 struct zclient *zclient = THREAD_ARG(thread);
248
249 zclient->t_write = NULL;
250 if (zclient->sock < 0)
251 return -1;
252 switch (buffer_flush_available(zclient->wb, zclient->sock))
253 {
254 case BUFFER_ERROR:
255 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
256 __func__, zclient->sock);
257 return zclient_failed(zclient);
258 break;
259 case BUFFER_PENDING:
Donald Sharp71252932015-09-24 09:25:19 -0400260 zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
261 zclient, zclient->sock);
ajs634f9ea2005-04-11 15:51:40 +0000262 break;
263 case BUFFER_EMPTY:
264 break;
265 }
266 return 0;
267}
268
paul718e3742002-12-13 20:15:29 +0000269int
ajs634f9ea2005-04-11 15:51:40 +0000270zclient_send_message(struct zclient *zclient)
271{
272 if (zclient->sock < 0)
273 return -1;
274 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
275 stream_get_endp(zclient->obuf)))
276 {
277 case BUFFER_ERROR:
278 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
279 __func__, zclient->sock);
280 return zclient_failed(zclient);
281 break;
282 case BUFFER_EMPTY:
283 THREAD_OFF(zclient->t_write);
284 break;
285 case BUFFER_PENDING:
Donald Sharp71252932015-09-24 09:25:19 -0400286 THREAD_WRITE_ON (zclient->master, zclient->t_write,
287 zclient_flush_data, zclient, zclient->sock);
ajs634f9ea2005-04-11 15:51:40 +0000288 break;
289 }
290 return 0;
291}
292
pauld2110862006-01-17 17:43:18 +0000293void
Feng Luc99f3482014-10-16 09:52:36 +0800294zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000295{
296 /* length placeholder, caller can update */
297 stream_putw (s, ZEBRA_HEADER_SIZE);
298 stream_putc (s, ZEBRA_HEADER_MARKER);
299 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800300 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000301 stream_putw (s, command);
302}
303
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200304int
305zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
306 u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
307{
308 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
309 return -1;
310
311 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
312 *marker = stream_getc (s);
313 *version = stream_getc (s);
314 *vrf_id = stream_getw (s);
315 *cmd = stream_getw (s);
316
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400317 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
318 {
319 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
320 __func__, sock, *marker, *version);
321 return -1;
322 }
323
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200324 if (*size && stream_read (s, sock, *size) != *size)
325 return -1;
326
327 return 0;
328}
329
ajs634f9ea2005-04-11 15:51:40 +0000330/* Send simple Zebra message. */
331static int
Feng Luc99f3482014-10-16 09:52:36 +0800332zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000333{
334 struct stream *s;
335
336 /* Get zclient output buffer. */
337 s = zclient->obuf;
338 stream_reset (s);
339
340 /* Send very simple command only Zebra message. */
Feng Luc99f3482014-10-16 09:52:36 +0800341 zclient_create_header (s, command, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000342
ajs634f9ea2005-04-11 15:51:40 +0000343 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000344}
345
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400346static int
347zebra_hello_send (struct zclient *zclient)
348{
349 struct stream *s;
350
351 if (zclient->redist_default)
352 {
353 s = zclient->obuf;
354 stream_reset (s);
355
Feng Luc99f3482014-10-16 09:52:36 +0800356 /* The VRF ID in the HELLO message is always 0. */
357 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400358 stream_putc (s, zclient->redist_default);
359 stream_putw_at (s, 0, stream_get_endp (s));
360 return zclient_send_message(zclient);
361 }
362
363 return 0;
364}
365
Feng Luc99f3482014-10-16 09:52:36 +0800366/* Send requests to zebra daemon for the information in a VRF. */
367void
368zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
369{
370 int i;
371
372 /* zclient is disabled. */
373 if (! zclient->enable)
374 return;
375
376 /* If not connected to the zebra yet. */
377 if (zclient->sock < 0)
378 return;
379
380 if (zclient_debug)
381 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
382
383 /* We need router-id information. */
384 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
385
386 /* We need interface information. */
387 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
388
389 /* Set unwanted redistribute route. */
390 vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
391
392 /* Flush all redistribute request. */
393 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
394 if (i != zclient->redist_default &&
395 vrf_bitmap_check (zclient->redist[i], vrf_id))
396 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
397
398 /* If default information is needed. */
399 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
400 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
401}
402
paul718e3742002-12-13 20:15:29 +0000403/* Make connection to zebra daemon. */
404int
405zclient_start (struct zclient *zclient)
406{
paul718e3742002-12-13 20:15:29 +0000407 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000408 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000409
410 /* zclient is disabled. */
411 if (! zclient->enable)
412 return 0;
413
414 /* If already connected to the zebra. */
415 if (zclient->sock >= 0)
416 return 0;
417
418 /* Check connect thread. */
419 if (zclient->t_connect)
420 return 0;
421
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400422 if (zclient_socket_connect(zclient) < 0)
paul718e3742002-12-13 20:15:29 +0000423 {
424 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000425 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000426 zclient->fail++;
427 zclient_event (ZCLIENT_CONNECT, zclient);
428 return -1;
429 }
430
ajs634f9ea2005-04-11 15:51:40 +0000431 if (set_nonblocking(zclient->sock) < 0)
432 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
433
paul718e3742002-12-13 20:15:29 +0000434 /* Clear fail count. */
435 zclient->fail = 0;
436 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000437 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000438
439 /* Create read thread. */
440 zclient_event (ZCLIENT_READ, zclient);
441
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400442 zebra_hello_send (zclient);
443
Feng Luc99f3482014-10-16 09:52:36 +0800444 /* Inform the successful connection. */
445 if (zclient->zebra_connected)
446 (*zclient->zebra_connected) (zclient);
paul718e3742002-12-13 20:15:29 +0000447
448 return 0;
449}
450
451/* This function is a wrapper function for calling zclient_start from
452 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000453static int
paul718e3742002-12-13 20:15:29 +0000454zclient_connect (struct thread *t)
455{
456 struct zclient *zclient;
457
458 zclient = THREAD_ARG (t);
459 zclient->t_connect = NULL;
460
461 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000462 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000463
464 return zclient_start (zclient);
465}
David Lamparter6b0655a2014-06-04 06:53:35 +0200466
paul0a589352004-05-08 11:48:26 +0000467 /*
468 * "xdr_encode"-like interface that allows daemon (client) to send
469 * a message to zebra server for a route that needs to be
470 * added/deleted to the kernel. Info about the route is specified
471 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
472 * the info down the zclient socket using the stream_* functions.
473 *
474 * The corresponding read ("xdr_decode") function on the server
475 * side is zread_ipv4_add()/zread_ipv4_delete().
476 *
477 * 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
478 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 * | Length (2) | Command | Route Type |
480 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481 * | ZEBRA Flags | Message Flags | Prefix length |
482 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
483 * | Destination IPv4 Prefix for route |
484 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
485 * | Nexthop count |
486 * +-+-+-+-+-+-+-+-+
487 *
488 *
489 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
490 * described, as per the Nexthop count. Each nexthop described as:
491 *
492 * +-+-+-+-+-+-+-+-+
493 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
494 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495 * | IPv4 Nexthop address or Interface Index number |
496 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
497 *
498 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
499 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
500 * nexthop information is provided, and the message describes a prefix
501 * to blackhole or reject route.
502 *
503 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
504 * byte value.
505 *
506 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
507 * byte value.
508 *
509 * XXX: No attention paid to alignment.
510 */
paul718e3742002-12-13 20:15:29 +0000511int
paul0a589352004-05-08 11:48:26 +0000512zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
513 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000514{
515 int i;
516 int psize;
517 struct stream *s;
518
519 /* Reset stream. */
520 s = zclient->obuf;
521 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800522
523 zclient_create_header (s, cmd, api->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000524
525 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000526 stream_putc (s, api->type);
527 stream_putc (s, api->flags);
528 stream_putc (s, api->message);
G.Balaji5a616c02011-11-26 21:58:42 +0400529 stream_putw (s, api->safi);
paul0a589352004-05-08 11:48:26 +0000530
paul718e3742002-12-13 20:15:29 +0000531 /* Put prefix information. */
532 psize = PSIZE (p->prefixlen);
533 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000534 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000535
536 /* Nexthop, ifindex, distance and metric information. */
537 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
538 {
paul595db7f2003-05-25 21:35:06 +0000539 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
540 {
541 stream_putc (s, 1);
542 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000543 /* XXX assert(api->nexthop_num == 0); */
544 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000545 }
546 else
547 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000548
549 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000550 {
551 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
552 stream_put_in_addr (s, api->nexthop[i]);
553 }
paul718e3742002-12-13 20:15:29 +0000554 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000555 {
556 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
557 stream_putl (s, api->ifindex[i]);
558 }
paul718e3742002-12-13 20:15:29 +0000559 }
560
561 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
562 stream_putc (s, api->distance);
563 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
564 stream_putl (s, api->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200565 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
566 stream_putl (s, api->mtu);
paul718e3742002-12-13 20:15:29 +0000567
568 /* Put length at the first point of the stream. */
569 stream_putw_at (s, 0, stream_get_endp (s));
570
ajs634f9ea2005-04-11 15:51:40 +0000571 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000572}
573
574#ifdef HAVE_IPV6
575int
paul0a589352004-05-08 11:48:26 +0000576zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000577 struct zapi_ipv6 *api)
578{
579 int i;
580 int psize;
581 struct stream *s;
582
583 /* Reset stream. */
584 s = zclient->obuf;
585 stream_reset (s);
586
Feng Luc99f3482014-10-16 09:52:36 +0800587 zclient_create_header (s, cmd, api->vrf_id);
paul718e3742002-12-13 20:15:29 +0000588
paulc1b98002006-01-16 01:54:02 +0000589 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000590 stream_putc (s, api->type);
591 stream_putc (s, api->flags);
592 stream_putc (s, api->message);
G.Balajic7ec1792011-11-26 22:04:05 +0400593 stream_putw (s, api->safi);
paul718e3742002-12-13 20:15:29 +0000594
595 /* Put prefix information. */
596 psize = PSIZE (p->prefixlen);
597 stream_putc (s, p->prefixlen);
598 stream_write (s, (u_char *)&p->prefix, psize);
599
600 /* Nexthop, ifindex, distance and metric information. */
601 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
602 {
603 stream_putc (s, api->nexthop_num + api->ifindex_num);
604
605 for (i = 0; i < api->nexthop_num; i++)
606 {
607 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
608 stream_write (s, (u_char *)api->nexthop[i], 16);
609 }
610 for (i = 0; i < api->ifindex_num; i++)
611 {
612 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
613 stream_putl (s, api->ifindex[i]);
614 }
615 }
616
617 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
618 stream_putc (s, api->distance);
619 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
620 stream_putl (s, api->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200621 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
622 stream_putl (s, api->mtu);
paul718e3742002-12-13 20:15:29 +0000623
624 /* Put length at the first point of the stream. */
625 stream_putw_at (s, 0, stream_get_endp (s));
626
ajs634f9ea2005-04-11 15:51:40 +0000627 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000628}
paul718e3742002-12-13 20:15:29 +0000629#endif /* HAVE_IPV6 */
630
paul0a589352004-05-08 11:48:26 +0000631/*
632 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
633 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
634 * then set/unset redist[type] in the client handle (a struct zserv) for the
635 * sending client
636 */
paul718e3742002-12-13 20:15:29 +0000637int
Feng Luc99f3482014-10-16 09:52:36 +0800638zebra_redistribute_send (int command, struct zclient *zclient, int type,
639 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000640{
paul718e3742002-12-13 20:15:29 +0000641 struct stream *s;
642
ajs634f9ea2005-04-11 15:51:40 +0000643 s = zclient->obuf;
644 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000645
Feng Luc99f3482014-10-16 09:52:36 +0800646 zclient_create_header (s, command, vrf_id);
paul718e3742002-12-13 20:15:29 +0000647 stream_putc (s, type);
paulc1b98002006-01-16 01:54:02 +0000648
649 stream_putw_at (s, 0, stream_get_endp (s));
650
ajs634f9ea2005-04-11 15:51:40 +0000651 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000652}
653
Paul Jakmabf83fa22016-02-09 15:23:03 +0000654/* Get prefix in ZServ format; family should be filled in on prefix */
655static void
656zclient_stream_get_prefix (struct stream *s, struct prefix *p)
657{
658 size_t plen = prefix_blen (p);
659 u_char c;
660 p->prefixlen = 0;
661
662 if (plen == 0)
663 return;
664
665 stream_get (&p->u.prefix, s, plen);
666 c = stream_getc(s);
667 p->prefixlen = MIN(plen * 8, c);
668}
669
hasso18a6dce2004-10-03 18:18:34 +0000670/* Router-id update from zebra daemon. */
671void
672zebra_router_id_update_read (struct stream *s, struct prefix *rid)
673{
hasso18a6dce2004-10-03 18:18:34 +0000674 /* Fetch interface address. */
675 rid->family = stream_getc (s);
Paul Jakmabf83fa22016-02-09 15:23:03 +0000676
677 zclient_stream_get_prefix (s, rid);
hasso18a6dce2004-10-03 18:18:34 +0000678}
679
paul718e3742002-12-13 20:15:29 +0000680/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000681/*
682 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
683 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
684 * 0 1 2 3
685 * 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
686 * +-+-+-+-+-+-+-+-+
687 * | type |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | ifname |
690 * | |
691 * | |
692 * | |
693 * | |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | ifindex |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * | if_flags |
paulc77d4542006-01-11 01:59:04 +0000698 * | |
paul0a589352004-05-08 11:48:26 +0000699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * | metric |
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
702 * | ifmtu |
703 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704 * | ifmtu6 |
705 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 * | bandwidth |
707 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
708 * | sockaddr_dl |
709 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
710 */
711
paul718e3742002-12-13 20:15:29 +0000712struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800713zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000714{
715 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000716 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000717
718 /* Read interface name. */
719 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
720
ajsa3491982005-04-02 22:50:38 +0000721 /* Lookup/create interface by name. */
Feng Luc99f3482014-10-16 09:52:36 +0800722 ifp = if_get_by_name_len_vrf (ifname_tmp,
723 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
724 vrf_id);
paul718e3742002-12-13 20:15:29 +0000725
Josh Bailey51d4ef82012-03-21 17:13:39 -0700726 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000727
paul718e3742002-12-13 20:15:29 +0000728 return ifp;
729}
730
paul0a589352004-05-08 11:48:26 +0000731/*
732 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
733 * from zebra server. The format of this message is the same as
734 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
735 * comments for zebra_interface_add_read), except that no sockaddr_dl
736 * is sent at the tail of the message.
737 */
paul718e3742002-12-13 20:15:29 +0000738struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800739zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000740{
741 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000742 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000743
744 /* Read interface name. */
745 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
746
747 /* Lookup this by interface index. */
Feng Luc99f3482014-10-16 09:52:36 +0800748 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
749 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
750 vrf_id);
paul718e3742002-12-13 20:15:29 +0000751
752 /* If such interface does not exist, indicate an error */
753 if (! ifp)
754 return NULL;
755
Josh Bailey51d4ef82012-03-21 17:13:39 -0700756 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000757
758 return ifp;
759}
760
paul0a589352004-05-08 11:48:26 +0000761/*
762 * format of message for address additon is:
763 * 0
764 * 0 1 2 3 4 5 6 7
765 * +-+-+-+-+-+-+-+-+
766 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
767 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
768 * | |
769 * + +
770 * | ifindex |
771 * + +
772 * | |
773 * + +
774 * | |
775 * +-+-+-+-+-+-+-+-+
776 * | ifc_flags | flags for connected address
777 * +-+-+-+-+-+-+-+-+
778 * | addr_family |
779 * +-+-+-+-+-+-+-+-+
780 * | addr... |
781 * : :
782 * | |
783 * +-+-+-+-+-+-+-+-+
784 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
785 * +-+-+-+-+-+-+-+-+
786 * | daddr.. |
787 * : :
788 * | |
789 * +-+-+-+-+-+-+-+-+
790 *
791 */
792
hasso18a6dce2004-10-03 18:18:34 +0000793void
794zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
795{
796 /* Read interface's index. */
797 ifp->ifindex = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000798 ifp->status = stream_getc (s);
hasso18a6dce2004-10-03 18:18:34 +0000799
800 /* Read interface's value. */
paulc77d4542006-01-11 01:59:04 +0000801 ifp->flags = stream_getq (s);
hasso18a6dce2004-10-03 18:18:34 +0000802 ifp->metric = stream_getl (s);
803 ifp->mtu = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000804 ifp->mtu6 = stream_getl (s);
hasso18a6dce2004-10-03 18:18:34 +0000805 ifp->bandwidth = stream_getl (s);
Timo Teräs954c7d62016-01-15 17:36:33 +0200806 ifp->ll_type = stream_getl (s);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700807 ifp->hw_addr_len = stream_getl (s);
808 if (ifp->hw_addr_len)
Paul Jakma2db96272016-02-08 14:46:28 +0000809 stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
hasso18a6dce2004-10-03 18:18:34 +0000810}
811
hasso3fb9cd62004-10-19 19:44:43 +0000812static int
813memconstant(const void *s, int c, size_t n)
814{
815 const u_char *p = s;
816
817 while (n-- > 0)
818 if (*p++ != c)
819 return 0;
820 return 1;
821}
822
paul718e3742002-12-13 20:15:29 +0000823struct connected *
Feng Luc99f3482014-10-16 09:52:36 +0800824zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000825{
Paul Jakma9099f9b2016-01-18 10:12:10 +0000826 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000827 struct interface *ifp;
828 struct connected *ifc;
Paul Jakmabf83fa22016-02-09 15:23:03 +0000829 struct prefix p, d, *dp;
paul718e3742002-12-13 20:15:29 +0000830 int plen;
paul0a589352004-05-08 11:48:26 +0000831 u_char ifc_flags;
832
833 memset (&p, 0, sizeof(p));
834 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000835
836 /* Get interface index. */
837 ifindex = stream_getl (s);
838
839 /* Lookup index. */
Feng Luc99f3482014-10-16 09:52:36 +0800840 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
paul718e3742002-12-13 20:15:29 +0000841 if (ifp == NULL)
842 {
paul0a589352004-05-08 11:48:26 +0000843 zlog_warn ("zebra_interface_address_read(%s): "
844 "Can't find interface by ifindex: %d ",
845 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
846 ifindex);
paul718e3742002-12-13 20:15:29 +0000847 return NULL;
848 }
849
850 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000851 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000852
853 /* Fetch interface address. */
Paul Jakmabf83fa22016-02-09 15:23:03 +0000854 d.family = p.family = stream_getc (s);
855 plen = prefix_blen (&d);
856
857 zclient_stream_get_prefix (s, &p);
paul718e3742002-12-13 20:15:29 +0000858
859 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +0000860 stream_get (&d.u.prefix, s, plen);
Paul Jakmabf83fa22016-02-09 15:23:03 +0000861
862 /* N.B. NULL destination pointers are encoded as all zeroes */
863 dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
864
paul0a589352004-05-08 11:48:26 +0000865 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
866 {
hasso3fb9cd62004-10-19 19:44:43 +0000867 /* N.B. NULL destination pointers are encoded as all zeroes */
Paul Jakmabf83fa22016-02-09 15:23:03 +0000868 ifc = connected_add_by_prefix(ifp, &p, dp);
paul0a589352004-05-08 11:48:26 +0000869 if (ifc != NULL)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000870 {
871 ifc->flags = ifc_flags;
872 if (ifc->destination)
873 ifc->destination->prefixlen = ifc->address->prefixlen;
David Lamparter90444ca2014-07-01 16:14:05 +0200874 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
875 {
876 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
Timo Teräs41eb9a42015-05-23 11:08:39 +0300877 char buf[PREFIX_STRLEN];
David Lamparter90444ca2014-07-01 16:14:05 +0200878 zlog_warn("warning: interface %s address %s "
879 "with peer flag set, but no peer address!",
Timo Teräs41eb9a42015-05-23 11:08:39 +0300880 ifp->name,
881 prefix2str (ifc->address, buf, sizeof buf));
David Lamparter90444ca2014-07-01 16:14:05 +0200882 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
883 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000884 }
paul0a589352004-05-08 11:48:26 +0000885 }
886 else
887 {
888 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
889 ifc = connected_delete_by_prefix(ifp, &p);
890 }
paul718e3742002-12-13 20:15:29 +0000891
892 return ifc;
893}
paul0a589352004-05-08 11:48:26 +0000894
David Lamparter6b0655a2014-06-04 06:53:35 +0200895
paul718e3742002-12-13 20:15:29 +0000896/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +0000897static int
paul718e3742002-12-13 20:15:29 +0000898zclient_read (struct thread *thread)
899{
ajs634f9ea2005-04-11 15:51:40 +0000900 size_t already;
paulc1b98002006-01-16 01:54:02 +0000901 uint16_t length, command;
902 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +0800903 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +0000904 struct zclient *zclient;
905
906 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +0000907 zclient = THREAD_ARG (thread);
908 zclient->t_read = NULL;
909
ajs634f9ea2005-04-11 15:51:40 +0000910 /* Read zebra header (if we don't have it already). */
911 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000912 {
ajs634f9ea2005-04-11 15:51:40 +0000913 ssize_t nbyte;
914 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
915 ZEBRA_HEADER_SIZE-already)) == 0) ||
916 (nbyte == -1))
917 {
918 if (zclient_debug)
919 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
920 return zclient_failed(zclient);
921 }
922 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
923 {
924 /* Try again later. */
925 zclient_event (ZCLIENT_READ, zclient);
926 return 0;
927 }
928 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000929 }
930
ajs634f9ea2005-04-11 15:51:40 +0000931 /* Reset to read from the beginning of the incoming packet. */
932 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +0000933
paulc1b98002006-01-16 01:54:02 +0000934 /* Fetch header values. */
paul718e3742002-12-13 20:15:29 +0000935 length = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000936 marker = stream_getc (zclient->ibuf);
937 version = stream_getc (zclient->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +0800938 vrf_id = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000939 command = stream_getw (zclient->ibuf);
940
941 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
942 {
943 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
944 __func__, zclient->sock, marker, version);
945 return zclient_failed(zclient);
946 }
947
ajs634f9ea2005-04-11 15:51:40 +0000948 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000949 {
ajs634f9ea2005-04-11 15:51:40 +0000950 zlog_err("%s: socket %d message length %u is less than %d ",
951 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
952 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +0000953 }
ajs634f9ea2005-04-11 15:51:40 +0000954
955 /* Length check. */
956 if (length > STREAM_SIZE(zclient->ibuf))
957 {
958 struct stream *ns;
959 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
960 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
961 ns = stream_new(length);
962 stream_copy(ns, zclient->ibuf);
963 stream_free (zclient->ibuf);
964 zclient->ibuf = ns;
965 }
paul718e3742002-12-13 20:15:29 +0000966
967 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +0000968 if (already < length)
969 {
970 ssize_t nbyte;
971 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
972 length-already)) == 0) ||
973 (nbyte == -1))
974 {
975 if (zclient_debug)
976 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
977 return zclient_failed(zclient);
978 }
979 if (nbyte != (ssize_t)(length-already))
980 {
981 /* Try again later. */
982 zclient_event (ZCLIENT_READ, zclient);
983 return 0;
984 }
985 }
986
987 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000988
paul0a589352004-05-08 11:48:26 +0000989 if (zclient_debug)
Feng Luc99f3482014-10-16 09:52:36 +0800990 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
paul0a589352004-05-08 11:48:26 +0000991
paul718e3742002-12-13 20:15:29 +0000992 switch (command)
993 {
hasso18a6dce2004-10-03 18:18:34 +0000994 case ZEBRA_ROUTER_ID_UPDATE:
995 if (zclient->router_id_update)
Feng Luc99f3482014-10-16 09:52:36 +0800996 (*zclient->router_id_update) (command, zclient, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000997 break;
paul718e3742002-12-13 20:15:29 +0000998 case ZEBRA_INTERFACE_ADD:
999 if (zclient->interface_add)
Feng Luc99f3482014-10-16 09:52:36 +08001000 (*zclient->interface_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001001 break;
1002 case ZEBRA_INTERFACE_DELETE:
1003 if (zclient->interface_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001004 (*zclient->interface_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001005 break;
1006 case ZEBRA_INTERFACE_ADDRESS_ADD:
1007 if (zclient->interface_address_add)
Feng Luc99f3482014-10-16 09:52:36 +08001008 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001009 break;
1010 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1011 if (zclient->interface_address_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001012 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001013 break;
1014 case ZEBRA_INTERFACE_UP:
1015 if (zclient->interface_up)
Feng Luc99f3482014-10-16 09:52:36 +08001016 (*zclient->interface_up) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001017 break;
1018 case ZEBRA_INTERFACE_DOWN:
1019 if (zclient->interface_down)
Feng Luc99f3482014-10-16 09:52:36 +08001020 (*zclient->interface_down) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001021 break;
1022 case ZEBRA_IPV4_ROUTE_ADD:
1023 if (zclient->ipv4_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001024 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001025 break;
1026 case ZEBRA_IPV4_ROUTE_DELETE:
1027 if (zclient->ipv4_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001028 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001029 break;
1030 case ZEBRA_IPV6_ROUTE_ADD:
1031 if (zclient->ipv6_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001032 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001033 break;
1034 case ZEBRA_IPV6_ROUTE_DELETE:
1035 if (zclient->ipv6_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001036 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001037 break;
1038 default:
1039 break;
1040 }
1041
ajs634f9ea2005-04-11 15:51:40 +00001042 if (zclient->sock < 0)
1043 /* Connection was closed during packet processing. */
1044 return -1;
1045
paul718e3742002-12-13 20:15:29 +00001046 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +00001047 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +00001048 zclient_event (ZCLIENT_READ, zclient);
1049
1050 return 0;
1051}
1052
1053void
Feng Luc99f3482014-10-16 09:52:36 +08001054zclient_redistribute (int command, struct zclient *zclient, int type,
1055 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001056{
paul718e3742002-12-13 20:15:29 +00001057
paul0a589352004-05-08 11:48:26 +00001058 if (command == ZEBRA_REDISTRIBUTE_ADD)
1059 {
Feng Luc99f3482014-10-16 09:52:36 +08001060 if (vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001061 return;
Feng Luc99f3482014-10-16 09:52:36 +08001062 vrf_bitmap_set (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001063 }
1064 else
1065 {
Feng Luc99f3482014-10-16 09:52:36 +08001066 if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001067 return;
Feng Luc99f3482014-10-16 09:52:36 +08001068 vrf_bitmap_unset (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001069 }
paul718e3742002-12-13 20:15:29 +00001070
1071 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001072 zebra_redistribute_send (command, zclient, type, vrf_id);
paul718e3742002-12-13 20:15:29 +00001073}
1074
paul0a589352004-05-08 11:48:26 +00001075
paul718e3742002-12-13 20:15:29 +00001076void
Feng Luc99f3482014-10-16 09:52:36 +08001077zclient_redistribute_default (int command, struct zclient *zclient,
1078 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001079{
paul718e3742002-12-13 20:15:29 +00001080
paul0a589352004-05-08 11:48:26 +00001081 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1082 {
Feng Luc99f3482014-10-16 09:52:36 +08001083 if (vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001084 return;
Feng Luc99f3482014-10-16 09:52:36 +08001085 vrf_bitmap_set (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001086 }
1087 else
1088 {
Feng Luc99f3482014-10-16 09:52:36 +08001089 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001090 return;
Feng Luc99f3482014-10-16 09:52:36 +08001091 vrf_bitmap_unset (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001092 }
paul718e3742002-12-13 20:15:29 +00001093
1094 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001095 zebra_message_send (zclient, command, vrf_id);
paul718e3742002-12-13 20:15:29 +00001096}
1097
paul718e3742002-12-13 20:15:29 +00001098static void
1099zclient_event (enum event event, struct zclient *zclient)
1100{
1101 switch (event)
1102 {
1103 case ZCLIENT_SCHEDULE:
1104 if (! zclient->t_connect)
1105 zclient->t_connect =
Donald Sharp71252932015-09-24 09:25:19 -04001106 thread_add_event (zclient->master, zclient_connect, zclient, 0);
paul718e3742002-12-13 20:15:29 +00001107 break;
1108 case ZCLIENT_CONNECT:
1109 if (zclient->fail >= 10)
1110 return;
1111 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +00001112 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +00001113 zclient->fail < 3 ? 10 : 60);
1114 if (! zclient->t_connect)
1115 zclient->t_connect =
Donald Sharp71252932015-09-24 09:25:19 -04001116 thread_add_timer (zclient->master, zclient_connect, zclient,
paul718e3742002-12-13 20:15:29 +00001117 zclient->fail < 3 ? 10 : 60);
1118 break;
1119 case ZCLIENT_READ:
1120 zclient->t_read =
Donald Sharp71252932015-09-24 09:25:19 -04001121 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
paul718e3742002-12-13 20:15:29 +00001122 break;
1123 }
1124}
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001125
David Lamparterfd8f6eb2015-03-03 08:57:02 +01001126const char *zclient_serv_path_get()
Everton Marques1f298942014-08-21 15:47:28 -03001127{
1128 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
1129}
1130
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001131void
1132zclient_serv_path_set (char *path)
1133{
1134 struct stat sb;
1135
1136 /* reset */
1137 zclient_serv_path = NULL;
1138
1139 /* test if `path' is socket. don't set it otherwise. */
1140 if (stat(path, &sb) == -1)
1141 {
1142 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1143 return;
1144 }
1145
1146 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1147 {
1148 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1149 return;
1150 }
1151
1152 /* it seems that path is unix socket */
1153 zclient_serv_path = path;
1154}
1155