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