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