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