blob: 097314afa0eb97d7d6196a3971f6c32c837723b3 [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{
David Lampartera3ee1202015-12-16 19:38:23 +0100115 int i;
116
paul718e3742002-12-13 20:15:29 +0000117 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000118 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000119
120 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000121 THREAD_OFF(zclient->t_read);
122 THREAD_OFF(zclient->t_connect);
123 THREAD_OFF(zclient->t_write);
124
125 /* Reset streams. */
126 stream_reset(zclient->ibuf);
127 stream_reset(zclient->obuf);
128
129 /* Empty the write buffer. */
130 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000131
132 /* Close socket. */
133 if (zclient->sock >= 0)
134 {
135 close (zclient->sock);
136 zclient->sock = -1;
137 }
138 zclient->fail = 0;
David Lampartera3ee1202015-12-16 19:38:23 +0100139
140 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
141 {
142 vrf_bitmap_free(zclient->redist[i]);
143 zclient->redist[i] = VRF_BITMAP_NULL;
144 }
145 vrf_bitmap_free(zclient->default_information);
146 zclient->default_information = VRF_BITMAP_NULL;
paul718e3742002-12-13 20:15:29 +0000147}
148
149void
150zclient_reset (struct zclient *zclient)
151{
152 zclient_stop (zclient);
153 zclient_init (zclient, zclient->redist_default);
154}
155
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400156#ifdef HAVE_TCP_ZEBRA
157
paul718e3742002-12-13 20:15:29 +0000158/* Make socket to zebra daemon. Return zebra socket. */
Everton Marques1a9352a2014-09-23 14:33:34 -0300159static int
ajs634f9ea2005-04-11 15:51:40 +0000160zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000161{
162 int sock;
163 int ret;
164 struct sockaddr_in serv;
165
166 /* We should think about IPv6 connection. */
167 sock = socket (AF_INET, SOCK_STREAM, 0);
168 if (sock < 0)
169 return -1;
170
171 /* Make server socket. */
172 memset (&serv, 0, sizeof (struct sockaddr_in));
173 serv.sin_family = AF_INET;
174 serv.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000175#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000176 serv.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000177#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000178 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
179
180 /* Connect to zebra. */
181 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
182 if (ret < 0)
183 {
Donald Sharp687bea22016-01-25 14:56:26 -0500184 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
paul718e3742002-12-13 20:15:29 +0000185 close (sock);
186 return -1;
187 }
188 return sock;
189}
190
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400191#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400192
paul718e3742002-12-13 20:15:29 +0000193/* For sockaddr_un. */
194#include <sys/un.h>
195
Everton Marques1a9352a2014-09-23 14:33:34 -0300196static int
hasso8c328f12004-10-05 21:01:23 +0000197zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000198{
199 int ret;
200 int sock, len;
201 struct sockaddr_un addr;
202
203 sock = socket (AF_UNIX, SOCK_STREAM, 0);
204 if (sock < 0)
205 return -1;
206
207 /* Make server socket. */
208 memset (&addr, 0, sizeof (struct sockaddr_un));
209 addr.sun_family = AF_UNIX;
210 strncpy (addr.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000211#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +0000212 len = addr.sun_len = SUN_LEN(&addr);
213#else
214 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000215#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +0000216
217 ret = connect (sock, (struct sockaddr *) &addr, len);
218 if (ret < 0)
219 {
Donald Sharp687bea22016-01-25 14:56:26 -0500220 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
paul718e3742002-12-13 20:15:29 +0000221 close (sock);
222 return -1;
223 }
224 return sock;
225}
226
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400227#endif /* HAVE_TCP_ZEBRA */
228
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400229/**
230 * Connect to zebra daemon.
231 * @param zclient a pointer to zclient structure
232 * @return socket fd just to make sure that connection established
233 * @see zclient_init
234 * @see zclient_new
235 */
236int
237zclient_socket_connect (struct zclient *zclient)
238{
239#ifdef HAVE_TCP_ZEBRA
240 zclient->sock = zclient_socket ();
241#else
Everton Marques1f298942014-08-21 15:47:28 -0300242 zclient->sock = zclient_socket_un (zclient_serv_path_get());
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400243#endif
244 return zclient->sock;
245}
246
ajs634f9ea2005-04-11 15:51:40 +0000247static int
248zclient_failed(struct zclient *zclient)
249{
250 zclient->fail++;
251 zclient_stop(zclient);
252 zclient_event(ZCLIENT_CONNECT, zclient);
253 return -1;
254}
255
256static int
257zclient_flush_data(struct thread *thread)
258{
259 struct zclient *zclient = THREAD_ARG(thread);
260
261 zclient->t_write = NULL;
262 if (zclient->sock < 0)
263 return -1;
264 switch (buffer_flush_available(zclient->wb, zclient->sock))
265 {
266 case BUFFER_ERROR:
267 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
268 __func__, zclient->sock);
269 return zclient_failed(zclient);
270 break;
271 case BUFFER_PENDING:
Donald Sharp71252932015-09-24 09:25:19 -0400272 zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
273 zclient, zclient->sock);
ajs634f9ea2005-04-11 15:51:40 +0000274 break;
275 case BUFFER_EMPTY:
276 break;
277 }
278 return 0;
279}
280
paul718e3742002-12-13 20:15:29 +0000281int
ajs634f9ea2005-04-11 15:51:40 +0000282zclient_send_message(struct zclient *zclient)
283{
284 if (zclient->sock < 0)
285 return -1;
286 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
287 stream_get_endp(zclient->obuf)))
288 {
289 case BUFFER_ERROR:
290 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
291 __func__, zclient->sock);
292 return zclient_failed(zclient);
293 break;
294 case BUFFER_EMPTY:
295 THREAD_OFF(zclient->t_write);
296 break;
297 case BUFFER_PENDING:
Donald Sharp71252932015-09-24 09:25:19 -0400298 THREAD_WRITE_ON (zclient->master, zclient->t_write,
299 zclient_flush_data, zclient, zclient->sock);
ajs634f9ea2005-04-11 15:51:40 +0000300 break;
301 }
302 return 0;
303}
304
pauld2110862006-01-17 17:43:18 +0000305void
Feng Luc99f3482014-10-16 09:52:36 +0800306zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000307{
308 /* length placeholder, caller can update */
309 stream_putw (s, ZEBRA_HEADER_SIZE);
310 stream_putc (s, ZEBRA_HEADER_MARKER);
311 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800312 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000313 stream_putw (s, command);
314}
315
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200316int
317zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
318 u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
319{
320 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
321 return -1;
322
323 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
324 *marker = stream_getc (s);
325 *version = stream_getc (s);
326 *vrf_id = stream_getw (s);
327 *cmd = stream_getw (s);
328
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400329 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
330 {
331 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
332 __func__, sock, *marker, *version);
333 return -1;
334 }
335
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200336 if (*size && stream_read (s, sock, *size) != *size)
337 return -1;
338
339 return 0;
340}
341
ajs634f9ea2005-04-11 15:51:40 +0000342/* Send simple Zebra message. */
343static int
Feng Luc99f3482014-10-16 09:52:36 +0800344zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000345{
346 struct stream *s;
347
348 /* Get zclient output buffer. */
349 s = zclient->obuf;
350 stream_reset (s);
351
352 /* Send very simple command only Zebra message. */
Feng Luc99f3482014-10-16 09:52:36 +0800353 zclient_create_header (s, command, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000354
ajs634f9ea2005-04-11 15:51:40 +0000355 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000356}
357
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400358static int
359zebra_hello_send (struct zclient *zclient)
360{
361 struct stream *s;
362
363 if (zclient->redist_default)
364 {
365 s = zclient->obuf;
366 stream_reset (s);
367
Feng Luc99f3482014-10-16 09:52:36 +0800368 /* The VRF ID in the HELLO message is always 0. */
369 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400370 stream_putc (s, zclient->redist_default);
371 stream_putw_at (s, 0, stream_get_endp (s));
372 return zclient_send_message(zclient);
373 }
374
375 return 0;
376}
377
Feng Luc99f3482014-10-16 09:52:36 +0800378/* Send requests to zebra daemon for the information in a VRF. */
379void
380zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
381{
382 int i;
383
384 /* zclient is disabled. */
385 if (! zclient->enable)
386 return;
387
388 /* If not connected to the zebra yet. */
389 if (zclient->sock < 0)
390 return;
391
392 if (zclient_debug)
393 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
394
395 /* We need router-id information. */
396 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
397
398 /* We need interface information. */
399 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
400
401 /* Set unwanted redistribute route. */
402 vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
403
404 /* Flush all redistribute request. */
405 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
406 if (i != zclient->redist_default &&
407 vrf_bitmap_check (zclient->redist[i], vrf_id))
408 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
409
410 /* If default information is needed. */
411 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
412 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
413}
414
paul718e3742002-12-13 20:15:29 +0000415/* Make connection to zebra daemon. */
416int
417zclient_start (struct zclient *zclient)
418{
paul718e3742002-12-13 20:15:29 +0000419 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000420 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000421
422 /* zclient is disabled. */
423 if (! zclient->enable)
424 return 0;
425
426 /* If already connected to the zebra. */
427 if (zclient->sock >= 0)
428 return 0;
429
430 /* Check connect thread. */
431 if (zclient->t_connect)
432 return 0;
433
Donald Sharp687bea22016-01-25 14:56:26 -0500434 /*
435 * If we fail to connect to the socket on initialization,
436 * Let's wait a second and see if we can reconnect.
437 * Cause if we don't connect, we never attempt to
438 * reconnect. On startup if zebra is slow we
439 * can get into this situation.
440 */
441 while (zclient_socket_connect(zclient) < 0 && zclient->fail < 5)
paul718e3742002-12-13 20:15:29 +0000442 {
443 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000444 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000445 zclient->fail++;
Donald Sharp687bea22016-01-25 14:56:26 -0500446 sleep (1);
447 }
448
449 if (zclient->sock < 0)
450 {
paul718e3742002-12-13 20:15:29 +0000451 zclient_event (ZCLIENT_CONNECT, zclient);
452 return -1;
453 }
454
ajs634f9ea2005-04-11 15:51:40 +0000455 if (set_nonblocking(zclient->sock) < 0)
456 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
457
paul718e3742002-12-13 20:15:29 +0000458 /* Clear fail count. */
459 zclient->fail = 0;
460 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000461 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000462
463 /* Create read thread. */
464 zclient_event (ZCLIENT_READ, zclient);
465
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400466 zebra_hello_send (zclient);
467
Feng Luc99f3482014-10-16 09:52:36 +0800468 /* Inform the successful connection. */
469 if (zclient->zebra_connected)
470 (*zclient->zebra_connected) (zclient);
paul718e3742002-12-13 20:15:29 +0000471
472 return 0;
473}
474
475/* This function is a wrapper function for calling zclient_start from
476 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000477static int
paul718e3742002-12-13 20:15:29 +0000478zclient_connect (struct thread *t)
479{
480 struct zclient *zclient;
481
482 zclient = THREAD_ARG (t);
483 zclient->t_connect = NULL;
484
485 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000486 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000487
488 return zclient_start (zclient);
489}
David Lamparter6b0655a2014-06-04 06:53:35 +0200490
paul0a589352004-05-08 11:48:26 +0000491 /*
492 * "xdr_encode"-like interface that allows daemon (client) to send
493 * a message to zebra server for a route that needs to be
494 * added/deleted to the kernel. Info about the route is specified
495 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
496 * the info down the zclient socket using the stream_* functions.
497 *
498 * The corresponding read ("xdr_decode") function on the server
499 * side is zread_ipv4_add()/zread_ipv4_delete().
500 *
501 * 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
502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * | Length (2) | Command | Route Type |
504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * | ZEBRA Flags | Message Flags | Prefix length |
506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507 * | Destination IPv4 Prefix for route |
508 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509 * | Nexthop count |
510 * +-+-+-+-+-+-+-+-+
511 *
512 *
513 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
514 * described, as per the Nexthop count. Each nexthop described as:
515 *
516 * +-+-+-+-+-+-+-+-+
517 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 * | IPv4 Nexthop address or Interface Index number |
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 *
522 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
523 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
524 * nexthop information is provided, and the message describes a prefix
525 * to blackhole or reject route.
526 *
527 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
528 * byte value.
529 *
530 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
531 * byte value.
532 *
533 * XXX: No attention paid to alignment.
534 */
paul718e3742002-12-13 20:15:29 +0000535int
paul0a589352004-05-08 11:48:26 +0000536zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
537 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000538{
539 int i;
540 int psize;
541 struct stream *s;
542
543 /* Reset stream. */
544 s = zclient->obuf;
545 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800546
547 zclient_create_header (s, cmd, api->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000548
549 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000550 stream_putc (s, api->type);
551 stream_putc (s, api->flags);
552 stream_putc (s, api->message);
G.Balaji5a616c02011-11-26 21:58:42 +0400553 stream_putw (s, api->safi);
paul0a589352004-05-08 11:48:26 +0000554
paul718e3742002-12-13 20:15:29 +0000555 /* Put prefix information. */
556 psize = PSIZE (p->prefixlen);
557 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000558 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000559
560 /* Nexthop, ifindex, distance and metric information. */
561 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
562 {
paul595db7f2003-05-25 21:35:06 +0000563 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
564 {
565 stream_putc (s, 1);
566 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000567 /* XXX assert(api->nexthop_num == 0); */
568 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000569 }
570 else
571 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000572
573 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000574 {
575 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
576 stream_put_in_addr (s, api->nexthop[i]);
577 }
paul718e3742002-12-13 20:15:29 +0000578 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000579 {
580 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
581 stream_putl (s, api->ifindex[i]);
582 }
paul718e3742002-12-13 20:15:29 +0000583 }
584
585 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
586 stream_putc (s, api->distance);
587 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
588 stream_putl (s, api->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200589 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
590 stream_putl (s, api->mtu);
paul718e3742002-12-13 20:15:29 +0000591
592 /* Put length at the first point of the stream. */
593 stream_putw_at (s, 0, stream_get_endp (s));
594
ajs634f9ea2005-04-11 15:51:40 +0000595 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000596}
597
598#ifdef HAVE_IPV6
599int
paul0a589352004-05-08 11:48:26 +0000600zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000601 struct zapi_ipv6 *api)
602{
603 int i;
604 int psize;
605 struct stream *s;
606
607 /* Reset stream. */
608 s = zclient->obuf;
609 stream_reset (s);
610
Feng Luc99f3482014-10-16 09:52:36 +0800611 zclient_create_header (s, cmd, api->vrf_id);
paul718e3742002-12-13 20:15:29 +0000612
paulc1b98002006-01-16 01:54:02 +0000613 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000614 stream_putc (s, api->type);
615 stream_putc (s, api->flags);
616 stream_putc (s, api->message);
G.Balajic7ec1792011-11-26 22:04:05 +0400617 stream_putw (s, api->safi);
paul718e3742002-12-13 20:15:29 +0000618
619 /* Put prefix information. */
620 psize = PSIZE (p->prefixlen);
621 stream_putc (s, p->prefixlen);
622 stream_write (s, (u_char *)&p->prefix, psize);
623
624 /* Nexthop, ifindex, distance and metric information. */
625 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
626 {
627 stream_putc (s, api->nexthop_num + api->ifindex_num);
628
629 for (i = 0; i < api->nexthop_num; i++)
630 {
631 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
632 stream_write (s, (u_char *)api->nexthop[i], 16);
633 }
634 for (i = 0; i < api->ifindex_num; i++)
635 {
636 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
637 stream_putl (s, api->ifindex[i]);
638 }
639 }
640
641 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
642 stream_putc (s, api->distance);
643 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
644 stream_putl (s, api->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200645 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
646 stream_putl (s, api->mtu);
paul718e3742002-12-13 20:15:29 +0000647
648 /* Put length at the first point of the stream. */
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}
paul718e3742002-12-13 20:15:29 +0000653#endif /* HAVE_IPV6 */
654
paul0a589352004-05-08 11:48:26 +0000655/*
656 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
657 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
658 * then set/unset redist[type] in the client handle (a struct zserv) for the
659 * sending client
660 */
paul718e3742002-12-13 20:15:29 +0000661int
Feng Luc99f3482014-10-16 09:52:36 +0800662zebra_redistribute_send (int command, struct zclient *zclient, int type,
663 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000664{
paul718e3742002-12-13 20:15:29 +0000665 struct stream *s;
666
ajs634f9ea2005-04-11 15:51:40 +0000667 s = zclient->obuf;
668 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000669
Feng Luc99f3482014-10-16 09:52:36 +0800670 zclient_create_header (s, command, vrf_id);
paul718e3742002-12-13 20:15:29 +0000671 stream_putc (s, type);
paulc1b98002006-01-16 01:54:02 +0000672
673 stream_putw_at (s, 0, stream_get_endp (s));
674
ajs634f9ea2005-04-11 15:51:40 +0000675 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000676}
677
Paul Jakmabf83fa22016-02-09 15:23:03 +0000678/* Get prefix in ZServ format; family should be filled in on prefix */
679static void
680zclient_stream_get_prefix (struct stream *s, struct prefix *p)
681{
682 size_t plen = prefix_blen (p);
683 u_char c;
684 p->prefixlen = 0;
685
686 if (plen == 0)
687 return;
688
689 stream_get (&p->u.prefix, s, plen);
690 c = stream_getc(s);
691 p->prefixlen = MIN(plen * 8, c);
692}
693
hasso18a6dce2004-10-03 18:18:34 +0000694/* Router-id update from zebra daemon. */
695void
696zebra_router_id_update_read (struct stream *s, struct prefix *rid)
697{
hasso18a6dce2004-10-03 18:18:34 +0000698 /* Fetch interface address. */
699 rid->family = stream_getc (s);
Paul Jakmabf83fa22016-02-09 15:23:03 +0000700
701 zclient_stream_get_prefix (s, rid);
hasso18a6dce2004-10-03 18:18:34 +0000702}
703
paul718e3742002-12-13 20:15:29 +0000704/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000705/*
706 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
707 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
708 * 0 1 2 3
709 * 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
paul0a589352004-05-08 11:48:26 +0000710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
711 * | ifname |
712 * | |
713 * | |
714 * | |
715 * | |
716 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200717 * | ifindex |
paul0a589352004-05-08 11:48:26 +0000718 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200719 * | status |
720 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
721 * | if_flags |
paulc77d4542006-01-11 01:59:04 +0000722 * | |
paul0a589352004-05-08 11:48:26 +0000723 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200724 * | metric |
paul0a589352004-05-08 11:48:26 +0000725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200726 * | ifmtu |
paul0a589352004-05-08 11:48:26 +0000727 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200728 * | ifmtu6 |
paul0a589352004-05-08 11:48:26 +0000729 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200730 * | bandwidth |
paul0a589352004-05-08 11:48:26 +0000731 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200732 * | Link Layer Type |
733 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
734 * | Harware Address Length |
735 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
736 * | Hardware Address if HW lenght different from 0 |
737 * | ... max INTERFACE_HWADDR_MAX |
738 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
739 * | Link_params? | Whether a link-params follows: 1 or 0.
740 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
741 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
742 * | .... (struct if_link_params). |
paul0a589352004-05-08 11:48:26 +0000743 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
744 */
745
paul718e3742002-12-13 20:15:29 +0000746struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800747zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000748{
749 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000750 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000751
752 /* Read interface name. */
753 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
754
ajsa3491982005-04-02 22:50:38 +0000755 /* Lookup/create interface by name. */
Feng Luc99f3482014-10-16 09:52:36 +0800756 ifp = if_get_by_name_len_vrf (ifname_tmp,
757 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
758 vrf_id);
paul718e3742002-12-13 20:15:29 +0000759
Josh Bailey51d4ef82012-03-21 17:13:39 -0700760 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000761
paul718e3742002-12-13 20:15:29 +0000762 return ifp;
763}
764
paul0a589352004-05-08 11:48:26 +0000765/*
766 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
767 * from zebra server. The format of this message is the same as
768 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
769 * comments for zebra_interface_add_read), except that no sockaddr_dl
770 * is sent at the tail of the message.
771 */
paul718e3742002-12-13 20:15:29 +0000772struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800773zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000774{
775 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000776 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000777
778 /* Read interface name. */
779 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
780
781 /* Lookup this by interface index. */
Feng Luc99f3482014-10-16 09:52:36 +0800782 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
783 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
784 vrf_id);
paul718e3742002-12-13 20:15:29 +0000785
786 /* If such interface does not exist, indicate an error */
787 if (! ifp)
788 return NULL;
789
Josh Bailey51d4ef82012-03-21 17:13:39 -0700790 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000791
792 return ifp;
793}
794
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200795static void
796link_params_set_value(struct stream *s, struct if_link_params *iflp)
797{
798
799 if (iflp == NULL)
800 return;
801
802 iflp->lp_status = stream_getl (s);
803 iflp->te_metric = stream_getl (s);
804 iflp->max_bw = stream_getf (s);
805 iflp->max_rsv_bw = stream_getf (s);
806 uint32_t bwclassnum = stream_getl (s);
807 {
808 unsigned int i;
809 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
810 iflp->unrsv_bw[i] = stream_getf (s);
811 if (i < bwclassnum)
812 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
813 " - outdated library?",
814 __func__, bwclassnum, MAX_CLASS_TYPE);
815 }
816 iflp->admin_grp = stream_getl (s);
817 iflp->rmt_as = stream_getl (s);
818 iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
819
820 iflp->av_delay = stream_getl (s);
821 iflp->min_delay = stream_getl (s);
822 iflp->max_delay = stream_getl (s);
823 iflp->delay_var = stream_getl (s);
824
825 iflp->pkt_loss = stream_getf (s);
826 iflp->res_bw = stream_getf (s);
827 iflp->ava_bw = stream_getf (s);
828 iflp->use_bw = stream_getf (s);
829}
830
831struct interface *
832zebra_interface_link_params_read (struct stream *s)
833{
834 struct if_link_params *iflp;
835 uint32_t ifindex = stream_getl (s);
836
837 struct interface *ifp = if_lookup_by_index (ifindex);
838
839 if (ifp == NULL || s == NULL)
840 {
841 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
842 __func__, ifindex);
843 return NULL;
844 }
845
846 if ((iflp = if_link_params_get (ifp)) == NULL)
847 return NULL;
848
849 link_params_set_value(s, iflp);
850
851 return ifp;
852}
853
854void
855zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
856{
857 u_char link_params_status = 0;
858
859 /* Read interface's index. */
860 ifp->ifindex = stream_getl (s);
861 ifp->status = stream_getc (s);
862
863 /* Read interface's value. */
864 ifp->flags = stream_getq (s);
865 ifp->metric = stream_getl (s);
866 ifp->mtu = stream_getl (s);
867 ifp->mtu6 = stream_getl (s);
868 ifp->bandwidth = stream_getl (s);
869 ifp->ll_type = stream_getl (s);
870 ifp->hw_addr_len = stream_getl (s);
871 if (ifp->hw_addr_len)
872 stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
873
874 /* Read Traffic Engineering status */
875 link_params_status = stream_getc (s);
876 /* Then, Traffic Engineering parameters if any */
877 if (link_params_status)
878 {
879 struct if_link_params *iflp = if_link_params_get (ifp);
880 link_params_set_value(s, iflp);
881 }
882}
883
884size_t
885zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
886{
887 size_t w;
888 struct if_link_params *iflp;
Paul Jakma3676cb02016-07-29 13:39:37 +0100889 int i;
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200890
891 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
892 return 0;
893
894 iflp = ifp->link_params;
895 w = 0;
896
897 w += stream_putl (s, iflp->lp_status);
898
899 w += stream_putl (s, iflp->te_metric);
900 w += stream_putf (s, iflp->max_bw);
901 w += stream_putf (s, iflp->max_rsv_bw);
902
903 w += stream_putl (s, MAX_CLASS_TYPE);
Paul Jakma3676cb02016-07-29 13:39:37 +0100904 for (i = 0; i < MAX_CLASS_TYPE; i++)
Olivier Dugeonae51c9d2016-04-19 16:21:46 +0200905 w += stream_putf (s, iflp->unrsv_bw[i]);
906
907 w += stream_putl (s, iflp->admin_grp);
908 w += stream_putl (s, iflp->rmt_as);
909 w += stream_put_in_addr (s, &iflp->rmt_ip);
910
911 w += stream_putl (s, iflp->av_delay);
912 w += stream_putl (s, iflp->min_delay);
913 w += stream_putl (s, iflp->max_delay);
914 w += stream_putl (s, iflp->delay_var);
915
916 w += stream_putf (s, iflp->pkt_loss);
917 w += stream_putf (s, iflp->res_bw);
918 w += stream_putf (s, iflp->ava_bw);
919 w += stream_putf (s, iflp->use_bw);
920
921 return w;
922}
923
924/*
paul0a589352004-05-08 11:48:26 +0000925 * format of message for address additon is:
926 * 0
927 * 0 1 2 3 4 5 6 7
928 * +-+-+-+-+-+-+-+-+
929 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
930 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
931 * | |
932 * + +
933 * | ifindex |
934 * + +
935 * | |
936 * + +
937 * | |
938 * +-+-+-+-+-+-+-+-+
939 * | ifc_flags | flags for connected address
940 * +-+-+-+-+-+-+-+-+
941 * | addr_family |
942 * +-+-+-+-+-+-+-+-+
943 * | addr... |
944 * : :
945 * | |
946 * +-+-+-+-+-+-+-+-+
947 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
948 * +-+-+-+-+-+-+-+-+
949 * | daddr.. |
950 * : :
951 * | |
952 * +-+-+-+-+-+-+-+-+
paul0a589352004-05-08 11:48:26 +0000953 */
954
hasso3fb9cd62004-10-19 19:44:43 +0000955static int
956memconstant(const void *s, int c, size_t n)
957{
958 const u_char *p = s;
959
960 while (n-- > 0)
961 if (*p++ != c)
962 return 0;
963 return 1;
964}
965
paul718e3742002-12-13 20:15:29 +0000966struct connected *
Feng Luc99f3482014-10-16 09:52:36 +0800967zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000968{
Paul Jakma9099f9b2016-01-18 10:12:10 +0000969 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000970 struct interface *ifp;
971 struct connected *ifc;
Paul Jakmabf83fa22016-02-09 15:23:03 +0000972 struct prefix p, d, *dp;
paul718e3742002-12-13 20:15:29 +0000973 int plen;
paul0a589352004-05-08 11:48:26 +0000974 u_char ifc_flags;
975
976 memset (&p, 0, sizeof(p));
977 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000978
979 /* Get interface index. */
980 ifindex = stream_getl (s);
981
982 /* Lookup index. */
Feng Luc99f3482014-10-16 09:52:36 +0800983 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
paul718e3742002-12-13 20:15:29 +0000984 if (ifp == NULL)
985 {
paul0a589352004-05-08 11:48:26 +0000986 zlog_warn ("zebra_interface_address_read(%s): "
987 "Can't find interface by ifindex: %d ",
988 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
989 ifindex);
paul718e3742002-12-13 20:15:29 +0000990 return NULL;
991 }
992
993 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000994 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000995
996 /* Fetch interface address. */
Paul Jakmabf83fa22016-02-09 15:23:03 +0000997 d.family = p.family = stream_getc (s);
998 plen = prefix_blen (&d);
999
1000 zclient_stream_get_prefix (s, &p);
paul718e3742002-12-13 20:15:29 +00001001
1002 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +00001003 stream_get (&d.u.prefix, s, plen);
Paul Jakmabf83fa22016-02-09 15:23:03 +00001004
1005 /* N.B. NULL destination pointers are encoded as all zeroes */
1006 dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
1007
paul0a589352004-05-08 11:48:26 +00001008 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1009 {
hasso3fb9cd62004-10-19 19:44:43 +00001010 /* N.B. NULL destination pointers are encoded as all zeroes */
Paul Jakmabf83fa22016-02-09 15:23:03 +00001011 ifc = connected_add_by_prefix(ifp, &p, dp);
paul0a589352004-05-08 11:48:26 +00001012 if (ifc != NULL)
Andrew J. Schorre4529632006-12-12 19:18:21 +00001013 {
1014 ifc->flags = ifc_flags;
1015 if (ifc->destination)
1016 ifc->destination->prefixlen = ifc->address->prefixlen;
David Lamparter90444ca2014-07-01 16:14:05 +02001017 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1018 {
1019 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
Timo Teräs41eb9a42015-05-23 11:08:39 +03001020 char buf[PREFIX_STRLEN];
David Lamparter90444ca2014-07-01 16:14:05 +02001021 zlog_warn("warning: interface %s address %s "
1022 "with peer flag set, but no peer address!",
Timo Teräs41eb9a42015-05-23 11:08:39 +03001023 ifp->name,
1024 prefix2str (ifc->address, buf, sizeof buf));
David Lamparter90444ca2014-07-01 16:14:05 +02001025 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1026 }
Andrew J. Schorre4529632006-12-12 19:18:21 +00001027 }
paul0a589352004-05-08 11:48:26 +00001028 }
1029 else
1030 {
1031 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1032 ifc = connected_delete_by_prefix(ifp, &p);
1033 }
paul718e3742002-12-13 20:15:29 +00001034
1035 return ifc;
1036}
paul0a589352004-05-08 11:48:26 +00001037
David Lamparter6b0655a2014-06-04 06:53:35 +02001038
paul718e3742002-12-13 20:15:29 +00001039/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +00001040static int
paul718e3742002-12-13 20:15:29 +00001041zclient_read (struct thread *thread)
1042{
ajs634f9ea2005-04-11 15:51:40 +00001043 size_t already;
paulc1b98002006-01-16 01:54:02 +00001044 uint16_t length, command;
1045 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001046 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001047 struct zclient *zclient;
1048
1049 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +00001050 zclient = THREAD_ARG (thread);
1051 zclient->t_read = NULL;
1052
ajs634f9ea2005-04-11 15:51:40 +00001053 /* Read zebra header (if we don't have it already). */
1054 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001055 {
ajs634f9ea2005-04-11 15:51:40 +00001056 ssize_t nbyte;
1057 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1058 ZEBRA_HEADER_SIZE-already)) == 0) ||
1059 (nbyte == -1))
1060 {
1061 if (zclient_debug)
1062 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1063 return zclient_failed(zclient);
1064 }
1065 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1066 {
1067 /* Try again later. */
1068 zclient_event (ZCLIENT_READ, zclient);
1069 return 0;
1070 }
1071 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +00001072 }
1073
ajs634f9ea2005-04-11 15:51:40 +00001074 /* Reset to read from the beginning of the incoming packet. */
1075 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +00001076
paulc1b98002006-01-16 01:54:02 +00001077 /* Fetch header values. */
paul718e3742002-12-13 20:15:29 +00001078 length = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +00001079 marker = stream_getc (zclient->ibuf);
1080 version = stream_getc (zclient->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001081 vrf_id = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +00001082 command = stream_getw (zclient->ibuf);
1083
1084 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1085 {
1086 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1087 __func__, zclient->sock, marker, version);
1088 return zclient_failed(zclient);
1089 }
1090
ajs634f9ea2005-04-11 15:51:40 +00001091 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001092 {
ajs634f9ea2005-04-11 15:51:40 +00001093 zlog_err("%s: socket %d message length %u is less than %d ",
1094 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1095 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +00001096 }
ajs634f9ea2005-04-11 15:51:40 +00001097
1098 /* Length check. */
1099 if (length > STREAM_SIZE(zclient->ibuf))
1100 {
1101 struct stream *ns;
1102 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1103 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1104 ns = stream_new(length);
1105 stream_copy(ns, zclient->ibuf);
1106 stream_free (zclient->ibuf);
1107 zclient->ibuf = ns;
1108 }
paul718e3742002-12-13 20:15:29 +00001109
1110 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +00001111 if (already < length)
1112 {
1113 ssize_t nbyte;
1114 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1115 length-already)) == 0) ||
1116 (nbyte == -1))
1117 {
1118 if (zclient_debug)
1119 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1120 return zclient_failed(zclient);
1121 }
1122 if (nbyte != (ssize_t)(length-already))
1123 {
1124 /* Try again later. */
1125 zclient_event (ZCLIENT_READ, zclient);
1126 return 0;
1127 }
1128 }
1129
1130 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +00001131
paul0a589352004-05-08 11:48:26 +00001132 if (zclient_debug)
Feng Luc99f3482014-10-16 09:52:36 +08001133 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
paul0a589352004-05-08 11:48:26 +00001134
paul718e3742002-12-13 20:15:29 +00001135 switch (command)
1136 {
hasso18a6dce2004-10-03 18:18:34 +00001137 case ZEBRA_ROUTER_ID_UPDATE:
1138 if (zclient->router_id_update)
Feng Luc99f3482014-10-16 09:52:36 +08001139 (*zclient->router_id_update) (command, zclient, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001140 break;
paul718e3742002-12-13 20:15:29 +00001141 case ZEBRA_INTERFACE_ADD:
1142 if (zclient->interface_add)
Feng Luc99f3482014-10-16 09:52:36 +08001143 (*zclient->interface_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001144 break;
1145 case ZEBRA_INTERFACE_DELETE:
1146 if (zclient->interface_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001147 (*zclient->interface_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001148 break;
1149 case ZEBRA_INTERFACE_ADDRESS_ADD:
1150 if (zclient->interface_address_add)
Feng Luc99f3482014-10-16 09:52:36 +08001151 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001152 break;
1153 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1154 if (zclient->interface_address_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001155 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001156 break;
1157 case ZEBRA_INTERFACE_UP:
1158 if (zclient->interface_up)
Feng Luc99f3482014-10-16 09:52:36 +08001159 (*zclient->interface_up) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001160 break;
1161 case ZEBRA_INTERFACE_DOWN:
1162 if (zclient->interface_down)
Feng Luc99f3482014-10-16 09:52:36 +08001163 (*zclient->interface_down) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001164 break;
1165 case ZEBRA_IPV4_ROUTE_ADD:
1166 if (zclient->ipv4_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001167 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001168 break;
1169 case ZEBRA_IPV4_ROUTE_DELETE:
1170 if (zclient->ipv4_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001171 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001172 break;
1173 case ZEBRA_IPV6_ROUTE_ADD:
1174 if (zclient->ipv6_route_add)
Feng Luc99f3482014-10-16 09:52:36 +08001175 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001176 break;
1177 case ZEBRA_IPV6_ROUTE_DELETE:
1178 if (zclient->ipv6_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +08001179 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001180 break;
Olivier Dugeonae51c9d2016-04-19 16:21:46 +02001181 case ZEBRA_INTERFACE_LINK_PARAMS:
1182 if (zclient->interface_link_params)
1183 (*zclient->interface_link_params) (command, zclient, length);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001184 case ZEBRA_NEXTHOP_UPDATE:
1185 if (zclient->nexthop_update)
1186 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
Olivier Dugeonae51c9d2016-04-19 16:21:46 +02001187 break;
paul718e3742002-12-13 20:15:29 +00001188 default:
1189 break;
1190 }
1191
ajs634f9ea2005-04-11 15:51:40 +00001192 if (zclient->sock < 0)
1193 /* Connection was closed during packet processing. */
1194 return -1;
1195
paul718e3742002-12-13 20:15:29 +00001196 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +00001197 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +00001198 zclient_event (ZCLIENT_READ, zclient);
1199
1200 return 0;
1201}
1202
1203void
Feng Luc99f3482014-10-16 09:52:36 +08001204zclient_redistribute (int command, struct zclient *zclient, int type,
1205 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001206{
paul718e3742002-12-13 20:15:29 +00001207
paul0a589352004-05-08 11:48:26 +00001208 if (command == ZEBRA_REDISTRIBUTE_ADD)
1209 {
Feng Luc99f3482014-10-16 09:52:36 +08001210 if (vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001211 return;
Feng Luc99f3482014-10-16 09:52:36 +08001212 vrf_bitmap_set (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001213 }
1214 else
1215 {
Feng Luc99f3482014-10-16 09:52:36 +08001216 if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001217 return;
Feng Luc99f3482014-10-16 09:52:36 +08001218 vrf_bitmap_unset (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001219 }
paul718e3742002-12-13 20:15:29 +00001220
1221 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001222 zebra_redistribute_send (command, zclient, type, vrf_id);
paul718e3742002-12-13 20:15:29 +00001223}
1224
paul0a589352004-05-08 11:48:26 +00001225
paul718e3742002-12-13 20:15:29 +00001226void
Feng Luc99f3482014-10-16 09:52:36 +08001227zclient_redistribute_default (int command, struct zclient *zclient,
1228 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001229{
paul718e3742002-12-13 20:15:29 +00001230
paul0a589352004-05-08 11:48:26 +00001231 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1232 {
Feng Luc99f3482014-10-16 09:52:36 +08001233 if (vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001234 return;
Feng Luc99f3482014-10-16 09:52:36 +08001235 vrf_bitmap_set (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001236 }
1237 else
1238 {
Feng Luc99f3482014-10-16 09:52:36 +08001239 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001240 return;
Feng Luc99f3482014-10-16 09:52:36 +08001241 vrf_bitmap_unset (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001242 }
paul718e3742002-12-13 20:15:29 +00001243
1244 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001245 zebra_message_send (zclient, command, vrf_id);
paul718e3742002-12-13 20:15:29 +00001246}
1247
paul718e3742002-12-13 20:15:29 +00001248static void
1249zclient_event (enum event event, struct zclient *zclient)
1250{
1251 switch (event)
1252 {
1253 case ZCLIENT_SCHEDULE:
1254 if (! zclient->t_connect)
1255 zclient->t_connect =
Donald Sharp71252932015-09-24 09:25:19 -04001256 thread_add_event (zclient->master, zclient_connect, zclient, 0);
paul718e3742002-12-13 20:15:29 +00001257 break;
1258 case ZCLIENT_CONNECT:
1259 if (zclient->fail >= 10)
1260 return;
1261 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +00001262 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +00001263 zclient->fail < 3 ? 10 : 60);
1264 if (! zclient->t_connect)
1265 zclient->t_connect =
Donald Sharp71252932015-09-24 09:25:19 -04001266 thread_add_timer (zclient->master, zclient_connect, zclient,
paul718e3742002-12-13 20:15:29 +00001267 zclient->fail < 3 ? 10 : 60);
1268 break;
1269 case ZCLIENT_READ:
1270 zclient->t_read =
Donald Sharp71252932015-09-24 09:25:19 -04001271 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
paul718e3742002-12-13 20:15:29 +00001272 break;
1273 }
1274}
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001275
David Lamparterfd8f6eb2015-03-03 08:57:02 +01001276const char *zclient_serv_path_get()
Everton Marques1f298942014-08-21 15:47:28 -03001277{
1278 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
1279}
1280
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001281void
1282zclient_serv_path_set (char *path)
1283{
1284 struct stat sb;
1285
1286 /* reset */
1287 zclient_serv_path = NULL;
1288
1289 /* test if `path' is socket. don't set it otherwise. */
1290 if (stat(path, &sb) == -1)
1291 {
1292 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1293 return;
1294 }
1295
1296 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1297 {
1298 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1299 return;
1300 }
1301
1302 /* it seems that path is unix socket */
1303 zclient_serv_path = path;
1304}
1305