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