]>
Commit | Line | Data |
---|---|---|
1 | /* Zebra's client library. | |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
3 | * Copyright (C) 2005 Andrew J. Schorr | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with GNU Zebra; see the file COPYING. If not, write to the | |
19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
20 | * MA 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | #include "prefix.h" | |
26 | #include "stream.h" | |
27 | #include "buffer.h" | |
28 | #include "network.h" | |
29 | #include "if.h" | |
30 | #include "log.h" | |
31 | #include "thread.h" | |
32 | #include "zclient.h" | |
33 | #include "memory.h" | |
34 | #include "table.h" | |
35 | ||
36 | /* Zebra client events. */ | |
37 | enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT}; | |
38 | ||
39 | /* Prototype for event manager. */ | |
40 | static void zclient_event (enum event, struct zclient *); | |
41 | ||
42 | char *zclient_serv_path = NULL; | |
43 | ||
44 | /* This file local debug flag. */ | |
45 | int zclient_debug = 0; | |
46 | ||
47 | /* Allocate zclient structure. */ | |
48 | struct zclient * | |
49 | zclient_new (struct thread_master *master) | |
50 | { | |
51 | struct zclient *zclient; | |
52 | zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient)); | |
53 | ||
54 | zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
55 | zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
56 | zclient->wb = buffer_new(0); | |
57 | zclient->master = master; | |
58 | ||
59 | return zclient; | |
60 | } | |
61 | ||
62 | /* This function is only called when exiting, because | |
63 | many parts of the code do not check for I/O errors, so they could | |
64 | reference an invalid pointer if the structure was ever freed. | |
65 | ||
66 | Free zclient structure. */ | |
67 | void | |
68 | zclient_free (struct zclient *zclient) | |
69 | { | |
70 | if (zclient->ibuf) | |
71 | stream_free(zclient->ibuf); | |
72 | if (zclient->obuf) | |
73 | stream_free(zclient->obuf); | |
74 | if (zclient->wb) | |
75 | buffer_free(zclient->wb); | |
76 | ||
77 | XFREE (MTYPE_ZCLIENT, zclient); | |
78 | } | |
79 | ||
80 | int | |
81 | redist_check_instance (struct redist_proto *red, u_short instance) | |
82 | { | |
83 | struct listnode *node; | |
84 | u_short *id; | |
85 | ||
86 | if (!red->instances) | |
87 | return 0; | |
88 | ||
89 | for (ALL_LIST_ELEMENTS_RO (red->instances, node, id)) | |
90 | if (*id == instance) | |
91 | return 1; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | void | |
97 | redist_add_instance (struct redist_proto *red, u_short instance) | |
98 | { | |
99 | u_short *in; | |
100 | ||
101 | red->enabled = 1; | |
102 | ||
103 | if (!red->instances) | |
104 | red->instances = list_new(); | |
105 | ||
106 | in = (u_short *)calloc(1, sizeof(u_short)); | |
107 | *in = instance; | |
108 | listnode_add (red->instances, in); | |
109 | } | |
110 | ||
111 | void | |
112 | redist_del_instance (struct redist_proto *red, u_short instance) | |
113 | { | |
114 | struct listnode *node; | |
115 | u_short *id = NULL; | |
116 | ||
117 | if (!red->instances) | |
118 | return; | |
119 | ||
120 | for (ALL_LIST_ELEMENTS_RO (red->instances, node, id)) | |
121 | if (*id == instance) | |
122 | break; | |
123 | ||
124 | if (id) | |
125 | { | |
126 | listnode_delete(red->instances, id); | |
127 | if (!red->instances->count) | |
128 | { | |
129 | red->enabled = 0; | |
130 | list_free(red->instances); | |
131 | red->instances = NULL; | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | /* Stop zebra client services. */ | |
137 | void | |
138 | zclient_stop (struct zclient *zclient) | |
139 | { | |
140 | if (zclient_debug) | |
141 | zlog_debug ("zclient stopped"); | |
142 | ||
143 | /* Stop threads. */ | |
144 | THREAD_OFF(zclient->t_read); | |
145 | THREAD_OFF(zclient->t_connect); | |
146 | THREAD_OFF(zclient->t_write); | |
147 | ||
148 | /* Reset streams. */ | |
149 | stream_reset(zclient->ibuf); | |
150 | stream_reset(zclient->obuf); | |
151 | ||
152 | /* Empty the write buffer. */ | |
153 | buffer_reset(zclient->wb); | |
154 | ||
155 | /* Close socket. */ | |
156 | if (zclient->sock >= 0) | |
157 | { | |
158 | close (zclient->sock); | |
159 | zclient->sock = -1; | |
160 | } | |
161 | zclient->fail = 0; | |
162 | } | |
163 | ||
164 | void | |
165 | zclient_reset (struct zclient *zclient) | |
166 | { | |
167 | int afi; | |
168 | ||
169 | zclient_stop (zclient); | |
170 | ||
171 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
172 | redist_del_instance (&zclient->redist[afi][zclient->redist_default], zclient->instance); | |
173 | ||
174 | zclient_init (zclient, zclient->redist_default, zclient->instance); | |
175 | } | |
176 | ||
177 | #ifdef HAVE_TCP_ZEBRA | |
178 | ||
179 | /* Make socket to zebra daemon. Return zebra socket. */ | |
180 | static int | |
181 | zclient_socket(void) | |
182 | { | |
183 | int sock; | |
184 | int ret; | |
185 | struct sockaddr_in serv; | |
186 | ||
187 | /* We should think about IPv6 connection. */ | |
188 | sock = socket (AF_INET, SOCK_STREAM, 0); | |
189 | if (sock < 0) | |
190 | return -1; | |
191 | ||
192 | /* Make server socket. */ | |
193 | memset (&serv, 0, sizeof (struct sockaddr_in)); | |
194 | serv.sin_family = AF_INET; | |
195 | serv.sin_port = htons (ZEBRA_PORT); | |
196 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN | |
197 | serv.sin_len = sizeof (struct sockaddr_in); | |
198 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ | |
199 | serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
200 | ||
201 | /* Connect to zebra. */ | |
202 | ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv)); | |
203 | if (ret < 0) | |
204 | { | |
205 | close (sock); | |
206 | return -1; | |
207 | } | |
208 | return sock; | |
209 | } | |
210 | ||
211 | #else | |
212 | ||
213 | /* For sockaddr_un. */ | |
214 | #include <sys/un.h> | |
215 | ||
216 | static int | |
217 | zclient_socket_un (const char *path) | |
218 | { | |
219 | int ret; | |
220 | int sock, len; | |
221 | struct sockaddr_un addr; | |
222 | ||
223 | sock = socket (AF_UNIX, SOCK_STREAM, 0); | |
224 | if (sock < 0) | |
225 | return -1; | |
226 | ||
227 | /* Make server socket. */ | |
228 | memset (&addr, 0, sizeof (struct sockaddr_un)); | |
229 | addr.sun_family = AF_UNIX; | |
230 | strncpy (addr.sun_path, path, strlen (path)); | |
231 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN | |
232 | len = addr.sun_len = SUN_LEN(&addr); | |
233 | #else | |
234 | len = sizeof (addr.sun_family) + strlen (addr.sun_path); | |
235 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ | |
236 | ||
237 | ret = connect (sock, (struct sockaddr *) &addr, len); | |
238 | if (ret < 0) | |
239 | { | |
240 | close (sock); | |
241 | return -1; | |
242 | } | |
243 | return sock; | |
244 | } | |
245 | ||
246 | #endif /* HAVE_TCP_ZEBRA */ | |
247 | ||
248 | /** | |
249 | * Connect to zebra daemon. | |
250 | * @param zclient a pointer to zclient structure | |
251 | * @return socket fd just to make sure that connection established | |
252 | * @see zclient_init | |
253 | * @see zclient_new | |
254 | */ | |
255 | int | |
256 | zclient_socket_connect (struct zclient *zclient) | |
257 | { | |
258 | #ifdef HAVE_TCP_ZEBRA | |
259 | zclient->sock = zclient_socket (); | |
260 | #else | |
261 | zclient->sock = zclient_socket_un (zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH); | |
262 | #endif | |
263 | return zclient->sock; | |
264 | } | |
265 | ||
266 | static int | |
267 | zclient_failed(struct zclient *zclient) | |
268 | { | |
269 | zclient->fail++; | |
270 | zclient_stop(zclient); | |
271 | zclient_event(ZCLIENT_CONNECT, zclient); | |
272 | return -1; | |
273 | } | |
274 | ||
275 | static int | |
276 | zclient_flush_data(struct thread *thread) | |
277 | { | |
278 | struct zclient *zclient = THREAD_ARG(thread); | |
279 | ||
280 | zclient->t_write = NULL; | |
281 | if (zclient->sock < 0) | |
282 | return -1; | |
283 | switch (buffer_flush_available(zclient->wb, zclient->sock)) | |
284 | { | |
285 | case BUFFER_ERROR: | |
286 | zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing", | |
287 | __func__, zclient->sock); | |
288 | return zclient_failed(zclient); | |
289 | break; | |
290 | case BUFFER_PENDING: | |
291 | zclient->t_write = thread_add_write(zclient->master, zclient_flush_data, | |
292 | zclient, zclient->sock); | |
293 | break; | |
294 | case BUFFER_EMPTY: | |
295 | break; | |
296 | } | |
297 | return 0; | |
298 | } | |
299 | ||
300 | int | |
301 | zclient_send_message(struct zclient *zclient) | |
302 | { | |
303 | if (zclient->sock < 0) | |
304 | return -1; | |
305 | switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf), | |
306 | stream_get_endp(zclient->obuf))) | |
307 | { | |
308 | case BUFFER_ERROR: | |
309 | zlog_warn("%s: buffer_write failed to zclient fd %d, closing", | |
310 | __func__, zclient->sock); | |
311 | return zclient_failed(zclient); | |
312 | break; | |
313 | case BUFFER_EMPTY: | |
314 | THREAD_OFF(zclient->t_write); | |
315 | break; | |
316 | case BUFFER_PENDING: | |
317 | THREAD_WRITE_ON(zclient->master, zclient->t_write, | |
318 | zclient_flush_data, zclient, zclient->sock); | |
319 | break; | |
320 | } | |
321 | return 0; | |
322 | } | |
323 | ||
324 | void | |
325 | zclient_create_header (struct stream *s, uint16_t command) | |
326 | { | |
327 | /* length placeholder, caller can update */ | |
328 | stream_putw (s, ZEBRA_HEADER_SIZE); | |
329 | stream_putc (s, ZEBRA_HEADER_MARKER); | |
330 | stream_putc (s, ZSERV_VERSION); | |
331 | stream_putw (s, command); | |
332 | } | |
333 | ||
334 | /* Send simple Zebra message. */ | |
335 | static int | |
336 | zebra_message_send (struct zclient *zclient, int command) | |
337 | { | |
338 | struct stream *s; | |
339 | ||
340 | /* Get zclient output buffer. */ | |
341 | s = zclient->obuf; | |
342 | stream_reset (s); | |
343 | ||
344 | /* Send very simple command only Zebra message. */ | |
345 | zclient_create_header (s, command); | |
346 | ||
347 | return zclient_send_message(zclient); | |
348 | } | |
349 | ||
350 | static int | |
351 | zebra_hello_send (struct zclient *zclient) | |
352 | { | |
353 | struct stream *s; | |
354 | ||
355 | if (zclient->redist_default) | |
356 | { | |
357 | s = zclient->obuf; | |
358 | stream_reset (s); | |
359 | ||
360 | zclient_create_header (s, ZEBRA_HELLO); | |
361 | stream_putc (s, zclient->redist_default); | |
362 | stream_putw (s, zclient->instance); | |
363 | stream_putw_at (s, 0, stream_get_endp (s)); | |
364 | return zclient_send_message(zclient); | |
365 | } | |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
370 | /* Make connection to zebra daemon. */ | |
371 | int | |
372 | zclient_start (struct zclient *zclient) | |
373 | { | |
374 | int i; | |
375 | afi_t afi; | |
376 | ||
377 | /* If already connected to the zebra. */ | |
378 | if (zclient->sock >= 0) | |
379 | return 0; | |
380 | ||
381 | /* Check connect thread. */ | |
382 | if (zclient->t_connect) | |
383 | return 0; | |
384 | ||
385 | /* Flush all redistribute request. */ | |
386 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
387 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
388 | if (zclient->redist[afi][i].enabled) | |
389 | { | |
390 | struct listnode *node; | |
391 | u_short *id; | |
392 | ||
393 | for (ALL_LIST_ELEMENTS_RO(zclient->redist[afi][i].instances, node, id)) | |
394 | if (!(i == zclient->redist_default && *id == zclient->instance)) | |
395 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, *id); | |
396 | } | |
397 | ||
398 | /* If default information is needed. */ | |
399 | if (zclient->default_information) | |
400 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | /* This function is a wrapper function for calling zclient_start from | |
406 | timer or event thread. */ | |
407 | static int | |
408 | zclient_connect (struct thread *t) | |
409 | { | |
410 | struct zclient *zclient; | |
411 | ||
412 | zclient = THREAD_ARG (t); | |
413 | zclient->t_connect = NULL; | |
414 | ||
415 | if (zclient_debug) | |
416 | zlog_debug ("zclient_connect is called"); | |
417 | ||
418 | return zclient_start (zclient); | |
419 | } | |
420 | ||
421 | /* Initialize zebra client. Argument redist_default is unwanted | |
422 | redistribute route type. */ | |
423 | void | |
424 | zclient_init (struct zclient *zclient, int redist_default, u_short instance) | |
425 | { | |
426 | int afi, i; | |
427 | ||
428 | /* Enable zebra client connection by default. */ | |
429 | zclient->enable = 1; | |
430 | ||
431 | /* Set -1 to the default socket value. */ | |
432 | zclient->sock = -1; | |
433 | ||
434 | /* Clear redistribution flags. */ | |
435 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
436 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
437 | memset(&zclient->redist[afi][i], 0, sizeof(struct redist_proto)); | |
438 | ||
439 | /* Set unwanted redistribute route. bgpd does not need BGP route | |
440 | redistribution. */ | |
441 | zclient->redist_default = redist_default; | |
442 | zclient->instance = instance; | |
443 | /* Pending: make afi(s) an arg. */ | |
444 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
445 | redist_add_instance (&zclient->redist[afi][redist_default], instance); | |
446 | ||
447 | /* Set default-information redistribute to zero. */ | |
448 | zclient->default_information = 0; | |
449 | ||
450 | if (zclient_debug) | |
451 | zlog_debug ("zclient_start is called"); | |
452 | ||
453 | /* zclient is disabled. */ | |
454 | if (! zclient->enable) | |
455 | return; | |
456 | ||
457 | if (zclient_socket_connect(zclient) < 0) | |
458 | { | |
459 | if (zclient_debug) | |
460 | zlog_debug ("zclient connection fail"); | |
461 | zclient->fail++; | |
462 | zclient_event (ZCLIENT_CONNECT, zclient); | |
463 | return; | |
464 | } | |
465 | ||
466 | if (set_nonblocking(zclient->sock) < 0) | |
467 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock); | |
468 | ||
469 | /* Clear fail count. */ | |
470 | zclient->fail = 0; | |
471 | if (zclient_debug) | |
472 | zlog_debug ("zclient connect success with socket [%d]", zclient->sock); | |
473 | ||
474 | /* Create read thread. */ | |
475 | zclient_event (ZCLIENT_READ, zclient); | |
476 | ||
477 | zebra_hello_send (zclient); | |
478 | ||
479 | /* We need router-id information. */ | |
480 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD); | |
481 | ||
482 | /* We need interface information. */ | |
483 | zebra_message_send (zclient, ZEBRA_INTERFACE_ADD); | |
484 | ||
485 | zclient_event (ZCLIENT_SCHEDULE, zclient); | |
486 | } | |
487 | ||
488 | /* | |
489 | * "xdr_encode"-like interface that allows daemon (client) to send | |
490 | * a message to zebra server for a route that needs to be | |
491 | * added/deleted to the kernel. Info about the route is specified | |
492 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes | |
493 | * the info down the zclient socket using the stream_* functions. | |
494 | * | |
495 | * The corresponding read ("xdr_decode") function on the server | |
496 | * side is zread_ipv4_add()/zread_ipv4_delete(). | |
497 | * | |
498 | * 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 | |
499 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
500 | * | Length (2) | Command | Route Type | | |
501 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
502 | * | ZEBRA Flags | Message Flags | Prefix length | | |
503 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
504 | * | Destination IPv4 Prefix for route | | |
505 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
506 | * | Nexthop count | | |
507 | * +-+-+-+-+-+-+-+-+ | |
508 | * | |
509 | * | |
510 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then | |
511 | * described, as per the Nexthop count. Each nexthop described as: | |
512 | * | |
513 | * +-+-+-+-+-+-+-+-+ | |
514 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* | |
515 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
516 | * | IPv4 Nexthop address or Interface Index number | | |
517 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
518 | * | |
519 | * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or | |
520 | * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ | |
521 | * nexthop information is provided, and the message describes a prefix | |
522 | * to blackhole or reject route. | |
523 | * | |
524 | * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*() | |
525 | * infrastructure was built around the traditional (32-bit "gate OR | |
526 | * ifindex") nexthop data unit. A special encoding can be used to feed | |
527 | * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route() | |
528 | * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4 | |
529 | * fields as follows: | |
530 | * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK | |
531 | * - .nexthop_num == .ifindex_num | |
532 | * - .nexthop and .ifindex are filled with gate and ifindex parts of | |
533 | * each compound nexthop, both in the same order | |
534 | * | |
535 | * zapi_ipv4_route() will produce two nexthop data units for each such | |
536 | * interleaved 64-bit nexthop. On the zserv side of the socket it will be | |
537 | * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure. | |
538 | * | |
539 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 | |
540 | * byte value. | |
541 | * | |
542 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 | |
543 | * byte value. | |
544 | * | |
545 | * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value | |
546 | * | |
547 | * XXX: No attention paid to alignment. | |
548 | */ | |
549 | int | |
550 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, | |
551 | struct zapi_ipv4 *api) | |
552 | { | |
553 | int i; | |
554 | int psize; | |
555 | struct stream *s; | |
556 | ||
557 | /* Reset stream. */ | |
558 | s = zclient->obuf; | |
559 | stream_reset (s); | |
560 | ||
561 | zclient_create_header (s, cmd); | |
562 | ||
563 | /* Put type and nexthop. */ | |
564 | stream_putc (s, api->type); | |
565 | stream_putw (s, api->instance); | |
566 | stream_putc (s, api->flags); | |
567 | stream_putc (s, api->message); | |
568 | stream_putw (s, api->safi); | |
569 | ||
570 | /* Put prefix information. */ | |
571 | psize = PSIZE (p->prefixlen); | |
572 | stream_putc (s, p->prefixlen); | |
573 | stream_write (s, (u_char *) & p->prefix, psize); | |
574 | ||
575 | /* Nexthop, ifindex, distance and metric information. */ | |
576 | /* ZAPI_MESSAGE_ONLINK implies interleaving */ | |
577 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_ONLINK)) | |
578 | { | |
579 | /* ZAPI_MESSAGE_NEXTHOP is required for proper receiving */ | |
580 | assert (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)); | |
581 | /* 64-bit data units, interleaved between nexthop[] and ifindex[] */ | |
582 | assert (api->nexthop_num == api->ifindex_num); | |
583 | stream_putc (s, api->nexthop_num * 2); | |
584 | for (i = 0; i < api->nexthop_num; i++) | |
585 | { | |
586 | stream_putc (s, ZEBRA_NEXTHOP_IPV4_ONLINK); | |
587 | stream_put_in_addr (s, api->nexthop[i]); | |
588 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
589 | stream_putl (s, api->ifindex[i]); | |
590 | } | |
591 | } | |
592 | else if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) | |
593 | { | |
594 | /* traditional 32-bit data units */ | |
595 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) | |
596 | { | |
597 | stream_putc (s, 1); | |
598 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
599 | /* XXX assert(api->nexthop_num == 0); */ | |
600 | /* XXX assert(api->ifindex_num == 0); */ | |
601 | } | |
602 | else | |
603 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
604 | ||
605 | for (i = 0; i < api->nexthop_num; i++) | |
606 | { | |
607 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); | |
608 | stream_put_in_addr (s, api->nexthop[i]); | |
609 | } | |
610 | for (i = 0; i < api->ifindex_num; i++) | |
611 | { | |
612 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
613 | stream_putl (s, api->ifindex[i]); | |
614 | } | |
615 | } | |
616 | ||
617 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
618 | stream_putc (s, api->distance); | |
619 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
620 | stream_putl (s, api->metric); | |
621 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) | |
622 | stream_putw (s, api->tag); | |
623 | ||
624 | /* Put length at the first point of the stream. */ | |
625 | stream_putw_at (s, 0, stream_get_endp (s)); | |
626 | ||
627 | return zclient_send_message(zclient); | |
628 | } | |
629 | ||
630 | #ifdef HAVE_IPV6 | |
631 | int | |
632 | zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient, | |
633 | struct prefix_ipv4 *p, struct zapi_ipv6 *api) | |
634 | { | |
635 | int i; | |
636 | int psize; | |
637 | struct stream *s; | |
638 | ||
639 | /* Reset stream. */ | |
640 | s = zclient->obuf; | |
641 | stream_reset (s); | |
642 | ||
643 | zclient_create_header (s, cmd); | |
644 | ||
645 | /* Put type and nexthop. */ | |
646 | stream_putc (s, api->type); | |
647 | stream_putw (s, api->instance); | |
648 | stream_putc (s, api->flags); | |
649 | stream_putc (s, api->message); | |
650 | stream_putw (s, api->safi); | |
651 | ||
652 | /* Put prefix information. */ | |
653 | psize = PSIZE (p->prefixlen); | |
654 | stream_putc (s, p->prefixlen); | |
655 | stream_write (s, (u_char *) & p->prefix, psize); | |
656 | ||
657 | /* Nexthop, ifindex, distance and metric information. */ | |
658 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) | |
659 | { | |
660 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) | |
661 | { | |
662 | stream_putc (s, 1); | |
663 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
664 | /* XXX assert(api->nexthop_num == 0); */ | |
665 | /* XXX assert(api->ifindex_num == 0); */ | |
666 | } | |
667 | else | |
668 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
669 | ||
670 | for (i = 0; i < api->nexthop_num; i++) | |
671 | { | |
672 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); | |
673 | stream_write (s, (u_char *)api->nexthop[i], 16); | |
674 | } | |
675 | for (i = 0; i < api->ifindex_num; i++) | |
676 | { | |
677 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
678 | stream_putl (s, api->ifindex[i]); | |
679 | } | |
680 | } | |
681 | ||
682 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
683 | stream_putc (s, api->distance); | |
684 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
685 | stream_putl (s, api->metric); | |
686 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) | |
687 | stream_putw (s, api->tag); | |
688 | ||
689 | /* Put length at the first point of the stream. */ | |
690 | stream_putw_at (s, 0, stream_get_endp (s)); | |
691 | ||
692 | return zclient_send_message(zclient); | |
693 | } | |
694 | ||
695 | int | |
696 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, | |
697 | struct zapi_ipv6 *api) | |
698 | { | |
699 | int i; | |
700 | int psize; | |
701 | struct stream *s; | |
702 | ||
703 | /* Reset stream. */ | |
704 | s = zclient->obuf; | |
705 | stream_reset (s); | |
706 | ||
707 | zclient_create_header (s, cmd); | |
708 | ||
709 | /* Put type and nexthop. */ | |
710 | stream_putc (s, api->type); | |
711 | stream_putw (s, api->instance); | |
712 | stream_putc (s, api->flags); | |
713 | stream_putc (s, api->message); | |
714 | stream_putw (s, api->safi); | |
715 | ||
716 | /* Put prefix information. */ | |
717 | psize = PSIZE (p->prefixlen); | |
718 | stream_putc (s, p->prefixlen); | |
719 | stream_write (s, (u_char *)&p->prefix, psize); | |
720 | ||
721 | /* Nexthop, ifindex, distance and metric information. */ | |
722 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) | |
723 | { | |
724 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) | |
725 | { | |
726 | stream_putc (s, 1); | |
727 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
728 | /* XXX assert(api->nexthop_num == 0); */ | |
729 | /* XXX assert(api->ifindex_num == 0); */ | |
730 | } | |
731 | else | |
732 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
733 | ||
734 | for (i = 0; i < api->nexthop_num; i++) | |
735 | { | |
736 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); | |
737 | stream_write (s, (u_char *)api->nexthop[i], 16); | |
738 | } | |
739 | for (i = 0; i < api->ifindex_num; i++) | |
740 | { | |
741 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
742 | stream_putl (s, api->ifindex[i]); | |
743 | } | |
744 | } | |
745 | ||
746 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
747 | stream_putc (s, api->distance); | |
748 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
749 | stream_putl (s, api->metric); | |
750 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) | |
751 | stream_putw (s, api->tag); | |
752 | ||
753 | /* Put length at the first point of the stream. */ | |
754 | stream_putw_at (s, 0, stream_get_endp (s)); | |
755 | ||
756 | return zclient_send_message(zclient); | |
757 | } | |
758 | #endif /* HAVE_IPV6 */ | |
759 | ||
760 | /* | |
761 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE | |
762 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will | |
763 | * then set/unset redist[type] in the client handle (a struct zserv) for the | |
764 | * sending client | |
765 | */ | |
766 | int | |
767 | zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type, | |
768 | u_short instance) | |
769 | { | |
770 | struct stream *s; | |
771 | ||
772 | s = zclient->obuf; | |
773 | stream_reset(s); | |
774 | ||
775 | zclient_create_header (s, command); | |
776 | stream_putc (s, afi); | |
777 | stream_putc (s, type); | |
778 | stream_putw (s, instance); | |
779 | ||
780 | stream_putw_at (s, 0, stream_get_endp (s)); | |
781 | ||
782 | return zclient_send_message(zclient); | |
783 | } | |
784 | ||
785 | /* Router-id update from zebra daemon. */ | |
786 | void | |
787 | zebra_router_id_update_read (struct stream *s, struct prefix *rid) | |
788 | { | |
789 | int plen; | |
790 | ||
791 | /* Fetch interface address. */ | |
792 | rid->family = stream_getc (s); | |
793 | ||
794 | plen = prefix_blen (rid); | |
795 | stream_get (&rid->u.prefix, s, plen); | |
796 | rid->prefixlen = stream_getc (s); | |
797 | } | |
798 | ||
799 | /* Interface addition from zebra daemon. */ | |
800 | /* | |
801 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or | |
802 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: | |
803 | * 0 1 2 3 | |
804 | * 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 | |
805 | * +-+-+-+-+-+-+-+-+ | |
806 | * | type | | |
807 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
808 | * | ifname | | |
809 | * | | | |
810 | * | | | |
811 | * | | | |
812 | * | | | |
813 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
814 | * | ifindex | | |
815 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
816 | * | if_flags | | |
817 | * | | | |
818 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
819 | * | metric | | |
820 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
821 | * | ifmtu | | |
822 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
823 | * | ifmtu6 | | |
824 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
825 | * | bandwidth | | |
826 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
827 | * | sockaddr_dl | | |
828 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
829 | */ | |
830 | ||
831 | struct interface * | |
832 | zebra_interface_add_read (struct stream *s) | |
833 | { | |
834 | struct interface *ifp; | |
835 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
836 | ||
837 | /* Read interface name. */ | |
838 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
839 | ||
840 | /* Lookup/create interface by name. */ | |
841 | ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ)); | |
842 | ||
843 | zebra_interface_if_set_value (s, ifp); | |
844 | ||
845 | return ifp; | |
846 | } | |
847 | ||
848 | /* | |
849 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) | |
850 | * from zebra server. The format of this message is the same as | |
851 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see | |
852 | * comments for zebra_interface_add_read), except that no sockaddr_dl | |
853 | * is sent at the tail of the message. | |
854 | */ | |
855 | struct interface * | |
856 | zebra_interface_state_read (struct stream *s) | |
857 | { | |
858 | struct interface *ifp; | |
859 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
860 | ||
861 | /* Read interface name. */ | |
862 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
863 | ||
864 | /* Lookup this by interface index. */ | |
865 | ifp = if_lookup_by_name_len (ifname_tmp, | |
866 | strnlen(ifname_tmp, INTERFACE_NAMSIZ)); | |
867 | ||
868 | /* If such interface does not exist, indicate an error */ | |
869 | if (! ifp) | |
870 | return NULL; | |
871 | ||
872 | zebra_interface_if_set_value (s, ifp); | |
873 | ||
874 | return ifp; | |
875 | } | |
876 | ||
877 | /* | |
878 | * format of message for address additon is: | |
879 | * 0 | |
880 | * 0 1 2 3 4 5 6 7 | |
881 | * +-+-+-+-+-+-+-+-+ | |
882 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or | |
883 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE | |
884 | * | | | |
885 | * + + | |
886 | * | ifindex | | |
887 | * + + | |
888 | * | | | |
889 | * + + | |
890 | * | | | |
891 | * +-+-+-+-+-+-+-+-+ | |
892 | * | ifc_flags | flags for connected address | |
893 | * +-+-+-+-+-+-+-+-+ | |
894 | * | addr_family | | |
895 | * +-+-+-+-+-+-+-+-+ | |
896 | * | addr... | | |
897 | * : : | |
898 | * | | | |
899 | * +-+-+-+-+-+-+-+-+ | |
900 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. | |
901 | * +-+-+-+-+-+-+-+-+ | |
902 | * | daddr.. | | |
903 | * : : | |
904 | * | | | |
905 | * +-+-+-+-+-+-+-+-+ | |
906 | * | |
907 | */ | |
908 | ||
909 | void | |
910 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) | |
911 | { | |
912 | /* Read interface's index. */ | |
913 | ifp->ifindex = stream_getl (s); | |
914 | ifp->status = stream_getc (s); | |
915 | ||
916 | /* Read interface's value. */ | |
917 | ifp->flags = stream_getq (s); | |
918 | ifp->ptm_enable = stream_getc (s); | |
919 | ifp->ptm_status = stream_getc (s); | |
920 | ifp->metric = stream_getl (s); | |
921 | ifp->mtu = stream_getl (s); | |
922 | ifp->mtu6 = stream_getl (s); | |
923 | ifp->bandwidth = stream_getl (s); | |
924 | #ifdef HAVE_STRUCT_SOCKADDR_DL | |
925 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage)); | |
926 | #else | |
927 | ifp->hw_addr_len = stream_getl (s); | |
928 | if (ifp->hw_addr_len) | |
929 | stream_get (ifp->hw_addr, s, ifp->hw_addr_len); | |
930 | #endif /* HAVE_STRUCT_SOCKADDR_DL */ | |
931 | } | |
932 | ||
933 | static int | |
934 | memconstant(const void *s, int c, size_t n) | |
935 | { | |
936 | const u_char *p = s; | |
937 | ||
938 | while (n-- > 0) | |
939 | if (*p++ != c) | |
940 | return 0; | |
941 | return 1; | |
942 | } | |
943 | ||
944 | ||
945 | struct connected * | |
946 | zebra_interface_address_read (int type, struct stream *s) | |
947 | { | |
948 | unsigned int ifindex; | |
949 | struct interface *ifp; | |
950 | struct connected *ifc; | |
951 | struct prefix p, d; | |
952 | int family; | |
953 | int plen; | |
954 | u_char ifc_flags; | |
955 | ||
956 | memset (&p, 0, sizeof(p)); | |
957 | memset (&d, 0, sizeof(d)); | |
958 | ||
959 | /* Get interface index. */ | |
960 | ifindex = stream_getl (s); | |
961 | ||
962 | /* Lookup index. */ | |
963 | ifp = if_lookup_by_index (ifindex); | |
964 | if (ifp == NULL) | |
965 | { | |
966 | zlog_warn ("zebra_interface_address_read(%s): " | |
967 | "Can't find interface by ifindex: %d ", | |
968 | (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"), | |
969 | ifindex); | |
970 | return NULL; | |
971 | } | |
972 | ||
973 | /* Fetch flag. */ | |
974 | ifc_flags = stream_getc (s); | |
975 | ||
976 | /* Fetch interface address. */ | |
977 | family = p.family = stream_getc (s); | |
978 | ||
979 | plen = prefix_blen (&p); | |
980 | stream_get (&p.u.prefix, s, plen); | |
981 | p.prefixlen = stream_getc (s); | |
982 | ||
983 | /* Fetch destination address. */ | |
984 | stream_get (&d.u.prefix, s, plen); | |
985 | d.family = family; | |
986 | ||
987 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) | |
988 | { | |
989 | /* N.B. NULL destination pointers are encoded as all zeroes */ | |
990 | ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ? | |
991 | NULL : &d)); | |
992 | if (ifc != NULL) | |
993 | { | |
994 | ifc->flags = ifc_flags; | |
995 | if (ifc->destination) | |
996 | ifc->destination->prefixlen = ifc->address->prefixlen; | |
997 | else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) | |
998 | { | |
999 | /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */ | |
1000 | char buf[BUFSIZ]; | |
1001 | prefix2str (ifc->address, buf, sizeof(buf)); | |
1002 | zlog_warn("warning: interface %s address %s " | |
1003 | "with peer flag set, but no peer address!", | |
1004 | ifp->name, buf); | |
1005 | UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER); | |
1006 | } | |
1007 | } | |
1008 | } | |
1009 | else | |
1010 | { | |
1011 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); | |
1012 | ifc = connected_delete_by_prefix(ifp, &p); | |
1013 | } | |
1014 | ||
1015 | return ifc; | |
1016 | } | |
1017 | ||
1018 | /* | |
1019 | * format of message for neighbor connected address is: | |
1020 | * 0 | |
1021 | * 0 1 2 3 4 5 6 7 | |
1022 | * +-+-+-+-+-+-+-+-+ | |
1023 | * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or | |
1024 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE | |
1025 | * | | | |
1026 | * + + | |
1027 | * | ifindex | | |
1028 | * + + | |
1029 | * | | | |
1030 | * + + | |
1031 | * | | | |
1032 | * +-+-+-+-+-+-+-+-+ | |
1033 | * | addr_family | | |
1034 | * +-+-+-+-+-+-+-+-+ | |
1035 | * | addr... | | |
1036 | * : : | |
1037 | * | | | |
1038 | * +-+-+-+-+-+-+-+-+ | |
1039 | * | addr_len | len of addr. | |
1040 | * +-+-+-+-+-+-+-+-+ | |
1041 | */ | |
1042 | struct nbr_connected * | |
1043 | zebra_interface_nbr_address_read (int type, struct stream *s) | |
1044 | { | |
1045 | unsigned int ifindex; | |
1046 | struct interface *ifp; | |
1047 | struct prefix p; | |
1048 | struct nbr_connected *ifc; | |
1049 | ||
1050 | /* Get interface index. */ | |
1051 | ifindex = stream_getl (s); | |
1052 | ||
1053 | /* Lookup index. */ | |
1054 | ifp = if_lookup_by_index (ifindex); | |
1055 | if (ifp == NULL) | |
1056 | { | |
1057 | zlog_warn ("zebra_nbr_interface_address_read(%s): " | |
1058 | "Can't find interface by ifindex: %d ", | |
1059 | (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD? "ADD" : "DELETE"), | |
1060 | ifindex); | |
1061 | return NULL; | |
1062 | } | |
1063 | ||
1064 | p.family = stream_getc (s); | |
1065 | stream_get (&p.u.prefix, s, prefix_blen (&p)); | |
1066 | p.prefixlen = stream_getc (s); | |
1067 | ||
1068 | if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) | |
1069 | { | |
1070 | /* Currently only supporting P2P links, so any new RA source address is | |
1071 | considered as the replacement of the previously learnt Link-Local address. */ | |
1072 | if (!(ifc = listnode_head(ifp->nbr_connected))) | |
1073 | { | |
1074 | ifc = nbr_connected_new (); | |
1075 | ifc->address = prefix_new (); | |
1076 | ifc->ifp = ifp; | |
1077 | listnode_add (ifp->nbr_connected, ifc); | |
1078 | } | |
1079 | ||
1080 | prefix_copy(ifc->address, &p); | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE); | |
1085 | ||
1086 | ifc = nbr_connected_check(ifp, &p); | |
1087 | if (ifc) | |
1088 | listnode_delete (ifp->nbr_connected, ifc); | |
1089 | } | |
1090 | ||
1091 | return ifc; | |
1092 | } | |
1093 | ||
1094 | /* Zebra client message read function. */ | |
1095 | static int | |
1096 | zclient_read (struct thread *thread) | |
1097 | { | |
1098 | size_t already; | |
1099 | uint16_t length, command; | |
1100 | uint8_t marker, version; | |
1101 | struct zclient *zclient; | |
1102 | ||
1103 | /* Get socket to zebra. */ | |
1104 | zclient = THREAD_ARG (thread); | |
1105 | zclient->t_read = NULL; | |
1106 | ||
1107 | /* Read zebra header (if we don't have it already). */ | |
1108 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) | |
1109 | { | |
1110 | ssize_t nbyte; | |
1111 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
1112 | ZEBRA_HEADER_SIZE-already)) == 0) || | |
1113 | (nbyte == -1)) | |
1114 | { | |
1115 | if (zclient_debug) | |
1116 | zlog_debug ("zclient connection closed socket [%d].", zclient->sock); | |
1117 | return zclient_failed(zclient); | |
1118 | } | |
1119 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) | |
1120 | { | |
1121 | /* Try again later. */ | |
1122 | zclient_event (ZCLIENT_READ, zclient); | |
1123 | return 0; | |
1124 | } | |
1125 | already = ZEBRA_HEADER_SIZE; | |
1126 | } | |
1127 | ||
1128 | /* Reset to read from the beginning of the incoming packet. */ | |
1129 | stream_set_getp(zclient->ibuf, 0); | |
1130 | ||
1131 | /* Fetch header values. */ | |
1132 | length = stream_getw (zclient->ibuf); | |
1133 | marker = stream_getc (zclient->ibuf); | |
1134 | version = stream_getc (zclient->ibuf); | |
1135 | command = stream_getw (zclient->ibuf); | |
1136 | ||
1137 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) | |
1138 | { | |
1139 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
1140 | __func__, zclient->sock, marker, version); | |
1141 | return zclient_failed(zclient); | |
1142 | } | |
1143 | ||
1144 | if (length < ZEBRA_HEADER_SIZE) | |
1145 | { | |
1146 | zlog_err("%s: socket %d message length %u is less than %d ", | |
1147 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); | |
1148 | return zclient_failed(zclient); | |
1149 | } | |
1150 | ||
1151 | /* Length check. */ | |
1152 | if (length > STREAM_SIZE(zclient->ibuf)) | |
1153 | { | |
1154 | struct stream *ns; | |
1155 | zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...", | |
1156 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); | |
1157 | ns = stream_new(length); | |
1158 | stream_copy(ns, zclient->ibuf); | |
1159 | stream_free (zclient->ibuf); | |
1160 | zclient->ibuf = ns; | |
1161 | } | |
1162 | ||
1163 | /* Read rest of zebra packet. */ | |
1164 | if (already < length) | |
1165 | { | |
1166 | ssize_t nbyte; | |
1167 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
1168 | length-already)) == 0) || | |
1169 | (nbyte == -1)) | |
1170 | { | |
1171 | if (zclient_debug) | |
1172 | zlog_debug("zclient connection closed socket [%d].", zclient->sock); | |
1173 | return zclient_failed(zclient); | |
1174 | } | |
1175 | if (nbyte != (ssize_t)(length-already)) | |
1176 | { | |
1177 | /* Try again later. */ | |
1178 | zclient_event (ZCLIENT_READ, zclient); | |
1179 | return 0; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | length -= ZEBRA_HEADER_SIZE; | |
1184 | ||
1185 | if (zclient_debug) | |
1186 | zlog_debug("zclient 0x%p command 0x%x \n", zclient, command); | |
1187 | ||
1188 | switch (command) | |
1189 | { | |
1190 | case ZEBRA_ROUTER_ID_UPDATE: | |
1191 | if (zclient->router_id_update) | |
1192 | (*zclient->router_id_update) (command, zclient, length); | |
1193 | break; | |
1194 | case ZEBRA_INTERFACE_ADD: | |
1195 | if (zclient->interface_add) | |
1196 | (*zclient->interface_add) (command, zclient, length); | |
1197 | break; | |
1198 | case ZEBRA_INTERFACE_DELETE: | |
1199 | if (zclient->interface_delete) | |
1200 | (*zclient->interface_delete) (command, zclient, length); | |
1201 | break; | |
1202 | case ZEBRA_INTERFACE_ADDRESS_ADD: | |
1203 | if (zclient->interface_address_add) | |
1204 | (*zclient->interface_address_add) (command, zclient, length); | |
1205 | break; | |
1206 | case ZEBRA_INTERFACE_ADDRESS_DELETE: | |
1207 | if (zclient->interface_address_delete) | |
1208 | (*zclient->interface_address_delete) (command, zclient, length); | |
1209 | break; | |
1210 | case ZEBRA_INTERFACE_BFD_DEST_UPDATE: | |
1211 | if (zclient->interface_bfd_dest_update) | |
1212 | (*zclient->interface_bfd_dest_update) (command, zclient, length); | |
1213 | break; | |
1214 | case ZEBRA_INTERFACE_NBR_ADDRESS_ADD: | |
1215 | if (zclient->interface_nbr_address_add) | |
1216 | (*zclient->interface_nbr_address_add) (command, zclient, length); | |
1217 | break; | |
1218 | case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE: | |
1219 | if (zclient->interface_nbr_address_delete) | |
1220 | (*zclient->interface_nbr_address_delete) (command, zclient, length); | |
1221 | break; | |
1222 | case ZEBRA_INTERFACE_UP: | |
1223 | if (zclient->interface_up) | |
1224 | (*zclient->interface_up) (command, zclient, length); | |
1225 | break; | |
1226 | case ZEBRA_INTERFACE_DOWN: | |
1227 | if (zclient->interface_down) | |
1228 | (*zclient->interface_down) (command, zclient, length); | |
1229 | break; | |
1230 | case ZEBRA_IPV4_ROUTE_ADD: | |
1231 | if (zclient->ipv4_route_add) | |
1232 | (*zclient->ipv4_route_add) (command, zclient, length); | |
1233 | break; | |
1234 | case ZEBRA_IPV4_ROUTE_DELETE: | |
1235 | if (zclient->ipv4_route_delete) | |
1236 | (*zclient->ipv4_route_delete) (command, zclient, length); | |
1237 | break; | |
1238 | case ZEBRA_IPV6_ROUTE_ADD: | |
1239 | if (zclient->ipv6_route_add) | |
1240 | (*zclient->ipv6_route_add) (command, zclient, length); | |
1241 | break; | |
1242 | case ZEBRA_IPV6_ROUTE_DELETE: | |
1243 | if (zclient->ipv6_route_delete) | |
1244 | (*zclient->ipv6_route_delete) (command, zclient, length); | |
1245 | break; | |
1246 | case ZEBRA_NEXTHOP_UPDATE: | |
1247 | if (zclient_debug) | |
1248 | zlog_debug("zclient rcvd nexthop update\n"); | |
1249 | if (zclient->nexthop_update) | |
1250 | (*zclient->nexthop_update) (command, zclient, length); | |
1251 | break; | |
1252 | case ZEBRA_IMPORT_CHECK_UPDATE: | |
1253 | if (zclient_debug) | |
1254 | zlog_debug("zclient rcvd import check update\n"); | |
1255 | if (zclient->import_check_update) | |
1256 | (*zclient->import_check_update) (command, zclient, length); | |
1257 | break; | |
1258 | case ZEBRA_BFD_DEST_REPLAY: | |
1259 | if (zclient->bfd_dest_replay) | |
1260 | (*zclient->bfd_dest_replay) (command, zclient, length); | |
1261 | break; | |
1262 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: | |
1263 | if (zclient->redistribute_route_ipv4_add) | |
1264 | (*zclient->redistribute_route_ipv4_add) (command, zclient, length); | |
1265 | break; | |
1266 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: | |
1267 | if (zclient->redistribute_route_ipv4_del) | |
1268 | (*zclient->redistribute_route_ipv4_del) (command, zclient, length); | |
1269 | break; | |
1270 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: | |
1271 | if (zclient->redistribute_route_ipv6_add) | |
1272 | (*zclient->redistribute_route_ipv6_add) (command, zclient, length); | |
1273 | break; | |
1274 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: | |
1275 | if (zclient->redistribute_route_ipv6_del) | |
1276 | (*zclient->redistribute_route_ipv6_del) (command, zclient, length); | |
1277 | break; | |
1278 | default: | |
1279 | break; | |
1280 | } | |
1281 | ||
1282 | if (zclient->sock < 0) | |
1283 | /* Connection was closed during packet processing. */ | |
1284 | return -1; | |
1285 | ||
1286 | /* Register read thread. */ | |
1287 | stream_reset(zclient->ibuf); | |
1288 | zclient_event (ZCLIENT_READ, zclient); | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | void | |
1294 | zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type, | |
1295 | u_short instance) | |
1296 | { | |
1297 | ||
1298 | if (command == ZEBRA_REDISTRIBUTE_ADD) | |
1299 | { | |
1300 | if (redist_check_instance(&zclient->redist[afi][type], instance)) | |
1301 | return; | |
1302 | redist_add_instance(&zclient->redist[afi][type], instance); | |
1303 | } | |
1304 | else | |
1305 | { | |
1306 | if (!redist_check_instance(&zclient->redist[afi][type], instance)) | |
1307 | return; | |
1308 | redist_del_instance(&zclient->redist[afi][type], instance); | |
1309 | } | |
1310 | ||
1311 | if (zclient->sock > 0) | |
1312 | zebra_redistribute_send (command, zclient, afi, type, instance); | |
1313 | } | |
1314 | ||
1315 | ||
1316 | void | |
1317 | zclient_redistribute_default (int command, struct zclient *zclient) | |
1318 | { | |
1319 | ||
1320 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) | |
1321 | { | |
1322 | if (zclient->default_information) | |
1323 | return; | |
1324 | zclient->default_information = 1; | |
1325 | } | |
1326 | else | |
1327 | { | |
1328 | if (!zclient->default_information) | |
1329 | return; | |
1330 | zclient->default_information = 0; | |
1331 | } | |
1332 | ||
1333 | if (zclient->sock > 0) | |
1334 | zebra_message_send (zclient, command); | |
1335 | } | |
1336 | ||
1337 | static void | |
1338 | zclient_event (enum event event, struct zclient *zclient) | |
1339 | { | |
1340 | switch (event) | |
1341 | { | |
1342 | case ZCLIENT_SCHEDULE: | |
1343 | if (! zclient->t_connect) | |
1344 | zclient->t_connect = | |
1345 | thread_add_event (zclient->master, zclient_connect, zclient, 0); | |
1346 | break; | |
1347 | case ZCLIENT_CONNECT: | |
1348 | if (zclient->fail >= 10) | |
1349 | return; | |
1350 | if (zclient_debug) | |
1351 | zlog_debug ("zclient connect schedule interval is %d", | |
1352 | zclient->fail < 3 ? 10 : 60); | |
1353 | if (! zclient->t_connect) | |
1354 | zclient->t_connect = | |
1355 | thread_add_timer (zclient->master, zclient_connect, zclient, | |
1356 | zclient->fail < 3 ? 10 : 60); | |
1357 | break; | |
1358 | case ZCLIENT_READ: | |
1359 | zclient->t_read = | |
1360 | thread_add_read (zclient->master, zclient_read, zclient, zclient->sock); | |
1361 | break; | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | void | |
1366 | zclient_serv_path_set (char *path) | |
1367 | { | |
1368 | struct stat sb; | |
1369 | ||
1370 | /* reset */ | |
1371 | zclient_serv_path = NULL; | |
1372 | ||
1373 | /* test if `path' is socket. don't set it otherwise. */ | |
1374 | if (stat(path, &sb) == -1) | |
1375 | { | |
1376 | zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path); | |
1377 | return; | |
1378 | } | |
1379 | ||
1380 | if ((sb.st_mode & S_IFMT) != S_IFSOCK) | |
1381 | { | |
1382 | zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path); | |
1383 | return; | |
1384 | } | |
1385 | ||
1386 | /* it seems that path is unix socket */ | |
1387 | zclient_serv_path = path; | |
1388 | } | |
1389 |