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