]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
BGP: Fix nexthop registration churn
[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 /* ZAPI_MESSAGE_ONLINK implies interleaving */
615 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_ONLINK))
616 {
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 */
633 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
634 {
635 stream_putc (s, 1);
636 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
637 /* XXX assert(api->nexthop_num == 0); */
638 /* XXX assert(api->ifindex_num == 0); */
639 }
640 else
641 stream_putc (s, api->nexthop_num + api->ifindex_num);
642
643 for (i = 0; i < api->nexthop_num; i++)
644 {
645 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
646 stream_put_in_addr (s, api->nexthop[i]);
647 }
648 for (i = 0; i < api->ifindex_num; i++)
649 {
650 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
651 stream_putl (s, api->ifindex[i]);
652 }
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);
659 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
660 stream_putw (s, api->tag);
661
662 /* Put length at the first point of the stream. */
663 stream_putw_at (s, 0, stream_get_endp (s));
664
665 return zclient_send_message(zclient);
666 }
667
668 #ifdef HAVE_IPV6
669 int
670 zapi_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
681 zclient_create_header (s, cmd, api->vrf_id);
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
733 int
734 zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
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
745 zclient_create_header (s, cmd, api->vrf_id);
746
747 /* Put type and nexthop. */
748 stream_putc (s, api->type);
749 stream_putw (s, api->instance);
750 stream_putc (s, api->flags);
751 stream_putc (s, api->message);
752 stream_putw (s, api->safi);
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 {
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);
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);
788 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
789 stream_putw (s, api->tag);
790
791 /* Put length at the first point of the stream. */
792 stream_putw_at (s, 0, stream_get_endp (s));
793
794 return zclient_send_message(zclient);
795 }
796 #endif /* HAVE_IPV6 */
797
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 */
804 int
805 zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type,
806 u_short instance, vrf_id_t vrf_id)
807 {
808 struct stream *s;
809
810 s = zclient->obuf;
811 stream_reset(s);
812
813 zclient_create_header (s, command, vrf_id);
814 stream_putc (s, afi);
815 stream_putc (s, type);
816 stream_putw (s, instance);
817
818 stream_putw_at (s, 0, stream_get_endp (s));
819
820 return zclient_send_message(zclient);
821 }
822
823 /* Router-id update from zebra daemon. */
824 void
825 zebra_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
837 /* Interface addition from zebra daemon. */
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 |
855 * | |
856 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
857 * | metric |
858 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
859 * | ifmtu |
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * | ifmtu6 |
862 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
863 * | bandwidth |
864 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
865 * | sockaddr_dl |
866 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
867 */
868
869 struct interface *
870 zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
871 {
872 struct interface *ifp;
873 char ifname_tmp[INTERFACE_NAMSIZ];
874
875 /* Read interface name. */
876 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
877
878 /* Lookup/create interface by name. */
879 ifp = if_get_by_name_len_vrf (ifname_tmp,
880 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
881 vrf_id);
882
883 zebra_interface_if_set_value (s, ifp);
884
885 return ifp;
886 }
887
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 */
895 struct interface *
896 zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
897 {
898 struct interface *ifp;
899 char ifname_tmp[INTERFACE_NAMSIZ];
900
901 /* Read interface name. */
902 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
903
904 /* Lookup this by interface index. */
905 ifp = if_lookup_by_name_len_vrf (ifname_tmp,
906 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
907 vrf_id);
908
909 /* If such interface does not exist, indicate an error */
910 if (! ifp)
911 return NULL;
912
913 zebra_interface_if_set_value (s, ifp);
914
915 return ifp;
916 }
917
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
950 void
951 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
952 {
953 /* Read interface's index. */
954 ifp->ifindex = stream_getl (s);
955 ifp->status = stream_getc (s);
956
957 /* Read interface's value. */
958 ifp->flags = stream_getq (s);
959 ifp->ptm_enable = stream_getc (s);
960 ifp->ptm_status = stream_getc (s);
961 ifp->metric = stream_getl (s);
962 ifp->mtu = stream_getl (s);
963 ifp->mtu6 = stream_getl (s);
964 ifp->bandwidth = stream_getl (s);
965 #ifdef HAVE_STRUCT_SOCKADDR_DL
966 stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
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 */
972 }
973
974 static int
975 memconstant(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
985
986 struct connected *
987 zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
988 {
989 unsigned int ifindex;
990 struct interface *ifp;
991 struct connected *ifc;
992 struct prefix p, d;
993 int family;
994 int plen;
995 u_char ifc_flags;
996
997 memset (&p, 0, sizeof(p));
998 memset (&d, 0, sizeof(d));
999
1000 /* Get interface index. */
1001 ifindex = stream_getl (s);
1002
1003 /* Lookup index. */
1004 ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
1005 if (ifp == NULL)
1006 {
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);
1011 return NULL;
1012 }
1013
1014 /* Fetch flag. */
1015 ifc_flags = stream_getc (s);
1016
1017 /* Fetch interface address. */
1018 family = p.family = stream_getc (s);
1019
1020 plen = prefix_blen (&p);
1021 stream_get (&p.u.prefix, s, plen);
1022 p.prefixlen = stream_getc (s);
1023
1024 /* Fetch destination address. */
1025 stream_get (&d.u.prefix, s, plen);
1026 d.family = family;
1027
1028 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1029 {
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));
1033 if (ifc != NULL)
1034 {
1035 ifc->flags = ifc_flags;
1036 if (ifc->destination)
1037 ifc->destination->prefixlen = ifc->address->prefixlen;
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 }
1048 }
1049 }
1050 else
1051 {
1052 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1053 ifc = connected_delete_by_prefix(ifp, &p);
1054 }
1055
1056 return ifc;
1057 }
1058
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 */
1083 struct nbr_connected *
1084 zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id)
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 }
1134
1135 /* Zebra client message read function. */
1136 static int
1137 zclient_read (struct thread *thread)
1138 {
1139 size_t already;
1140 uint16_t length, command;
1141 uint8_t marker, version;
1142 vrf_id_t vrf_id;
1143 struct zclient *zclient;
1144
1145 /* Get socket to zebra. */
1146 zclient = THREAD_ARG (thread);
1147 zclient->t_read = NULL;
1148
1149 /* Read zebra header (if we don't have it already). */
1150 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
1151 {
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;
1168 }
1169
1170 /* Reset to read from the beginning of the incoming packet. */
1171 stream_set_getp(zclient->ibuf, 0);
1172
1173 /* Fetch header values. */
1174 length = stream_getw (zclient->ibuf);
1175 marker = stream_getc (zclient->ibuf);
1176 version = stream_getc (zclient->ibuf);
1177 vrf_id = stream_getw (zclient->ibuf);
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
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
1194 /* Length check. */
1195 if (length > STREAM_SIZE(zclient->ibuf))
1196 {
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);
1202 stream_free (zclient->ibuf);
1203 zclient->ibuf = ns;
1204 }
1205
1206 /* Read rest of zebra packet. */
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;
1227
1228 if (zclient_debug)
1229 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
1230
1231 switch (command)
1232 {
1233 case ZEBRA_ROUTER_ID_UPDATE:
1234 if (zclient->router_id_update)
1235 (*zclient->router_id_update) (command, zclient, length, vrf_id);
1236 break;
1237 case ZEBRA_INTERFACE_ADD:
1238 if (zclient->interface_add)
1239 (*zclient->interface_add) (command, zclient, length, vrf_id);
1240 break;
1241 case ZEBRA_INTERFACE_DELETE:
1242 if (zclient->interface_delete)
1243 (*zclient->interface_delete) (command, zclient, length, vrf_id);
1244 break;
1245 case ZEBRA_INTERFACE_ADDRESS_ADD:
1246 if (zclient->interface_address_add)
1247 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
1248 break;
1249 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1250 if (zclient->interface_address_delete)
1251 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
1252 break;
1253 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1254 if (zclient->interface_bfd_dest_update)
1255 (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id);
1256 break;
1257 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1258 if (zclient->interface_nbr_address_add)
1259 (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id);
1260 break;
1261 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1262 if (zclient->interface_nbr_address_delete)
1263 (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id);
1264 break;
1265 case ZEBRA_INTERFACE_UP:
1266 if (zclient->interface_up)
1267 (*zclient->interface_up) (command, zclient, length, vrf_id);
1268 break;
1269 case ZEBRA_INTERFACE_DOWN:
1270 if (zclient->interface_down)
1271 (*zclient->interface_down) (command, zclient, length, vrf_id);
1272 break;
1273 case ZEBRA_IPV4_ROUTE_ADD:
1274 if (zclient->ipv4_route_add)
1275 (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
1276 break;
1277 case ZEBRA_IPV4_ROUTE_DELETE:
1278 if (zclient->ipv4_route_delete)
1279 (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
1280 break;
1281 case ZEBRA_IPV6_ROUTE_ADD:
1282 if (zclient->ipv6_route_add)
1283 (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
1284 break;
1285 case ZEBRA_IPV6_ROUTE_DELETE:
1286 if (zclient->ipv6_route_delete)
1287 (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
1288 break;
1289 case ZEBRA_NEXTHOP_UPDATE:
1290 if (zclient_debug)
1291 zlog_debug("zclient rcvd nexthop update\n");
1292 if (zclient->nexthop_update)
1293 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
1294 break;
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)
1299 (*zclient->import_check_update) (command, zclient, length, vrf_id);
1300 break;
1301 case ZEBRA_BFD_DEST_REPLAY:
1302 if (zclient->bfd_dest_replay)
1303 (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id);
1304 break;
1305 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
1306 if (zclient->redistribute_route_ipv4_add)
1307 (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id);
1308 break;
1309 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
1310 if (zclient->redistribute_route_ipv4_del)
1311 (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id);
1312 break;
1313 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
1314 if (zclient->redistribute_route_ipv6_add)
1315 (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id);
1316 break;
1317 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
1318 if (zclient->redistribute_route_ipv6_del)
1319 (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id);
1320 break;
1321 default:
1322 break;
1323 }
1324
1325 if (zclient->sock < 0)
1326 /* Connection was closed during packet processing. */
1327 return -1;
1328
1329 /* Register read thread. */
1330 stream_reset(zclient->ibuf);
1331 zclient_event (ZCLIENT_READ, zclient);
1332
1333 return 0;
1334 }
1335
1336 void
1337 zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type,
1338 u_short instance, vrf_id_t vrf_id)
1339 {
1340
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 }
1369
1370 if (zclient->sock > 0)
1371 zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id);
1372 }
1373
1374
1375 void
1376 zclient_redistribute_default (int command, struct zclient *zclient,
1377 vrf_id_t vrf_id)
1378 {
1379
1380 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1381 {
1382 if (vrf_bitmap_check (zclient->default_information, vrf_id))
1383 return;
1384 vrf_bitmap_set (zclient->default_information, vrf_id);
1385 }
1386 else
1387 {
1388 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
1389 return;
1390 vrf_bitmap_unset (zclient->default_information, vrf_id);
1391 }
1392
1393 if (zclient->sock > 0)
1394 zebra_message_send (zclient, command, vrf_id);
1395 }
1396
1397 static void
1398 zclient_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 =
1405 thread_add_event (zclient->master, zclient_connect, zclient, 0);
1406 break;
1407 case ZCLIENT_CONNECT:
1408 if (zclient->fail >= 10)
1409 return;
1410 if (zclient_debug)
1411 zlog_debug ("zclient connect schedule interval is %d",
1412 zclient->fail < 3 ? 10 : 60);
1413 if (! zclient->t_connect)
1414 zclient->t_connect =
1415 thread_add_timer (zclient->master, zclient_connect, zclient,
1416 zclient->fail < 3 ? 10 : 60);
1417 break;
1418 case ZCLIENT_READ:
1419 zclient->t_read =
1420 thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
1421 break;
1422 }
1423 }
1424
1425 void
1426 zclient_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