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