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