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