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