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