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