]> git.proxmox.com Git - mirror_frr.git/blame - lib/zclient.c
zserv: identify label type based on the client protocol
[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 *
624 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
625 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
626 * nexthop information is provided, and the message describes a prefix
627 * to blackhole or reject route.
628 *
629 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
630 * infrastructure was built around the traditional (32-bit "gate OR
631 * ifindex") nexthop data unit. A special encoding can be used to feed
632 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
633 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
634 * fields as follows:
635 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
636 * - .nexthop_num == .ifindex_num
637 * - .nexthop and .ifindex are filled with gate and ifindex parts of
638 * each compound nexthop, both in the same order
639 *
640 * zapi_ipv4_route() will produce two nexthop data units for each such
641 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
642 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
643 *
644 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
645 * byte value.
646 *
647 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
648 * byte value.
649 *
650 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
651 *
652 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
653 *
654 * XXX: No attention paid to alignment.
655 */
656int zapi_ipv4_route(u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
657 struct zapi_ipv4 *api)
718e3744 658{
d62a17ae 659 int i;
660 int psize;
661 struct stream *s;
662
663 /* Reset stream. */
664 s = zclient->obuf;
665 stream_reset(s);
666
667 /* Some checks for labeled-unicast. The current expectation is that each
668 * nexthop is accompanied by a label in the case of labeled-unicast.
669 */
670 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)
671 && CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
672 /* We expect prefixes installed with labels and the number to
673 * match
674 * the number of nexthops.
675 */
676 assert(api->label_num == api->nexthop_num);
677 }
678
679 zclient_create_header(s, cmd, api->vrf_id);
680
681 /* Put type and nexthop. */
682 stream_putc(s, api->type);
683 stream_putw(s, api->instance);
684 stream_putl(s, api->flags);
685 stream_putc(s, api->message);
686 stream_putw(s, api->safi);
687
688 /* Put prefix information. */
689 psize = PSIZE(p->prefixlen);
690 stream_putc(s, p->prefixlen);
691 stream_write(s, (u_char *)&p->prefix, psize);
692
693 /* Nexthop, ifindex, distance and metric information. */
694 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
695 /* traditional 32-bit data units */
696 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_BLACKHOLE)) {
697 stream_putc(s, 1);
698 stream_putc(s, NEXTHOP_TYPE_BLACKHOLE);
699 /* XXX assert(api->nexthop_num == 0); */
700 /* XXX assert(api->ifindex_num == 0); */
701 } else
702 stream_putc(s, api->nexthop_num + api->ifindex_num);
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)) {
772 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_BLACKHOLE)) {
773 stream_putc(s, 1);
774 stream_putc(s, NEXTHOP_TYPE_BLACKHOLE);
775 /* XXX assert(api->nexthop_num == 0); */
776 /* XXX assert(api->ifindex_num == 0); */
777 } else
778 stream_putc(s, api->nexthop_num + api->ifindex_num);
779
780 for (i = 0; i < api->nexthop_num; i++) {
781 stream_putc(s, NEXTHOP_TYPE_IPV6);
782 stream_write(s, (u_char *)api->nexthop[i], 16);
783 /* For labeled-unicast, each nexthop is followed by
784 * label. */
785 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL))
786 stream_putl(s, api->label[i]);
787 }
788 for (i = 0; i < api->ifindex_num; i++) {
789 stream_putc(s, NEXTHOP_TYPE_IFINDEX);
790 stream_putl(s, api->ifindex[i]);
791 }
8a92a8a0 792 }
8a92a8a0 793
d62a17ae 794 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
795 stream_putc(s, api->distance);
796 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
797 stream_putl(s, api->metric);
798 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
799 stream_putl(s, api->tag);
800 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
801 stream_putl(s, api->mtu);
8a92a8a0 802
d62a17ae 803 /* Put length at the first point of the stream. */
804 stream_putw_at(s, 0, stream_get_endp(s));
8a92a8a0 805
d62a17ae 806 return zclient_send_message(zclient);
8a92a8a0
DS
807}
808
d62a17ae 809int zapi_ipv6_route(u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
810 struct prefix_ipv6 *src_p, struct zapi_ipv6 *api)
718e3744 811{
d62a17ae 812 int i;
813 int psize;
814 struct stream *s;
815
816 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
817 */
818 assert(!(api->message & ZAPI_MESSAGE_SRCPFX) == !src_p);
819
820 /* Reset stream. */
821 s = zclient->obuf;
822 stream_reset(s);
823
824 /* Some checks for labeled-unicast. The current expectation is that each
825 * nexthop is accompanied by a label in the case of labeled-unicast.
826 */
827 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)
828 && CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
829 /* We expect prefixes installed with labels and the number to
830 * match
831 * the number of nexthops.
832 */
833 assert(api->label_num == api->nexthop_num);
718e3744 834 }
d62a17ae 835
836 zclient_create_header(s, cmd, api->vrf_id);
837
838 /* Put type and nexthop. */
839 stream_putc(s, api->type);
840 stream_putw(s, api->instance);
841 stream_putl(s, api->flags);
842 stream_putc(s, api->message);
843 stream_putw(s, api->safi);
844
845 /* Put prefix information. */
846 psize = PSIZE(p->prefixlen);
847 stream_putc(s, p->prefixlen);
848 stream_write(s, (u_char *)&p->prefix, psize);
849
850 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
851 psize = PSIZE(src_p->prefixlen);
852 stream_putc(s, src_p->prefixlen);
853 stream_write(s, (u_char *)&src_p->prefix, psize);
718e3744 854 }
718e3744 855
d62a17ae 856 /* Nexthop, ifindex, distance and metric information. */
857 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
858 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_BLACKHOLE)) {
859 stream_putc(s, 1);
860 stream_putc(s, NEXTHOP_TYPE_BLACKHOLE);
861 /* XXX assert(api->nexthop_num == 0); */
862 /* XXX assert(api->ifindex_num == 0); */
863 } else
864 stream_putc(s, api->nexthop_num + api->ifindex_num);
865
866 for (i = 0; i < api->nexthop_num; i++) {
867 stream_putc(s, NEXTHOP_TYPE_IPV6);
868 stream_write(s, (u_char *)api->nexthop[i], 16);
869 /* For labeled-unicast, each nexthop is followed by
870 * label. */
871 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL))
872 stream_putl(s, api->label[i]);
873 }
874 for (i = 0; i < api->ifindex_num; i++) {
875 stream_putc(s, NEXTHOP_TYPE_IFINDEX);
876 stream_putl(s, api->ifindex[i]);
877 }
878 }
718e3744 879
d62a17ae 880 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
881 stream_putc(s, api->distance);
882 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
883 stream_putl(s, api->metric);
884 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
885 stream_putl(s, api->tag);
886 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
887 stream_putl(s, api->mtu);
718e3744 888
d62a17ae 889 /* Put length at the first point of the stream. */
890 stream_putw_at(s, 0, stream_get_endp(s));
891
892 return zclient_send_message(zclient);
718e3744 893}
718e3744 894
d62a17ae 895int zapi_route(u_char cmd, struct zclient *zclient, struct prefix *p,
896 struct prefix_ipv6 *src_p, struct zapi_route *api)
657cde12 897{
d62a17ae 898 int i;
899 int psize;
900 struct stream *s;
901
902 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
903 */
904 assert(!(api->message & ZAPI_MESSAGE_SRCPFX) == !src_p);
905
906 /* Reset stream. */
907 s = zclient->obuf;
908 stream_reset(s);
909
910 zclient_create_header(s, cmd, api->vrf_id);
911
912 /* Put type and nexthop. */
913 stream_putc(s, api->type);
914 stream_putw(s, api->instance);
915 stream_putl(s, api->flags);
916 stream_putc(s, api->message);
917 stream_putw(s, api->safi);
918
919 /* Put prefix information. */
920 psize = PSIZE(p->prefixlen);
921 stream_putc(s, p->prefixlen);
922 stream_write(s, (u_char *)&p->u.prefix, psize);
923
924 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
925 psize = PSIZE(src_p->prefixlen);
926 stream_putc(s, src_p->prefixlen);
927 stream_write(s, (u_char *)&src_p->prefix, psize);
928 }
929
930 /* Nexthop, ifindex, distance and metric information. */
931 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
932 stream_putc(s, api->nexthop_num);
933
934 for (i = 0; i < api->nexthop_num; i++) {
935 stream_putc(s, api->nexthop[i]->type);
936 switch (api->nexthop[i]->type) {
937 case NEXTHOP_TYPE_BLACKHOLE:
938 break;
939 case NEXTHOP_TYPE_IPV4:
940 stream_put_in_addr(s,
941 &api->nexthop[i]->gate.ipv4);
942
943 /* For labeled-unicast, each nexthop is followed
944 * by label. */
945 if (CHECK_FLAG(api->message,
946 ZAPI_MESSAGE_LABEL))
947 stream_putl(
948 s,
949 api->nexthop[i]
950 ->nh_label->label[0]);
951 break;
952 case NEXTHOP_TYPE_IPV4_IFINDEX:
953 stream_put_in_addr(s,
954 &api->nexthop[i]->gate.ipv4);
955 stream_putl(s, api->nexthop[i]->ifindex);
956 break;
957 case NEXTHOP_TYPE_IFINDEX:
958 stream_putl(s, api->nexthop[i]->ifindex);
959 break;
960 case NEXTHOP_TYPE_IPV6:
961 stream_write(
962 s,
963 (u_char *)&api->nexthop[i]->gate.ipv6,
964 16);
965
966 /* For labeled-unicast, each nexthop is followed
967 * by label. */
968 if (CHECK_FLAG(api->message,
969 ZAPI_MESSAGE_LABEL))
970 stream_putl(
971 s,
972 api->nexthop[i]
973 ->nh_label->label[0]);
974 break;
975 case NEXTHOP_TYPE_IPV6_IFINDEX:
976 stream_write(
977 s,
978 (u_char *)&api->nexthop[i]->gate.ipv6,
979 16);
980 stream_putl(s, api->nexthop[i]->ifindex);
981 break;
982 }
983 }
984 }
985
986 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
987 stream_putc(s, api->distance);
988 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
989 stream_putl(s, api->metric);
990 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
991 stream_putl(s, api->tag);
992 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
993 stream_putl(s, api->mtu);
994
995 /* Put length at the first point of the stream. */
996 stream_putw_at(s, 0, stream_get_endp(s));
997
998 return zclient_send_message(zclient);
657cde12
DS
999}
1000
d62a17ae 1001/*
0a589359 1002 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1003 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
d62a17ae 1004 * then set/unset redist[type] in the client handle (a struct zserv) for the
0a589359 1005 * sending client
1006 */
d62a17ae 1007int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1008 int type, u_short instance, vrf_id_t vrf_id)
718e3744 1009{
d62a17ae 1010 struct stream *s;
1011
1012 s = zclient->obuf;
1013 stream_reset(s);
1014
1015 zclient_create_header(s, command, vrf_id);
1016 stream_putc(s, afi);
1017 stream_putc(s, type);
1018 stream_putw(s, instance);
1019
1020 stream_putw_at(s, 0, stream_get_endp(s));
1021
1022 return zclient_send_message(zclient);
718e3744 1023}
1024
d9178828 1025/* Get prefix in ZServ format; family should be filled in on prefix */
d62a17ae 1026static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
d9178828 1027{
d62a17ae 1028 size_t plen = prefix_blen(p);
1029 u_char c;
1030 p->prefixlen = 0;
1031
1032 if (plen == 0)
1033 return;
1034
1035 stream_get(&p->u.prefix, s, plen);
1036 c = stream_getc(s);
1037 p->prefixlen = MIN(plen * 8, c);
d9178828
PJ
1038}
1039
18a6dce6 1040/* Router-id update from zebra daemon. */
d62a17ae 1041void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
18a6dce6 1042{
d62a17ae 1043 /* Fetch interface address. */
1044 rid->family = stream_getc(s);
1045
1046 zclient_stream_get_prefix(s, rid);
18a6dce6 1047}
1048
718e3744 1049/* Interface addition from zebra daemon. */
d62a17ae 1050/*
0a589359 1051 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1052 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1053 * 0 1 2 3
1054 * 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 1055 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1056 * | ifname |
1057 * | |
1058 * | |
1059 * | |
1060 * | |
1061 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1062 * | ifindex |
1063 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1064 * | status |
0a589359 1065 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1066 * | if_flags |
c77d4546 1067 * | |
0a589359 1068 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1069 * | metric |
1070 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2d7f0d76
DS
1071 * | speed |
1072 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1073 * | ifmtu |
1074 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1075 * | ifmtu6 |
1076 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1077 * | bandwidth |
1078 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1079 * | Link Layer Type |
0a589359 1080 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1081 * | Harware Address Length |
0a589359 1082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1083 * | Hardware Address if HW lenght different from 0 |
1084 * | ... max INTERFACE_HWADDR_MAX |
0a589359 1085 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee 1086 * | Link_params? | Whether a link-params follows: 1 or 0.
0a589359 1087 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16f1b9ee
OD
1088 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1089 * | .... (struct if_link_params). |
0a589359 1090 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1091 */
1092
d62a17ae 1093static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1892f15e 1094{
d62a17ae 1095 struct vrf *vrf;
1096 char vrfname_tmp[VRF_NAMSIZ];
1097 struct vrf_data data;
1892f15e 1098
d62a17ae 1099 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1100 /* Read interface name. */
1101 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1892f15e 1102
d62a17ae 1103 /* Lookup/create vrf by vrf_id. */
1104 vrf = vrf_get(vrf_id, vrfname_tmp);
1105 vrf->data = data;
1892f15e 1106
d62a17ae 1107 vrf_enable(vrf);
1892f15e
DS
1108}
1109
d62a17ae 1110static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1892f15e 1111{
d62a17ae 1112 struct vrf *vrf;
1892f15e 1113
d62a17ae 1114 /* Lookup vrf by vrf_id. */
1115 vrf = vrf_lookup_by_id(vrf_id);
1892f15e 1116
d62a17ae 1117 /*
1118 * If a routing protocol doesn't know about a
1119 * vrf that is about to be deleted. There is
1120 * no point in attempting to delete it.
1121 */
1122 if (!vrf)
1123 return;
beef1990 1124
d62a17ae 1125 vrf_delete(vrf);
1892f15e
DS
1126}
1127
d62a17ae 1128struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
718e3744 1129{
d62a17ae 1130 struct interface *ifp;
1131 char ifname_tmp[INTERFACE_NAMSIZ];
718e3744 1132
d62a17ae 1133 /* Read interface name. */
1134 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
718e3744 1135
d62a17ae 1136 /* Lookup/create interface by name. */
1137 ifp = if_get_by_name_len(
1138 ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), vrf_id, 0);
718e3744 1139
d62a17ae 1140 zebra_interface_if_set_value(s, ifp);
718e3744 1141
d62a17ae 1142 return ifp;
718e3744 1143}
1144
d62a17ae 1145/*
0a589359 1146 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1147 * from zebra server. The format of this message is the same as
1148 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1149 * comments for zebra_interface_add_read), except that no sockaddr_dl
1150 * is sent at the tail of the message.
1151 */
d62a17ae 1152struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
718e3744 1153{
d62a17ae 1154 struct interface *ifp;
1155 char ifname_tmp[INTERFACE_NAMSIZ];
1156
1157 /* Read interface name. */
1158 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1159
1160 /* Lookup this by interface index. */
1161 ifp = if_lookup_by_name_len(
1162 ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), vrf_id);
1163 if (ifp == NULL) {
1164 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1165 ifname_tmp, vrf_id);
1166 return NULL;
1167 }
1168
1169 zebra_interface_if_set_value(s, ifp);
1170
1171 return ifp;
718e3744 1172}
1173
d62a17ae 1174static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
16f1b9ee
OD
1175{
1176
d62a17ae 1177 if (iflp == NULL)
1178 return;
1179
1180 iflp->lp_status = stream_getl(s);
1181 iflp->te_metric = stream_getl(s);
1182 iflp->max_bw = stream_getf(s);
1183 iflp->max_rsv_bw = stream_getf(s);
1184 uint32_t bwclassnum = stream_getl(s);
1185 {
1186 unsigned int i;
1187 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1188 iflp->unrsv_bw[i] = stream_getf(s);
1189 if (i < bwclassnum)
1190 zlog_err(
1191 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1192 " - outdated library?",
1193 __func__, bwclassnum, MAX_CLASS_TYPE);
1194 }
1195 iflp->admin_grp = stream_getl(s);
1196 iflp->rmt_as = stream_getl(s);
1197 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1198
1199 iflp->av_delay = stream_getl(s);
1200 iflp->min_delay = stream_getl(s);
1201 iflp->max_delay = stream_getl(s);
1202 iflp->delay_var = stream_getl(s);
1203
1204 iflp->pkt_loss = stream_getf(s);
1205 iflp->res_bw = stream_getf(s);
1206 iflp->ava_bw = stream_getf(s);
1207 iflp->use_bw = stream_getf(s);
16f1b9ee
OD
1208}
1209
d62a17ae 1210struct interface *zebra_interface_link_params_read(struct stream *s)
16f1b9ee 1211{
d62a17ae 1212 struct if_link_params *iflp;
1213 ifindex_t ifindex;
c28e5b2a 1214
d62a17ae 1215 assert(s);
c28e5b2a 1216
d62a17ae 1217 ifindex = stream_getl(s);
16f1b9ee 1218
d62a17ae 1219 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
16f1b9ee 1220
d62a17ae 1221 if (ifp == NULL) {
1222 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__,
1223 ifindex);
1224 return NULL;
1225 }
16f1b9ee 1226
d62a17ae 1227 if ((iflp = if_link_params_get(ifp)) == NULL)
1228 return NULL;
16f1b9ee 1229
d62a17ae 1230 link_params_set_value(s, iflp);
16f1b9ee 1231
d62a17ae 1232 return ifp;
16f1b9ee
OD
1233}
1234
d62a17ae 1235void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
16f1b9ee 1236{
d62a17ae 1237 u_char link_params_status = 0;
1238
1239 /* Read interface's index. */
1240 ifp->ifindex = stream_getl(s);
1241 ifp->status = stream_getc(s);
1242
1243 /* Read interface's value. */
1244 ifp->flags = stream_getq(s);
1245 ifp->ptm_enable = stream_getc(s);
1246 ifp->ptm_status = stream_getc(s);
1247 ifp->metric = stream_getl(s);
1248 ifp->speed = stream_getl(s);
1249 ifp->mtu = stream_getl(s);
1250 ifp->mtu6 = stream_getl(s);
1251 ifp->bandwidth = stream_getl(s);
1252 ifp->ll_type = stream_getl(s);
1253 ifp->hw_addr_len = stream_getl(s);
1254 if (ifp->hw_addr_len)
1255 stream_get(ifp->hw_addr, s,
1256 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1257
1258 /* Read Traffic Engineering status */
1259 link_params_status = stream_getc(s);
1260 /* Then, Traffic Engineering parameters if any */
1261 if (link_params_status) {
1262 struct if_link_params *iflp = if_link_params_get(ifp);
1263 link_params_set_value(s, iflp);
1264 }
16f1b9ee
OD
1265}
1266
d62a17ae 1267size_t zebra_interface_link_params_write(struct stream *s,
1268 struct interface *ifp)
16f1b9ee 1269{
d62a17ae 1270 size_t w;
1271 struct if_link_params *iflp;
1272 int i;
16f1b9ee 1273
d62a17ae 1274 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1275 return 0;
16f1b9ee 1276
d62a17ae 1277 iflp = ifp->link_params;
1278 w = 0;
16f1b9ee 1279
d62a17ae 1280 w += stream_putl(s, iflp->lp_status);
16f1b9ee 1281
d62a17ae 1282 w += stream_putl(s, iflp->te_metric);
1283 w += stream_putf(s, iflp->max_bw);
1284 w += stream_putf(s, iflp->max_rsv_bw);
16f1b9ee 1285
d62a17ae 1286 w += stream_putl(s, MAX_CLASS_TYPE);
1287 for (i = 0; i < MAX_CLASS_TYPE; i++)
1288 w += stream_putf(s, iflp->unrsv_bw[i]);
16f1b9ee 1289
d62a17ae 1290 w += stream_putl(s, iflp->admin_grp);
1291 w += stream_putl(s, iflp->rmt_as);
1292 w += stream_put_in_addr(s, &iflp->rmt_ip);
16f1b9ee 1293
d62a17ae 1294 w += stream_putl(s, iflp->av_delay);
1295 w += stream_putl(s, iflp->min_delay);
1296 w += stream_putl(s, iflp->max_delay);
1297 w += stream_putl(s, iflp->delay_var);
16f1b9ee 1298
d62a17ae 1299 w += stream_putf(s, iflp->pkt_loss);
1300 w += stream_putf(s, iflp->res_bw);
1301 w += stream_putf(s, iflp->ava_bw);
1302 w += stream_putf(s, iflp->use_bw);
16f1b9ee 1303
d62a17ae 1304 return w;
16f1b9ee
OD
1305}
1306
1307/*
0a589359 1308 * format of message for address additon is:
1309 * 0
1310 * 0 1 2 3 4 5 6 7
1311 * +-+-+-+-+-+-+-+-+
1312 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1313 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1314 * | |
1315 * + +
1316 * | ifindex |
1317 * + +
1318 * | |
1319 * + +
1320 * | |
1321 * +-+-+-+-+-+-+-+-+
1322 * | ifc_flags | flags for connected address
1323 * +-+-+-+-+-+-+-+-+
1324 * | addr_family |
1325 * +-+-+-+-+-+-+-+-+
1326 * | addr... |
1327 * : :
1328 * | |
1329 * +-+-+-+-+-+-+-+-+
1330 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1331 * +-+-+-+-+-+-+-+-+
1332 * | daddr.. |
1333 * : :
1334 * | |
1335 * +-+-+-+-+-+-+-+-+
0a589359 1336 */
1337
d62a17ae 1338static int memconstant(const void *s, int c, size_t n)
3fb9cd6e 1339{
d62a17ae 1340 const u_char *p = s;
3fb9cd6e 1341
d62a17ae 1342 while (n-- > 0)
1343 if (*p++ != c)
1344 return 0;
1345 return 1;
3fb9cd6e 1346}
1347
d5a5c8f0 1348
d62a17ae 1349struct connected *zebra_interface_address_read(int type, struct stream *s,
1350 vrf_id_t vrf_id)
718e3744 1351{
d62a17ae 1352 ifindex_t ifindex;
1353 struct interface *ifp;
1354 struct connected *ifc;
1355 struct prefix p, d, *dp;
1356 int plen;
1357 u_char ifc_flags;
1358
1359 memset(&p, 0, sizeof(p));
1360 memset(&d, 0, sizeof(d));
1361
1362 /* Get interface index. */
1363 ifindex = stream_getl(s);
1364
1365 /* Lookup index. */
1366 ifp = if_lookup_by_index(ifindex, vrf_id);
1367 if (ifp == NULL) {
1368 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1369 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1370 ifindex, vrf_id);
1371 return NULL;
1372 }
1373
1374 /* Fetch flag. */
1375 ifc_flags = stream_getc(s);
1376
1377 /* Fetch interface address. */
1378 d.family = p.family = stream_getc(s);
1379 plen = prefix_blen(&d);
1380
1381 zclient_stream_get_prefix(s, &p);
1382
1383 /* Fetch destination address. */
1384 stream_get(&d.u.prefix, s, plen);
1385
1386 /* N.B. NULL destination pointers are encoded as all zeroes */
1387 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1388
1389 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1390 ifc = connected_lookup_prefix_exact(ifp, &p);
1391 if (!ifc) {
1392 /* N.B. NULL destination pointers are encoded as all
1393 * zeroes */
1394 ifc = connected_add_by_prefix(ifp, &p, dp);
1395 }
1396 if (ifc) {
1397 ifc->flags = ifc_flags;
1398 if (ifc->destination)
1399 ifc->destination->prefixlen =
1400 ifc->address->prefixlen;
1401 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1402 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1403 * "peer" */
1404 char buf[PREFIX_STRLEN];
1405 zlog_warn(
1406 "warning: interface %s address %s "
1407 "with peer flag set, but no peer address!",
9d303b37
DL
1408 ifp->name, prefix2str(ifc->address, buf,
1409 sizeof buf));
d62a17ae 1410 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1411 }
1412 }
1413 } else {
1414 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1415 ifc = connected_delete_by_prefix(ifp, &p);
1416 }
1417
1418 return ifc;
718e3744 1419}
0a589359 1420
a80beece
DS
1421/*
1422 * format of message for neighbor connected address is:
1423 * 0
1424 * 0 1 2 3 4 5 6 7
1425 * +-+-+-+-+-+-+-+-+
1426 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1427 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1428 * | |
1429 * + +
1430 * | ifindex |
1431 * + +
1432 * | |
1433 * + +
1434 * | |
1435 * +-+-+-+-+-+-+-+-+
1436 * | addr_family |
1437 * +-+-+-+-+-+-+-+-+
1438 * | addr... |
1439 * : :
1440 * | |
1441 * +-+-+-+-+-+-+-+-+
1442 * | addr_len | len of addr.
1443 * +-+-+-+-+-+-+-+-+
1444 */
1445struct nbr_connected *
d62a17ae 1446zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
a80beece 1447{
d62a17ae 1448 unsigned int ifindex;
1449 struct interface *ifp;
1450 struct prefix p;
1451 struct nbr_connected *ifc;
1452
1453 /* Get interface index. */
1454 ifindex = stream_getl(s);
1455
1456 /* Lookup index. */
1457 ifp = if_lookup_by_index(ifindex, vrf_id);
1458 if (ifp == NULL) {
1459 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1460 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1461 : "DELETE",
1462 ifindex, vrf_id);
1463 return NULL;
1464 }
1465
1466 p.family = stream_getc(s);
1467 stream_get(&p.u.prefix, s, prefix_blen(&p));
1468 p.prefixlen = stream_getc(s);
1469
1470 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1471 /* Currently only supporting P2P links, so any new RA source
1472 address is
1473 considered as the replacement of the previously learnt
1474 Link-Local address. */
1475 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1476 ifc = nbr_connected_new();
1477 ifc->address = prefix_new();
1478 ifc->ifp = ifp;
1479 listnode_add(ifp->nbr_connected, ifc);
1480 }
1481
1482 prefix_copy(ifc->address, &p);
1483 } else {
1484 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1485
1486 ifc = nbr_connected_check(ifp, &p);
1487 if (ifc)
1488 listnode_delete(ifp->nbr_connected, ifc);
1489 }
1490
1491 return ifc;
a80beece 1492}
6b0655a2 1493
d62a17ae 1494struct interface *zebra_interface_vrf_update_read(struct stream *s,
1495 vrf_id_t vrf_id,
1496 vrf_id_t *new_vrf_id)
c8e264b6 1497{
d62a17ae 1498 unsigned int ifindex;
1499 struct interface *ifp;
1500 vrf_id_t new_id = VRF_DEFAULT;
1501
1502 /* Get interface index. */
1503 ifindex = stream_getl(s);
1504
1505 /* Lookup interface. */
1506 ifp = if_lookup_by_index(ifindex, vrf_id);
1507 if (ifp == NULL) {
1508 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1509 ifindex, vrf_id);
1510 return NULL;
1511 }
1512
1513 /* Fetch new VRF Id. */
1514 new_id = stream_getw(s);
1515
1516 *new_vrf_id = new_id;
1517 return ifp;
c8e264b6 1518}
5c7ef8dc 1519
1520/* filter unwanted messages until the expected one arrives */
d62a17ae 1521static int zclient_read_sync_response(struct zclient *zclient,
1522 u_int16_t expected_cmd)
5c7ef8dc 1523{
d62a17ae 1524 struct stream *s;
1525 u_int16_t size;
1526 u_char marker;
1527 u_char version;
1528 vrf_id_t vrf_id;
1529 u_int16_t cmd;
1530 fd_set readfds;
1531 int ret;
1532
1533 ret = 0;
1534 cmd = expected_cmd + 1;
1535 while (ret == 0 && cmd != expected_cmd) {
1536 s = zclient->ibuf;
1537 stream_reset(s);
1538
1539 /* wait until response arrives */
1540 FD_ZERO(&readfds);
1541 FD_SET(zclient->sock, &readfds);
1542 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1543 if (!FD_ISSET(zclient->sock, &readfds))
1544 continue;
1545 /* read response */
1546 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1547 &version, &vrf_id, &cmd);
1548 if (zclient_debug)
1549 zlog_debug("%s: Response (%d bytes) received", __func__,
1550 size);
1551 }
1552 if (ret != 0) {
1553 zlog_err("%s: Invalid Sync Message Reply", __func__);
1554 return -1;
1555 }
1556
1557 return 0;
5c7ef8dc 1558}
fea12efb 1559/**
1560 * Connect to label manager in a syncronous way
1561 *
1562 * It first writes the request to zcient output buffer and then
1563 * immediately reads the answer from the input buffer.
1564 *
1565 * @param zclient Zclient used to connect to label manager (zebra)
1566 * @result Result of response
1567 */
d62a17ae 1568int lm_label_manager_connect(struct zclient *zclient)
fea12efb 1569{
d62a17ae 1570 int ret;
1571 struct stream *s;
1572 u_char result;
1573
1574 if (zclient_debug)
1575 zlog_debug("Connecting to Label Manager");
1576
1577 if (zclient->sock < 0)
1578 return -1;
1579
1580 /* send request */
1581 s = zclient->obuf;
1582 stream_reset(s);
1583 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
1584
1585 /* proto */
1586 stream_putc(s, zclient->redist_default);
1587 /* instance */
1588 stream_putw(s, zclient->instance);
1589
1590 /* Put length at the first point of the stream. */
1591 stream_putw_at(s, 0, stream_get_endp(s));
1592
1593 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1594 if (ret < 0) {
1595 zlog_err("%s: can't write to zclient->sock", __func__);
1596 close(zclient->sock);
1597 zclient->sock = -1;
1598 return -1;
1599 }
1600 if (ret == 0) {
1601 zlog_err("%s: zclient->sock connection closed", __func__);
1602 close(zclient->sock);
1603 zclient->sock = -1;
1604 return -1;
1605 }
1606 if (zclient_debug)
1607 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1608 __func__, ret);
1609
1610 /* read response */
1611 if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT)
1612 != 0)
1613 return -1;
1614
1615 /* result */
1616 s = zclient->ibuf;
1617 result = stream_getc(s);
1618 if (zclient_debug)
1619 zlog_debug(
1620 "%s: Label Manager connect response received, result %u",
1621 __func__, result);
1622
1623 return (int)result;
fea12efb 1624}
1625
1626/**
1627 * Function to request a label chunk in a syncronous way
1628 *
1629 * It first writes the request to zlcient output buffer and then
1630 * immediately reads the answer from the input buffer.
1631 *
1632 * @param zclient Zclient used to connect to label manager (zebra)
1633 * @param keep Avoid garbage collection
1634 * @param chunk_size Amount of labels requested
1635 * @param start To write first assigned chunk label to
1636 * @param end To write last assigned chunk label to
1637 * @result 0 on success, -1 otherwise
1638 */
d62a17ae 1639int lm_get_label_chunk(struct zclient *zclient, u_char keep,
1640 uint32_t chunk_size, uint32_t *start, uint32_t *end)
fea12efb 1641{
d62a17ae 1642 int ret;
1643 struct stream *s;
1644 u_char response_keep;
1645
1646 if (zclient_debug)
1647 zlog_debug("Getting Label Chunk");
1648
1649 if (zclient->sock < 0)
1650 return -1;
1651
1652 /* send request */
1653 s = zclient->obuf;
1654 stream_reset(s);
1655 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1656 /* keep */
1657 stream_putc(s, keep);
1658 /* chunk size */
1659 stream_putl(s, chunk_size);
1660 /* Put length at the first point of the stream. */
1661 stream_putw_at(s, 0, stream_get_endp(s));
1662
1663 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1664 if (ret < 0) {
1665 zlog_err("%s: can't write to zclient->sock", __func__);
1666 close(zclient->sock);
1667 zclient->sock = -1;
1668 return -1;
1669 }
1670 if (ret == 0) {
1671 zlog_err("%s: zclient->sock connection closed", __func__);
1672 close(zclient->sock);
1673 zclient->sock = -1;
1674 return -1;
1675 }
1676 if (zclient_debug)
1677 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__,
1678 ret);
1679
1680 /* read response */
1681 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
1682 return -1;
1683
1684 s = zclient->ibuf;
1685 /* keep */
1686 response_keep = stream_getc(s);
1687 /* start and end labels */
1688 *start = stream_getl(s);
1689 *end = stream_getl(s);
1690
1691 /* not owning this response */
1692 if (keep != response_keep) {
1693 zlog_err(
1694 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1695 __func__, *start, *end, keep, response_keep);
1696 }
1697 /* sanity */
1698 if (*start > *end || *start < MPLS_MIN_UNRESERVED_LABEL
1699 || *end > MPLS_MAX_UNRESERVED_LABEL) {
1700 zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start,
1701 *end);
1702 return -1;
1703 }
1704
1705 if (zclient_debug)
1706 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end,
1707 response_keep);
1708
1709 return 0;
fea12efb 1710}
1711
1712/**
1713 * Function to release a label chunk
1714 *
1715 * @param zclient Zclient used to connect to label manager (zebra)
1716 * @param start First label of chunk
1717 * @param end Last label of chunk
1718 * @result 0 on success, -1 otherwise
1719 */
d62a17ae 1720int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
1721 uint32_t end)
fea12efb 1722{
d62a17ae 1723 int ret;
1724 struct stream *s;
1725
1726 if (zclient_debug)
1727 zlog_debug("Releasing Label Chunk");
1728
1729 if (zclient->sock < 0)
1730 return -1;
1731
1732 /* send request */
1733 s = zclient->obuf;
1734 stream_reset(s);
1735 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
1736
1737 /* start */
1738 stream_putl(s, start);
1739 /* end */
1740 stream_putl(s, end);
1741
1742 /* Put length at the first point of the stream. */
1743 stream_putw_at(s, 0, stream_get_endp(s));
1744
1745 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1746 if (ret < 0) {
1747 zlog_err("%s: can't write to zclient->sock", __func__);
1748 close(zclient->sock);
1749 zclient->sock = -1;
1750 return -1;
1751 }
1752 if (ret == 0) {
1753 zlog_err("%s: zclient->sock connection closed", __func__);
1754 close(zclient->sock);
1755 zclient->sock = -1;
1756 return -1;
1757 }
1758
1759 return 0;
fea12efb 1760}
c8e264b6 1761
6833ae01 1762int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
1763{
1764 struct stream *s;
1765
1766 /* Reset stream. */
1767 s = zclient->obuf;
1768 stream_reset(s);
1769
1770 zclient_create_header(s, command, VRF_DEFAULT);
1771 stream_write(s, pw->ifname, IF_NAMESIZE);
1772 stream_putl(s, pw->ifindex);
1773
1774 /* Put type */
1775 stream_putl(s, pw->type);
1776
1777 /* Put nexthop */
1778 stream_putl(s, pw->af);
1779 switch (pw->af) {
1780 case AF_INET:
1781 stream_put_in_addr(s, &pw->nexthop.ipv4);
1782 break;
1783 case AF_INET6:
1784 stream_write(s, (u_char *)&pw->nexthop.ipv6, 16);
1785 break;
1786 default:
1787 zlog_err("%s: unknown af", __func__);
1788 return -1;
1789 }
1790
1791 /* Put labels */
1792 stream_putl(s, pw->local_label);
1793 stream_putl(s, pw->remote_label);
1794
1795 /* Put flags */
1796 stream_putc(s, pw->flags);
1797
1798 /* Protocol specific fields */
1799 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
1800
1801 /* Put length at the first point of the stream. */
1802 stream_putw_at(s, 0, stream_get_endp(s));
1803
1804 return zclient_send_message(zclient);
1805}
1806
1807/*
1808 * Receive PW status update from Zebra and send it to LDE process.
1809 */
1810void zebra_read_pw_status_update(int command, struct zclient *zclient,
1811 zebra_size_t length, vrf_id_t vrf_id,
1812 struct zapi_pw_status *pw)
1813{
1814 struct stream *s;
1815
1816 memset(pw, 0, sizeof(struct zapi_pw_status));
1817 s = zclient->ibuf;
1818
1819 /* Get data. */
1820 stream_get(pw->ifname, s, IF_NAMESIZE);
1821 pw->ifindex = stream_getl(s);
1822 pw->status = stream_getl(s);
1823}
1824
718e3744 1825/* Zebra client message read function. */
d62a17ae 1826static int zclient_read(struct thread *thread)
718e3744 1827{
d62a17ae 1828 size_t already;
1829 uint16_t length, command;
1830 uint8_t marker, version;
1831 vrf_id_t vrf_id;
1832 struct zclient *zclient;
1833
1834 /* Get socket to zebra. */
1835 zclient = THREAD_ARG(thread);
1836 zclient->t_read = NULL;
1837
1838 /* Read zebra header (if we don't have it already). */
1839 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
1840 ssize_t nbyte;
1841 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1842 ZEBRA_HEADER_SIZE - already))
1843 == 0)
1844 || (nbyte == -1)) {
1845 if (zclient_debug)
1846 zlog_debug(
1847 "zclient connection closed socket [%d].",
1848 zclient->sock);
1849 return zclient_failed(zclient);
1850 }
1851 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
1852 /* Try again later. */
1853 zclient_event(ZCLIENT_READ, zclient);
1854 return 0;
1855 }
1856 already = ZEBRA_HEADER_SIZE;
634f9ea2 1857 }
d62a17ae 1858
1859 /* Reset to read from the beginning of the incoming packet. */
1860 stream_set_getp(zclient->ibuf, 0);
1861
1862 /* Fetch header values. */
1863 length = stream_getw(zclient->ibuf);
1864 marker = stream_getc(zclient->ibuf);
1865 version = stream_getc(zclient->ibuf);
1866 vrf_id = stream_getw(zclient->ibuf);
1867 command = stream_getw(zclient->ibuf);
1868
1869 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
1870 zlog_err(
1871 "%s: socket %d version mismatch, marker %d, version %d",
1872 __func__, zclient->sock, marker, version);
1873 return zclient_failed(zclient);
634f9ea2 1874 }
d62a17ae 1875
1876 if (length < ZEBRA_HEADER_SIZE) {
1877 zlog_err("%s: socket %d message length %u is less than %d ",
1878 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1879 return zclient_failed(zclient);
634f9ea2 1880 }
d62a17ae 1881
1882 /* Length check. */
1883 if (length > STREAM_SIZE(zclient->ibuf)) {
1884 struct stream *ns;
1885 zlog_warn(
1886 "%s: message size %u exceeds buffer size %lu, expanding...",
1887 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1888 ns = stream_new(length);
1889 stream_copy(ns, zclient->ibuf);
1890 stream_free(zclient->ibuf);
1891 zclient->ibuf = ns;
1892 }
1893
1894 /* Read rest of zebra packet. */
1895 if (already < length) {
1896 ssize_t nbyte;
1897 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1898 length - already))
1899 == 0)
1900 || (nbyte == -1)) {
1901 if (zclient_debug)
1902 zlog_debug(
1903 "zclient connection closed socket [%d].",
1904 zclient->sock);
1905 return zclient_failed(zclient);
1906 }
1907 if (nbyte != (ssize_t)(length - already)) {
1908 /* Try again later. */
1909 zclient_event(ZCLIENT_READ, zclient);
1910 return 0;
1911 }
1912 }
1913
1914 length -= ZEBRA_HEADER_SIZE;
1915
1916 if (zclient_debug)
1917 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
1918 (void *)zclient, command, vrf_id);
1919
1920 switch (command) {
1921 case ZEBRA_ROUTER_ID_UPDATE:
1922 if (zclient->router_id_update)
1923 (*zclient->router_id_update)(command, zclient, length,
1924 vrf_id);
1925 break;
1926 case ZEBRA_VRF_ADD:
1927 zclient_vrf_add(zclient, vrf_id);
1928 break;
1929 case ZEBRA_VRF_DELETE:
1930 zclient_vrf_delete(zclient, vrf_id);
1931 break;
1932 case ZEBRA_INTERFACE_ADD:
1933 if (zclient->interface_add)
1934 (*zclient->interface_add)(command, zclient, length,
1935 vrf_id);
1936 break;
1937 case ZEBRA_INTERFACE_DELETE:
1938 if (zclient->interface_delete)
1939 (*zclient->interface_delete)(command, zclient, length,
1940 vrf_id);
1941 break;
1942 case ZEBRA_INTERFACE_ADDRESS_ADD:
1943 if (zclient->interface_address_add)
1944 (*zclient->interface_address_add)(command, zclient,
1945 length, vrf_id);
1946 break;
1947 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1948 if (zclient->interface_address_delete)
1949 (*zclient->interface_address_delete)(command, zclient,
1950 length, vrf_id);
1951 break;
1952 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1953 if (zclient->interface_bfd_dest_update)
1954 (*zclient->interface_bfd_dest_update)(command, zclient,
1955 length, vrf_id);
1956 break;
1957 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1958 if (zclient->interface_nbr_address_add)
1959 (*zclient->interface_nbr_address_add)(command, zclient,
1960 length, vrf_id);
1961 break;
1962 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1963 if (zclient->interface_nbr_address_delete)
1964 (*zclient->interface_nbr_address_delete)(
1965 command, zclient, length, vrf_id);
1966 break;
1967 case ZEBRA_INTERFACE_UP:
1968 if (zclient->interface_up)
1969 (*zclient->interface_up)(command, zclient, length,
1970 vrf_id);
1971 break;
1972 case ZEBRA_INTERFACE_DOWN:
1973 if (zclient->interface_down)
1974 (*zclient->interface_down)(command, zclient, length,
1975 vrf_id);
1976 break;
1977 case ZEBRA_INTERFACE_VRF_UPDATE:
1978 if (zclient->interface_vrf_update)
1979 (*zclient->interface_vrf_update)(command, zclient,
1980 length, vrf_id);
1981 break;
1982 case ZEBRA_NEXTHOP_UPDATE:
1983 if (zclient_debug)
1984 zlog_debug("zclient rcvd nexthop update\n");
1985 if (zclient->nexthop_update)
1986 (*zclient->nexthop_update)(command, zclient, length,
1987 vrf_id);
1988 break;
1989 case ZEBRA_IMPORT_CHECK_UPDATE:
1990 if (zclient_debug)
1991 zlog_debug("zclient rcvd import check update\n");
1992 if (zclient->import_check_update)
1993 (*zclient->import_check_update)(command, zclient,
1994 length, vrf_id);
1995 break;
1996 case ZEBRA_BFD_DEST_REPLAY:
1997 if (zclient->bfd_dest_replay)
1998 (*zclient->bfd_dest_replay)(command, zclient, length,
1999 vrf_id);
2000 break;
2001 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
2002 if (zclient->redistribute_route_ipv4_add)
2003 (*zclient->redistribute_route_ipv4_add)(
2004 command, zclient, length, vrf_id);
2005 break;
2006 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
2007 if (zclient->redistribute_route_ipv4_del)
2008 (*zclient->redistribute_route_ipv4_del)(
2009 command, zclient, length, vrf_id);
2010 break;
2011 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
2012 if (zclient->redistribute_route_ipv6_add)
2013 (*zclient->redistribute_route_ipv6_add)(
2014 command, zclient, length, vrf_id);
2015 break;
2016 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
2017 if (zclient->redistribute_route_ipv6_del)
2018 (*zclient->redistribute_route_ipv6_del)(
2019 command, zclient, length, vrf_id);
2020 break;
2021 case ZEBRA_INTERFACE_LINK_PARAMS:
2022 if (zclient->interface_link_params)
2023 (*zclient->interface_link_params)(command, zclient,
2024 length);
2025 break;
2026 case ZEBRA_FEC_UPDATE:
2027 if (zclient_debug)
2028 zlog_debug("zclient rcvd fec update\n");
2029 if (zclient->fec_update)
2030 (*zclient->fec_update)(command, zclient, length);
2031 break;
2032 case ZEBRA_VNI_ADD:
2033 if (zclient->local_vni_add)
2034 (*zclient->local_vni_add)(command, zclient, length,
2035 vrf_id);
2036 break;
2037 case ZEBRA_VNI_DEL:
2038 if (zclient->local_vni_del)
2039 (*zclient->local_vni_del)(command, zclient, length,
2040 vrf_id);
2041 break;
2042 case ZEBRA_MACIP_ADD:
2043 if (zclient->local_macip_add)
2044 (*zclient->local_macip_add)(command, zclient, length,
2045 vrf_id);
2046 break;
2047 case ZEBRA_MACIP_DEL:
2048 if (zclient->local_macip_del)
2049 (*zclient->local_macip_del)(command, zclient, length,
2050 vrf_id);
2051 break;
6833ae01 2052 case ZEBRA_PW_STATUS_UPDATE:
2053 if (zclient->pw_status_update)
2054 (*zclient->pw_status_update)(command, zclient, length,
2055 vrf_id);
2056 break;
d62a17ae 2057 default:
2058 break;
634f9ea2 2059 }
d62a17ae 2060
2061 if (zclient->sock < 0)
2062 /* Connection was closed during packet processing. */
2063 return -1;
2064
2065 /* Register read thread. */
2066 stream_reset(zclient->ibuf);
2067 zclient_event(ZCLIENT_READ, zclient);
2068
2069 return 0;
718e3744 2070}
2071
d62a17ae 2072void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2073 int type, u_short instance, vrf_id_t vrf_id)
718e3744 2074{
718e3744 2075
d62a17ae 2076 if (instance) {
2077 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2078 if (redist_check_instance(
2079 &zclient->mi_redist[afi][type], instance))
2080 return;
2081 redist_add_instance(&zclient->mi_redist[afi][type],
2082 instance);
2083 } else {
2084 if (!redist_check_instance(
2085 &zclient->mi_redist[afi][type], instance))
2086 return;
2087 redist_del_instance(&zclient->mi_redist[afi][type],
2088 instance);
2089 }
2090
2091 } else {
2092 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2093 if (vrf_bitmap_check(zclient->redist[afi][type],
2094 vrf_id))
2095 return;
2096 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2097 } else {
2098 if (!vrf_bitmap_check(zclient->redist[afi][type],
2099 vrf_id))
2100 return;
2101 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2102 }
2103 }
2104
2105 if (zclient->sock > 0)
2106 zebra_redistribute_send(command, zclient, afi, type, instance,
2107 vrf_id);
718e3744 2108}
2109
718e3744 2110
d62a17ae 2111void zclient_redistribute_default(int command, struct zclient *zclient,
2112 vrf_id_t vrf_id)
718e3744 2113{
718e3744 2114
d62a17ae 2115 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2116 if (vrf_bitmap_check(zclient->default_information, vrf_id))
2117 return;
2118 vrf_bitmap_set(zclient->default_information, vrf_id);
2119 } else {
2120 if (!vrf_bitmap_check(zclient->default_information, vrf_id))
2121 return;
2122 vrf_bitmap_unset(zclient->default_information, vrf_id);
2123 }
2124
2125 if (zclient->sock > 0)
2126 zebra_message_send(zclient, command, vrf_id);
718e3744 2127}
2128
d62a17ae 2129static void zclient_event(enum event event, struct zclient *zclient)
718e3744 2130{
d62a17ae 2131 switch (event) {
2132 case ZCLIENT_SCHEDULE:
2133 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2134 &zclient->t_connect);
2135 break;
2136 case ZCLIENT_CONNECT:
2137 if (zclient_debug)
2138 zlog_debug(
2139 "zclient connect failures: %d schedule interval is now %d",
2140 zclient->fail, zclient->fail < 3 ? 10 : 60);
2141 thread_add_timer(zclient->master, zclient_connect, zclient,
2142 zclient->fail < 3 ? 10 : 60,
2143 &zclient->t_connect);
2144 break;
2145 case ZCLIENT_READ:
2146 zclient->t_read = NULL;
2147 thread_add_read(zclient->master, zclient_read, zclient,
2148 zclient->sock, &zclient->t_read);
2149 break;
2150 }
718e3744 2151}
b5114685 2152
e0ae31b8
DS
2153void zclient_interface_set_master(struct zclient *client,
2154 struct interface *master,
2155 struct interface *slave)
2156{
2157 struct stream *s;
2158
2159 s = client->obuf;
2160 stream_reset(s);
2161
2162 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2163
2164 stream_putw(s, master->vrf_id);
2165 stream_putl(s, master->ifindex);
2166 stream_putw(s, slave->vrf_id);
2167 stream_putl(s, slave->ifindex);
2168
2169 stream_putw_at(s, 0, stream_get_endp(s));
2170 zclient_send_message(client);
2171}