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