]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
quagga: option "-z" ("--socket <path>") added
[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 \f
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 extern struct thread_master *master;
43
44 char *zclient_serv_path = NULL;
45
46 /* This file local debug flag. */
47 int zclient_debug = 0;
48 \f
49 /* Allocate zclient structure. */
50 struct zclient *
51 zclient_new ()
52 {
53 struct zclient *zclient;
54 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
55
56 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
57 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
58 zclient->wb = buffer_new(0);
59
60 return zclient;
61 }
62
63 /* This function is only called when exiting, because
64 many parts of the code do not check for I/O errors, so they could
65 reference an invalid pointer if the structure was ever freed.
66
67 Free zclient structure. */
68 void
69 zclient_free (struct zclient *zclient)
70 {
71 if (zclient->ibuf)
72 stream_free(zclient->ibuf);
73 if (zclient->obuf)
74 stream_free(zclient->obuf);
75 if (zclient->wb)
76 buffer_free(zclient->wb);
77
78 XFREE (MTYPE_ZCLIENT, zclient);
79 }
80
81 /* Initialize zebra client. Argument redist_default is unwanted
82 redistribute route type. */
83 void
84 zclient_init (struct zclient *zclient, int redist_default)
85 {
86 int i;
87
88 /* Enable zebra client connection by default. */
89 zclient->enable = 1;
90
91 /* Set -1 to the default socket value. */
92 zclient->sock = -1;
93
94 /* Clear redistribution flags. */
95 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
96 zclient->redist[i] = 0;
97
98 /* Set unwanted redistribute route. bgpd does not need BGP route
99 redistribution. */
100 zclient->redist_default = redist_default;
101 zclient->redist[redist_default] = 1;
102
103 /* Set default-information redistribute to zero. */
104 zclient->default_information = 0;
105
106 /* Schedule first zclient connection. */
107 if (zclient_debug)
108 zlog_debug ("zclient start scheduled");
109
110 zclient_event (ZCLIENT_SCHEDULE, zclient);
111 }
112
113 /* Stop zebra client services. */
114 void
115 zclient_stop (struct zclient *zclient)
116 {
117 if (zclient_debug)
118 zlog_debug ("zclient stopped");
119
120 /* Stop threads. */
121 THREAD_OFF(zclient->t_read);
122 THREAD_OFF(zclient->t_connect);
123 THREAD_OFF(zclient->t_write);
124
125 /* Reset streams. */
126 stream_reset(zclient->ibuf);
127 stream_reset(zclient->obuf);
128
129 /* Empty the write buffer. */
130 buffer_reset(zclient->wb);
131
132 /* Close socket. */
133 if (zclient->sock >= 0)
134 {
135 close (zclient->sock);
136 zclient->sock = -1;
137 }
138 zclient->fail = 0;
139 }
140
141 void
142 zclient_reset (struct zclient *zclient)
143 {
144 zclient_stop (zclient);
145 zclient_init (zclient, zclient->redist_default);
146 }
147
148 #ifdef HAVE_TCP_ZEBRA
149
150 /* Make socket to zebra daemon. Return zebra socket. */
151 static int
152 zclient_socket(void)
153 {
154 int sock;
155 int ret;
156 struct sockaddr_in serv;
157
158 /* We should think about IPv6 connection. */
159 sock = socket (AF_INET, SOCK_STREAM, 0);
160 if (sock < 0)
161 return -1;
162
163 /* Make server socket. */
164 memset (&serv, 0, sizeof (struct sockaddr_in));
165 serv.sin_family = AF_INET;
166 serv.sin_port = htons (ZEBRA_PORT);
167 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
168 serv.sin_len = sizeof (struct sockaddr_in);
169 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
170 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
171
172 /* Connect to zebra. */
173 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
174 if (ret < 0)
175 {
176 close (sock);
177 return -1;
178 }
179 return sock;
180 }
181
182 #endif /* HAVE_TCP_ZEBRA */
183
184 /* For sockaddr_un. */
185 #include <sys/un.h>
186
187 static int
188 zclient_socket_un (const char *path)
189 {
190 int ret;
191 int sock, len;
192 struct sockaddr_un addr;
193
194 sock = socket (AF_UNIX, SOCK_STREAM, 0);
195 if (sock < 0)
196 return -1;
197
198 /* Make server socket. */
199 memset (&addr, 0, sizeof (struct sockaddr_un));
200 addr.sun_family = AF_UNIX;
201 strncpy (addr.sun_path, path, strlen (path));
202 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
203 len = addr.sun_len = SUN_LEN(&addr);
204 #else
205 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
206 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
207
208 ret = connect (sock, (struct sockaddr *) &addr, len);
209 if (ret < 0)
210 {
211 close (sock);
212 return -1;
213 }
214 return sock;
215 }
216
217 /**
218 * Connect to zebra daemon.
219 * @param zclient a pointer to zclient structure
220 * @return socket fd just to make sure that connection established
221 * @see zclient_init
222 * @see zclient_new
223 */
224 int
225 zclient_socket_connect (struct zclient *zclient)
226 {
227 #ifdef HAVE_TCP_ZEBRA
228 zclient->sock = zclient_socket ();
229 #else
230 zclient->sock = zclient_socket_un (zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH);
231 #endif
232 return zclient->sock;
233 }
234
235 static int
236 zclient_failed(struct zclient *zclient)
237 {
238 zclient->fail++;
239 zclient_stop(zclient);
240 zclient_event(ZCLIENT_CONNECT, zclient);
241 return -1;
242 }
243
244 static int
245 zclient_flush_data(struct thread *thread)
246 {
247 struct zclient *zclient = THREAD_ARG(thread);
248
249 zclient->t_write = NULL;
250 if (zclient->sock < 0)
251 return -1;
252 switch (buffer_flush_available(zclient->wb, zclient->sock))
253 {
254 case BUFFER_ERROR:
255 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
256 __func__, zclient->sock);
257 return zclient_failed(zclient);
258 break;
259 case BUFFER_PENDING:
260 zclient->t_write = thread_add_write(master, zclient_flush_data,
261 zclient, zclient->sock);
262 break;
263 case BUFFER_EMPTY:
264 break;
265 }
266 return 0;
267 }
268
269 int
270 zclient_send_message(struct zclient *zclient)
271 {
272 if (zclient->sock < 0)
273 return -1;
274 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
275 stream_get_endp(zclient->obuf)))
276 {
277 case BUFFER_ERROR:
278 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
279 __func__, zclient->sock);
280 return zclient_failed(zclient);
281 break;
282 case BUFFER_EMPTY:
283 THREAD_OFF(zclient->t_write);
284 break;
285 case BUFFER_PENDING:
286 THREAD_WRITE_ON(master, zclient->t_write,
287 zclient_flush_data, zclient, zclient->sock);
288 break;
289 }
290 return 0;
291 }
292
293 void
294 zclient_create_header (struct stream *s, uint16_t command)
295 {
296 /* length placeholder, caller can update */
297 stream_putw (s, ZEBRA_HEADER_SIZE);
298 stream_putc (s, ZEBRA_HEADER_MARKER);
299 stream_putc (s, ZSERV_VERSION);
300 stream_putw (s, command);
301 }
302
303 /* Send simple Zebra message. */
304 static int
305 zebra_message_send (struct zclient *zclient, int command)
306 {
307 struct stream *s;
308
309 /* Get zclient output buffer. */
310 s = zclient->obuf;
311 stream_reset (s);
312
313 /* Send very simple command only Zebra message. */
314 zclient_create_header (s, command);
315
316 return zclient_send_message(zclient);
317 }
318
319 /* Make connection to zebra daemon. */
320 int
321 zclient_start (struct zclient *zclient)
322 {
323 int i;
324
325 if (zclient_debug)
326 zlog_debug ("zclient_start is called");
327
328 /* zclient is disabled. */
329 if (! zclient->enable)
330 return 0;
331
332 /* If already connected to the zebra. */
333 if (zclient->sock >= 0)
334 return 0;
335
336 /* Check connect thread. */
337 if (zclient->t_connect)
338 return 0;
339
340 if (zclient_socket_connect(zclient) < 0)
341 {
342 if (zclient_debug)
343 zlog_debug ("zclient connection fail");
344 zclient->fail++;
345 zclient_event (ZCLIENT_CONNECT, zclient);
346 return -1;
347 }
348
349 if (set_nonblocking(zclient->sock) < 0)
350 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
351
352 /* Clear fail count. */
353 zclient->fail = 0;
354 if (zclient_debug)
355 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
356
357 /* Create read thread. */
358 zclient_event (ZCLIENT_READ, zclient);
359
360 /* We need router-id information. */
361 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD);
362
363 /* We need interface information. */
364 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD);
365
366 /* Flush all redistribute request. */
367 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
368 if (i != zclient->redist_default && zclient->redist[i])
369 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i);
370
371 /* If default information is needed. */
372 if (zclient->default_information)
373 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
374
375 return 0;
376 }
377
378 /* This function is a wrapper function for calling zclient_start from
379 timer or event thread. */
380 static int
381 zclient_connect (struct thread *t)
382 {
383 struct zclient *zclient;
384
385 zclient = THREAD_ARG (t);
386 zclient->t_connect = NULL;
387
388 if (zclient_debug)
389 zlog_debug ("zclient_connect is called");
390
391 return zclient_start (zclient);
392 }
393 \f
394 /*
395 * "xdr_encode"-like interface that allows daemon (client) to send
396 * a message to zebra server for a route that needs to be
397 * added/deleted to the kernel. Info about the route is specified
398 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
399 * the info down the zclient socket using the stream_* functions.
400 *
401 * The corresponding read ("xdr_decode") function on the server
402 * side is zread_ipv4_add()/zread_ipv4_delete().
403 *
404 * 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
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 * | Length (2) | Command | Route Type |
407 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
408 * | ZEBRA Flags | Message Flags | Prefix length |
409 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
410 * | Destination IPv4 Prefix for route |
411 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
412 * | Nexthop count |
413 * +-+-+-+-+-+-+-+-+
414 *
415 *
416 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
417 * described, as per the Nexthop count. Each nexthop described as:
418 *
419 * +-+-+-+-+-+-+-+-+
420 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
422 * | IPv4 Nexthop address or Interface Index number |
423 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 *
425 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
426 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
427 * nexthop information is provided, and the message describes a prefix
428 * to blackhole or reject route.
429 *
430 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
431 * byte value.
432 *
433 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
434 * byte value.
435 *
436 * XXX: No attention paid to alignment.
437 */
438 int
439 zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
440 struct zapi_ipv4 *api)
441 {
442 int i;
443 int psize;
444 struct stream *s;
445
446 /* Reset stream. */
447 s = zclient->obuf;
448 stream_reset (s);
449
450 zclient_create_header (s, cmd);
451
452 /* Put type and nexthop. */
453 stream_putc (s, api->type);
454 stream_putc (s, api->flags);
455 stream_putc (s, api->message);
456
457 /* Put prefix information. */
458 psize = PSIZE (p->prefixlen);
459 stream_putc (s, p->prefixlen);
460 stream_write (s, (u_char *) & p->prefix, psize);
461
462 /* Nexthop, ifindex, distance and metric information. */
463 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
464 {
465 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
466 {
467 stream_putc (s, 1);
468 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
469 /* XXX assert(api->nexthop_num == 0); */
470 /* XXX assert(api->ifindex_num == 0); */
471 }
472 else
473 stream_putc (s, api->nexthop_num + api->ifindex_num);
474
475 for (i = 0; i < api->nexthop_num; i++)
476 {
477 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
478 stream_put_in_addr (s, api->nexthop[i]);
479 }
480 for (i = 0; i < api->ifindex_num; i++)
481 {
482 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
483 stream_putl (s, api->ifindex[i]);
484 }
485 }
486
487 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
488 stream_putc (s, api->distance);
489 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
490 stream_putl (s, api->metric);
491
492 /* Put length at the first point of the stream. */
493 stream_putw_at (s, 0, stream_get_endp (s));
494
495 return zclient_send_message(zclient);
496 }
497
498 #ifdef HAVE_IPV6
499 int
500 zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
501 struct zapi_ipv6 *api)
502 {
503 int i;
504 int psize;
505 struct stream *s;
506
507 /* Reset stream. */
508 s = zclient->obuf;
509 stream_reset (s);
510
511 zclient_create_header (s, cmd);
512
513 /* Put type and nexthop. */
514 stream_putc (s, api->type);
515 stream_putc (s, api->flags);
516 stream_putc (s, api->message);
517
518 /* Put prefix information. */
519 psize = PSIZE (p->prefixlen);
520 stream_putc (s, p->prefixlen);
521 stream_write (s, (u_char *)&p->prefix, psize);
522
523 /* Nexthop, ifindex, distance and metric information. */
524 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
525 {
526 stream_putc (s, api->nexthop_num + api->ifindex_num);
527
528 for (i = 0; i < api->nexthop_num; i++)
529 {
530 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
531 stream_write (s, (u_char *)api->nexthop[i], 16);
532 }
533 for (i = 0; i < api->ifindex_num; i++)
534 {
535 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
536 stream_putl (s, api->ifindex[i]);
537 }
538 }
539
540 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
541 stream_putc (s, api->distance);
542 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
543 stream_putl (s, api->metric);
544
545 /* Put length at the first point of the stream. */
546 stream_putw_at (s, 0, stream_get_endp (s));
547
548 return zclient_send_message(zclient);
549 }
550 #endif /* HAVE_IPV6 */
551
552 /*
553 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
554 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
555 * then set/unset redist[type] in the client handle (a struct zserv) for the
556 * sending client
557 */
558 int
559 zebra_redistribute_send (int command, struct zclient *zclient, int type)
560 {
561 struct stream *s;
562
563 s = zclient->obuf;
564 stream_reset(s);
565
566 zclient_create_header (s, command);
567 stream_putc (s, type);
568
569 stream_putw_at (s, 0, stream_get_endp (s));
570
571 return zclient_send_message(zclient);
572 }
573
574 /* Router-id update from zebra daemon. */
575 void
576 zebra_router_id_update_read (struct stream *s, struct prefix *rid)
577 {
578 int plen;
579
580 /* Fetch interface address. */
581 rid->family = stream_getc (s);
582
583 plen = prefix_blen (rid);
584 stream_get (&rid->u.prefix, s, plen);
585 rid->prefixlen = stream_getc (s);
586 }
587
588 /* Interface addition from zebra daemon. */
589 /*
590 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
591 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
592 * 0 1 2 3
593 * 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
594 * +-+-+-+-+-+-+-+-+
595 * | type |
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | ifname |
598 * | |
599 * | |
600 * | |
601 * | |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | ifindex |
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 * | if_flags |
606 * | |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | metric |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * | ifmtu |
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * | ifmtu6 |
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * | bandwidth |
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * | sockaddr_dl |
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 */
619
620 struct interface *
621 zebra_interface_add_read (struct stream *s)
622 {
623 struct interface *ifp;
624 char ifname_tmp[INTERFACE_NAMSIZ];
625
626 /* Read interface name. */
627 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
628
629 /* Lookup/create interface by name. */
630 ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ));
631
632 /* Read interface's index. */
633 ifp->ifindex = stream_getl (s);
634
635 /* Read interface's value. */
636 ifp->status = stream_getc (s);
637 ifp->flags = stream_getq (s);
638 ifp->metric = stream_getl (s);
639 ifp->mtu = stream_getl (s);
640 ifp->mtu6 = stream_getl (s);
641 ifp->bandwidth = stream_getl (s);
642 #ifdef HAVE_STRUCT_SOCKADDR_DL
643 stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
644 #else
645 ifp->hw_addr_len = stream_getl (s);
646 if (ifp->hw_addr_len)
647 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
648 #endif /* HAVE_STRUCT_SOCKADDR_DL */
649
650 return ifp;
651 }
652
653 /*
654 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
655 * from zebra server. The format of this message is the same as
656 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
657 * comments for zebra_interface_add_read), except that no sockaddr_dl
658 * is sent at the tail of the message.
659 */
660 struct interface *
661 zebra_interface_state_read (struct stream *s)
662 {
663 struct interface *ifp;
664 char ifname_tmp[INTERFACE_NAMSIZ];
665
666 /* Read interface name. */
667 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
668
669 /* Lookup this by interface index. */
670 ifp = if_lookup_by_name_len (ifname_tmp,
671 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
672
673 /* If such interface does not exist, indicate an error */
674 if (! ifp)
675 return NULL;
676
677 /* Read interface's index. */
678 ifp->ifindex = stream_getl (s);
679
680 /* Read interface's value. */
681 ifp->status = stream_getc (s);
682 ifp->flags = stream_getq (s);
683 ifp->metric = stream_getl (s);
684 ifp->mtu = stream_getl (s);
685 ifp->mtu6 = stream_getl (s);
686 ifp->bandwidth = stream_getl (s);
687
688 return ifp;
689 }
690
691 /*
692 * format of message for address additon is:
693 * 0
694 * 0 1 2 3 4 5 6 7
695 * +-+-+-+-+-+-+-+-+
696 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
697 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
698 * | |
699 * + +
700 * | ifindex |
701 * + +
702 * | |
703 * + +
704 * | |
705 * +-+-+-+-+-+-+-+-+
706 * | ifc_flags | flags for connected address
707 * +-+-+-+-+-+-+-+-+
708 * | addr_family |
709 * +-+-+-+-+-+-+-+-+
710 * | addr... |
711 * : :
712 * | |
713 * +-+-+-+-+-+-+-+-+
714 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
715 * +-+-+-+-+-+-+-+-+
716 * | daddr.. |
717 * : :
718 * | |
719 * +-+-+-+-+-+-+-+-+
720 *
721 */
722
723 void
724 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
725 {
726 /* Read interface's index. */
727 ifp->ifindex = stream_getl (s);
728 ifp->status = stream_getc (s);
729
730 /* Read interface's value. */
731 ifp->flags = stream_getq (s);
732 ifp->metric = stream_getl (s);
733 ifp->mtu = stream_getl (s);
734 ifp->mtu6 = stream_getl (s);
735 ifp->bandwidth = stream_getl (s);
736 }
737
738 static int
739 memconstant(const void *s, int c, size_t n)
740 {
741 const u_char *p = s;
742
743 while (n-- > 0)
744 if (*p++ != c)
745 return 0;
746 return 1;
747 }
748
749 struct connected *
750 zebra_interface_address_read (int type, struct stream *s)
751 {
752 unsigned int ifindex;
753 struct interface *ifp;
754 struct connected *ifc;
755 struct prefix p, d;
756 int family;
757 int plen;
758 u_char ifc_flags;
759
760 memset (&p, 0, sizeof(p));
761 memset (&d, 0, sizeof(d));
762
763 /* Get interface index. */
764 ifindex = stream_getl (s);
765
766 /* Lookup index. */
767 ifp = if_lookup_by_index (ifindex);
768 if (ifp == NULL)
769 {
770 zlog_warn ("zebra_interface_address_read(%s): "
771 "Can't find interface by ifindex: %d ",
772 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
773 ifindex);
774 return NULL;
775 }
776
777 /* Fetch flag. */
778 ifc_flags = stream_getc (s);
779
780 /* Fetch interface address. */
781 family = p.family = stream_getc (s);
782
783 plen = prefix_blen (&p);
784 stream_get (&p.u.prefix, s, plen);
785 p.prefixlen = stream_getc (s);
786
787 /* Fetch destination address. */
788 stream_get (&d.u.prefix, s, plen);
789 d.family = family;
790
791 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
792 {
793 /* N.B. NULL destination pointers are encoded as all zeroes */
794 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
795 NULL : &d));
796 if (ifc != NULL)
797 {
798 ifc->flags = ifc_flags;
799 if (ifc->destination)
800 ifc->destination->prefixlen = ifc->address->prefixlen;
801 }
802 }
803 else
804 {
805 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
806 ifc = connected_delete_by_prefix(ifp, &p);
807 }
808
809 return ifc;
810 }
811
812 \f
813 /* Zebra client message read function. */
814 static int
815 zclient_read (struct thread *thread)
816 {
817 int ret;
818 size_t already;
819 uint16_t length, command;
820 uint8_t marker, version;
821 struct zclient *zclient;
822
823 /* Get socket to zebra. */
824 zclient = THREAD_ARG (thread);
825 zclient->t_read = NULL;
826
827 /* Read zebra header (if we don't have it already). */
828 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
829 {
830 ssize_t nbyte;
831 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
832 ZEBRA_HEADER_SIZE-already)) == 0) ||
833 (nbyte == -1))
834 {
835 if (zclient_debug)
836 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
837 return zclient_failed(zclient);
838 }
839 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
840 {
841 /* Try again later. */
842 zclient_event (ZCLIENT_READ, zclient);
843 return 0;
844 }
845 already = ZEBRA_HEADER_SIZE;
846 }
847
848 /* Reset to read from the beginning of the incoming packet. */
849 stream_set_getp(zclient->ibuf, 0);
850
851 /* Fetch header values. */
852 length = stream_getw (zclient->ibuf);
853 marker = stream_getc (zclient->ibuf);
854 version = stream_getc (zclient->ibuf);
855 command = stream_getw (zclient->ibuf);
856
857 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
858 {
859 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
860 __func__, zclient->sock, marker, version);
861 return zclient_failed(zclient);
862 }
863
864 if (length < ZEBRA_HEADER_SIZE)
865 {
866 zlog_err("%s: socket %d message length %u is less than %d ",
867 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
868 return zclient_failed(zclient);
869 }
870
871 /* Length check. */
872 if (length > STREAM_SIZE(zclient->ibuf))
873 {
874 struct stream *ns;
875 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
876 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
877 ns = stream_new(length);
878 stream_copy(ns, zclient->ibuf);
879 stream_free (zclient->ibuf);
880 zclient->ibuf = ns;
881 }
882
883 /* Read rest of zebra packet. */
884 if (already < length)
885 {
886 ssize_t nbyte;
887 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
888 length-already)) == 0) ||
889 (nbyte == -1))
890 {
891 if (zclient_debug)
892 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
893 return zclient_failed(zclient);
894 }
895 if (nbyte != (ssize_t)(length-already))
896 {
897 /* Try again later. */
898 zclient_event (ZCLIENT_READ, zclient);
899 return 0;
900 }
901 }
902
903 length -= ZEBRA_HEADER_SIZE;
904
905 if (zclient_debug)
906 zlog_debug("zclient 0x%p command 0x%x \n", zclient, command);
907
908 switch (command)
909 {
910 case ZEBRA_ROUTER_ID_UPDATE:
911 if (zclient->router_id_update)
912 ret = (*zclient->router_id_update) (command, zclient, length);
913 break;
914 case ZEBRA_INTERFACE_ADD:
915 if (zclient->interface_add)
916 ret = (*zclient->interface_add) (command, zclient, length);
917 break;
918 case ZEBRA_INTERFACE_DELETE:
919 if (zclient->interface_delete)
920 ret = (*zclient->interface_delete) (command, zclient, length);
921 break;
922 case ZEBRA_INTERFACE_ADDRESS_ADD:
923 if (zclient->interface_address_add)
924 ret = (*zclient->interface_address_add) (command, zclient, length);
925 break;
926 case ZEBRA_INTERFACE_ADDRESS_DELETE:
927 if (zclient->interface_address_delete)
928 ret = (*zclient->interface_address_delete) (command, zclient, length);
929 break;
930 case ZEBRA_INTERFACE_UP:
931 if (zclient->interface_up)
932 ret = (*zclient->interface_up) (command, zclient, length);
933 break;
934 case ZEBRA_INTERFACE_DOWN:
935 if (zclient->interface_down)
936 ret = (*zclient->interface_down) (command, zclient, length);
937 break;
938 case ZEBRA_IPV4_ROUTE_ADD:
939 if (zclient->ipv4_route_add)
940 ret = (*zclient->ipv4_route_add) (command, zclient, length);
941 break;
942 case ZEBRA_IPV4_ROUTE_DELETE:
943 if (zclient->ipv4_route_delete)
944 ret = (*zclient->ipv4_route_delete) (command, zclient, length);
945 break;
946 case ZEBRA_IPV6_ROUTE_ADD:
947 if (zclient->ipv6_route_add)
948 ret = (*zclient->ipv6_route_add) (command, zclient, length);
949 break;
950 case ZEBRA_IPV6_ROUTE_DELETE:
951 if (zclient->ipv6_route_delete)
952 ret = (*zclient->ipv6_route_delete) (command, zclient, length);
953 break;
954 default:
955 break;
956 }
957
958 if (zclient->sock < 0)
959 /* Connection was closed during packet processing. */
960 return -1;
961
962 /* Register read thread. */
963 stream_reset(zclient->ibuf);
964 zclient_event (ZCLIENT_READ, zclient);
965
966 return 0;
967 }
968
969 void
970 zclient_redistribute (int command, struct zclient *zclient, int type)
971 {
972
973 if (command == ZEBRA_REDISTRIBUTE_ADD)
974 {
975 if (zclient->redist[type])
976 return;
977 zclient->redist[type] = 1;
978 }
979 else
980 {
981 if (!zclient->redist[type])
982 return;
983 zclient->redist[type] = 0;
984 }
985
986 if (zclient->sock > 0)
987 zebra_redistribute_send (command, zclient, type);
988 }
989
990
991 void
992 zclient_redistribute_default (int command, struct zclient *zclient)
993 {
994
995 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
996 {
997 if (zclient->default_information)
998 return;
999 zclient->default_information = 1;
1000 }
1001 else
1002 {
1003 if (!zclient->default_information)
1004 return;
1005 zclient->default_information = 0;
1006 }
1007
1008 if (zclient->sock > 0)
1009 zebra_message_send (zclient, command);
1010 }
1011
1012 static void
1013 zclient_event (enum event event, struct zclient *zclient)
1014 {
1015 switch (event)
1016 {
1017 case ZCLIENT_SCHEDULE:
1018 if (! zclient->t_connect)
1019 zclient->t_connect =
1020 thread_add_event (master, zclient_connect, zclient, 0);
1021 break;
1022 case ZCLIENT_CONNECT:
1023 if (zclient->fail >= 10)
1024 return;
1025 if (zclient_debug)
1026 zlog_debug ("zclient connect schedule interval is %d",
1027 zclient->fail < 3 ? 10 : 60);
1028 if (! zclient->t_connect)
1029 zclient->t_connect =
1030 thread_add_timer (master, zclient_connect, zclient,
1031 zclient->fail < 3 ? 10 : 60);
1032 break;
1033 case ZCLIENT_READ:
1034 zclient->t_read =
1035 thread_add_read (master, zclient_read, zclient, zclient->sock);
1036 break;
1037 }
1038 }
1039
1040 void
1041 zclient_serv_path_set (char *path)
1042 {
1043 struct stat sb;
1044
1045 /* reset */
1046 zclient_serv_path = NULL;
1047
1048 /* test if `path' is socket. don't set it otherwise. */
1049 if (stat(path, &sb) == -1)
1050 {
1051 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
1052 return;
1053 }
1054
1055 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
1056 {
1057 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
1058 return;
1059 }
1060
1061 /* it seems that path is unix socket */
1062 zclient_serv_path = path;
1063 }
1064