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