]> git.proxmox.com Git - mirror_frr.git/blame - lib/zclient.c
*: Add tableid the route entry was sent to
[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{
1216 STREAM_GET(note, s, sizeof(*note));
1217
1218 STREAM_GETC(s, p->family);
1219 STREAM_GETC(s, p->prefixlen);
1220 STREAM_GET(&p->u.prefix, s,
1221 PSIZE(p->prefixlen));
28610f7e 1222 STREAM_GETL(s, *tableid);
7ea7b86e
DS
1223
1224 return true;
1225
1226stream_failure:
1227 return false;
1228}
1229
4a749e2c
DS
1230struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh)
1231{
1232 struct nexthop *n = nexthop_new();
1233
1234 n->type = znh->type;
4a7371e9 1235 n->vrf_id = znh->vrf_id;
4a749e2c
DS
1236 n->ifindex = znh->ifindex;
1237 n->gate = znh->gate;
1238
1239 /*
1240 * This function does not currently handle labels
1241 */
1242
1243 return n;
1244}
1245
1246bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr)
1247{
1248 uint32_t i;
1249
1250 memset(nhr, 0, sizeof(*nhr));
1251
1252 STREAM_GETW(s, nhr->prefix.family);
1253 STREAM_GETC(s, nhr->prefix.prefixlen);
1254 switch(nhr->prefix.family) {
1255 case AF_INET:
1256 STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1257 break;
1258 case AF_INET6:
1259 STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN);
1260 break;
1261 default:
1262 break;
1263 }
1264
1265 STREAM_GETC(s, nhr->distance);
1266 STREAM_GETL(s, nhr->metric);
1267 STREAM_GETC(s, nhr->nexthop_num);
1268
1269 for (i = 0; i < nhr->nexthop_num ; i++) {
1270 STREAM_GETC(s, nhr->nexthops[i].type);
1271 switch (nhr->nexthops[i].type) {
1272 case NEXTHOP_TYPE_IPV4:
1273 case NEXTHOP_TYPE_IPV4_IFINDEX:
1274 STREAM_GET(&nhr->nexthops[i].gate.ipv4.s_addr,
1275 s, IPV4_MAX_BYTELEN);
1276 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1277 break;
1278 case NEXTHOP_TYPE_IFINDEX:
1279 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1280 break;
1281 case NEXTHOP_TYPE_IPV6:
1282 case NEXTHOP_TYPE_IPV6_IFINDEX:
1283 STREAM_GET(&nhr->nexthops[i].gate.ipv6,
1284 s, IPV6_MAX_BYTELEN);
1285 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1286 break;
1287 case NEXTHOP_TYPE_BLACKHOLE:
1288 break;
1289 }
1290 }
1291
1292 return true;
1293stream_failure:
1294 return false;
1295}
1296
d62a17ae 1297/*
0a589359 1298 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1299 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
d62a17ae 1300 * then set/unset redist[type] in the client handle (a struct zserv) for the
0a589359 1301 * sending client
1302 */
d62a17ae 1303int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1304 int type, u_short instance, vrf_id_t vrf_id)
718e3744 1305{
d62a17ae 1306 struct stream *s;
1307
1308 s = zclient->obuf;
1309 stream_reset(s);
1310
1311 zclient_create_header(s, command, vrf_id);
1312 stream_putc(s, afi);
1313 stream_putc(s, type);
1314 stream_putw(s, instance);
1315
1316 stream_putw_at(s, 0, stream_get_endp(s));
1317
1318 return zclient_send_message(zclient);
718e3744 1319}
1320
d9178828 1321/* Get prefix in ZServ format; family should be filled in on prefix */
d62a17ae 1322static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
d9178828 1323{
d62a17ae 1324 size_t plen = prefix_blen(p);
1325 u_char c;
1326 p->prefixlen = 0;
1327
1328 if (plen == 0)
1329 return;
1330
1331 stream_get(&p->u.prefix, s, plen);
ec93aa12 1332 STREAM_GETC(s, c);
d62a17ae 1333 p->prefixlen = MIN(plen * 8, c);
ec93aa12
DS
1334
1335stream_failure:
1336 return;
d9178828
PJ
1337}
1338
18a6dce6 1339/* Router-id update from zebra daemon. */
d62a17ae 1340void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
18a6dce6 1341{
d62a17ae 1342 /* Fetch interface address. */
ec93aa12 1343 STREAM_GETC(s, rid->family);
d62a17ae 1344
1345 zclient_stream_get_prefix(s, rid);
ec93aa12
DS
1346
1347stream_failure:
1348 return;
18a6dce6 1349}
1350
718e3744 1351/* Interface addition from zebra daemon. */
d62a17ae 1352/*
0a589359 1353 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1354 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1355 * 0 1 2 3
1356 * 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 1357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1358 * | ifname |
1359 * | |
1360 * | |
1361 * | |
1362 * | |
1363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1364 * | ifindex |
1365 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1366 * | status |
0a589359 1367 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1368 * | if_flags |
c77d4546 1369 * | |
0a589359 1370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1371 * | metric |
1372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2d7f0d76
DS
1373 * | speed |
1374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1375 * | ifmtu |
1376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1377 * | ifmtu6 |
1378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1379 * | bandwidth |
1380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1381 * | Link Layer Type |
0a589359 1382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1383 * | Harware Address Length |
0a589359 1384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1385 * | Hardware Address if HW lenght different from 0 |
1386 * | ... max INTERFACE_HWADDR_MAX |
0a589359 1387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1388 * | Link_params? | Whether a link-params follows: 1 or 0.
0a589359 1389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1390 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1391 * | .... (struct if_link_params). |
0a589359 1392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1393 */
1394
d62a17ae 1395static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1892f15e 1396{
d62a17ae 1397 struct vrf *vrf;
1398 char vrfname_tmp[VRF_NAMSIZ];
1399 struct vrf_data data;
1892f15e 1400
d62a17ae 1401 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1402 /* Read interface name. */
1403 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1892f15e 1404
d62a17ae 1405 /* Lookup/create vrf by vrf_id. */
1406 vrf = vrf_get(vrf_id, vrfname_tmp);
1407 vrf->data = data;
1892f15e 1408
d62a17ae 1409 vrf_enable(vrf);
1892f15e
DS
1410}
1411
d62a17ae 1412static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1892f15e 1413{
d62a17ae 1414 struct vrf *vrf;
1892f15e 1415
d62a17ae 1416 /* Lookup vrf by vrf_id. */
1417 vrf = vrf_lookup_by_id(vrf_id);
1892f15e 1418
d62a17ae 1419 /*
1420 * If a routing protocol doesn't know about a
1421 * vrf that is about to be deleted. There is
1422 * no point in attempting to delete it.
1423 */
1424 if (!vrf)
1425 return;
beef1990 1426
d62a17ae 1427 vrf_delete(vrf);
1892f15e
DS
1428}
1429
d62a17ae 1430struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
718e3744 1431{
d62a17ae 1432 struct interface *ifp;
1433 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 1434
d62a17ae 1435 /* Read interface name. */
1436 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
718e3744 1437
d62a17ae 1438 /* Lookup/create interface by name. */
bcc24579 1439 ifp = if_get_by_name(ifname_tmp, vrf_id, 0);
718e3744 1440
d62a17ae 1441 zebra_interface_if_set_value(s, ifp);
718e3744 1442
d62a17ae 1443 return ifp;
718e3744 1444}
1445
d62a17ae 1446/*
0a589359 1447 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1448 * from zebra server. The format of this message is the same as
1449 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1450 * comments for zebra_interface_add_read), except that no sockaddr_dl
1451 * is sent at the tail of the message.
1452 */
d62a17ae 1453struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
718e3744 1454{
d62a17ae 1455 struct interface *ifp;
1456 char ifname_tmp[INTERFACE_NAMSIZ];
1457
1458 /* Read interface name. */
1459 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1460
1461 /* Lookup this by interface index. */
bcc24579 1462 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
d62a17ae 1463 if (ifp == NULL) {
1464 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1465 ifname_tmp, vrf_id);
1466 return NULL;
1467 }
1468
1469 zebra_interface_if_set_value(s, ifp);
1470
1471 return ifp;
718e3744 1472}
1473
d62a17ae 1474static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
16f1b9ee
OD
1475{
1476
d62a17ae 1477 if (iflp == NULL)
1478 return;
1479
1480 iflp->lp_status = stream_getl(s);
1481 iflp->te_metric = stream_getl(s);
1482 iflp->max_bw = stream_getf(s);
1483 iflp->max_rsv_bw = stream_getf(s);
1484 uint32_t bwclassnum = stream_getl(s);
1485 {
1486 unsigned int i;
1487 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1488 iflp->unrsv_bw[i] = stream_getf(s);
1489 if (i < bwclassnum)
1490 zlog_err(
1491 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1492 " - outdated library?",
1493 __func__, bwclassnum, MAX_CLASS_TYPE);
1494 }
1495 iflp->admin_grp = stream_getl(s);
1496 iflp->rmt_as = stream_getl(s);
1497 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1498
1499 iflp->av_delay = stream_getl(s);
1500 iflp->min_delay = stream_getl(s);
1501 iflp->max_delay = stream_getl(s);
1502 iflp->delay_var = stream_getl(s);
1503
1504 iflp->pkt_loss = stream_getf(s);
1505 iflp->res_bw = stream_getf(s);
1506 iflp->ava_bw = stream_getf(s);
1507 iflp->use_bw = stream_getf(s);
16f1b9ee
OD
1508}
1509
d62a17ae 1510struct interface *zebra_interface_link_params_read(struct stream *s)
16f1b9ee 1511{
d62a17ae 1512 struct if_link_params *iflp;
1513 ifindex_t ifindex;
c28e5b2a 1514
d62a17ae 1515 assert(s);
c28e5b2a 1516
d62a17ae 1517 ifindex = stream_getl(s);
16f1b9ee 1518
d62a17ae 1519 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
16f1b9ee 1520
d62a17ae 1521 if (ifp == NULL) {
1522 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__,
1523 ifindex);
1524 return NULL;
1525 }
16f1b9ee 1526
d62a17ae 1527 if ((iflp = if_link_params_get(ifp)) == NULL)
1528 return NULL;
16f1b9ee 1529
d62a17ae 1530 link_params_set_value(s, iflp);
16f1b9ee 1531
d62a17ae 1532 return ifp;
16f1b9ee
OD
1533}
1534
d62a17ae 1535void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
16f1b9ee 1536{
d62a17ae 1537 u_char link_params_status = 0;
1538
1539 /* Read interface's index. */
ff880b78 1540 if_set_index(ifp, stream_getl(s));
d62a17ae 1541 ifp->status = stream_getc(s);
1542
1543 /* Read interface's value. */
1544 ifp->flags = stream_getq(s);
1545 ifp->ptm_enable = stream_getc(s);
1546 ifp->ptm_status = stream_getc(s);
1547 ifp->metric = stream_getl(s);
1548 ifp->speed = stream_getl(s);
1549 ifp->mtu = stream_getl(s);
1550 ifp->mtu6 = stream_getl(s);
1551 ifp->bandwidth = stream_getl(s);
1552 ifp->ll_type = stream_getl(s);
1553 ifp->hw_addr_len = stream_getl(s);
1554 if (ifp->hw_addr_len)
1555 stream_get(ifp->hw_addr, s,
1556 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1557
1558 /* Read Traffic Engineering status */
1559 link_params_status = stream_getc(s);
1560 /* Then, Traffic Engineering parameters if any */
1561 if (link_params_status) {
1562 struct if_link_params *iflp = if_link_params_get(ifp);
1563 link_params_set_value(s, iflp);
1564 }
16f1b9ee
OD
1565}
1566
d62a17ae 1567size_t zebra_interface_link_params_write(struct stream *s,
1568 struct interface *ifp)
16f1b9ee 1569{
d62a17ae 1570 size_t w;
1571 struct if_link_params *iflp;
1572 int i;
16f1b9ee 1573
d62a17ae 1574 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1575 return 0;
16f1b9ee 1576
d62a17ae 1577 iflp = ifp->link_params;
1578 w = 0;
16f1b9ee 1579
d62a17ae 1580 w += stream_putl(s, iflp->lp_status);
16f1b9ee 1581
d62a17ae 1582 w += stream_putl(s, iflp->te_metric);
1583 w += stream_putf(s, iflp->max_bw);
1584 w += stream_putf(s, iflp->max_rsv_bw);
16f1b9ee 1585
d62a17ae 1586 w += stream_putl(s, MAX_CLASS_TYPE);
1587 for (i = 0; i < MAX_CLASS_TYPE; i++)
1588 w += stream_putf(s, iflp->unrsv_bw[i]);
16f1b9ee 1589
d62a17ae 1590 w += stream_putl(s, iflp->admin_grp);
1591 w += stream_putl(s, iflp->rmt_as);
1592 w += stream_put_in_addr(s, &iflp->rmt_ip);
16f1b9ee 1593
d62a17ae 1594 w += stream_putl(s, iflp->av_delay);
1595 w += stream_putl(s, iflp->min_delay);
1596 w += stream_putl(s, iflp->max_delay);
1597 w += stream_putl(s, iflp->delay_var);
16f1b9ee 1598
d62a17ae 1599 w += stream_putf(s, iflp->pkt_loss);
1600 w += stream_putf(s, iflp->res_bw);
1601 w += stream_putf(s, iflp->ava_bw);
1602 w += stream_putf(s, iflp->use_bw);
16f1b9ee 1603
d62a17ae 1604 return w;
16f1b9ee
OD
1605}
1606
1607/*
0a589359 1608 * format of message for address additon is:
1609 * 0
1610 * 0 1 2 3 4 5 6 7
1611 * +-+-+-+-+-+-+-+-+
1612 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1613 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1614 * | |
1615 * + +
1616 * | ifindex |
1617 * + +
1618 * | |
1619 * + +
1620 * | |
1621 * +-+-+-+-+-+-+-+-+
1622 * | ifc_flags | flags for connected address
1623 * +-+-+-+-+-+-+-+-+
1624 * | addr_family |
1625 * +-+-+-+-+-+-+-+-+
1626 * | addr... |
1627 * : :
1628 * | |
1629 * +-+-+-+-+-+-+-+-+
1630 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1631 * +-+-+-+-+-+-+-+-+
1632 * | daddr.. |
1633 * : :
1634 * | |
1635 * +-+-+-+-+-+-+-+-+
0a589359 1636 */
1637
d62a17ae 1638static int memconstant(const void *s, int c, size_t n)
3fb9cd6e 1639{
d62a17ae 1640 const u_char *p = s;
3fb9cd6e 1641
d62a17ae 1642 while (n-- > 0)
1643 if (*p++ != c)
1644 return 0;
1645 return 1;
3fb9cd6e 1646}
1647
d5a5c8f0 1648
d62a17ae 1649struct connected *zebra_interface_address_read(int type, struct stream *s,
1650 vrf_id_t vrf_id)
718e3744 1651{
d62a17ae 1652 ifindex_t ifindex;
1653 struct interface *ifp;
1654 struct connected *ifc;
1655 struct prefix p, d, *dp;
1656 int plen;
1657 u_char ifc_flags;
1658
1659 memset(&p, 0, sizeof(p));
1660 memset(&d, 0, sizeof(d));
1661
1662 /* Get interface index. */
1663 ifindex = stream_getl(s);
1664
1665 /* Lookup index. */
1666 ifp = if_lookup_by_index(ifindex, vrf_id);
1667 if (ifp == NULL) {
1668 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1669 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1670 ifindex, vrf_id);
1671 return NULL;
1672 }
1673
1674 /* Fetch flag. */
1675 ifc_flags = stream_getc(s);
1676
1677 /* Fetch interface address. */
1678 d.family = p.family = stream_getc(s);
1679 plen = prefix_blen(&d);
1680
1681 zclient_stream_get_prefix(s, &p);
1682
1683 /* Fetch destination address. */
1684 stream_get(&d.u.prefix, s, plen);
1685
1686 /* N.B. NULL destination pointers are encoded as all zeroes */
1687 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1688
1689 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1690 ifc = connected_lookup_prefix_exact(ifp, &p);
1691 if (!ifc) {
1692 /* N.B. NULL destination pointers are encoded as all
1693 * zeroes */
1694 ifc = connected_add_by_prefix(ifp, &p, dp);
1695 }
1696 if (ifc) {
1697 ifc->flags = ifc_flags;
1698 if (ifc->destination)
1699 ifc->destination->prefixlen =
1700 ifc->address->prefixlen;
1701 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1702 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1703 * "peer" */
1704 char buf[PREFIX_STRLEN];
1705 zlog_warn(
1706 "warning: interface %s address %s "
1707 "with peer flag set, but no peer address!",
9d303b37
DL
1708 ifp->name, prefix2str(ifc->address, buf,
1709 sizeof buf));
d62a17ae 1710 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1711 }
1712 }
1713 } else {
1714 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1715 ifc = connected_delete_by_prefix(ifp, &p);
1716 }
1717
1718 return ifc;
718e3744 1719}
0a589359 1720
a80beece
DS
1721/*
1722 * format of message for neighbor connected address is:
1723 * 0
1724 * 0 1 2 3 4 5 6 7
1725 * +-+-+-+-+-+-+-+-+
1726 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1727 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1728 * | |
1729 * + +
1730 * | ifindex |
1731 * + +
1732 * | |
1733 * + +
1734 * | |
1735 * +-+-+-+-+-+-+-+-+
1736 * | addr_family |
1737 * +-+-+-+-+-+-+-+-+
1738 * | addr... |
1739 * : :
1740 * | |
1741 * +-+-+-+-+-+-+-+-+
1742 * | addr_len | len of addr.
1743 * +-+-+-+-+-+-+-+-+
1744 */
1745struct nbr_connected *
d62a17ae 1746zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
a80beece 1747{
d62a17ae 1748 unsigned int ifindex;
1749 struct interface *ifp;
1750 struct prefix p;
1751 struct nbr_connected *ifc;
1752
1753 /* Get interface index. */
1754 ifindex = stream_getl(s);
1755
1756 /* Lookup index. */
1757 ifp = if_lookup_by_index(ifindex, vrf_id);
1758 if (ifp == NULL) {
1759 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1760 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1761 : "DELETE",
1762 ifindex, vrf_id);
1763 return NULL;
1764 }
1765
1766 p.family = stream_getc(s);
1767 stream_get(&p.u.prefix, s, prefix_blen(&p));
1768 p.prefixlen = stream_getc(s);
1769
1770 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1771 /* Currently only supporting P2P links, so any new RA source
1772 address is
1773 considered as the replacement of the previously learnt
1774 Link-Local address. */
1775 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1776 ifc = nbr_connected_new();
1777 ifc->address = prefix_new();
1778 ifc->ifp = ifp;
1779 listnode_add(ifp->nbr_connected, ifc);
1780 }
1781
1782 prefix_copy(ifc->address, &p);
1783 } else {
1784 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1785
1786 ifc = nbr_connected_check(ifp, &p);
1787 if (ifc)
1788 listnode_delete(ifp->nbr_connected, ifc);
1789 }
1790
1791 return ifc;
a80beece 1792}
6b0655a2 1793
d62a17ae 1794struct interface *zebra_interface_vrf_update_read(struct stream *s,
1795 vrf_id_t vrf_id,
1796 vrf_id_t *new_vrf_id)
c8e264b6 1797{
d62a17ae 1798 unsigned int ifindex;
1799 struct interface *ifp;
a9ff90c4 1800 vrf_id_t new_id;
d62a17ae 1801
1802 /* Get interface index. */
1803 ifindex = stream_getl(s);
1804
1805 /* Lookup interface. */
1806 ifp = if_lookup_by_index(ifindex, vrf_id);
1807 if (ifp == NULL) {
1808 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1809 ifindex, vrf_id);
1810 return NULL;
1811 }
1812
1813 /* Fetch new VRF Id. */
1814 new_id = stream_getw(s);
1815
1816 *new_vrf_id = new_id;
1817 return ifp;
c8e264b6 1818}
5c7ef8dc 1819
1820/* filter unwanted messages until the expected one arrives */
d62a17ae 1821static int zclient_read_sync_response(struct zclient *zclient,
1822 u_int16_t expected_cmd)
5c7ef8dc 1823{
d62a17ae 1824 struct stream *s;
c31a793b 1825 u_int16_t size = -1;
d62a17ae 1826 u_char marker;
1827 u_char version;
1828 vrf_id_t vrf_id;
1829 u_int16_t cmd;
1830 fd_set readfds;
1831 int ret;
1832
1833 ret = 0;
1834 cmd = expected_cmd + 1;
1835 while (ret == 0 && cmd != expected_cmd) {
1836 s = zclient->ibuf;
1837 stream_reset(s);
1838
1839 /* wait until response arrives */
1840 FD_ZERO(&readfds);
1841 FD_SET(zclient->sock, &readfds);
1842 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1843 if (!FD_ISSET(zclient->sock, &readfds))
1844 continue;
1845 /* read response */
1846 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1847 &version, &vrf_id, &cmd);
1848 if (zclient_debug)
1849 zlog_debug("%s: Response (%d bytes) received", __func__,
1850 size);
1851 }
1852 if (ret != 0) {
1853 zlog_err("%s: Invalid Sync Message Reply", __func__);
1854 return -1;
1855 }
1856
1857 return 0;
5c7ef8dc 1858}
fea12efb 1859/**
1860 * Connect to label manager in a syncronous way
1861 *
1862 * It first writes the request to zcient output buffer and then
1863 * immediately reads the answer from the input buffer.
1864 *
1865 * @param zclient Zclient used to connect to label manager (zebra)
1866 * @result Result of response
1867 */
d62a17ae 1868int lm_label_manager_connect(struct zclient *zclient)
fea12efb 1869{
d62a17ae 1870 int ret;
1871 struct stream *s;
1872 u_char result;
1873
1874 if (zclient_debug)
1875 zlog_debug("Connecting to Label Manager");
1876
1877 if (zclient->sock < 0)
1878 return -1;
1879
1880 /* send request */
1881 s = zclient->obuf;
1882 stream_reset(s);
1883 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
1884
1885 /* proto */
1886 stream_putc(s, zclient->redist_default);
1887 /* instance */
1888 stream_putw(s, zclient->instance);
1889
1890 /* Put length at the first point of the stream. */
1891 stream_putw_at(s, 0, stream_get_endp(s));
1892
1893 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1894 if (ret < 0) {
1895 zlog_err("%s: can't write to zclient->sock", __func__);
1896 close(zclient->sock);
1897 zclient->sock = -1;
1898 return -1;
1899 }
1900 if (ret == 0) {
1901 zlog_err("%s: zclient->sock connection closed", __func__);
1902 close(zclient->sock);
1903 zclient->sock = -1;
1904 return -1;
1905 }
1906 if (zclient_debug)
1907 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1908 __func__, ret);
1909
1910 /* read response */
1911 if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT)
1912 != 0)
1913 return -1;
1914
1915 /* result */
1916 s = zclient->ibuf;
1917 result = stream_getc(s);
1918 if (zclient_debug)
1919 zlog_debug(
1920 "%s: Label Manager connect response received, result %u",
1921 __func__, result);
1922
1923 return (int)result;
fea12efb 1924}
1925
1926/**
1927 * Function to request a label chunk in a syncronous way
1928 *
1929 * It first writes the request to zlcient output buffer and then
1930 * immediately reads the answer from the input buffer.
1931 *
1932 * @param zclient Zclient used to connect to label manager (zebra)
1933 * @param keep Avoid garbage collection
1934 * @param chunk_size Amount of labels requested
1935 * @param start To write first assigned chunk label to
1936 * @param end To write last assigned chunk label to
1937 * @result 0 on success, -1 otherwise
1938 */
d62a17ae 1939int lm_get_label_chunk(struct zclient *zclient, u_char keep,
1940 uint32_t chunk_size, uint32_t *start, uint32_t *end)
fea12efb 1941{
d62a17ae 1942 int ret;
1943 struct stream *s;
1944 u_char response_keep;
1945
1946 if (zclient_debug)
1947 zlog_debug("Getting Label Chunk");
1948
1949 if (zclient->sock < 0)
1950 return -1;
1951
1952 /* send request */
1953 s = zclient->obuf;
1954 stream_reset(s);
1955 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1956 /* keep */
1957 stream_putc(s, keep);
1958 /* chunk size */
1959 stream_putl(s, chunk_size);
1960 /* Put length at the first point of the stream. */
1961 stream_putw_at(s, 0, stream_get_endp(s));
1962
1963 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1964 if (ret < 0) {
1965 zlog_err("%s: can't write to zclient->sock", __func__);
1966 close(zclient->sock);
1967 zclient->sock = -1;
1968 return -1;
1969 }
1970 if (ret == 0) {
1971 zlog_err("%s: zclient->sock connection closed", __func__);
1972 close(zclient->sock);
1973 zclient->sock = -1;
1974 return -1;
1975 }
1976 if (zclient_debug)
1977 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__,
1978 ret);
1979
1980 /* read response */
1981 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
1982 return -1;
1983
1984 s = zclient->ibuf;
1985 /* keep */
1986 response_keep = stream_getc(s);
1987 /* start and end labels */
1988 *start = stream_getl(s);
1989 *end = stream_getl(s);
1990
1991 /* not owning this response */
1992 if (keep != response_keep) {
1993 zlog_err(
1994 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1995 __func__, *start, *end, keep, response_keep);
1996 }
1997 /* sanity */
70e98a7f
DS
1998 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
1999 || *end > MPLS_LABEL_UNRESERVED_MAX) {
d62a17ae 2000 zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start,
2001 *end);
2002 return -1;
2003 }
2004
2005 if (zclient_debug)
2006 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end,
2007 response_keep);
2008
2009 return 0;
fea12efb 2010}
2011
2012/**
2013 * Function to release a label chunk
2014 *
2015 * @param zclient Zclient used to connect to label manager (zebra)
2016 * @param start First label of chunk
2017 * @param end Last label of chunk
2018 * @result 0 on success, -1 otherwise
2019 */
d62a17ae 2020int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
2021 uint32_t end)
fea12efb 2022{
d62a17ae 2023 int ret;
2024 struct stream *s;
2025
2026 if (zclient_debug)
2027 zlog_debug("Releasing Label Chunk");
2028
2029 if (zclient->sock < 0)
2030 return -1;
2031
2032 /* send request */
2033 s = zclient->obuf;
2034 stream_reset(s);
2035 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
2036
2037 /* start */
2038 stream_putl(s, start);
2039 /* end */
2040 stream_putl(s, end);
2041
2042 /* Put length at the first point of the stream. */
2043 stream_putw_at(s, 0, stream_get_endp(s));
2044
2045 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2046 if (ret < 0) {
2047 zlog_err("%s: can't write to zclient->sock", __func__);
2048 close(zclient->sock);
2049 zclient->sock = -1;
2050 return -1;
2051 }
2052 if (ret == 0) {
2053 zlog_err("%s: zclient->sock connection closed", __func__);
2054 close(zclient->sock);
2055 zclient->sock = -1;
2056 return -1;
2057 }
2058
2059 return 0;
fea12efb 2060}
c8e264b6 2061
6833ae01 2062int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
2063{
2064 struct stream *s;
2065
2066 /* Reset stream. */
2067 s = zclient->obuf;
2068 stream_reset(s);
2069
2070 zclient_create_header(s, command, VRF_DEFAULT);
2071 stream_write(s, pw->ifname, IF_NAMESIZE);
2072 stream_putl(s, pw->ifindex);
2073
2074 /* Put type */
2075 stream_putl(s, pw->type);
2076
2077 /* Put nexthop */
2078 stream_putl(s, pw->af);
2079 switch (pw->af) {
2080 case AF_INET:
2081 stream_put_in_addr(s, &pw->nexthop.ipv4);
2082 break;
2083 case AF_INET6:
2084 stream_write(s, (u_char *)&pw->nexthop.ipv6, 16);
2085 break;
2086 default:
2087 zlog_err("%s: unknown af", __func__);
2088 return -1;
2089 }
2090
2091 /* Put labels */
2092 stream_putl(s, pw->local_label);
2093 stream_putl(s, pw->remote_label);
2094
2095 /* Put flags */
2096 stream_putc(s, pw->flags);
2097
2098 /* Protocol specific fields */
2099 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2100
2101 /* Put length at the first point of the stream. */
2102 stream_putw_at(s, 0, stream_get_endp(s));
2103
2104 return zclient_send_message(zclient);
2105}
2106
2107/*
2108 * Receive PW status update from Zebra and send it to LDE process.
2109 */
2110void zebra_read_pw_status_update(int command, struct zclient *zclient,
2111 zebra_size_t length, vrf_id_t vrf_id,
2112 struct zapi_pw_status *pw)
2113{
2114 struct stream *s;
2115
2116 memset(pw, 0, sizeof(struct zapi_pw_status));
2117 s = zclient->ibuf;
2118
2119 /* Get data. */
2120 stream_get(pw->ifname, s, IF_NAMESIZE);
2121 pw->ifindex = stream_getl(s);
2122 pw->status = stream_getl(s);
2123}
2124
718e3744 2125/* Zebra client message read function. */
d62a17ae 2126static int zclient_read(struct thread *thread)
718e3744 2127{
d62a17ae 2128 size_t already;
2129 uint16_t length, command;
2130 uint8_t marker, version;
2131 vrf_id_t vrf_id;
2132 struct zclient *zclient;
2133
2134 /* Get socket to zebra. */
2135 zclient = THREAD_ARG(thread);
2136 zclient->t_read = NULL;
2137
2138 /* Read zebra header (if we don't have it already). */
2139 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2140 ssize_t nbyte;
2141 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2142 ZEBRA_HEADER_SIZE - already))
2143 == 0)
2144 || (nbyte == -1)) {
2145 if (zclient_debug)
2146 zlog_debug(
2147 "zclient connection closed socket [%d].",
2148 zclient->sock);
2149 return zclient_failed(zclient);
2150 }
2151 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2152 /* Try again later. */
2153 zclient_event(ZCLIENT_READ, zclient);
2154 return 0;
2155 }
2156 already = ZEBRA_HEADER_SIZE;
634f9ea2 2157 }
d62a17ae 2158
2159 /* Reset to read from the beginning of the incoming packet. */
2160 stream_set_getp(zclient->ibuf, 0);
2161
2162 /* Fetch header values. */
2163 length = stream_getw(zclient->ibuf);
2164 marker = stream_getc(zclient->ibuf);
2165 version = stream_getc(zclient->ibuf);
a9ff90c4 2166 vrf_id = stream_getl(zclient->ibuf);
d62a17ae 2167 command = stream_getw(zclient->ibuf);
2168
2169 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2170 zlog_err(
2171 "%s: socket %d version mismatch, marker %d, version %d",
2172 __func__, zclient->sock, marker, version);
2173 return zclient_failed(zclient);
634f9ea2 2174 }
d62a17ae 2175
2176 if (length < ZEBRA_HEADER_SIZE) {
2177 zlog_err("%s: socket %d message length %u is less than %d ",
2178 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2179 return zclient_failed(zclient);
634f9ea2 2180 }
d62a17ae 2181
2182 /* Length check. */
2183 if (length > STREAM_SIZE(zclient->ibuf)) {
2184 struct stream *ns;
2185 zlog_warn(
2186 "%s: message size %u exceeds buffer size %lu, expanding...",
2187 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
2188 ns = stream_new(length);
2189 stream_copy(ns, zclient->ibuf);
2190 stream_free(zclient->ibuf);
2191 zclient->ibuf = ns;
2192 }
2193
2194 /* Read rest of zebra packet. */
2195 if (already < length) {
2196 ssize_t nbyte;
2197 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2198 length - already))
2199 == 0)
2200 || (nbyte == -1)) {
2201 if (zclient_debug)
2202 zlog_debug(
2203 "zclient connection closed socket [%d].",
2204 zclient->sock);
2205 return zclient_failed(zclient);
2206 }
2207 if (nbyte != (ssize_t)(length - already)) {
2208 /* Try again later. */
2209 zclient_event(ZCLIENT_READ, zclient);
2210 return 0;
2211 }
2212 }
2213
2214 length -= ZEBRA_HEADER_SIZE;
2215
2216 if (zclient_debug)
2217 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2218 (void *)zclient, command, vrf_id);
2219
2220 switch (command) {
2221 case ZEBRA_ROUTER_ID_UPDATE:
2222 if (zclient->router_id_update)
2223 (*zclient->router_id_update)(command, zclient, length,
2224 vrf_id);
2225 break;
2226 case ZEBRA_VRF_ADD:
2227 zclient_vrf_add(zclient, vrf_id);
2228 break;
2229 case ZEBRA_VRF_DELETE:
2230 zclient_vrf_delete(zclient, vrf_id);
2231 break;
2232 case ZEBRA_INTERFACE_ADD:
2233 if (zclient->interface_add)
2234 (*zclient->interface_add)(command, zclient, length,
2235 vrf_id);
2236 break;
2237 case ZEBRA_INTERFACE_DELETE:
2238 if (zclient->interface_delete)
2239 (*zclient->interface_delete)(command, zclient, length,
2240 vrf_id);
2241 break;
2242 case ZEBRA_INTERFACE_ADDRESS_ADD:
2243 if (zclient->interface_address_add)
2244 (*zclient->interface_address_add)(command, zclient,
2245 length, vrf_id);
2246 break;
2247 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2248 if (zclient->interface_address_delete)
2249 (*zclient->interface_address_delete)(command, zclient,
2250 length, vrf_id);
2251 break;
2252 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2253 if (zclient->interface_bfd_dest_update)
2254 (*zclient->interface_bfd_dest_update)(command, zclient,
2255 length, vrf_id);
2256 break;
2257 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2258 if (zclient->interface_nbr_address_add)
2259 (*zclient->interface_nbr_address_add)(command, zclient,
2260 length, vrf_id);
2261 break;
2262 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2263 if (zclient->interface_nbr_address_delete)
2264 (*zclient->interface_nbr_address_delete)(
2265 command, zclient, length, vrf_id);
2266 break;
2267 case ZEBRA_INTERFACE_UP:
2268 if (zclient->interface_up)
2269 (*zclient->interface_up)(command, zclient, length,
2270 vrf_id);
2271 break;
2272 case ZEBRA_INTERFACE_DOWN:
2273 if (zclient->interface_down)
2274 (*zclient->interface_down)(command, zclient, length,
2275 vrf_id);
2276 break;
2277 case ZEBRA_INTERFACE_VRF_UPDATE:
2278 if (zclient->interface_vrf_update)
2279 (*zclient->interface_vrf_update)(command, zclient,
2280 length, vrf_id);
2281 break;
2282 case ZEBRA_NEXTHOP_UPDATE:
2283 if (zclient_debug)
2284 zlog_debug("zclient rcvd nexthop update\n");
2285 if (zclient->nexthop_update)
2286 (*zclient->nexthop_update)(command, zclient, length,
2287 vrf_id);
2288 break;
2289 case ZEBRA_IMPORT_CHECK_UPDATE:
2290 if (zclient_debug)
2291 zlog_debug("zclient rcvd import check update\n");
2292 if (zclient->import_check_update)
2293 (*zclient->import_check_update)(command, zclient,
2294 length, vrf_id);
2295 break;
2296 case ZEBRA_BFD_DEST_REPLAY:
2297 if (zclient->bfd_dest_replay)
2298 (*zclient->bfd_dest_replay)(command, zclient, length,
2299 vrf_id);
2300 break;
74489921
RW
2301 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2302 if (zclient->redistribute_route_add)
2303 (*zclient->redistribute_route_add)(command, zclient,
2304 length, vrf_id);
d62a17ae 2305 break;
74489921
RW
2306 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2307 if (zclient->redistribute_route_del)
2308 (*zclient->redistribute_route_del)(command, zclient,
2309 length, vrf_id);
d62a17ae 2310 break;
2311 case ZEBRA_INTERFACE_LINK_PARAMS:
2312 if (zclient->interface_link_params)
2313 (*zclient->interface_link_params)(command, zclient,
2314 length);
2315 break;
2316 case ZEBRA_FEC_UPDATE:
2317 if (zclient_debug)
2318 zlog_debug("zclient rcvd fec update\n");
2319 if (zclient->fec_update)
2320 (*zclient->fec_update)(command, zclient, length);
2321 break;
2322 case ZEBRA_VNI_ADD:
2323 if (zclient->local_vni_add)
2324 (*zclient->local_vni_add)(command, zclient, length,
2325 vrf_id);
2326 break;
2327 case ZEBRA_VNI_DEL:
2328 if (zclient->local_vni_del)
2329 (*zclient->local_vni_del)(command, zclient, length,
2330 vrf_id);
2331 break;
b7cfce93
MK
2332 case ZEBRA_L3VNI_ADD:
2333 if (zclient->local_l3vni_add)
2334 (*zclient->local_l3vni_add)(command, zclient, length,
2335 vrf_id);
2336 break;
2337 case ZEBRA_L3VNI_DEL:
2338 if (zclient->local_l3vni_del)
2339 (*zclient->local_l3vni_del)(command, zclient, length,
2340 vrf_id);
2341 break;
d62a17ae 2342 case ZEBRA_MACIP_ADD:
2343 if (zclient->local_macip_add)
2344 (*zclient->local_macip_add)(command, zclient, length,
2345 vrf_id);
2346 break;
2347 case ZEBRA_MACIP_DEL:
2348 if (zclient->local_macip_del)
2349 (*zclient->local_macip_del)(command, zclient, length,
2350 vrf_id);
2351 break;
31310b25
MK
2352 case ZEBRA_IP_PREFIX_ROUTE_ADD:
2353 if (zclient->local_ip_prefix_add)
2354 (*zclient->local_ip_prefix_add)(command, zclient,
2355 length, vrf_id);
2356 break;
2357 case ZEBRA_IP_PREFIX_ROUTE_DEL:
2358 if (zclient->local_ip_prefix_del)
2359 (*zclient->local_ip_prefix_del)(command, zclient,
2360 length, vrf_id);
2361 break;
6833ae01 2362 case ZEBRA_PW_STATUS_UPDATE:
2363 if (zclient->pw_status_update)
2364 (*zclient->pw_status_update)(command, zclient, length,
2365 vrf_id);
2366 break;
7ea7b86e
DS
2367 case ZEBRA_ROUTE_NOTIFY_OWNER:
2368 if (zclient->notify_owner)
2369 (*zclient->notify_owner)(command, zclient,
2370 length, vrf_id);
2371 break;
d62a17ae 2372 default:
2373 break;
634f9ea2 2374 }
d62a17ae 2375
2376 if (zclient->sock < 0)
2377 /* Connection was closed during packet processing. */
2378 return -1;
2379
2380 /* Register read thread. */
2381 stream_reset(zclient->ibuf);
2382 zclient_event(ZCLIENT_READ, zclient);
2383
2384 return 0;
718e3744 2385}
2386
d62a17ae 2387void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2388 int type, u_short instance, vrf_id_t vrf_id)
718e3744 2389{
718e3744 2390
d62a17ae 2391 if (instance) {
2392 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2393 if (redist_check_instance(
2394 &zclient->mi_redist[afi][type], instance))
2395 return;
2396 redist_add_instance(&zclient->mi_redist[afi][type],
2397 instance);
2398 } else {
2399 if (!redist_check_instance(
2400 &zclient->mi_redist[afi][type], instance))
2401 return;
2402 redist_del_instance(&zclient->mi_redist[afi][type],
2403 instance);
2404 }
2405
2406 } else {
2407 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2408 if (vrf_bitmap_check(zclient->redist[afi][type],
2409 vrf_id))
2410 return;
2411 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2412 } else {
2413 if (!vrf_bitmap_check(zclient->redist[afi][type],
2414 vrf_id))
2415 return;
2416 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2417 }
2418 }
2419
2420 if (zclient->sock > 0)
2421 zebra_redistribute_send(command, zclient, afi, type, instance,
2422 vrf_id);
718e3744 2423}
2424
718e3744 2425
d62a17ae 2426void zclient_redistribute_default(int command, struct zclient *zclient,
2427 vrf_id_t vrf_id)
718e3744 2428{
718e3744 2429
d62a17ae 2430 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2431 if (vrf_bitmap_check(zclient->default_information, vrf_id))
2432 return;
2433 vrf_bitmap_set(zclient->default_information, vrf_id);
2434 } else {
2435 if (!vrf_bitmap_check(zclient->default_information, vrf_id))
2436 return;
2437 vrf_bitmap_unset(zclient->default_information, vrf_id);
2438 }
2439
2440 if (zclient->sock > 0)
2441 zebra_message_send(zclient, command, vrf_id);
718e3744 2442}
2443
d62a17ae 2444static void zclient_event(enum event event, struct zclient *zclient)
718e3744 2445{
d62a17ae 2446 switch (event) {
2447 case ZCLIENT_SCHEDULE:
2448 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2449 &zclient->t_connect);
2450 break;
2451 case ZCLIENT_CONNECT:
2452 if (zclient_debug)
2453 zlog_debug(
2454 "zclient connect failures: %d schedule interval is now %d",
2455 zclient->fail, zclient->fail < 3 ? 10 : 60);
2456 thread_add_timer(zclient->master, zclient_connect, zclient,
2457 zclient->fail < 3 ? 10 : 60,
2458 &zclient->t_connect);
2459 break;
2460 case ZCLIENT_READ:
2461 zclient->t_read = NULL;
2462 thread_add_read(zclient->master, zclient_read, zclient,
2463 zclient->sock, &zclient->t_read);
2464 break;
2465 }
718e3744 2466}
b5114685 2467
e0ae31b8
DS
2468void zclient_interface_set_master(struct zclient *client,
2469 struct interface *master,
2470 struct interface *slave)
2471{
2472 struct stream *s;
2473
2474 s = client->obuf;
2475 stream_reset(s);
2476
2477 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2478
a9ff90c4 2479 stream_putl(s, master->vrf_id);
e0ae31b8 2480 stream_putl(s, master->ifindex);
a9ff90c4 2481 stream_putl(s, slave->vrf_id);
e0ae31b8
DS
2482 stream_putl(s, slave->ifindex);
2483
2484 stream_putw_at(s, 0, stream_get_endp(s));
2485 zclient_send_message(client);
2486}