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