]>
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 | ||
b5114685 VT |
42 | char *zclient_serv_path = NULL; |
43 | ||
718e3744 | 44 | /* This file local debug flag. */ |
45 | int zclient_debug = 0; | |
6b0655a2 | 46 | |
718e3744 | 47 | /* Allocate zclient structure. */ |
48 | struct zclient * | |
4140ca4d | 49 | zclient_new (struct thread_master *master) |
718e3744 | 50 | { |
51 | struct zclient *zclient; | |
393deb9b | 52 | zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient)); |
718e3744 | 53 | |
54 | zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
55 | zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
634f9ea2 | 56 | zclient->wb = buffer_new(0); |
4140ca4d | 57 | zclient->master = master; |
718e3744 | 58 | |
59 | return zclient; | |
60 | } | |
61 | ||
228da428 | 62 | /* This function is only called when exiting, because |
634f9ea2 | 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. | |
634f9ea2 | 65 | |
228da428 | 66 | Free zclient structure. */ |
718e3744 | 67 | void |
68 | zclient_free (struct zclient *zclient) | |
69 | { | |
634f9ea2 | 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 | ||
718e3744 | 77 | XFREE (MTYPE_ZCLIENT, zclient); |
78 | } | |
79 | ||
7c8ff89e DS |
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) | |
24873f0c | 118 | return; |
7c8ff89e DS |
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 | ||
718e3744 | 136 | /* Stop zebra client services. */ |
137 | void | |
138 | zclient_stop (struct zclient *zclient) | |
139 | { | |
140 | if (zclient_debug) | |
8ddca704 | 141 | zlog_debug ("zclient stopped"); |
718e3744 | 142 | |
143 | /* Stop threads. */ | |
634f9ea2 | 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); | |
718e3744 | 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 | { | |
3d68677e DS |
167 | int afi; |
168 | ||
718e3744 | 169 | zclient_stop (zclient); |
3d68677e DS |
170 | |
171 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
172 | redist_del_instance (&zclient->redist[afi][zclient->redist_default], zclient->instance); | |
173 | ||
7c8ff89e | 174 | zclient_init (zclient, zclient->redist_default, zclient->instance); |
718e3744 | 175 | } |
176 | ||
b5114685 VT |
177 | #ifdef HAVE_TCP_ZEBRA |
178 | ||
718e3744 | 179 | /* Make socket to zebra daemon. Return zebra socket. */ |
b5114685 | 180 | static int |
634f9ea2 | 181 | zclient_socket(void) |
718e3744 | 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); | |
6f0e3f6e | 196 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 197 | serv.sin_len = sizeof (struct sockaddr_in); |
6f0e3f6e | 198 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 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 | ||
3414d035 | 211 | #else |
b5114685 | 212 | |
718e3744 | 213 | /* For sockaddr_un. */ |
214 | #include <sys/un.h> | |
215 | ||
b5114685 | 216 | static int |
8c328f11 | 217 | zclient_socket_un (const char *path) |
718e3744 | 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)); | |
6f0e3f6e | 231 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
718e3744 | 232 | len = addr.sun_len = SUN_LEN(&addr); |
233 | #else | |
234 | len = sizeof (addr.sun_family) + strlen (addr.sun_path); | |
6f0e3f6e | 235 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
718e3744 | 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 | ||
3414d035 VT |
246 | #endif /* HAVE_TCP_ZEBRA */ |
247 | ||
b5114685 VT |
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 | ||
634f9ea2 | 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: | |
4140ca4d | 291 | zclient->t_write = thread_add_write(zclient->master, zclient_flush_data, |
634f9ea2 | 292 | zclient, zclient->sock); |
293 | break; | |
294 | case BUFFER_EMPTY: | |
295 | break; | |
296 | } | |
297 | return 0; | |
298 | } | |
299 | ||
718e3744 | 300 | int |
634f9ea2 | 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: | |
4140ca4d | 317 | THREAD_WRITE_ON(zclient->master, zclient->t_write, |
634f9ea2 | 318 | zclient_flush_data, zclient, zclient->sock); |
319 | break; | |
320 | } | |
321 | return 0; | |
322 | } | |
323 | ||
d211086a | 324 | void |
c1b9800a | 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 | ||
634f9ea2 | 334 | /* Send simple Zebra message. */ |
335 | static int | |
718e3744 | 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. */ | |
c1b9800a | 345 | zclient_create_header (s, command); |
346 | ||
634f9ea2 | 347 | return zclient_send_message(zclient); |
718e3744 | 348 | } |
349 | ||
2ea1ab1c VT |
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); | |
7c8ff89e | 362 | stream_putw (s, zclient->instance); |
2ea1ab1c VT |
363 | stream_putw_at (s, 0, stream_get_endp (s)); |
364 | return zclient_send_message(zclient); | |
365 | } | |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
718e3744 | 370 | /* Make connection to zebra daemon. */ |
371 | int | |
372 | zclient_start (struct zclient *zclient) | |
373 | { | |
374 | int i; | |
8bb0831e | 375 | afi_t afi; |
718e3744 | 376 | |
718e3744 | 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 | ||
718e3744 | 385 | /* Flush all redistribute request. */ |
8bb0831e DS |
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; | |
7c8ff89e | 392 | |
8bb0831e DS |
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 | } | |
718e3744 | 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. */ | |
634f9ea2 | 407 | static int |
718e3744 | 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) | |
8ddca704 | 416 | zlog_debug ("zclient_connect is called"); |
718e3744 | 417 | |
418 | return zclient_start (zclient); | |
419 | } | |
6b0655a2 | 420 | |
078430f6 DS |
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 | ||
0a589359 | 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 | * | |
c8a1cb5c DS |
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 | * | |
0a589359 | 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 | * | |
0d9551dc DS |
545 | * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value |
546 | * | |
0a589359 | 547 | * XXX: No attention paid to alignment. |
548 | */ | |
718e3744 | 549 | int |
0a589359 | 550 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, |
551 | struct zapi_ipv4 *api) | |
718e3744 | 552 | { |
553 | int i; | |
554 | int psize; | |
555 | struct stream *s; | |
556 | ||
557 | /* Reset stream. */ | |
558 | s = zclient->obuf; | |
559 | stream_reset (s); | |
c1b9800a | 560 | |
561 | zclient_create_header (s, cmd); | |
562 | ||
563 | /* Put type and nexthop. */ | |
718e3744 | 564 | stream_putc (s, api->type); |
7c8ff89e | 565 | stream_putw (s, api->instance); |
718e3744 | 566 | stream_putc (s, api->flags); |
567 | stream_putc (s, api->message); | |
5a616c08 | 568 | stream_putw (s, api->safi); |
718e3744 | 569 | |
718e3744 | 570 | /* Put prefix information. */ |
571 | psize = PSIZE (p->prefixlen); | |
572 | stream_putc (s, p->prefixlen); | |
0a589359 | 573 | stream_write (s, (u_char *) & p->prefix, psize); |
718e3744 | 574 | |
575 | /* Nexthop, ifindex, distance and metric information. */ | |
c8a1cb5c DS |
576 | /* ZAPI_MESSAGE_ONLINK implies interleaving */ |
577 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_ONLINK)) | |
718e3744 | 578 | { |
c8a1cb5c DS |
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 */ | |
595db7f1 | 595 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
596 | { | |
597 | stream_putc (s, 1); | |
598 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
0a589359 | 599 | /* XXX assert(api->nexthop_num == 0); */ |
600 | /* XXX assert(api->ifindex_num == 0); */ | |
595db7f1 | 601 | } |
602 | else | |
603 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
718e3744 | 604 | |
605 | for (i = 0; i < api->nexthop_num; i++) | |
595db7f1 | 606 | { |
607 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); | |
608 | stream_put_in_addr (s, api->nexthop[i]); | |
609 | } | |
718e3744 | 610 | for (i = 0; i < api->ifindex_num; i++) |
595db7f1 | 611 | { |
612 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
613 | stream_putl (s, api->ifindex[i]); | |
614 | } | |
718e3744 | 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); | |
0d9551dc DS |
621 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) |
622 | stream_putw (s, api->tag); | |
718e3744 | 623 | |
624 | /* Put length at the first point of the stream. */ | |
625 | stream_putw_at (s, 0, stream_get_endp (s)); | |
626 | ||
634f9ea2 | 627 | return zclient_send_message(zclient); |
718e3744 | 628 | } |
629 | ||
630 | #ifdef HAVE_IPV6 | |
8a92a8a0 DS |
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 | ||
718e3744 | 695 | int |
0a589359 | 696 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
718e3744 | 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 | ||
c1b9800a | 707 | zclient_create_header (s, cmd); |
718e3744 | 708 | |
c1b9800a | 709 | /* Put type and nexthop. */ |
718e3744 | 710 | stream_putc (s, api->type); |
7c8ff89e | 711 | stream_putw (s, api->instance); |
718e3744 | 712 | stream_putc (s, api->flags); |
713 | stream_putc (s, api->message); | |
c7ec179a | 714 | stream_putw (s, api->safi); |
718e3744 | 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 | { | |
c3c0ac83 DS |
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); | |
718e3744 | 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); | |
0d9551dc DS |
750 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) |
751 | stream_putw (s, api->tag); | |
718e3744 | 752 | |
753 | /* Put length at the first point of the stream. */ | |
754 | stream_putw_at (s, 0, stream_get_endp (s)); | |
755 | ||
634f9ea2 | 756 | return zclient_send_message(zclient); |
718e3744 | 757 | } |
718e3744 | 758 | #endif /* HAVE_IPV6 */ |
759 | ||
0a589359 | 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 | */ | |
718e3744 | 766 | int |
8bb0831e | 767 | zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type, |
7c8ff89e | 768 | u_short instance) |
718e3744 | 769 | { |
718e3744 | 770 | struct stream *s; |
771 | ||
634f9ea2 | 772 | s = zclient->obuf; |
773 | stream_reset(s); | |
718e3744 | 774 | |
c1b9800a | 775 | zclient_create_header (s, command); |
8bb0831e | 776 | stream_putc (s, afi); |
718e3744 | 777 | stream_putc (s, type); |
7c8ff89e | 778 | stream_putw (s, instance); |
c1b9800a | 779 | |
780 | stream_putw_at (s, 0, stream_get_endp (s)); | |
781 | ||
634f9ea2 | 782 | return zclient_send_message(zclient); |
718e3744 | 783 | } |
784 | ||
18a6dce6 | 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 | ||
718e3744 | 799 | /* Interface addition from zebra daemon. */ |
0a589359 | 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 | | |
c77d4546 | 817 | * | | |
0a589359 | 818 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
819 | * | metric | | |
820 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
821 | * | ifmtu | | |
822 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
823 | * | ifmtu6 | | |
824 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
825 | * | bandwidth | | |
826 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
827 | * | sockaddr_dl | | |
828 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
829 | */ | |
830 | ||
718e3744 | 831 | struct interface * |
832 | zebra_interface_add_read (struct stream *s) | |
833 | { | |
834 | struct interface *ifp; | |
02ff83c5 | 835 | char ifname_tmp[INTERFACE_NAMSIZ]; |
718e3744 | 836 | |
837 | /* Read interface name. */ | |
838 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
839 | ||
a349198f | 840 | /* Lookup/create interface by name. */ |
841 | ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ)); | |
718e3744 | 842 | |
51d4ef83 | 843 | zebra_interface_if_set_value (s, ifp); |
718e3744 | 844 | |
718e3744 | 845 | return ifp; |
846 | } | |
847 | ||
0a589359 | 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 | */ | |
718e3744 | 855 | struct interface * |
856 | zebra_interface_state_read (struct stream *s) | |
857 | { | |
858 | struct interface *ifp; | |
02ff83c5 | 859 | char ifname_tmp[INTERFACE_NAMSIZ]; |
718e3744 | 860 | |
861 | /* Read interface name. */ | |
862 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
863 | ||
864 | /* Lookup this by interface index. */ | |
a349198f | 865 | ifp = if_lookup_by_name_len (ifname_tmp, |
866 | strnlen(ifname_tmp, INTERFACE_NAMSIZ)); | |
718e3744 | 867 | |
868 | /* If such interface does not exist, indicate an error */ | |
869 | if (! ifp) | |
870 | return NULL; | |
871 | ||
51d4ef83 | 872 | zebra_interface_if_set_value (s, ifp); |
718e3744 | 873 | |
874 | return ifp; | |
875 | } | |
876 | ||
0a589359 | 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 | ||
18a6dce6 | 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); | |
508ec910 | 914 | ifp->status = stream_getc (s); |
18a6dce6 | 915 | |
916 | /* Read interface's value. */ | |
c77d4546 | 917 | ifp->flags = stream_getq (s); |
244c1cdc DS |
918 | ifp->ptm_enable = stream_getc (s); |
919 | ifp->ptm_status = stream_getc (s); | |
18a6dce6 | 920 | ifp->metric = stream_getl (s); |
921 | ifp->mtu = stream_getl (s); | |
508ec910 | 922 | ifp->mtu6 = stream_getl (s); |
18a6dce6 | 923 | ifp->bandwidth = stream_getl (s); |
51d4ef83 | 924 | #ifdef HAVE_STRUCT_SOCKADDR_DL |
ca3ccd87 | 925 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage)); |
51d4ef83 JB |
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 */ | |
18a6dce6 | 931 | } |
932 | ||
3fb9cd6e | 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 | ||
d5a5c8f0 | 944 | |
718e3744 | 945 | struct connected * |
0a589359 | 946 | zebra_interface_address_read (int type, struct stream *s) |
718e3744 | 947 | { |
948 | unsigned int ifindex; | |
949 | struct interface *ifp; | |
950 | struct connected *ifc; | |
0a589359 | 951 | struct prefix p, d; |
718e3744 | 952 | int family; |
953 | int plen; | |
0a589359 | 954 | u_char ifc_flags; |
718e3744 | 955 | |
0a589359 | 956 | memset (&p, 0, sizeof(p)); |
957 | memset (&d, 0, sizeof(d)); | |
718e3744 | 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 | { | |
0a589359 | 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); | |
718e3744 | 970 | return NULL; |
971 | } | |
972 | ||
973 | /* Fetch flag. */ | |
0a589359 | 974 | ifc_flags = stream_getc (s); |
718e3744 | 975 | |
976 | /* Fetch interface address. */ | |
977 | family = p.family = stream_getc (s); | |
978 | ||
0a589359 | 979 | plen = prefix_blen (&p); |
980 | stream_get (&p.u.prefix, s, plen); | |
718e3744 | 981 | p.prefixlen = stream_getc (s); |
982 | ||
983 | /* Fetch destination address. */ | |
0a589359 | 984 | stream_get (&d.u.prefix, s, plen); |
718e3744 | 985 | d.family = family; |
986 | ||
0a589359 | 987 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) |
988 | { | |
3fb9cd6e | 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)); | |
0a589359 | 992 | if (ifc != NULL) |
e4529636 AS |
993 | { |
994 | ifc->flags = ifc_flags; | |
995 | if (ifc->destination) | |
996 | ifc->destination->prefixlen = ifc->address->prefixlen; | |
90444ca3 DL |
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 | } | |
e4529636 | 1007 | } |
0a589359 | 1008 | } |
1009 | else | |
1010 | { | |
1011 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); | |
1012 | ifc = connected_delete_by_prefix(ifp, &p); | |
1013 | } | |
718e3744 | 1014 | |
1015 | return ifc; | |
1016 | } | |
0a589359 | 1017 | |
a80beece DS |
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 | } | |
6b0655a2 | 1093 | |
718e3744 | 1094 | /* Zebra client message read function. */ |
634f9ea2 | 1095 | static int |
718e3744 | 1096 | zclient_read (struct thread *thread) |
1097 | { | |
634f9ea2 | 1098 | size_t already; |
c1b9800a | 1099 | uint16_t length, command; |
1100 | uint8_t marker, version; | |
718e3744 | 1101 | struct zclient *zclient; |
1102 | ||
1103 | /* Get socket to zebra. */ | |
718e3744 | 1104 | zclient = THREAD_ARG (thread); |
1105 | zclient->t_read = NULL; | |
1106 | ||
634f9ea2 | 1107 | /* Read zebra header (if we don't have it already). */ |
1108 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) | |
718e3744 | 1109 | { |
634f9ea2 | 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; | |
718e3744 | 1126 | } |
1127 | ||
634f9ea2 | 1128 | /* Reset to read from the beginning of the incoming packet. */ |
1129 | stream_set_getp(zclient->ibuf, 0); | |
718e3744 | 1130 | |
c1b9800a | 1131 | /* Fetch header values. */ |
718e3744 | 1132 | length = stream_getw (zclient->ibuf); |
c1b9800a | 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 | ||
634f9ea2 | 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 | ||
718e3744 | 1151 | /* Length check. */ |
634f9ea2 | 1152 | if (length > STREAM_SIZE(zclient->ibuf)) |
718e3744 | 1153 | { |
634f9ea2 | 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); | |
718e3744 | 1159 | stream_free (zclient->ibuf); |
634f9ea2 | 1160 | zclient->ibuf = ns; |
718e3744 | 1161 | } |
718e3744 | 1162 | |
1163 | /* Read rest of zebra packet. */ | |
634f9ea2 | 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; | |
718e3744 | 1184 | |
0a589359 | 1185 | if (zclient_debug) |
8ddca704 | 1186 | zlog_debug("zclient 0x%p command 0x%x \n", zclient, command); |
0a589359 | 1187 | |
718e3744 | 1188 | switch (command) |
1189 | { | |
18a6dce6 | 1190 | case ZEBRA_ROUTER_ID_UPDATE: |
1191 | if (zclient->router_id_update) | |
9206f9ec | 1192 | (*zclient->router_id_update) (command, zclient, length); |
18a6dce6 | 1193 | break; |
718e3744 | 1194 | case ZEBRA_INTERFACE_ADD: |
1195 | if (zclient->interface_add) | |
9206f9ec | 1196 | (*zclient->interface_add) (command, zclient, length); |
718e3744 | 1197 | break; |
1198 | case ZEBRA_INTERFACE_DELETE: | |
1199 | if (zclient->interface_delete) | |
9206f9ec | 1200 | (*zclient->interface_delete) (command, zclient, length); |
718e3744 | 1201 | break; |
1202 | case ZEBRA_INTERFACE_ADDRESS_ADD: | |
1203 | if (zclient->interface_address_add) | |
9206f9ec | 1204 | (*zclient->interface_address_add) (command, zclient, length); |
718e3744 | 1205 | break; |
1206 | case ZEBRA_INTERFACE_ADDRESS_DELETE: | |
1207 | if (zclient->interface_address_delete) | |
9206f9ec | 1208 | (*zclient->interface_address_delete) (command, zclient, length); |
718e3744 | 1209 | break; |
68fe91d6 | 1210 | case ZEBRA_INTERFACE_BFD_DEST_UPDATE: |
1211 | if (zclient->interface_bfd_dest_update) | |
1212 | (*zclient->interface_bfd_dest_update) (command, zclient, length); | |
d5a5c8f0 | 1213 | break; |
a80beece DS |
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; | |
718e3744 | 1222 | case ZEBRA_INTERFACE_UP: |
1223 | if (zclient->interface_up) | |
9206f9ec | 1224 | (*zclient->interface_up) (command, zclient, length); |
718e3744 | 1225 | break; |
1226 | case ZEBRA_INTERFACE_DOWN: | |
1227 | if (zclient->interface_down) | |
9206f9ec | 1228 | (*zclient->interface_down) (command, zclient, length); |
718e3744 | 1229 | break; |
1230 | case ZEBRA_IPV4_ROUTE_ADD: | |
1231 | if (zclient->ipv4_route_add) | |
9206f9ec | 1232 | (*zclient->ipv4_route_add) (command, zclient, length); |
718e3744 | 1233 | break; |
1234 | case ZEBRA_IPV4_ROUTE_DELETE: | |
1235 | if (zclient->ipv4_route_delete) | |
9206f9ec | 1236 | (*zclient->ipv4_route_delete) (command, zclient, length); |
718e3744 | 1237 | break; |
1238 | case ZEBRA_IPV6_ROUTE_ADD: | |
1239 | if (zclient->ipv6_route_add) | |
9206f9ec | 1240 | (*zclient->ipv6_route_add) (command, zclient, length); |
718e3744 | 1241 | break; |
1242 | case ZEBRA_IPV6_ROUTE_DELETE: | |
1243 | if (zclient->ipv6_route_delete) | |
9206f9ec | 1244 | (*zclient->ipv6_route_delete) (command, zclient, length); |
718e3744 | 1245 | break; |
fb018d25 DS |
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; | |
078430f6 DS |
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; | |
c43ed2e4 DS |
1258 | case ZEBRA_BFD_DEST_REPLAY: |
1259 | if (zclient->bfd_dest_replay) | |
1260 | (*zclient->bfd_dest_replay) (command, zclient, length); | |
1261 | break; | |
718e3744 | 1262 | default: |
1263 | break; | |
1264 | } | |
1265 | ||
634f9ea2 | 1266 | if (zclient->sock < 0) |
1267 | /* Connection was closed during packet processing. */ | |
1268 | return -1; | |
1269 | ||
718e3744 | 1270 | /* Register read thread. */ |
634f9ea2 | 1271 | stream_reset(zclient->ibuf); |
718e3744 | 1272 | zclient_event (ZCLIENT_READ, zclient); |
1273 | ||
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | void | |
8bb0831e | 1278 | zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type, |
7c8ff89e | 1279 | u_short instance) |
718e3744 | 1280 | { |
718e3744 | 1281 | |
0a589359 | 1282 | if (command == ZEBRA_REDISTRIBUTE_ADD) |
1283 | { | |
8bb0831e | 1284 | if (redist_check_instance(&zclient->redist[afi][type], instance)) |
0a589359 | 1285 | return; |
8bb0831e | 1286 | redist_add_instance(&zclient->redist[afi][type], instance); |
0a589359 | 1287 | } |
1288 | else | |
1289 | { | |
8bb0831e | 1290 | if (!redist_check_instance(&zclient->redist[afi][type], instance)) |
0a589359 | 1291 | return; |
8bb0831e | 1292 | redist_del_instance(&zclient->redist[afi][type], instance); |
0a589359 | 1293 | } |
718e3744 | 1294 | |
1295 | if (zclient->sock > 0) | |
8bb0831e | 1296 | zebra_redistribute_send (command, zclient, afi, type, instance); |
718e3744 | 1297 | } |
1298 | ||
718e3744 | 1299 | |
1300 | void | |
0a589359 | 1301 | zclient_redistribute_default (int command, struct zclient *zclient) |
718e3744 | 1302 | { |
718e3744 | 1303 | |
0a589359 | 1304 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) |
1305 | { | |
1306 | if (zclient->default_information) | |
1307 | return; | |
1308 | zclient->default_information = 1; | |
1309 | } | |
1310 | else | |
1311 | { | |
1312 | if (!zclient->default_information) | |
1313 | return; | |
1314 | zclient->default_information = 0; | |
1315 | } | |
718e3744 | 1316 | |
1317 | if (zclient->sock > 0) | |
0a589359 | 1318 | zebra_message_send (zclient, command); |
718e3744 | 1319 | } |
1320 | ||
718e3744 | 1321 | static void |
1322 | zclient_event (enum event event, struct zclient *zclient) | |
1323 | { | |
1324 | switch (event) | |
1325 | { | |
1326 | case ZCLIENT_SCHEDULE: | |
1327 | if (! zclient->t_connect) | |
1328 | zclient->t_connect = | |
4140ca4d | 1329 | thread_add_event (zclient->master, zclient_connect, zclient, 0); |
718e3744 | 1330 | break; |
1331 | case ZCLIENT_CONNECT: | |
1332 | if (zclient->fail >= 10) | |
1333 | return; | |
1334 | if (zclient_debug) | |
8ddca704 | 1335 | zlog_debug ("zclient connect schedule interval is %d", |
718e3744 | 1336 | zclient->fail < 3 ? 10 : 60); |
1337 | if (! zclient->t_connect) | |
1338 | zclient->t_connect = | |
4140ca4d | 1339 | thread_add_timer (zclient->master, zclient_connect, zclient, |
718e3744 | 1340 | zclient->fail < 3 ? 10 : 60); |
1341 | break; | |
1342 | case ZCLIENT_READ: | |
1343 | zclient->t_read = | |
4140ca4d | 1344 | thread_add_read (zclient->master, zclient_read, zclient, zclient->sock); |
718e3744 | 1345 | break; |
1346 | } | |
1347 | } | |
b5114685 VT |
1348 | |
1349 | void | |
1350 | zclient_serv_path_set (char *path) | |
1351 | { | |
1352 | struct stat sb; | |
1353 | ||
1354 | /* reset */ | |
1355 | zclient_serv_path = NULL; | |
1356 | ||
1357 | /* test if `path' is socket. don't set it otherwise. */ | |
1358 | if (stat(path, &sb) == -1) | |
1359 | { | |
1360 | zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path); | |
1361 | return; | |
1362 | } | |
1363 | ||
1364 | if ((sb.st_mode & S_IFMT) != S_IFSOCK) | |
1365 | { | |
1366 | zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path); | |
1367 | return; | |
1368 | } | |
1369 | ||
1370 | /* it seems that path is unix socket */ | |
1371 | zclient_serv_path = path; | |
1372 | } | |
1373 |