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