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