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