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