]>
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 | ||
744f4685 | 42 | const char *zclient_serv_path = NULL; |
b5114685 | 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 | { | |
7076bb2f | 167 | afi_t afi; |
3d68677e | 168 | |
718e3744 | 169 | zclient_stop (zclient); |
3d68677e DS |
170 | |
171 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
7076bb2f | 172 | redist_del_instance (&zclient->mi_redist[afi][zclient->redist_default], zclient->instance); |
3d68677e | 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 | { | |
4ecc09d3 | 205 | zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno); |
718e3744 | 206 | close (sock); |
207 | return -1; | |
208 | } | |
209 | return sock; | |
210 | } | |
211 | ||
3414d035 | 212 | #else |
b5114685 | 213 | |
718e3744 | 214 | /* For sockaddr_un. */ |
215 | #include <sys/un.h> | |
216 | ||
1b91e000 | 217 | static int |
8c328f11 | 218 | zclient_socket_un (const char *path) |
718e3744 | 219 | { |
220 | int ret; | |
221 | int sock, len; | |
222 | struct sockaddr_un addr; | |
223 | ||
224 | sock = socket (AF_UNIX, SOCK_STREAM, 0); | |
225 | if (sock < 0) | |
226 | return -1; | |
227 | ||
228 | /* Make server socket. */ | |
229 | memset (&addr, 0, sizeof (struct sockaddr_un)); | |
230 | addr.sun_family = AF_UNIX; | |
231 | strncpy (addr.sun_path, path, strlen (path)); | |
6f0e3f6e | 232 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
718e3744 | 233 | len = addr.sun_len = SUN_LEN(&addr); |
234 | #else | |
235 | len = sizeof (addr.sun_family) + strlen (addr.sun_path); | |
6f0e3f6e | 236 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
718e3744 | 237 | |
238 | ret = connect (sock, (struct sockaddr *) &addr, len); | |
239 | if (ret < 0) | |
240 | { | |
4ecc09d3 | 241 | zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno); |
718e3744 | 242 | close (sock); |
243 | return -1; | |
244 | } | |
245 | return sock; | |
246 | } | |
247 | ||
3414d035 VT |
248 | #endif /* HAVE_TCP_ZEBRA */ |
249 | ||
b5114685 VT |
250 | /** |
251 | * Connect to zebra daemon. | |
252 | * @param zclient a pointer to zclient structure | |
253 | * @return socket fd just to make sure that connection established | |
254 | * @see zclient_init | |
255 | * @see zclient_new | |
256 | */ | |
257 | int | |
258 | zclient_socket_connect (struct zclient *zclient) | |
259 | { | |
260 | #ifdef HAVE_TCP_ZEBRA | |
261 | zclient->sock = zclient_socket (); | |
262 | #else | |
12e41d03 | 263 | zclient->sock = zclient_socket_un (zclient_serv_path_get()); |
b5114685 VT |
264 | #endif |
265 | return zclient->sock; | |
266 | } | |
267 | ||
634f9ea2 | 268 | static int |
269 | zclient_failed(struct zclient *zclient) | |
270 | { | |
271 | zclient->fail++; | |
272 | zclient_stop(zclient); | |
273 | zclient_event(ZCLIENT_CONNECT, zclient); | |
274 | return -1; | |
275 | } | |
276 | ||
277 | static int | |
278 | zclient_flush_data(struct thread *thread) | |
279 | { | |
280 | struct zclient *zclient = THREAD_ARG(thread); | |
281 | ||
282 | zclient->t_write = NULL; | |
283 | if (zclient->sock < 0) | |
284 | return -1; | |
285 | switch (buffer_flush_available(zclient->wb, zclient->sock)) | |
286 | { | |
287 | case BUFFER_ERROR: | |
288 | zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing", | |
289 | __func__, zclient->sock); | |
290 | return zclient_failed(zclient); | |
291 | break; | |
292 | case BUFFER_PENDING: | |
4140ca4d | 293 | zclient->t_write = thread_add_write(zclient->master, zclient_flush_data, |
634f9ea2 | 294 | zclient, zclient->sock); |
295 | break; | |
296 | case BUFFER_EMPTY: | |
297 | break; | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
718e3744 | 302 | int |
634f9ea2 | 303 | zclient_send_message(struct zclient *zclient) |
304 | { | |
305 | if (zclient->sock < 0) | |
306 | return -1; | |
307 | switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf), | |
308 | stream_get_endp(zclient->obuf))) | |
309 | { | |
310 | case BUFFER_ERROR: | |
311 | zlog_warn("%s: buffer_write failed to zclient fd %d, closing", | |
312 | __func__, zclient->sock); | |
313 | return zclient_failed(zclient); | |
314 | break; | |
315 | case BUFFER_EMPTY: | |
316 | THREAD_OFF(zclient->t_write); | |
317 | break; | |
318 | case BUFFER_PENDING: | |
4140ca4d | 319 | THREAD_WRITE_ON(zclient->master, zclient->t_write, |
634f9ea2 | 320 | zclient_flush_data, zclient, zclient->sock); |
321 | break; | |
322 | } | |
323 | return 0; | |
324 | } | |
325 | ||
d211086a | 326 | void |
7076bb2f | 327 | zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id) |
c1b9800a | 328 | { |
329 | /* length placeholder, caller can update */ | |
330 | stream_putw (s, ZEBRA_HEADER_SIZE); | |
331 | stream_putc (s, ZEBRA_HEADER_MARKER); | |
332 | stream_putc (s, ZSERV_VERSION); | |
7076bb2f | 333 | stream_putw (s, vrf_id); |
c1b9800a | 334 | stream_putw (s, command); |
335 | } | |
336 | ||
55119089 ND |
337 | int |
338 | zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker, | |
e7a2870b | 339 | u_char *version, vrf_id_t *vrf_id, u_int16_t *cmd) |
55119089 ND |
340 | { |
341 | if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE) | |
342 | return -1; | |
343 | ||
344 | *size = stream_getw (s) - ZEBRA_HEADER_SIZE; | |
345 | *marker = stream_getc (s); | |
346 | *version = stream_getc (s); | |
347 | *vrf_id = stream_getw (s); | |
348 | *cmd = stream_getw (s); | |
349 | ||
350 | if (*size && stream_read (s, sock, *size) != *size) | |
351 | return -1; | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
634f9ea2 | 356 | /* Send simple Zebra message. */ |
357 | static int | |
7076bb2f | 358 | zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id) |
718e3744 | 359 | { |
360 | struct stream *s; | |
361 | ||
362 | /* Get zclient output buffer. */ | |
363 | s = zclient->obuf; | |
364 | stream_reset (s); | |
365 | ||
366 | /* Send very simple command only Zebra message. */ | |
7076bb2f | 367 | zclient_create_header (s, command, vrf_id); |
c1b9800a | 368 | |
634f9ea2 | 369 | return zclient_send_message(zclient); |
718e3744 | 370 | } |
371 | ||
2ea1ab1c VT |
372 | static int |
373 | zebra_hello_send (struct zclient *zclient) | |
374 | { | |
375 | struct stream *s; | |
376 | ||
377 | if (zclient->redist_default) | |
378 | { | |
379 | s = zclient->obuf; | |
380 | stream_reset (s); | |
381 | ||
7076bb2f FL |
382 | /* The VRF ID in the HELLO message is always 0. */ |
383 | zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT); | |
2ea1ab1c | 384 | stream_putc (s, zclient->redist_default); |
7c8ff89e | 385 | stream_putw (s, zclient->instance); |
2ea1ab1c VT |
386 | stream_putw_at (s, 0, stream_get_endp (s)); |
387 | return zclient_send_message(zclient); | |
388 | } | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
0e5223e7 | 393 | /* Send register requests to zebra daemon for the information in a VRF. */ |
7076bb2f | 394 | void |
0e5223e7 | 395 | zclient_send_reg_requests (struct zclient *zclient, vrf_id_t vrf_id) |
7076bb2f FL |
396 | { |
397 | int i; | |
398 | afi_t afi; | |
399 | ||
400 | /* zclient is disabled. */ | |
401 | if (! zclient->enable) | |
402 | return; | |
403 | ||
404 | /* If not connected to the zebra yet. */ | |
405 | if (zclient->sock < 0) | |
406 | return; | |
407 | ||
408 | if (zclient_debug) | |
0e5223e7 | 409 | zlog_debug ("%s: send register messages for VRF %u", __func__, vrf_id); |
7076bb2f FL |
410 | |
411 | /* We need router-id information. */ | |
412 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id); | |
413 | ||
414 | /* We need interface information. */ | |
415 | zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id); | |
416 | ||
417 | /* Set unwanted redistribute route. */ | |
418 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
419 | vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id); | |
420 | ||
421 | /* Flush all redistribute request. */ | |
422 | if (vrf_id == VRF_DEFAULT) | |
423 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
424 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
425 | if (zclient->mi_redist[afi][i].enabled) | |
426 | { | |
427 | struct listnode *node; | |
428 | u_short *id; | |
429 | ||
430 | for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id)) | |
431 | if (!(i == zclient->redist_default && *id == zclient->instance)) | |
432 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, | |
433 | *id, VRF_DEFAULT); | |
434 | } | |
435 | ||
436 | /* Flush all redistribute request. */ | |
437 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
438 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
439 | if (i != zclient->redist_default && | |
440 | vrf_bitmap_check (zclient->redist[afi][i], vrf_id)) | |
441 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, 0, vrf_id); | |
442 | ||
443 | /* If default information is needed. */ | |
444 | if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT)) | |
445 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id); | |
446 | } | |
447 | ||
0e5223e7 | 448 | /* Send unregister requests to zebra daemon for the information in a VRF. */ |
449 | void | |
450 | zclient_send_dereg_requests (struct zclient *zclient, vrf_id_t vrf_id) | |
451 | { | |
452 | int i; | |
453 | afi_t afi; | |
454 | ||
455 | /* zclient is disabled. */ | |
456 | if (! zclient->enable) | |
457 | return; | |
458 | ||
459 | /* If not connected to the zebra yet. */ | |
460 | if (zclient->sock < 0) | |
461 | return; | |
462 | ||
463 | if (zclient_debug) | |
464 | zlog_debug ("%s: send deregister messages for VRF %u", __func__, vrf_id); | |
465 | ||
466 | /* We need router-id information. */ | |
467 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id); | |
468 | ||
469 | /* We need interface information. */ | |
470 | zebra_message_send (zclient, ZEBRA_INTERFACE_DELETE, vrf_id); | |
471 | ||
472 | /* Set unwanted redistribute route. */ | |
473 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
474 | vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id); | |
475 | ||
476 | /* Flush all redistribute request. */ | |
477 | if (vrf_id == VRF_DEFAULT) | |
478 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
479 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
480 | if (zclient->mi_redist[afi][i].enabled) | |
481 | { | |
482 | struct listnode *node; | |
483 | u_short *id; | |
484 | ||
485 | for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id)) | |
486 | if (!(i == zclient->redist_default && *id == zclient->instance)) | |
487 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i, | |
488 | *id, VRF_DEFAULT); | |
489 | } | |
490 | ||
491 | /* Flush all redistribute request. */ | |
492 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
493 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
494 | if (i != zclient->redist_default && | |
495 | vrf_bitmap_check (zclient->redist[afi][i], vrf_id)) | |
496 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i, 0, vrf_id); | |
497 | ||
498 | /* If default information is needed. */ | |
499 | if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT)) | |
500 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, vrf_id); | |
501 | } | |
502 | ||
4a04e5f7 | 503 | /* Send request to zebra daemon to start or stop RA. */ |
504 | void | |
505 | zclient_send_interface_radv_req (struct zclient *zclient, vrf_id_t vrf_id, | |
5c81b96a | 506 | struct interface *ifp, int enable, int ra_interval) |
4a04e5f7 | 507 | { |
508 | struct stream *s; | |
509 | ||
510 | /* zclient is disabled. */ | |
511 | if (!zclient->enable) | |
512 | return; | |
513 | ||
514 | /* If not connected to the zebra yet. */ | |
515 | if (zclient->sock < 0) | |
516 | return; | |
517 | ||
518 | /* Form and send message. */ | |
519 | s = zclient->obuf; | |
520 | stream_reset (s); | |
521 | ||
522 | if (enable) | |
523 | zclient_create_header (s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id); | |
524 | else | |
525 | zclient_create_header (s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id); | |
526 | ||
527 | stream_putl (s, ifp->ifindex); | |
5c81b96a | 528 | stream_putl (s, ra_interval); |
4a04e5f7 | 529 | |
530 | stream_putw_at (s, 0, stream_get_endp (s)); | |
531 | ||
532 | zclient_send_message(zclient); | |
533 | } | |
534 | ||
718e3744 | 535 | /* Make connection to zebra daemon. */ |
536 | int | |
537 | zclient_start (struct zclient *zclient) | |
538 | { | |
7076bb2f FL |
539 | if (zclient_debug) |
540 | zlog_info ("zclient_start is called"); | |
541 | ||
542 | /* zclient is disabled. */ | |
543 | if (! zclient->enable) | |
544 | return 0; | |
718e3744 | 545 | |
718e3744 | 546 | /* If already connected to the zebra. */ |
547 | if (zclient->sock >= 0) | |
548 | return 0; | |
549 | ||
550 | /* Check connect thread. */ | |
551 | if (zclient->t_connect) | |
552 | return 0; | |
553 | ||
4ecc09d3 DS |
554 | /* |
555 | * If we fail to connect to the socket on initialization, | |
556 | * Let's wait a second and see if we can reconnect. | |
557 | * Cause if we don't connect, we never attempt to | |
558 | * reconnect. On startup if zebra is slow we | |
559 | * can get into this situation. | |
560 | */ | |
561 | while (zclient_socket_connect(zclient) < 0 && zclient->fail < 5) | |
7076bb2f FL |
562 | { |
563 | if (zclient_debug) | |
564 | zlog_debug ("zclient connection fail"); | |
565 | zclient->fail++; | |
4ecc09d3 DS |
566 | sleep (1); |
567 | } | |
568 | ||
569 | if (zclient->sock < 0) | |
570 | { | |
7076bb2f FL |
571 | zclient_event (ZCLIENT_CONNECT, zclient); |
572 | return -1; | |
573 | } | |
718e3744 | 574 | |
7076bb2f FL |
575 | if (set_nonblocking(zclient->sock) < 0) |
576 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock); | |
718e3744 | 577 | |
7076bb2f FL |
578 | /* Clear fail count. */ |
579 | zclient->fail = 0; | |
580 | if (zclient_debug) | |
581 | zlog_debug ("zclient connect success with socket [%d]", zclient->sock); | |
718e3744 | 582 | |
7076bb2f FL |
583 | /* Create read thread. */ |
584 | zclient_event (ZCLIENT_READ, zclient); | |
718e3744 | 585 | |
7076bb2f | 586 | zebra_hello_send (zclient); |
718e3744 | 587 | |
7076bb2f FL |
588 | /* Inform the successful connection. */ |
589 | if (zclient->zebra_connected) | |
590 | (*zclient->zebra_connected) (zclient); | |
718e3744 | 591 | |
7076bb2f | 592 | return 0; |
718e3744 | 593 | } |
6b0655a2 | 594 | |
078430f6 DS |
595 | /* Initialize zebra client. Argument redist_default is unwanted |
596 | redistribute route type. */ | |
597 | void | |
598 | zclient_init (struct zclient *zclient, int redist_default, u_short instance) | |
599 | { | |
600 | int afi, i; | |
601 | ||
602 | /* Enable zebra client connection by default. */ | |
603 | zclient->enable = 1; | |
604 | ||
605 | /* Set -1 to the default socket value. */ | |
606 | zclient->sock = -1; | |
607 | ||
608 | /* Clear redistribution flags. */ | |
609 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
610 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
7076bb2f | 611 | zclient->redist[afi][i] = vrf_bitmap_init(); |
078430f6 DS |
612 | |
613 | /* Set unwanted redistribute route. bgpd does not need BGP route | |
614 | redistribution. */ | |
615 | zclient->redist_default = redist_default; | |
616 | zclient->instance = instance; | |
617 | /* Pending: make afi(s) an arg. */ | |
618 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
7076bb2f | 619 | redist_add_instance (&zclient->mi_redist[afi][redist_default], instance); |
078430f6 DS |
620 | |
621 | /* Set default-information redistribute to zero. */ | |
7076bb2f | 622 | zclient->default_information = vrf_bitmap_init ();; |
078430f6 DS |
623 | |
624 | if (zclient_debug) | |
625 | zlog_debug ("zclient_start is called"); | |
626 | ||
7076bb2f FL |
627 | zclient_event (ZCLIENT_SCHEDULE, zclient); |
628 | } | |
078430f6 | 629 | |
7076bb2f FL |
630 | /* This function is a wrapper function for calling zclient_start from |
631 | timer or event thread. */ | |
632 | static int | |
633 | zclient_connect (struct thread *t) | |
634 | { | |
635 | struct zclient *zclient; | |
078430f6 | 636 | |
7076bb2f FL |
637 | zclient = THREAD_ARG (t); |
638 | zclient->t_connect = NULL; | |
078430f6 | 639 | |
078430f6 | 640 | if (zclient_debug) |
7076bb2f | 641 | zlog_debug ("zclient_connect is called"); |
078430f6 | 642 | |
7076bb2f | 643 | return zclient_start (zclient); |
078430f6 DS |
644 | } |
645 | ||
0a589359 | 646 | /* |
647 | * "xdr_encode"-like interface that allows daemon (client) to send | |
648 | * a message to zebra server for a route that needs to be | |
649 | * added/deleted to the kernel. Info about the route is specified | |
650 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes | |
651 | * the info down the zclient socket using the stream_* functions. | |
652 | * | |
653 | * The corresponding read ("xdr_decode") function on the server | |
654 | * side is zread_ipv4_add()/zread_ipv4_delete(). | |
655 | * | |
656 | * 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 | |
657 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
658 | * | Length (2) | Command | Route Type | | |
659 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
660 | * | ZEBRA Flags | Message Flags | Prefix length | | |
661 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
662 | * | Destination IPv4 Prefix for route | | |
663 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
664 | * | Nexthop count | | |
665 | * +-+-+-+-+-+-+-+-+ | |
666 | * | |
667 | * | |
668 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then | |
669 | * described, as per the Nexthop count. Each nexthop described as: | |
670 | * | |
671 | * +-+-+-+-+-+-+-+-+ | |
672 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* | |
673 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
674 | * | IPv4 Nexthop address or Interface Index number | | |
675 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
676 | * | |
677 | * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or | |
678 | * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ | |
679 | * nexthop information is provided, and the message describes a prefix | |
680 | * to blackhole or reject route. | |
681 | * | |
c8a1cb5c DS |
682 | * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*() |
683 | * infrastructure was built around the traditional (32-bit "gate OR | |
684 | * ifindex") nexthop data unit. A special encoding can be used to feed | |
685 | * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route() | |
686 | * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4 | |
687 | * fields as follows: | |
688 | * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK | |
689 | * - .nexthop_num == .ifindex_num | |
690 | * - .nexthop and .ifindex are filled with gate and ifindex parts of | |
691 | * each compound nexthop, both in the same order | |
692 | * | |
693 | * zapi_ipv4_route() will produce two nexthop data units for each such | |
694 | * interleaved 64-bit nexthop. On the zserv side of the socket it will be | |
695 | * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure. | |
696 | * | |
0a589359 | 697 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 |
698 | * byte value. | |
699 | * | |
700 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 | |
701 | * byte value. | |
702 | * | |
0d9551dc DS |
703 | * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value |
704 | * | |
0a589359 | 705 | * XXX: No attention paid to alignment. |
706 | */ | |
718e3744 | 707 | int |
0a589359 | 708 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, |
709 | struct zapi_ipv4 *api) | |
718e3744 | 710 | { |
711 | int i; | |
712 | int psize; | |
713 | struct stream *s; | |
714 | ||
715 | /* Reset stream. */ | |
716 | s = zclient->obuf; | |
717 | stream_reset (s); | |
7076bb2f FL |
718 | |
719 | zclient_create_header (s, cmd, api->vrf_id); | |
c1b9800a | 720 | |
721 | /* Put type and nexthop. */ | |
718e3744 | 722 | stream_putc (s, api->type); |
7c8ff89e | 723 | stream_putw (s, api->instance); |
718e3744 | 724 | stream_putc (s, api->flags); |
725 | stream_putc (s, api->message); | |
5a616c08 | 726 | stream_putw (s, api->safi); |
718e3744 | 727 | |
718e3744 | 728 | /* Put prefix information. */ |
729 | psize = PSIZE (p->prefixlen); | |
730 | stream_putc (s, p->prefixlen); | |
0a589359 | 731 | stream_write (s, (u_char *) & p->prefix, psize); |
718e3744 | 732 | |
733 | /* Nexthop, ifindex, distance and metric information. */ | |
d44ca835 | 734 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
c8a1cb5c DS |
735 | { |
736 | /* traditional 32-bit data units */ | |
595db7f1 | 737 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
738 | { | |
739 | stream_putc (s, 1); | |
740 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
0a589359 | 741 | /* XXX assert(api->nexthop_num == 0); */ |
742 | /* XXX assert(api->ifindex_num == 0); */ | |
595db7f1 | 743 | } |
744 | else | |
745 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
718e3744 | 746 | |
747 | for (i = 0; i < api->nexthop_num; i++) | |
595db7f1 | 748 | { |
749 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); | |
750 | stream_put_in_addr (s, api->nexthop[i]); | |
751 | } | |
718e3744 | 752 | for (i = 0; i < api->ifindex_num; i++) |
595db7f1 | 753 | { |
754 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
755 | stream_putl (s, api->ifindex[i]); | |
756 | } | |
718e3744 | 757 | } |
758 | ||
759 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
760 | stream_putc (s, api->distance); | |
761 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
762 | stream_putl (s, api->metric); | |
0d9551dc DS |
763 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) |
764 | stream_putw (s, api->tag); | |
718e3744 | 765 | |
766 | /* Put length at the first point of the stream. */ | |
767 | stream_putw_at (s, 0, stream_get_endp (s)); | |
768 | ||
634f9ea2 | 769 | return zclient_send_message(zclient); |
718e3744 | 770 | } |
771 | ||
772 | #ifdef HAVE_IPV6 | |
8a92a8a0 DS |
773 | int |
774 | zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient, | |
775 | struct prefix_ipv4 *p, struct zapi_ipv6 *api) | |
776 | { | |
777 | int i; | |
778 | int psize; | |
779 | struct stream *s; | |
780 | ||
781 | /* Reset stream. */ | |
782 | s = zclient->obuf; | |
783 | stream_reset (s); | |
784 | ||
7076bb2f | 785 | zclient_create_header (s, cmd, api->vrf_id); |
8a92a8a0 DS |
786 | |
787 | /* Put type and nexthop. */ | |
788 | stream_putc (s, api->type); | |
789 | stream_putw (s, api->instance); | |
790 | stream_putc (s, api->flags); | |
791 | stream_putc (s, api->message); | |
792 | stream_putw (s, api->safi); | |
793 | ||
794 | /* Put prefix information. */ | |
795 | psize = PSIZE (p->prefixlen); | |
796 | stream_putc (s, p->prefixlen); | |
797 | stream_write (s, (u_char *) & p->prefix, psize); | |
798 | ||
799 | /* Nexthop, ifindex, distance and metric information. */ | |
800 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) | |
801 | { | |
802 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) | |
803 | { | |
804 | stream_putc (s, 1); | |
805 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
806 | /* XXX assert(api->nexthop_num == 0); */ | |
807 | /* XXX assert(api->ifindex_num == 0); */ | |
808 | } | |
809 | else | |
810 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
811 | ||
812 | for (i = 0; i < api->nexthop_num; i++) | |
813 | { | |
814 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); | |
815 | stream_write (s, (u_char *)api->nexthop[i], 16); | |
816 | } | |
817 | for (i = 0; i < api->ifindex_num; i++) | |
818 | { | |
819 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
820 | stream_putl (s, api->ifindex[i]); | |
821 | } | |
822 | } | |
823 | ||
824 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
825 | stream_putc (s, api->distance); | |
826 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
827 | stream_putl (s, api->metric); | |
828 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) | |
829 | stream_putw (s, api->tag); | |
830 | ||
831 | /* Put length at the first point of the stream. */ | |
832 | stream_putw_at (s, 0, stream_get_endp (s)); | |
833 | ||
834 | return zclient_send_message(zclient); | |
835 | } | |
836 | ||
718e3744 | 837 | int |
0a589359 | 838 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
718e3744 | 839 | struct zapi_ipv6 *api) |
840 | { | |
841 | int i; | |
842 | int psize; | |
843 | struct stream *s; | |
844 | ||
845 | /* Reset stream. */ | |
846 | s = zclient->obuf; | |
847 | stream_reset (s); | |
848 | ||
7076bb2f | 849 | zclient_create_header (s, cmd, api->vrf_id); |
718e3744 | 850 | |
c1b9800a | 851 | /* Put type and nexthop. */ |
718e3744 | 852 | stream_putc (s, api->type); |
7c8ff89e | 853 | stream_putw (s, api->instance); |
718e3744 | 854 | stream_putc (s, api->flags); |
855 | stream_putc (s, api->message); | |
c7ec179a | 856 | stream_putw (s, api->safi); |
718e3744 | 857 | |
858 | /* Put prefix information. */ | |
859 | psize = PSIZE (p->prefixlen); | |
860 | stream_putc (s, p->prefixlen); | |
861 | stream_write (s, (u_char *)&p->prefix, psize); | |
862 | ||
863 | /* Nexthop, ifindex, distance and metric information. */ | |
864 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) | |
865 | { | |
c3c0ac83 DS |
866 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
867 | { | |
868 | stream_putc (s, 1); | |
869 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); | |
870 | /* XXX assert(api->nexthop_num == 0); */ | |
871 | /* XXX assert(api->ifindex_num == 0); */ | |
872 | } | |
873 | else | |
874 | stream_putc (s, api->nexthop_num + api->ifindex_num); | |
718e3744 | 875 | |
876 | for (i = 0; i < api->nexthop_num; i++) | |
877 | { | |
878 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); | |
879 | stream_write (s, (u_char *)api->nexthop[i], 16); | |
880 | } | |
881 | for (i = 0; i < api->ifindex_num; i++) | |
882 | { | |
883 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); | |
884 | stream_putl (s, api->ifindex[i]); | |
885 | } | |
886 | } | |
887 | ||
888 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) | |
889 | stream_putc (s, api->distance); | |
890 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) | |
891 | stream_putl (s, api->metric); | |
0d9551dc DS |
892 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG)) |
893 | stream_putw (s, api->tag); | |
718e3744 | 894 | |
895 | /* Put length at the first point of the stream. */ | |
896 | stream_putw_at (s, 0, stream_get_endp (s)); | |
897 | ||
634f9ea2 | 898 | return zclient_send_message(zclient); |
718e3744 | 899 | } |
718e3744 | 900 | #endif /* HAVE_IPV6 */ |
901 | ||
0a589359 | 902 | /* |
903 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE | |
904 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will | |
905 | * then set/unset redist[type] in the client handle (a struct zserv) for the | |
906 | * sending client | |
907 | */ | |
718e3744 | 908 | int |
8bb0831e | 909 | zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type, |
7076bb2f | 910 | u_short instance, vrf_id_t vrf_id) |
718e3744 | 911 | { |
718e3744 | 912 | struct stream *s; |
913 | ||
634f9ea2 | 914 | s = zclient->obuf; |
915 | stream_reset(s); | |
718e3744 | 916 | |
7076bb2f | 917 | zclient_create_header (s, command, vrf_id); |
8bb0831e | 918 | stream_putc (s, afi); |
718e3744 | 919 | stream_putc (s, type); |
7c8ff89e | 920 | stream_putw (s, instance); |
c1b9800a | 921 | |
922 | stream_putw_at (s, 0, stream_get_endp (s)); | |
923 | ||
634f9ea2 | 924 | return zclient_send_message(zclient); |
718e3744 | 925 | } |
926 | ||
18a6dce6 | 927 | /* Router-id update from zebra daemon. */ |
928 | void | |
929 | zebra_router_id_update_read (struct stream *s, struct prefix *rid) | |
930 | { | |
931 | int plen; | |
932 | ||
933 | /* Fetch interface address. */ | |
934 | rid->family = stream_getc (s); | |
935 | ||
936 | plen = prefix_blen (rid); | |
937 | stream_get (&rid->u.prefix, s, plen); | |
938 | rid->prefixlen = stream_getc (s); | |
939 | } | |
940 | ||
718e3744 | 941 | /* Interface addition from zebra daemon. */ |
0a589359 | 942 | /* |
943 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or | |
944 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: | |
945 | * 0 1 2 3 | |
946 | * 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 | |
947 | * +-+-+-+-+-+-+-+-+ | |
948 | * | type | | |
949 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
950 | * | ifname | | |
951 | * | | | |
952 | * | | | |
953 | * | | | |
954 | * | | | |
955 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
956 | * | ifindex | | |
957 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
958 | * | if_flags | | |
c77d4546 | 959 | * | | |
0a589359 | 960 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
961 | * | metric | | |
962 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
963 | * | ifmtu | | |
964 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
965 | * | ifmtu6 | | |
966 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
967 | * | bandwidth | | |
968 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
969 | * | sockaddr_dl | | |
970 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
971 | */ | |
972 | ||
2fcc254e | 973 | static void |
ebe32f70 | 974 | zclient_vrf_add (struct zclient *zclient, vrf_id_t vrf_id) |
1892f15e DS |
975 | { |
976 | struct vrf *vrf; | |
977 | char vrfname_tmp[VRF_NAMSIZ]; | |
978 | ||
979 | /* Read interface name. */ | |
ebe32f70 | 980 | stream_get (vrfname_tmp, zclient->ibuf, VRF_NAMSIZ); |
1892f15e DS |
981 | |
982 | /* Lookup/create vrf by vrf_id. */ | |
983 | vrf = vrf_get (vrf_id, vrfname_tmp); | |
984 | ||
2fcc254e | 985 | vrf_enable (vrf); |
1892f15e DS |
986 | } |
987 | ||
2fcc254e | 988 | static void |
ebe32f70 | 989 | zclient_vrf_delete (struct zclient *zclient, vrf_id_t vrf_id) |
1892f15e DS |
990 | { |
991 | struct vrf *vrf; | |
992 | ||
993 | /* Lookup vrf by vrf_id. */ | |
994 | vrf = vrf_lookup (vrf_id); | |
995 | ||
beef1990 DS |
996 | /* |
997 | * If a routing protocol doesn't know about a | |
998 | * vrf that is about to be deleted. There is | |
999 | * no point in attempting to delete it. | |
1000 | */ | |
1001 | if (!vrf) | |
1002 | return; | |
1003 | ||
2fcc254e | 1004 | vrf_delete (vrf); |
1892f15e DS |
1005 | } |
1006 | ||
718e3744 | 1007 | struct interface * |
7076bb2f | 1008 | zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id) |
718e3744 | 1009 | { |
1010 | struct interface *ifp; | |
02ff83c5 | 1011 | char ifname_tmp[INTERFACE_NAMSIZ]; |
718e3744 | 1012 | |
1013 | /* Read interface name. */ | |
1014 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
1015 | ||
a349198f | 1016 | /* Lookup/create interface by name. */ |
7076bb2f FL |
1017 | ifp = if_get_by_name_len_vrf (ifname_tmp, |
1018 | strnlen (ifname_tmp, INTERFACE_NAMSIZ), | |
85f9da7f | 1019 | vrf_id, 0); |
718e3744 | 1020 | |
51d4ef83 | 1021 | zebra_interface_if_set_value (s, ifp); |
718e3744 | 1022 | |
718e3744 | 1023 | return ifp; |
1024 | } | |
1025 | ||
0a589359 | 1026 | /* |
1027 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) | |
1028 | * from zebra server. The format of this message is the same as | |
1029 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see | |
1030 | * comments for zebra_interface_add_read), except that no sockaddr_dl | |
1031 | * is sent at the tail of the message. | |
1032 | */ | |
718e3744 | 1033 | struct interface * |
7076bb2f | 1034 | zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id) |
718e3744 | 1035 | { |
1036 | struct interface *ifp; | |
02ff83c5 | 1037 | char ifname_tmp[INTERFACE_NAMSIZ]; |
718e3744 | 1038 | |
1039 | /* Read interface name. */ | |
1040 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); | |
1041 | ||
1042 | /* Lookup this by interface index. */ | |
7076bb2f FL |
1043 | ifp = if_lookup_by_name_len_vrf (ifname_tmp, |
1044 | strnlen (ifname_tmp, INTERFACE_NAMSIZ), | |
1045 | vrf_id); | |
b4dd5eaa | 1046 | if (ifp == NULL) |
1047 | { | |
1048 | zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d", | |
1049 | ifname_tmp, vrf_id); | |
1050 | return NULL; | |
1051 | } | |
718e3744 | 1052 | |
51d4ef83 | 1053 | zebra_interface_if_set_value (s, ifp); |
718e3744 | 1054 | |
1055 | return ifp; | |
1056 | } | |
1057 | ||
0a589359 | 1058 | /* |
1059 | * format of message for address additon is: | |
1060 | * 0 | |
1061 | * 0 1 2 3 4 5 6 7 | |
1062 | * +-+-+-+-+-+-+-+-+ | |
1063 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or | |
1064 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE | |
1065 | * | | | |
1066 | * + + | |
1067 | * | ifindex | | |
1068 | * + + | |
1069 | * | | | |
1070 | * + + | |
1071 | * | | | |
1072 | * +-+-+-+-+-+-+-+-+ | |
1073 | * | ifc_flags | flags for connected address | |
1074 | * +-+-+-+-+-+-+-+-+ | |
1075 | * | addr_family | | |
1076 | * +-+-+-+-+-+-+-+-+ | |
1077 | * | addr... | | |
1078 | * : : | |
1079 | * | | | |
1080 | * +-+-+-+-+-+-+-+-+ | |
1081 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. | |
1082 | * +-+-+-+-+-+-+-+-+ | |
1083 | * | daddr.. | | |
1084 | * : : | |
1085 | * | | | |
1086 | * +-+-+-+-+-+-+-+-+ | |
1087 | * | |
1088 | */ | |
1089 | ||
18a6dce6 | 1090 | void |
1091 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) | |
1092 | { | |
1093 | /* Read interface's index. */ | |
1094 | ifp->ifindex = stream_getl (s); | |
508ec910 | 1095 | ifp->status = stream_getc (s); |
18a6dce6 | 1096 | |
1097 | /* Read interface's value. */ | |
c77d4546 | 1098 | ifp->flags = stream_getq (s); |
244c1cdc DS |
1099 | ifp->ptm_enable = stream_getc (s); |
1100 | ifp->ptm_status = stream_getc (s); | |
18a6dce6 | 1101 | ifp->metric = stream_getl (s); |
1102 | ifp->mtu = stream_getl (s); | |
508ec910 | 1103 | ifp->mtu6 = stream_getl (s); |
18a6dce6 | 1104 | ifp->bandwidth = stream_getl (s); |
51d4ef83 | 1105 | #ifdef HAVE_STRUCT_SOCKADDR_DL |
ca3ccd87 | 1106 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage)); |
51d4ef83 JB |
1107 | #else |
1108 | ifp->hw_addr_len = stream_getl (s); | |
1109 | if (ifp->hw_addr_len) | |
cbe0a6a1 | 1110 | stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX)); |
51d4ef83 | 1111 | #endif /* HAVE_STRUCT_SOCKADDR_DL */ |
18a6dce6 | 1112 | } |
1113 | ||
3fb9cd6e | 1114 | static int |
1115 | memconstant(const void *s, int c, size_t n) | |
1116 | { | |
1117 | const u_char *p = s; | |
1118 | ||
1119 | while (n-- > 0) | |
1120 | if (*p++ != c) | |
1121 | return 0; | |
1122 | return 1; | |
1123 | } | |
1124 | ||
d5a5c8f0 | 1125 | |
718e3744 | 1126 | struct connected * |
7076bb2f | 1127 | zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id) |
718e3744 | 1128 | { |
1129 | unsigned int ifindex; | |
1130 | struct interface *ifp; | |
1131 | struct connected *ifc; | |
0a589359 | 1132 | struct prefix p, d; |
718e3744 | 1133 | int family; |
1134 | int plen; | |
0a589359 | 1135 | u_char ifc_flags; |
718e3744 | 1136 | |
0a589359 | 1137 | memset (&p, 0, sizeof(p)); |
1138 | memset (&d, 0, sizeof(d)); | |
718e3744 | 1139 | |
1140 | /* Get interface index. */ | |
1141 | ifindex = stream_getl (s); | |
1142 | ||
1143 | /* Lookup index. */ | |
7076bb2f | 1144 | ifp = if_lookup_by_index_vrf (ifindex, vrf_id); |
718e3744 | 1145 | if (ifp == NULL) |
1146 | { | |
b4dd5eaa | 1147 | zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d", |
1148 | (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL", | |
1149 | ifindex, vrf_id); | |
718e3744 | 1150 | return NULL; |
1151 | } | |
1152 | ||
1153 | /* Fetch flag. */ | |
0a589359 | 1154 | ifc_flags = stream_getc (s); |
718e3744 | 1155 | |
1156 | /* Fetch interface address. */ | |
1157 | family = p.family = stream_getc (s); | |
1158 | ||
0a589359 | 1159 | plen = prefix_blen (&p); |
1160 | stream_get (&p.u.prefix, s, plen); | |
718e3744 | 1161 | p.prefixlen = stream_getc (s); |
1162 | ||
1163 | /* Fetch destination address. */ | |
0a589359 | 1164 | stream_get (&d.u.prefix, s, plen); |
718e3744 | 1165 | d.family = family; |
1166 | ||
0a589359 | 1167 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) |
1168 | { | |
38485402 DS |
1169 | ifc = connected_lookup_prefix_exact (ifp, &p); |
1170 | if (!ifc) | |
1171 | { | |
1172 | /* N.B. NULL destination pointers are encoded as all zeroes */ | |
1173 | ifc = connected_add_by_prefix(ifp, &p, (memconstant(&d.u.prefix,0,plen) ? | |
1174 | NULL : &d)); | |
1175 | } | |
1176 | if (ifc) | |
e4529636 AS |
1177 | { |
1178 | ifc->flags = ifc_flags; | |
1179 | if (ifc->destination) | |
1180 | ifc->destination->prefixlen = ifc->address->prefixlen; | |
90444ca3 DL |
1181 | else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) |
1182 | { | |
1183 | /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */ | |
855110bb | 1184 | char buf[PREFIX_STRLEN]; |
90444ca3 DL |
1185 | zlog_warn("warning: interface %s address %s " |
1186 | "with peer flag set, but no peer address!", | |
855110bb TT |
1187 | ifp->name, |
1188 | prefix2str (ifc->address, buf, sizeof buf)); | |
90444ca3 DL |
1189 | UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER); |
1190 | } | |
e4529636 | 1191 | } |
0a589359 | 1192 | } |
1193 | else | |
1194 | { | |
1195 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); | |
1196 | ifc = connected_delete_by_prefix(ifp, &p); | |
1197 | } | |
718e3744 | 1198 | |
1199 | return ifc; | |
1200 | } | |
0a589359 | 1201 | |
a80beece DS |
1202 | /* |
1203 | * format of message for neighbor connected address is: | |
1204 | * 0 | |
1205 | * 0 1 2 3 4 5 6 7 | |
1206 | * +-+-+-+-+-+-+-+-+ | |
1207 | * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or | |
1208 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE | |
1209 | * | | | |
1210 | * + + | |
1211 | * | ifindex | | |
1212 | * + + | |
1213 | * | | | |
1214 | * + + | |
1215 | * | | | |
1216 | * +-+-+-+-+-+-+-+-+ | |
1217 | * | addr_family | | |
1218 | * +-+-+-+-+-+-+-+-+ | |
1219 | * | addr... | | |
1220 | * : : | |
1221 | * | | | |
1222 | * +-+-+-+-+-+-+-+-+ | |
1223 | * | addr_len | len of addr. | |
1224 | * +-+-+-+-+-+-+-+-+ | |
1225 | */ | |
1226 | struct nbr_connected * | |
7076bb2f | 1227 | zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id) |
a80beece DS |
1228 | { |
1229 | unsigned int ifindex; | |
1230 | struct interface *ifp; | |
1231 | struct prefix p; | |
1232 | struct nbr_connected *ifc; | |
1233 | ||
1234 | /* Get interface index. */ | |
1235 | ifindex = stream_getl (s); | |
1236 | ||
1237 | /* Lookup index. */ | |
f1aa3df6 | 1238 | ifp = if_lookup_by_index_vrf (ifindex, vrf_id); |
a80beece DS |
1239 | if (ifp == NULL) |
1240 | { | |
f1aa3df6 | 1241 | zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d", |
1242 | (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD" : "DELETE", | |
1243 | ifindex, vrf_id); | |
a80beece DS |
1244 | return NULL; |
1245 | } | |
1246 | ||
1247 | p.family = stream_getc (s); | |
1248 | stream_get (&p.u.prefix, s, prefix_blen (&p)); | |
1249 | p.prefixlen = stream_getc (s); | |
1250 | ||
1251 | if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) | |
1252 | { | |
1253 | /* Currently only supporting P2P links, so any new RA source address is | |
1254 | considered as the replacement of the previously learnt Link-Local address. */ | |
1255 | if (!(ifc = listnode_head(ifp->nbr_connected))) | |
1256 | { | |
1257 | ifc = nbr_connected_new (); | |
1258 | ifc->address = prefix_new (); | |
1259 | ifc->ifp = ifp; | |
1260 | listnode_add (ifp->nbr_connected, ifc); | |
1261 | } | |
1262 | ||
1263 | prefix_copy(ifc->address, &p); | |
1264 | } | |
1265 | else | |
1266 | { | |
1267 | assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE); | |
1268 | ||
1269 | ifc = nbr_connected_check(ifp, &p); | |
1270 | if (ifc) | |
1271 | listnode_delete (ifp->nbr_connected, ifc); | |
1272 | } | |
1273 | ||
1274 | return ifc; | |
1275 | } | |
6b0655a2 | 1276 | |
c8e264b6 | 1277 | struct interface * |
1278 | zebra_interface_vrf_update_read (struct stream *s, vrf_id_t vrf_id, | |
1279 | vrf_id_t *new_vrf_id) | |
1280 | { | |
1281 | unsigned int ifindex; | |
1282 | struct interface *ifp; | |
1283 | vrf_id_t new_id = VRF_DEFAULT; | |
1284 | ||
1285 | /* Get interface index. */ | |
1286 | ifindex = stream_getl (s); | |
1287 | ||
1288 | /* Lookup interface. */ | |
1289 | ifp = if_lookup_by_index_vrf (ifindex, vrf_id); | |
1290 | if (ifp == NULL) | |
1291 | { | |
1292 | zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d", | |
1293 | ifindex, vrf_id); | |
1294 | return NULL; | |
1295 | } | |
1296 | ||
1297 | /* Fetch new VRF Id. */ | |
1298 | new_id = stream_getw (s); | |
1299 | ||
1300 | *new_vrf_id = new_id; | |
1301 | return ifp; | |
1302 | } | |
1303 | ||
718e3744 | 1304 | /* Zebra client message read function. */ |
634f9ea2 | 1305 | static int |
718e3744 | 1306 | zclient_read (struct thread *thread) |
1307 | { | |
634f9ea2 | 1308 | size_t already; |
c1b9800a | 1309 | uint16_t length, command; |
1310 | uint8_t marker, version; | |
7076bb2f | 1311 | vrf_id_t vrf_id; |
718e3744 | 1312 | struct zclient *zclient; |
1313 | ||
1314 | /* Get socket to zebra. */ | |
718e3744 | 1315 | zclient = THREAD_ARG (thread); |
1316 | zclient->t_read = NULL; | |
1317 | ||
634f9ea2 | 1318 | /* Read zebra header (if we don't have it already). */ |
1319 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) | |
718e3744 | 1320 | { |
634f9ea2 | 1321 | ssize_t nbyte; |
1322 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
1323 | ZEBRA_HEADER_SIZE-already)) == 0) || | |
1324 | (nbyte == -1)) | |
1325 | { | |
1326 | if (zclient_debug) | |
1327 | zlog_debug ("zclient connection closed socket [%d].", zclient->sock); | |
1328 | return zclient_failed(zclient); | |
1329 | } | |
1330 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) | |
1331 | { | |
1332 | /* Try again later. */ | |
1333 | zclient_event (ZCLIENT_READ, zclient); | |
1334 | return 0; | |
1335 | } | |
1336 | already = ZEBRA_HEADER_SIZE; | |
718e3744 | 1337 | } |
1338 | ||
634f9ea2 | 1339 | /* Reset to read from the beginning of the incoming packet. */ |
1340 | stream_set_getp(zclient->ibuf, 0); | |
718e3744 | 1341 | |
c1b9800a | 1342 | /* Fetch header values. */ |
718e3744 | 1343 | length = stream_getw (zclient->ibuf); |
c1b9800a | 1344 | marker = stream_getc (zclient->ibuf); |
1345 | version = stream_getc (zclient->ibuf); | |
7076bb2f | 1346 | vrf_id = stream_getw (zclient->ibuf); |
c1b9800a | 1347 | command = stream_getw (zclient->ibuf); |
1348 | ||
1349 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) | |
1350 | { | |
1351 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
1352 | __func__, zclient->sock, marker, version); | |
1353 | return zclient_failed(zclient); | |
1354 | } | |
1355 | ||
634f9ea2 | 1356 | if (length < ZEBRA_HEADER_SIZE) |
1357 | { | |
1358 | zlog_err("%s: socket %d message length %u is less than %d ", | |
1359 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); | |
1360 | return zclient_failed(zclient); | |
1361 | } | |
1362 | ||
718e3744 | 1363 | /* Length check. */ |
634f9ea2 | 1364 | if (length > STREAM_SIZE(zclient->ibuf)) |
718e3744 | 1365 | { |
634f9ea2 | 1366 | struct stream *ns; |
1367 | zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...", | |
1368 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); | |
1369 | ns = stream_new(length); | |
1370 | stream_copy(ns, zclient->ibuf); | |
718e3744 | 1371 | stream_free (zclient->ibuf); |
634f9ea2 | 1372 | zclient->ibuf = ns; |
718e3744 | 1373 | } |
718e3744 | 1374 | |
1375 | /* Read rest of zebra packet. */ | |
634f9ea2 | 1376 | if (already < length) |
1377 | { | |
1378 | ssize_t nbyte; | |
1379 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
1380 | length-already)) == 0) || | |
1381 | (nbyte == -1)) | |
1382 | { | |
1383 | if (zclient_debug) | |
1384 | zlog_debug("zclient connection closed socket [%d].", zclient->sock); | |
1385 | return zclient_failed(zclient); | |
1386 | } | |
1387 | if (nbyte != (ssize_t)(length-already)) | |
1388 | { | |
1389 | /* Try again later. */ | |
1390 | zclient_event (ZCLIENT_READ, zclient); | |
1391 | return 0; | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | length -= ZEBRA_HEADER_SIZE; | |
718e3744 | 1396 | |
0a589359 | 1397 | if (zclient_debug) |
7076bb2f | 1398 | zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id); |
0a589359 | 1399 | |
718e3744 | 1400 | switch (command) |
1401 | { | |
18a6dce6 | 1402 | case ZEBRA_ROUTER_ID_UPDATE: |
1403 | if (zclient->router_id_update) | |
7076bb2f | 1404 | (*zclient->router_id_update) (command, zclient, length, vrf_id); |
18a6dce6 | 1405 | break; |
1892f15e | 1406 | case ZEBRA_VRF_ADD: |
ebe32f70 | 1407 | zclient_vrf_add (zclient, vrf_id); |
1892f15e DS |
1408 | break; |
1409 | case ZEBRA_VRF_DELETE: | |
ebe32f70 | 1410 | zclient_vrf_delete (zclient, vrf_id); |
1892f15e | 1411 | break; |
718e3744 | 1412 | case ZEBRA_INTERFACE_ADD: |
1413 | if (zclient->interface_add) | |
7076bb2f | 1414 | (*zclient->interface_add) (command, zclient, length, vrf_id); |
718e3744 | 1415 | break; |
1416 | case ZEBRA_INTERFACE_DELETE: | |
1417 | if (zclient->interface_delete) | |
7076bb2f | 1418 | (*zclient->interface_delete) (command, zclient, length, vrf_id); |
718e3744 | 1419 | break; |
1420 | case ZEBRA_INTERFACE_ADDRESS_ADD: | |
1421 | if (zclient->interface_address_add) | |
7076bb2f | 1422 | (*zclient->interface_address_add) (command, zclient, length, vrf_id); |
718e3744 | 1423 | break; |
1424 | case ZEBRA_INTERFACE_ADDRESS_DELETE: | |
1425 | if (zclient->interface_address_delete) | |
7076bb2f | 1426 | (*zclient->interface_address_delete) (command, zclient, length, vrf_id); |
718e3744 | 1427 | break; |
68fe91d6 | 1428 | case ZEBRA_INTERFACE_BFD_DEST_UPDATE: |
1429 | if (zclient->interface_bfd_dest_update) | |
7076bb2f | 1430 | (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id); |
d5a5c8f0 | 1431 | break; |
a80beece DS |
1432 | case ZEBRA_INTERFACE_NBR_ADDRESS_ADD: |
1433 | if (zclient->interface_nbr_address_add) | |
7076bb2f | 1434 | (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id); |
a80beece DS |
1435 | break; |
1436 | case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE: | |
1437 | if (zclient->interface_nbr_address_delete) | |
7076bb2f | 1438 | (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id); |
a80beece | 1439 | break; |
718e3744 | 1440 | case ZEBRA_INTERFACE_UP: |
1441 | if (zclient->interface_up) | |
7076bb2f | 1442 | (*zclient->interface_up) (command, zclient, length, vrf_id); |
718e3744 | 1443 | break; |
1444 | case ZEBRA_INTERFACE_DOWN: | |
1445 | if (zclient->interface_down) | |
7076bb2f | 1446 | (*zclient->interface_down) (command, zclient, length, vrf_id); |
c8e264b6 | 1447 | break; |
1448 | case ZEBRA_INTERFACE_VRF_UPDATE: | |
1449 | if (zclient->interface_vrf_update) | |
1450 | (*zclient->interface_vrf_update) (command, zclient, length, vrf_id); | |
718e3744 | 1451 | break; |
1452 | case ZEBRA_IPV4_ROUTE_ADD: | |
1453 | if (zclient->ipv4_route_add) | |
7076bb2f | 1454 | (*zclient->ipv4_route_add) (command, zclient, length, vrf_id); |
718e3744 | 1455 | break; |
1456 | case ZEBRA_IPV4_ROUTE_DELETE: | |
1457 | if (zclient->ipv4_route_delete) | |
7076bb2f | 1458 | (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id); |
718e3744 | 1459 | break; |
1460 | case ZEBRA_IPV6_ROUTE_ADD: | |
1461 | if (zclient->ipv6_route_add) | |
7076bb2f | 1462 | (*zclient->ipv6_route_add) (command, zclient, length, vrf_id); |
718e3744 | 1463 | break; |
1464 | case ZEBRA_IPV6_ROUTE_DELETE: | |
1465 | if (zclient->ipv6_route_delete) | |
7076bb2f | 1466 | (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id); |
718e3744 | 1467 | break; |
fb018d25 DS |
1468 | case ZEBRA_NEXTHOP_UPDATE: |
1469 | if (zclient_debug) | |
1470 | zlog_debug("zclient rcvd nexthop update\n"); | |
1471 | if (zclient->nexthop_update) | |
7076bb2f | 1472 | (*zclient->nexthop_update) (command, zclient, length, vrf_id); |
fb018d25 | 1473 | break; |
078430f6 DS |
1474 | case ZEBRA_IMPORT_CHECK_UPDATE: |
1475 | if (zclient_debug) | |
1476 | zlog_debug("zclient rcvd import check update\n"); | |
1477 | if (zclient->import_check_update) | |
7076bb2f | 1478 | (*zclient->import_check_update) (command, zclient, length, vrf_id); |
078430f6 | 1479 | break; |
c43ed2e4 DS |
1480 | case ZEBRA_BFD_DEST_REPLAY: |
1481 | if (zclient->bfd_dest_replay) | |
7076bb2f | 1482 | (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id); |
c43ed2e4 | 1483 | break; |
5048fe14 | 1484 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: |
1485 | if (zclient->redistribute_route_ipv4_add) | |
7076bb2f | 1486 | (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id); |
5048fe14 | 1487 | break; |
1488 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: | |
1489 | if (zclient->redistribute_route_ipv4_del) | |
7076bb2f | 1490 | (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id); |
5048fe14 | 1491 | break; |
1492 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: | |
1493 | if (zclient->redistribute_route_ipv6_add) | |
7076bb2f | 1494 | (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id); |
5048fe14 | 1495 | break; |
1496 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: | |
1497 | if (zclient->redistribute_route_ipv6_del) | |
7076bb2f | 1498 | (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id); |
5048fe14 | 1499 | break; |
718e3744 | 1500 | default: |
1501 | break; | |
1502 | } | |
1503 | ||
634f9ea2 | 1504 | if (zclient->sock < 0) |
1505 | /* Connection was closed during packet processing. */ | |
1506 | return -1; | |
1507 | ||
718e3744 | 1508 | /* Register read thread. */ |
634f9ea2 | 1509 | stream_reset(zclient->ibuf); |
718e3744 | 1510 | zclient_event (ZCLIENT_READ, zclient); |
1511 | ||
1512 | return 0; | |
1513 | } | |
1514 | ||
1515 | void | |
8bb0831e | 1516 | zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type, |
7076bb2f | 1517 | u_short instance, vrf_id_t vrf_id) |
718e3744 | 1518 | { |
718e3744 | 1519 | |
7076bb2f FL |
1520 | if (instance) { |
1521 | if (command == ZEBRA_REDISTRIBUTE_ADD) | |
1522 | { | |
1523 | if (redist_check_instance(&zclient->mi_redist[afi][type], instance)) | |
1524 | return; | |
1525 | redist_add_instance(&zclient->mi_redist[afi][type], instance); | |
1526 | } | |
1527 | else | |
1528 | { | |
1529 | if (!redist_check_instance(&zclient->mi_redist[afi][type], instance)) | |
1530 | return; | |
1531 | redist_del_instance(&zclient->mi_redist[afi][type], instance); | |
1532 | } | |
1533 | ||
1534 | } else { | |
1535 | if (command == ZEBRA_REDISTRIBUTE_ADD) | |
1536 | { | |
1537 | if (vrf_bitmap_check (zclient->redist[afi][type], vrf_id)) | |
1538 | return; | |
1539 | vrf_bitmap_set (zclient->redist[afi][type], vrf_id); | |
1540 | } | |
1541 | else | |
1542 | { | |
1543 | if (!vrf_bitmap_check (zclient->redist[afi][type], vrf_id)) | |
1544 | return; | |
1545 | vrf_bitmap_unset (zclient->redist[afi][type], vrf_id); | |
1546 | } | |
1547 | } | |
718e3744 | 1548 | |
1549 | if (zclient->sock > 0) | |
7076bb2f | 1550 | zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id); |
718e3744 | 1551 | } |
1552 | ||
718e3744 | 1553 | |
1554 | void | |
7076bb2f FL |
1555 | zclient_redistribute_default (int command, struct zclient *zclient, |
1556 | vrf_id_t vrf_id) | |
718e3744 | 1557 | { |
718e3744 | 1558 | |
0a589359 | 1559 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) |
1560 | { | |
7076bb2f | 1561 | if (vrf_bitmap_check (zclient->default_information, vrf_id)) |
0a589359 | 1562 | return; |
7076bb2f | 1563 | vrf_bitmap_set (zclient->default_information, vrf_id); |
0a589359 | 1564 | } |
1565 | else | |
1566 | { | |
7076bb2f | 1567 | if (!vrf_bitmap_check (zclient->default_information, vrf_id)) |
0a589359 | 1568 | return; |
7076bb2f | 1569 | vrf_bitmap_unset (zclient->default_information, vrf_id); |
0a589359 | 1570 | } |
718e3744 | 1571 | |
1572 | if (zclient->sock > 0) | |
7076bb2f | 1573 | zebra_message_send (zclient, command, vrf_id); |
718e3744 | 1574 | } |
1575 | ||
718e3744 | 1576 | static void |
1577 | zclient_event (enum event event, struct zclient *zclient) | |
1578 | { | |
1579 | switch (event) | |
1580 | { | |
1581 | case ZCLIENT_SCHEDULE: | |
1582 | if (! zclient->t_connect) | |
1583 | zclient->t_connect = | |
4140ca4d | 1584 | thread_add_event (zclient->master, zclient_connect, zclient, 0); |
718e3744 | 1585 | break; |
1586 | case ZCLIENT_CONNECT: | |
1587 | if (zclient->fail >= 10) | |
1588 | return; | |
1589 | if (zclient_debug) | |
8ddca704 | 1590 | zlog_debug ("zclient connect schedule interval is %d", |
718e3744 | 1591 | zclient->fail < 3 ? 10 : 60); |
1592 | if (! zclient->t_connect) | |
1593 | zclient->t_connect = | |
4140ca4d | 1594 | thread_add_timer (zclient->master, zclient_connect, zclient, |
718e3744 | 1595 | zclient->fail < 3 ? 10 : 60); |
1596 | break; | |
1597 | case ZCLIENT_READ: | |
1598 | zclient->t_read = | |
4140ca4d | 1599 | thread_add_read (zclient->master, zclient_read, zclient, zclient->sock); |
718e3744 | 1600 | break; |
1601 | } | |
1602 | } | |
b5114685 | 1603 | |
744f4685 | 1604 | const char *zclient_serv_path_get() |
12e41d03 DL |
1605 | { |
1606 | return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH; | |
1607 | } | |
1608 | ||
b5114685 VT |
1609 | void |
1610 | zclient_serv_path_set (char *path) | |
1611 | { | |
1612 | struct stat sb; | |
1613 | ||
1614 | /* reset */ | |
1615 | zclient_serv_path = NULL; | |
1616 | ||
1617 | /* test if `path' is socket. don't set it otherwise. */ | |
1618 | if (stat(path, &sb) == -1) | |
1619 | { | |
1620 | zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path); | |
1621 | return; | |
1622 | } | |
1623 | ||
1624 | if ((sb.st_mode & S_IFMT) != S_IFSOCK) | |
1625 | { | |
1626 | zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path); | |
1627 | return; | |
1628 | } | |
1629 | ||
1630 | /* it seems that path is unix socket */ | |
1631 | zclient_serv_path = path; | |
1632 | } | |
1633 |