blob: cd99b843236b08c346452ea6d93c38adc76764a0 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
ajs634f9ea2005-04-11 15:51:40 +00003 * Copyright (C) 2005 Andrew J. Schorr
paul718e3742002-12-13 20:15:29 +00004 *
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"
ajs634f9ea2005-04-11 15:51:40 +000027#include "buffer.h"
paul718e3742002-12-13 20:15:29 +000028#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#include "zebra/rib.h"
37#include "zebra/zserv.h"
38
39/* Zebra client events. */
40enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
41
42/* Prototype for event manager. */
43static void zclient_event (enum event, struct zclient *);
44
ajs634f9ea2005-04-11 15:51:40 +000045extern struct thread_master *master;
46
paul718e3742002-12-13 20:15:29 +000047/* This file local debug flag. */
48int zclient_debug = 0;
49
50/* Allocate zclient structure. */
51struct zclient *
52zclient_new ()
53{
54 struct zclient *zclient;
55 zclient = XMALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
56 memset (zclient, 0, sizeof (struct zclient));
57
58 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
59 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs634f9ea2005-04-11 15:51:40 +000060 zclient->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +000061
62 return zclient;
63}
64
ajs634f9ea2005-04-11 15:51:40 +000065#if 0
66/* This function is never used. And it must not be used, because
67 many parts of the code do not check for I/O errors, so they could
68 reference an invalid pointer if the structure was ever freed.
69*/
70
paul718e3742002-12-13 20:15:29 +000071/* Free zclient structure. */
72void
73zclient_free (struct zclient *zclient)
74{
ajs634f9ea2005-04-11 15:51:40 +000075 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
paul718e3742002-12-13 20:15:29 +000082 XFREE (MTYPE_ZCLIENT, zclient);
83}
ajs634f9ea2005-04-11 15:51:40 +000084#endif
paul718e3742002-12-13 20:15:29 +000085
86/* Initialize zebra client. Argument redist_default is unwanted
87 redistribute route type. */
88void
89zclient_init (struct zclient *zclient, int redist_default)
90{
91 int i;
92
93 /* Enable zebra client connection by default. */
94 zclient->enable = 1;
95
96 /* Set -1 to the default socket value. */
97 zclient->sock = -1;
98
99 /* Clear redistribution flags. */
100 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
101 zclient->redist[i] = 0;
102
103 /* Set unwanted redistribute route. bgpd does not need BGP route
104 redistribution. */
105 zclient->redist_default = redist_default;
106 zclient->redist[redist_default] = 1;
107
108 /* Set default-information redistribute to zero. */
109 zclient->default_information = 0;
110
111 /* Schedule first zclient connection. */
112 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000113 zlog_debug ("zclient start scheduled");
paul718e3742002-12-13 20:15:29 +0000114
115 zclient_event (ZCLIENT_SCHEDULE, zclient);
116}
117
118/* Stop zebra client services. */
119void
120zclient_stop (struct zclient *zclient)
121{
122 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000123 zlog_debug ("zclient stopped");
paul718e3742002-12-13 20:15:29 +0000124
125 /* Stop threads. */
ajs634f9ea2005-04-11 15:51:40 +0000126 THREAD_OFF(zclient->t_read);
127 THREAD_OFF(zclient->t_connect);
128 THREAD_OFF(zclient->t_write);
129
130 /* Reset streams. */
131 stream_reset(zclient->ibuf);
132 stream_reset(zclient->obuf);
133
134 /* Empty the write buffer. */
135 buffer_reset(zclient->wb);
paul718e3742002-12-13 20:15:29 +0000136
137 /* Close socket. */
138 if (zclient->sock >= 0)
139 {
140 close (zclient->sock);
141 zclient->sock = -1;
142 }
143 zclient->fail = 0;
144}
145
146void
147zclient_reset (struct zclient *zclient)
148{
149 zclient_stop (zclient);
150 zclient_init (zclient, zclient->redist_default);
151}
152
153/* Make socket to zebra daemon. Return zebra socket. */
154int
ajs634f9ea2005-04-11 15:51:40 +0000155zclient_socket(void)
paul718e3742002-12-13 20:15:29 +0000156{
157 int sock;
158 int ret;
159 struct sockaddr_in serv;
160
161 /* We should think about IPv6 connection. */
162 sock = socket (AF_INET, SOCK_STREAM, 0);
163 if (sock < 0)
164 return -1;
165
166 /* Make server socket. */
167 memset (&serv, 0, sizeof (struct sockaddr_in));
168 serv.sin_family = AF_INET;
169 serv.sin_port = htons (ZEBRA_PORT);
170#ifdef HAVE_SIN_LEN
171 serv.sin_len = sizeof (struct sockaddr_in);
172#endif /* HAVE_SIN_LEN */
173 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
174
175 /* Connect to zebra. */
176 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
177 if (ret < 0)
178 {
179 close (sock);
180 return -1;
181 }
182 return sock;
183}
184
185/* For sockaddr_un. */
186#include <sys/un.h>
187
188int
hasso8c328f12004-10-05 21:01:23 +0000189zclient_socket_un (const char *path)
paul718e3742002-12-13 20:15:29 +0000190{
191 int ret;
192 int sock, len;
193 struct sockaddr_un addr;
194
195 sock = socket (AF_UNIX, SOCK_STREAM, 0);
196 if (sock < 0)
197 return -1;
198
199 /* Make server socket. */
200 memset (&addr, 0, sizeof (struct sockaddr_un));
201 addr.sun_family = AF_UNIX;
202 strncpy (addr.sun_path, path, strlen (path));
203#ifdef HAVE_SUN_LEN
204 len = addr.sun_len = SUN_LEN(&addr);
205#else
206 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
207#endif /* HAVE_SUN_LEN */
208
209 ret = connect (sock, (struct sockaddr *) &addr, len);
210 if (ret < 0)
211 {
212 close (sock);
213 return -1;
214 }
215 return sock;
216}
217
ajs634f9ea2005-04-11 15:51:40 +0000218static int
219zclient_failed(struct zclient *zclient)
220{
221 zclient->fail++;
222 zclient_stop(zclient);
223 zclient_event(ZCLIENT_CONNECT, zclient);
224 return -1;
225}
226
227static int
228zclient_flush_data(struct thread *thread)
229{
230 struct zclient *zclient = THREAD_ARG(thread);
231
232 zclient->t_write = NULL;
233 if (zclient->sock < 0)
234 return -1;
235 switch (buffer_flush_available(zclient->wb, zclient->sock))
236 {
237 case BUFFER_ERROR:
238 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
239 __func__, zclient->sock);
240 return zclient_failed(zclient);
241 break;
242 case BUFFER_PENDING:
243 zclient->t_write = thread_add_write(master, zclient_flush_data,
244 zclient, zclient->sock);
245 break;
246 case BUFFER_EMPTY:
247 break;
248 }
249 return 0;
250}
251
paul718e3742002-12-13 20:15:29 +0000252int
ajs634f9ea2005-04-11 15:51:40 +0000253zclient_send_message(struct zclient *zclient)
254{
255 if (zclient->sock < 0)
256 return -1;
257 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
258 stream_get_endp(zclient->obuf)))
259 {
260 case BUFFER_ERROR:
261 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
262 __func__, zclient->sock);
263 return zclient_failed(zclient);
264 break;
265 case BUFFER_EMPTY:
266 THREAD_OFF(zclient->t_write);
267 break;
268 case BUFFER_PENDING:
269 THREAD_WRITE_ON(master, zclient->t_write,
270 zclient_flush_data, zclient, zclient->sock);
271 break;
272 }
273 return 0;
274}
275
276/* Send simple Zebra message. */
277static int
paul718e3742002-12-13 20:15:29 +0000278zebra_message_send (struct zclient *zclient, int command)
279{
280 struct stream *s;
281
282 /* Get zclient output buffer. */
283 s = zclient->obuf;
284 stream_reset (s);
285
286 /* Send very simple command only Zebra message. */
287 stream_putw (s, 3);
288 stream_putc (s, command);
289
ajs634f9ea2005-04-11 15:51:40 +0000290 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000291}
292
293/* Make connection to zebra daemon. */
294int
295zclient_start (struct zclient *zclient)
296{
297 int i;
298
299 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000300 zlog_debug ("zclient_start is called");
paul718e3742002-12-13 20:15:29 +0000301
302 /* zclient is disabled. */
303 if (! zclient->enable)
304 return 0;
305
306 /* If already connected to the zebra. */
307 if (zclient->sock >= 0)
308 return 0;
309
310 /* Check connect thread. */
311 if (zclient->t_connect)
312 return 0;
313
314 /* Make socket. */
315#ifdef HAVE_TCP_ZEBRA
316 zclient->sock = zclient_socket ();
317#else
318 zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
319#endif /* HAVE_TCP_ZEBRA */
320 if (zclient->sock < 0)
321 {
322 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000323 zlog_debug ("zclient connection fail");
paul718e3742002-12-13 20:15:29 +0000324 zclient->fail++;
325 zclient_event (ZCLIENT_CONNECT, zclient);
326 return -1;
327 }
328
ajs634f9ea2005-04-11 15:51:40 +0000329 if (set_nonblocking(zclient->sock) < 0)
330 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
331
paul718e3742002-12-13 20:15:29 +0000332 /* Clear fail count. */
333 zclient->fail = 0;
334 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000335 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
paul718e3742002-12-13 20:15:29 +0000336
337 /* Create read thread. */
338 zclient_event (ZCLIENT_READ, zclient);
339
340 /* We need interface information. */
341 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD);
342
hasso18a6dce2004-10-03 18:18:34 +0000343 /* We need router-id information. */
344 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD);
345
paul718e3742002-12-13 20:15:29 +0000346 /* Flush all redistribute request. */
347 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
348 if (i != zclient->redist_default && zclient->redist[i])
ajs634f9ea2005-04-11 15:51:40 +0000349 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i);
paul718e3742002-12-13 20:15:29 +0000350
351 /* If default information is needed. */
352 if (zclient->default_information)
353 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
354
355 return 0;
356}
357
358/* This function is a wrapper function for calling zclient_start from
359 timer or event thread. */
ajs634f9ea2005-04-11 15:51:40 +0000360static int
paul718e3742002-12-13 20:15:29 +0000361zclient_connect (struct thread *t)
362{
363 struct zclient *zclient;
364
365 zclient = THREAD_ARG (t);
366 zclient->t_connect = NULL;
367
368 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000369 zlog_debug ("zclient_connect is called");
paul718e3742002-12-13 20:15:29 +0000370
371 return zclient_start (zclient);
372}
373
paul0a589352004-05-08 11:48:26 +0000374 /*
375 * "xdr_encode"-like interface that allows daemon (client) to send
376 * a message to zebra server for a route that needs to be
377 * added/deleted to the kernel. Info about the route is specified
378 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
379 * the info down the zclient socket using the stream_* functions.
380 *
381 * The corresponding read ("xdr_decode") function on the server
382 * side is zread_ipv4_add()/zread_ipv4_delete().
383 *
384 * 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
385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 * | Length (2) | Command | Route Type |
387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
388 * | ZEBRA Flags | Message Flags | Prefix length |
389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
390 * | Destination IPv4 Prefix for route |
391 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392 * | Nexthop count |
393 * +-+-+-+-+-+-+-+-+
394 *
395 *
396 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
397 * described, as per the Nexthop count. Each nexthop described as:
398 *
399 * +-+-+-+-+-+-+-+-+
400 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * | IPv4 Nexthop address or Interface Index number |
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 *
405 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
406 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
407 * nexthop information is provided, and the message describes a prefix
408 * to blackhole or reject route.
409 *
410 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
411 * byte value.
412 *
413 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
414 * byte value.
415 *
416 * XXX: No attention paid to alignment.
417 */
paul718e3742002-12-13 20:15:29 +0000418int
paul0a589352004-05-08 11:48:26 +0000419zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
420 struct zapi_ipv4 *api)
paul718e3742002-12-13 20:15:29 +0000421{
422 int i;
423 int psize;
424 struct stream *s;
425
426 /* Reset stream. */
427 s = zclient->obuf;
428 stream_reset (s);
429
430 /* Length place holder. */
431 stream_putw (s, 0);
432
433 /* Put command, type and nexthop. */
paul0a589352004-05-08 11:48:26 +0000434 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000435 stream_putc (s, api->type);
436 stream_putc (s, api->flags);
437 stream_putc (s, api->message);
paul0a589352004-05-08 11:48:26 +0000438
paul718e3742002-12-13 20:15:29 +0000439 /* Put prefix information. */
440 psize = PSIZE (p->prefixlen);
441 stream_putc (s, p->prefixlen);
paul0a589352004-05-08 11:48:26 +0000442 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000443
444 /* Nexthop, ifindex, distance and metric information. */
445 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
446 {
paul595db7f2003-05-25 21:35:06 +0000447 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
448 {
449 stream_putc (s, 1);
450 stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
paul0a589352004-05-08 11:48:26 +0000451 /* XXX assert(api->nexthop_num == 0); */
452 /* XXX assert(api->ifindex_num == 0); */
paul595db7f2003-05-25 21:35:06 +0000453 }
454 else
455 stream_putc (s, api->nexthop_num + api->ifindex_num);
paul718e3742002-12-13 20:15:29 +0000456
457 for (i = 0; i < api->nexthop_num; i++)
paul595db7f2003-05-25 21:35:06 +0000458 {
459 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
460 stream_put_in_addr (s, api->nexthop[i]);
461 }
paul718e3742002-12-13 20:15:29 +0000462 for (i = 0; i < api->ifindex_num; i++)
paul595db7f2003-05-25 21:35:06 +0000463 {
464 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
465 stream_putl (s, api->ifindex[i]);
466 }
paul718e3742002-12-13 20:15:29 +0000467 }
468
469 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
470 stream_putc (s, api->distance);
471 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
472 stream_putl (s, api->metric);
473
474 /* Put length at the first point of the stream. */
475 stream_putw_at (s, 0, stream_get_endp (s));
476
ajs634f9ea2005-04-11 15:51:40 +0000477 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000478}
479
480#ifdef HAVE_IPV6
481int
paul0a589352004-05-08 11:48:26 +0000482zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
paul718e3742002-12-13 20:15:29 +0000483 struct zapi_ipv6 *api)
484{
485 int i;
486 int psize;
487 struct stream *s;
488
489 /* Reset stream. */
490 s = zclient->obuf;
491 stream_reset (s);
492
493 /* Length place holder. */
494 stream_putw (s, 0);
495
496 /* Put command, type and nexthop. */
paul0a589352004-05-08 11:48:26 +0000497 stream_putc (s, cmd);
paul718e3742002-12-13 20:15:29 +0000498 stream_putc (s, api->type);
499 stream_putc (s, api->flags);
500 stream_putc (s, api->message);
501
502 /* Put prefix information. */
503 psize = PSIZE (p->prefixlen);
504 stream_putc (s, p->prefixlen);
505 stream_write (s, (u_char *)&p->prefix, psize);
506
507 /* Nexthop, ifindex, distance and metric information. */
508 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
509 {
510 stream_putc (s, api->nexthop_num + api->ifindex_num);
511
512 for (i = 0; i < api->nexthop_num; i++)
513 {
514 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
515 stream_write (s, (u_char *)api->nexthop[i], 16);
516 }
517 for (i = 0; i < api->ifindex_num; i++)
518 {
519 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
520 stream_putl (s, api->ifindex[i]);
521 }
522 }
523
524 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
525 stream_putc (s, api->distance);
526 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
527 stream_putl (s, api->metric);
528
529 /* Put length at the first point of the stream. */
530 stream_putw_at (s, 0, stream_get_endp (s));
531
ajs634f9ea2005-04-11 15:51:40 +0000532 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000533}
paul718e3742002-12-13 20:15:29 +0000534#endif /* HAVE_IPV6 */
535
paul0a589352004-05-08 11:48:26 +0000536/*
537 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
538 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
539 * then set/unset redist[type] in the client handle (a struct zserv) for the
540 * sending client
541 */
paul718e3742002-12-13 20:15:29 +0000542int
ajs634f9ea2005-04-11 15:51:40 +0000543zebra_redistribute_send (int command, struct zclient *zclient, int type)
paul718e3742002-12-13 20:15:29 +0000544{
paul718e3742002-12-13 20:15:29 +0000545 struct stream *s;
546
ajs634f9ea2005-04-11 15:51:40 +0000547 s = zclient->obuf;
548 stream_reset(s);
paul718e3742002-12-13 20:15:29 +0000549
ajs634f9ea2005-04-11 15:51:40 +0000550 /* Total length of the message. */
paul718e3742002-12-13 20:15:29 +0000551 stream_putw (s, 4);
552
553 stream_putc (s, command);
554 stream_putc (s, type);
555
ajs634f9ea2005-04-11 15:51:40 +0000556 return zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000557}
558
hasso18a6dce2004-10-03 18:18:34 +0000559/* Router-id update from zebra daemon. */
560void
561zebra_router_id_update_read (struct stream *s, struct prefix *rid)
562{
563 int plen;
564
565 /* Fetch interface address. */
566 rid->family = stream_getc (s);
567
568 plen = prefix_blen (rid);
569 stream_get (&rid->u.prefix, s, plen);
570 rid->prefixlen = stream_getc (s);
571}
572
paul718e3742002-12-13 20:15:29 +0000573/* Interface addition from zebra daemon. */
paul0a589352004-05-08 11:48:26 +0000574/*
575 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
576 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
577 * 0 1 2 3
578 * 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
579 * +-+-+-+-+-+-+-+-+
580 * | type |
581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
582 * | ifname |
583 * | |
584 * | |
585 * | |
586 * | |
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | ifindex |
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * | if_flags |
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 * | metric |
593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594 * | ifmtu |
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | ifmtu6 |
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | bandwidth |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * | sockaddr_dl |
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 */
603
paul718e3742002-12-13 20:15:29 +0000604struct interface *
605zebra_interface_add_read (struct stream *s)
606{
607 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000608 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000609
610 /* Read interface name. */
611 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
612
ajsa3491982005-04-02 22:50:38 +0000613 /* Lookup/create interface by name. */
614 ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000615
616 /* Read interface's index. */
617 ifp->ifindex = stream_getl (s);
618
619 /* Read interface's value. */
paul2e3b2e42002-12-13 21:03:13 +0000620 ifp->status = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000621 ifp->flags = stream_getl (s);
622 ifp->metric = stream_getl (s);
623 ifp->mtu = stream_getl (s);
paul0a589352004-05-08 11:48:26 +0000624 ifp->mtu6 = stream_getl (s);
paul718e3742002-12-13 20:15:29 +0000625 ifp->bandwidth = stream_getl (s);
626#ifdef HAVE_SOCKADDR_DL
627 stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
628#else
629 ifp->hw_addr_len = stream_getl (s);
630 if (ifp->hw_addr_len)
631 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
632#endif /* HAVE_SOCKADDR_DL */
633
634 return ifp;
635}
636
paul0a589352004-05-08 11:48:26 +0000637/*
638 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
639 * from zebra server. The format of this message is the same as
640 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
641 * comments for zebra_interface_add_read), except that no sockaddr_dl
642 * is sent at the tail of the message.
643 */
paul718e3742002-12-13 20:15:29 +0000644struct interface *
645zebra_interface_state_read (struct stream *s)
646{
647 struct interface *ifp;
paul02ff83c2004-06-11 11:27:03 +0000648 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000649
650 /* Read interface name. */
651 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
652
653 /* Lookup this by interface index. */
ajsa3491982005-04-02 22:50:38 +0000654 ifp = if_lookup_by_name_len (ifname_tmp,
655 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000656
657 /* If such interface does not exist, indicate an error */
658 if (! ifp)
659 return NULL;
660
661 /* Read interface's index. */
662 ifp->ifindex = stream_getl (s);
663
664 /* Read interface's value. */
paul2e3b2e42002-12-13 21:03:13 +0000665 ifp->status = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000666 ifp->flags = stream_getl (s);
667 ifp->metric = stream_getl (s);
668 ifp->mtu = stream_getl (s);
paul0a589352004-05-08 11:48:26 +0000669 ifp->mtu6 = stream_getl (s);
paul718e3742002-12-13 20:15:29 +0000670 ifp->bandwidth = stream_getl (s);
671
672 return ifp;
673}
674
paul0a589352004-05-08 11:48:26 +0000675/*
676 * format of message for address additon is:
677 * 0
678 * 0 1 2 3 4 5 6 7
679 * +-+-+-+-+-+-+-+-+
680 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
681 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
682 * | |
683 * + +
684 * | ifindex |
685 * + +
686 * | |
687 * + +
688 * | |
689 * +-+-+-+-+-+-+-+-+
690 * | ifc_flags | flags for connected address
691 * +-+-+-+-+-+-+-+-+
692 * | addr_family |
693 * +-+-+-+-+-+-+-+-+
694 * | addr... |
695 * : :
696 * | |
697 * +-+-+-+-+-+-+-+-+
698 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
699 * +-+-+-+-+-+-+-+-+
700 * | daddr.. |
701 * : :
702 * | |
703 * +-+-+-+-+-+-+-+-+
704 *
705 */
706
hasso18a6dce2004-10-03 18:18:34 +0000707void
708zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
709{
710 /* Read interface's index. */
711 ifp->ifindex = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000712 ifp->status = stream_getc (s);
hasso18a6dce2004-10-03 18:18:34 +0000713
714 /* Read interface's value. */
715 ifp->flags = stream_getl (s);
716 ifp->metric = stream_getl (s);
717 ifp->mtu = stream_getl (s);
hasso508ec912004-10-23 14:26:49 +0000718 ifp->mtu6 = stream_getl (s);
hasso18a6dce2004-10-03 18:18:34 +0000719 ifp->bandwidth = stream_getl (s);
720}
721
hasso3fb9cd62004-10-19 19:44:43 +0000722static int
723memconstant(const void *s, int c, size_t n)
724{
725 const u_char *p = s;
726
727 while (n-- > 0)
728 if (*p++ != c)
729 return 0;
730 return 1;
731}
732
paul718e3742002-12-13 20:15:29 +0000733struct connected *
paul0a589352004-05-08 11:48:26 +0000734zebra_interface_address_read (int type, struct stream *s)
paul718e3742002-12-13 20:15:29 +0000735{
736 unsigned int ifindex;
737 struct interface *ifp;
738 struct connected *ifc;
paul0a589352004-05-08 11:48:26 +0000739 struct prefix p, d;
paul718e3742002-12-13 20:15:29 +0000740 int family;
741 int plen;
paul0a589352004-05-08 11:48:26 +0000742 u_char ifc_flags;
743
744 memset (&p, 0, sizeof(p));
745 memset (&d, 0, sizeof(d));
paul718e3742002-12-13 20:15:29 +0000746
747 /* Get interface index. */
748 ifindex = stream_getl (s);
749
750 /* Lookup index. */
751 ifp = if_lookup_by_index (ifindex);
752 if (ifp == NULL)
753 {
paul0a589352004-05-08 11:48:26 +0000754 zlog_warn ("zebra_interface_address_read(%s): "
755 "Can't find interface by ifindex: %d ",
756 (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
757 ifindex);
paul718e3742002-12-13 20:15:29 +0000758 return NULL;
759 }
760
761 /* Fetch flag. */
paul0a589352004-05-08 11:48:26 +0000762 ifc_flags = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000763
764 /* Fetch interface address. */
765 family = p.family = stream_getc (s);
766
paul0a589352004-05-08 11:48:26 +0000767 plen = prefix_blen (&p);
768 stream_get (&p.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000769 p.prefixlen = stream_getc (s);
770
771 /* Fetch destination address. */
paul0a589352004-05-08 11:48:26 +0000772 stream_get (&d.u.prefix, s, plen);
paul718e3742002-12-13 20:15:29 +0000773 d.family = family;
774
paul0a589352004-05-08 11:48:26 +0000775 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
776 {
hasso3fb9cd62004-10-19 19:44:43 +0000777 /* N.B. NULL destination pointers are encoded as all zeroes */
778 ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
779 NULL : &d));
paul0a589352004-05-08 11:48:26 +0000780 if (ifc != NULL)
781 ifc->flags = ifc_flags;
782 }
783 else
784 {
785 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
786 ifc = connected_delete_by_prefix(ifp, &p);
787 }
paul718e3742002-12-13 20:15:29 +0000788
789 return ifc;
790}
paul0a589352004-05-08 11:48:26 +0000791
paul718e3742002-12-13 20:15:29 +0000792
793/* Zebra client message read function. */
ajs634f9ea2005-04-11 15:51:40 +0000794static int
paul718e3742002-12-13 20:15:29 +0000795zclient_read (struct thread *thread)
796{
797 int ret;
ajs634f9ea2005-04-11 15:51:40 +0000798 size_t already;
paul718e3742002-12-13 20:15:29 +0000799 zebra_size_t length;
800 zebra_command_t command;
801 struct zclient *zclient;
802
803 /* Get socket to zebra. */
paul718e3742002-12-13 20:15:29 +0000804 zclient = THREAD_ARG (thread);
805 zclient->t_read = NULL;
806
ajs634f9ea2005-04-11 15:51:40 +0000807 /* Read zebra header (if we don't have it already). */
808 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000809 {
ajs634f9ea2005-04-11 15:51:40 +0000810 ssize_t nbyte;
811 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
812 ZEBRA_HEADER_SIZE-already)) == 0) ||
813 (nbyte == -1))
814 {
815 if (zclient_debug)
816 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
817 return zclient_failed(zclient);
818 }
819 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
820 {
821 /* Try again later. */
822 zclient_event (ZCLIENT_READ, zclient);
823 return 0;
824 }
825 already = ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000826 }
827
ajs634f9ea2005-04-11 15:51:40 +0000828 /* Reset to read from the beginning of the incoming packet. */
829 stream_set_getp(zclient->ibuf, 0);
paul718e3742002-12-13 20:15:29 +0000830
831 /* Fetch length and command. */
832 length = stream_getw (zclient->ibuf);
833 command = stream_getc (zclient->ibuf);
834
ajs634f9ea2005-04-11 15:51:40 +0000835 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +0000836 {
ajs634f9ea2005-04-11 15:51:40 +0000837 zlog_err("%s: socket %d message length %u is less than %d ",
838 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
839 return zclient_failed(zclient);
paul718e3742002-12-13 20:15:29 +0000840 }
ajs634f9ea2005-04-11 15:51:40 +0000841
842 /* Length check. */
843 if (length > STREAM_SIZE(zclient->ibuf))
844 {
845 struct stream *ns;
846 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
847 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
848 ns = stream_new(length);
849 stream_copy(ns, zclient->ibuf);
850 stream_free (zclient->ibuf);
851 zclient->ibuf = ns;
852 }
paul718e3742002-12-13 20:15:29 +0000853
854 /* Read rest of zebra packet. */
ajs634f9ea2005-04-11 15:51:40 +0000855 if (already < length)
856 {
857 ssize_t nbyte;
858 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
859 length-already)) == 0) ||
860 (nbyte == -1))
861 {
862 if (zclient_debug)
863 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
864 return zclient_failed(zclient);
865 }
866 if (nbyte != (ssize_t)(length-already))
867 {
868 /* Try again later. */
869 zclient_event (ZCLIENT_READ, zclient);
870 return 0;
871 }
872 }
873
874 length -= ZEBRA_HEADER_SIZE;
paul718e3742002-12-13 20:15:29 +0000875
paul0a589352004-05-08 11:48:26 +0000876 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000877 zlog_debug("zclient 0x%p command 0x%x \n", zclient, command);
paul0a589352004-05-08 11:48:26 +0000878
paul718e3742002-12-13 20:15:29 +0000879 switch (command)
880 {
hasso18a6dce2004-10-03 18:18:34 +0000881 case ZEBRA_ROUTER_ID_UPDATE:
882 if (zclient->router_id_update)
883 ret = (*zclient->router_id_update) (command, zclient, length);
884 break;
paul718e3742002-12-13 20:15:29 +0000885 case ZEBRA_INTERFACE_ADD:
886 if (zclient->interface_add)
887 ret = (*zclient->interface_add) (command, zclient, length);
888 break;
889 case ZEBRA_INTERFACE_DELETE:
890 if (zclient->interface_delete)
891 ret = (*zclient->interface_delete) (command, zclient, length);
892 break;
893 case ZEBRA_INTERFACE_ADDRESS_ADD:
894 if (zclient->interface_address_add)
895 ret = (*zclient->interface_address_add) (command, zclient, length);
896 break;
897 case ZEBRA_INTERFACE_ADDRESS_DELETE:
898 if (zclient->interface_address_delete)
899 ret = (*zclient->interface_address_delete) (command, zclient, length);
900 break;
901 case ZEBRA_INTERFACE_UP:
902 if (zclient->interface_up)
903 ret = (*zclient->interface_up) (command, zclient, length);
904 break;
905 case ZEBRA_INTERFACE_DOWN:
906 if (zclient->interface_down)
907 ret = (*zclient->interface_down) (command, zclient, length);
908 break;
909 case ZEBRA_IPV4_ROUTE_ADD:
910 if (zclient->ipv4_route_add)
911 ret = (*zclient->ipv4_route_add) (command, zclient, length);
912 break;
913 case ZEBRA_IPV4_ROUTE_DELETE:
914 if (zclient->ipv4_route_delete)
915 ret = (*zclient->ipv4_route_delete) (command, zclient, length);
916 break;
917 case ZEBRA_IPV6_ROUTE_ADD:
918 if (zclient->ipv6_route_add)
919 ret = (*zclient->ipv6_route_add) (command, zclient, length);
920 break;
921 case ZEBRA_IPV6_ROUTE_DELETE:
922 if (zclient->ipv6_route_delete)
923 ret = (*zclient->ipv6_route_delete) (command, zclient, length);
924 break;
925 default:
926 break;
927 }
928
ajs634f9ea2005-04-11 15:51:40 +0000929 if (zclient->sock < 0)
930 /* Connection was closed during packet processing. */
931 return -1;
932
paul718e3742002-12-13 20:15:29 +0000933 /* Register read thread. */
ajs634f9ea2005-04-11 15:51:40 +0000934 stream_reset(zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000935 zclient_event (ZCLIENT_READ, zclient);
936
937 return 0;
938}
939
940void
paul0a589352004-05-08 11:48:26 +0000941zclient_redistribute (int command, struct zclient *zclient, int type)
paul718e3742002-12-13 20:15:29 +0000942{
paul718e3742002-12-13 20:15:29 +0000943
paul0a589352004-05-08 11:48:26 +0000944 if (command == ZEBRA_REDISTRIBUTE_ADD)
945 {
946 if (zclient->redist[type])
947 return;
948 zclient->redist[type] = 1;
949 }
950 else
951 {
952 if (!zclient->redist[type])
953 return;
954 zclient->redist[type] = 0;
955 }
paul718e3742002-12-13 20:15:29 +0000956
957 if (zclient->sock > 0)
ajs634f9ea2005-04-11 15:51:40 +0000958 zebra_redistribute_send (command, zclient, type);
paul718e3742002-12-13 20:15:29 +0000959}
960
paul0a589352004-05-08 11:48:26 +0000961
paul718e3742002-12-13 20:15:29 +0000962void
paul0a589352004-05-08 11:48:26 +0000963zclient_redistribute_default (int command, struct zclient *zclient)
paul718e3742002-12-13 20:15:29 +0000964{
paul718e3742002-12-13 20:15:29 +0000965
paul0a589352004-05-08 11:48:26 +0000966 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
967 {
968 if (zclient->default_information)
969 return;
970 zclient->default_information = 1;
971 }
972 else
973 {
974 if (!zclient->default_information)
975 return;
976 zclient->default_information = 0;
977 }
paul718e3742002-12-13 20:15:29 +0000978
979 if (zclient->sock > 0)
paul0a589352004-05-08 11:48:26 +0000980 zebra_message_send (zclient, command);
paul718e3742002-12-13 20:15:29 +0000981}
982
paul718e3742002-12-13 20:15:29 +0000983static void
984zclient_event (enum event event, struct zclient *zclient)
985{
986 switch (event)
987 {
988 case ZCLIENT_SCHEDULE:
989 if (! zclient->t_connect)
990 zclient->t_connect =
991 thread_add_event (master, zclient_connect, zclient, 0);
992 break;
993 case ZCLIENT_CONNECT:
994 if (zclient->fail >= 10)
995 return;
996 if (zclient_debug)
ajs8ddca702004-12-07 18:53:52 +0000997 zlog_debug ("zclient connect schedule interval is %d",
paul718e3742002-12-13 20:15:29 +0000998 zclient->fail < 3 ? 10 : 60);
999 if (! zclient->t_connect)
1000 zclient->t_connect =
1001 thread_add_timer (master, zclient_connect, zclient,
1002 zclient->fail < 3 ? 10 : 60);
1003 break;
1004 case ZCLIENT_READ:
1005 zclient->t_read =
1006 thread_add_read (master, zclient_read, zclient, zclient->sock);
1007 break;
1008 }
1009}