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