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