]> git.proxmox.com Git - mirror_frr.git/blame - lib/zclient.c
zebra: display_vrf_name_on_interface
[mirror_frr.git] / lib / zclient.c
CommitLineData
718e3744 1/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
634f9ea2 3 * Copyright (C) 2005 Andrew J. Schorr
718e3744 4 *
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"
634f9ea2 27#include "buffer.h"
718e3744 28#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"
6b0655a2 35
718e3744 36/* 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
b5114685
VT
42char *zclient_serv_path = NULL;
43
718e3744 44/* This file local debug flag. */
45int zclient_debug = 0;
6b0655a2 46
718e3744 47/* Allocate zclient structure. */
48struct zclient *
4140ca4d 49zclient_new (struct thread_master *master)
718e3744 50{
51 struct zclient *zclient;
393deb9b 52 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
718e3744 53
54 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
55 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
634f9ea2 56 zclient->wb = buffer_new(0);
4140ca4d 57 zclient->master = master;
718e3744 58
59 return zclient;
60}
61
228da428 62/* This function is only called when exiting, because
634f9ea2 63 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.
634f9ea2 65
228da428 66 Free zclient structure. */
718e3744 67void
68zclient_free (struct zclient *zclient)
69{
634f9ea2 70 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
718e3744 77 XFREE (MTYPE_ZCLIENT, zclient);
78}
79
7c8ff89e
DS
80int
81redist_check_instance (struct redist_proto *red, u_short instance)
82{
83 struct listnode *node;
84 u_short *id;
85
86 if (!red->instances)
87 return 0;
88
89 for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
90 if (*id == instance)
91 return 1;
92
93 return 0;
94}
95
96void
97redist_add_instance (struct redist_proto *red, u_short instance)
98{
99 u_short *in;
100
101 red->enabled = 1;
102
103 if (!red->instances)
104 red->instances = list_new();
105
106 in = (u_short *)calloc(1, sizeof(u_short));
107 *in = instance;
108 listnode_add (red->instances, in);
109}
110
111void
112redist_del_instance (struct redist_proto *red, u_short instance)
113{
114 struct listnode *node;
115 u_short *id = NULL;
116
117 if (!red->instances)
24873f0c 118 return;
7c8ff89e
DS
119
120 for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
121 if (*id == instance)
122 break;
123
124 if (id)
125 {
126 listnode_delete(red->instances, id);
127 if (!red->instances->count)
128 {
129 red->enabled = 0;
130 list_free(red->instances);
131 red->instances = NULL;
132 }
133 }
134}
135
718e3744 136/* Stop zebra client services. */
137void
138zclient_stop (struct zclient *zclient)
139{
140 if (zclient_debug)
8ddca704 141 zlog_debug ("zclient stopped");
718e3744 142
143 /* Stop threads. */
634f9ea2 144 THREAD_OFF(zclient->t_read);
145 THREAD_OFF(zclient->t_connect);
146 THREAD_OFF(zclient->t_write);
147
148 /* Reset streams. */
149 stream_reset(zclient->ibuf);
150 stream_reset(zclient->obuf);
151
152 /* Empty the write buffer. */
153 buffer_reset(zclient->wb);
718e3744 154
155 /* Close socket. */
156 if (zclient->sock >= 0)
157 {
158 close (zclient->sock);
159 zclient->sock = -1;
160 }
161 zclient->fail = 0;
162}
163
164void
165zclient_reset (struct zclient *zclient)
166{
7076bb2f 167 afi_t afi;
3d68677e 168
718e3744 169 zclient_stop (zclient);
3d68677e
DS
170
171 for (afi = AFI_IP; afi < AFI_MAX; afi++)
7076bb2f 172 redist_del_instance (&zclient->mi_redist[afi][zclient->redist_default], zclient->instance);
3d68677e 173
7c8ff89e 174 zclient_init (zclient, zclient->redist_default, zclient->instance);
718e3744 175}
176
b5114685
VT
177#ifdef HAVE_TCP_ZEBRA
178
718e3744 179/* Make socket to zebra daemon. Return zebra socket. */
b5114685 180static int
634f9ea2 181zclient_socket(void)
718e3744 182{
183 int sock;
184 int ret;
185 struct sockaddr_in serv;
186
187 /* We should think about IPv6 connection. */
188 sock = socket (AF_INET, SOCK_STREAM, 0);
189 if (sock < 0)
190 return -1;
191
192 /* Make server socket. */
193 memset (&serv, 0, sizeof (struct sockaddr_in));
194 serv.sin_family = AF_INET;
195 serv.sin_port = htons (ZEBRA_PORT);
6f0e3f6e 196#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 197 serv.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 198#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 199 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
200
201 /* Connect to zebra. */
202 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
203 if (ret < 0)
204 {
4ecc09d3 205 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
718e3744 206 close (sock);
207 return -1;
208 }
209 return sock;
210}
211
3414d035 212#else
b5114685 213
718e3744 214/* For sockaddr_un. */
215#include <sys/un.h>
216
b5114685 217static int
8c328f11 218zclient_socket_un (const char *path)
718e3744 219{
220 int ret;
221 int sock, len;
222 struct sockaddr_un addr;
223
224 sock = socket (AF_UNIX, SOCK_STREAM, 0);
225 if (sock < 0)
226 return -1;
227
228 /* Make server socket. */
229 memset (&addr, 0, sizeof (struct sockaddr_un));
230 addr.sun_family = AF_UNIX;
231 strncpy (addr.sun_path, path, strlen (path));
6f0e3f6e 232#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
718e3744 233 len = addr.sun_len = SUN_LEN(&addr);
234#else
235 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
6f0e3f6e 236#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 237
238 ret = connect (sock, (struct sockaddr *) &addr, len);
239 if (ret < 0)
240 {
4ecc09d3 241 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
718e3744 242 close (sock);
243 return -1;
244 }
245 return sock;
246}
247
3414d035
VT
248#endif /* HAVE_TCP_ZEBRA */
249
b5114685
VT
250/**
251 * Connect to zebra daemon.
252 * @param zclient a pointer to zclient structure
253 * @return socket fd just to make sure that connection established
254 * @see zclient_init
255 * @see zclient_new
256 */
257int
258zclient_socket_connect (struct zclient *zclient)
259{
260#ifdef HAVE_TCP_ZEBRA
261 zclient->sock = zclient_socket ();
262#else
263 zclient->sock = zclient_socket_un (zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH);
264#endif
265 return zclient->sock;
266}
267
634f9ea2 268static int
269zclient_failed(struct zclient *zclient)
270{
271 zclient->fail++;
272 zclient_stop(zclient);
273 zclient_event(ZCLIENT_CONNECT, zclient);
274 return -1;
275}
276
277static int
278zclient_flush_data(struct thread *thread)
279{
280 struct zclient *zclient = THREAD_ARG(thread);
281
282 zclient->t_write = NULL;
283 if (zclient->sock < 0)
284 return -1;
285 switch (buffer_flush_available(zclient->wb, zclient->sock))
286 {
287 case BUFFER_ERROR:
288 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
289 __func__, zclient->sock);
290 return zclient_failed(zclient);
291 break;
292 case BUFFER_PENDING:
4140ca4d 293 zclient->t_write = thread_add_write(zclient->master, zclient_flush_data,
634f9ea2 294 zclient, zclient->sock);
295 break;
296 case BUFFER_EMPTY:
297 break;
298 }
299 return 0;
300}
301
718e3744 302int
634f9ea2 303zclient_send_message(struct zclient *zclient)
304{
305 if (zclient->sock < 0)
306 return -1;
307 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
308 stream_get_endp(zclient->obuf)))
309 {
310 case BUFFER_ERROR:
311 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
312 __func__, zclient->sock);
313 return zclient_failed(zclient);
314 break;
315 case BUFFER_EMPTY:
316 THREAD_OFF(zclient->t_write);
317 break;
318 case BUFFER_PENDING:
4140ca4d 319 THREAD_WRITE_ON(zclient->master, zclient->t_write,
634f9ea2 320 zclient_flush_data, zclient, zclient->sock);
321 break;
322 }
323 return 0;
324}
325
d211086a 326void
7076bb2f 327zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
c1b9800a 328{
329 /* length placeholder, caller can update */
330 stream_putw (s, ZEBRA_HEADER_SIZE);
331 stream_putc (s, ZEBRA_HEADER_MARKER);
332 stream_putc (s, ZSERV_VERSION);
7076bb2f 333 stream_putw (s, vrf_id);
c1b9800a 334 stream_putw (s, command);
335}
336
634f9ea2 337/* Send simple Zebra message. */
338static int
7076bb2f 339zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
718e3744 340{
341 struct stream *s;
342
343 /* Get zclient output buffer. */
344 s = zclient->obuf;
345 stream_reset (s);
346
347 /* Send very simple command only Zebra message. */
7076bb2f 348 zclient_create_header (s, command, vrf_id);
c1b9800a 349
634f9ea2 350 return zclient_send_message(zclient);
718e3744 351}
352
2ea1ab1c
VT
353static int
354zebra_hello_send (struct zclient *zclient)
355{
356 struct stream *s;
357
358 if (zclient->redist_default)
359 {
360 s = zclient->obuf;
361 stream_reset (s);
362
7076bb2f
FL
363 /* The VRF ID in the HELLO message is always 0. */
364 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
2ea1ab1c 365 stream_putc (s, zclient->redist_default);
7c8ff89e 366 stream_putw (s, zclient->instance);
2ea1ab1c
VT
367 stream_putw_at (s, 0, stream_get_endp (s));
368 return zclient_send_message(zclient);
369 }
370
371 return 0;
372}
373
7076bb2f
FL
374/* Send requests to zebra daemon for the information in a VRF. */
375void
376zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
377{
378 int i;
379 afi_t afi;
380
381 /* zclient is disabled. */
382 if (! zclient->enable)
383 return;
384
385 /* If not connected to the zebra yet. */
386 if (zclient->sock < 0)
387 return;
388
389 if (zclient_debug)
390 zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
391
392 /* We need router-id information. */
393 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
394
395 /* We need interface information. */
396 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
397
398 /* Set unwanted redistribute route. */
399 for (afi = AFI_IP; afi < AFI_MAX; afi++)
400 vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id);
401
402 /* Flush all redistribute request. */
403 if (vrf_id == VRF_DEFAULT)
404 for (afi = AFI_IP; afi < AFI_MAX; afi++)
405 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
406 if (zclient->mi_redist[afi][i].enabled)
407 {
408 struct listnode *node;
409 u_short *id;
410
411 for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id))
412 if (!(i == zclient->redist_default && *id == zclient->instance))
413 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i,
414 *id, VRF_DEFAULT);
415 }
416
417 /* Flush all redistribute request. */
418 for (afi = AFI_IP; afi < AFI_MAX; afi++)
419 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
420 if (i != zclient->redist_default &&
421 vrf_bitmap_check (zclient->redist[afi][i], vrf_id))
422 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, 0, vrf_id);
423
424 /* If default information is needed. */
425 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
426 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
427}
428
718e3744 429/* Make connection to zebra daemon. */
430int
431zclient_start (struct zclient *zclient)
432{
7076bb2f
FL
433 if (zclient_debug)
434 zlog_info ("zclient_start is called");
435
436 /* zclient is disabled. */
437 if (! zclient->enable)
438 return 0;
718e3744 439
718e3744 440 /* If already connected to the zebra. */
441 if (zclient->sock >= 0)
442 return 0;
443
444 /* Check connect thread. */
445 if (zclient->t_connect)
446 return 0;
447
4ecc09d3
DS
448 /*
449 * If we fail to connect to the socket on initialization,
450 * Let's wait a second and see if we can reconnect.
451 * Cause if we don't connect, we never attempt to
452 * reconnect. On startup if zebra is slow we
453 * can get into this situation.
454 */
455 while (zclient_socket_connect(zclient) < 0 && zclient->fail < 5)
7076bb2f
FL
456 {
457 if (zclient_debug)
458 zlog_debug ("zclient connection fail");
459 zclient->fail++;
4ecc09d3
DS
460 sleep (1);
461 }
462
463 if (zclient->sock < 0)
464 {
7076bb2f
FL
465 zclient_event (ZCLIENT_CONNECT, zclient);
466 return -1;
467 }
718e3744 468
7076bb2f
FL
469 if (set_nonblocking(zclient->sock) < 0)
470 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
718e3744 471
7076bb2f
FL
472 /* Clear fail count. */
473 zclient->fail = 0;
474 if (zclient_debug)
475 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
718e3744 476
7076bb2f
FL
477 /* Create read thread. */
478 zclient_event (ZCLIENT_READ, zclient);
718e3744 479
7076bb2f 480 zebra_hello_send (zclient);
718e3744 481
7076bb2f
FL
482 /* Inform the successful connection. */
483 if (zclient->zebra_connected)
484 (*zclient->zebra_connected) (zclient);
718e3744 485
7076bb2f 486 return 0;
718e3744 487}
6b0655a2 488
078430f6
DS
489/* Initialize zebra client. Argument redist_default is unwanted
490 redistribute route type. */
491void
492zclient_init (struct zclient *zclient, int redist_default, u_short instance)
493{
494 int afi, i;
495
496 /* Enable zebra client connection by default. */
497 zclient->enable = 1;
498
499 /* Set -1 to the default socket value. */
500 zclient->sock = -1;
501
502 /* Clear redistribution flags. */
503 for (afi = AFI_IP; afi < AFI_MAX; afi++)
504 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
7076bb2f 505 zclient->redist[afi][i] = vrf_bitmap_init();
078430f6
DS
506
507 /* Set unwanted redistribute route. bgpd does not need BGP route
508 redistribution. */
509 zclient->redist_default = redist_default;
510 zclient->instance = instance;
511 /* Pending: make afi(s) an arg. */
512 for (afi = AFI_IP; afi < AFI_MAX; afi++)
7076bb2f 513 redist_add_instance (&zclient->mi_redist[afi][redist_default], instance);
078430f6
DS
514
515 /* Set default-information redistribute to zero. */
7076bb2f 516 zclient->default_information = vrf_bitmap_init ();;
078430f6
DS
517
518 if (zclient_debug)
519 zlog_debug ("zclient_start is called");
520
7076bb2f
FL
521 zclient_event (ZCLIENT_SCHEDULE, zclient);
522}
078430f6 523
7076bb2f
FL
524/* This function is a wrapper function for calling zclient_start from
525 timer or event thread. */
526static int
527zclient_connect (struct thread *t)
528{
529 struct zclient *zclient;
078430f6 530
7076bb2f
FL
531 zclient = THREAD_ARG (t);
532 zclient->t_connect = NULL;
078430f6 533
078430f6 534 if (zclient_debug)
7076bb2f 535 zlog_debug ("zclient_connect is called");
078430f6 536
7076bb2f 537 return zclient_start (zclient);
078430f6
DS
538}
539
0a589359 540 /*
541 * "xdr_encode"-like interface that allows daemon (client) to send
542 * a message to zebra server for a route that needs to be
543 * added/deleted to the kernel. Info about the route is specified
544 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
545 * the info down the zclient socket using the stream_* functions.
546 *
547 * The corresponding read ("xdr_decode") function on the server
548 * side is zread_ipv4_add()/zread_ipv4_delete().
549 *
550 * 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
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552 * | Length (2) | Command | Route Type |
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
554 * | ZEBRA Flags | Message Flags | Prefix length |
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * | Destination IPv4 Prefix for route |
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558 * | Nexthop count |
559 * +-+-+-+-+-+-+-+-+
560 *
561 *
562 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
563 * described, as per the Nexthop count. Each nexthop described as:
564 *
565 * +-+-+-+-+-+-+-+-+
566 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
568 * | IPv4 Nexthop address or Interface Index number |
569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 *
571 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
572 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
573 * nexthop information is provided, and the message describes a prefix
574 * to blackhole or reject route.
575 *
c8a1cb5c
DS
576 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
577 * infrastructure was built around the traditional (32-bit "gate OR
578 * ifindex") nexthop data unit. A special encoding can be used to feed
579 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
580 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
581 * fields as follows:
582 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
583 * - .nexthop_num == .ifindex_num
584 * - .nexthop and .ifindex are filled with gate and ifindex parts of
585 * each compound nexthop, both in the same order
586 *
587 * zapi_ipv4_route() will produce two nexthop data units for each such
588 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
589 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
590 *
0a589359 591 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
592 * byte value.
593 *
594 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
595 * byte value.
596 *
0d9551dc
DS
597 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value
598 *
0a589359 599 * XXX: No attention paid to alignment.
600 */
718e3744 601int
0a589359 602zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
603 struct zapi_ipv4 *api)
718e3744 604{
605 int i;
606 int psize;
607 struct stream *s;
608
609 /* Reset stream. */
610 s = zclient->obuf;
611 stream_reset (s);
7076bb2f
FL
612
613 zclient_create_header (s, cmd, api->vrf_id);
c1b9800a 614
615 /* Put type and nexthop. */
718e3744 616 stream_putc (s, api->type);
7c8ff89e 617 stream_putw (s, api->instance);
718e3744 618 stream_putc (s, api->flags);
619 stream_putc (s, api->message);
5a616c08 620 stream_putw (s, api->safi);
718e3744 621
718e3744 622 /* Put prefix information. */
623 psize = PSIZE (p->prefixlen);
624 stream_putc (s, p->prefixlen);
0a589359 625 stream_write (s, (u_char *) & p->prefix, psize);
718e3744 626
627 /* Nexthop, ifindex, distance and metric information. */
d44ca835 628 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
c8a1cb5c
DS
629 {
630 /* traditional 32-bit data units */
595db7f1 631 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
632 {
633 stream_putc (s, 1);
634 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
0a589359 635 /* XXX assert(api->nexthop_num == 0); */
636 /* XXX assert(api->ifindex_num == 0); */
595db7f1 637 }
638 else
639 stream_putc (s, api->nexthop_num + api->ifindex_num);
718e3744 640
641 for (i = 0; i < api->nexthop_num; i++)
595db7f1 642 {
643 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
644 stream_put_in_addr (s, api->nexthop[i]);
645 }
718e3744 646 for (i = 0; i < api->ifindex_num; i++)
595db7f1 647 {
648 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
649 stream_putl (s, api->ifindex[i]);
650 }
718e3744 651 }
652
653 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
654 stream_putc (s, api->distance);
655 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
656 stream_putl (s, api->metric);
0d9551dc
DS
657 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
658 stream_putw (s, api->tag);
718e3744 659
660 /* Put length at the first point of the stream. */
661 stream_putw_at (s, 0, stream_get_endp (s));
662
634f9ea2 663 return zclient_send_message(zclient);
718e3744 664}
665
666#ifdef HAVE_IPV6
8a92a8a0
DS
667int
668zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient,
669 struct prefix_ipv4 *p, struct zapi_ipv6 *api)
670{
671 int i;
672 int psize;
673 struct stream *s;
674
675 /* Reset stream. */
676 s = zclient->obuf;
677 stream_reset (s);
678
7076bb2f 679 zclient_create_header (s, cmd, api->vrf_id);
8a92a8a0
DS
680
681 /* Put type and nexthop. */
682 stream_putc (s, api->type);
683 stream_putw (s, api->instance);
684 stream_putc (s, api->flags);
685 stream_putc (s, api->message);
686 stream_putw (s, api->safi);
687
688 /* Put prefix information. */
689 psize = PSIZE (p->prefixlen);
690 stream_putc (s, p->prefixlen);
691 stream_write (s, (u_char *) & p->prefix, psize);
692
693 /* Nexthop, ifindex, distance and metric information. */
694 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
695 {
696 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
697 {
698 stream_putc (s, 1);
699 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
700 /* XXX assert(api->nexthop_num == 0); */
701 /* XXX assert(api->ifindex_num == 0); */
702 }
703 else
704 stream_putc (s, api->nexthop_num + api->ifindex_num);
705
706 for (i = 0; i < api->nexthop_num; i++)
707 {
708 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
709 stream_write (s, (u_char *)api->nexthop[i], 16);
710 }
711 for (i = 0; i < api->ifindex_num; i++)
712 {
713 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
714 stream_putl (s, api->ifindex[i]);
715 }
716 }
717
718 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
719 stream_putc (s, api->distance);
720 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
721 stream_putl (s, api->metric);
722 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
723 stream_putw (s, api->tag);
724
725 /* Put length at the first point of the stream. */
726 stream_putw_at (s, 0, stream_get_endp (s));
727
728 return zclient_send_message(zclient);
729}
730
718e3744 731int
0a589359 732zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
718e3744 733 struct zapi_ipv6 *api)
734{
735 int i;
736 int psize;
737 struct stream *s;
738
739 /* Reset stream. */
740 s = zclient->obuf;
741 stream_reset (s);
742
7076bb2f 743 zclient_create_header (s, cmd, api->vrf_id);
718e3744 744
c1b9800a 745 /* Put type and nexthop. */
718e3744 746 stream_putc (s, api->type);
7c8ff89e 747 stream_putw (s, api->instance);
718e3744 748 stream_putc (s, api->flags);
749 stream_putc (s, api->message);
c7ec179a 750 stream_putw (s, api->safi);
718e3744 751
752 /* Put prefix information. */
753 psize = PSIZE (p->prefixlen);
754 stream_putc (s, p->prefixlen);
755 stream_write (s, (u_char *)&p->prefix, psize);
756
757 /* Nexthop, ifindex, distance and metric information. */
758 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
759 {
c3c0ac83
DS
760 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
761 {
762 stream_putc (s, 1);
763 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
764 /* XXX assert(api->nexthop_num == 0); */
765 /* XXX assert(api->ifindex_num == 0); */
766 }
767 else
768 stream_putc (s, api->nexthop_num + api->ifindex_num);
718e3744 769
770 for (i = 0; i < api->nexthop_num; i++)
771 {
772 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
773 stream_write (s, (u_char *)api->nexthop[i], 16);
774 }
775 for (i = 0; i < api->ifindex_num; i++)
776 {
777 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
778 stream_putl (s, api->ifindex[i]);
779 }
780 }
781
782 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
783 stream_putc (s, api->distance);
784 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
785 stream_putl (s, api->metric);
0d9551dc
DS
786 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
787 stream_putw (s, api->tag);
718e3744 788
789 /* Put length at the first point of the stream. */
790 stream_putw_at (s, 0, stream_get_endp (s));
791
634f9ea2 792 return zclient_send_message(zclient);
718e3744 793}
718e3744 794#endif /* HAVE_IPV6 */
795
0a589359 796/*
797 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
798 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
799 * then set/unset redist[type] in the client handle (a struct zserv) for the
800 * sending client
801 */
718e3744 802int
8bb0831e 803zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type,
7076bb2f 804 u_short instance, vrf_id_t vrf_id)
718e3744 805{
718e3744 806 struct stream *s;
807
634f9ea2 808 s = zclient->obuf;
809 stream_reset(s);
718e3744 810
7076bb2f 811 zclient_create_header (s, command, vrf_id);
8bb0831e 812 stream_putc (s, afi);
718e3744 813 stream_putc (s, type);
7c8ff89e 814 stream_putw (s, instance);
c1b9800a 815
816 stream_putw_at (s, 0, stream_get_endp (s));
817
634f9ea2 818 return zclient_send_message(zclient);
718e3744 819}
820
18a6dce6 821/* Router-id update from zebra daemon. */
822void
823zebra_router_id_update_read (struct stream *s, struct prefix *rid)
824{
825 int plen;
826
827 /* Fetch interface address. */
828 rid->family = stream_getc (s);
829
830 plen = prefix_blen (rid);
831 stream_get (&rid->u.prefix, s, plen);
832 rid->prefixlen = stream_getc (s);
833}
834
718e3744 835/* Interface addition from zebra daemon. */
0a589359 836/*
837 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
838 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
839 * 0 1 2 3
840 * 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
841 * +-+-+-+-+-+-+-+-+
842 * | type |
843 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
844 * | ifname |
845 * | |
846 * | |
847 * | |
848 * | |
849 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
850 * | ifindex |
851 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
852 * | if_flags |
c77d4546 853 * | |
0a589359 854 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
855 * | metric |
856 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
857 * | ifmtu |
858 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
859 * | ifmtu6 |
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * | bandwidth |
862 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
863 * | sockaddr_dl |
864 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
865 */
866
1892f15e
DS
867struct vrf *
868zebra_vrf_add_read (struct stream *s, vrf_id_t vrf_id)
869{
870 struct vrf *vrf;
871 char vrfname_tmp[VRF_NAMSIZ];
872
873 /* Read interface name. */
874 stream_get (vrfname_tmp, s, VRF_NAMSIZ);
875
876 /* Lookup/create vrf by vrf_id. */
877 vrf = vrf_get (vrf_id, vrfname_tmp);
878
879 return vrf;
880}
881
882struct vrf *
883zebra_vrf_state_read (struct stream *s, vrf_id_t vrf_id)
884{
885 struct vrf *vrf;
886
887 /* Lookup vrf by vrf_id. */
888 vrf = vrf_lookup (vrf_id);
889
890 return vrf;
891}
892
718e3744 893struct interface *
7076bb2f 894zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
718e3744 895{
896 struct interface *ifp;
02ff83c5 897 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 898
899 /* Read interface name. */
900 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
901
a349198f 902 /* Lookup/create interface by name. */
7076bb2f
FL
903 ifp = if_get_by_name_len_vrf (ifname_tmp,
904 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
905 vrf_id);
718e3744 906
51d4ef83 907 zebra_interface_if_set_value (s, ifp);
718e3744 908
718e3744 909 return ifp;
910}
911
0a589359 912/*
913 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
914 * from zebra server. The format of this message is the same as
915 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
916 * comments for zebra_interface_add_read), except that no sockaddr_dl
917 * is sent at the tail of the message.
918 */
718e3744 919struct interface *
7076bb2f 920zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
718e3744 921{
922 struct interface *ifp;
02ff83c5 923 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 924
925 /* Read interface name. */
926 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
927
928 /* Lookup this by interface index. */
7076bb2f
FL
929 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
930 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
931 vrf_id);
718e3744 932
933 /* If such interface does not exist, indicate an error */
934 if (! ifp)
935 return NULL;
936
51d4ef83 937 zebra_interface_if_set_value (s, ifp);
718e3744 938
939 return ifp;
940}
941
0a589359 942/*
943 * format of message for address additon is:
944 * 0
945 * 0 1 2 3 4 5 6 7
946 * +-+-+-+-+-+-+-+-+
947 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
948 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
949 * | |
950 * + +
951 * | ifindex |
952 * + +
953 * | |
954 * + +
955 * | |
956 * +-+-+-+-+-+-+-+-+
957 * | ifc_flags | flags for connected address
958 * +-+-+-+-+-+-+-+-+
959 * | addr_family |
960 * +-+-+-+-+-+-+-+-+
961 * | addr... |
962 * : :
963 * | |
964 * +-+-+-+-+-+-+-+-+
965 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
966 * +-+-+-+-+-+-+-+-+
967 * | daddr.. |
968 * : :
969 * | |
970 * +-+-+-+-+-+-+-+-+
971 *
972 */
973
18a6dce6 974void
975zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
976{
977 /* Read interface's index. */
978 ifp->ifindex = stream_getl (s);
508ec910 979 ifp->status = stream_getc (s);
18a6dce6 980
981 /* Read interface's value. */
c77d4546 982 ifp->flags = stream_getq (s);
244c1cdc
DS
983 ifp->ptm_enable = stream_getc (s);
984 ifp->ptm_status = stream_getc (s);
18a6dce6 985 ifp->metric = stream_getl (s);
986 ifp->mtu = stream_getl (s);
508ec910 987 ifp->mtu6 = stream_getl (s);
18a6dce6 988 ifp->bandwidth = stream_getl (s);
51d4ef83 989#ifdef HAVE_STRUCT_SOCKADDR_DL
ca3ccd87 990 stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
51d4ef83
JB
991#else
992 ifp->hw_addr_len = stream_getl (s);
993 if (ifp->hw_addr_len)
994 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
995#endif /* HAVE_STRUCT_SOCKADDR_DL */
18a6dce6 996}
997
3fb9cd6e 998static int
999memconstant(const void *s, int c, size_t n)
1000{
1001 const u_char *p = s;
1002
1003 while (n-- > 0)
1004 if (*p++ != c)
1005 return 0;
1006 return 1;
1007}
1008
d5a5c8f0 1009
718e3744 1010struct connected *
7076bb2f 1011zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
718e3744 1012{
1013 unsigned int ifindex;
1014 struct interface *ifp;
1015 struct connected *ifc;
0a589359 1016 struct prefix p, d;
718e3744 1017 int family;
1018 int plen;
0a589359 1019 u_char ifc_flags;
718e3744 1020
0a589359 1021 memset (&p, 0, sizeof(p));
1022 memset (&d, 0, sizeof(d));
718e3744 1023
1024 /* Get interface index. */
1025 ifindex = stream_getl (s);
1026
1027 /* Lookup index. */
7076bb2f 1028 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
718e3744 1029 if (ifp == NULL)
1030 {
0a589359 1031 zlog_warn ("zebra_interface_address_read(%s): "
1032 "Can't find interface by ifindex: %d ",
1033 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
1034 ifindex);
718e3744 1035 return NULL;
1036 }
1037
1038 /* Fetch flag. */
0a589359 1039 ifc_flags = stream_getc (s);
718e3744 1040
1041 /* Fetch interface address. */
1042 family = p.family = stream_getc (s);
1043
0a589359 1044 plen = prefix_blen (&p);
1045 stream_get (&p.u.prefix, s, plen);
718e3744 1046 p.prefixlen = stream_getc (s);
1047
1048 /* Fetch destination address. */
0a589359 1049 stream_get (&d.u.prefix, s, plen);
718e3744 1050 d.family = family;
1051
0a589359 1052 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1053 {
3fb9cd6e 1054 /* N.B. NULL destination pointers are encoded as all zeroes */
1055 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
1056 NULL : &d));
0a589359 1057 if (ifc != NULL)
e4529636
AS
1058 {
1059 ifc->flags = ifc_flags;
1060 if (ifc->destination)
1061 ifc->destination->prefixlen = ifc->address->prefixlen;
90444ca3
DL
1062 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1063 {
1064 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
4690c7d7 1065 char buf[PREFIX2STR_BUFFER];
90444ca3
DL
1066 prefix2str (ifc->address, buf, sizeof(buf));
1067 zlog_warn("warning: interface %s address %s "
1068 "with peer flag set, but no peer address!",
1069 ifp->name, buf);
1070 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1071 }
e4529636 1072 }
0a589359 1073 }
1074 else
1075 {
1076 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1077 ifc = connected_delete_by_prefix(ifp, &p);
1078 }
718e3744 1079
1080 return ifc;
1081}
0a589359 1082
a80beece
DS
1083/*
1084 * format of message for neighbor connected address is:
1085 * 0
1086 * 0 1 2 3 4 5 6 7
1087 * +-+-+-+-+-+-+-+-+
1088 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1089 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1090 * | |
1091 * + +
1092 * | ifindex |
1093 * + +
1094 * | |
1095 * + +
1096 * | |
1097 * +-+-+-+-+-+-+-+-+
1098 * | addr_family |
1099 * +-+-+-+-+-+-+-+-+
1100 * | addr... |
1101 * : :
1102 * | |
1103 * +-+-+-+-+-+-+-+-+
1104 * | addr_len | len of addr.
1105 * +-+-+-+-+-+-+-+-+
1106 */
1107struct nbr_connected *
7076bb2f 1108zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id)
a80beece
DS
1109{
1110 unsigned int ifindex;
1111 struct interface *ifp;
1112 struct prefix p;
1113 struct nbr_connected *ifc;
1114
1115 /* Get interface index. */
1116 ifindex = stream_getl (s);
1117
1118 /* Lookup index. */
1119 ifp = if_lookup_by_index (ifindex);
1120 if (ifp == NULL)
1121 {
1122 zlog_warn ("zebra_nbr_interface_address_read(%s): "
1123 "Can't find interface by ifindex: %d ",
1124 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD? "ADD" : "DELETE"),
1125 ifindex);
1126 return NULL;
1127 }
1128
1129 p.family = stream_getc (s);
1130 stream_get (&p.u.prefix, s, prefix_blen (&p));
1131 p.prefixlen = stream_getc (s);
1132
1133 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD)
1134 {
1135 /* Currently only supporting P2P links, so any new RA source address is
1136 considered as the replacement of the previously learnt Link-Local address. */
1137 if (!(ifc = listnode_head(ifp->nbr_connected)))
1138 {
1139 ifc = nbr_connected_new ();
1140 ifc->address = prefix_new ();
1141 ifc->ifp = ifp;
1142 listnode_add (ifp->nbr_connected, ifc);
1143 }
1144
1145 prefix_copy(ifc->address, &p);
1146 }
1147 else
1148 {
1149 assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1150
1151 ifc = nbr_connected_check(ifp, &p);
1152 if (ifc)
1153 listnode_delete (ifp->nbr_connected, ifc);
1154 }
1155
1156 return ifc;
1157}
6b0655a2 1158
718e3744 1159/* Zebra client message read function. */
634f9ea2 1160static int
718e3744 1161zclient_read (struct thread *thread)
1162{
634f9ea2 1163 size_t already;
c1b9800a 1164 uint16_t length, command;
1165 uint8_t marker, version;
7076bb2f 1166 vrf_id_t vrf_id;
718e3744 1167 struct zclient *zclient;
1168
1169 /* Get socket to zebra. */
718e3744 1170 zclient = THREAD_ARG (thread);
1171 zclient->t_read = NULL;
1172
634f9ea2 1173 /* Read zebra header (if we don't have it already). */
1174 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
718e3744 1175 {
634f9ea2 1176 ssize_t nbyte;
1177 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1178 ZEBRA_HEADER_SIZE-already)) == 0) ||
1179 (nbyte == -1))
1180 {
1181 if (zclient_debug)
1182 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1183 return zclient_failed(zclient);
1184 }
1185 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1186 {
1187 /* Try again later. */
1188 zclient_event (ZCLIENT_READ, zclient);
1189 return 0;
1190 }
1191 already = ZEBRA_HEADER_SIZE;
718e3744 1192 }
1193
634f9ea2 1194 /* Reset to read from the beginning of the incoming packet. */
1195 stream_set_getp(zclient->ibuf, 0);
718e3744 1196
c1b9800a 1197 /* Fetch header values. */
718e3744 1198 length = stream_getw (zclient->ibuf);
c1b9800a 1199 marker = stream_getc (zclient->ibuf);
1200 version = stream_getc (zclient->ibuf);
7076bb2f 1201 vrf_id = stream_getw (zclient->ibuf);
c1b9800a 1202 command = stream_getw (zclient->ibuf);
1203
1204 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1205 {
1206 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1207 __func__, zclient->sock, marker, version);
1208 return zclient_failed(zclient);
1209 }
1210
634f9ea2 1211 if (length < ZEBRA_HEADER_SIZE)
1212 {
1213 zlog_err("%s: socket %d message length %u is less than %d ",
1214 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1215 return zclient_failed(zclient);
1216 }
1217
718e3744 1218 /* Length check. */
634f9ea2 1219 if (length > STREAM_SIZE(zclient->ibuf))
718e3744 1220 {
634f9ea2 1221 struct stream *ns;
1222 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1223 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1224 ns = stream_new(length);
1225 stream_copy(ns, zclient->ibuf);
718e3744 1226 stream_free (zclient->ibuf);
634f9ea2 1227 zclient->ibuf = ns;
718e3744 1228 }
718e3744 1229
1230 /* Read rest of zebra packet. */
634f9ea2 1231 if (already < length)
1232 {
1233 ssize_t nbyte;
1234 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1235 length-already)) == 0) ||
1236 (nbyte == -1))
1237 {
1238 if (zclient_debug)
1239 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1240 return zclient_failed(zclient);
1241 }
1242 if (nbyte != (ssize_t)(length-already))
1243 {
1244 /* Try again later. */
1245 zclient_event (ZCLIENT_READ, zclient);
1246 return 0;
1247 }
1248 }
1249
1250 length -= ZEBRA_HEADER_SIZE;
718e3744 1251
0a589359 1252 if (zclient_debug)
7076bb2f 1253 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
0a589359 1254
718e3744 1255 switch (command)
1256 {
18a6dce6 1257 case ZEBRA_ROUTER_ID_UPDATE:
1258 if (zclient->router_id_update)
7076bb2f 1259 (*zclient->router_id_update) (command, zclient, length, vrf_id);
18a6dce6 1260 break;
1892f15e
DS
1261 case ZEBRA_VRF_ADD:
1262 if (zclient->vrf_add)
1263 (*zclient->vrf_add) (command, zclient, length, vrf_id);
1264 break;
1265 case ZEBRA_VRF_DELETE:
1266 if (zclient->vrf_delete)
1267 (*zclient->vrf_delete) (command, zclient, length, vrf_id);
1268 break;
718e3744 1269 case ZEBRA_INTERFACE_ADD:
1270 if (zclient->interface_add)
7076bb2f 1271 (*zclient->interface_add) (command, zclient, length, vrf_id);
718e3744 1272 break;
1273 case ZEBRA_INTERFACE_DELETE:
1274 if (zclient->interface_delete)
7076bb2f 1275 (*zclient->interface_delete) (command, zclient, length, vrf_id);
718e3744 1276 break;
1277 case ZEBRA_INTERFACE_ADDRESS_ADD:
1278 if (zclient->interface_address_add)
7076bb2f 1279 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
718e3744 1280 break;
1281 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1282 if (zclient->interface_address_delete)
7076bb2f 1283 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
718e3744 1284 break;
68fe91d6 1285 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1286 if (zclient->interface_bfd_dest_update)
7076bb2f 1287 (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id);
d5a5c8f0 1288 break;
a80beece
DS
1289 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1290 if (zclient->interface_nbr_address_add)
7076bb2f 1291 (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id);
a80beece
DS
1292 break;
1293 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1294 if (zclient->interface_nbr_address_delete)
7076bb2f 1295 (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id);
a80beece 1296 break;
718e3744 1297 case ZEBRA_INTERFACE_UP:
1298 if (zclient->interface_up)
7076bb2f 1299 (*zclient->interface_up) (command, zclient, length, vrf_id);
718e3744 1300 break;
1301 case ZEBRA_INTERFACE_DOWN:
1302 if (zclient->interface_down)
7076bb2f 1303 (*zclient->interface_down) (command, zclient, length, vrf_id);
718e3744 1304 break;
1305 case ZEBRA_IPV4_ROUTE_ADD:
1306 if (zclient->ipv4_route_add)
7076bb2f 1307 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
718e3744 1308 break;
1309 case ZEBRA_IPV4_ROUTE_DELETE:
1310 if (zclient->ipv4_route_delete)
7076bb2f 1311 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
718e3744 1312 break;
1313 case ZEBRA_IPV6_ROUTE_ADD:
1314 if (zclient->ipv6_route_add)
7076bb2f 1315 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
718e3744 1316 break;
1317 case ZEBRA_IPV6_ROUTE_DELETE:
1318 if (zclient->ipv6_route_delete)
7076bb2f 1319 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
718e3744 1320 break;
fb018d25
DS
1321 case ZEBRA_NEXTHOP_UPDATE:
1322 if (zclient_debug)
1323 zlog_debug("zclient rcvd nexthop update\n");
1324 if (zclient->nexthop_update)
7076bb2f 1325 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
fb018d25 1326 break;
078430f6
DS
1327 case ZEBRA_IMPORT_CHECK_UPDATE:
1328 if (zclient_debug)
1329 zlog_debug("zclient rcvd import check update\n");
1330 if (zclient->import_check_update)
7076bb2f 1331 (*zclient->import_check_update) (command, zclient, length, vrf_id);
078430f6 1332 break;
c43ed2e4
DS
1333 case ZEBRA_BFD_DEST_REPLAY:
1334 if (zclient->bfd_dest_replay)
7076bb2f 1335 (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id);
c43ed2e4 1336 break;
5048fe14 1337 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
1338 if (zclient->redistribute_route_ipv4_add)
7076bb2f 1339 (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id);
5048fe14 1340 break;
1341 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
1342 if (zclient->redistribute_route_ipv4_del)
7076bb2f 1343 (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id);
5048fe14 1344 break;
1345 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
1346 if (zclient->redistribute_route_ipv6_add)
7076bb2f 1347 (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id);
5048fe14 1348 break;
1349 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
1350 if (zclient->redistribute_route_ipv6_del)
7076bb2f 1351 (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id);
5048fe14 1352 break;
718e3744 1353 default:
1354 break;
1355 }
1356
634f9ea2 1357 if (zclient->sock < 0)
1358 /* Connection was closed during packet processing. */
1359 return -1;
1360
718e3744 1361 /* Register read thread. */
634f9ea2 1362 stream_reset(zclient->ibuf);
718e3744 1363 zclient_event (ZCLIENT_READ, zclient);
1364
1365 return 0;
1366}
1367
1368void
8bb0831e 1369zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type,
7076bb2f 1370 u_short instance, vrf_id_t vrf_id)
718e3744 1371{
718e3744 1372
7076bb2f
FL
1373 if (instance) {
1374 if (command == ZEBRA_REDISTRIBUTE_ADD)
1375 {
1376 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1377 return;
1378 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1379 }
1380 else
1381 {
1382 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1383 return;
1384 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1385 }
1386
1387 } else {
1388 if (command == ZEBRA_REDISTRIBUTE_ADD)
1389 {
1390 if (vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1391 return;
1392 vrf_bitmap_set (zclient->redist[afi][type], vrf_id);
1393 }
1394 else
1395 {
1396 if (!vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1397 return;
1398 vrf_bitmap_unset (zclient->redist[afi][type], vrf_id);
1399 }
1400 }
718e3744 1401
1402 if (zclient->sock > 0)
7076bb2f 1403 zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id);
718e3744 1404}
1405
718e3744 1406
1407void
7076bb2f
FL
1408zclient_redistribute_default (int command, struct zclient *zclient,
1409 vrf_id_t vrf_id)
718e3744 1410{
718e3744 1411
0a589359 1412 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1413 {
7076bb2f 1414 if (vrf_bitmap_check (zclient->default_information, vrf_id))
0a589359 1415 return;
7076bb2f 1416 vrf_bitmap_set (zclient->default_information, vrf_id);
0a589359 1417 }
1418 else
1419 {
7076bb2f 1420 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
0a589359 1421 return;
7076bb2f 1422 vrf_bitmap_unset (zclient->default_information, vrf_id);
0a589359 1423 }
718e3744 1424
1425 if (zclient->sock > 0)
7076bb2f 1426 zebra_message_send (zclient, command, vrf_id);
718e3744 1427}
1428
718e3744 1429static void
1430zclient_event (enum event event, struct zclient *zclient)
1431{
1432 switch (event)
1433 {
1434 case ZCLIENT_SCHEDULE:
1435 if (! zclient->t_connect)
1436 zclient->t_connect =
4140ca4d 1437 thread_add_event (zclient->master, zclient_connect, zclient, 0);
718e3744 1438 break;
1439 case ZCLIENT_CONNECT:
1440 if (zclient->fail >= 10)
1441 return;
1442 if (zclient_debug)
8ddca704 1443 zlog_debug ("zclient connect schedule interval is %d",
718e3744 1444 zclient->fail < 3 ? 10 : 60);
1445 if (! zclient->t_connect)
1446 zclient->t_connect =
4140ca4d 1447 thread_add_timer (zclient->master, zclient_connect, zclient,
718e3744 1448 zclient->fail < 3 ? 10 : 60);
1449 break;
1450 case ZCLIENT_READ:
1451 zclient->t_read =
4140ca4d 1452 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
718e3744 1453 break;
1454 }
1455}
b5114685
VT
1456
1457void
1458zclient_serv_path_set (char *path)
1459{
1460 struct stat sb;
1461
1462 /* reset */
1463 zclient_serv_path = NULL;
1464
1465 /* test if `path' is socket. don't set it otherwise. */
1466 if (stat(path, &sb) == -1)
1467 {
1468 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1469 return;
1470 }
1471
1472 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1473 {
1474 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1475 return;
1476 }
1477
1478 /* it seems that path is unix socket */
1479 zclient_serv_path = path;
1480}
1481