]> git.proxmox.com Git - mirror_frr.git/blame - lib/zclient.c
Merge branch 'stable/3.0'
[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 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "stream.h"
634f9ea2 26#include "buffer.h"
718e3744 27#include "network.h"
7922fc65
DS
28#include "vrf.h"
29#include "vrf_int.h"
718e3744 30#include "if.h"
31#include "log.h"
32#include "thread.h"
33#include "zclient.h"
34#include "memory.h"
35#include "table.h"
5b30316e 36#include "nexthop.h"
fea12efb 37#include "mpls.h"
6b0655a2 38
4a1ab8e4 39DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient")
14878121 40DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs")
4a1ab8e4 41
718e3744 42/* Zebra client events. */
43enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
44
45/* Prototype for event manager. */
46static void zclient_event (enum event, struct zclient *);
47
744f4685 48const char *zclient_serv_path = NULL;
b5114685 49
718e3744 50/* This file local debug flag. */
51int zclient_debug = 0;
6b0655a2 52
718e3744 53/* Allocate zclient structure. */
54struct zclient *
4140ca4d 55zclient_new (struct thread_master *master)
718e3744 56{
57 struct zclient *zclient;
393deb9b 58 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
718e3744 59
60 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
61 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
634f9ea2 62 zclient->wb = buffer_new(0);
4140ca4d 63 zclient->master = master;
718e3744 64
65 return zclient;
66}
67
228da428 68/* This function is only called when exiting, because
634f9ea2 69 many parts of the code do not check for I/O errors, so they could
70 reference an invalid pointer if the structure was ever freed.
634f9ea2 71
228da428 72 Free zclient structure. */
718e3744 73void
74zclient_free (struct zclient *zclient)
75{
634f9ea2 76 if (zclient->ibuf)
77 stream_free(zclient->ibuf);
78 if (zclient->obuf)
79 stream_free(zclient->obuf);
80 if (zclient->wb)
81 buffer_free(zclient->wb);
82
718e3744 83 XFREE (MTYPE_ZCLIENT, zclient);
84}
85
43e7c3b4 86u_short *
7c8ff89e
DS
87redist_check_instance (struct redist_proto *red, u_short instance)
88{
89 struct listnode *node;
90 u_short *id;
91
92 if (!red->instances)
43e7c3b4 93 return NULL;
7c8ff89e
DS
94
95 for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
96 if (*id == instance)
43e7c3b4 97 return id;
7c8ff89e 98
43e7c3b4 99 return NULL;
7c8ff89e
DS
100}
101
102void
103redist_add_instance (struct redist_proto *red, u_short instance)
104{
105 u_short *in;
106
107 red->enabled = 1;
108
109 if (!red->instances)
110 red->instances = list_new();
111
14878121 112 in = XMALLOC (MTYPE_REDIST_INST, sizeof(u_short));
7c8ff89e
DS
113 *in = instance;
114 listnode_add (red->instances, in);
115}
116
117void
118redist_del_instance (struct redist_proto *red, u_short instance)
119{
43e7c3b4 120 u_short *id;
7c8ff89e 121
43e7c3b4
RW
122 id = redist_check_instance (red, instance);
123 if (! id)
24873f0c 124 return;
7c8ff89e 125
43e7c3b4 126 listnode_delete(red->instances, id);
14878121 127 XFREE (MTYPE_REDIST_INST, id);
43e7c3b4 128 if (!red->instances->count)
7c8ff89e 129 {
43e7c3b4
RW
130 red->enabled = 0;
131 list_free(red->instances);
132 red->instances = NULL;
7c8ff89e
DS
133 }
134}
135
718e3744 136/* Stop zebra client services. */
137void
138zclient_stop (struct zclient *zclient)
139{
615d4265
DL
140 afi_t afi;
141 int i;
142
718e3744 143 if (zclient_debug)
8ddca704 144 zlog_debug ("zclient stopped");
718e3744 145
146 /* Stop threads. */
634f9ea2 147 THREAD_OFF(zclient->t_read);
148 THREAD_OFF(zclient->t_connect);
149 THREAD_OFF(zclient->t_write);
150
151 /* Reset streams. */
152 stream_reset(zclient->ibuf);
153 stream_reset(zclient->obuf);
154
155 /* Empty the write buffer. */
156 buffer_reset(zclient->wb);
718e3744 157
158 /* Close socket. */
159 if (zclient->sock >= 0)
160 {
161 close (zclient->sock);
162 zclient->sock = -1;
163 }
164 zclient->fail = 0;
615d4265
DL
165
166 for (afi = AFI_IP; afi < AFI_MAX; afi++)
41246cb6
DS
167 {
168 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
169 {
170 vrf_bitmap_free(zclient->redist[afi][i]);
171 zclient->redist[afi][i] = VRF_BITMAP_NULL;
172 }
173 redist_del_instance(&zclient->mi_redist[afi][zclient->redist_default],
174 zclient->instance);
175 }
176
615d4265
DL
177 vrf_bitmap_free(zclient->default_information);
178 zclient->default_information = VRF_BITMAP_NULL;
718e3744 179}
180
181void
182zclient_reset (struct zclient *zclient)
183{
7076bb2f 184 afi_t afi;
3d68677e 185
718e3744 186 zclient_stop (zclient);
3d68677e
DS
187
188 for (afi = AFI_IP; afi < AFI_MAX; afi++)
7076bb2f 189 redist_del_instance (&zclient->mi_redist[afi][zclient->redist_default], zclient->instance);
3d68677e 190
7c8ff89e 191 zclient_init (zclient, zclient->redist_default, zclient->instance);
718e3744 192}
193
b5114685
VT
194#ifdef HAVE_TCP_ZEBRA
195
718e3744 196/* Make socket to zebra daemon. Return zebra socket. */
b5114685 197static int
634f9ea2 198zclient_socket(void)
718e3744 199{
200 int sock;
201 int ret;
202 struct sockaddr_in serv;
203
204 /* We should think about IPv6 connection. */
205 sock = socket (AF_INET, SOCK_STREAM, 0);
206 if (sock < 0)
207 return -1;
208
209 /* Make server socket. */
210 memset (&serv, 0, sizeof (struct sockaddr_in));
211 serv.sin_family = AF_INET;
212 serv.sin_port = htons (ZEBRA_PORT);
6f0e3f6e 213#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 214 serv.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 215#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 216 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
217
218 /* Connect to zebra. */
219 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
220 if (ret < 0)
221 {
b0e67bb0
DS
222 if (zclient_debug)
223 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__,
224 errno, safe_strerror (errno));
718e3744 225 close (sock);
226 return -1;
227 }
228 return sock;
229}
230
3414d035 231#else
b5114685 232
718e3744 233/* For sockaddr_un. */
234#include <sys/un.h>
235
1b91e000 236static int
8c328f11 237zclient_socket_un (const char *path)
718e3744 238{
239 int ret;
240 int sock, len;
241 struct sockaddr_un addr;
242
243 sock = socket (AF_UNIX, SOCK_STREAM, 0);
244 if (sock < 0)
245 return -1;
246
247 /* Make server socket. */
248 memset (&addr, 0, sizeof (struct sockaddr_un));
249 addr.sun_family = AF_UNIX;
250 strncpy (addr.sun_path, path, strlen (path));
6f0e3f6e 251#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
718e3744 252 len = addr.sun_len = SUN_LEN(&addr);
253#else
254 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
6f0e3f6e 255#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 256
257 ret = connect (sock, (struct sockaddr *) &addr, len);
258 if (ret < 0)
259 {
b0e67bb0
DS
260 if (zclient_debug)
261 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__,
262 errno, safe_strerror (errno));
718e3744 263 close (sock);
264 return -1;
265 }
266 return sock;
267}
268
3414d035
VT
269#endif /* HAVE_TCP_ZEBRA */
270
b5114685
VT
271/**
272 * Connect to zebra daemon.
273 * @param zclient a pointer to zclient structure
274 * @return socket fd just to make sure that connection established
275 * @see zclient_init
276 * @see zclient_new
277 */
278int
279zclient_socket_connect (struct zclient *zclient)
280{
281#ifdef HAVE_TCP_ZEBRA
282 zclient->sock = zclient_socket ();
283#else
12e41d03 284 zclient->sock = zclient_socket_un (zclient_serv_path_get());
b5114685
VT
285#endif
286 return zclient->sock;
287}
288
634f9ea2 289static int
290zclient_failed(struct zclient *zclient)
291{
292 zclient->fail++;
293 zclient_stop(zclient);
294 zclient_event(ZCLIENT_CONNECT, zclient);
295 return -1;
296}
297
298static int
299zclient_flush_data(struct thread *thread)
300{
301 struct zclient *zclient = THREAD_ARG(thread);
302
303 zclient->t_write = NULL;
304 if (zclient->sock < 0)
305 return -1;
306 switch (buffer_flush_available(zclient->wb, zclient->sock))
307 {
308 case BUFFER_ERROR:
309 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
310 __func__, zclient->sock);
311 return zclient_failed(zclient);
312 break;
313 case BUFFER_PENDING:
66e78ae6
QY
314 zclient->t_write = NULL;
315 thread_add_write(zclient->master, zclient_flush_data, zclient, zclient->sock,
316 &zclient->t_write);
634f9ea2 317 break;
318 case BUFFER_EMPTY:
319 break;
320 }
321 return 0;
322}
323
718e3744 324int
634f9ea2 325zclient_send_message(struct zclient *zclient)
326{
327 if (zclient->sock < 0)
328 return -1;
329 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
330 stream_get_endp(zclient->obuf)))
331 {
332 case BUFFER_ERROR:
333 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
334 __func__, zclient->sock);
335 return zclient_failed(zclient);
336 break;
337 case BUFFER_EMPTY:
338 THREAD_OFF(zclient->t_write);
339 break;
340 case BUFFER_PENDING:
ffa2c898
QY
341 thread_add_write(zclient->master, zclient_flush_data, zclient,
342 zclient->sock, &zclient->t_write);
634f9ea2 343 break;
344 }
345 return 0;
346}
347
d211086a 348void
7076bb2f 349zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
c1b9800a 350{
351 /* length placeholder, caller can update */
352 stream_putw (s, ZEBRA_HEADER_SIZE);
353 stream_putc (s, ZEBRA_HEADER_MARKER);
354 stream_putc (s, ZSERV_VERSION);
7076bb2f 355 stream_putw (s, vrf_id);
c1b9800a 356 stream_putw (s, command);
357}
358
55119089
ND
359int
360zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
e7a2870b 361 u_char *version, vrf_id_t *vrf_id, u_int16_t *cmd)
55119089
ND
362{
363 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
364 return -1;
365
366 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
367 *marker = stream_getc (s);
368 *version = stream_getc (s);
369 *vrf_id = stream_getw (s);
370 *cmd = stream_getw (s);
371
195dd232
DS
372 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
373 {
374 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
375 __func__, sock, *marker, *version);
376 return -1;
377 }
378
55119089
ND
379 if (*size && stream_read (s, sock, *size) != *size)
380 return -1;
381
382 return 0;
383}
384
634f9ea2 385/* Send simple Zebra message. */
386static int
7076bb2f 387zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
718e3744 388{
389 struct stream *s;
390
391 /* Get zclient output buffer. */
392 s = zclient->obuf;
393 stream_reset (s);
394
395 /* Send very simple command only Zebra message. */
7076bb2f 396 zclient_create_header (s, command, vrf_id);
c1b9800a 397
634f9ea2 398 return zclient_send_message(zclient);
718e3744 399}
400
2ea1ab1c
VT
401static int
402zebra_hello_send (struct zclient *zclient)
403{
404 struct stream *s;
405
406 if (zclient->redist_default)
407 {
408 s = zclient->obuf;
409 stream_reset (s);
410
7076bb2f
FL
411 /* The VRF ID in the HELLO message is always 0. */
412 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
2ea1ab1c 413 stream_putc (s, zclient->redist_default);
7c8ff89e 414 stream_putw (s, zclient->instance);
2ea1ab1c
VT
415 stream_putw_at (s, 0, stream_get_endp (s));
416 return zclient_send_message(zclient);
417 }
418
419 return 0;
420}
421
0e5223e7 422/* Send register requests to zebra daemon for the information in a VRF. */
7076bb2f 423void
0e5223e7 424zclient_send_reg_requests (struct zclient *zclient, vrf_id_t vrf_id)
7076bb2f
FL
425{
426 int i;
427 afi_t afi;
428
429 /* zclient is disabled. */
430 if (! zclient->enable)
431 return;
432
433 /* If not connected to the zebra yet. */
434 if (zclient->sock < 0)
435 return;
436
437 if (zclient_debug)
0e5223e7 438 zlog_debug ("%s: send register messages for VRF %u", __func__, vrf_id);
7076bb2f
FL
439
440 /* We need router-id information. */
441 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
442
443 /* We need interface information. */
444 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
445
446 /* Set unwanted redistribute route. */
447 for (afi = AFI_IP; afi < AFI_MAX; afi++)
448 vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id);
449
450 /* Flush all redistribute request. */
451 if (vrf_id == VRF_DEFAULT)
452 for (afi = AFI_IP; afi < AFI_MAX; afi++)
453 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
454 if (zclient->mi_redist[afi][i].enabled)
455 {
456 struct listnode *node;
457 u_short *id;
458
459 for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id))
460 if (!(i == zclient->redist_default && *id == zclient->instance))
461 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i,
462 *id, VRF_DEFAULT);
463 }
464
465 /* Flush all redistribute request. */
466 for (afi = AFI_IP; afi < AFI_MAX; afi++)
467 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
468 if (i != zclient->redist_default &&
469 vrf_bitmap_check (zclient->redist[afi][i], vrf_id))
470 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, 0, vrf_id);
471
472 /* If default information is needed. */
473 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
474 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
475}
476
0e5223e7 477/* Send unregister requests to zebra daemon for the information in a VRF. */
478void
479zclient_send_dereg_requests (struct zclient *zclient, vrf_id_t vrf_id)
480{
481 int i;
482 afi_t afi;
483
484 /* zclient is disabled. */
485 if (! zclient->enable)
486 return;
487
488 /* If not connected to the zebra yet. */
489 if (zclient->sock < 0)
490 return;
491
492 if (zclient_debug)
493 zlog_debug ("%s: send deregister messages for VRF %u", __func__, vrf_id);
494
495 /* We need router-id information. */
496 zebra_message_send (zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id);
497
498 /* We need interface information. */
499 zebra_message_send (zclient, ZEBRA_INTERFACE_DELETE, vrf_id);
500
501 /* Set unwanted redistribute route. */
502 for (afi = AFI_IP; afi < AFI_MAX; afi++)
503 vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id);
504
505 /* Flush all redistribute request. */
506 if (vrf_id == VRF_DEFAULT)
507 for (afi = AFI_IP; afi < AFI_MAX; afi++)
508 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
509 if (zclient->mi_redist[afi][i].enabled)
510 {
511 struct listnode *node;
512 u_short *id;
513
514 for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id))
515 if (!(i == zclient->redist_default && *id == zclient->instance))
516 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i,
517 *id, VRF_DEFAULT);
518 }
519
520 /* Flush all redistribute request. */
521 for (afi = AFI_IP; afi < AFI_MAX; afi++)
522 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
523 if (i != zclient->redist_default &&
524 vrf_bitmap_check (zclient->redist[afi][i], vrf_id))
525 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i, 0, vrf_id);
526
527 /* If default information is needed. */
528 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
529 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, vrf_id);
530}
531
4a04e5f7 532/* Send request to zebra daemon to start or stop RA. */
533void
534zclient_send_interface_radv_req (struct zclient *zclient, vrf_id_t vrf_id,
5c81b96a 535 struct interface *ifp, int enable, int ra_interval)
4a04e5f7 536{
537 struct stream *s;
538
539 /* zclient is disabled. */
540 if (!zclient->enable)
541 return;
542
543 /* If not connected to the zebra yet. */
544 if (zclient->sock < 0)
545 return;
546
547 /* Form and send message. */
548 s = zclient->obuf;
549 stream_reset (s);
550
551 if (enable)
552 zclient_create_header (s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id);
553 else
554 zclient_create_header (s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id);
555
556 stream_putl (s, ifp->ifindex);
5c81b96a 557 stream_putl (s, ra_interval);
4a04e5f7 558
559 stream_putw_at (s, 0, stream_get_endp (s));
560
561 zclient_send_message(zclient);
562}
563
718e3744 564/* Make connection to zebra daemon. */
565int
566zclient_start (struct zclient *zclient)
567{
7076bb2f
FL
568 if (zclient_debug)
569 zlog_info ("zclient_start is called");
570
571 /* zclient is disabled. */
572 if (! zclient->enable)
573 return 0;
718e3744 574
718e3744 575 /* If already connected to the zebra. */
576 if (zclient->sock >= 0)
577 return 0;
578
579 /* Check connect thread. */
580 if (zclient->t_connect)
581 return 0;
582
b0e67bb0 583 if (zclient_socket_connect(zclient) < 0)
7076bb2f
FL
584 {
585 if (zclient_debug)
586 zlog_debug ("zclient connection fail");
587 zclient->fail++;
588 zclient_event (ZCLIENT_CONNECT, zclient);
589 return -1;
590 }
718e3744 591
7076bb2f
FL
592 if (set_nonblocking(zclient->sock) < 0)
593 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
718e3744 594
7076bb2f
FL
595 /* Clear fail count. */
596 zclient->fail = 0;
597 if (zclient_debug)
598 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
718e3744 599
7076bb2f
FL
600 /* Create read thread. */
601 zclient_event (ZCLIENT_READ, zclient);
718e3744 602
7076bb2f 603 zebra_hello_send (zclient);
718e3744 604
7076bb2f
FL
605 /* Inform the successful connection. */
606 if (zclient->zebra_connected)
607 (*zclient->zebra_connected) (zclient);
718e3744 608
7076bb2f 609 return 0;
718e3744 610}
6b0655a2 611
078430f6
DS
612/* Initialize zebra client. Argument redist_default is unwanted
613 redistribute route type. */
614void
615zclient_init (struct zclient *zclient, int redist_default, u_short instance)
616{
617 int afi, i;
618
619 /* Enable zebra client connection by default. */
620 zclient->enable = 1;
621
622 /* Set -1 to the default socket value. */
623 zclient->sock = -1;
624
625 /* Clear redistribution flags. */
626 for (afi = AFI_IP; afi < AFI_MAX; afi++)
627 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
7076bb2f 628 zclient->redist[afi][i] = vrf_bitmap_init();
078430f6
DS
629
630 /* Set unwanted redistribute route. bgpd does not need BGP route
631 redistribution. */
632 zclient->redist_default = redist_default;
633 zclient->instance = instance;
634 /* Pending: make afi(s) an arg. */
635 for (afi = AFI_IP; afi < AFI_MAX; afi++)
7076bb2f 636 redist_add_instance (&zclient->mi_redist[afi][redist_default], instance);
078430f6
DS
637
638 /* Set default-information redistribute to zero. */
7076bb2f 639 zclient->default_information = vrf_bitmap_init ();;
078430f6
DS
640
641 if (zclient_debug)
642 zlog_debug ("zclient_start is called");
643
7076bb2f
FL
644 zclient_event (ZCLIENT_SCHEDULE, zclient);
645}
078430f6 646
7076bb2f
FL
647/* This function is a wrapper function for calling zclient_start from
648 timer or event thread. */
649static int
650zclient_connect (struct thread *t)
651{
652 struct zclient *zclient;
078430f6 653
7076bb2f
FL
654 zclient = THREAD_ARG (t);
655 zclient->t_connect = NULL;
078430f6 656
078430f6 657 if (zclient_debug)
7076bb2f 658 zlog_debug ("zclient_connect is called");
078430f6 659
7076bb2f 660 return zclient_start (zclient);
078430f6
DS
661}
662
0a589359 663 /*
664 * "xdr_encode"-like interface that allows daemon (client) to send
665 * a message to zebra server for a route that needs to be
666 * added/deleted to the kernel. Info about the route is specified
667 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
668 * the info down the zclient socket using the stream_* functions.
669 *
670 * The corresponding read ("xdr_decode") function on the server
671 * side is zread_ipv4_add()/zread_ipv4_delete().
672 *
673 * 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
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | Length (2) | Command | Route Type |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 * | ZEBRA Flags | Message Flags | Prefix length |
678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 * | Destination IPv4 Prefix for route |
680 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 * | Nexthop count |
682 * +-+-+-+-+-+-+-+-+
683 *
684 *
685 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
686 * described, as per the Nexthop count. Each nexthop described as:
687 *
688 * +-+-+-+-+-+-+-+-+
689 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | IPv4 Nexthop address or Interface Index number |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 *
694 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
695 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
696 * nexthop information is provided, and the message describes a prefix
697 * to blackhole or reject route.
698 *
c8a1cb5c
DS
699 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
700 * infrastructure was built around the traditional (32-bit "gate OR
701 * ifindex") nexthop data unit. A special encoding can be used to feed
702 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
703 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
704 * fields as follows:
705 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
706 * - .nexthop_num == .ifindex_num
707 * - .nexthop and .ifindex are filled with gate and ifindex parts of
708 * each compound nexthop, both in the same order
709 *
710 * zapi_ipv4_route() will produce two nexthop data units for each such
711 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
712 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
713 *
0a589359 714 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
715 * byte value.
716 *
717 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
718 * byte value.
719 *
dc9ffce8 720 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
0d9551dc 721 *
c50ca33a
TT
722 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
723 *
0a589359 724 * XXX: No attention paid to alignment.
725 */
718e3744 726int
0a589359 727zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
728 struct zapi_ipv4 *api)
718e3744 729{
730 int i;
731 int psize;
732 struct stream *s;
733
734 /* Reset stream. */
735 s = zclient->obuf;
736 stream_reset (s);
7076bb2f 737
a64448ba
DS
738 /* Some checks for labeled-unicast. The current expectation is that each
739 * nexthop is accompanied by a label in the case of labeled-unicast.
740 */
741 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
742 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
743 {
744 /* We expect prefixes installed with labels and the number to match
745 * the number of nexthops.
746 */
747 assert (api->label_num == api->nexthop_num);
748 }
749
7076bb2f 750 zclient_create_header (s, cmd, api->vrf_id);
c1b9800a 751
752 /* Put type and nexthop. */
718e3744 753 stream_putc (s, api->type);
7c8ff89e 754 stream_putw (s, api->instance);
0fc452dc 755 stream_putl (s, api->flags);
718e3744 756 stream_putc (s, api->message);
5a616c08 757 stream_putw (s, api->safi);
718e3744 758
718e3744 759 /* Put prefix information. */
760 psize = PSIZE (p->prefixlen);
761 stream_putc (s, p->prefixlen);
0a589359 762 stream_write (s, (u_char *) & p->prefix, psize);
718e3744 763
764 /* Nexthop, ifindex, distance and metric information. */
d44ca835 765 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
a64448ba 766 {
c8a1cb5c 767 /* traditional 32-bit data units */
595db7f1 768 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
769 {
770 stream_putc (s, 1);
5b30316e 771 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
0a589359 772 /* XXX assert(api->nexthop_num == 0); */
773 /* XXX assert(api->ifindex_num == 0); */
595db7f1 774 }
775 else
776 stream_putc (s, api->nexthop_num + api->ifindex_num);
718e3744 777
778 for (i = 0; i < api->nexthop_num; i++)
595db7f1 779 {
5b30316e 780 stream_putc (s, NEXTHOP_TYPE_IPV4);
595db7f1 781 stream_put_in_addr (s, api->nexthop[i]);
a64448ba
DS
782 /* For labeled-unicast, each nexthop is followed by label. */
783 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
784 stream_putl (s, api->label[i]);
595db7f1 785 }
718e3744 786 for (i = 0; i < api->ifindex_num; i++)
595db7f1 787 {
5b30316e 788 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
595db7f1 789 stream_putl (s, api->ifindex[i]);
790 }
718e3744 791 }
792
793 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
794 stream_putc (s, api->distance);
795 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
796 stream_putl (s, api->metric);
0d9551dc 797 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
dc9ffce8 798 stream_putl (s, api->tag);
c50ca33a
TT
799 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
800 stream_putl (s, api->mtu);
718e3744 801
802 /* Put length at the first point of the stream. */
803 stream_putw_at (s, 0, stream_get_endp (s));
804
634f9ea2 805 return zclient_send_message(zclient);
718e3744 806}
807
8a92a8a0
DS
808int
809zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient,
810 struct prefix_ipv4 *p, struct zapi_ipv6 *api)
811{
812 int i;
813 int psize;
814 struct stream *s;
815
816 /* Reset stream. */
817 s = zclient->obuf;
818 stream_reset (s);
819
a64448ba
DS
820 /* Some checks for labeled-unicast. The current expectation is that each
821 * nexthop is accompanied by a label in the case of labeled-unicast.
822 */
823 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
824 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
825 {
826 /* We expect prefixes installed with labels and the number to match
827 * the number of nexthops.
828 */
829 assert (api->label_num == api->nexthop_num);
830 }
831
7076bb2f 832 zclient_create_header (s, cmd, api->vrf_id);
8a92a8a0
DS
833
834 /* Put type and nexthop. */
835 stream_putc (s, api->type);
836 stream_putw (s, api->instance);
0fc452dc 837 stream_putl (s, api->flags);
8a92a8a0
DS
838 stream_putc (s, api->message);
839 stream_putw (s, api->safi);
840
841 /* Put prefix information. */
842 psize = PSIZE (p->prefixlen);
843 stream_putc (s, p->prefixlen);
844 stream_write (s, (u_char *) & p->prefix, psize);
845
846 /* Nexthop, ifindex, distance and metric information. */
847 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
848 {
849 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
850 {
851 stream_putc (s, 1);
5b30316e 852 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
8a92a8a0
DS
853 /* XXX assert(api->nexthop_num == 0); */
854 /* XXX assert(api->ifindex_num == 0); */
855 }
856 else
857 stream_putc (s, api->nexthop_num + api->ifindex_num);
858
859 for (i = 0; i < api->nexthop_num; i++)
860 {
5b30316e 861 stream_putc (s, NEXTHOP_TYPE_IPV6);
8a92a8a0 862 stream_write (s, (u_char *)api->nexthop[i], 16);
a64448ba
DS
863 /* For labeled-unicast, each nexthop is followed by label. */
864 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
865 stream_putl (s, api->label[i]);
8a92a8a0
DS
866 }
867 for (i = 0; i < api->ifindex_num; i++)
868 {
5b30316e 869 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
8a92a8a0
DS
870 stream_putl (s, api->ifindex[i]);
871 }
872 }
873
874 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
875 stream_putc (s, api->distance);
876 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
877 stream_putl (s, api->metric);
878 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
dc9ffce8 879 stream_putl (s, api->tag);
c50ca33a
TT
880 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
881 stream_putl (s, api->mtu);
8a92a8a0
DS
882
883 /* Put length at the first point of the stream. */
884 stream_putw_at (s, 0, stream_get_endp (s));
885
886 return zclient_send_message(zclient);
887}
888
718e3744 889int
0a589359 890zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
d75f3b00 891 struct prefix_ipv6 *src_p, struct zapi_ipv6 *api)
718e3744 892{
893 int i;
894 int psize;
895 struct stream *s;
896
d75f3b00
DL
897 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
898 assert (!(api->message & ZAPI_MESSAGE_SRCPFX) == !src_p);
899
718e3744 900 /* Reset stream. */
901 s = zclient->obuf;
902 stream_reset (s);
903
a64448ba
DS
904 /* Some checks for labeled-unicast. The current expectation is that each
905 * nexthop is accompanied by a label in the case of labeled-unicast.
906 */
907 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
908 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
909 {
910 /* We expect prefixes installed with labels and the number to match
911 * the number of nexthops.
912 */
913 assert (api->label_num == api->nexthop_num);
914 }
915
7076bb2f 916 zclient_create_header (s, cmd, api->vrf_id);
718e3744 917
c1b9800a 918 /* Put type and nexthop. */
718e3744 919 stream_putc (s, api->type);
7c8ff89e 920 stream_putw (s, api->instance);
0fc452dc 921 stream_putl (s, api->flags);
718e3744 922 stream_putc (s, api->message);
c7ec179a 923 stream_putw (s, api->safi);
718e3744 924
925 /* Put prefix information. */
926 psize = PSIZE (p->prefixlen);
927 stream_putc (s, p->prefixlen);
928 stream_write (s, (u_char *)&p->prefix, psize);
929
d75f3b00
DL
930 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_SRCPFX))
931 {
932 psize = PSIZE (src_p->prefixlen);
933 stream_putc (s, src_p->prefixlen);
934 stream_write (s, (u_char *)&src_p->prefix, psize);
935 }
936
718e3744 937 /* Nexthop, ifindex, distance and metric information. */
938 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
939 {
c3c0ac83
DS
940 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
941 {
942 stream_putc (s, 1);
5b30316e 943 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
c3c0ac83
DS
944 /* XXX assert(api->nexthop_num == 0); */
945 /* XXX assert(api->ifindex_num == 0); */
946 }
947 else
948 stream_putc (s, api->nexthop_num + api->ifindex_num);
718e3744 949
950 for (i = 0; i < api->nexthop_num; i++)
951 {
5b30316e 952 stream_putc (s, NEXTHOP_TYPE_IPV6);
718e3744 953 stream_write (s, (u_char *)api->nexthop[i], 16);
a64448ba
DS
954 /* For labeled-unicast, each nexthop is followed by label. */
955 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
956 stream_putl (s, api->label[i]);
718e3744 957 }
958 for (i = 0; i < api->ifindex_num; i++)
959 {
5b30316e 960 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
718e3744 961 stream_putl (s, api->ifindex[i]);
962 }
963 }
964
965 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
966 stream_putc (s, api->distance);
967 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
968 stream_putl (s, api->metric);
0d9551dc 969 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
dc9ffce8 970 stream_putl (s, api->tag);
c50ca33a
TT
971 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
972 stream_putl (s, api->mtu);
718e3744 973
974 /* Put length at the first point of the stream. */
975 stream_putw_at (s, 0, stream_get_endp (s));
976
634f9ea2 977 return zclient_send_message(zclient);
718e3744 978}
718e3744 979
0a589359 980/*
981 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
982 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
983 * then set/unset redist[type] in the client handle (a struct zserv) for the
984 * sending client
985 */
718e3744 986int
8bb0831e 987zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type,
7076bb2f 988 u_short instance, vrf_id_t vrf_id)
718e3744 989{
718e3744 990 struct stream *s;
991
634f9ea2 992 s = zclient->obuf;
993 stream_reset(s);
718e3744 994
7076bb2f 995 zclient_create_header (s, command, vrf_id);
8bb0831e 996 stream_putc (s, afi);
718e3744 997 stream_putc (s, type);
7c8ff89e 998 stream_putw (s, instance);
c1b9800a 999
1000 stream_putw_at (s, 0, stream_get_endp (s));
1001
634f9ea2 1002 return zclient_send_message(zclient);
718e3744 1003}
1004
d9178828
PJ
1005/* Get prefix in ZServ format; family should be filled in on prefix */
1006static void
1007zclient_stream_get_prefix (struct stream *s, struct prefix *p)
1008{
1009 size_t plen = prefix_blen (p);
1010 u_char c;
1011 p->prefixlen = 0;
1012
1013 if (plen == 0)
1014 return;
1015
1016 stream_get (&p->u.prefix, s, plen);
1017 c = stream_getc(s);
1018 p->prefixlen = MIN(plen * 8, c);
1019}
1020
18a6dce6 1021/* Router-id update from zebra daemon. */
1022void
1023zebra_router_id_update_read (struct stream *s, struct prefix *rid)
1024{
18a6dce6 1025 /* Fetch interface address. */
1026 rid->family = stream_getc (s);
d9178828
PJ
1027
1028 zclient_stream_get_prefix (s, rid);
18a6dce6 1029}
1030
718e3744 1031/* Interface addition from zebra daemon. */
0a589359 1032/*
1033 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1034 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1035 * 0 1 2 3
1036 * 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
0a589359 1037 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1038 * | ifname |
1039 * | |
1040 * | |
1041 * | |
1042 * | |
1043 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1044 * | ifindex |
1045 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1046 * | status |
0a589359 1047 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1048 * | if_flags |
c77d4546 1049 * | |
0a589359 1050 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1051 * | metric |
1052 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2d7f0d76
DS
1053 * | speed |
1054 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1055 * | ifmtu |
1056 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1057 * | ifmtu6 |
1058 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1059 * | bandwidth |
1060 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1061 * | Link Layer Type |
0a589359 1062 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1063 * | Harware Address Length |
0a589359 1064 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1065 * | Hardware Address if HW lenght different from 0 |
1066 * | ... max INTERFACE_HWADDR_MAX |
0a589359 1067 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1068 * | Link_params? | Whether a link-params follows: 1 or 0.
0a589359 1069 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1070 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1071 * | .... (struct if_link_params). |
0a589359 1072 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1073 */
1074
2fcc254e 1075static void
ebe32f70 1076zclient_vrf_add (struct zclient *zclient, vrf_id_t vrf_id)
1892f15e
DS
1077{
1078 struct vrf *vrf;
1079 char vrfname_tmp[VRF_NAMSIZ];
1da29456 1080 struct vrf_data data;
1892f15e 1081
1da29456 1082 stream_get (&data, zclient->ibuf, sizeof (struct vrf_data));
1892f15e 1083 /* Read interface name. */
ebe32f70 1084 stream_get (vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1892f15e
DS
1085
1086 /* Lookup/create vrf by vrf_id. */
1087 vrf = vrf_get (vrf_id, vrfname_tmp);
1da29456 1088 vrf->data = data;
1892f15e 1089
2fcc254e 1090 vrf_enable (vrf);
1892f15e
DS
1091}
1092
2fcc254e 1093static void
ebe32f70 1094zclient_vrf_delete (struct zclient *zclient, vrf_id_t vrf_id)
1892f15e
DS
1095{
1096 struct vrf *vrf;
1097
1098 /* Lookup vrf by vrf_id. */
5f3d1bdf 1099 vrf = vrf_lookup_by_id (vrf_id);
1892f15e 1100
beef1990
DS
1101 /*
1102 * If a routing protocol doesn't know about a
1103 * vrf that is about to be deleted. There is
1104 * no point in attempting to delete it.
1105 */
1106 if (!vrf)
1107 return;
1108
2fcc254e 1109 vrf_delete (vrf);
1892f15e
DS
1110}
1111
718e3744 1112struct interface *
7076bb2f 1113zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
718e3744 1114{
1115 struct interface *ifp;
02ff83c5 1116 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 1117
1118 /* Read interface name. */
1119 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
1120
a349198f 1121 /* Lookup/create interface by name. */
07a112a1
DS
1122 ifp = if_get_by_name_len (ifname_tmp,
1123 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
1124 vrf_id, 0);
718e3744 1125
51d4ef83 1126 zebra_interface_if_set_value (s, ifp);
718e3744 1127
718e3744 1128 return ifp;
1129}
1130
0a589359 1131/*
1132 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1133 * from zebra server. The format of this message is the same as
1134 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1135 * comments for zebra_interface_add_read), except that no sockaddr_dl
1136 * is sent at the tail of the message.
1137 */
718e3744 1138struct interface *
7076bb2f 1139zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
718e3744 1140{
1141 struct interface *ifp;
02ff83c5 1142 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 1143
1144 /* Read interface name. */
1145 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
1146
1147 /* Lookup this by interface index. */
fa787f91
DS
1148 ifp = if_lookup_by_name_len (ifname_tmp,
1149 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
1150 vrf_id);
b4dd5eaa 1151 if (ifp == NULL)
1152 {
1153 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1154 ifname_tmp, vrf_id);
1155 return NULL;
1156 }
718e3744 1157
51d4ef83 1158 zebra_interface_if_set_value (s, ifp);
718e3744 1159
1160 return ifp;
1161}
1162
16f1b9ee
OD
1163static void
1164link_params_set_value(struct stream *s, struct if_link_params *iflp)
1165{
1166
1167 if (iflp == NULL)
1168 return;
1169
1170 iflp->lp_status = stream_getl (s);
1171 iflp->te_metric = stream_getl (s);
1172 iflp->max_bw = stream_getf (s);
1173 iflp->max_rsv_bw = stream_getf (s);
1174 uint32_t bwclassnum = stream_getl (s);
1175 {
1176 unsigned int i;
1177 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1178 iflp->unrsv_bw[i] = stream_getf (s);
1179 if (i < bwclassnum)
1180 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1181 " - outdated library?",
1182 __func__, bwclassnum, MAX_CLASS_TYPE);
1183 }
1184 iflp->admin_grp = stream_getl (s);
1185 iflp->rmt_as = stream_getl (s);
1186 iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
1187
1188 iflp->av_delay = stream_getl (s);
1189 iflp->min_delay = stream_getl (s);
1190 iflp->max_delay = stream_getl (s);
1191 iflp->delay_var = stream_getl (s);
1192
1193 iflp->pkt_loss = stream_getf (s);
1194 iflp->res_bw = stream_getf (s);
1195 iflp->ava_bw = stream_getf (s);
1196 iflp->use_bw = stream_getf (s);
1197}
1198
1199struct interface *
1200zebra_interface_link_params_read (struct stream *s)
1201{
1202 struct if_link_params *iflp;
c28e5b2a
DS
1203 ifindex_t ifindex;
1204
1205 assert (s);
1206
1207 ifindex = stream_getl (s);
16f1b9ee 1208
7e2b7603 1209 struct interface *ifp = if_lookup_by_index (ifindex, VRF_DEFAULT);
16f1b9ee 1210
c28e5b2a 1211 if (ifp == NULL)
16f1b9ee
OD
1212 {
1213 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1214 __func__, ifindex);
1215 return NULL;
1216 }
1217
1218 if ((iflp = if_link_params_get (ifp)) == NULL)
1219 return NULL;
1220
1221 link_params_set_value(s, iflp);
1222
1223 return ifp;
1224}
1225
1226void
1227zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
1228{
1229 u_char link_params_status = 0;
1230
1231 /* Read interface's index. */
1232 ifp->ifindex = stream_getl (s);
1233 ifp->status = stream_getc (s);
1234
1235 /* Read interface's value. */
1236 ifp->flags = stream_getq (s);
1237 ifp->ptm_enable = stream_getc (s);
1238 ifp->ptm_status = stream_getc (s);
1239 ifp->metric = stream_getl (s);
2d7f0d76 1240 ifp->speed = stream_getl (s);
16f1b9ee
OD
1241 ifp->mtu = stream_getl (s);
1242 ifp->mtu6 = stream_getl (s);
1243 ifp->bandwidth = stream_getl (s);
1244 ifp->ll_type = stream_getl (s);
1245 ifp->hw_addr_len = stream_getl (s);
1246 if (ifp->hw_addr_len)
1247 stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1248
1249 /* Read Traffic Engineering status */
1250 link_params_status = stream_getc (s);
1251 /* Then, Traffic Engineering parameters if any */
1252 if (link_params_status)
1253 {
1254 struct if_link_params *iflp = if_link_params_get (ifp);
1255 link_params_set_value(s, iflp);
1256 }
1257}
1258
1259size_t
1260zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
1261{
1262 size_t w;
1263 struct if_link_params *iflp;
1264 int i;
1265
1266 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1267 return 0;
1268
1269 iflp = ifp->link_params;
1270 w = 0;
1271
1272 w += stream_putl (s, iflp->lp_status);
1273
1274 w += stream_putl (s, iflp->te_metric);
1275 w += stream_putf (s, iflp->max_bw);
1276 w += stream_putf (s, iflp->max_rsv_bw);
1277
1278 w += stream_putl (s, MAX_CLASS_TYPE);
1279 for (i = 0; i < MAX_CLASS_TYPE; i++)
1280 w += stream_putf (s, iflp->unrsv_bw[i]);
1281
1282 w += stream_putl (s, iflp->admin_grp);
1283 w += stream_putl (s, iflp->rmt_as);
1284 w += stream_put_in_addr (s, &iflp->rmt_ip);
1285
1286 w += stream_putl (s, iflp->av_delay);
1287 w += stream_putl (s, iflp->min_delay);
1288 w += stream_putl (s, iflp->max_delay);
1289 w += stream_putl (s, iflp->delay_var);
1290
1291 w += stream_putf (s, iflp->pkt_loss);
1292 w += stream_putf (s, iflp->res_bw);
1293 w += stream_putf (s, iflp->ava_bw);
1294 w += stream_putf (s, iflp->use_bw);
1295
1296 return w;
1297}
1298
1299/*
0a589359 1300 * format of message for address additon is:
1301 * 0
1302 * 0 1 2 3 4 5 6 7
1303 * +-+-+-+-+-+-+-+-+
1304 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1305 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1306 * | |
1307 * + +
1308 * | ifindex |
1309 * + +
1310 * | |
1311 * + +
1312 * | |
1313 * +-+-+-+-+-+-+-+-+
1314 * | ifc_flags | flags for connected address
1315 * +-+-+-+-+-+-+-+-+
1316 * | addr_family |
1317 * +-+-+-+-+-+-+-+-+
1318 * | addr... |
1319 * : :
1320 * | |
1321 * +-+-+-+-+-+-+-+-+
1322 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1323 * +-+-+-+-+-+-+-+-+
1324 * | daddr.. |
1325 * : :
1326 * | |
1327 * +-+-+-+-+-+-+-+-+
0a589359 1328 */
1329
3fb9cd6e 1330static int
1331memconstant(const void *s, int c, size_t n)
1332{
1333 const u_char *p = s;
1334
1335 while (n-- > 0)
1336 if (*p++ != c)
1337 return 0;
1338 return 1;
1339}
1340
d5a5c8f0 1341
718e3744 1342struct connected *
7076bb2f 1343zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
718e3744 1344{
b892f1dd 1345 ifindex_t ifindex;
718e3744 1346 struct interface *ifp;
1347 struct connected *ifc;
d9178828 1348 struct prefix p, d, *dp;
718e3744 1349 int plen;
0a589359 1350 u_char ifc_flags;
718e3744 1351
0a589359 1352 memset (&p, 0, sizeof(p));
1353 memset (&d, 0, sizeof(d));
718e3744 1354
1355 /* Get interface index. */
1356 ifindex = stream_getl (s);
1357
1358 /* Lookup index. */
7e2b7603 1359 ifp = if_lookup_by_index (ifindex, vrf_id);
718e3744 1360 if (ifp == NULL)
1361 {
b4dd5eaa 1362 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1363 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1364 ifindex, vrf_id);
718e3744 1365 return NULL;
1366 }
1367
1368 /* Fetch flag. */
0a589359 1369 ifc_flags = stream_getc (s);
718e3744 1370
1371 /* Fetch interface address. */
d9178828
PJ
1372 d.family = p.family = stream_getc (s);
1373 plen = prefix_blen (&d);
1374
1375 zclient_stream_get_prefix (s, &p);
718e3744 1376
1377 /* Fetch destination address. */
0a589359 1378 stream_get (&d.u.prefix, s, plen);
d9178828
PJ
1379
1380 /* N.B. NULL destination pointers are encoded as all zeroes */
1381 dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
1382
0a589359 1383 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1384 {
38485402
DS
1385 ifc = connected_lookup_prefix_exact (ifp, &p);
1386 if (!ifc)
1387 {
1388 /* N.B. NULL destination pointers are encoded as all zeroes */
d9178828 1389 ifc = connected_add_by_prefix(ifp, &p, dp);
38485402
DS
1390 }
1391 if (ifc)
e4529636
AS
1392 {
1393 ifc->flags = ifc_flags;
1394 if (ifc->destination)
1395 ifc->destination->prefixlen = ifc->address->prefixlen;
90444ca3
DL
1396 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1397 {
1398 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
855110bb 1399 char buf[PREFIX_STRLEN];
90444ca3
DL
1400 zlog_warn("warning: interface %s address %s "
1401 "with peer flag set, but no peer address!",
855110bb
TT
1402 ifp->name,
1403 prefix2str (ifc->address, buf, sizeof buf));
90444ca3
DL
1404 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1405 }
e4529636 1406 }
0a589359 1407 }
1408 else
1409 {
1410 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1411 ifc = connected_delete_by_prefix(ifp, &p);
1412 }
718e3744 1413
1414 return ifc;
1415}
0a589359 1416
a80beece
DS
1417/*
1418 * format of message for neighbor connected address is:
1419 * 0
1420 * 0 1 2 3 4 5 6 7
1421 * +-+-+-+-+-+-+-+-+
1422 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1423 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1424 * | |
1425 * + +
1426 * | ifindex |
1427 * + +
1428 * | |
1429 * + +
1430 * | |
1431 * +-+-+-+-+-+-+-+-+
1432 * | addr_family |
1433 * +-+-+-+-+-+-+-+-+
1434 * | addr... |
1435 * : :
1436 * | |
1437 * +-+-+-+-+-+-+-+-+
1438 * | addr_len | len of addr.
1439 * +-+-+-+-+-+-+-+-+
1440 */
1441struct nbr_connected *
7076bb2f 1442zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id)
a80beece
DS
1443{
1444 unsigned int ifindex;
1445 struct interface *ifp;
1446 struct prefix p;
1447 struct nbr_connected *ifc;
1448
1449 /* Get interface index. */
1450 ifindex = stream_getl (s);
1451
1452 /* Lookup index. */
7e2b7603 1453 ifp = if_lookup_by_index (ifindex, vrf_id);
a80beece
DS
1454 if (ifp == NULL)
1455 {
f1aa3df6 1456 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1457 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD" : "DELETE",
1458 ifindex, vrf_id);
a80beece
DS
1459 return NULL;
1460 }
1461
1462 p.family = stream_getc (s);
1463 stream_get (&p.u.prefix, s, prefix_blen (&p));
1464 p.prefixlen = stream_getc (s);
1465
1466 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD)
1467 {
1468 /* Currently only supporting P2P links, so any new RA source address is
1469 considered as the replacement of the previously learnt Link-Local address. */
1470 if (!(ifc = listnode_head(ifp->nbr_connected)))
1471 {
1472 ifc = nbr_connected_new ();
1473 ifc->address = prefix_new ();
1474 ifc->ifp = ifp;
1475 listnode_add (ifp->nbr_connected, ifc);
1476 }
1477
1478 prefix_copy(ifc->address, &p);
1479 }
1480 else
1481 {
1482 assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1483
1484 ifc = nbr_connected_check(ifp, &p);
1485 if (ifc)
1486 listnode_delete (ifp->nbr_connected, ifc);
1487 }
1488
1489 return ifc;
1490}
6b0655a2 1491
c8e264b6 1492struct interface *
1493zebra_interface_vrf_update_read (struct stream *s, vrf_id_t vrf_id,
1494 vrf_id_t *new_vrf_id)
1495{
1496 unsigned int ifindex;
1497 struct interface *ifp;
1498 vrf_id_t new_id = VRF_DEFAULT;
1499
1500 /* Get interface index. */
1501 ifindex = stream_getl (s);
1502
1503 /* Lookup interface. */
7e2b7603 1504 ifp = if_lookup_by_index (ifindex, vrf_id);
c8e264b6 1505 if (ifp == NULL)
1506 {
1507 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1508 ifindex, vrf_id);
1509 return NULL;
1510 }
1511
1512 /* Fetch new VRF Id. */
1513 new_id = stream_getw (s);
1514
1515 *new_vrf_id = new_id;
1516 return ifp;
1517}
5c7ef8dc 1518
1519/* filter unwanted messages until the expected one arrives */
1520static int
1521zclient_read_sync_response (struct zclient *zclient, u_int16_t expected_cmd)
1522{
1523 struct stream *s;
1524 u_int16_t size;
1525 u_char marker;
1526 u_char version;
1527 vrf_id_t vrf_id;
1528 u_int16_t cmd;
1529 fd_set readfds;
1530 int ret;
1531
1532 ret = 0;
1533 cmd = expected_cmd + 1;
1534 while (ret == 0 && cmd != expected_cmd)
1535 {
1536 s = zclient->ibuf;
1537 stream_reset (s);
1538
1539 /* wait until response arrives */
1540 FD_ZERO (&readfds);
1541 FD_SET (zclient->sock, &readfds);
1542 select (zclient->sock+1, &readfds, NULL, NULL, NULL);
1543 if (!FD_ISSET(zclient->sock, &readfds))
1544 continue;
1545 /* read response */
1546 ret = zclient_read_header (s, zclient->sock, &size, &marker, &version,
1547 &vrf_id, &cmd);
1548 if (zclient_debug)
1549 zlog_debug ("%s: Response (%d bytes) received", __func__, size);
1550 }
1551 if (ret != 0)
1552 {
1553 zlog_err ("%s: Invalid Sync Message Reply", __func__);
1554 return -1;
1555 }
1556
1557 return 0;
1558}
fea12efb 1559/**
1560 * Connect to label manager in a syncronous way
1561 *
1562 * It first writes the request to zcient output buffer and then
1563 * immediately reads the answer from the input buffer.
1564 *
1565 * @param zclient Zclient used to connect to label manager (zebra)
1566 * @result Result of response
1567 */
1568int
1569lm_label_manager_connect (struct zclient *zclient)
1570{
1571 int ret;
1572 struct stream *s;
1573 u_char result;
fea12efb 1574
08c08a35
DS
1575 if (zclient_debug)
1576 zlog_debug ("Connecting to Label Manager");
1577
fea12efb 1578 if (zclient->sock < 0)
1579 return -1;
1580
1581 /* send request */
1582 s = zclient->obuf;
1583 stream_reset (s);
1584 zclient_create_header (s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
1585
1586 /* proto */
1587 stream_putc (s, zclient->redist_default);
1588 /* instance */
1589 stream_putw (s, zclient->instance);
1590
1591 /* Put length at the first point of the stream. */
1592 stream_putw_at(s, 0, stream_get_endp(s));
1593
1594 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1595 if (ret < 0)
1596 {
1597 zlog_err ("%s: can't write to zclient->sock", __func__);
1598 close (zclient->sock);
1599 zclient->sock = -1;
1600 return -1;
1601 }
1602 if (ret == 0)
1603 {
1604 zlog_err ("%s: zclient->sock connection closed", __func__);
1605 close (zclient->sock);
1606 zclient->sock = -1;
1607 return -1;
1608 }
08c08a35
DS
1609 if (zclient_debug)
1610 zlog_debug ("%s: Label manager connect request (%d bytes) sent", __func__, ret);
fea12efb 1611
1612 /* read response */
5c7ef8dc 1613 if (zclient_read_sync_response (zclient, ZEBRA_LABEL_MANAGER_CONNECT) != 0)
1614 return -1;
fea12efb 1615
fea12efb 1616 /* result */
5c7ef8dc 1617 s = zclient->ibuf;
fea12efb 1618 result = stream_getc(s);
08c08a35 1619 if (zclient_debug)
5c7ef8dc 1620 zlog_debug ("%s: Label Manager connect response received, result %u",
1621 __func__, result);
fea12efb 1622
1623 return (int)result;
1624}
1625
1626/**
1627 * Function to request a label chunk in a syncronous way
1628 *
1629 * It first writes the request to zlcient output buffer and then
1630 * immediately reads the answer from the input buffer.
1631 *
1632 * @param zclient Zclient used to connect to label manager (zebra)
1633 * @param keep Avoid garbage collection
1634 * @param chunk_size Amount of labels requested
1635 * @param start To write first assigned chunk label to
1636 * @param end To write last assigned chunk label to
1637 * @result 0 on success, -1 otherwise
1638 */
1639int
1640lm_get_label_chunk (struct zclient *zclient, u_char keep, uint32_t chunk_size,
1641 uint32_t *start, uint32_t *end)
1642{
1643 int ret;
1644 struct stream *s;
fea12efb 1645 u_char response_keep;
1646
08c08a35
DS
1647 if (zclient_debug)
1648 zlog_debug ("Getting Label Chunk");
1649
fea12efb 1650 if (zclient->sock < 0)
1651 return -1;
1652
1653 /* send request */
1654 s = zclient->obuf;
1655 stream_reset (s);
1656 zclient_create_header (s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1657 /* keep */
1658 stream_putc (s, keep);
1659 /* chunk size */
1660 stream_putl (s, chunk_size);
1661 /* Put length at the first point of the stream. */
1662 stream_putw_at(s, 0, stream_get_endp(s));
1663
1664 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1665 if (ret < 0)
1666 {
1667 zlog_err ("%s: can't write to zclient->sock", __func__);
1668 close (zclient->sock);
1669 zclient->sock = -1;
1670 return -1;
1671 }
1672 if (ret == 0)
1673 {
1674 zlog_err ("%s: zclient->sock connection closed", __func__);
1675 close (zclient->sock);
1676 zclient->sock = -1;
1677 return -1;
1678 }
08c08a35
DS
1679 if (zclient_debug)
1680 zlog_debug ("%s: Label chunk request (%d bytes) sent", __func__, ret);
fea12efb 1681
1682 /* read response */
5c7ef8dc 1683 if (zclient_read_sync_response (zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
1684 return -1;
08c08a35 1685
5c7ef8dc 1686 s = zclient->ibuf;
fea12efb 1687 /* keep */
1688 response_keep = stream_getc(s);
1689 /* start and end labels */
1690 *start = stream_getl(s);
1691 *end = stream_getl(s);
1692
1693 /* not owning this response */
5c7ef8dc 1694 if (keep != response_keep)
1695 {
1696 zlog_err ("%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1697 __func__, *start, *end, keep, response_keep);
1698 }
fea12efb 1699 /* sanity */
1700 if (*start > *end
1701 || *start < MPLS_MIN_UNRESERVED_LABEL
5c7ef8dc 1702 || *end > MPLS_MAX_UNRESERVED_LABEL)
1703 {
1704 zlog_err ("%s: Invalid Label chunk: %u - %u", __func__,
1705 *start, *end);
1706 return -1;
1707 }
fea12efb 1708
08c08a35
DS
1709 if (zclient_debug)
1710 zlog_debug ("Label Chunk assign: %u - %u (%u) ",
1711 *start, *end, response_keep);
fea12efb 1712
1713 return 0;
1714}
1715
1716/**
1717 * Function to release a label chunk
1718 *
1719 * @param zclient Zclient used to connect to label manager (zebra)
1720 * @param start First label of chunk
1721 * @param end Last label of chunk
1722 * @result 0 on success, -1 otherwise
1723 */
1724int
1725lm_release_label_chunk (struct zclient *zclient, uint32_t start, uint32_t end)
1726{
1727 int ret;
1728 struct stream *s;
1729
08c08a35
DS
1730 if (zclient_debug)
1731 zlog_debug ("Releasing Label Chunk");
1732
fea12efb 1733 if (zclient->sock < 0)
1734 return -1;
1735
1736 /* send request */
1737 s = zclient->obuf;
1738 stream_reset (s);
1739 zclient_create_header (s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
1740
1741 /* start */
1742 stream_putl (s, start);
1743 /* end */
1744 stream_putl (s, end);
1745
1746 /* Put length at the first point of the stream. */
1747 stream_putw_at(s, 0, stream_get_endp(s));
1748
1749 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1750 if (ret < 0)
1751 {
1752 zlog_err ("%s: can't write to zclient->sock", __func__);
1753 close (zclient->sock);
1754 zclient->sock = -1;
1755 return -1;
1756 }
1757 if (ret == 0)
1758 {
1759 zlog_err ("%s: zclient->sock connection closed", __func__);
1760 close (zclient->sock);
1761 zclient->sock = -1;
1762 return -1;
1763 }
1764
1765 return 0;
1766}
c8e264b6 1767
718e3744 1768/* Zebra client message read function. */
634f9ea2 1769static int
718e3744 1770zclient_read (struct thread *thread)
1771{
634f9ea2 1772 size_t already;
c1b9800a 1773 uint16_t length, command;
1774 uint8_t marker, version;
7076bb2f 1775 vrf_id_t vrf_id;
718e3744 1776 struct zclient *zclient;
1777
1778 /* Get socket to zebra. */
718e3744 1779 zclient = THREAD_ARG (thread);
1780 zclient->t_read = NULL;
1781
634f9ea2 1782 /* Read zebra header (if we don't have it already). */
1783 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
718e3744 1784 {
634f9ea2 1785 ssize_t nbyte;
1786 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1787 ZEBRA_HEADER_SIZE-already)) == 0) ||
1788 (nbyte == -1))
1789 {
1790 if (zclient_debug)
1791 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1792 return zclient_failed(zclient);
1793 }
1794 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1795 {
1796 /* Try again later. */
1797 zclient_event (ZCLIENT_READ, zclient);
1798 return 0;
1799 }
1800 already = ZEBRA_HEADER_SIZE;
718e3744 1801 }
1802
634f9ea2 1803 /* Reset to read from the beginning of the incoming packet. */
1804 stream_set_getp(zclient->ibuf, 0);
718e3744 1805
c1b9800a 1806 /* Fetch header values. */
718e3744 1807 length = stream_getw (zclient->ibuf);
c1b9800a 1808 marker = stream_getc (zclient->ibuf);
1809 version = stream_getc (zclient->ibuf);
7076bb2f 1810 vrf_id = stream_getw (zclient->ibuf);
c1b9800a 1811 command = stream_getw (zclient->ibuf);
1812
1813 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1814 {
1815 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1816 __func__, zclient->sock, marker, version);
1817 return zclient_failed(zclient);
1818 }
1819
634f9ea2 1820 if (length < ZEBRA_HEADER_SIZE)
1821 {
1822 zlog_err("%s: socket %d message length %u is less than %d ",
1823 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1824 return zclient_failed(zclient);
1825 }
1826
718e3744 1827 /* Length check. */
634f9ea2 1828 if (length > STREAM_SIZE(zclient->ibuf))
718e3744 1829 {
634f9ea2 1830 struct stream *ns;
1831 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1832 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1833 ns = stream_new(length);
1834 stream_copy(ns, zclient->ibuf);
718e3744 1835 stream_free (zclient->ibuf);
634f9ea2 1836 zclient->ibuf = ns;
718e3744 1837 }
718e3744 1838
1839 /* Read rest of zebra packet. */
634f9ea2 1840 if (already < length)
1841 {
1842 ssize_t nbyte;
1843 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1844 length-already)) == 0) ||
1845 (nbyte == -1))
1846 {
1847 if (zclient_debug)
1848 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1849 return zclient_failed(zclient);
1850 }
1851 if (nbyte != (ssize_t)(length-already))
1852 {
1853 /* Try again later. */
1854 zclient_event (ZCLIENT_READ, zclient);
1855 return 0;
1856 }
1857 }
1858
1859 length -= ZEBRA_HEADER_SIZE;
718e3744 1860
0a589359 1861 if (zclient_debug)
7076bb2f 1862 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
0a589359 1863
718e3744 1864 switch (command)
1865 {
18a6dce6 1866 case ZEBRA_ROUTER_ID_UPDATE:
1867 if (zclient->router_id_update)
7076bb2f 1868 (*zclient->router_id_update) (command, zclient, length, vrf_id);
18a6dce6 1869 break;
1892f15e 1870 case ZEBRA_VRF_ADD:
ebe32f70 1871 zclient_vrf_add (zclient, vrf_id);
1892f15e
DS
1872 break;
1873 case ZEBRA_VRF_DELETE:
ebe32f70 1874 zclient_vrf_delete (zclient, vrf_id);
1892f15e 1875 break;
718e3744 1876 case ZEBRA_INTERFACE_ADD:
1877 if (zclient->interface_add)
7076bb2f 1878 (*zclient->interface_add) (command, zclient, length, vrf_id);
718e3744 1879 break;
1880 case ZEBRA_INTERFACE_DELETE:
1881 if (zclient->interface_delete)
7076bb2f 1882 (*zclient->interface_delete) (command, zclient, length, vrf_id);
718e3744 1883 break;
1884 case ZEBRA_INTERFACE_ADDRESS_ADD:
1885 if (zclient->interface_address_add)
7076bb2f 1886 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
718e3744 1887 break;
1888 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1889 if (zclient->interface_address_delete)
7076bb2f 1890 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
718e3744 1891 break;
68fe91d6 1892 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1893 if (zclient->interface_bfd_dest_update)
7076bb2f 1894 (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id);
d5a5c8f0 1895 break;
a80beece
DS
1896 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1897 if (zclient->interface_nbr_address_add)
7076bb2f 1898 (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id);
a80beece
DS
1899 break;
1900 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1901 if (zclient->interface_nbr_address_delete)
7076bb2f 1902 (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id);
a80beece 1903 break;
718e3744 1904 case ZEBRA_INTERFACE_UP:
1905 if (zclient->interface_up)
7076bb2f 1906 (*zclient->interface_up) (command, zclient, length, vrf_id);
718e3744 1907 break;
1908 case ZEBRA_INTERFACE_DOWN:
1909 if (zclient->interface_down)
7076bb2f 1910 (*zclient->interface_down) (command, zclient, length, vrf_id);
c8e264b6 1911 break;
1912 case ZEBRA_INTERFACE_VRF_UPDATE:
1913 if (zclient->interface_vrf_update)
1914 (*zclient->interface_vrf_update) (command, zclient, length, vrf_id);
718e3744 1915 break;
fb018d25
DS
1916 case ZEBRA_NEXTHOP_UPDATE:
1917 if (zclient_debug)
1918 zlog_debug("zclient rcvd nexthop update\n");
1919 if (zclient->nexthop_update)
7076bb2f 1920 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
fb018d25 1921 break;
078430f6
DS
1922 case ZEBRA_IMPORT_CHECK_UPDATE:
1923 if (zclient_debug)
1924 zlog_debug("zclient rcvd import check update\n");
1925 if (zclient->import_check_update)
7076bb2f 1926 (*zclient->import_check_update) (command, zclient, length, vrf_id);
078430f6 1927 break;
c43ed2e4
DS
1928 case ZEBRA_BFD_DEST_REPLAY:
1929 if (zclient->bfd_dest_replay)
7076bb2f 1930 (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id);
c43ed2e4 1931 break;
5048fe14 1932 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
1933 if (zclient->redistribute_route_ipv4_add)
7076bb2f 1934 (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id);
5048fe14 1935 break;
1936 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
1937 if (zclient->redistribute_route_ipv4_del)
7076bb2f 1938 (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id);
5048fe14 1939 break;
1940 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
1941 if (zclient->redistribute_route_ipv6_add)
7076bb2f 1942 (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id);
5048fe14 1943 break;
1944 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
1945 if (zclient->redistribute_route_ipv6_del)
7076bb2f 1946 (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id);
5cd459e8 1947 break;
16f1b9ee
OD
1948 case ZEBRA_INTERFACE_LINK_PARAMS:
1949 if (zclient->interface_link_params)
1950 (*zclient->interface_link_params) (command, zclient, length);
5048fe14 1951 break;
5aba114a
DS
1952 case ZEBRA_FEC_UPDATE:
1953 if (zclient_debug)
1954 zlog_debug("zclient rcvd fec update\n");
1955 if (zclient->fec_update)
1956 (*zclient->fec_update) (command, zclient, length);
1957 break;
718e3744 1958 default:
1959 break;
1960 }
1961
634f9ea2 1962 if (zclient->sock < 0)
1963 /* Connection was closed during packet processing. */
1964 return -1;
1965
718e3744 1966 /* Register read thread. */
634f9ea2 1967 stream_reset(zclient->ibuf);
718e3744 1968 zclient_event (ZCLIENT_READ, zclient);
1969
1970 return 0;
1971}
1972
1973void
8bb0831e 1974zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type,
7076bb2f 1975 u_short instance, vrf_id_t vrf_id)
718e3744 1976{
718e3744 1977
7076bb2f
FL
1978 if (instance) {
1979 if (command == ZEBRA_REDISTRIBUTE_ADD)
1980 {
1981 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1982 return;
1983 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1984 }
1985 else
1986 {
1987 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1988 return;
1989 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1990 }
1991
1992 } else {
1993 if (command == ZEBRA_REDISTRIBUTE_ADD)
1994 {
1995 if (vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1996 return;
1997 vrf_bitmap_set (zclient->redist[afi][type], vrf_id);
1998 }
1999 else
2000 {
2001 if (!vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
2002 return;
2003 vrf_bitmap_unset (zclient->redist[afi][type], vrf_id);
2004 }
2005 }
718e3744 2006
2007 if (zclient->sock > 0)
7076bb2f 2008 zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id);
718e3744 2009}
2010
718e3744 2011
2012void
7076bb2f
FL
2013zclient_redistribute_default (int command, struct zclient *zclient,
2014 vrf_id_t vrf_id)
718e3744 2015{
718e3744 2016
0a589359 2017 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
2018 {
7076bb2f 2019 if (vrf_bitmap_check (zclient->default_information, vrf_id))
0a589359 2020 return;
7076bb2f 2021 vrf_bitmap_set (zclient->default_information, vrf_id);
0a589359 2022 }
2023 else
2024 {
7076bb2f 2025 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
0a589359 2026 return;
7076bb2f 2027 vrf_bitmap_unset (zclient->default_information, vrf_id);
0a589359 2028 }
718e3744 2029
2030 if (zclient->sock > 0)
7076bb2f 2031 zebra_message_send (zclient, command, vrf_id);
718e3744 2032}
2033
718e3744 2034static void
2035zclient_event (enum event event, struct zclient *zclient)
2036{
2037 switch (event)
2038 {
2039 case ZCLIENT_SCHEDULE:
ffa2c898
QY
2040 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2041 &zclient->t_connect);
718e3744 2042 break;
2043 case ZCLIENT_CONNECT:
718e3744 2044 if (zclient_debug)
b0e67bb0
DS
2045 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
2046 zclient->fail, zclient->fail < 3 ? 10 : 60);
ffa2c898
QY
2047 thread_add_timer(zclient->master, zclient_connect, zclient,
2048 zclient->fail < 3 ? 10 : 60, &zclient->t_connect);
718e3744 2049 break;
2050 case ZCLIENT_READ:
66e78ae6
QY
2051 zclient->t_read = NULL;
2052 thread_add_read(zclient->master, zclient_read, zclient, zclient->sock,
2053 &zclient->t_read);
718e3744 2054 break;
2055 }
2056}
b5114685 2057
744f4685 2058const char *zclient_serv_path_get()
12e41d03
DL
2059{
2060 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
2061}
2062
b5114685
VT
2063void
2064zclient_serv_path_set (char *path)
2065{
2066 struct stat sb;
2067
2068 /* reset */
2069 zclient_serv_path = NULL;
2070
2071 /* test if `path' is socket. don't set it otherwise. */
2072 if (stat(path, &sb) == -1)
2073 {
2074 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
2075 return;
2076 }
2077
2078 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
2079 {
2080 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
2081 return;
2082 }
2083
2084 /* it seems that path is unix socket */
2085 zclient_serv_path = path;
2086}
2087