blob: 8e443e28d26f08c720a5d6a29189e8da07e9edf8 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
ajs634f9ea2005-04-11 15:51:40 +00003 * Copyright (C) 2005 Andrew J. Schorr
paul718e3742002-12-13 20:15:29 +00004 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "stream.h"
ajs634f9ea2005-04-11 15:51:40 +000027#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000028#include "network.h"
29#include "if.h"
30#include "log.h"
31#include "thread.h"
32#include "zclient.h"
33#include "memory.h"
34#include "table.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020035
paul718e3742002-12-13 20:15:29 +000036/* Zebra client events. */
37enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
38
39/* Prototype for event manager. */
40static void zclient_event (enum event, struct zclient *);
41
ajs634f9ea2005-04-11 15:51:40 +000042extern struct thread_master *master;
43
David Lamparterfd8f6eb2015-03-03 08:57:02 +010044const char *zclient_serv_path = NULL;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040045
paul718e3742002-12-13 20:15:29 +000046/* This file local debug flag. */
47int zclient_debug = 0;
David Lamparter6b0655a2014-06-04 06:53:35 +020048
paul718e3742002-12-13 20:15:29 +000049/* Allocate zclient structure. */
50struct zclient *
51zclient_new ()
52{
53 struct zclient *zclient;
Stephen Hemminger393deb92008-08-18 14:13:29 -070054 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
paul718e3742002-12-13 20:15:29 +000055
56 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
57 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs634f9ea2005-04-11 15:51:40 +000058 zclient->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +000059
60 return zclient;
61}
62
Chris Caputo228da422009-07-18 05:44:03 +000063/* This function is only called when exiting, because
ajs634f9ea2005-04-11 15:51:40 +000064 many parts of the code do not check for I/O errors, so they could
65 reference an invalid pointer if the structure was ever freed.
ajs634f9ea2005-04-11 15:51:40 +000066
Chris Caputo228da422009-07-18 05:44:03 +000067 Free zclient structure. */
paul718e3742002-12-13 20:15:29 +000068void
69zclient_free (struct zclient *zclient)
70{
ajs634f9ea2005-04-11 15:51:40 +000071 if (zclient->ibuf)
72 stream_free(zclient->ibuf);
73 if (zclient->obuf)
74 stream_free(zclient->obuf);
75 if (zclient->wb)
76 buffer_free(zclient->wb);
77
paul718e3742002-12-13 20:15:29 +000078 XFREE (MTYPE_ZCLIENT, zclient);
79}
80
81/* Initialize zebra client. Argument redist_default is unwanted
82 redistribute route type. */
83void
84zclient_init (struct zclient *zclient, int redist_default)
85{
86 int i;
87
88 /* Enable zebra client connection by default. */
89 zclient->enable = 1;
90
91 /* Set -1 to the default socket value. */
92 zclient->sock = -1;
93
94 /* Clear redistribution flags. */
95 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
Feng Luc99f3482014-10-16 09:52:36 +080096 zclient->redist[i] = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +000097
98 /* Set unwanted redistribute route. bgpd does not need BGP route
99 redistribution. */
100 zclient->redist_default = redist_default;
paul718e3742002-12-13 20:15:29 +0000101
102 /* Set default-information redistribute to zero. */
Feng Luc99f3482014-10-16 09:52:36 +0800103 zclient->default_information = vrf_bitmap_init ();
paul718e3742002-12-13 20:15:29 +0000104
105 /* Schedule first zclient connection. */
106 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000107 zlog_debug ("zclient start scheduled");
paul718e3742002-12-13 20:15:29 +0000108
109 zclient_event (ZCLIENT_SCHEDULE, zclient);
110}
111
112/* Stop zebra client services. */
113void
114zclient_stop (struct zclient *zclient)
115{
116 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000117 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000118
119 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000120 THREAD_OFF(zclient->t_read);
121 THREAD_OFF(zclient->t_connect);
122 THREAD_OFF(zclient->t_write);
123
124 /* Reset streams. */
125 stream_reset(zclient->ibuf);
126 stream_reset(zclient->obuf);
127
128 /* Empty the write buffer. */
129 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000130
131 /* Close socket. */
132 if (zclient->sock >= 0)
133 {
134 close (zclient->sock);
135 zclient->sock = -1;
136 }
137 zclient->fail = 0;
138}
139
140void
141zclient_reset (struct zclient *zclient)
142{
143 zclient_stop (zclient);
144 zclient_init (zclient, zclient->redist_default);
145}
146
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400147#ifdef HAVE_TCP_ZEBRA
148
paul718e3742002-12-13 20:15:29 +0000149/* Make socket to zebra daemon. Return zebra socket. */
Everton Marques1a9352a2014-09-23 14:33:34 -0300150static int
ajs634f9ea2005-04-11 15:51:40 +0000151zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000152{
153 int sock;
154 int ret;
155 struct sockaddr_in serv;
156
157 /* We should think about IPv6 connection. */
158 sock = socket (AF_INET, SOCK_STREAM, 0);
159 if (sock < 0)
160 return -1;
161
162 /* Make server socket. */
163 memset (&serv, 0, sizeof (struct sockaddr_in));
164 serv.sin_family = AF_INET;
165 serv.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000166#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000167 serv.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000168#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000169 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
170
171 /* Connect to zebra. */
172 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
173 if (ret < 0)
174 {
175 close (sock);
176 return -1;
177 }
178 return sock;
179}
180
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400181#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400182
paul718e3742002-12-13 20:15:29 +0000183/* For sockaddr_un. */
184#include <sys/un.h>
185
Everton Marques1a9352a2014-09-23 14:33:34 -0300186static int
hasso8c328f12004-10-05 21:01:23 +0000187zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000188{
189 int ret;
190 int sock, len;
191 struct sockaddr_un addr;
192
193 sock = socket (AF_UNIX, SOCK_STREAM, 0);
194 if (sock < 0)
195 return -1;
196
197 /* Make server socket. */
198 memset (&addr, 0, sizeof (struct sockaddr_un));
199 addr.sun_family = AF_UNIX;
200 strncpy (addr.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000201#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +0000202 len = addr.sun_len = SUN_LEN(&addr);
203#else
204 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000205#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +0000206
207 ret = connect (sock, (struct sockaddr *) &addr, len);
208 if (ret < 0)
209 {
210 close (sock);
211 return -1;
212 }
213 return sock;
214}
215
Vyacheslav Trushkin3414d032011-11-30 21:03:44 +0400216#endif /* HAVE_TCP_ZEBRA */
217
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400218/**
219 * Connect to zebra daemon.
220 * @param zclient a pointer to zclient structure
221 * @return socket fd just to make sure that connection established
222 * @see zclient_init
223 * @see zclient_new
224 */
225int
226zclient_socket_connect (struct zclient *zclient)
227{
228#ifdef HAVE_TCP_ZEBRA
229 zclient->sock = zclient_socket ();
230#else
Everton Marques1f298942014-08-21 15:47:28 -0300231 zclient->sock = zclient_socket_un (zclient_serv_path_get());
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400232#endif
233 return zclient->sock;
234}
235
ajs634f9ea2005-04-11 15:51:40 +0000236static int
237zclient_failed(struct zclient *zclient)
238{
239 zclient->fail++;
240 zclient_stop(zclient);
241 zclient_event(ZCLIENT_CONNECT, zclient);
242 return -1;
243}
244
245static int
246zclient_flush_data(struct thread *thread)
247{
248 struct zclient *zclient = THREAD_ARG(thread);
249
250 zclient->t_write = NULL;
251 if (zclient->sock < 0)
252 return -1;
253 switch (buffer_flush_available(zclient->wb, zclient->sock))
254 {
255 case BUFFER_ERROR:
256 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
257 __func__, zclient->sock);
258 return zclient_failed(zclient);
259 break;
260 case BUFFER_PENDING:
261 zclient->t_write = thread_add_write(master, zclient_flush_data,
262 zclient, zclient->sock);
263 break;
264 case BUFFER_EMPTY:
265 break;
266 }
267 return 0;
268}
269
paul718e3742002-12-13 20:15:29 +0000270int
ajs634f9ea2005-04-11 15:51:40 +0000271zclient_send_message(struct zclient *zclient)
272{
273 if (zclient->sock < 0)
274 return -1;
275 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
276 stream_get_endp(zclient->obuf)))
277 {
278 case BUFFER_ERROR:
279 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
280 __func__, zclient->sock);
281 return zclient_failed(zclient);
282 break;
283 case BUFFER_EMPTY:
284 THREAD_OFF(zclient->t_write);
285 break;
286 case BUFFER_PENDING:
287 THREAD_WRITE_ON(master, zclient->t_write,
288 zclient_flush_data, zclient, zclient->sock);
289 break;
290 }
291 return 0;
292}
293
pauld2110862006-01-17 17:43:18 +0000294void
Feng Luc99f3482014-10-16 09:52:36 +0800295zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000296{
297 /* length placeholder, caller can update */
298 stream_putw (s, ZEBRA_HEADER_SIZE);
299 stream_putc (s, ZEBRA_HEADER_MARKER);
300 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800301 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000302 stream_putw (s, command);
303}
304
ajs634f9ea2005-04-11 15:51:40 +0000305/* Send simple Zebra message. */
306static int
Feng Luc99f3482014-10-16 09:52:36 +0800307zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000308{
309 struct stream *s;
310
311 /* Get zclient output buffer. */
312 s = zclient->obuf;
313 stream_reset (s);
314
315 /* Send very simple command only Zebra message. */
Feng Luc99f3482014-10-16 09:52:36 +0800316 zclient_create_header (s, command, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000317
ajs634f9ea2005-04-11 15:51:40 +0000318 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000319}
320
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400321static int
322zebra_hello_send (struct zclient *zclient)
323{
324 struct stream *s;
325
326 if (zclient->redist_default)
327 {
328 s = zclient->obuf;
329 stream_reset (s);
330
Feng Luc99f3482014-10-16 09:52:36 +0800331 /* The VRF ID in the HELLO message is always 0. */
332 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400333 stream_putc (s, zclient->redist_default);
334 stream_putw_at (s, 0, stream_get_endp (s));
335 return zclient_send_message(zclient);
336 }
337
338 return 0;
339}
340
Feng Luc99f3482014-10-16 09:52:36 +0800341/* Send requests to zebra daemon for the information in a VRF. */
342void
343zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
344{
345 int i;
346
347 /* zclient is disabled. */
348 if (! zclient->enable)
349 return;
350
351 /* If not connected to the zebra yet. */
352 if (zclient->sock < 0)
353 return;
354
355 if (zclient_debug)
356 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
357
358 /* We need router-id information. */
359 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
360
361 /* We need interface information. */
362 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
363
364 /* Set unwanted redistribute route. */
365 vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
366
367 /* Flush all redistribute request. */
368 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
369 if (i != zclient->redist_default &&
370 vrf_bitmap_check (zclient->redist[i], vrf_id))
371 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
372
373 /* If default information is needed. */
374 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
375 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
376}
377
paul718e3742002-12-13 20:15:29 +0000378/* Make connection to zebra daemon. */
379int
380zclient_start (struct zclient *zclient)
381{
paul718e3742002-12-13 20:15:29 +0000382 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000383 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000384
385 /* zclient is disabled. */
386 if (! zclient->enable)
387 return 0;
388
389 /* If already connected to the zebra. */
390 if (zclient->sock >= 0)
391 return 0;
392
393 /* Check connect thread. */
394 if (zclient->t_connect)
395 return 0;
396
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400397 if (zclient_socket_connect(zclient) < 0)
paul718e3742002-12-13 20:15:29 +0000398 {
399 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000400 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000401 zclient->fail++;
402 zclient_event (ZCLIENT_CONNECT, zclient);
403 return -1;
404 }
405
ajs634f9ea2005-04-11 15:51:40 +0000406 if (set_nonblocking(zclient->sock) < 0)
407 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
408
paul718e3742002-12-13 20:15:29 +0000409 /* Clear fail count. */
410 zclient->fail = 0;
411 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000412 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000413
414 /* Create read thread. */
415 zclient_event (ZCLIENT_READ, zclient);
416
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400417 zebra_hello_send (zclient);
418
Feng Luc99f3482014-10-16 09:52:36 +0800419 /* Inform the successful connection. */
420 if (zclient->zebra_connected)
421 (*zclient->zebra_connected) (zclient);
paul718e3742002-12-13 20:15:29 +0000422
423 return 0;
424}
425
426/* This function is a wrapper function for calling zclient_start from
427 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000428static int
paul718e3742002-12-13 20:15:29 +0000429zclient_connect (struct thread *t)
430{
431 struct zclient *zclient;
432
433 zclient = THREAD_ARG (t);
434 zclient->t_connect = NULL;
435
436 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000437 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000438
439 return zclient_start (zclient);
440}
David Lamparter6b0655a2014-06-04 06:53:35 +0200441
paul0a589352004-05-08 11:48:26 +0000442 /*
443 * "xdr_encode"-like interface that allows daemon (client) to send
444 * a message to zebra server for a route that needs to be
445 * added/deleted to the kernel. Info about the route is specified
446 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
447 * the info down the zclient socket using the stream_* functions.
448 *
449 * The corresponding read ("xdr_decode") function on the server
450 * side is zread_ipv4_add()/zread_ipv4_delete().
451 *
452 * 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
453 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
454 * | Length (2) | Command | Route Type |
455 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
456 * | ZEBRA Flags | Message Flags | Prefix length |
457 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
458 * | Destination IPv4 Prefix for route |
459 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
460 * | Nexthop count |
461 * +-+-+-+-+-+-+-+-+
462 *
463 *
464 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
465 * described, as per the Nexthop count. Each nexthop described as:
466 *
467 * +-+-+-+-+-+-+-+-+
468 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
469 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * | IPv4 Nexthop address or Interface Index number |
471 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
472 *
473 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
474 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
475 * nexthop information is provided, and the message describes a prefix
476 * to blackhole or reject route.
477 *
478 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
479 * byte value.
480 *
481 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
482 * byte value.
483 *
484 * XXX: No attention paid to alignment.
485 */
paul718e3742002-12-13 20:15:29 +0000486int
paul0a589352004-05-08 11:48:26 +0000487zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
488 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000489{
490 int i;
491 int psize;
492 struct stream *s;
493
494 /* Reset stream. */
495 s = zclient->obuf;
496 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800497
498 zclient_create_header (s, cmd, api->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000499
500 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000501 stream_putc (s, api->type);
502 stream_putc (s, api->flags);
503 stream_putc (s, api->message);
G.Balaji5a616c02011-11-26 21:58:42 +0400504 stream_putw (s, api->safi);
paul0a589352004-05-08 11:48:26 +0000505
paul718e3742002-12-13 20:15:29 +0000506 /* Put prefix information. */
507 psize = PSIZE (p->prefixlen);
508 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000509 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000510
511 /* Nexthop, ifindex, distance and metric information. */
512 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
513 {
paul595db7f2003-05-25 21:35:06 +0000514 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
515 {
516 stream_putc (s, 1);
517 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000518 /* XXX assert(api->nexthop_num == 0); */
519 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000520 }
521 else
522 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000523
524 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000525 {
526 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
527 stream_put_in_addr (s, api->nexthop[i]);
528 }
paul718e3742002-12-13 20:15:29 +0000529 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000530 {
531 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
532 stream_putl (s, api->ifindex[i]);
533 }
paul718e3742002-12-13 20:15:29 +0000534 }
535
536 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
537 stream_putc (s, api->distance);
538 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
539 stream_putl (s, api->metric);
540
541 /* Put length at the first point of the stream. */
542 stream_putw_at (s, 0, stream_get_endp (s));
543
ajs634f9ea2005-04-11 15:51:40 +0000544 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000545}
546
547#ifdef HAVE_IPV6
548int
paul0a589352004-05-08 11:48:26 +0000549zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000550 struct zapi_ipv6 *api)
551{
552 int i;
553 int psize;
554 struct stream *s;
555
556 /* Reset stream. */
557 s = zclient->obuf;
558 stream_reset (s);
559
Feng Luc99f3482014-10-16 09:52:36 +0800560 zclient_create_header (s, cmd, api->vrf_id);
paul718e3742002-12-13 20:15:29 +0000561
paulc1b98002006-01-16 01:54:02 +0000562 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000563 stream_putc (s, api->type);
564 stream_putc (s, api->flags);
565 stream_putc (s, api->message);
G.Balajic7ec1792011-11-26 22:04:05 +0400566 stream_putw (s, api->safi);
paul718e3742002-12-13 20:15:29 +0000567
568 /* Put prefix information. */
569 psize = PSIZE (p->prefixlen);
570 stream_putc (s, p->prefixlen);
571 stream_write (s, (u_char *)&p->prefix, psize);
572
573 /* Nexthop, ifindex, distance and metric information. */
574 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
575 {
576 stream_putc (s, api->nexthop_num + api->ifindex_num);
577
578 for (i = 0; i < api->nexthop_num; i++)
579 {
580 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
581 stream_write (s, (u_char *)api->nexthop[i], 16);
582 }
583 for (i = 0; i < api->ifindex_num; i++)
584 {
585 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
586 stream_putl (s, api->ifindex[i]);
587 }
588 }
589
590 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
591 stream_putc (s, api->distance);
592 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
593 stream_putl (s, api->metric);
594
595 /* Put length at the first point of the stream. */
596 stream_putw_at (s, 0, stream_get_endp (s));
597
ajs634f9ea2005-04-11 15:51:40 +0000598 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000599}
paul718e3742002-12-13 20:15:29 +0000600#endif /* HAVE_IPV6 */
601
paul0a589352004-05-08 11:48:26 +0000602/*
603 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
604 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
605 * then set/unset redist[type] in the client handle (a struct zserv) for the
606 * sending client
607 */
paul718e3742002-12-13 20:15:29 +0000608int
Feng Luc99f3482014-10-16 09:52:36 +0800609zebra_redistribute_send (int command, struct zclient *zclient, int type,
610 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000611{
paul718e3742002-12-13 20:15:29 +0000612 struct stream *s;
613
ajs634f9ea2005-04-11 15:51:40 +0000614 s = zclient->obuf;
615 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000616
Feng Luc99f3482014-10-16 09:52:36 +0800617 zclient_create_header (s, command, vrf_id);
paul718e3742002-12-13 20:15:29 +0000618 stream_putc (s, type);
paulc1b98002006-01-16 01:54:02 +0000619
620 stream_putw_at (s, 0, stream_get_endp (s));
621
ajs634f9ea2005-04-11 15:51:40 +0000622 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000623}
624
hasso18a6dce2004-10-03 18:18:34 +0000625/* Router-id update from zebra daemon. */
626void
627zebra_router_id_update_read (struct stream *s, struct prefix *rid)
628{
629 int plen;
630
631 /* Fetch interface address. */
632 rid->family = stream_getc (s);
633
634 plen = prefix_blen (rid);
635 stream_get (&rid->u.prefix, s, plen);
636 rid->prefixlen = stream_getc (s);
637}
638
paul718e3742002-12-13 20:15:29 +0000639/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000640/*
641 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
642 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
643 * 0 1 2 3
644 * 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
645 * +-+-+-+-+-+-+-+-+
646 * | type |
647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 * | ifname |
649 * | |
650 * | |
651 * | |
652 * | |
653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
654 * | ifindex |
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * | if_flags |
paulc77d4542006-01-11 01:59:04 +0000657 * | |
paul0a589352004-05-08 11:48:26 +0000658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 * | metric |
660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661 * | ifmtu |
662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663 * | ifmtu6 |
664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665 * | bandwidth |
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * | sockaddr_dl |
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 */
670
paul718e3742002-12-13 20:15:29 +0000671struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800672zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000673{
674 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000675 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000676
677 /* Read interface name. */
678 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
679
ajsa3491982005-04-02 22:50:38 +0000680 /* Lookup/create interface by name. */
Feng Luc99f3482014-10-16 09:52:36 +0800681 ifp = if_get_by_name_len_vrf (ifname_tmp,
682 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
683 vrf_id);
paul718e3742002-12-13 20:15:29 +0000684
Josh Bailey51d4ef82012-03-21 17:13:39 -0700685 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000686
paul718e3742002-12-13 20:15:29 +0000687 return ifp;
688}
689
paul0a589352004-05-08 11:48:26 +0000690/*
691 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
692 * from zebra server. The format of this message is the same as
693 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
694 * comments for zebra_interface_add_read), except that no sockaddr_dl
695 * is sent at the tail of the message.
696 */
paul718e3742002-12-13 20:15:29 +0000697struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800698zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000699{
700 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000701 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000702
703 /* Read interface name. */
704 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
705
706 /* Lookup this by interface index. */
Feng Luc99f3482014-10-16 09:52:36 +0800707 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
708 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
709 vrf_id);
paul718e3742002-12-13 20:15:29 +0000710
711 /* If such interface does not exist, indicate an error */
712 if (! ifp)
713 return NULL;
714
Josh Bailey51d4ef82012-03-21 17:13:39 -0700715 zebra_interface_if_set_value (s, ifp);
paul718e3742002-12-13 20:15:29 +0000716
717 return ifp;
718}
719
paul0a589352004-05-08 11:48:26 +0000720/*
721 * format of message for address additon is:
722 * 0
723 * 0 1 2 3 4 5 6 7
724 * +-+-+-+-+-+-+-+-+
725 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
726 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
727 * | |
728 * + +
729 * | ifindex |
730 * + +
731 * | |
732 * + +
733 * | |
734 * +-+-+-+-+-+-+-+-+
735 * | ifc_flags | flags for connected address
736 * +-+-+-+-+-+-+-+-+
737 * | addr_family |
738 * +-+-+-+-+-+-+-+-+
739 * | addr... |
740 * : :
741 * | |
742 * +-+-+-+-+-+-+-+-+
743 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
744 * +-+-+-+-+-+-+-+-+
745 * | daddr.. |
746 * : :
747 * | |
748 * +-+-+-+-+-+-+-+-+
749 *
750 */
751
hasso18a6dce2004-10-03 18:18:34 +0000752void
753zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
754{
755 /* Read interface's index. */
756 ifp->ifindex = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000757 ifp->status = stream_getc (s);
hasso18a6dce2004-10-03 18:18:34 +0000758
759 /* Read interface's value. */
paulc77d4542006-01-11 01:59:04 +0000760 ifp->flags = stream_getq (s);
hasso18a6dce2004-10-03 18:18:34 +0000761 ifp->metric = stream_getl (s);
762 ifp->mtu = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000763 ifp->mtu6 = stream_getl (s);
hasso18a6dce2004-10-03 18:18:34 +0000764 ifp->bandwidth = stream_getl (s);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700765#ifdef HAVE_STRUCT_SOCKADDR_DL
David Lamparterca3ccd82012-09-26 14:52:39 +0200766 stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
Josh Bailey51d4ef82012-03-21 17:13:39 -0700767#else
768 ifp->hw_addr_len = stream_getl (s);
769 if (ifp->hw_addr_len)
770 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
771#endif /* HAVE_STRUCT_SOCKADDR_DL */
hasso18a6dce2004-10-03 18:18:34 +0000772}
773
hasso3fb9cd62004-10-19 19:44:43 +0000774static int
775memconstant(const void *s, int c, size_t n)
776{
777 const u_char *p = s;
778
779 while (n-- > 0)
780 if (*p++ != c)
781 return 0;
782 return 1;
783}
784
paul718e3742002-12-13 20:15:29 +0000785struct connected *
Feng Luc99f3482014-10-16 09:52:36 +0800786zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000787{
788 unsigned int ifindex;
789 struct interface *ifp;
790 struct connected *ifc;
paul0a589352004-05-08 11:48:26 +0000791 struct prefix p, d;
paul718e3742002-12-13 20:15:29 +0000792 int family;
793 int plen;
paul0a589352004-05-08 11:48:26 +0000794 u_char ifc_flags;
795
796 memset (&p, 0, sizeof(p));
797 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000798
799 /* Get interface index. */
800 ifindex = stream_getl (s);
801
802 /* Lookup index. */
Feng Luc99f3482014-10-16 09:52:36 +0800803 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
paul718e3742002-12-13 20:15:29 +0000804 if (ifp == NULL)
805 {
paul0a589352004-05-08 11:48:26 +0000806 zlog_warn ("zebra_interface_address_read(%s): "
807 "Can't find interface by ifindex: %d ",
808 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
809 ifindex);
paul718e3742002-12-13 20:15:29 +0000810 return NULL;
811 }
812
813 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000814 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000815
816 /* Fetch interface address. */
817 family = p.family = stream_getc (s);
818
paul0a589352004-05-08 11:48:26 +0000819 plen = prefix_blen (&p);
820 stream_get (&p.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000821 p.prefixlen = stream_getc (s);
822
823 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +0000824 stream_get (&d.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000825 d.family = family;
826
paul0a589352004-05-08 11:48:26 +0000827 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
828 {
hasso3fb9cd62004-10-19 19:44:43 +0000829 /* N.B. NULL destination pointers are encoded as all zeroes */
830 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
831 NULL : &d));
paul0a589352004-05-08 11:48:26 +0000832 if (ifc != NULL)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000833 {
834 ifc->flags = ifc_flags;
835 if (ifc->destination)
836 ifc->destination->prefixlen = ifc->address->prefixlen;
David Lamparter90444ca2014-07-01 16:14:05 +0200837 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
838 {
839 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
Timo Teräs41eb9a42015-05-23 11:08:39 +0300840 char buf[PREFIX_STRLEN];
David Lamparter90444ca2014-07-01 16:14:05 +0200841 zlog_warn("warning: interface %s address %s "
842 "with peer flag set, but no peer address!",
Timo Teräs41eb9a42015-05-23 11:08:39 +0300843 ifp->name,
844 prefix2str (ifc->address, buf, sizeof buf));
David Lamparter90444ca2014-07-01 16:14:05 +0200845 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
846 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000847 }
paul0a589352004-05-08 11:48:26 +0000848 }
849 else
850 {
851 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
852 ifc = connected_delete_by_prefix(ifp, &p);
853 }
paul718e3742002-12-13 20:15:29 +0000854
855 return ifc;
856}
paul0a589352004-05-08 11:48:26 +0000857
David Lamparter6b0655a2014-06-04 06:53:35 +0200858
paul718e3742002-12-13 20:15:29 +0000859/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +0000860static int
paul718e3742002-12-13 20:15:29 +0000861zclient_read (struct thread *thread)
862{
ajs634f9ea2005-04-11 15:51:40 +0000863 size_t already;
paulc1b98002006-01-16 01:54:02 +0000864 uint16_t length, command;
865 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +0800866 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +0000867 struct zclient *zclient;
868
869 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +0000870 zclient = THREAD_ARG (thread);
871 zclient->t_read = NULL;
872
ajs634f9ea2005-04-11 15:51:40 +0000873 /* Read zebra header (if we don't have it already). */
874 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000875 {
ajs634f9ea2005-04-11 15:51:40 +0000876 ssize_t nbyte;
877 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
878 ZEBRA_HEADER_SIZE-already)) == 0) ||
879 (nbyte == -1))
880 {
881 if (zclient_debug)
882 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
883 return zclient_failed(zclient);
884 }
885 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
886 {
887 /* Try again later. */
888 zclient_event (ZCLIENT_READ, zclient);
889 return 0;
890 }
891 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000892 }
893
ajs634f9ea2005-04-11 15:51:40 +0000894 /* Reset to read from the beginning of the incoming packet. */
895 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +0000896
paulc1b98002006-01-16 01:54:02 +0000897 /* Fetch header values. */
paul718e3742002-12-13 20:15:29 +0000898 length = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000899 marker = stream_getc (zclient->ibuf);
900 version = stream_getc (zclient->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +0800901 vrf_id = stream_getw (zclient->ibuf);
paulc1b98002006-01-16 01:54:02 +0000902 command = stream_getw (zclient->ibuf);
903
904 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
905 {
906 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
907 __func__, zclient->sock, marker, version);
908 return zclient_failed(zclient);
909 }
910
ajs634f9ea2005-04-11 15:51:40 +0000911 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000912 {
ajs634f9ea2005-04-11 15:51:40 +0000913 zlog_err("%s: socket %d message length %u is less than %d ",
914 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
915 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +0000916 }
ajs634f9ea2005-04-11 15:51:40 +0000917
918 /* Length check. */
919 if (length > STREAM_SIZE(zclient->ibuf))
920 {
921 struct stream *ns;
922 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
923 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
924 ns = stream_new(length);
925 stream_copy(ns, zclient->ibuf);
926 stream_free (zclient->ibuf);
927 zclient->ibuf = ns;
928 }
paul718e3742002-12-13 20:15:29 +0000929
930 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +0000931 if (already < length)
932 {
933 ssize_t nbyte;
934 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
935 length-already)) == 0) ||
936 (nbyte == -1))
937 {
938 if (zclient_debug)
939 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
940 return zclient_failed(zclient);
941 }
942 if (nbyte != (ssize_t)(length-already))
943 {
944 /* Try again later. */
945 zclient_event (ZCLIENT_READ, zclient);
946 return 0;
947 }
948 }
949
950 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000951
paul0a589352004-05-08 11:48:26 +0000952 if (zclient_debug)
Feng Luc99f3482014-10-16 09:52:36 +0800953 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
paul0a589352004-05-08 11:48:26 +0000954
paul718e3742002-12-13 20:15:29 +0000955 switch (command)
956 {
hasso18a6dce2004-10-03 18:18:34 +0000957 case ZEBRA_ROUTER_ID_UPDATE:
958 if (zclient->router_id_update)
Feng Luc99f3482014-10-16 09:52:36 +0800959 (*zclient->router_id_update) (command, zclient, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000960 break;
paul718e3742002-12-13 20:15:29 +0000961 case ZEBRA_INTERFACE_ADD:
962 if (zclient->interface_add)
Feng Luc99f3482014-10-16 09:52:36 +0800963 (*zclient->interface_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000964 break;
965 case ZEBRA_INTERFACE_DELETE:
966 if (zclient->interface_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800967 (*zclient->interface_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000968 break;
969 case ZEBRA_INTERFACE_ADDRESS_ADD:
970 if (zclient->interface_address_add)
Feng Luc99f3482014-10-16 09:52:36 +0800971 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000972 break;
973 case ZEBRA_INTERFACE_ADDRESS_DELETE:
974 if (zclient->interface_address_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800975 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000976 break;
977 case ZEBRA_INTERFACE_UP:
978 if (zclient->interface_up)
Feng Luc99f3482014-10-16 09:52:36 +0800979 (*zclient->interface_up) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000980 break;
981 case ZEBRA_INTERFACE_DOWN:
982 if (zclient->interface_down)
Feng Luc99f3482014-10-16 09:52:36 +0800983 (*zclient->interface_down) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000984 break;
985 case ZEBRA_IPV4_ROUTE_ADD:
986 if (zclient->ipv4_route_add)
Feng Luc99f3482014-10-16 09:52:36 +0800987 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000988 break;
989 case ZEBRA_IPV4_ROUTE_DELETE:
990 if (zclient->ipv4_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800991 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000992 break;
993 case ZEBRA_IPV6_ROUTE_ADD:
994 if (zclient->ipv6_route_add)
Feng Luc99f3482014-10-16 09:52:36 +0800995 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +0000996 break;
997 case ZEBRA_IPV6_ROUTE_DELETE:
998 if (zclient->ipv6_route_delete)
Feng Luc99f3482014-10-16 09:52:36 +0800999 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001000 break;
1001 default:
1002 break;
1003 }
1004
ajs634f9ea2005-04-11 15:51:40 +00001005 if (zclient->sock < 0)
1006 /* Connection was closed during packet processing. */
1007 return -1;
1008
paul718e3742002-12-13 20:15:29 +00001009 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +00001010 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +00001011 zclient_event (ZCLIENT_READ, zclient);
1012
1013 return 0;
1014}
1015
1016void
Feng Luc99f3482014-10-16 09:52:36 +08001017zclient_redistribute (int command, struct zclient *zclient, int type,
1018 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001019{
paul718e3742002-12-13 20:15:29 +00001020
paul0a589352004-05-08 11:48:26 +00001021 if (command == ZEBRA_REDISTRIBUTE_ADD)
1022 {
Feng Luc99f3482014-10-16 09:52:36 +08001023 if (vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001024 return;
Feng Luc99f3482014-10-16 09:52:36 +08001025 vrf_bitmap_set (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001026 }
1027 else
1028 {
Feng Luc99f3482014-10-16 09:52:36 +08001029 if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
paul0a589352004-05-08 11:48:26 +00001030 return;
Feng Luc99f3482014-10-16 09:52:36 +08001031 vrf_bitmap_unset (zclient->redist[type], vrf_id);
paul0a589352004-05-08 11:48:26 +00001032 }
paul718e3742002-12-13 20:15:29 +00001033
1034 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001035 zebra_redistribute_send (command, zclient, type, vrf_id);
paul718e3742002-12-13 20:15:29 +00001036}
1037
paul0a589352004-05-08 11:48:26 +00001038
paul718e3742002-12-13 20:15:29 +00001039void
Feng Luc99f3482014-10-16 09:52:36 +08001040zclient_redistribute_default (int command, struct zclient *zclient,
1041 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001042{
paul718e3742002-12-13 20:15:29 +00001043
paul0a589352004-05-08 11:48:26 +00001044 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1045 {
Feng Luc99f3482014-10-16 09:52:36 +08001046 if (vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001047 return;
Feng Luc99f3482014-10-16 09:52:36 +08001048 vrf_bitmap_set (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001049 }
1050 else
1051 {
Feng Luc99f3482014-10-16 09:52:36 +08001052 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
paul0a589352004-05-08 11:48:26 +00001053 return;
Feng Luc99f3482014-10-16 09:52:36 +08001054 vrf_bitmap_unset (zclient->default_information, vrf_id);
paul0a589352004-05-08 11:48:26 +00001055 }
paul718e3742002-12-13 20:15:29 +00001056
1057 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +08001058 zebra_message_send (zclient, command, vrf_id);
paul718e3742002-12-13 20:15:29 +00001059}
1060
paul718e3742002-12-13 20:15:29 +00001061static void
1062zclient_event (enum event event, struct zclient *zclient)
1063{
1064 switch (event)
1065 {
1066 case ZCLIENT_SCHEDULE:
1067 if (! zclient->t_connect)
1068 zclient->t_connect =
1069 thread_add_event (master, zclient_connect, zclient, 0);
1070 break;
1071 case ZCLIENT_CONNECT:
1072 if (zclient->fail >= 10)
1073 return;
1074 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +00001075 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +00001076 zclient->fail < 3 ? 10 : 60);
1077 if (! zclient->t_connect)
1078 zclient->t_connect =
1079 thread_add_timer (master, zclient_connect, zclient,
1080 zclient->fail < 3 ? 10 : 60);
1081 break;
1082 case ZCLIENT_READ:
1083 zclient->t_read =
1084 thread_add_read (master, zclient_read, zclient, zclient->sock);
1085 break;
1086 }
1087}
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001088
David Lamparterfd8f6eb2015-03-03 08:57:02 +01001089const char *zclient_serv_path_get()
Everton Marques1f298942014-08-21 15:47:28 -03001090{
1091 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
1092}
1093
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001094void
1095zclient_serv_path_set (char *path)
1096{
1097 struct stat sb;
1098
1099 /* reset */
1100 zclient_serv_path = NULL;
1101
1102 /* test if `path' is socket. don't set it otherwise. */
1103 if (stat(path, &sb) == -1)
1104 {
1105 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1106 return;
1107 }
1108
1109 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1110 {
1111 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1112 return;
1113 }
1114
1115 /* it seems that path is unix socket */
1116 zclient_serv_path = path;
1117}
1118