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