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