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