]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
Merge branch 'cmaster' of ssh://stash.cumulusnetworks.com:7999/quag/quagga into cmaster
[mirror_frr.git] / lib / zclient.c
1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
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"
27 #include "buffer.h"
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"
35
36 /* Zebra client events. */
37 enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
38
39 /* Prototype for event manager. */
40 static void zclient_event (enum event, struct zclient *);
41
42 char *zclient_serv_path = NULL;
43
44 /* This file local debug flag. */
45 int zclient_debug = 0;
46
47 /* Allocate zclient structure. */
48 struct zclient *
49 zclient_new (struct thread_master *master)
50 {
51 struct zclient *zclient;
52 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
53
54 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
55 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
56 zclient->wb = buffer_new(0);
57 zclient->master = master;
58
59 return zclient;
60 }
61
62 /* This function is only called when exiting, because
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.
65
66 Free zclient structure. */
67 void
68 zclient_free (struct zclient *zclient)
69 {
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
77 XFREE (MTYPE_ZCLIENT, zclient);
78 }
79
80 int
81 redist_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
96 void
97 redist_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
111 void
112 redist_del_instance (struct redist_proto *red, u_short instance)
113 {
114 struct listnode *node;
115 u_short *id = NULL;
116
117 if (!red->instances)
118 return;
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
136 /* Stop zebra client services. */
137 void
138 zclient_stop (struct zclient *zclient)
139 {
140 if (zclient_debug)
141 zlog_debug ("zclient stopped");
142
143 /* Stop threads. */
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);
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
164 void
165 zclient_reset (struct zclient *zclient)
166 {
167 afi_t afi;
168
169 zclient_stop (zclient);
170
171 for (afi = AFI_IP; afi < AFI_MAX; afi++)
172 redist_del_instance (&zclient->mi_redist[afi][zclient->redist_default], zclient->instance);
173
174 zclient_init (zclient, zclient->redist_default, zclient->instance);
175 }
176
177 #ifdef HAVE_TCP_ZEBRA
178
179 /* Make socket to zebra daemon. Return zebra socket. */
180 static int
181 zclient_socket(void)
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);
196 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
197 serv.sin_len = sizeof (struct sockaddr_in);
198 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
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
211 #else
212
213 /* For sockaddr_un. */
214 #include <sys/un.h>
215
216 static int
217 zclient_socket_un (const char *path)
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));
231 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
232 len = addr.sun_len = SUN_LEN(&addr);
233 #else
234 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
235 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
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
246 #endif /* HAVE_TCP_ZEBRA */
247
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 */
255 int
256 zclient_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
266 static int
267 zclient_failed(struct zclient *zclient)
268 {
269 zclient->fail++;
270 zclient_stop(zclient);
271 zclient_event(ZCLIENT_CONNECT, zclient);
272 return -1;
273 }
274
275 static int
276 zclient_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:
291 zclient->t_write = thread_add_write(zclient->master, zclient_flush_data,
292 zclient, zclient->sock);
293 break;
294 case BUFFER_EMPTY:
295 break;
296 }
297 return 0;
298 }
299
300 int
301 zclient_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:
317 THREAD_WRITE_ON(zclient->master, zclient->t_write,
318 zclient_flush_data, zclient, zclient->sock);
319 break;
320 }
321 return 0;
322 }
323
324 void
325 zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
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);
331 stream_putw (s, vrf_id);
332 stream_putw (s, command);
333 }
334
335 /* Send simple Zebra message. */
336 static int
337 zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
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. */
346 zclient_create_header (s, command, vrf_id);
347
348 return zclient_send_message(zclient);
349 }
350
351 static int
352 zebra_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
361 /* The VRF ID in the HELLO message is always 0. */
362 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
363 stream_putc (s, zclient->redist_default);
364 stream_putw (s, zclient->instance);
365 stream_putw_at (s, 0, stream_get_endp (s));
366 return zclient_send_message(zclient);
367 }
368
369 return 0;
370 }
371
372 /* Send requests to zebra daemon for the information in a VRF. */
373 void
374 zclient_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
427 /* Make connection to zebra daemon. */
428 int
429 zclient_start (struct zclient *zclient)
430 {
431 if (zclient_debug)
432 zlog_info ("zclient_start is called");
433
434 /* zclient is disabled. */
435 if (! zclient->enable)
436 return 0;
437
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
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 }
454
455 if (set_nonblocking(zclient->sock) < 0)
456 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
457
458 /* Clear fail count. */
459 zclient->fail = 0;
460 if (zclient_debug)
461 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
462
463 /* Create read thread. */
464 zclient_event (ZCLIENT_READ, zclient);
465
466 zebra_hello_send (zclient);
467
468 /* Inform the successful connection. */
469 if (zclient->zebra_connected)
470 (*zclient->zebra_connected) (zclient);
471
472 return 0;
473 }
474
475 /* Initialize zebra client. Argument redist_default is unwanted
476 redistribute route type. */
477 void
478 zclient_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++)
491 zclient->redist[afi][i] = vrf_bitmap_init();
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++)
499 redist_add_instance (&zclient->mi_redist[afi][redist_default], instance);
500
501 /* Set default-information redistribute to zero. */
502 zclient->default_information = vrf_bitmap_init ();;
503
504 if (zclient_debug)
505 zlog_debug ("zclient_start is called");
506
507 zclient_event (ZCLIENT_SCHEDULE, zclient);
508 }
509
510 /* This function is a wrapper function for calling zclient_start from
511 timer or event thread. */
512 static int
513 zclient_connect (struct thread *t)
514 {
515 struct zclient *zclient;
516
517 zclient = THREAD_ARG (t);
518 zclient->t_connect = NULL;
519
520 if (zclient_debug)
521 zlog_debug ("zclient_connect is called");
522
523 return zclient_start (zclient);
524 }
525
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 *
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 *
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 *
583 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value
584 *
585 * XXX: No attention paid to alignment.
586 */
587 int
588 zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
589 struct zapi_ipv4 *api)
590 {
591 int i;
592 int psize;
593 struct stream *s;
594
595 /* Reset stream. */
596 s = zclient->obuf;
597 stream_reset (s);
598
599 zclient_create_header (s, cmd, api->vrf_id);
600
601 /* Put type and nexthop. */
602 stream_putc (s, api->type);
603 stream_putw (s, api->instance);
604 stream_putc (s, api->flags);
605 stream_putc (s, api->message);
606 stream_putw (s, api->safi);
607
608 /* Put prefix information. */
609 psize = PSIZE (p->prefixlen);
610 stream_putc (s, p->prefixlen);
611 stream_write (s, (u_char *) & p->prefix, psize);
612
613 /* Nexthop, ifindex, distance and metric information. */
614 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
615 {
616 /* traditional 32-bit data units */
617 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
618 {
619 stream_putc (s, 1);
620 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
621 /* XXX assert(api->nexthop_num == 0); */
622 /* XXX assert(api->ifindex_num == 0); */
623 }
624 else
625 stream_putc (s, api->nexthop_num + api->ifindex_num);
626
627 for (i = 0; i < api->nexthop_num; i++)
628 {
629 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
630 stream_put_in_addr (s, api->nexthop[i]);
631 }
632 for (i = 0; i < api->ifindex_num; i++)
633 {
634 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
635 stream_putl (s, api->ifindex[i]);
636 }
637 }
638
639 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
640 stream_putc (s, api->distance);
641 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
642 stream_putl (s, api->metric);
643 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
644 stream_putw (s, api->tag);
645
646 /* Put length at the first point of the stream. */
647 stream_putw_at (s, 0, stream_get_endp (s));
648
649 return zclient_send_message(zclient);
650 }
651
652 #ifdef HAVE_IPV6
653 int
654 zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient,
655 struct prefix_ipv4 *p, struct zapi_ipv6 *api)
656 {
657 int i;
658 int psize;
659 struct stream *s;
660
661 /* Reset stream. */
662 s = zclient->obuf;
663 stream_reset (s);
664
665 zclient_create_header (s, cmd, api->vrf_id);
666
667 /* Put type and nexthop. */
668 stream_putc (s, api->type);
669 stream_putw (s, api->instance);
670 stream_putc (s, api->flags);
671 stream_putc (s, api->message);
672 stream_putw (s, api->safi);
673
674 /* Put prefix information. */
675 psize = PSIZE (p->prefixlen);
676 stream_putc (s, p->prefixlen);
677 stream_write (s, (u_char *) & p->prefix, psize);
678
679 /* Nexthop, ifindex, distance and metric information. */
680 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
681 {
682 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
683 {
684 stream_putc (s, 1);
685 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
686 /* XXX assert(api->nexthop_num == 0); */
687 /* XXX assert(api->ifindex_num == 0); */
688 }
689 else
690 stream_putc (s, api->nexthop_num + api->ifindex_num);
691
692 for (i = 0; i < api->nexthop_num; i++)
693 {
694 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
695 stream_write (s, (u_char *)api->nexthop[i], 16);
696 }
697 for (i = 0; i < api->ifindex_num; i++)
698 {
699 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
700 stream_putl (s, api->ifindex[i]);
701 }
702 }
703
704 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
705 stream_putc (s, api->distance);
706 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
707 stream_putl (s, api->metric);
708 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
709 stream_putw (s, api->tag);
710
711 /* Put length at the first point of the stream. */
712 stream_putw_at (s, 0, stream_get_endp (s));
713
714 return zclient_send_message(zclient);
715 }
716
717 int
718 zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
719 struct zapi_ipv6 *api)
720 {
721 int i;
722 int psize;
723 struct stream *s;
724
725 /* Reset stream. */
726 s = zclient->obuf;
727 stream_reset (s);
728
729 zclient_create_header (s, cmd, api->vrf_id);
730
731 /* Put type and nexthop. */
732 stream_putc (s, api->type);
733 stream_putw (s, api->instance);
734 stream_putc (s, api->flags);
735 stream_putc (s, api->message);
736 stream_putw (s, api->safi);
737
738 /* Put prefix information. */
739 psize = PSIZE (p->prefixlen);
740 stream_putc (s, p->prefixlen);
741 stream_write (s, (u_char *)&p->prefix, psize);
742
743 /* Nexthop, ifindex, distance and metric information. */
744 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
745 {
746 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
747 {
748 stream_putc (s, 1);
749 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
750 /* XXX assert(api->nexthop_num == 0); */
751 /* XXX assert(api->ifindex_num == 0); */
752 }
753 else
754 stream_putc (s, api->nexthop_num + api->ifindex_num);
755
756 for (i = 0; i < api->nexthop_num; i++)
757 {
758 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
759 stream_write (s, (u_char *)api->nexthop[i], 16);
760 }
761 for (i = 0; i < api->ifindex_num; i++)
762 {
763 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
764 stream_putl (s, api->ifindex[i]);
765 }
766 }
767
768 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
769 stream_putc (s, api->distance);
770 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
771 stream_putl (s, api->metric);
772 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
773 stream_putw (s, api->tag);
774
775 /* Put length at the first point of the stream. */
776 stream_putw_at (s, 0, stream_get_endp (s));
777
778 return zclient_send_message(zclient);
779 }
780 #endif /* HAVE_IPV6 */
781
782 /*
783 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
784 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
785 * then set/unset redist[type] in the client handle (a struct zserv) for the
786 * sending client
787 */
788 int
789 zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type,
790 u_short instance, vrf_id_t vrf_id)
791 {
792 struct stream *s;
793
794 s = zclient->obuf;
795 stream_reset(s);
796
797 zclient_create_header (s, command, vrf_id);
798 stream_putc (s, afi);
799 stream_putc (s, type);
800 stream_putw (s, instance);
801
802 stream_putw_at (s, 0, stream_get_endp (s));
803
804 return zclient_send_message(zclient);
805 }
806
807 /* Router-id update from zebra daemon. */
808 void
809 zebra_router_id_update_read (struct stream *s, struct prefix *rid)
810 {
811 int plen;
812
813 /* Fetch interface address. */
814 rid->family = stream_getc (s);
815
816 plen = prefix_blen (rid);
817 stream_get (&rid->u.prefix, s, plen);
818 rid->prefixlen = stream_getc (s);
819 }
820
821 /* Interface addition from zebra daemon. */
822 /*
823 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
824 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
825 * 0 1 2 3
826 * 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
827 * +-+-+-+-+-+-+-+-+
828 * | type |
829 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
830 * | ifname |
831 * | |
832 * | |
833 * | |
834 * | |
835 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
836 * | ifindex |
837 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
838 * | if_flags |
839 * | |
840 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
841 * | metric |
842 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
843 * | ifmtu |
844 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
845 * | ifmtu6 |
846 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
847 * | bandwidth |
848 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
849 * | sockaddr_dl |
850 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
851 */
852
853 struct interface *
854 zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
855 {
856 struct interface *ifp;
857 char ifname_tmp[INTERFACE_NAMSIZ];
858
859 /* Read interface name. */
860 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
861
862 /* Lookup/create interface by name. */
863 ifp = if_get_by_name_len_vrf (ifname_tmp,
864 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
865 vrf_id);
866
867 zebra_interface_if_set_value (s, ifp);
868
869 return ifp;
870 }
871
872 /*
873 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
874 * from zebra server. The format of this message is the same as
875 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
876 * comments for zebra_interface_add_read), except that no sockaddr_dl
877 * is sent at the tail of the message.
878 */
879 struct interface *
880 zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
881 {
882 struct interface *ifp;
883 char ifname_tmp[INTERFACE_NAMSIZ];
884
885 /* Read interface name. */
886 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
887
888 /* Lookup this by interface index. */
889 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
890 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
891 vrf_id);
892
893 /* If such interface does not exist, indicate an error */
894 if (! ifp)
895 return NULL;
896
897 zebra_interface_if_set_value (s, ifp);
898
899 return ifp;
900 }
901
902 /*
903 * format of message for address additon is:
904 * 0
905 * 0 1 2 3 4 5 6 7
906 * +-+-+-+-+-+-+-+-+
907 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
908 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
909 * | |
910 * + +
911 * | ifindex |
912 * + +
913 * | |
914 * + +
915 * | |
916 * +-+-+-+-+-+-+-+-+
917 * | ifc_flags | flags for connected address
918 * +-+-+-+-+-+-+-+-+
919 * | addr_family |
920 * +-+-+-+-+-+-+-+-+
921 * | addr... |
922 * : :
923 * | |
924 * +-+-+-+-+-+-+-+-+
925 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
926 * +-+-+-+-+-+-+-+-+
927 * | daddr.. |
928 * : :
929 * | |
930 * +-+-+-+-+-+-+-+-+
931 *
932 */
933
934 void
935 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
936 {
937 /* Read interface's index. */
938 ifp->ifindex = stream_getl (s);
939 ifp->status = stream_getc (s);
940
941 /* Read interface's value. */
942 ifp->flags = stream_getq (s);
943 ifp->ptm_enable = stream_getc (s);
944 ifp->ptm_status = stream_getc (s);
945 ifp->metric = stream_getl (s);
946 ifp->mtu = stream_getl (s);
947 ifp->mtu6 = stream_getl (s);
948 ifp->bandwidth = stream_getl (s);
949 #ifdef HAVE_STRUCT_SOCKADDR_DL
950 stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
951 #else
952 ifp->hw_addr_len = stream_getl (s);
953 if (ifp->hw_addr_len)
954 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
955 #endif /* HAVE_STRUCT_SOCKADDR_DL */
956 }
957
958 static int
959 memconstant(const void *s, int c, size_t n)
960 {
961 const u_char *p = s;
962
963 while (n-- > 0)
964 if (*p++ != c)
965 return 0;
966 return 1;
967 }
968
969
970 struct connected *
971 zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
972 {
973 unsigned int ifindex;
974 struct interface *ifp;
975 struct connected *ifc;
976 struct prefix p, d;
977 int family;
978 int plen;
979 u_char ifc_flags;
980
981 memset (&p, 0, sizeof(p));
982 memset (&d, 0, sizeof(d));
983
984 /* Get interface index. */
985 ifindex = stream_getl (s);
986
987 /* Lookup index. */
988 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
989 if (ifp == NULL)
990 {
991 zlog_warn ("zebra_interface_address_read(%s): "
992 "Can't find interface by ifindex: %d ",
993 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
994 ifindex);
995 return NULL;
996 }
997
998 /* Fetch flag. */
999 ifc_flags = stream_getc (s);
1000
1001 /* Fetch interface address. */
1002 family = p.family = stream_getc (s);
1003
1004 plen = prefix_blen (&p);
1005 stream_get (&p.u.prefix, s, plen);
1006 p.prefixlen = stream_getc (s);
1007
1008 /* Fetch destination address. */
1009 stream_get (&d.u.prefix, s, plen);
1010 d.family = family;
1011
1012 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1013 {
1014 /* N.B. NULL destination pointers are encoded as all zeroes */
1015 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
1016 NULL : &d));
1017 if (ifc != NULL)
1018 {
1019 ifc->flags = ifc_flags;
1020 if (ifc->destination)
1021 ifc->destination->prefixlen = ifc->address->prefixlen;
1022 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1023 {
1024 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1025 char buf[BUFSIZ];
1026 prefix2str (ifc->address, buf, sizeof(buf));
1027 zlog_warn("warning: interface %s address %s "
1028 "with peer flag set, but no peer address!",
1029 ifp->name, buf);
1030 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1031 }
1032 }
1033 }
1034 else
1035 {
1036 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1037 ifc = connected_delete_by_prefix(ifp, &p);
1038 }
1039
1040 return ifc;
1041 }
1042
1043 /*
1044 * format of message for neighbor connected address is:
1045 * 0
1046 * 0 1 2 3 4 5 6 7
1047 * +-+-+-+-+-+-+-+-+
1048 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1049 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1050 * | |
1051 * + +
1052 * | ifindex |
1053 * + +
1054 * | |
1055 * + +
1056 * | |
1057 * +-+-+-+-+-+-+-+-+
1058 * | addr_family |
1059 * +-+-+-+-+-+-+-+-+
1060 * | addr... |
1061 * : :
1062 * | |
1063 * +-+-+-+-+-+-+-+-+
1064 * | addr_len | len of addr.
1065 * +-+-+-+-+-+-+-+-+
1066 */
1067 struct nbr_connected *
1068 zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id)
1069 {
1070 unsigned int ifindex;
1071 struct interface *ifp;
1072 struct prefix p;
1073 struct nbr_connected *ifc;
1074
1075 /* Get interface index. */
1076 ifindex = stream_getl (s);
1077
1078 /* Lookup index. */
1079 ifp = if_lookup_by_index (ifindex);
1080 if (ifp == NULL)
1081 {
1082 zlog_warn ("zebra_nbr_interface_address_read(%s): "
1083 "Can't find interface by ifindex: %d ",
1084 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD? "ADD" : "DELETE"),
1085 ifindex);
1086 return NULL;
1087 }
1088
1089 p.family = stream_getc (s);
1090 stream_get (&p.u.prefix, s, prefix_blen (&p));
1091 p.prefixlen = stream_getc (s);
1092
1093 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD)
1094 {
1095 /* Currently only supporting P2P links, so any new RA source address is
1096 considered as the replacement of the previously learnt Link-Local address. */
1097 if (!(ifc = listnode_head(ifp->nbr_connected)))
1098 {
1099 ifc = nbr_connected_new ();
1100 ifc->address = prefix_new ();
1101 ifc->ifp = ifp;
1102 listnode_add (ifp->nbr_connected, ifc);
1103 }
1104
1105 prefix_copy(ifc->address, &p);
1106 }
1107 else
1108 {
1109 assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1110
1111 ifc = nbr_connected_check(ifp, &p);
1112 if (ifc)
1113 listnode_delete (ifp->nbr_connected, ifc);
1114 }
1115
1116 return ifc;
1117 }
1118
1119 /* Zebra client message read function. */
1120 static int
1121 zclient_read (struct thread *thread)
1122 {
1123 size_t already;
1124 uint16_t length, command;
1125 uint8_t marker, version;
1126 vrf_id_t vrf_id;
1127 struct zclient *zclient;
1128
1129 /* Get socket to zebra. */
1130 zclient = THREAD_ARG (thread);
1131 zclient->t_read = NULL;
1132
1133 /* Read zebra header (if we don't have it already). */
1134 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
1135 {
1136 ssize_t nbyte;
1137 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1138 ZEBRA_HEADER_SIZE-already)) == 0) ||
1139 (nbyte == -1))
1140 {
1141 if (zclient_debug)
1142 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1143 return zclient_failed(zclient);
1144 }
1145 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1146 {
1147 /* Try again later. */
1148 zclient_event (ZCLIENT_READ, zclient);
1149 return 0;
1150 }
1151 already = ZEBRA_HEADER_SIZE;
1152 }
1153
1154 /* Reset to read from the beginning of the incoming packet. */
1155 stream_set_getp(zclient->ibuf, 0);
1156
1157 /* Fetch header values. */
1158 length = stream_getw (zclient->ibuf);
1159 marker = stream_getc (zclient->ibuf);
1160 version = stream_getc (zclient->ibuf);
1161 vrf_id = stream_getw (zclient->ibuf);
1162 command = stream_getw (zclient->ibuf);
1163
1164 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1165 {
1166 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1167 __func__, zclient->sock, marker, version);
1168 return zclient_failed(zclient);
1169 }
1170
1171 if (length < ZEBRA_HEADER_SIZE)
1172 {
1173 zlog_err("%s: socket %d message length %u is less than %d ",
1174 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1175 return zclient_failed(zclient);
1176 }
1177
1178 /* Length check. */
1179 if (length > STREAM_SIZE(zclient->ibuf))
1180 {
1181 struct stream *ns;
1182 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1183 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1184 ns = stream_new(length);
1185 stream_copy(ns, zclient->ibuf);
1186 stream_free (zclient->ibuf);
1187 zclient->ibuf = ns;
1188 }
1189
1190 /* Read rest of zebra packet. */
1191 if (already < length)
1192 {
1193 ssize_t nbyte;
1194 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1195 length-already)) == 0) ||
1196 (nbyte == -1))
1197 {
1198 if (zclient_debug)
1199 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1200 return zclient_failed(zclient);
1201 }
1202 if (nbyte != (ssize_t)(length-already))
1203 {
1204 /* Try again later. */
1205 zclient_event (ZCLIENT_READ, zclient);
1206 return 0;
1207 }
1208 }
1209
1210 length -= ZEBRA_HEADER_SIZE;
1211
1212 if (zclient_debug)
1213 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
1214
1215 switch (command)
1216 {
1217 case ZEBRA_ROUTER_ID_UPDATE:
1218 if (zclient->router_id_update)
1219 (*zclient->router_id_update) (command, zclient, length, vrf_id);
1220 break;
1221 case ZEBRA_INTERFACE_ADD:
1222 if (zclient->interface_add)
1223 (*zclient->interface_add) (command, zclient, length, vrf_id);
1224 break;
1225 case ZEBRA_INTERFACE_DELETE:
1226 if (zclient->interface_delete)
1227 (*zclient->interface_delete) (command, zclient, length, vrf_id);
1228 break;
1229 case ZEBRA_INTERFACE_ADDRESS_ADD:
1230 if (zclient->interface_address_add)
1231 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
1232 break;
1233 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1234 if (zclient->interface_address_delete)
1235 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
1236 break;
1237 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1238 if (zclient->interface_bfd_dest_update)
1239 (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id);
1240 break;
1241 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1242 if (zclient->interface_nbr_address_add)
1243 (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id);
1244 break;
1245 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1246 if (zclient->interface_nbr_address_delete)
1247 (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id);
1248 break;
1249 case ZEBRA_INTERFACE_UP:
1250 if (zclient->interface_up)
1251 (*zclient->interface_up) (command, zclient, length, vrf_id);
1252 break;
1253 case ZEBRA_INTERFACE_DOWN:
1254 if (zclient->interface_down)
1255 (*zclient->interface_down) (command, zclient, length, vrf_id);
1256 break;
1257 case ZEBRA_IPV4_ROUTE_ADD:
1258 if (zclient->ipv4_route_add)
1259 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
1260 break;
1261 case ZEBRA_IPV4_ROUTE_DELETE:
1262 if (zclient->ipv4_route_delete)
1263 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
1264 break;
1265 case ZEBRA_IPV6_ROUTE_ADD:
1266 if (zclient->ipv6_route_add)
1267 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
1268 break;
1269 case ZEBRA_IPV6_ROUTE_DELETE:
1270 if (zclient->ipv6_route_delete)
1271 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
1272 break;
1273 case ZEBRA_NEXTHOP_UPDATE:
1274 if (zclient_debug)
1275 zlog_debug("zclient rcvd nexthop update\n");
1276 if (zclient->nexthop_update)
1277 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
1278 break;
1279 case ZEBRA_IMPORT_CHECK_UPDATE:
1280 if (zclient_debug)
1281 zlog_debug("zclient rcvd import check update\n");
1282 if (zclient->import_check_update)
1283 (*zclient->import_check_update) (command, zclient, length, vrf_id);
1284 break;
1285 case ZEBRA_BFD_DEST_REPLAY:
1286 if (zclient->bfd_dest_replay)
1287 (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id);
1288 break;
1289 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
1290 if (zclient->redistribute_route_ipv4_add)
1291 (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id);
1292 break;
1293 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
1294 if (zclient->redistribute_route_ipv4_del)
1295 (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id);
1296 break;
1297 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
1298 if (zclient->redistribute_route_ipv6_add)
1299 (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id);
1300 break;
1301 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
1302 if (zclient->redistribute_route_ipv6_del)
1303 (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id);
1304 break;
1305 default:
1306 break;
1307 }
1308
1309 if (zclient->sock < 0)
1310 /* Connection was closed during packet processing. */
1311 return -1;
1312
1313 /* Register read thread. */
1314 stream_reset(zclient->ibuf);
1315 zclient_event (ZCLIENT_READ, zclient);
1316
1317 return 0;
1318 }
1319
1320 void
1321 zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type,
1322 u_short instance, vrf_id_t vrf_id)
1323 {
1324
1325 if (instance) {
1326 if (command == ZEBRA_REDISTRIBUTE_ADD)
1327 {
1328 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1329 return;
1330 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1331 }
1332 else
1333 {
1334 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1335 return;
1336 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1337 }
1338
1339 } else {
1340 if (command == ZEBRA_REDISTRIBUTE_ADD)
1341 {
1342 if (vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1343 return;
1344 vrf_bitmap_set (zclient->redist[afi][type], vrf_id);
1345 }
1346 else
1347 {
1348 if (!vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1349 return;
1350 vrf_bitmap_unset (zclient->redist[afi][type], vrf_id);
1351 }
1352 }
1353
1354 if (zclient->sock > 0)
1355 zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id);
1356 }
1357
1358
1359 void
1360 zclient_redistribute_default (int command, struct zclient *zclient,
1361 vrf_id_t vrf_id)
1362 {
1363
1364 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1365 {
1366 if (vrf_bitmap_check (zclient->default_information, vrf_id))
1367 return;
1368 vrf_bitmap_set (zclient->default_information, vrf_id);
1369 }
1370 else
1371 {
1372 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
1373 return;
1374 vrf_bitmap_unset (zclient->default_information, vrf_id);
1375 }
1376
1377 if (zclient->sock > 0)
1378 zebra_message_send (zclient, command, vrf_id);
1379 }
1380
1381 static void
1382 zclient_event (enum event event, struct zclient *zclient)
1383 {
1384 switch (event)
1385 {
1386 case ZCLIENT_SCHEDULE:
1387 if (! zclient->t_connect)
1388 zclient->t_connect =
1389 thread_add_event (zclient->master, zclient_connect, zclient, 0);
1390 break;
1391 case ZCLIENT_CONNECT:
1392 if (zclient->fail >= 10)
1393 return;
1394 if (zclient_debug)
1395 zlog_debug ("zclient connect schedule interval is %d",
1396 zclient->fail < 3 ? 10 : 60);
1397 if (! zclient->t_connect)
1398 zclient->t_connect =
1399 thread_add_timer (zclient->master, zclient_connect, zclient,
1400 zclient->fail < 3 ? 10 : 60);
1401 break;
1402 case ZCLIENT_READ:
1403 zclient->t_read =
1404 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
1405 break;
1406 }
1407 }
1408
1409 void
1410 zclient_serv_path_set (char *path)
1411 {
1412 struct stat sb;
1413
1414 /* reset */
1415 zclient_serv_path = NULL;
1416
1417 /* test if `path' is socket. don't set it otherwise. */
1418 if (stat(path, &sb) == -1)
1419 {
1420 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1421 return;
1422 }
1423
1424 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1425 {
1426 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1427 return;
1428 }
1429
1430 /* it seems that path is unix socket */
1431 zclient_serv_path = path;
1432 }
1433