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