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