]>
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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "stream.h" | |
634f9ea2 | 26 | #include "buffer.h" |
718e3744 | 27 | #include "network.h" |
7922fc65 DS |
28 | #include "vrf.h" |
29 | #include "vrf_int.h" | |
718e3744 | 30 | #include "if.h" |
31 | #include "log.h" | |
32 | #include "thread.h" | |
33 | #include "zclient.h" | |
34 | #include "memory.h" | |
35 | #include "table.h" | |
5b30316e | 36 | #include "nexthop.h" |
fea12efb | 37 | #include "mpls.h" |
342213ea | 38 | #include "sockopt.h" |
6b0655a2 | 39 | |
4a1ab8e4 | 40 | DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient") |
14878121 | 41 | DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs") |
4a1ab8e4 | 42 | |
718e3744 | 43 | /* Zebra client events. */ |
d62a17ae | 44 | enum event { ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT }; |
718e3744 | 45 | |
46 | /* Prototype for event manager. */ | |
d62a17ae | 47 | static void zclient_event(enum event, struct zclient *); |
718e3744 | 48 | |
689f5a8c DL |
49 | struct sockaddr_storage zclient_addr; |
50 | socklen_t zclient_addr_len; | |
b5114685 | 51 | |
718e3744 | 52 | /* This file local debug flag. */ |
53 | int zclient_debug = 0; | |
6b0655a2 | 54 | |
e1a1880d DS |
55 | struct zclient_options zclient_options_default = { .receive_notify = false }; |
56 | ||
718e3744 | 57 | /* Allocate zclient structure. */ |
e1a1880d DS |
58 | struct zclient *zclient_new_notify(struct thread_master *master, |
59 | struct zclient_options *opt) | |
718e3744 | 60 | { |
d62a17ae | 61 | struct zclient *zclient; |
62 | zclient = XCALLOC(MTYPE_ZCLIENT, sizeof(struct zclient)); | |
718e3744 | 63 | |
d62a17ae | 64 | zclient->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
65 | zclient->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
66 | zclient->wb = buffer_new(0); | |
67 | zclient->master = master; | |
718e3744 | 68 | |
e1a1880d DS |
69 | zclient->receive_notify = opt->receive_notify; |
70 | ||
d62a17ae | 71 | return zclient; |
718e3744 | 72 | } |
73 | ||
228da428 | 74 | /* This function is only called when exiting, because |
634f9ea2 | 75 | many parts of the code do not check for I/O errors, so they could |
76 | reference an invalid pointer if the structure was ever freed. | |
634f9ea2 | 77 | |
228da428 | 78 | Free zclient structure. */ |
d62a17ae | 79 | void zclient_free(struct zclient *zclient) |
718e3744 | 80 | { |
d62a17ae | 81 | if (zclient->ibuf) |
82 | stream_free(zclient->ibuf); | |
83 | if (zclient->obuf) | |
84 | stream_free(zclient->obuf); | |
85 | if (zclient->wb) | |
86 | buffer_free(zclient->wb); | |
87 | ||
88 | XFREE(MTYPE_ZCLIENT, zclient); | |
718e3744 | 89 | } |
90 | ||
d62a17ae | 91 | u_short *redist_check_instance(struct redist_proto *red, u_short instance) |
7c8ff89e | 92 | { |
d62a17ae | 93 | struct listnode *node; |
94 | u_short *id; | |
7c8ff89e | 95 | |
d62a17ae | 96 | if (!red->instances) |
97 | return NULL; | |
7c8ff89e | 98 | |
d62a17ae | 99 | for (ALL_LIST_ELEMENTS_RO(red->instances, node, id)) |
100 | if (*id == instance) | |
101 | return id; | |
7c8ff89e | 102 | |
d62a17ae | 103 | return NULL; |
7c8ff89e DS |
104 | } |
105 | ||
d62a17ae | 106 | void redist_add_instance(struct redist_proto *red, u_short instance) |
7c8ff89e | 107 | { |
d62a17ae | 108 | u_short *in; |
7c8ff89e | 109 | |
d62a17ae | 110 | red->enabled = 1; |
7c8ff89e | 111 | |
d62a17ae | 112 | if (!red->instances) |
113 | red->instances = list_new(); | |
7c8ff89e | 114 | |
d62a17ae | 115 | in = XMALLOC(MTYPE_REDIST_INST, sizeof(u_short)); |
116 | *in = instance; | |
117 | listnode_add(red->instances, in); | |
7c8ff89e DS |
118 | } |
119 | ||
d62a17ae | 120 | void redist_del_instance(struct redist_proto *red, u_short instance) |
7c8ff89e | 121 | { |
d62a17ae | 122 | u_short *id; |
123 | ||
124 | id = redist_check_instance(red, instance); | |
125 | if (!id) | |
126 | return; | |
127 | ||
128 | listnode_delete(red->instances, id); | |
129 | XFREE(MTYPE_REDIST_INST, id); | |
130 | if (!red->instances->count) { | |
131 | red->enabled = 0; | |
acdf5e25 | 132 | list_delete_and_null(&red->instances); |
d62a17ae | 133 | } |
7c8ff89e DS |
134 | } |
135 | ||
718e3744 | 136 | /* Stop zebra client services. */ |
d62a17ae | 137 | void zclient_stop(struct zclient *zclient) |
718e3744 | 138 | { |
d62a17ae | 139 | afi_t afi; |
140 | int i; | |
141 | ||
142 | if (zclient_debug) | |
143 | zlog_debug("zclient stopped"); | |
144 | ||
145 | /* Stop threads. */ | |
146 | THREAD_OFF(zclient->t_read); | |
147 | THREAD_OFF(zclient->t_connect); | |
148 | THREAD_OFF(zclient->t_write); | |
149 | ||
150 | /* Reset streams. */ | |
151 | stream_reset(zclient->ibuf); | |
152 | stream_reset(zclient->obuf); | |
153 | ||
154 | /* Empty the write buffer. */ | |
155 | buffer_reset(zclient->wb); | |
156 | ||
157 | /* Close socket. */ | |
158 | if (zclient->sock >= 0) { | |
159 | close(zclient->sock); | |
160 | zclient->sock = -1; | |
161 | } | |
162 | zclient->fail = 0; | |
163 | ||
164 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
165 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
166 | vrf_bitmap_free(zclient->redist[afi][i]); | |
167 | zclient->redist[afi][i] = VRF_BITMAP_NULL; | |
168 | } | |
169 | redist_del_instance( | |
170 | &zclient->mi_redist[afi][zclient->redist_default], | |
171 | zclient->instance); | |
41246cb6 | 172 | } |
41246cb6 | 173 | |
d62a17ae | 174 | vrf_bitmap_free(zclient->default_information); |
175 | zclient->default_information = VRF_BITMAP_NULL; | |
718e3744 | 176 | } |
177 | ||
d62a17ae | 178 | void zclient_reset(struct zclient *zclient) |
718e3744 | 179 | { |
d62a17ae | 180 | afi_t afi; |
3d68677e | 181 | |
d62a17ae | 182 | zclient_stop(zclient); |
3d68677e | 183 | |
d62a17ae | 184 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
185 | redist_del_instance( | |
186 | &zclient->mi_redist[afi][zclient->redist_default], | |
187 | zclient->instance); | |
3d68677e | 188 | |
342213ea DS |
189 | zclient_init(zclient, zclient->redist_default, |
190 | zclient->instance, zclient->privs); | |
718e3744 | 191 | } |
192 | ||
689f5a8c DL |
193 | /** |
194 | * Connect to zebra daemon. | |
195 | * @param zclient a pointer to zclient structure | |
196 | * @return socket fd just to make sure that connection established | |
197 | * @see zclient_init | |
e1a1880d | 198 | * @see zclient_new_notify |
689f5a8c DL |
199 | */ |
200 | int zclient_socket_connect(struct zclient *zclient) | |
718e3744 | 201 | { |
d62a17ae | 202 | int sock; |
203 | int ret; | |
d62a17ae | 204 | |
205 | /* We should think about IPv6 connection. */ | |
689f5a8c | 206 | sock = socket(zclient_addr.ss_family, SOCK_STREAM, 0); |
d62a17ae | 207 | if (sock < 0) |
208 | return -1; | |
209 | ||
689f5a8c | 210 | set_cloexec(sock); |
d62a17ae | 211 | |
342213ea DS |
212 | zclient->privs->change(ZPRIVS_RAISE); |
213 | setsockopt_so_sendbuf(sock, 1048576); | |
214 | zclient->privs->change(ZPRIVS_LOWER); | |
215 | ||
d62a17ae | 216 | /* Connect to zebra. */ |
689f5a8c DL |
217 | ret = connect(sock, (struct sockaddr *)&zclient_addr, |
218 | zclient_addr_len); | |
d62a17ae | 219 | if (ret < 0) { |
220 | if (zclient_debug) | |
221 | zlog_warn("%s connect failure: %d(%s)", | |
222 | __PRETTY_FUNCTION__, errno, | |
223 | safe_strerror(errno)); | |
224 | close(sock); | |
225 | return -1; | |
226 | } | |
718e3744 | 227 | |
689f5a8c | 228 | zclient->sock = sock; |
d62a17ae | 229 | return sock; |
718e3744 | 230 | } |
231 | ||
d62a17ae | 232 | static int zclient_failed(struct zclient *zclient) |
634f9ea2 | 233 | { |
d62a17ae | 234 | zclient->fail++; |
235 | zclient_stop(zclient); | |
236 | zclient_event(ZCLIENT_CONNECT, zclient); | |
237 | return -1; | |
634f9ea2 | 238 | } |
239 | ||
d62a17ae | 240 | static int zclient_flush_data(struct thread *thread) |
634f9ea2 | 241 | { |
d62a17ae | 242 | struct zclient *zclient = THREAD_ARG(thread); |
243 | ||
244 | zclient->t_write = NULL; | |
245 | if (zclient->sock < 0) | |
246 | return -1; | |
247 | switch (buffer_flush_available(zclient->wb, zclient->sock)) { | |
248 | case BUFFER_ERROR: | |
249 | zlog_warn( | |
250 | "%s: buffer_flush_available failed on zclient fd %d, closing", | |
251 | __func__, zclient->sock); | |
252 | return zclient_failed(zclient); | |
253 | break; | |
254 | case BUFFER_PENDING: | |
255 | zclient->t_write = NULL; | |
256 | thread_add_write(zclient->master, zclient_flush_data, zclient, | |
257 | zclient->sock, &zclient->t_write); | |
258 | break; | |
259 | case BUFFER_EMPTY: | |
260 | break; | |
261 | } | |
262 | return 0; | |
634f9ea2 | 263 | } |
264 | ||
d62a17ae | 265 | int zclient_send_message(struct zclient *zclient) |
634f9ea2 | 266 | { |
d62a17ae | 267 | if (zclient->sock < 0) |
268 | return -1; | |
269 | switch (buffer_write(zclient->wb, zclient->sock, | |
270 | STREAM_DATA(zclient->obuf), | |
271 | stream_get_endp(zclient->obuf))) { | |
272 | case BUFFER_ERROR: | |
273 | zlog_warn("%s: buffer_write failed to zclient fd %d, closing", | |
274 | __func__, zclient->sock); | |
275 | return zclient_failed(zclient); | |
276 | break; | |
277 | case BUFFER_EMPTY: | |
278 | THREAD_OFF(zclient->t_write); | |
279 | break; | |
280 | case BUFFER_PENDING: | |
281 | thread_add_write(zclient->master, zclient_flush_data, zclient, | |
282 | zclient->sock, &zclient->t_write); | |
283 | break; | |
284 | } | |
285 | return 0; | |
634f9ea2 | 286 | } |
287 | ||
d62a17ae | 288 | void zclient_create_header(struct stream *s, uint16_t command, vrf_id_t vrf_id) |
c1b9800a | 289 | { |
d62a17ae | 290 | /* length placeholder, caller can update */ |
291 | stream_putw(s, ZEBRA_HEADER_SIZE); | |
292 | stream_putc(s, ZEBRA_HEADER_MARKER); | |
293 | stream_putc(s, ZSERV_VERSION); | |
a9ff90c4 | 294 | stream_putl(s, vrf_id); |
d62a17ae | 295 | stream_putw(s, command); |
c1b9800a | 296 | } |
297 | ||
d62a17ae | 298 | int zclient_read_header(struct stream *s, int sock, u_int16_t *size, |
299 | u_char *marker, u_char *version, vrf_id_t *vrf_id, | |
300 | u_int16_t *cmd) | |
55119089 | 301 | { |
d62a17ae | 302 | if (stream_read(s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE) |
303 | return -1; | |
304 | ||
ec93aa12 DS |
305 | STREAM_GETW(s, *size); |
306 | *size -= ZEBRA_HEADER_SIZE; | |
307 | STREAM_GETC(s, *marker); | |
308 | STREAM_GETC(s, *version); | |
a9ff90c4 | 309 | STREAM_GETL(s, *vrf_id); |
ec93aa12 | 310 | STREAM_GETW(s, *cmd); |
d62a17ae | 311 | |
312 | if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER) { | |
313 | zlog_err( | |
314 | "%s: socket %d version mismatch, marker %d, version %d", | |
315 | __func__, sock, *marker, *version); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | if (*size && stream_read(s, sock, *size) != *size) | |
320 | return -1; | |
321 | ||
ec93aa12 | 322 | stream_failure: |
d62a17ae | 323 | return 0; |
55119089 ND |
324 | } |
325 | ||
634f9ea2 | 326 | /* Send simple Zebra message. */ |
d62a17ae | 327 | static int zebra_message_send(struct zclient *zclient, int command, |
328 | vrf_id_t vrf_id) | |
718e3744 | 329 | { |
d62a17ae | 330 | struct stream *s; |
718e3744 | 331 | |
d62a17ae | 332 | /* Get zclient output buffer. */ |
333 | s = zclient->obuf; | |
334 | stream_reset(s); | |
718e3744 | 335 | |
d62a17ae | 336 | /* Send very simple command only Zebra message. */ |
337 | zclient_create_header(s, command, vrf_id); | |
338 | ||
339 | return zclient_send_message(zclient); | |
718e3744 | 340 | } |
341 | ||
d62a17ae | 342 | static int zebra_hello_send(struct zclient *zclient) |
2ea1ab1c | 343 | { |
d62a17ae | 344 | struct stream *s; |
345 | ||
346 | if (zclient->redist_default) { | |
347 | s = zclient->obuf; | |
348 | stream_reset(s); | |
349 | ||
350 | /* The VRF ID in the HELLO message is always 0. */ | |
351 | zclient_create_header(s, ZEBRA_HELLO, VRF_DEFAULT); | |
352 | stream_putc(s, zclient->redist_default); | |
353 | stream_putw(s, zclient->instance); | |
e1a1880d DS |
354 | if (zclient->receive_notify) |
355 | stream_putc(s, 1); | |
356 | else | |
357 | stream_putc(s, 0); | |
358 | ||
d62a17ae | 359 | stream_putw_at(s, 0, stream_get_endp(s)); |
360 | return zclient_send_message(zclient); | |
361 | } | |
362 | ||
363 | return 0; | |
2ea1ab1c VT |
364 | } |
365 | ||
0e5223e7 | 366 | /* Send register requests to zebra daemon for the information in a VRF. */ |
d62a17ae | 367 | void zclient_send_reg_requests(struct zclient *zclient, vrf_id_t vrf_id) |
7076bb2f | 368 | { |
d62a17ae | 369 | int i; |
370 | afi_t afi; | |
371 | ||
d62a17ae | 372 | /* If not connected to the zebra yet. */ |
373 | if (zclient->sock < 0) | |
374 | return; | |
375 | ||
376 | if (zclient_debug) | |
377 | zlog_debug("%s: send register messages for VRF %u", __func__, | |
378 | vrf_id); | |
379 | ||
380 | /* We need router-id information. */ | |
381 | zebra_message_send(zclient, ZEBRA_ROUTER_ID_ADD, vrf_id); | |
382 | ||
383 | /* We need interface information. */ | |
384 | zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, vrf_id); | |
385 | ||
386 | /* Set unwanted redistribute route. */ | |
387 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
388 | vrf_bitmap_set(zclient->redist[afi][zclient->redist_default], | |
389 | vrf_id); | |
390 | ||
391 | /* Flush all redistribute request. */ | |
392 | if (vrf_id == VRF_DEFAULT) | |
393 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
394 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
395 | if (zclient->mi_redist[afi][i].enabled) { | |
396 | struct listnode *node; | |
397 | u_short *id; | |
398 | ||
399 | for (ALL_LIST_ELEMENTS_RO( | |
400 | zclient->mi_redist[afi][i] | |
401 | .instances, | |
402 | node, id)) | |
403 | if (!(i == zclient->redist_default | |
404 | && *id == zclient->instance)) | |
405 | zebra_redistribute_send( | |
406 | ZEBRA_REDISTRIBUTE_ADD, | |
407 | zclient, afi, i, | |
408 | *id, | |
409 | VRF_DEFAULT); | |
410 | } | |
411 | ||
412 | /* Flush all redistribute request. */ | |
413 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
414 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
415 | if (i != zclient->redist_default | |
416 | && vrf_bitmap_check(zclient->redist[afi][i], | |
417 | vrf_id)) | |
418 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, | |
419 | zclient, afi, i, 0, | |
420 | vrf_id); | |
421 | ||
422 | /* If default information is needed. */ | |
423 | if (vrf_bitmap_check(zclient->default_information, VRF_DEFAULT)) | |
424 | zebra_message_send(zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, | |
425 | vrf_id); | |
7076bb2f FL |
426 | } |
427 | ||
0e5223e7 | 428 | /* Send unregister requests to zebra daemon for the information in a VRF. */ |
d62a17ae | 429 | void zclient_send_dereg_requests(struct zclient *zclient, vrf_id_t vrf_id) |
0e5223e7 | 430 | { |
d62a17ae | 431 | int i; |
432 | afi_t afi; | |
433 | ||
d62a17ae | 434 | /* If not connected to the zebra yet. */ |
435 | if (zclient->sock < 0) | |
436 | return; | |
437 | ||
438 | if (zclient_debug) | |
439 | zlog_debug("%s: send deregister messages for VRF %u", __func__, | |
440 | vrf_id); | |
441 | ||
442 | /* We need router-id information. */ | |
443 | zebra_message_send(zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id); | |
444 | ||
445 | /* We need interface information. */ | |
446 | zebra_message_send(zclient, ZEBRA_INTERFACE_DELETE, vrf_id); | |
447 | ||
448 | /* Set unwanted redistribute route. */ | |
449 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
450 | vrf_bitmap_set(zclient->redist[afi][zclient->redist_default], | |
451 | vrf_id); | |
452 | ||
453 | /* Flush all redistribute request. */ | |
454 | if (vrf_id == VRF_DEFAULT) | |
455 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
456 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
457 | if (zclient->mi_redist[afi][i].enabled) { | |
458 | struct listnode *node; | |
459 | u_short *id; | |
460 | ||
461 | for (ALL_LIST_ELEMENTS_RO( | |
462 | zclient->mi_redist[afi][i] | |
463 | .instances, | |
464 | node, id)) | |
465 | if (!(i == zclient->redist_default | |
466 | && *id == zclient->instance)) | |
467 | zebra_redistribute_send( | |
468 | ZEBRA_REDISTRIBUTE_DELETE, | |
469 | zclient, afi, i, | |
470 | *id, | |
471 | VRF_DEFAULT); | |
472 | } | |
473 | ||
474 | /* Flush all redistribute request. */ | |
475 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
476 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
477 | if (i != zclient->redist_default | |
478 | && vrf_bitmap_check(zclient->redist[afi][i], | |
479 | vrf_id)) | |
480 | zebra_redistribute_send( | |
481 | ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, | |
482 | i, 0, vrf_id); | |
483 | ||
484 | /* If default information is needed. */ | |
485 | if (vrf_bitmap_check(zclient->default_information, VRF_DEFAULT)) | |
486 | zebra_message_send(zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, | |
487 | vrf_id); | |
0e5223e7 | 488 | } |
489 | ||
4a04e5f7 | 490 | /* Send request to zebra daemon to start or stop RA. */ |
d62a17ae | 491 | void zclient_send_interface_radv_req(struct zclient *zclient, vrf_id_t vrf_id, |
492 | struct interface *ifp, int enable, | |
493 | int ra_interval) | |
4a04e5f7 | 494 | { |
d62a17ae | 495 | struct stream *s; |
4a04e5f7 | 496 | |
d62a17ae | 497 | /* If not connected to the zebra yet. */ |
498 | if (zclient->sock < 0) | |
499 | return; | |
4a04e5f7 | 500 | |
d62a17ae | 501 | /* Form and send message. */ |
502 | s = zclient->obuf; | |
503 | stream_reset(s); | |
4a04e5f7 | 504 | |
d62a17ae | 505 | if (enable) |
506 | zclient_create_header(s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id); | |
507 | else | |
508 | zclient_create_header(s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id); | |
4a04e5f7 | 509 | |
d62a17ae | 510 | stream_putl(s, ifp->ifindex); |
511 | stream_putl(s, ra_interval); | |
4a04e5f7 | 512 | |
d62a17ae | 513 | stream_putw_at(s, 0, stream_get_endp(s)); |
4a04e5f7 | 514 | |
d62a17ae | 515 | zclient_send_message(zclient); |
4a04e5f7 | 516 | } |
517 | ||
718e3744 | 518 | /* Make connection to zebra daemon. */ |
d62a17ae | 519 | int zclient_start(struct zclient *zclient) |
718e3744 | 520 | { |
d62a17ae | 521 | if (zclient_debug) |
522 | zlog_info("zclient_start is called"); | |
523 | ||
d62a17ae | 524 | /* If already connected to the zebra. */ |
525 | if (zclient->sock >= 0) | |
526 | return 0; | |
527 | ||
528 | /* Check connect thread. */ | |
529 | if (zclient->t_connect) | |
530 | return 0; | |
531 | ||
532 | if (zclient_socket_connect(zclient) < 0) { | |
533 | if (zclient_debug) | |
534 | zlog_debug("zclient connection fail"); | |
535 | zclient->fail++; | |
536 | zclient_event(ZCLIENT_CONNECT, zclient); | |
537 | return -1; | |
538 | } | |
718e3744 | 539 | |
d62a17ae | 540 | if (set_nonblocking(zclient->sock) < 0) |
541 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, | |
542 | zclient->sock); | |
718e3744 | 543 | |
d62a17ae | 544 | /* Clear fail count. */ |
545 | zclient->fail = 0; | |
546 | if (zclient_debug) | |
547 | zlog_debug("zclient connect success with socket [%d]", | |
548 | zclient->sock); | |
718e3744 | 549 | |
d62a17ae | 550 | /* Create read thread. */ |
551 | zclient_event(ZCLIENT_READ, zclient); | |
718e3744 | 552 | |
d62a17ae | 553 | zebra_hello_send(zclient); |
718e3744 | 554 | |
d62a17ae | 555 | /* Inform the successful connection. */ |
556 | if (zclient->zebra_connected) | |
557 | (*zclient->zebra_connected)(zclient); | |
718e3744 | 558 | |
d62a17ae | 559 | return 0; |
718e3744 | 560 | } |
6b0655a2 | 561 | |
078430f6 DS |
562 | /* Initialize zebra client. Argument redist_default is unwanted |
563 | redistribute route type. */ | |
342213ea DS |
564 | void zclient_init(struct zclient *zclient, int redist_default, |
565 | u_short instance, struct zebra_privs_t *privs) | |
078430f6 | 566 | { |
d62a17ae | 567 | int afi, i; |
568 | ||
d62a17ae | 569 | /* Set -1 to the default socket value. */ |
570 | zclient->sock = -1; | |
342213ea | 571 | zclient->privs = privs; |
d62a17ae | 572 | |
573 | /* Clear redistribution flags. */ | |
574 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
575 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
576 | zclient->redist[afi][i] = vrf_bitmap_init(); | |
577 | ||
578 | /* Set unwanted redistribute route. bgpd does not need BGP route | |
579 | redistribution. */ | |
580 | zclient->redist_default = redist_default; | |
581 | zclient->instance = instance; | |
582 | /* Pending: make afi(s) an arg. */ | |
583 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
584 | redist_add_instance(&zclient->mi_redist[afi][redist_default], | |
585 | instance); | |
586 | ||
587 | /* Set default-information redistribute to zero. */ | |
588 | zclient->default_information = vrf_bitmap_init(); | |
d62a17ae | 589 | |
590 | if (zclient_debug) | |
591 | zlog_debug("zclient_start is called"); | |
592 | ||
593 | zclient_event(ZCLIENT_SCHEDULE, zclient); | |
7076bb2f | 594 | } |
078430f6 | 595 | |
7076bb2f FL |
596 | /* This function is a wrapper function for calling zclient_start from |
597 | timer or event thread. */ | |
d62a17ae | 598 | static int zclient_connect(struct thread *t) |
7076bb2f | 599 | { |
d62a17ae | 600 | struct zclient *zclient; |
078430f6 | 601 | |
d62a17ae | 602 | zclient = THREAD_ARG(t); |
603 | zclient->t_connect = NULL; | |
078430f6 | 604 | |
d62a17ae | 605 | if (zclient_debug) |
606 | zlog_debug("zclient_connect is called"); | |
078430f6 | 607 | |
d62a17ae | 608 | return zclient_start(zclient); |
078430f6 DS |
609 | } |
610 | ||
d62a17ae | 611 | /* |
612 | * "xdr_encode"-like interface that allows daemon (client) to send | |
613 | * a message to zebra server for a route that needs to be | |
614 | * added/deleted to the kernel. Info about the route is specified | |
615 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes | |
616 | * the info down the zclient socket using the stream_* functions. | |
617 | * | |
618 | * The corresponding read ("xdr_decode") function on the server | |
619 | * side is zread_ipv4_add()/zread_ipv4_delete(). | |
620 | * | |
621 | * 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 | |
622 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
623 | * | Length (2) | Command | Route Type | | |
624 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
625 | * | ZEBRA Flags | Message Flags | Prefix length | | |
626 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
627 | * | Destination IPv4 Prefix for route | | |
628 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
629 | * | Nexthop count | | |
630 | * +-+-+-+-+-+-+-+-+ | |
631 | * | |
632 | * | |
633 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then | |
634 | * described, as per the Nexthop count. Each nexthop described as: | |
635 | * | |
636 | * +-+-+-+-+-+-+-+-+ | |
637 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* | |
638 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
639 | * | IPv4 Nexthop address or Interface Index number | | |
640 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
641 | * | |
09a484dd DL |
642 | * Alternatively, if the route is a blackhole route, then Nexthop count |
643 | * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole | |
644 | * nexthop. | |
d62a17ae | 645 | * |
646 | * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*() | |
647 | * infrastructure was built around the traditional (32-bit "gate OR | |
648 | * ifindex") nexthop data unit. A special encoding can be used to feed | |
649 | * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route() | |
650 | * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4 | |
651 | * fields as follows: | |
652 | * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK | |
653 | * - .nexthop_num == .ifindex_num | |
654 | * - .nexthop and .ifindex are filled with gate and ifindex parts of | |
655 | * each compound nexthop, both in the same order | |
656 | * | |
657 | * zapi_ipv4_route() will produce two nexthop data units for each such | |
658 | * interleaved 64-bit nexthop. On the zserv side of the socket it will be | |
659 | * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure. | |
660 | * | |
661 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 | |
662 | * byte value. | |
663 | * | |
664 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 | |
665 | * byte value. | |
666 | * | |
667 | * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value | |
668 | * | |
669 | * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value | |
670 | * | |
671 | * XXX: No attention paid to alignment. | |
672 | */ | |
673 | int zapi_ipv4_route(u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, | |
674 | struct zapi_ipv4 *api) | |
718e3744 | 675 | { |
d62a17ae | 676 | int i; |
677 | int psize; | |
678 | struct stream *s; | |
679 | ||
680 | /* Reset stream. */ | |
681 | s = zclient->obuf; | |
682 | stream_reset(s); | |
683 | ||
684 | /* Some checks for labeled-unicast. The current expectation is that each | |
685 | * nexthop is accompanied by a label in the case of labeled-unicast. | |
686 | */ | |
687 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL) | |
688 | && CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
689 | /* We expect prefixes installed with labels and the number to | |
690 | * match | |
691 | * the number of nexthops. | |
692 | */ | |
693 | assert(api->label_num == api->nexthop_num); | |
694 | } | |
695 | ||
696 | zclient_create_header(s, cmd, api->vrf_id); | |
697 | ||
698 | /* Put type and nexthop. */ | |
699 | stream_putc(s, api->type); | |
700 | stream_putw(s, api->instance); | |
701 | stream_putl(s, api->flags); | |
702 | stream_putc(s, api->message); | |
703 | stream_putw(s, api->safi); | |
704 | ||
705 | /* Put prefix information. */ | |
706 | psize = PSIZE(p->prefixlen); | |
707 | stream_putc(s, p->prefixlen); | |
708 | stream_write(s, (u_char *)&p->prefix, psize); | |
709 | ||
710 | /* Nexthop, ifindex, distance and metric information. */ | |
711 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
09a484dd | 712 | stream_putc(s, api->nexthop_num + api->ifindex_num); |
d62a17ae | 713 | |
714 | for (i = 0; i < api->nexthop_num; i++) { | |
715 | stream_putc(s, NEXTHOP_TYPE_IPV4); | |
716 | stream_put_in_addr(s, api->nexthop[i]); | |
717 | /* For labeled-unicast, each nexthop is followed by | |
718 | * label. */ | |
719 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) | |
720 | stream_putl(s, api->label[i]); | |
721 | } | |
722 | for (i = 0; i < api->ifindex_num; i++) { | |
723 | stream_putc(s, NEXTHOP_TYPE_IFINDEX); | |
724 | stream_putl(s, api->ifindex[i]); | |
725 | } | |
726 | } | |
727 | ||
728 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) | |
729 | stream_putc(s, api->distance); | |
730 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
731 | stream_putl(s, api->metric); | |
732 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
733 | stream_putl(s, api->tag); | |
734 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
735 | stream_putl(s, api->mtu); | |
736 | ||
737 | /* Put length at the first point of the stream. */ | |
738 | stream_putw_at(s, 0, stream_get_endp(s)); | |
739 | ||
740 | return zclient_send_message(zclient); | |
718e3744 | 741 | } |
742 | ||
d62a17ae | 743 | int zapi_ipv4_route_ipv6_nexthop(u_char cmd, struct zclient *zclient, |
744 | struct prefix_ipv4 *p, struct zapi_ipv6 *api) | |
8a92a8a0 | 745 | { |
d62a17ae | 746 | int i; |
747 | int psize; | |
748 | struct stream *s; | |
749 | ||
750 | /* Reset stream. */ | |
751 | s = zclient->obuf; | |
752 | stream_reset(s); | |
753 | ||
754 | /* Some checks for labeled-unicast. The current expectation is that each | |
755 | * nexthop is accompanied by a label in the case of labeled-unicast. | |
756 | */ | |
757 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL) | |
758 | && CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
759 | /* We expect prefixes installed with labels and the number to | |
760 | * match | |
761 | * the number of nexthops. | |
762 | */ | |
763 | assert(api->label_num == api->nexthop_num); | |
8a92a8a0 | 764 | } |
d62a17ae | 765 | |
766 | zclient_create_header(s, cmd, api->vrf_id); | |
767 | ||
768 | /* Put type and nexthop. */ | |
769 | stream_putc(s, api->type); | |
770 | stream_putw(s, api->instance); | |
771 | stream_putl(s, api->flags); | |
772 | stream_putc(s, api->message); | |
773 | stream_putw(s, api->safi); | |
774 | ||
775 | /* Put prefix information. */ | |
776 | psize = PSIZE(p->prefixlen); | |
777 | stream_putc(s, p->prefixlen); | |
778 | stream_write(s, (u_char *)&p->prefix, psize); | |
779 | ||
780 | /* Nexthop, ifindex, distance and metric information. */ | |
781 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
09a484dd | 782 | stream_putc(s, api->nexthop_num + api->ifindex_num); |
d62a17ae | 783 | |
784 | for (i = 0; i < api->nexthop_num; i++) { | |
785 | stream_putc(s, NEXTHOP_TYPE_IPV6); | |
786 | stream_write(s, (u_char *)api->nexthop[i], 16); | |
787 | /* For labeled-unicast, each nexthop is followed by | |
788 | * label. */ | |
789 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) | |
790 | stream_putl(s, api->label[i]); | |
791 | } | |
792 | for (i = 0; i < api->ifindex_num; i++) { | |
793 | stream_putc(s, NEXTHOP_TYPE_IFINDEX); | |
794 | stream_putl(s, api->ifindex[i]); | |
795 | } | |
8a92a8a0 | 796 | } |
8a92a8a0 | 797 | |
d62a17ae | 798 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) |
799 | stream_putc(s, api->distance); | |
800 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
801 | stream_putl(s, api->metric); | |
802 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
803 | stream_putl(s, api->tag); | |
804 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
805 | stream_putl(s, api->mtu); | |
8a92a8a0 | 806 | |
d62a17ae | 807 | /* Put length at the first point of the stream. */ |
808 | stream_putw_at(s, 0, stream_get_endp(s)); | |
8a92a8a0 | 809 | |
d62a17ae | 810 | return zclient_send_message(zclient); |
8a92a8a0 DS |
811 | } |
812 | ||
d62a17ae | 813 | int zapi_ipv6_route(u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
814 | struct prefix_ipv6 *src_p, struct zapi_ipv6 *api) | |
718e3744 | 815 | { |
d62a17ae | 816 | int i; |
817 | int psize; | |
818 | struct stream *s; | |
819 | ||
820 | /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL | |
821 | */ | |
822 | assert(!(api->message & ZAPI_MESSAGE_SRCPFX) == !src_p); | |
823 | ||
824 | /* Reset stream. */ | |
825 | s = zclient->obuf; | |
826 | stream_reset(s); | |
827 | ||
828 | /* Some checks for labeled-unicast. The current expectation is that each | |
829 | * nexthop is accompanied by a label in the case of labeled-unicast. | |
830 | */ | |
831 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL) | |
832 | && CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
833 | /* We expect prefixes installed with labels and the number to | |
834 | * match | |
835 | * the number of nexthops. | |
836 | */ | |
837 | assert(api->label_num == api->nexthop_num); | |
718e3744 | 838 | } |
d62a17ae | 839 | |
840 | zclient_create_header(s, cmd, api->vrf_id); | |
841 | ||
842 | /* Put type and nexthop. */ | |
843 | stream_putc(s, api->type); | |
844 | stream_putw(s, api->instance); | |
845 | stream_putl(s, api->flags); | |
846 | stream_putc(s, api->message); | |
847 | stream_putw(s, api->safi); | |
848 | ||
849 | /* Put prefix information. */ | |
850 | psize = PSIZE(p->prefixlen); | |
851 | stream_putc(s, p->prefixlen); | |
852 | stream_write(s, (u_char *)&p->prefix, psize); | |
853 | ||
854 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) { | |
855 | psize = PSIZE(src_p->prefixlen); | |
856 | stream_putc(s, src_p->prefixlen); | |
857 | stream_write(s, (u_char *)&src_p->prefix, psize); | |
718e3744 | 858 | } |
718e3744 | 859 | |
d62a17ae | 860 | /* Nexthop, ifindex, distance and metric information. */ |
861 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
09a484dd | 862 | stream_putc(s, api->nexthop_num + api->ifindex_num); |
d62a17ae | 863 | |
864 | for (i = 0; i < api->nexthop_num; i++) { | |
865 | stream_putc(s, NEXTHOP_TYPE_IPV6); | |
866 | stream_write(s, (u_char *)api->nexthop[i], 16); | |
867 | /* For labeled-unicast, each nexthop is followed by | |
868 | * label. */ | |
869 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) | |
870 | stream_putl(s, api->label[i]); | |
871 | } | |
872 | for (i = 0; i < api->ifindex_num; i++) { | |
873 | stream_putc(s, NEXTHOP_TYPE_IFINDEX); | |
874 | stream_putl(s, api->ifindex[i]); | |
875 | } | |
876 | } | |
718e3744 | 877 | |
d62a17ae | 878 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) |
879 | stream_putc(s, api->distance); | |
880 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
881 | stream_putl(s, api->metric); | |
882 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
883 | stream_putl(s, api->tag); | |
884 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
885 | stream_putl(s, api->mtu); | |
718e3744 | 886 | |
d62a17ae | 887 | /* Put length at the first point of the stream. */ |
888 | stream_putw_at(s, 0, stream_get_endp(s)); | |
889 | ||
890 | return zclient_send_message(zclient); | |
718e3744 | 891 | } |
718e3744 | 892 | |
0e51b4a3 RW |
893 | int zclient_route_send(u_char cmd, struct zclient *zclient, |
894 | struct zapi_route *api) | |
657cde12 | 895 | { |
0e51b4a3 RW |
896 | if (zapi_route_encode(cmd, zclient->obuf, api) < 0) |
897 | return -1; | |
898 | return zclient_send_message(zclient); | |
899 | } | |
900 | ||
901 | int zapi_route_encode(u_char cmd, struct stream *s, struct zapi_route *api) | |
902 | { | |
903 | struct zapi_nexthop *api_nh; | |
d62a17ae | 904 | int i; |
905 | int psize; | |
d62a17ae | 906 | |
d62a17ae | 907 | stream_reset(s); |
d62a17ae | 908 | zclient_create_header(s, cmd, api->vrf_id); |
909 | ||
d62a17ae | 910 | stream_putc(s, api->type); |
911 | stream_putw(s, api->instance); | |
912 | stream_putl(s, api->flags); | |
913 | stream_putc(s, api->message); | |
832d0f56 | 914 | stream_putc(s, api->safi); |
90264d64 | 915 | if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) |
2dbad57f | 916 | stream_put(s, &(api->rmac), sizeof(struct ethaddr)); |
d62a17ae | 917 | |
918 | /* Put prefix information. */ | |
0e51b4a3 | 919 | stream_putc(s, api->prefix.family); |
bb1b9c47 RW |
920 | psize = PSIZE(api->prefix.prefixlen); |
921 | stream_putc(s, api->prefix.prefixlen); | |
922 | stream_write(s, (u_char *)&api->prefix.u.prefix, psize); | |
d62a17ae | 923 | |
924 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) { | |
bb1b9c47 RW |
925 | psize = PSIZE(api->src_prefix.prefixlen); |
926 | stream_putc(s, api->src_prefix.prefixlen); | |
927 | stream_write(s, (u_char *)&api->src_prefix.prefix, psize); | |
d62a17ae | 928 | } |
929 | ||
0e51b4a3 | 930 | /* Nexthops. */ |
d62a17ae | 931 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { |
bb1b9c47 RW |
932 | /* limit the number of nexthops if necessary */ |
933 | if (api->nexthop_num > MULTIPATH_NUM) { | |
934 | char buf[PREFIX2STR_BUFFER]; | |
935 | ||
936 | prefix2str(&api->prefix, buf, sizeof(buf)); | |
937 | zlog_warn( | |
a74e593b RW |
938 | "%s: prefix %s: can't encode %u nexthops " |
939 | "(maximum is %u)", | |
940 | __func__, buf, api->nexthop_num, MULTIPATH_NUM); | |
941 | return -1; | |
bb1b9c47 RW |
942 | } |
943 | ||
b5f79651 | 944 | stream_putw(s, api->nexthop_num); |
d5b2119c DS |
945 | if (api->nexthop_num) |
946 | stream_putw(s, api->nh_vrf_id); | |
d62a17ae | 947 | |
948 | for (i = 0; i < api->nexthop_num; i++) { | |
bb1b9c47 RW |
949 | api_nh = &api->nexthops[i]; |
950 | ||
951 | stream_putc(s, api_nh->type); | |
952 | switch (api_nh->type) { | |
d62a17ae | 953 | case NEXTHOP_TYPE_BLACKHOLE: |
94758e66 | 954 | stream_putc(s, api_nh->bh_type); |
d62a17ae | 955 | break; |
956 | case NEXTHOP_TYPE_IPV4: | |
bb1b9c47 | 957 | stream_put_in_addr(s, &api_nh->gate.ipv4); |
d62a17ae | 958 | break; |
959 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
bb1b9c47 RW |
960 | stream_put_in_addr(s, &api_nh->gate.ipv4); |
961 | stream_putl(s, api_nh->ifindex); | |
d62a17ae | 962 | break; |
963 | case NEXTHOP_TYPE_IFINDEX: | |
bb1b9c47 | 964 | stream_putl(s, api_nh->ifindex); |
d62a17ae | 965 | break; |
966 | case NEXTHOP_TYPE_IPV6: | |
bb1b9c47 RW |
967 | stream_write(s, (u_char *)&api_nh->gate.ipv6, |
968 | 16); | |
d62a17ae | 969 | break; |
970 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
bb1b9c47 RW |
971 | stream_write(s, (u_char *)&api_nh->gate.ipv6, |
972 | 16); | |
973 | stream_putl(s, api_nh->ifindex); | |
d62a17ae | 974 | break; |
ec93aa12 DS |
975 | default: |
976 | zlog_warn("%s: Specified Nexthop type %d does not exist", | |
977 | __PRETTY_FUNCTION__, api_nh->type); | |
978 | return -1; | |
d62a17ae | 979 | } |
bb1b9c47 | 980 | |
52dd3aa4 RW |
981 | /* MPLS labels for BGP-LU or Segment Routing */ |
982 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) { | |
983 | if (api_nh->label_num > MPLS_MAX_LABELS) { | |
984 | char buf[PREFIX2STR_BUFFER]; | |
985 | prefix2str(&api->prefix, buf, | |
986 | sizeof(buf)); | |
987 | zlog_err( | |
988 | "%s: prefix %s: can't encode " | |
989 | "%u labels (maximum is %u)", | |
990 | __func__, buf, | |
991 | api_nh->label_num, | |
992 | MPLS_MAX_LABELS); | |
993 | return -1; | |
994 | } | |
995 | ||
996 | stream_putc(s, api_nh->label_num); | |
997 | stream_put(s, &api_nh->labels[0], | |
998 | api_nh->label_num | |
999 | * sizeof(mpls_label_t)); | |
1000 | } | |
d62a17ae | 1001 | } |
1002 | } | |
1003 | ||
0e51b4a3 | 1004 | /* Attributes. */ |
d62a17ae | 1005 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) |
1006 | stream_putc(s, api->distance); | |
1007 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
1008 | stream_putl(s, api->metric); | |
1009 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
1010 | stream_putl(s, api->tag); | |
1011 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
1012 | stream_putl(s, api->mtu); | |
1013 | ||
1014 | /* Put length at the first point of the stream. */ | |
1015 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1016 | ||
0e51b4a3 RW |
1017 | return 0; |
1018 | } | |
1019 | ||
1020 | int zapi_route_decode(struct stream *s, struct zapi_route *api) | |
1021 | { | |
1022 | struct zapi_nexthop *api_nh; | |
1023 | int i; | |
1024 | ||
1025 | memset(api, 0, sizeof(*api)); | |
1026 | ||
1027 | /* Type, flags, message. */ | |
ec93aa12 DS |
1028 | STREAM_GETC(s, api->type); |
1029 | if (api->type > ZEBRA_ROUTE_MAX) { | |
1030 | zlog_warn("%s: Specified route type: %d is not a legal value\n", | |
1031 | __PRETTY_FUNCTION__, api->type); | |
1032 | return -1; | |
1033 | } | |
1034 | ||
1035 | STREAM_GETW(s, api->instance); | |
1036 | STREAM_GETL(s, api->flags); | |
1037 | STREAM_GETC(s, api->message); | |
832d0f56 | 1038 | STREAM_GETC(s, api->safi); |
90264d64 | 1039 | if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) |
2dbad57f | 1040 | stream_get(&(api->rmac), s, sizeof(struct ethaddr)); |
0e51b4a3 RW |
1041 | |
1042 | /* Prefix. */ | |
ec93aa12 DS |
1043 | STREAM_GETC(s, api->prefix.family); |
1044 | STREAM_GETC(s, api->prefix.prefixlen); | |
0e51b4a3 RW |
1045 | switch (api->prefix.family) { |
1046 | case AF_INET: | |
ec93aa12 DS |
1047 | if (api->prefix.prefixlen > IPV4_MAX_PREFIXLEN) { |
1048 | zlog_warn("%s: V4 prefixlen is %d which should not be more than 32", | |
1049 | __PRETTY_FUNCTION__, api->prefix.prefixlen); | |
1050 | return -1; | |
1051 | } | |
0e51b4a3 RW |
1052 | break; |
1053 | case AF_INET6: | |
ec93aa12 DS |
1054 | if (api->prefix.prefixlen > IPV6_MAX_PREFIXLEN) { |
1055 | zlog_warn("%s: v6 prefixlen is %d which should not be more than 128", | |
1056 | __PRETTY_FUNCTION__, api->prefix.prefixlen); | |
1057 | return -1; | |
1058 | } | |
0e51b4a3 | 1059 | break; |
ec93aa12 DS |
1060 | default: |
1061 | zlog_warn("%s: Specified family %d is not v4 or v6", | |
1062 | __PRETTY_FUNCTION__, api->prefix.family); | |
1063 | return -1; | |
0e51b4a3 | 1064 | } |
ec93aa12 DS |
1065 | STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen)); |
1066 | ||
0e51b4a3 RW |
1067 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) { |
1068 | api->src_prefix.family = AF_INET6; | |
ec93aa12 DS |
1069 | STREAM_GETC(s, api->src_prefix.prefixlen); |
1070 | if (api->src_prefix.prefixlen > IPV6_MAX_PREFIXLEN) { | |
1071 | zlog_warn("%s: SRC Prefix prefixlen received: %d is too large", | |
1072 | __PRETTY_FUNCTION__, | |
1073 | api->src_prefix.prefixlen); | |
1074 | return -1; | |
1075 | } | |
1076 | STREAM_GET(&api->src_prefix.prefix, s, | |
0e51b4a3 RW |
1077 | PSIZE(api->src_prefix.prefixlen)); |
1078 | ||
1079 | if (api->prefix.family != AF_INET6 | |
ec93aa12 DS |
1080 | || api->src_prefix.prefixlen == 0) { |
1081 | zlog_warn("%s: SRC prefix specified in some manner that makes no sense", | |
1082 | __PRETTY_FUNCTION__); | |
1083 | return -1; | |
1084 | } | |
0e51b4a3 RW |
1085 | } |
1086 | ||
1087 | /* Nexthops. */ | |
1088 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
ec93aa12 | 1089 | STREAM_GETW(s, api->nexthop_num); |
0e51b4a3 RW |
1090 | if (api->nexthop_num > MULTIPATH_NUM) { |
1091 | zlog_warn("%s: invalid number of nexthops (%u)", | |
1092 | __func__, api->nexthop_num); | |
1093 | return -1; | |
1094 | } | |
1095 | ||
d5b2119c DS |
1096 | if (api->nexthop_num) |
1097 | STREAM_GETW(s, api->nh_vrf_id); | |
1098 | ||
0e51b4a3 RW |
1099 | for (i = 0; i < api->nexthop_num; i++) { |
1100 | api_nh = &api->nexthops[i]; | |
1101 | ||
ec93aa12 | 1102 | STREAM_GETC(s, api_nh->type); |
0e51b4a3 RW |
1103 | switch (api_nh->type) { |
1104 | case NEXTHOP_TYPE_BLACKHOLE: | |
ec93aa12 | 1105 | STREAM_GETC(s, api_nh->bh_type); |
0e51b4a3 RW |
1106 | break; |
1107 | case NEXTHOP_TYPE_IPV4: | |
ec93aa12 DS |
1108 | STREAM_GET(&api_nh->gate.ipv4.s_addr, s, |
1109 | IPV4_MAX_BYTELEN); | |
0e51b4a3 RW |
1110 | break; |
1111 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
ec93aa12 DS |
1112 | STREAM_GET(&api_nh->gate.ipv4.s_addr, s, |
1113 | IPV4_MAX_BYTELEN); | |
1114 | STREAM_GETL(s, api_nh->ifindex); | |
0e51b4a3 RW |
1115 | break; |
1116 | case NEXTHOP_TYPE_IFINDEX: | |
ec93aa12 | 1117 | STREAM_GETL(s, api_nh->ifindex); |
0e51b4a3 RW |
1118 | break; |
1119 | case NEXTHOP_TYPE_IPV6: | |
ec93aa12 | 1120 | STREAM_GET(&api_nh->gate.ipv6, s, 16); |
0e51b4a3 RW |
1121 | break; |
1122 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
ec93aa12 DS |
1123 | STREAM_GET(&api_nh->gate.ipv6, s, 16); |
1124 | STREAM_GETL(s, api_nh->ifindex); | |
0e51b4a3 | 1125 | break; |
ec93aa12 DS |
1126 | default: |
1127 | zlog_warn("%s: Specified nexthop type %d does not exist", | |
1128 | __PRETTY_FUNCTION__, | |
1129 | api_nh->type); | |
1130 | return -1; | |
0e51b4a3 RW |
1131 | } |
1132 | ||
52dd3aa4 | 1133 | /* MPLS labels for BGP-LU or Segment Routing */ |
0e51b4a3 | 1134 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) { |
ec93aa12 | 1135 | STREAM_GETC(s, api_nh->label_num); |
52dd3aa4 RW |
1136 | |
1137 | if (api_nh->label_num > MPLS_MAX_LABELS) { | |
1138 | zlog_warn( | |
1139 | "%s: invalid number of MPLS " | |
1140 | "labels (%u)", | |
1141 | __func__, api_nh->label_num); | |
1142 | return -1; | |
1143 | } | |
1144 | ||
ec93aa12 | 1145 | STREAM_GET(&api_nh->labels[0], s, |
52dd3aa4 RW |
1146 | api_nh->label_num |
1147 | * sizeof(mpls_label_t)); | |
0e51b4a3 RW |
1148 | } |
1149 | } | |
1150 | } | |
1151 | ||
1152 | /* Attributes. */ | |
1153 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) | |
ec93aa12 | 1154 | STREAM_GETC(s, api->distance); |
0e51b4a3 | 1155 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) |
ec93aa12 | 1156 | STREAM_GETL(s, api->metric); |
0e51b4a3 | 1157 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) |
ec93aa12 | 1158 | STREAM_GETL(s, api->tag); |
0e51b4a3 | 1159 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) |
ec93aa12 | 1160 | STREAM_GETL(s, api->mtu); |
0e51b4a3 | 1161 | |
ec93aa12 | 1162 | stream_failure: |
0e51b4a3 | 1163 | return 0; |
657cde12 DS |
1164 | } |
1165 | ||
7ea7b86e DS |
1166 | bool zapi_route_notify_decode(struct stream *s, struct prefix *p, |
1167 | enum zapi_route_notify_owner *note) | |
1168 | { | |
1169 | STREAM_GET(note, s, sizeof(*note)); | |
1170 | ||
1171 | STREAM_GETC(s, p->family); | |
1172 | STREAM_GETC(s, p->prefixlen); | |
1173 | STREAM_GET(&p->u.prefix, s, | |
1174 | PSIZE(p->prefixlen)); | |
1175 | ||
1176 | return true; | |
1177 | ||
1178 | stream_failure: | |
1179 | return false; | |
1180 | } | |
1181 | ||
d62a17ae | 1182 | /* |
0a589359 | 1183 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE |
1184 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will | |
d62a17ae | 1185 | * then set/unset redist[type] in the client handle (a struct zserv) for the |
0a589359 | 1186 | * sending client |
1187 | */ | |
d62a17ae | 1188 | int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi, |
1189 | int type, u_short instance, vrf_id_t vrf_id) | |
718e3744 | 1190 | { |
d62a17ae | 1191 | struct stream *s; |
1192 | ||
1193 | s = zclient->obuf; | |
1194 | stream_reset(s); | |
1195 | ||
1196 | zclient_create_header(s, command, vrf_id); | |
1197 | stream_putc(s, afi); | |
1198 | stream_putc(s, type); | |
1199 | stream_putw(s, instance); | |
1200 | ||
1201 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1202 | ||
1203 | return zclient_send_message(zclient); | |
718e3744 | 1204 | } |
1205 | ||
d9178828 | 1206 | /* Get prefix in ZServ format; family should be filled in on prefix */ |
d62a17ae | 1207 | static void zclient_stream_get_prefix(struct stream *s, struct prefix *p) |
d9178828 | 1208 | { |
d62a17ae | 1209 | size_t plen = prefix_blen(p); |
1210 | u_char c; | |
1211 | p->prefixlen = 0; | |
1212 | ||
1213 | if (plen == 0) | |
1214 | return; | |
1215 | ||
1216 | stream_get(&p->u.prefix, s, plen); | |
ec93aa12 | 1217 | STREAM_GETC(s, c); |
d62a17ae | 1218 | p->prefixlen = MIN(plen * 8, c); |
ec93aa12 DS |
1219 | |
1220 | stream_failure: | |
1221 | return; | |
d9178828 PJ |
1222 | } |
1223 | ||
18a6dce6 | 1224 | /* Router-id update from zebra daemon. */ |
d62a17ae | 1225 | void zebra_router_id_update_read(struct stream *s, struct prefix *rid) |
18a6dce6 | 1226 | { |
d62a17ae | 1227 | /* Fetch interface address. */ |
ec93aa12 | 1228 | STREAM_GETC(s, rid->family); |
d62a17ae | 1229 | |
1230 | zclient_stream_get_prefix(s, rid); | |
ec93aa12 DS |
1231 | |
1232 | stream_failure: | |
1233 | return; | |
18a6dce6 | 1234 | } |
1235 | ||
718e3744 | 1236 | /* Interface addition from zebra daemon. */ |
d62a17ae | 1237 | /* |
0a589359 | 1238 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or |
1239 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: | |
1240 | * 0 1 2 3 | |
1241 | * 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 | |
0a589359 | 1242 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
1243 | * | ifname | | |
1244 | * | | | |
1245 | * | | | |
1246 | * | | | |
1247 | * | | | |
1248 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
16f1b9ee OD |
1249 | * | ifindex | |
1250 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1251 | * | status | | |
0a589359 | 1252 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee | 1253 | * | if_flags | |
c77d4546 | 1254 | * | | |
0a589359 | 1255 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee OD |
1256 | * | metric | |
1257 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
2d7f0d76 DS |
1258 | * | speed | |
1259 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
16f1b9ee OD |
1260 | * | ifmtu | |
1261 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1262 | * | ifmtu6 | | |
1263 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1264 | * | bandwidth | | |
1265 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1266 | * | Link Layer Type | | |
0a589359 | 1267 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee | 1268 | * | Harware Address Length | |
0a589359 | 1269 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee OD |
1270 | * | Hardware Address if HW lenght different from 0 | |
1271 | * | ... max INTERFACE_HWADDR_MAX | | |
0a589359 | 1272 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee | 1273 | * | Link_params? | Whether a link-params follows: 1 or 0. |
0a589359 | 1274 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
16f1b9ee OD |
1275 | * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized | |
1276 | * | .... (struct if_link_params). | | |
0a589359 | 1277 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
1278 | */ | |
1279 | ||
d62a17ae | 1280 | static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id) |
1892f15e | 1281 | { |
d62a17ae | 1282 | struct vrf *vrf; |
1283 | char vrfname_tmp[VRF_NAMSIZ]; | |
1284 | struct vrf_data data; | |
1892f15e | 1285 | |
d62a17ae | 1286 | stream_get(&data, zclient->ibuf, sizeof(struct vrf_data)); |
1287 | /* Read interface name. */ | |
1288 | stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ); | |
1892f15e | 1289 | |
d62a17ae | 1290 | /* Lookup/create vrf by vrf_id. */ |
1291 | vrf = vrf_get(vrf_id, vrfname_tmp); | |
1292 | vrf->data = data; | |
1892f15e | 1293 | |
d62a17ae | 1294 | vrf_enable(vrf); |
1892f15e DS |
1295 | } |
1296 | ||
d62a17ae | 1297 | static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id) |
1892f15e | 1298 | { |
d62a17ae | 1299 | struct vrf *vrf; |
1892f15e | 1300 | |
d62a17ae | 1301 | /* Lookup vrf by vrf_id. */ |
1302 | vrf = vrf_lookup_by_id(vrf_id); | |
1892f15e | 1303 | |
d62a17ae | 1304 | /* |
1305 | * If a routing protocol doesn't know about a | |
1306 | * vrf that is about to be deleted. There is | |
1307 | * no point in attempting to delete it. | |
1308 | */ | |
1309 | if (!vrf) | |
1310 | return; | |
beef1990 | 1311 | |
d62a17ae | 1312 | vrf_delete(vrf); |
1892f15e DS |
1313 | } |
1314 | ||
d62a17ae | 1315 | struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id) |
718e3744 | 1316 | { |
d62a17ae | 1317 | struct interface *ifp; |
1318 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
718e3744 | 1319 | |
d62a17ae | 1320 | /* Read interface name. */ |
1321 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
718e3744 | 1322 | |
d62a17ae | 1323 | /* Lookup/create interface by name. */ |
bcc24579 | 1324 | ifp = if_get_by_name(ifname_tmp, vrf_id, 0); |
718e3744 | 1325 | |
d62a17ae | 1326 | zebra_interface_if_set_value(s, ifp); |
718e3744 | 1327 | |
d62a17ae | 1328 | return ifp; |
718e3744 | 1329 | } |
1330 | ||
d62a17ae | 1331 | /* |
0a589359 | 1332 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) |
1333 | * from zebra server. The format of this message is the same as | |
1334 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see | |
1335 | * comments for zebra_interface_add_read), except that no sockaddr_dl | |
1336 | * is sent at the tail of the message. | |
1337 | */ | |
d62a17ae | 1338 | struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id) |
718e3744 | 1339 | { |
d62a17ae | 1340 | struct interface *ifp; |
1341 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
1342 | ||
1343 | /* Read interface name. */ | |
1344 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
1345 | ||
1346 | /* Lookup this by interface index. */ | |
bcc24579 | 1347 | ifp = if_lookup_by_name(ifname_tmp, vrf_id); |
d62a17ae | 1348 | if (ifp == NULL) { |
1349 | zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d", | |
1350 | ifname_tmp, vrf_id); | |
1351 | return NULL; | |
1352 | } | |
1353 | ||
1354 | zebra_interface_if_set_value(s, ifp); | |
1355 | ||
1356 | return ifp; | |
718e3744 | 1357 | } |
1358 | ||
d62a17ae | 1359 | static void link_params_set_value(struct stream *s, struct if_link_params *iflp) |
16f1b9ee OD |
1360 | { |
1361 | ||
d62a17ae | 1362 | if (iflp == NULL) |
1363 | return; | |
1364 | ||
1365 | iflp->lp_status = stream_getl(s); | |
1366 | iflp->te_metric = stream_getl(s); | |
1367 | iflp->max_bw = stream_getf(s); | |
1368 | iflp->max_rsv_bw = stream_getf(s); | |
1369 | uint32_t bwclassnum = stream_getl(s); | |
1370 | { | |
1371 | unsigned int i; | |
1372 | for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++) | |
1373 | iflp->unrsv_bw[i] = stream_getf(s); | |
1374 | if (i < bwclassnum) | |
1375 | zlog_err( | |
1376 | "%s: received %d > %d (MAX_CLASS_TYPE) bw entries" | |
1377 | " - outdated library?", | |
1378 | __func__, bwclassnum, MAX_CLASS_TYPE); | |
1379 | } | |
1380 | iflp->admin_grp = stream_getl(s); | |
1381 | iflp->rmt_as = stream_getl(s); | |
1382 | iflp->rmt_ip.s_addr = stream_get_ipv4(s); | |
1383 | ||
1384 | iflp->av_delay = stream_getl(s); | |
1385 | iflp->min_delay = stream_getl(s); | |
1386 | iflp->max_delay = stream_getl(s); | |
1387 | iflp->delay_var = stream_getl(s); | |
1388 | ||
1389 | iflp->pkt_loss = stream_getf(s); | |
1390 | iflp->res_bw = stream_getf(s); | |
1391 | iflp->ava_bw = stream_getf(s); | |
1392 | iflp->use_bw = stream_getf(s); | |
16f1b9ee OD |
1393 | } |
1394 | ||
d62a17ae | 1395 | struct interface *zebra_interface_link_params_read(struct stream *s) |
16f1b9ee | 1396 | { |
d62a17ae | 1397 | struct if_link_params *iflp; |
1398 | ifindex_t ifindex; | |
c28e5b2a | 1399 | |
d62a17ae | 1400 | assert(s); |
c28e5b2a | 1401 | |
d62a17ae | 1402 | ifindex = stream_getl(s); |
16f1b9ee | 1403 | |
d62a17ae | 1404 | struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT); |
16f1b9ee | 1405 | |
d62a17ae | 1406 | if (ifp == NULL) { |
1407 | zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__, | |
1408 | ifindex); | |
1409 | return NULL; | |
1410 | } | |
16f1b9ee | 1411 | |
d62a17ae | 1412 | if ((iflp = if_link_params_get(ifp)) == NULL) |
1413 | return NULL; | |
16f1b9ee | 1414 | |
d62a17ae | 1415 | link_params_set_value(s, iflp); |
16f1b9ee | 1416 | |
d62a17ae | 1417 | return ifp; |
16f1b9ee OD |
1418 | } |
1419 | ||
d62a17ae | 1420 | void zebra_interface_if_set_value(struct stream *s, struct interface *ifp) |
16f1b9ee | 1421 | { |
d62a17ae | 1422 | u_char link_params_status = 0; |
1423 | ||
1424 | /* Read interface's index. */ | |
ff880b78 | 1425 | if_set_index(ifp, stream_getl(s)); |
d62a17ae | 1426 | ifp->status = stream_getc(s); |
1427 | ||
1428 | /* Read interface's value. */ | |
1429 | ifp->flags = stream_getq(s); | |
1430 | ifp->ptm_enable = stream_getc(s); | |
1431 | ifp->ptm_status = stream_getc(s); | |
1432 | ifp->metric = stream_getl(s); | |
1433 | ifp->speed = stream_getl(s); | |
1434 | ifp->mtu = stream_getl(s); | |
1435 | ifp->mtu6 = stream_getl(s); | |
1436 | ifp->bandwidth = stream_getl(s); | |
1437 | ifp->ll_type = stream_getl(s); | |
1438 | ifp->hw_addr_len = stream_getl(s); | |
1439 | if (ifp->hw_addr_len) | |
1440 | stream_get(ifp->hw_addr, s, | |
1441 | MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX)); | |
1442 | ||
1443 | /* Read Traffic Engineering status */ | |
1444 | link_params_status = stream_getc(s); | |
1445 | /* Then, Traffic Engineering parameters if any */ | |
1446 | if (link_params_status) { | |
1447 | struct if_link_params *iflp = if_link_params_get(ifp); | |
1448 | link_params_set_value(s, iflp); | |
1449 | } | |
16f1b9ee OD |
1450 | } |
1451 | ||
d62a17ae | 1452 | size_t zebra_interface_link_params_write(struct stream *s, |
1453 | struct interface *ifp) | |
16f1b9ee | 1454 | { |
d62a17ae | 1455 | size_t w; |
1456 | struct if_link_params *iflp; | |
1457 | int i; | |
16f1b9ee | 1458 | |
d62a17ae | 1459 | if (s == NULL || ifp == NULL || ifp->link_params == NULL) |
1460 | return 0; | |
16f1b9ee | 1461 | |
d62a17ae | 1462 | iflp = ifp->link_params; |
1463 | w = 0; | |
16f1b9ee | 1464 | |
d62a17ae | 1465 | w += stream_putl(s, iflp->lp_status); |
16f1b9ee | 1466 | |
d62a17ae | 1467 | w += stream_putl(s, iflp->te_metric); |
1468 | w += stream_putf(s, iflp->max_bw); | |
1469 | w += stream_putf(s, iflp->max_rsv_bw); | |
16f1b9ee | 1470 | |
d62a17ae | 1471 | w += stream_putl(s, MAX_CLASS_TYPE); |
1472 | for (i = 0; i < MAX_CLASS_TYPE; i++) | |
1473 | w += stream_putf(s, iflp->unrsv_bw[i]); | |
16f1b9ee | 1474 | |
d62a17ae | 1475 | w += stream_putl(s, iflp->admin_grp); |
1476 | w += stream_putl(s, iflp->rmt_as); | |
1477 | w += stream_put_in_addr(s, &iflp->rmt_ip); | |
16f1b9ee | 1478 | |
d62a17ae | 1479 | w += stream_putl(s, iflp->av_delay); |
1480 | w += stream_putl(s, iflp->min_delay); | |
1481 | w += stream_putl(s, iflp->max_delay); | |
1482 | w += stream_putl(s, iflp->delay_var); | |
16f1b9ee | 1483 | |
d62a17ae | 1484 | w += stream_putf(s, iflp->pkt_loss); |
1485 | w += stream_putf(s, iflp->res_bw); | |
1486 | w += stream_putf(s, iflp->ava_bw); | |
1487 | w += stream_putf(s, iflp->use_bw); | |
16f1b9ee | 1488 | |
d62a17ae | 1489 | return w; |
16f1b9ee OD |
1490 | } |
1491 | ||
1492 | /* | |
0a589359 | 1493 | * format of message for address additon is: |
1494 | * 0 | |
1495 | * 0 1 2 3 4 5 6 7 | |
1496 | * +-+-+-+-+-+-+-+-+ | |
1497 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or | |
1498 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE | |
1499 | * | | | |
1500 | * + + | |
1501 | * | ifindex | | |
1502 | * + + | |
1503 | * | | | |
1504 | * + + | |
1505 | * | | | |
1506 | * +-+-+-+-+-+-+-+-+ | |
1507 | * | ifc_flags | flags for connected address | |
1508 | * +-+-+-+-+-+-+-+-+ | |
1509 | * | addr_family | | |
1510 | * +-+-+-+-+-+-+-+-+ | |
1511 | * | addr... | | |
1512 | * : : | |
1513 | * | | | |
1514 | * +-+-+-+-+-+-+-+-+ | |
1515 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. | |
1516 | * +-+-+-+-+-+-+-+-+ | |
1517 | * | daddr.. | | |
1518 | * : : | |
1519 | * | | | |
1520 | * +-+-+-+-+-+-+-+-+ | |
0a589359 | 1521 | */ |
1522 | ||
d62a17ae | 1523 | static int memconstant(const void *s, int c, size_t n) |
3fb9cd6e | 1524 | { |
d62a17ae | 1525 | const u_char *p = s; |
3fb9cd6e | 1526 | |
d62a17ae | 1527 | while (n-- > 0) |
1528 | if (*p++ != c) | |
1529 | return 0; | |
1530 | return 1; | |
3fb9cd6e | 1531 | } |
1532 | ||
d5a5c8f0 | 1533 | |
d62a17ae | 1534 | struct connected *zebra_interface_address_read(int type, struct stream *s, |
1535 | vrf_id_t vrf_id) | |
718e3744 | 1536 | { |
d62a17ae | 1537 | ifindex_t ifindex; |
1538 | struct interface *ifp; | |
1539 | struct connected *ifc; | |
1540 | struct prefix p, d, *dp; | |
1541 | int plen; | |
1542 | u_char ifc_flags; | |
1543 | ||
1544 | memset(&p, 0, sizeof(p)); | |
1545 | memset(&d, 0, sizeof(d)); | |
1546 | ||
1547 | /* Get interface index. */ | |
1548 | ifindex = stream_getl(s); | |
1549 | ||
1550 | /* Lookup index. */ | |
1551 | ifp = if_lookup_by_index(ifindex, vrf_id); | |
1552 | if (ifp == NULL) { | |
1553 | zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d", | |
1554 | (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL", | |
1555 | ifindex, vrf_id); | |
1556 | return NULL; | |
1557 | } | |
1558 | ||
1559 | /* Fetch flag. */ | |
1560 | ifc_flags = stream_getc(s); | |
1561 | ||
1562 | /* Fetch interface address. */ | |
1563 | d.family = p.family = stream_getc(s); | |
1564 | plen = prefix_blen(&d); | |
1565 | ||
1566 | zclient_stream_get_prefix(s, &p); | |
1567 | ||
1568 | /* Fetch destination address. */ | |
1569 | stream_get(&d.u.prefix, s, plen); | |
1570 | ||
1571 | /* N.B. NULL destination pointers are encoded as all zeroes */ | |
1572 | dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d; | |
1573 | ||
1574 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) { | |
1575 | ifc = connected_lookup_prefix_exact(ifp, &p); | |
1576 | if (!ifc) { | |
1577 | /* N.B. NULL destination pointers are encoded as all | |
1578 | * zeroes */ | |
1579 | ifc = connected_add_by_prefix(ifp, &p, dp); | |
1580 | } | |
1581 | if (ifc) { | |
1582 | ifc->flags = ifc_flags; | |
1583 | if (ifc->destination) | |
1584 | ifc->destination->prefixlen = | |
1585 | ifc->address->prefixlen; | |
1586 | else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) { | |
1587 | /* carp interfaces on OpenBSD with 0.0.0.0/0 as | |
1588 | * "peer" */ | |
1589 | char buf[PREFIX_STRLEN]; | |
1590 | zlog_warn( | |
1591 | "warning: interface %s address %s " | |
1592 | "with peer flag set, but no peer address!", | |
9d303b37 DL |
1593 | ifp->name, prefix2str(ifc->address, buf, |
1594 | sizeof buf)); | |
d62a17ae | 1595 | UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER); |
1596 | } | |
1597 | } | |
1598 | } else { | |
1599 | assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE); | |
1600 | ifc = connected_delete_by_prefix(ifp, &p); | |
1601 | } | |
1602 | ||
1603 | return ifc; | |
718e3744 | 1604 | } |
0a589359 | 1605 | |
a80beece DS |
1606 | /* |
1607 | * format of message for neighbor connected address is: | |
1608 | * 0 | |
1609 | * 0 1 2 3 4 5 6 7 | |
1610 | * +-+-+-+-+-+-+-+-+ | |
1611 | * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or | |
1612 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE | |
1613 | * | | | |
1614 | * + + | |
1615 | * | ifindex | | |
1616 | * + + | |
1617 | * | | | |
1618 | * + + | |
1619 | * | | | |
1620 | * +-+-+-+-+-+-+-+-+ | |
1621 | * | addr_family | | |
1622 | * +-+-+-+-+-+-+-+-+ | |
1623 | * | addr... | | |
1624 | * : : | |
1625 | * | | | |
1626 | * +-+-+-+-+-+-+-+-+ | |
1627 | * | addr_len | len of addr. | |
1628 | * +-+-+-+-+-+-+-+-+ | |
1629 | */ | |
1630 | struct nbr_connected * | |
d62a17ae | 1631 | zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id) |
a80beece | 1632 | { |
d62a17ae | 1633 | unsigned int ifindex; |
1634 | struct interface *ifp; | |
1635 | struct prefix p; | |
1636 | struct nbr_connected *ifc; | |
1637 | ||
1638 | /* Get interface index. */ | |
1639 | ifindex = stream_getl(s); | |
1640 | ||
1641 | /* Lookup index. */ | |
1642 | ifp = if_lookup_by_index(ifindex, vrf_id); | |
1643 | if (ifp == NULL) { | |
1644 | zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d", | |
1645 | (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD" | |
1646 | : "DELETE", | |
1647 | ifindex, vrf_id); | |
1648 | return NULL; | |
1649 | } | |
1650 | ||
1651 | p.family = stream_getc(s); | |
1652 | stream_get(&p.u.prefix, s, prefix_blen(&p)); | |
1653 | p.prefixlen = stream_getc(s); | |
1654 | ||
1655 | if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) { | |
1656 | /* Currently only supporting P2P links, so any new RA source | |
1657 | address is | |
1658 | considered as the replacement of the previously learnt | |
1659 | Link-Local address. */ | |
1660 | if (!(ifc = listnode_head(ifp->nbr_connected))) { | |
1661 | ifc = nbr_connected_new(); | |
1662 | ifc->address = prefix_new(); | |
1663 | ifc->ifp = ifp; | |
1664 | listnode_add(ifp->nbr_connected, ifc); | |
1665 | } | |
1666 | ||
1667 | prefix_copy(ifc->address, &p); | |
1668 | } else { | |
1669 | assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE); | |
1670 | ||
1671 | ifc = nbr_connected_check(ifp, &p); | |
1672 | if (ifc) | |
1673 | listnode_delete(ifp->nbr_connected, ifc); | |
1674 | } | |
1675 | ||
1676 | return ifc; | |
a80beece | 1677 | } |
6b0655a2 | 1678 | |
d62a17ae | 1679 | struct interface *zebra_interface_vrf_update_read(struct stream *s, |
1680 | vrf_id_t vrf_id, | |
1681 | vrf_id_t *new_vrf_id) | |
c8e264b6 | 1682 | { |
d62a17ae | 1683 | unsigned int ifindex; |
1684 | struct interface *ifp; | |
a9ff90c4 | 1685 | vrf_id_t new_id; |
d62a17ae | 1686 | |
1687 | /* Get interface index. */ | |
1688 | ifindex = stream_getl(s); | |
1689 | ||
1690 | /* Lookup interface. */ | |
1691 | ifp = if_lookup_by_index(ifindex, vrf_id); | |
1692 | if (ifp == NULL) { | |
1693 | zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d", | |
1694 | ifindex, vrf_id); | |
1695 | return NULL; | |
1696 | } | |
1697 | ||
1698 | /* Fetch new VRF Id. */ | |
1699 | new_id = stream_getw(s); | |
1700 | ||
1701 | *new_vrf_id = new_id; | |
1702 | return ifp; | |
c8e264b6 | 1703 | } |
5c7ef8dc | 1704 | |
1705 | /* filter unwanted messages until the expected one arrives */ | |
d62a17ae | 1706 | static int zclient_read_sync_response(struct zclient *zclient, |
1707 | u_int16_t expected_cmd) | |
5c7ef8dc | 1708 | { |
d62a17ae | 1709 | struct stream *s; |
c31a793b | 1710 | u_int16_t size = -1; |
d62a17ae | 1711 | u_char marker; |
1712 | u_char version; | |
1713 | vrf_id_t vrf_id; | |
1714 | u_int16_t cmd; | |
1715 | fd_set readfds; | |
1716 | int ret; | |
1717 | ||
1718 | ret = 0; | |
1719 | cmd = expected_cmd + 1; | |
1720 | while (ret == 0 && cmd != expected_cmd) { | |
1721 | s = zclient->ibuf; | |
1722 | stream_reset(s); | |
1723 | ||
1724 | /* wait until response arrives */ | |
1725 | FD_ZERO(&readfds); | |
1726 | FD_SET(zclient->sock, &readfds); | |
1727 | select(zclient->sock + 1, &readfds, NULL, NULL, NULL); | |
1728 | if (!FD_ISSET(zclient->sock, &readfds)) | |
1729 | continue; | |
1730 | /* read response */ | |
1731 | ret = zclient_read_header(s, zclient->sock, &size, &marker, | |
1732 | &version, &vrf_id, &cmd); | |
1733 | if (zclient_debug) | |
1734 | zlog_debug("%s: Response (%d bytes) received", __func__, | |
1735 | size); | |
1736 | } | |
1737 | if (ret != 0) { | |
1738 | zlog_err("%s: Invalid Sync Message Reply", __func__); | |
1739 | return -1; | |
1740 | } | |
1741 | ||
1742 | return 0; | |
5c7ef8dc | 1743 | } |
fea12efb | 1744 | /** |
1745 | * Connect to label manager in a syncronous way | |
1746 | * | |
1747 | * It first writes the request to zcient output buffer and then | |
1748 | * immediately reads the answer from the input buffer. | |
1749 | * | |
1750 | * @param zclient Zclient used to connect to label manager (zebra) | |
1751 | * @result Result of response | |
1752 | */ | |
d62a17ae | 1753 | int lm_label_manager_connect(struct zclient *zclient) |
fea12efb | 1754 | { |
d62a17ae | 1755 | int ret; |
1756 | struct stream *s; | |
1757 | u_char result; | |
1758 | ||
1759 | if (zclient_debug) | |
1760 | zlog_debug("Connecting to Label Manager"); | |
1761 | ||
1762 | if (zclient->sock < 0) | |
1763 | return -1; | |
1764 | ||
1765 | /* send request */ | |
1766 | s = zclient->obuf; | |
1767 | stream_reset(s); | |
1768 | zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT); | |
1769 | ||
1770 | /* proto */ | |
1771 | stream_putc(s, zclient->redist_default); | |
1772 | /* instance */ | |
1773 | stream_putw(s, zclient->instance); | |
1774 | ||
1775 | /* Put length at the first point of the stream. */ | |
1776 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1777 | ||
1778 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
1779 | if (ret < 0) { | |
1780 | zlog_err("%s: can't write to zclient->sock", __func__); | |
1781 | close(zclient->sock); | |
1782 | zclient->sock = -1; | |
1783 | return -1; | |
1784 | } | |
1785 | if (ret == 0) { | |
1786 | zlog_err("%s: zclient->sock connection closed", __func__); | |
1787 | close(zclient->sock); | |
1788 | zclient->sock = -1; | |
1789 | return -1; | |
1790 | } | |
1791 | if (zclient_debug) | |
1792 | zlog_debug("%s: Label manager connect request (%d bytes) sent", | |
1793 | __func__, ret); | |
1794 | ||
1795 | /* read response */ | |
1796 | if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT) | |
1797 | != 0) | |
1798 | return -1; | |
1799 | ||
1800 | /* result */ | |
1801 | s = zclient->ibuf; | |
1802 | result = stream_getc(s); | |
1803 | if (zclient_debug) | |
1804 | zlog_debug( | |
1805 | "%s: Label Manager connect response received, result %u", | |
1806 | __func__, result); | |
1807 | ||
1808 | return (int)result; | |
fea12efb | 1809 | } |
1810 | ||
1811 | /** | |
1812 | * Function to request a label chunk in a syncronous way | |
1813 | * | |
1814 | * It first writes the request to zlcient output buffer and then | |
1815 | * immediately reads the answer from the input buffer. | |
1816 | * | |
1817 | * @param zclient Zclient used to connect to label manager (zebra) | |
1818 | * @param keep Avoid garbage collection | |
1819 | * @param chunk_size Amount of labels requested | |
1820 | * @param start To write first assigned chunk label to | |
1821 | * @param end To write last assigned chunk label to | |
1822 | * @result 0 on success, -1 otherwise | |
1823 | */ | |
d62a17ae | 1824 | int lm_get_label_chunk(struct zclient *zclient, u_char keep, |
1825 | uint32_t chunk_size, uint32_t *start, uint32_t *end) | |
fea12efb | 1826 | { |
d62a17ae | 1827 | int ret; |
1828 | struct stream *s; | |
1829 | u_char response_keep; | |
1830 | ||
1831 | if (zclient_debug) | |
1832 | zlog_debug("Getting Label Chunk"); | |
1833 | ||
1834 | if (zclient->sock < 0) | |
1835 | return -1; | |
1836 | ||
1837 | /* send request */ | |
1838 | s = zclient->obuf; | |
1839 | stream_reset(s); | |
1840 | zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT); | |
1841 | /* keep */ | |
1842 | stream_putc(s, keep); | |
1843 | /* chunk size */ | |
1844 | stream_putl(s, chunk_size); | |
1845 | /* Put length at the first point of the stream. */ | |
1846 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1847 | ||
1848 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
1849 | if (ret < 0) { | |
1850 | zlog_err("%s: can't write to zclient->sock", __func__); | |
1851 | close(zclient->sock); | |
1852 | zclient->sock = -1; | |
1853 | return -1; | |
1854 | } | |
1855 | if (ret == 0) { | |
1856 | zlog_err("%s: zclient->sock connection closed", __func__); | |
1857 | close(zclient->sock); | |
1858 | zclient->sock = -1; | |
1859 | return -1; | |
1860 | } | |
1861 | if (zclient_debug) | |
1862 | zlog_debug("%s: Label chunk request (%d bytes) sent", __func__, | |
1863 | ret); | |
1864 | ||
1865 | /* read response */ | |
1866 | if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0) | |
1867 | return -1; | |
1868 | ||
1869 | s = zclient->ibuf; | |
1870 | /* keep */ | |
1871 | response_keep = stream_getc(s); | |
1872 | /* start and end labels */ | |
1873 | *start = stream_getl(s); | |
1874 | *end = stream_getl(s); | |
1875 | ||
1876 | /* not owning this response */ | |
1877 | if (keep != response_keep) { | |
1878 | zlog_err( | |
1879 | "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u", | |
1880 | __func__, *start, *end, keep, response_keep); | |
1881 | } | |
1882 | /* sanity */ | |
1883 | if (*start > *end || *start < MPLS_MIN_UNRESERVED_LABEL | |
1884 | || *end > MPLS_MAX_UNRESERVED_LABEL) { | |
1885 | zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start, | |
1886 | *end); | |
1887 | return -1; | |
1888 | } | |
1889 | ||
1890 | if (zclient_debug) | |
1891 | zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end, | |
1892 | response_keep); | |
1893 | ||
1894 | return 0; | |
fea12efb | 1895 | } |
1896 | ||
1897 | /** | |
1898 | * Function to release a label chunk | |
1899 | * | |
1900 | * @param zclient Zclient used to connect to label manager (zebra) | |
1901 | * @param start First label of chunk | |
1902 | * @param end Last label of chunk | |
1903 | * @result 0 on success, -1 otherwise | |
1904 | */ | |
d62a17ae | 1905 | int lm_release_label_chunk(struct zclient *zclient, uint32_t start, |
1906 | uint32_t end) | |
fea12efb | 1907 | { |
d62a17ae | 1908 | int ret; |
1909 | struct stream *s; | |
1910 | ||
1911 | if (zclient_debug) | |
1912 | zlog_debug("Releasing Label Chunk"); | |
1913 | ||
1914 | if (zclient->sock < 0) | |
1915 | return -1; | |
1916 | ||
1917 | /* send request */ | |
1918 | s = zclient->obuf; | |
1919 | stream_reset(s); | |
1920 | zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT); | |
1921 | ||
1922 | /* start */ | |
1923 | stream_putl(s, start); | |
1924 | /* end */ | |
1925 | stream_putl(s, end); | |
1926 | ||
1927 | /* Put length at the first point of the stream. */ | |
1928 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1929 | ||
1930 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
1931 | if (ret < 0) { | |
1932 | zlog_err("%s: can't write to zclient->sock", __func__); | |
1933 | close(zclient->sock); | |
1934 | zclient->sock = -1; | |
1935 | return -1; | |
1936 | } | |
1937 | if (ret == 0) { | |
1938 | zlog_err("%s: zclient->sock connection closed", __func__); | |
1939 | close(zclient->sock); | |
1940 | zclient->sock = -1; | |
1941 | return -1; | |
1942 | } | |
1943 | ||
1944 | return 0; | |
fea12efb | 1945 | } |
c8e264b6 | 1946 | |
6833ae01 | 1947 | int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw) |
1948 | { | |
1949 | struct stream *s; | |
1950 | ||
1951 | /* Reset stream. */ | |
1952 | s = zclient->obuf; | |
1953 | stream_reset(s); | |
1954 | ||
1955 | zclient_create_header(s, command, VRF_DEFAULT); | |
1956 | stream_write(s, pw->ifname, IF_NAMESIZE); | |
1957 | stream_putl(s, pw->ifindex); | |
1958 | ||
1959 | /* Put type */ | |
1960 | stream_putl(s, pw->type); | |
1961 | ||
1962 | /* Put nexthop */ | |
1963 | stream_putl(s, pw->af); | |
1964 | switch (pw->af) { | |
1965 | case AF_INET: | |
1966 | stream_put_in_addr(s, &pw->nexthop.ipv4); | |
1967 | break; | |
1968 | case AF_INET6: | |
1969 | stream_write(s, (u_char *)&pw->nexthop.ipv6, 16); | |
1970 | break; | |
1971 | default: | |
1972 | zlog_err("%s: unknown af", __func__); | |
1973 | return -1; | |
1974 | } | |
1975 | ||
1976 | /* Put labels */ | |
1977 | stream_putl(s, pw->local_label); | |
1978 | stream_putl(s, pw->remote_label); | |
1979 | ||
1980 | /* Put flags */ | |
1981 | stream_putc(s, pw->flags); | |
1982 | ||
1983 | /* Protocol specific fields */ | |
1984 | stream_write(s, &pw->data, sizeof(union pw_protocol_fields)); | |
1985 | ||
1986 | /* Put length at the first point of the stream. */ | |
1987 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1988 | ||
1989 | return zclient_send_message(zclient); | |
1990 | } | |
1991 | ||
1992 | /* | |
1993 | * Receive PW status update from Zebra and send it to LDE process. | |
1994 | */ | |
1995 | void zebra_read_pw_status_update(int command, struct zclient *zclient, | |
1996 | zebra_size_t length, vrf_id_t vrf_id, | |
1997 | struct zapi_pw_status *pw) | |
1998 | { | |
1999 | struct stream *s; | |
2000 | ||
2001 | memset(pw, 0, sizeof(struct zapi_pw_status)); | |
2002 | s = zclient->ibuf; | |
2003 | ||
2004 | /* Get data. */ | |
2005 | stream_get(pw->ifname, s, IF_NAMESIZE); | |
2006 | pw->ifindex = stream_getl(s); | |
2007 | pw->status = stream_getl(s); | |
2008 | } | |
2009 | ||
718e3744 | 2010 | /* Zebra client message read function. */ |
d62a17ae | 2011 | static int zclient_read(struct thread *thread) |
718e3744 | 2012 | { |
d62a17ae | 2013 | size_t already; |
2014 | uint16_t length, command; | |
2015 | uint8_t marker, version; | |
2016 | vrf_id_t vrf_id; | |
2017 | struct zclient *zclient; | |
2018 | ||
2019 | /* Get socket to zebra. */ | |
2020 | zclient = THREAD_ARG(thread); | |
2021 | zclient->t_read = NULL; | |
2022 | ||
2023 | /* Read zebra header (if we don't have it already). */ | |
2024 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) { | |
2025 | ssize_t nbyte; | |
2026 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
2027 | ZEBRA_HEADER_SIZE - already)) | |
2028 | == 0) | |
2029 | || (nbyte == -1)) { | |
2030 | if (zclient_debug) | |
2031 | zlog_debug( | |
2032 | "zclient connection closed socket [%d].", | |
2033 | zclient->sock); | |
2034 | return zclient_failed(zclient); | |
2035 | } | |
2036 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) { | |
2037 | /* Try again later. */ | |
2038 | zclient_event(ZCLIENT_READ, zclient); | |
2039 | return 0; | |
2040 | } | |
2041 | already = ZEBRA_HEADER_SIZE; | |
634f9ea2 | 2042 | } |
d62a17ae | 2043 | |
2044 | /* Reset to read from the beginning of the incoming packet. */ | |
2045 | stream_set_getp(zclient->ibuf, 0); | |
2046 | ||
2047 | /* Fetch header values. */ | |
2048 | length = stream_getw(zclient->ibuf); | |
2049 | marker = stream_getc(zclient->ibuf); | |
2050 | version = stream_getc(zclient->ibuf); | |
a9ff90c4 | 2051 | vrf_id = stream_getl(zclient->ibuf); |
d62a17ae | 2052 | command = stream_getw(zclient->ibuf); |
2053 | ||
2054 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) { | |
2055 | zlog_err( | |
2056 | "%s: socket %d version mismatch, marker %d, version %d", | |
2057 | __func__, zclient->sock, marker, version); | |
2058 | return zclient_failed(zclient); | |
634f9ea2 | 2059 | } |
d62a17ae | 2060 | |
2061 | if (length < ZEBRA_HEADER_SIZE) { | |
2062 | zlog_err("%s: socket %d message length %u is less than %d ", | |
2063 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); | |
2064 | return zclient_failed(zclient); | |
634f9ea2 | 2065 | } |
d62a17ae | 2066 | |
2067 | /* Length check. */ | |
2068 | if (length > STREAM_SIZE(zclient->ibuf)) { | |
2069 | struct stream *ns; | |
2070 | zlog_warn( | |
2071 | "%s: message size %u exceeds buffer size %lu, expanding...", | |
2072 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); | |
2073 | ns = stream_new(length); | |
2074 | stream_copy(ns, zclient->ibuf); | |
2075 | stream_free(zclient->ibuf); | |
2076 | zclient->ibuf = ns; | |
2077 | } | |
2078 | ||
2079 | /* Read rest of zebra packet. */ | |
2080 | if (already < length) { | |
2081 | ssize_t nbyte; | |
2082 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
2083 | length - already)) | |
2084 | == 0) | |
2085 | || (nbyte == -1)) { | |
2086 | if (zclient_debug) | |
2087 | zlog_debug( | |
2088 | "zclient connection closed socket [%d].", | |
2089 | zclient->sock); | |
2090 | return zclient_failed(zclient); | |
2091 | } | |
2092 | if (nbyte != (ssize_t)(length - already)) { | |
2093 | /* Try again later. */ | |
2094 | zclient_event(ZCLIENT_READ, zclient); | |
2095 | return 0; | |
2096 | } | |
2097 | } | |
2098 | ||
2099 | length -= ZEBRA_HEADER_SIZE; | |
2100 | ||
2101 | if (zclient_debug) | |
2102 | zlog_debug("zclient 0x%p command 0x%x VRF %u\n", | |
2103 | (void *)zclient, command, vrf_id); | |
2104 | ||
2105 | switch (command) { | |
2106 | case ZEBRA_ROUTER_ID_UPDATE: | |
2107 | if (zclient->router_id_update) | |
2108 | (*zclient->router_id_update)(command, zclient, length, | |
2109 | vrf_id); | |
2110 | break; | |
2111 | case ZEBRA_VRF_ADD: | |
2112 | zclient_vrf_add(zclient, vrf_id); | |
2113 | break; | |
2114 | case ZEBRA_VRF_DELETE: | |
2115 | zclient_vrf_delete(zclient, vrf_id); | |
2116 | break; | |
2117 | case ZEBRA_INTERFACE_ADD: | |
2118 | if (zclient->interface_add) | |
2119 | (*zclient->interface_add)(command, zclient, length, | |
2120 | vrf_id); | |
2121 | break; | |
2122 | case ZEBRA_INTERFACE_DELETE: | |
2123 | if (zclient->interface_delete) | |
2124 | (*zclient->interface_delete)(command, zclient, length, | |
2125 | vrf_id); | |
2126 | break; | |
2127 | case ZEBRA_INTERFACE_ADDRESS_ADD: | |
2128 | if (zclient->interface_address_add) | |
2129 | (*zclient->interface_address_add)(command, zclient, | |
2130 | length, vrf_id); | |
2131 | break; | |
2132 | case ZEBRA_INTERFACE_ADDRESS_DELETE: | |
2133 | if (zclient->interface_address_delete) | |
2134 | (*zclient->interface_address_delete)(command, zclient, | |
2135 | length, vrf_id); | |
2136 | break; | |
2137 | case ZEBRA_INTERFACE_BFD_DEST_UPDATE: | |
2138 | if (zclient->interface_bfd_dest_update) | |
2139 | (*zclient->interface_bfd_dest_update)(command, zclient, | |
2140 | length, vrf_id); | |
2141 | break; | |
2142 | case ZEBRA_INTERFACE_NBR_ADDRESS_ADD: | |
2143 | if (zclient->interface_nbr_address_add) | |
2144 | (*zclient->interface_nbr_address_add)(command, zclient, | |
2145 | length, vrf_id); | |
2146 | break; | |
2147 | case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE: | |
2148 | if (zclient->interface_nbr_address_delete) | |
2149 | (*zclient->interface_nbr_address_delete)( | |
2150 | command, zclient, length, vrf_id); | |
2151 | break; | |
2152 | case ZEBRA_INTERFACE_UP: | |
2153 | if (zclient->interface_up) | |
2154 | (*zclient->interface_up)(command, zclient, length, | |
2155 | vrf_id); | |
2156 | break; | |
2157 | case ZEBRA_INTERFACE_DOWN: | |
2158 | if (zclient->interface_down) | |
2159 | (*zclient->interface_down)(command, zclient, length, | |
2160 | vrf_id); | |
2161 | break; | |
2162 | case ZEBRA_INTERFACE_VRF_UPDATE: | |
2163 | if (zclient->interface_vrf_update) | |
2164 | (*zclient->interface_vrf_update)(command, zclient, | |
2165 | length, vrf_id); | |
2166 | break; | |
2167 | case ZEBRA_NEXTHOP_UPDATE: | |
2168 | if (zclient_debug) | |
2169 | zlog_debug("zclient rcvd nexthop update\n"); | |
2170 | if (zclient->nexthop_update) | |
2171 | (*zclient->nexthop_update)(command, zclient, length, | |
2172 | vrf_id); | |
2173 | break; | |
2174 | case ZEBRA_IMPORT_CHECK_UPDATE: | |
2175 | if (zclient_debug) | |
2176 | zlog_debug("zclient rcvd import check update\n"); | |
2177 | if (zclient->import_check_update) | |
2178 | (*zclient->import_check_update)(command, zclient, | |
2179 | length, vrf_id); | |
2180 | break; | |
2181 | case ZEBRA_BFD_DEST_REPLAY: | |
2182 | if (zclient->bfd_dest_replay) | |
2183 | (*zclient->bfd_dest_replay)(command, zclient, length, | |
2184 | vrf_id); | |
2185 | break; | |
74489921 RW |
2186 | case ZEBRA_REDISTRIBUTE_ROUTE_ADD: |
2187 | if (zclient->redistribute_route_add) | |
2188 | (*zclient->redistribute_route_add)(command, zclient, | |
2189 | length, vrf_id); | |
d62a17ae | 2190 | break; |
74489921 RW |
2191 | case ZEBRA_REDISTRIBUTE_ROUTE_DEL: |
2192 | if (zclient->redistribute_route_del) | |
2193 | (*zclient->redistribute_route_del)(command, zclient, | |
2194 | length, vrf_id); | |
d62a17ae | 2195 | break; |
2196 | case ZEBRA_INTERFACE_LINK_PARAMS: | |
2197 | if (zclient->interface_link_params) | |
2198 | (*zclient->interface_link_params)(command, zclient, | |
2199 | length); | |
2200 | break; | |
2201 | case ZEBRA_FEC_UPDATE: | |
2202 | if (zclient_debug) | |
2203 | zlog_debug("zclient rcvd fec update\n"); | |
2204 | if (zclient->fec_update) | |
2205 | (*zclient->fec_update)(command, zclient, length); | |
2206 | break; | |
2207 | case ZEBRA_VNI_ADD: | |
2208 | if (zclient->local_vni_add) | |
2209 | (*zclient->local_vni_add)(command, zclient, length, | |
2210 | vrf_id); | |
2211 | break; | |
2212 | case ZEBRA_VNI_DEL: | |
2213 | if (zclient->local_vni_del) | |
2214 | (*zclient->local_vni_del)(command, zclient, length, | |
2215 | vrf_id); | |
2216 | break; | |
b7cfce93 MK |
2217 | case ZEBRA_L3VNI_ADD: |
2218 | if (zclient->local_l3vni_add) | |
2219 | (*zclient->local_l3vni_add)(command, zclient, length, | |
2220 | vrf_id); | |
2221 | break; | |
2222 | case ZEBRA_L3VNI_DEL: | |
2223 | if (zclient->local_l3vni_del) | |
2224 | (*zclient->local_l3vni_del)(command, zclient, length, | |
2225 | vrf_id); | |
2226 | break; | |
d62a17ae | 2227 | case ZEBRA_MACIP_ADD: |
2228 | if (zclient->local_macip_add) | |
2229 | (*zclient->local_macip_add)(command, zclient, length, | |
2230 | vrf_id); | |
2231 | break; | |
2232 | case ZEBRA_MACIP_DEL: | |
2233 | if (zclient->local_macip_del) | |
2234 | (*zclient->local_macip_del)(command, zclient, length, | |
2235 | vrf_id); | |
2236 | break; | |
31310b25 MK |
2237 | case ZEBRA_IP_PREFIX_ROUTE_ADD: |
2238 | if (zclient->local_ip_prefix_add) | |
2239 | (*zclient->local_ip_prefix_add)(command, zclient, | |
2240 | length, vrf_id); | |
2241 | break; | |
2242 | case ZEBRA_IP_PREFIX_ROUTE_DEL: | |
2243 | if (zclient->local_ip_prefix_del) | |
2244 | (*zclient->local_ip_prefix_del)(command, zclient, | |
2245 | length, vrf_id); | |
2246 | break; | |
6833ae01 | 2247 | case ZEBRA_PW_STATUS_UPDATE: |
2248 | if (zclient->pw_status_update) | |
2249 | (*zclient->pw_status_update)(command, zclient, length, | |
2250 | vrf_id); | |
2251 | break; | |
7ea7b86e DS |
2252 | case ZEBRA_ROUTE_NOTIFY_OWNER: |
2253 | if (zclient->notify_owner) | |
2254 | (*zclient->notify_owner)(command, zclient, | |
2255 | length, vrf_id); | |
2256 | break; | |
d62a17ae | 2257 | default: |
2258 | break; | |
634f9ea2 | 2259 | } |
d62a17ae | 2260 | |
2261 | if (zclient->sock < 0) | |
2262 | /* Connection was closed during packet processing. */ | |
2263 | return -1; | |
2264 | ||
2265 | /* Register read thread. */ | |
2266 | stream_reset(zclient->ibuf); | |
2267 | zclient_event(ZCLIENT_READ, zclient); | |
2268 | ||
2269 | return 0; | |
718e3744 | 2270 | } |
2271 | ||
d62a17ae | 2272 | void zclient_redistribute(int command, struct zclient *zclient, afi_t afi, |
2273 | int type, u_short instance, vrf_id_t vrf_id) | |
718e3744 | 2274 | { |
718e3744 | 2275 | |
d62a17ae | 2276 | if (instance) { |
2277 | if (command == ZEBRA_REDISTRIBUTE_ADD) { | |
2278 | if (redist_check_instance( | |
2279 | &zclient->mi_redist[afi][type], instance)) | |
2280 | return; | |
2281 | redist_add_instance(&zclient->mi_redist[afi][type], | |
2282 | instance); | |
2283 | } else { | |
2284 | if (!redist_check_instance( | |
2285 | &zclient->mi_redist[afi][type], instance)) | |
2286 | return; | |
2287 | redist_del_instance(&zclient->mi_redist[afi][type], | |
2288 | instance); | |
2289 | } | |
2290 | ||
2291 | } else { | |
2292 | if (command == ZEBRA_REDISTRIBUTE_ADD) { | |
2293 | if (vrf_bitmap_check(zclient->redist[afi][type], | |
2294 | vrf_id)) | |
2295 | return; | |
2296 | vrf_bitmap_set(zclient->redist[afi][type], vrf_id); | |
2297 | } else { | |
2298 | if (!vrf_bitmap_check(zclient->redist[afi][type], | |
2299 | vrf_id)) | |
2300 | return; | |
2301 | vrf_bitmap_unset(zclient->redist[afi][type], vrf_id); | |
2302 | } | |
2303 | } | |
2304 | ||
2305 | if (zclient->sock > 0) | |
2306 | zebra_redistribute_send(command, zclient, afi, type, instance, | |
2307 | vrf_id); | |
718e3744 | 2308 | } |
2309 | ||
718e3744 | 2310 | |
d62a17ae | 2311 | void zclient_redistribute_default(int command, struct zclient *zclient, |
2312 | vrf_id_t vrf_id) | |
718e3744 | 2313 | { |
718e3744 | 2314 | |
d62a17ae | 2315 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) { |
2316 | if (vrf_bitmap_check(zclient->default_information, vrf_id)) | |
2317 | return; | |
2318 | vrf_bitmap_set(zclient->default_information, vrf_id); | |
2319 | } else { | |
2320 | if (!vrf_bitmap_check(zclient->default_information, vrf_id)) | |
2321 | return; | |
2322 | vrf_bitmap_unset(zclient->default_information, vrf_id); | |
2323 | } | |
2324 | ||
2325 | if (zclient->sock > 0) | |
2326 | zebra_message_send(zclient, command, vrf_id); | |
718e3744 | 2327 | } |
2328 | ||
d62a17ae | 2329 | static void zclient_event(enum event event, struct zclient *zclient) |
718e3744 | 2330 | { |
d62a17ae | 2331 | switch (event) { |
2332 | case ZCLIENT_SCHEDULE: | |
2333 | thread_add_event(zclient->master, zclient_connect, zclient, 0, | |
2334 | &zclient->t_connect); | |
2335 | break; | |
2336 | case ZCLIENT_CONNECT: | |
2337 | if (zclient_debug) | |
2338 | zlog_debug( | |
2339 | "zclient connect failures: %d schedule interval is now %d", | |
2340 | zclient->fail, zclient->fail < 3 ? 10 : 60); | |
2341 | thread_add_timer(zclient->master, zclient_connect, zclient, | |
2342 | zclient->fail < 3 ? 10 : 60, | |
2343 | &zclient->t_connect); | |
2344 | break; | |
2345 | case ZCLIENT_READ: | |
2346 | zclient->t_read = NULL; | |
2347 | thread_add_read(zclient->master, zclient_read, zclient, | |
2348 | zclient->sock, &zclient->t_read); | |
2349 | break; | |
2350 | } | |
718e3744 | 2351 | } |
b5114685 | 2352 | |
e0ae31b8 DS |
2353 | void zclient_interface_set_master(struct zclient *client, |
2354 | struct interface *master, | |
2355 | struct interface *slave) | |
2356 | { | |
2357 | struct stream *s; | |
2358 | ||
2359 | s = client->obuf; | |
2360 | stream_reset(s); | |
2361 | ||
2362 | zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id); | |
2363 | ||
a9ff90c4 | 2364 | stream_putl(s, master->vrf_id); |
e0ae31b8 | 2365 | stream_putl(s, master->ifindex); |
a9ff90c4 | 2366 | stream_putl(s, slave->vrf_id); |
e0ae31b8 DS |
2367 | stream_putl(s, slave->ifindex); |
2368 | ||
2369 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2370 | zclient_send_message(client); | |
2371 | } |