]> git.proxmox.com Git - mirror_frr.git/blame_incremental - lib/zclient.c
Merge pull request #3625 from donaldsharp/clist_strlen
[mirror_frr.git] / lib / zclient.c
... / ...
CommitLineData
1/* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
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 *
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
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "stream.h"
26#include "buffer.h"
27#include "network.h"
28#include "vrf.h"
29#include "vrf_int.h"
30#include "if.h"
31#include "log.h"
32#include "thread.h"
33#include "zclient.h"
34#include "memory.h"
35#include "table.h"
36#include "nexthop.h"
37#include "mpls.h"
38#include "sockopt.h"
39#include "pbr.h"
40#include "nexthop_group.h"
41#include "lib_errors.h"
42
43DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient")
44DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs")
45
46/* Zebra client events. */
47enum event { ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT };
48
49/* Prototype for event manager. */
50static void zclient_event(enum event, struct zclient *);
51
52struct sockaddr_storage zclient_addr;
53socklen_t zclient_addr_len;
54
55/* This file local debug flag. */
56int zclient_debug = 0;
57
58struct zclient_options zclient_options_default = {.receive_notify = false};
59
60/* Allocate zclient structure. */
61struct zclient *zclient_new(struct thread_master *master,
62 struct zclient_options *opt)
63{
64 struct zclient *zclient;
65 zclient = XCALLOC(MTYPE_ZCLIENT, sizeof(struct zclient));
66
67 zclient->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
68 zclient->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
69 zclient->wb = buffer_new(0);
70 zclient->master = master;
71
72 zclient->receive_notify = opt->receive_notify;
73
74 return zclient;
75}
76
77/* This function is only called when exiting, because
78 many parts of the code do not check for I/O errors, so they could
79 reference an invalid pointer if the structure was ever freed.
80
81 Free zclient structure. */
82void zclient_free(struct zclient *zclient)
83{
84 if (zclient->ibuf)
85 stream_free(zclient->ibuf);
86 if (zclient->obuf)
87 stream_free(zclient->obuf);
88 if (zclient->wb)
89 buffer_free(zclient->wb);
90
91 XFREE(MTYPE_ZCLIENT, zclient);
92}
93
94unsigned short *redist_check_instance(struct redist_proto *red,
95 unsigned short instance)
96{
97 struct listnode *node;
98 unsigned short *id;
99
100 if (!red->instances)
101 return NULL;
102
103 for (ALL_LIST_ELEMENTS_RO(red->instances, node, id))
104 if (*id == instance)
105 return id;
106
107 return NULL;
108}
109
110void redist_add_instance(struct redist_proto *red, unsigned short instance)
111{
112 unsigned short *in;
113
114 red->enabled = 1;
115
116 if (!red->instances)
117 red->instances = list_new();
118
119 in = XMALLOC(MTYPE_REDIST_INST, sizeof(unsigned short));
120 *in = instance;
121 listnode_add(red->instances, in);
122}
123
124void redist_del_instance(struct redist_proto *red, unsigned short instance)
125{
126 unsigned short *id;
127
128 id = redist_check_instance(red, instance);
129 if (!id)
130 return;
131
132 listnode_delete(red->instances, id);
133 XFREE(MTYPE_REDIST_INST, id);
134 if (!red->instances->count) {
135 red->enabled = 0;
136 list_delete(&red->instances);
137 }
138}
139
140/* Stop zebra client services. */
141void zclient_stop(struct zclient *zclient)
142{
143 afi_t afi;
144 int i;
145
146 if (zclient_debug)
147 zlog_debug("zclient stopped");
148
149 /* Stop threads. */
150 THREAD_OFF(zclient->t_read);
151 THREAD_OFF(zclient->t_connect);
152 THREAD_OFF(zclient->t_write);
153
154 /* Reset streams. */
155 stream_reset(zclient->ibuf);
156 stream_reset(zclient->obuf);
157
158 /* Empty the write buffer. */
159 buffer_reset(zclient->wb);
160
161 /* Close socket. */
162 if (zclient->sock >= 0) {
163 close(zclient->sock);
164 zclient->sock = -1;
165 }
166 zclient->fail = 0;
167
168 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
169 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
170 vrf_bitmap_free(zclient->redist[afi][i]);
171 zclient->redist[afi][i] = VRF_BITMAP_NULL;
172 }
173 redist_del_instance(
174 &zclient->mi_redist[afi][zclient->redist_default],
175 zclient->instance);
176
177 vrf_bitmap_free(zclient->default_information[afi]);
178 zclient->default_information[afi] = VRF_BITMAP_NULL;
179 }
180}
181
182void zclient_reset(struct zclient *zclient)
183{
184 afi_t afi;
185
186 zclient_stop(zclient);
187
188 for (afi = AFI_IP; afi < AFI_MAX; afi++)
189 redist_del_instance(
190 &zclient->mi_redist[afi][zclient->redist_default],
191 zclient->instance);
192
193 zclient_init(zclient, zclient->redist_default, zclient->instance,
194 zclient->privs);
195}
196
197/**
198 * Connect to zebra daemon.
199 * @param zclient a pointer to zclient structure
200 * @return socket fd just to make sure that connection established
201 * @see zclient_init
202 * @see zclient_new
203 */
204int zclient_socket_connect(struct zclient *zclient)
205{
206 int sock;
207 int ret;
208
209 /* We should think about IPv6 connection. */
210 sock = socket(zclient_addr.ss_family, SOCK_STREAM, 0);
211 if (sock < 0)
212 return -1;
213
214 set_cloexec(sock);
215
216 frr_elevate_privs(zclient->privs) {
217 setsockopt_so_sendbuf(sock, 1048576);
218 }
219
220 /* Connect to zebra. */
221 ret = connect(sock, (struct sockaddr *)&zclient_addr, zclient_addr_len);
222 if (ret < 0) {
223 if (zclient_debug)
224 zlog_debug("%s connect failure: %d(%s)",
225 __PRETTY_FUNCTION__, errno,
226 safe_strerror(errno));
227 close(sock);
228 return -1;
229 }
230
231 zclient->sock = sock;
232 return sock;
233}
234
235static int zclient_failed(struct zclient *zclient)
236{
237 zclient->fail++;
238 zclient_stop(zclient);
239 zclient_event(ZCLIENT_CONNECT, zclient);
240 return -1;
241}
242
243static int zclient_flush_data(struct thread *thread)
244{
245 struct zclient *zclient = THREAD_ARG(thread);
246
247 zclient->t_write = NULL;
248 if (zclient->sock < 0)
249 return -1;
250 switch (buffer_flush_available(zclient->wb, zclient->sock)) {
251 case BUFFER_ERROR:
252 flog_err(
253 EC_LIB_ZAPI_SOCKET,
254 "%s: buffer_flush_available failed on zclient fd %d, closing",
255 __func__, zclient->sock);
256 return zclient_failed(zclient);
257 break;
258 case BUFFER_PENDING:
259 zclient->t_write = NULL;
260 thread_add_write(zclient->master, zclient_flush_data, zclient,
261 zclient->sock, &zclient->t_write);
262 break;
263 case BUFFER_EMPTY:
264 break;
265 }
266 return 0;
267}
268
269int zclient_send_message(struct zclient *zclient)
270{
271 if (zclient->sock < 0)
272 return -1;
273 switch (buffer_write(zclient->wb, zclient->sock,
274 STREAM_DATA(zclient->obuf),
275 stream_get_endp(zclient->obuf))) {
276 case BUFFER_ERROR:
277 flog_err(EC_LIB_ZAPI_SOCKET,
278 "%s: buffer_write failed to zclient fd %d, closing",
279 __func__, zclient->sock);
280 return zclient_failed(zclient);
281 break;
282 case BUFFER_EMPTY:
283 THREAD_OFF(zclient->t_write);
284 break;
285 case BUFFER_PENDING:
286 thread_add_write(zclient->master, zclient_flush_data, zclient,
287 zclient->sock, &zclient->t_write);
288 break;
289 }
290 return 0;
291}
292
293void zclient_create_header(struct stream *s, uint16_t command, vrf_id_t vrf_id)
294{
295 /* length placeholder, caller can update */
296 stream_putw(s, ZEBRA_HEADER_SIZE);
297 stream_putc(s, ZEBRA_HEADER_MARKER);
298 stream_putc(s, ZSERV_VERSION);
299 stream_putl(s, vrf_id);
300 stream_putw(s, command);
301}
302
303int zclient_read_header(struct stream *s, int sock, uint16_t *size,
304 uint8_t *marker, uint8_t *version, vrf_id_t *vrf_id,
305 uint16_t *cmd)
306{
307 if (stream_read(s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
308 return -1;
309
310 STREAM_GETW(s, *size);
311 *size -= ZEBRA_HEADER_SIZE;
312 STREAM_GETC(s, *marker);
313 STREAM_GETC(s, *version);
314 STREAM_GETL(s, *vrf_id);
315 STREAM_GETW(s, *cmd);
316
317 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER) {
318 flog_err(
319 EC_LIB_ZAPI_MISSMATCH,
320 "%s: socket %d version mismatch, marker %d, version %d",
321 __func__, sock, *marker, *version);
322 return -1;
323 }
324
325 if (*size && stream_read(s, sock, *size) != *size)
326 return -1;
327
328stream_failure:
329 return 0;
330}
331
332bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr)
333{
334 STREAM_GETW(zmsg, hdr->length);
335 STREAM_GETC(zmsg, hdr->marker);
336 STREAM_GETC(zmsg, hdr->version);
337 STREAM_GETL(zmsg, hdr->vrf_id);
338 STREAM_GETW(zmsg, hdr->command);
339 return true;
340stream_failure:
341 return false;
342}
343
344/* Send simple Zebra message. */
345static int zebra_message_send(struct zclient *zclient, int command,
346 vrf_id_t vrf_id)
347{
348 struct stream *s;
349
350 /* Get zclient output buffer. */
351 s = zclient->obuf;
352 stream_reset(s);
353
354 /* Send very simple command only Zebra message. */
355 zclient_create_header(s, command, vrf_id);
356
357 return zclient_send_message(zclient);
358}
359
360static int zebra_hello_send(struct zclient *zclient)
361{
362 struct stream *s;
363
364 if (zclient->redist_default) {
365 s = zclient->obuf;
366 stream_reset(s);
367
368 /* The VRF ID in the HELLO message is always 0. */
369 zclient_create_header(s, ZEBRA_HELLO, VRF_DEFAULT);
370 stream_putc(s, zclient->redist_default);
371 stream_putw(s, zclient->instance);
372 if (zclient->receive_notify)
373 stream_putc(s, 1);
374 else
375 stream_putc(s, 0);
376
377 stream_putw_at(s, 0, stream_get_endp(s));
378 return zclient_send_message(zclient);
379 }
380
381 return 0;
382}
383
384void zclient_send_vrf_label(struct zclient *zclient, vrf_id_t vrf_id, afi_t afi,
385 mpls_label_t label, enum lsp_types_t ltype)
386{
387 struct stream *s;
388
389 s = zclient->obuf;
390 stream_reset(s);
391
392 zclient_create_header(s, ZEBRA_VRF_LABEL, vrf_id);
393 stream_putl(s, label);
394 stream_putc(s, afi);
395 stream_putc(s, ltype);
396 stream_putw_at(s, 0, stream_get_endp(s));
397 zclient_send_message(zclient);
398}
399
400/* Send register requests to zebra daemon for the information in a VRF. */
401void zclient_send_reg_requests(struct zclient *zclient, vrf_id_t vrf_id)
402{
403 int i;
404 afi_t afi;
405
406 /* If not connected to the zebra yet. */
407 if (zclient->sock < 0)
408 return;
409
410 if (zclient_debug)
411 zlog_debug("%s: send register messages for VRF %u", __func__,
412 vrf_id);
413
414 /* We need router-id information. */
415 zebra_message_send(zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
416
417 /* We need interface information. */
418 zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, vrf_id);
419
420 /* Set unwanted redistribute route. */
421 for (afi = AFI_IP; afi < AFI_MAX; afi++)
422 vrf_bitmap_set(zclient->redist[afi][zclient->redist_default],
423 vrf_id);
424
425 /* Flush all redistribute request. */
426 if (vrf_id == VRF_DEFAULT) {
427 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
428 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
429 if (!zclient->mi_redist[afi][i].enabled)
430 continue;
431
432 struct listnode *node;
433 unsigned short *id;
434
435 for (ALL_LIST_ELEMENTS_RO(
436 zclient->mi_redist[afi][i]
437 .instances,
438 node, id))
439 if (!(i == zclient->redist_default
440 && *id == zclient->instance))
441 zebra_redistribute_send(
442 ZEBRA_REDISTRIBUTE_ADD,
443 zclient, afi, i, *id,
444 VRF_DEFAULT);
445 }
446 }
447 }
448
449 /* Resend all redistribute request. */
450 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
451 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
452 if (i != zclient->redist_default
453 && vrf_bitmap_check(zclient->redist[afi][i],
454 vrf_id))
455 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD,
456 zclient, afi, i, 0,
457 vrf_id);
458
459 /* If default information is needed. */
460 if (vrf_bitmap_check(zclient->default_information[afi],
461 VRF_DEFAULT))
462 zebra_redistribute_default_send(
463 ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient, afi,
464 vrf_id);
465 }
466}
467
468/* Send unregister requests to zebra daemon for the information in a VRF. */
469void zclient_send_dereg_requests(struct zclient *zclient, vrf_id_t vrf_id)
470{
471 int i;
472 afi_t afi;
473
474 /* If not connected to the zebra yet. */
475 if (zclient->sock < 0)
476 return;
477
478 if (zclient_debug)
479 zlog_debug("%s: send deregister messages for VRF %u", __func__,
480 vrf_id);
481
482 /* We need router-id information. */
483 zebra_message_send(zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id);
484
485 /* We need interface information. */
486 zebra_message_send(zclient, ZEBRA_INTERFACE_DELETE, vrf_id);
487
488 /* Set unwanted redistribute route. */
489 for (afi = AFI_IP; afi < AFI_MAX; afi++)
490 vrf_bitmap_unset(zclient->redist[afi][zclient->redist_default],
491 vrf_id);
492
493 /* Flush all redistribute request. */
494 if (vrf_id == VRF_DEFAULT) {
495 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
496 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
497 if (!zclient->mi_redist[afi][i].enabled)
498 continue;
499
500 struct listnode *node;
501 unsigned short *id;
502
503 for (ALL_LIST_ELEMENTS_RO(
504 zclient->mi_redist[afi][i]
505 .instances,
506 node, id))
507 if (!(i == zclient->redist_default
508 && *id == zclient->instance))
509 zebra_redistribute_send(
510 ZEBRA_REDISTRIBUTE_DELETE,
511 zclient, afi, i, *id,
512 VRF_DEFAULT);
513 }
514 }
515 }
516
517 /* Flush all redistribute request. */
518 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
519 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
520 if (i != zclient->redist_default
521 && vrf_bitmap_check(zclient->redist[afi][i],
522 vrf_id))
523 zebra_redistribute_send(
524 ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
525 i, 0, vrf_id);
526
527 /* If default information is needed. */
528 if (vrf_bitmap_check(zclient->default_information[afi],
529 VRF_DEFAULT))
530 zebra_redistribute_default_send(
531 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient, afi,
532 vrf_id);
533 }
534}
535
536/* Send request to zebra daemon to start or stop RA. */
537void zclient_send_interface_radv_req(struct zclient *zclient, vrf_id_t vrf_id,
538 struct interface *ifp, int enable,
539 int ra_interval)
540{
541 struct stream *s;
542
543 /* If not connected to the zebra yet. */
544 if (zclient->sock < 0)
545 return;
546
547 /* Form and send message. */
548 s = zclient->obuf;
549 stream_reset(s);
550
551 if (enable)
552 zclient_create_header(s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id);
553 else
554 zclient_create_header(s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id);
555
556 stream_putl(s, ifp->ifindex);
557 stream_putl(s, ra_interval);
558
559 stream_putw_at(s, 0, stream_get_endp(s));
560
561 zclient_send_message(zclient);
562}
563
564/* Make connection to zebra daemon. */
565int zclient_start(struct zclient *zclient)
566{
567 if (zclient_debug)
568 zlog_info("zclient_start is called");
569
570 /* If already connected to the zebra. */
571 if (zclient->sock >= 0)
572 return 0;
573
574 /* Check connect thread. */
575 if (zclient->t_connect)
576 return 0;
577
578 if (zclient_socket_connect(zclient) < 0) {
579 if (zclient_debug)
580 zlog_debug("zclient connection fail");
581 zclient->fail++;
582 zclient_event(ZCLIENT_CONNECT, zclient);
583 return -1;
584 }
585
586 if (set_nonblocking(zclient->sock) < 0)
587 flog_err(EC_LIB_ZAPI_SOCKET, "%s: set_nonblocking(%d) failed",
588 __func__, zclient->sock);
589
590 /* Clear fail count. */
591 zclient->fail = 0;
592 if (zclient_debug)
593 zlog_debug("zclient connect success with socket [%d]",
594 zclient->sock);
595
596 /* Create read thread. */
597 zclient_event(ZCLIENT_READ, zclient);
598
599 zebra_hello_send(zclient);
600
601 /* Inform the successful connection. */
602 if (zclient->zebra_connected)
603 (*zclient->zebra_connected)(zclient);
604
605 return 0;
606}
607
608/* Initialize zebra client. Argument redist_default is unwanted
609 redistribute route type. */
610void zclient_init(struct zclient *zclient, int redist_default,
611 unsigned short instance, struct zebra_privs_t *privs)
612{
613 int afi, i;
614
615 /* Set -1 to the default socket value. */
616 zclient->sock = -1;
617 zclient->privs = privs;
618
619 /* Clear redistribution flags. */
620 for (afi = AFI_IP; afi < AFI_MAX; afi++)
621 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
622 zclient->redist[afi][i] = vrf_bitmap_init();
623
624 /* Set unwanted redistribute route. bgpd does not need BGP route
625 redistribution. */
626 zclient->redist_default = redist_default;
627 zclient->instance = instance;
628 /* Pending: make afi(s) an arg. */
629 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
630 redist_add_instance(&zclient->mi_redist[afi][redist_default],
631 instance);
632
633 /* Set default-information redistribute to zero. */
634 zclient->default_information[afi] = vrf_bitmap_init();
635 }
636
637 if (zclient_debug)
638 zlog_debug("zclient_start is called");
639
640 zclient_event(ZCLIENT_SCHEDULE, zclient);
641}
642
643/* This function is a wrapper function for calling zclient_start from
644 timer or event thread. */
645static int zclient_connect(struct thread *t)
646{
647 struct zclient *zclient;
648
649 zclient = THREAD_ARG(t);
650 zclient->t_connect = NULL;
651
652 if (zclient_debug)
653 zlog_debug("zclient_connect is called");
654
655 return zclient_start(zclient);
656}
657
658int zclient_send_rnh(struct zclient *zclient, int command, struct prefix *p,
659 bool exact_match, vrf_id_t vrf_id)
660{
661 struct stream *s;
662
663 s = zclient->obuf;
664 stream_reset(s);
665 zclient_create_header(s, command, vrf_id);
666 stream_putc(s, (exact_match) ? 1 : 0);
667
668 stream_putw(s, PREFIX_FAMILY(p));
669 stream_putc(s, p->prefixlen);
670 switch (PREFIX_FAMILY(p)) {
671 case AF_INET:
672 stream_put_in_addr(s, &p->u.prefix4);
673 break;
674 case AF_INET6:
675 stream_put(s, &(p->u.prefix6), 16);
676 break;
677 default:
678 break;
679 }
680 stream_putw_at(s, 0, stream_get_endp(s));
681
682 return zclient_send_message(zclient);
683}
684
685/*
686 * "xdr_encode"-like interface that allows daemon (client) to send
687 * a message to zebra server for a route that needs to be
688 * added/deleted to the kernel. Info about the route is specified
689 * by the caller in a struct zapi_route. zapi_route_encode() then writes
690 * the info down the zclient socket using the stream_* functions.
691 *
692 * The corresponding read ("xdr_decode") function on the server
693 * side is zapi_route_decode().
694 *
695 * 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
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * | Length (2) | Command | Route Type |
698 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * | ZEBRA Flags | Message Flags | Prefix length |
700 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 * | Destination IPv4 Prefix for route |
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * | Nexthop count |
704 * +-+-+-+-+-+-+-+-+
705 *
706 *
707 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
708 * described, as per the Nexthop count. Each nexthop described as:
709 *
710 * +-+-+-+-+-+-+-+-+
711 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
713 * | IPv4 Nexthop address or Interface Index number |
714 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
715 *
716 * Alternatively, if the route is a blackhole route, then Nexthop count
717 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
718 * nexthop.
719 *
720 * The original struct zapi_route_*() infrastructure was built around
721 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
722 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
723 * nexthops into zapi_route_encode() using the same zapi_route structure.
724 * This is done by setting zapi_route fields as follows:
725 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
726 * - .nexthop_num == .ifindex_num
727 * - .nexthop and .ifindex are filled with gate and ifindex parts of
728 * each compound nexthop, both in the same order
729 *
730 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
731 * byte value.
732 *
733 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
734 * byte value.
735 *
736 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
737 *
738 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
739 *
740 * XXX: No attention paid to alignment.
741 */
742int zclient_route_send(uint8_t cmd, struct zclient *zclient,
743 struct zapi_route *api)
744{
745 if (zapi_route_encode(cmd, zclient->obuf, api) < 0)
746 return -1;
747 return zclient_send_message(zclient);
748}
749
750int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api)
751{
752 struct zapi_nexthop *api_nh;
753 int i;
754 int psize;
755
756 stream_reset(s);
757 zclient_create_header(s, cmd, api->vrf_id);
758
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_putc(s, api->safi);
764
765 /* Put prefix information. */
766 stream_putc(s, api->prefix.family);
767 psize = PSIZE(api->prefix.prefixlen);
768 stream_putc(s, api->prefix.prefixlen);
769 stream_write(s, (uint8_t *)&api->prefix.u.prefix, psize);
770
771 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
772 psize = PSIZE(api->src_prefix.prefixlen);
773 stream_putc(s, api->src_prefix.prefixlen);
774 stream_write(s, (uint8_t *)&api->src_prefix.prefix, psize);
775 }
776
777 /* Nexthops. */
778 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
779 /* limit the number of nexthops if necessary */
780 if (api->nexthop_num > MULTIPATH_NUM) {
781 char buf[PREFIX2STR_BUFFER];
782
783 prefix2str(&api->prefix, buf, sizeof(buf));
784 flog_err(
785 EC_LIB_ZAPI_ENCODE,
786 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
787 __func__, buf, api->nexthop_num, MULTIPATH_NUM);
788 return -1;
789 }
790
791 stream_putw(s, api->nexthop_num);
792
793 for (i = 0; i < api->nexthop_num; i++) {
794 api_nh = &api->nexthops[i];
795
796 stream_putl(s, api_nh->vrf_id);
797 stream_putc(s, api_nh->type);
798 switch (api_nh->type) {
799 case NEXTHOP_TYPE_BLACKHOLE:
800 stream_putc(s, api_nh->bh_type);
801 break;
802 case NEXTHOP_TYPE_IPV4:
803 stream_put_in_addr(s, &api_nh->gate.ipv4);
804 break;
805 case NEXTHOP_TYPE_IPV4_IFINDEX:
806 stream_put_in_addr(s, &api_nh->gate.ipv4);
807 stream_putl(s, api_nh->ifindex);
808 break;
809 case NEXTHOP_TYPE_IFINDEX:
810 stream_putl(s, api_nh->ifindex);
811 break;
812 case NEXTHOP_TYPE_IPV6:
813 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
814 16);
815 break;
816 case NEXTHOP_TYPE_IPV6_IFINDEX:
817 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
818 16);
819 stream_putl(s, api_nh->ifindex);
820 break;
821 }
822
823 /* MPLS labels for BGP-LU or Segment Routing */
824 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
825 if (api_nh->label_num > MPLS_MAX_LABELS) {
826 char buf[PREFIX2STR_BUFFER];
827 prefix2str(&api->prefix, buf,
828 sizeof(buf));
829 flog_err(EC_LIB_ZAPI_ENCODE,
830 "%s: prefix %s: can't encode "
831 "%u labels (maximum is %u)",
832 __func__, buf,
833 api_nh->label_num,
834 MPLS_MAX_LABELS);
835 return -1;
836 }
837
838 stream_putc(s, api_nh->label_num);
839 stream_put(s, &api_nh->labels[0],
840 api_nh->label_num
841 * sizeof(mpls_label_t));
842 }
843
844 /* Router MAC for EVPN routes. */
845 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
846 stream_put(s, &(api_nh->rmac),
847 sizeof(struct ethaddr));
848 }
849 }
850
851 /* Attributes. */
852 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
853 stream_putc(s, api->distance);
854 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
855 stream_putl(s, api->metric);
856 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
857 stream_putl(s, api->tag);
858 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
859 stream_putl(s, api->mtu);
860 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
861 stream_putl(s, api->tableid);
862
863 /* Put length at the first point of the stream. */
864 stream_putw_at(s, 0, stream_get_endp(s));
865
866 return 0;
867}
868
869int zapi_route_decode(struct stream *s, struct zapi_route *api)
870{
871 struct zapi_nexthop *api_nh;
872 int i;
873
874 memset(api, 0, sizeof(*api));
875
876 /* Type, flags, message. */
877 STREAM_GETC(s, api->type);
878 if (api->type > ZEBRA_ROUTE_MAX) {
879 flog_err(EC_LIB_ZAPI_ENCODE,
880 "%s: Specified route type: %d is not a legal value\n",
881 __PRETTY_FUNCTION__, api->type);
882 return -1;
883 }
884
885 STREAM_GETW(s, api->instance);
886 STREAM_GETL(s, api->flags);
887 STREAM_GETC(s, api->message);
888 STREAM_GETC(s, api->safi);
889
890 /* Prefix. */
891 STREAM_GETC(s, api->prefix.family);
892 STREAM_GETC(s, api->prefix.prefixlen);
893 switch (api->prefix.family) {
894 case AF_INET:
895 if (api->prefix.prefixlen > IPV4_MAX_PREFIXLEN) {
896 flog_err(
897 EC_LIB_ZAPI_ENCODE,
898 "%s: V4 prefixlen is %d which should not be more than 32",
899 __PRETTY_FUNCTION__, api->prefix.prefixlen);
900 return -1;
901 }
902 break;
903 case AF_INET6:
904 if (api->prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
905 flog_err(
906 EC_LIB_ZAPI_ENCODE,
907 "%s: v6 prefixlen is %d which should not be more than 128",
908 __PRETTY_FUNCTION__, api->prefix.prefixlen);
909 return -1;
910 }
911 break;
912 default:
913 flog_err(EC_LIB_ZAPI_ENCODE,
914 "%s: Specified family %d is not v4 or v6",
915 __PRETTY_FUNCTION__, api->prefix.family);
916 return -1;
917 }
918 STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen));
919
920 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
921 api->src_prefix.family = AF_INET6;
922 STREAM_GETC(s, api->src_prefix.prefixlen);
923 if (api->src_prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
924 flog_err(
925 EC_LIB_ZAPI_ENCODE,
926 "%s: SRC Prefix prefixlen received: %d is too large",
927 __PRETTY_FUNCTION__, api->src_prefix.prefixlen);
928 return -1;
929 }
930 STREAM_GET(&api->src_prefix.prefix, s,
931 PSIZE(api->src_prefix.prefixlen));
932
933 if (api->prefix.family != AF_INET6
934 || api->src_prefix.prefixlen == 0) {
935 flog_err(
936 EC_LIB_ZAPI_ENCODE,
937 "%s: SRC prefix specified in some manner that makes no sense",
938 __PRETTY_FUNCTION__);
939 return -1;
940 }
941 }
942
943 /* Nexthops. */
944 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
945 STREAM_GETW(s, api->nexthop_num);
946 if (api->nexthop_num > MULTIPATH_NUM) {
947 flog_err(EC_LIB_ZAPI_ENCODE,
948 "%s: invalid number of nexthops (%u)",
949 __func__, api->nexthop_num);
950 return -1;
951 }
952
953 for (i = 0; i < api->nexthop_num; i++) {
954 api_nh = &api->nexthops[i];
955
956 STREAM_GETL(s, api_nh->vrf_id);
957 STREAM_GETC(s, api_nh->type);
958 switch (api_nh->type) {
959 case NEXTHOP_TYPE_BLACKHOLE:
960 STREAM_GETC(s, api_nh->bh_type);
961 break;
962 case NEXTHOP_TYPE_IPV4:
963 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
964 IPV4_MAX_BYTELEN);
965 break;
966 case NEXTHOP_TYPE_IPV4_IFINDEX:
967 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
968 IPV4_MAX_BYTELEN);
969 STREAM_GETL(s, api_nh->ifindex);
970 break;
971 case NEXTHOP_TYPE_IFINDEX:
972 STREAM_GETL(s, api_nh->ifindex);
973 break;
974 case NEXTHOP_TYPE_IPV6:
975 STREAM_GET(&api_nh->gate.ipv6, s, 16);
976 break;
977 case NEXTHOP_TYPE_IPV6_IFINDEX:
978 STREAM_GET(&api_nh->gate.ipv6, s, 16);
979 STREAM_GETL(s, api_nh->ifindex);
980 break;
981 }
982
983 /* MPLS labels for BGP-LU or Segment Routing */
984 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
985 STREAM_GETC(s, api_nh->label_num);
986
987 if (api_nh->label_num > MPLS_MAX_LABELS) {
988 flog_err(
989 EC_LIB_ZAPI_ENCODE,
990 "%s: invalid number of MPLS labels (%u)",
991 __func__, api_nh->label_num);
992 return -1;
993 }
994
995 STREAM_GET(&api_nh->labels[0], s,
996 api_nh->label_num
997 * sizeof(mpls_label_t));
998 }
999
1000 /* Router MAC for EVPN routes. */
1001 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
1002 stream_get(&(api_nh->rmac), s,
1003 sizeof(struct ethaddr));
1004 }
1005 }
1006
1007 /* Attributes. */
1008 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1009 STREAM_GETC(s, api->distance);
1010 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1011 STREAM_GETL(s, api->metric);
1012 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1013 STREAM_GETL(s, api->tag);
1014 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1015 STREAM_GETL(s, api->mtu);
1016 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1017 STREAM_GETL(s, api->tableid);
1018
1019stream_failure:
1020 return 0;
1021}
1022
1023static void zapi_encode_prefix(struct stream *s, struct prefix *p,
1024 uint8_t family)
1025{
1026 struct prefix any;
1027
1028 if (!p) {
1029 memset(&any, 0, sizeof(any));
1030 any.family = family;
1031 p = &any;
1032 }
1033
1034 stream_putc(s, p->family);
1035 stream_putc(s, p->prefixlen);
1036 stream_put(s, &p->u.prefix, prefix_blen(p));
1037}
1038
1039int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s, struct pbr_rule *zrule)
1040{
1041 stream_reset(s);
1042 zclient_create_header(s, cmd, zrule->vrf_id);
1043
1044 /*
1045 * We are sending one item at a time at the moment
1046 */
1047 stream_putl(s, 1);
1048
1049 stream_putl(s, zrule->seq);
1050 stream_putl(s, zrule->priority);
1051 stream_putl(s, zrule->unique);
1052
1053 zapi_encode_prefix(s, &(zrule->filter.src_ip),
1054 zrule->filter.src_ip.family);
1055 stream_putw(s, zrule->filter.src_port); /* src port */
1056 zapi_encode_prefix(s, &(zrule->filter.dst_ip),
1057 zrule->filter.src_ip.family);
1058 stream_putw(s, zrule->filter.dst_port); /* dst port */
1059 stream_putw(s, zrule->filter.fwmark); /* fwmark */
1060
1061 stream_putl(s, zrule->action.table);
1062 stream_putl(s, zrule->ifindex);
1063
1064 /* Put length at the first point of the stream. */
1065 stream_putw_at(s, 0, stream_get_endp(s));
1066
1067 return 0;
1068}
1069
1070bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
1071 uint32_t *tableid,
1072 enum zapi_route_notify_owner *note)
1073{
1074 uint32_t t;
1075
1076 STREAM_GET(note, s, sizeof(*note));
1077
1078 STREAM_GETC(s, p->family);
1079 STREAM_GETC(s, p->prefixlen);
1080 STREAM_GET(&p->u.prefix, s, prefix_blen(p));
1081 STREAM_GETL(s, t);
1082
1083 *tableid = t;
1084
1085 return true;
1086
1087stream_failure:
1088 return false;
1089}
1090
1091bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
1092 uint32_t *priority, uint32_t *unique,
1093 ifindex_t *ifindex,
1094 enum zapi_rule_notify_owner *note)
1095{
1096 uint32_t prio, seq, uni;
1097 ifindex_t ifi;
1098
1099 STREAM_GET(note, s, sizeof(*note));
1100
1101 STREAM_GETL(s, seq);
1102 STREAM_GETL(s, prio);
1103 STREAM_GETL(s, uni);
1104 STREAM_GETL(s, ifi);
1105
1106 if (zclient_debug)
1107 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__, seq, prio,
1108 uni, ifi);
1109 *seqno = seq;
1110 *priority = prio;
1111 *unique = uni;
1112 *ifindex = ifi;
1113
1114 return true;
1115
1116stream_failure:
1117 return false;
1118}
1119
1120bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique,
1121 enum zapi_ipset_notify_owner *note)
1122{
1123 uint32_t uni;
1124
1125 STREAM_GET(note, s, sizeof(*note));
1126
1127 STREAM_GETL(s, uni);
1128
1129 if (zclient_debug)
1130 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1131 *unique = uni;
1132
1133 return true;
1134
1135stream_failure:
1136 return false;
1137}
1138
1139bool zapi_ipset_entry_notify_decode(struct stream *s, uint32_t *unique,
1140 char *ipset_name,
1141 enum zapi_ipset_entry_notify_owner *note)
1142{
1143 uint32_t uni;
1144
1145 STREAM_GET(note, s, sizeof(*note));
1146
1147 STREAM_GETL(s, uni);
1148
1149 STREAM_GET(ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
1150
1151 if (zclient_debug)
1152 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1153 *unique = uni;
1154
1155 return true;
1156
1157stream_failure:
1158 return false;
1159}
1160
1161bool zapi_iptable_notify_decode(struct stream *s,
1162 uint32_t *unique,
1163 enum zapi_iptable_notify_owner *note)
1164{
1165 uint32_t uni;
1166
1167 STREAM_GET(note, s, sizeof(*note));
1168
1169 STREAM_GETL(s, uni);
1170
1171 if (zclient_debug)
1172 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1173 *unique = uni;
1174
1175 return true;
1176
1177stream_failure:
1178 return false;
1179}
1180
1181struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh)
1182{
1183 struct nexthop *n = nexthop_new();
1184
1185 n->type = znh->type;
1186 n->vrf_id = znh->vrf_id;
1187 n->ifindex = znh->ifindex;
1188 n->gate = znh->gate;
1189
1190 /*
1191 * This function currently handles labels
1192 */
1193 if (znh->label_num) {
1194 nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num,
1195 znh->labels);
1196 }
1197
1198 return n;
1199}
1200
1201bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr)
1202{
1203 uint32_t i;
1204
1205 memset(nhr, 0, sizeof(*nhr));
1206
1207 STREAM_GETW(s, nhr->prefix.family);
1208 STREAM_GETC(s, nhr->prefix.prefixlen);
1209 switch (nhr->prefix.family) {
1210 case AF_INET:
1211 STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1212 break;
1213 case AF_INET6:
1214 STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN);
1215 break;
1216 default:
1217 break;
1218 }
1219
1220 STREAM_GETC(s, nhr->type);
1221 STREAM_GETW(s, nhr->instance);
1222 STREAM_GETC(s, nhr->distance);
1223 STREAM_GETL(s, nhr->metric);
1224 STREAM_GETC(s, nhr->nexthop_num);
1225
1226 for (i = 0; i < nhr->nexthop_num; i++) {
1227 STREAM_GETL(s, nhr->nexthops[i].vrf_id);
1228 STREAM_GETC(s, nhr->nexthops[i].type);
1229 switch (nhr->nexthops[i].type) {
1230 case NEXTHOP_TYPE_IPV4:
1231 case NEXTHOP_TYPE_IPV4_IFINDEX:
1232 STREAM_GET(&nhr->nexthops[i].gate.ipv4.s_addr, s,
1233 IPV4_MAX_BYTELEN);
1234 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1235 break;
1236 case NEXTHOP_TYPE_IFINDEX:
1237 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1238 break;
1239 case NEXTHOP_TYPE_IPV6:
1240 case NEXTHOP_TYPE_IPV6_IFINDEX:
1241 STREAM_GET(&nhr->nexthops[i].gate.ipv6, s,
1242 IPV6_MAX_BYTELEN);
1243 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1244 break;
1245 case NEXTHOP_TYPE_BLACKHOLE:
1246 break;
1247 }
1248 STREAM_GETC(s, nhr->nexthops[i].label_num);
1249 if (nhr->nexthops[i].label_num > MPLS_MAX_LABELS) {
1250 flog_err(EC_LIB_ZAPI_ENCODE,
1251 "%s: invalid number of MPLS labels (%u)",
1252 __func__, nhr->nexthops[i].label_num);
1253 return false;
1254 }
1255 if (nhr->nexthops[i].label_num)
1256 STREAM_GET(&nhr->nexthops[i].labels[0], s,
1257 nhr->nexthops[i].label_num
1258 * sizeof(mpls_label_t));
1259 }
1260
1261 return true;
1262stream_failure:
1263 return false;
1264}
1265
1266/*
1267 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1268 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1269 * then set/unset redist[type] in the client handle (a struct zserv) for the
1270 * sending client
1271 */
1272int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1273 int type, unsigned short instance, vrf_id_t vrf_id)
1274{
1275 struct stream *s;
1276
1277 s = zclient->obuf;
1278 stream_reset(s);
1279
1280 zclient_create_header(s, command, vrf_id);
1281 stream_putc(s, afi);
1282 stream_putc(s, type);
1283 stream_putw(s, instance);
1284
1285 stream_putw_at(s, 0, stream_get_endp(s));
1286
1287 return zclient_send_message(zclient);
1288}
1289
1290int zebra_redistribute_default_send(int command, struct zclient *zclient,
1291 afi_t afi, vrf_id_t vrf_id)
1292{
1293 struct stream *s;
1294
1295 s = zclient->obuf;
1296 stream_reset(s);
1297
1298 zclient_create_header(s, command, vrf_id);
1299 stream_putc(s, afi);
1300
1301 stream_putw_at(s, 0, stream_get_endp(s));
1302
1303 return zclient_send_message(zclient);
1304}
1305
1306/* Get prefix in ZServ format; family should be filled in on prefix */
1307static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
1308{
1309 size_t plen = prefix_blen(p);
1310 uint8_t c;
1311 p->prefixlen = 0;
1312
1313 if (plen == 0)
1314 return;
1315
1316 stream_get(&p->u.prefix, s, plen);
1317 STREAM_GETC(s, c);
1318 p->prefixlen = MIN(plen * 8, c);
1319
1320stream_failure:
1321 return;
1322}
1323
1324/* Router-id update from zebra daemon. */
1325void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
1326{
1327 /* Fetch interface address. */
1328 STREAM_GETC(s, rid->family);
1329
1330 zclient_stream_get_prefix(s, rid);
1331
1332stream_failure:
1333 return;
1334}
1335
1336/* Interface addition from zebra daemon. */
1337/*
1338 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1339 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1340 * 0 1 2 3
1341 * 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
1342 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1343 * | ifname |
1344 * | |
1345 * | |
1346 * | |
1347 * | |
1348 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1349 * | ifindex |
1350 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1351 * | status |
1352 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1353 * | if_flags |
1354 * | |
1355 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1356 * | metric |
1357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1358 * | speed |
1359 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1360 * | ifmtu |
1361 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1362 * | ifmtu6 |
1363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1364 * | bandwidth |
1365 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1366 * | Link Layer Type |
1367 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1368 * | Harware Address Length |
1369 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1370 * | Hardware Address if HW lenght different from 0 |
1371 * | ... max INTERFACE_HWADDR_MAX |
1372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1373 * | Link_params? | Whether a link-params follows: 1 or 0.
1374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1375 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1376 * | .... (struct if_link_params). |
1377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1378 */
1379
1380static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1381{
1382 struct vrf *vrf;
1383 char vrfname_tmp[VRF_NAMSIZ];
1384 struct vrf_data data;
1385
1386 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1387 /* Read interface name. */
1388 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1389
1390 /* Lookup/create vrf by vrf_id. */
1391 vrf = vrf_get(vrf_id, vrfname_tmp);
1392 vrf->data.l.table_id = data.l.table_id;
1393 memcpy(vrf->data.l.netns_name, data.l.netns_name, NS_NAMSIZ);
1394 /* overwrite default vrf */
1395 if (vrf_id == VRF_DEFAULT)
1396 vrf_set_default_name(vrfname_tmp, false);
1397 vrf_enable(vrf);
1398}
1399
1400static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1401{
1402 struct vrf *vrf;
1403
1404 /* Lookup vrf by vrf_id. */
1405 vrf = vrf_lookup_by_id(vrf_id);
1406
1407 /*
1408 * If a routing protocol doesn't know about a
1409 * vrf that is about to be deleted. There is
1410 * no point in attempting to delete it.
1411 */
1412 if (!vrf)
1413 return;
1414
1415 vrf_delete(vrf);
1416}
1417
1418struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
1419{
1420 struct interface *ifp;
1421 char ifname_tmp[INTERFACE_NAMSIZ];
1422
1423 /* Read interface name. */
1424 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1425
1426 /* Lookup/create interface by name. */
1427 ifp = if_get_by_name(ifname_tmp, vrf_id);
1428
1429 zebra_interface_if_set_value(s, ifp);
1430
1431 return ifp;
1432}
1433
1434/*
1435 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1436 * from zebra server. The format of this message is the same as
1437 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1438 * comments for zebra_interface_add_read), except that no sockaddr_dl
1439 * is sent at the tail of the message.
1440 */
1441struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
1442{
1443 struct interface *ifp;
1444 char ifname_tmp[INTERFACE_NAMSIZ];
1445
1446 /* Read interface name. */
1447 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1448
1449 /* Lookup this by interface index. */
1450 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
1451 if (ifp == NULL) {
1452 flog_err(EC_LIB_ZAPI_ENCODE,
1453 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1454 ifname_tmp, vrf_id);
1455 return NULL;
1456 }
1457
1458 zebra_interface_if_set_value(s, ifp);
1459
1460 return ifp;
1461}
1462
1463static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
1464{
1465
1466 if (iflp == NULL)
1467 return;
1468
1469 iflp->lp_status = stream_getl(s);
1470 iflp->te_metric = stream_getl(s);
1471 iflp->max_bw = stream_getf(s);
1472 iflp->max_rsv_bw = stream_getf(s);
1473 uint32_t bwclassnum = stream_getl(s);
1474 {
1475 unsigned int i;
1476 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1477 iflp->unrsv_bw[i] = stream_getf(s);
1478 if (i < bwclassnum)
1479 flog_err(
1480 EC_LIB_ZAPI_MISSMATCH,
1481 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1482 " - outdated library?",
1483 __func__, bwclassnum, MAX_CLASS_TYPE);
1484 }
1485 iflp->admin_grp = stream_getl(s);
1486 iflp->rmt_as = stream_getl(s);
1487 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1488
1489 iflp->av_delay = stream_getl(s);
1490 iflp->min_delay = stream_getl(s);
1491 iflp->max_delay = stream_getl(s);
1492 iflp->delay_var = stream_getl(s);
1493
1494 iflp->pkt_loss = stream_getf(s);
1495 iflp->res_bw = stream_getf(s);
1496 iflp->ava_bw = stream_getf(s);
1497 iflp->use_bw = stream_getf(s);
1498}
1499
1500struct interface *zebra_interface_link_params_read(struct stream *s)
1501{
1502 struct if_link_params *iflp;
1503 ifindex_t ifindex;
1504
1505 assert(s);
1506
1507 ifindex = stream_getl(s);
1508
1509 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
1510
1511 if (ifp == NULL) {
1512 flog_err(EC_LIB_ZAPI_ENCODE,
1513 "%s: unknown ifindex %u, shouldn't happen", __func__,
1514 ifindex);
1515 return NULL;
1516 }
1517
1518 if ((iflp = if_link_params_get(ifp)) == NULL)
1519 return NULL;
1520
1521 link_params_set_value(s, iflp);
1522
1523 return ifp;
1524}
1525
1526void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
1527{
1528 uint8_t link_params_status = 0;
1529 ifindex_t old_ifindex;
1530
1531 old_ifindex = ifp->ifindex;
1532 /* Read interface's index. */
1533 if_set_index(ifp, stream_getl(s));
1534 ifp->status = stream_getc(s);
1535
1536 /* Read interface's value. */
1537 ifp->flags = stream_getq(s);
1538 ifp->ptm_enable = stream_getc(s);
1539 ifp->ptm_status = stream_getc(s);
1540 ifp->metric = stream_getl(s);
1541 ifp->speed = stream_getl(s);
1542 ifp->mtu = stream_getl(s);
1543 ifp->mtu6 = stream_getl(s);
1544 ifp->bandwidth = stream_getl(s);
1545 ifp->ll_type = stream_getl(s);
1546 ifp->hw_addr_len = stream_getl(s);
1547 if (ifp->hw_addr_len)
1548 stream_get(ifp->hw_addr, s,
1549 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1550
1551 /* Read Traffic Engineering status */
1552 link_params_status = stream_getc(s);
1553 /* Then, Traffic Engineering parameters if any */
1554 if (link_params_status) {
1555 struct if_link_params *iflp = if_link_params_get(ifp);
1556 link_params_set_value(s, iflp);
1557 }
1558
1559 nexthop_group_interface_state_change(ifp, old_ifindex);
1560}
1561
1562size_t zebra_interface_link_params_write(struct stream *s,
1563 struct interface *ifp)
1564{
1565 size_t w;
1566 struct if_link_params *iflp;
1567 int i;
1568
1569 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1570 return 0;
1571
1572 iflp = ifp->link_params;
1573 w = 0;
1574
1575 w += stream_putl(s, iflp->lp_status);
1576
1577 w += stream_putl(s, iflp->te_metric);
1578 w += stream_putf(s, iflp->max_bw);
1579 w += stream_putf(s, iflp->max_rsv_bw);
1580
1581 w += stream_putl(s, MAX_CLASS_TYPE);
1582 for (i = 0; i < MAX_CLASS_TYPE; i++)
1583 w += stream_putf(s, iflp->unrsv_bw[i]);
1584
1585 w += stream_putl(s, iflp->admin_grp);
1586 w += stream_putl(s, iflp->rmt_as);
1587 w += stream_put_in_addr(s, &iflp->rmt_ip);
1588
1589 w += stream_putl(s, iflp->av_delay);
1590 w += stream_putl(s, iflp->min_delay);
1591 w += stream_putl(s, iflp->max_delay);
1592 w += stream_putl(s, iflp->delay_var);
1593
1594 w += stream_putf(s, iflp->pkt_loss);
1595 w += stream_putf(s, iflp->res_bw);
1596 w += stream_putf(s, iflp->ava_bw);
1597 w += stream_putf(s, iflp->use_bw);
1598
1599 return w;
1600}
1601
1602/*
1603 * format of message for address additon is:
1604 * 0
1605 * 0 1 2 3 4 5 6 7
1606 * +-+-+-+-+-+-+-+-+
1607 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1608 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1609 * | |
1610 * + +
1611 * | ifindex |
1612 * + +
1613 * | |
1614 * + +
1615 * | |
1616 * +-+-+-+-+-+-+-+-+
1617 * | ifc_flags | flags for connected address
1618 * +-+-+-+-+-+-+-+-+
1619 * | addr_family |
1620 * +-+-+-+-+-+-+-+-+
1621 * | addr... |
1622 * : :
1623 * | |
1624 * +-+-+-+-+-+-+-+-+
1625 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1626 * +-+-+-+-+-+-+-+-+
1627 * | daddr.. |
1628 * : :
1629 * | |
1630 * +-+-+-+-+-+-+-+-+
1631 */
1632
1633static int memconstant(const void *s, int c, size_t n)
1634{
1635 const uint8_t *p = s;
1636
1637 while (n-- > 0)
1638 if (*p++ != c)
1639 return 0;
1640 return 1;
1641}
1642
1643
1644struct connected *zebra_interface_address_read(int type, struct stream *s,
1645 vrf_id_t vrf_id)
1646{
1647 ifindex_t ifindex;
1648 struct interface *ifp;
1649 struct connected *ifc;
1650 struct prefix p, d, *dp;
1651 int plen;
1652 uint8_t ifc_flags;
1653
1654 memset(&p, 0, sizeof(p));
1655 memset(&d, 0, sizeof(d));
1656
1657 /* Get interface index. */
1658 ifindex = stream_getl(s);
1659
1660 /* Lookup index. */
1661 ifp = if_lookup_by_index(ifindex, vrf_id);
1662 if (ifp == NULL) {
1663 flog_err(EC_LIB_ZAPI_ENCODE,
1664 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1665 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1666 ifindex, vrf_id);
1667 return NULL;
1668 }
1669
1670 /* Fetch flag. */
1671 ifc_flags = stream_getc(s);
1672
1673 /* Fetch interface address. */
1674 d.family = p.family = stream_getc(s);
1675 plen = prefix_blen(&d);
1676
1677 zclient_stream_get_prefix(s, &p);
1678
1679 /* Fetch destination address. */
1680 stream_get(&d.u.prefix, s, plen);
1681
1682 /* N.B. NULL destination pointers are encoded as all zeroes */
1683 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1684
1685 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1686 ifc = connected_lookup_prefix_exact(ifp, &p);
1687 if (!ifc) {
1688 /* N.B. NULL destination pointers are encoded as all
1689 * zeroes */
1690 ifc = connected_add_by_prefix(ifp, &p, dp);
1691 }
1692 if (ifc) {
1693 ifc->flags = ifc_flags;
1694 if (ifc->destination)
1695 ifc->destination->prefixlen =
1696 ifc->address->prefixlen;
1697 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1698 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1699 * "peer" */
1700 char buf[PREFIX_STRLEN];
1701 flog_err(
1702 EC_LIB_ZAPI_ENCODE,
1703 "warning: interface %s address %s with peer flag set, but no peer address!",
1704 ifp->name,
1705 prefix2str(ifc->address, buf,
1706 sizeof buf));
1707 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1708 }
1709 }
1710 } else {
1711 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1712 ifc = connected_delete_by_prefix(ifp, &p);
1713 }
1714
1715 return ifc;
1716}
1717
1718/*
1719 * format of message for neighbor connected address is:
1720 * 0
1721 * 0 1 2 3 4 5 6 7
1722 * +-+-+-+-+-+-+-+-+
1723 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1724 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1725 * | |
1726 * + +
1727 * | ifindex |
1728 * + +
1729 * | |
1730 * + +
1731 * | |
1732 * +-+-+-+-+-+-+-+-+
1733 * | addr_family |
1734 * +-+-+-+-+-+-+-+-+
1735 * | addr... |
1736 * : :
1737 * | |
1738 * +-+-+-+-+-+-+-+-+
1739 * | addr_len | len of addr.
1740 * +-+-+-+-+-+-+-+-+
1741 */
1742struct nbr_connected *
1743zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
1744{
1745 unsigned int ifindex;
1746 struct interface *ifp;
1747 struct prefix p;
1748 struct nbr_connected *ifc;
1749
1750 /* Get interface index. */
1751 ifindex = stream_getl(s);
1752
1753 /* Lookup index. */
1754 ifp = if_lookup_by_index(ifindex, vrf_id);
1755 if (ifp == NULL) {
1756 flog_err(EC_LIB_ZAPI_ENCODE,
1757 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1758 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1759 : "DELETE",
1760 ifindex, vrf_id);
1761 return NULL;
1762 }
1763
1764 p.family = stream_getc(s);
1765 stream_get(&p.u.prefix, s, prefix_blen(&p));
1766 p.prefixlen = stream_getc(s);
1767
1768 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1769 /* Currently only supporting P2P links, so any new RA source
1770 address is
1771 considered as the replacement of the previously learnt
1772 Link-Local address. */
1773 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1774 ifc = nbr_connected_new();
1775 ifc->address = prefix_new();
1776 ifc->ifp = ifp;
1777 listnode_add(ifp->nbr_connected, ifc);
1778 }
1779
1780 prefix_copy(ifc->address, &p);
1781 } else {
1782 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1783
1784 ifc = nbr_connected_check(ifp, &p);
1785 if (ifc)
1786 listnode_delete(ifp->nbr_connected, ifc);
1787 }
1788
1789 return ifc;
1790}
1791
1792struct interface *zebra_interface_vrf_update_read(struct stream *s,
1793 vrf_id_t vrf_id,
1794 vrf_id_t *new_vrf_id)
1795{
1796 char ifname[INTERFACE_NAMSIZ];
1797 struct interface *ifp;
1798 vrf_id_t new_id;
1799
1800 /* Read interface name. */
1801 stream_get(ifname, s, INTERFACE_NAMSIZ);
1802
1803 /* Lookup interface. */
1804 ifp = if_lookup_by_name(ifname, vrf_id);
1805 if (ifp == NULL) {
1806 flog_err(EC_LIB_ZAPI_ENCODE,
1807 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1808 ifname, vrf_id);
1809 return NULL;
1810 }
1811
1812 /* Fetch new VRF Id. */
1813 new_id = stream_getw(s);
1814
1815 *new_vrf_id = new_id;
1816 return ifp;
1817}
1818
1819/* filter unwanted messages until the expected one arrives */
1820static int zclient_read_sync_response(struct zclient *zclient,
1821 uint16_t expected_cmd)
1822{
1823 struct stream *s;
1824 uint16_t size = -1;
1825 uint8_t marker;
1826 uint8_t version;
1827 vrf_id_t vrf_id;
1828 uint16_t cmd;
1829 fd_set readfds;
1830 int ret;
1831
1832 ret = 0;
1833 cmd = expected_cmd + 1;
1834 while (ret == 0 && cmd != expected_cmd) {
1835 s = zclient->ibuf;
1836 stream_reset(s);
1837
1838 /* wait until response arrives */
1839 FD_ZERO(&readfds);
1840 FD_SET(zclient->sock, &readfds);
1841 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1842 if (!FD_ISSET(zclient->sock, &readfds))
1843 continue;
1844 /* read response */
1845 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1846 &version, &vrf_id, &cmd);
1847 if (zclient_debug)
1848 zlog_debug("%s: Response (%d bytes) received", __func__,
1849 size);
1850 }
1851 if (ret != 0) {
1852 flog_err(EC_LIB_ZAPI_ENCODE, "%s: Invalid Sync Message Reply",
1853 __func__);
1854 return -1;
1855 }
1856
1857 return 0;
1858}
1859/**
1860 * Connect to label manager in a syncronous way
1861 *
1862 * It first writes the request to zcient output buffer and then
1863 * immediately reads the answer from the input buffer.
1864 *
1865 * @param zclient Zclient used to connect to label manager (zebra)
1866 * @param async Synchronous (0) or asynchronous (1) operation
1867 * @result Result of response
1868 */
1869int lm_label_manager_connect(struct zclient *zclient, int async)
1870{
1871 int ret;
1872 struct stream *s;
1873 uint8_t result;
1874 uint16_t cmd = async ? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC :
1875 ZEBRA_LABEL_MANAGER_CONNECT;
1876
1877 if (zclient_debug)
1878 zlog_debug("Connecting to Label Manager (LM)");
1879
1880 if (zclient->sock < 0) {
1881 zlog_debug("%s: invalid zclient socket", __func__);
1882 return -1;
1883 }
1884
1885 /* send request */
1886 s = zclient->obuf;
1887 stream_reset(s);
1888 zclient_create_header(s, cmd, VRF_DEFAULT);
1889
1890 /* proto */
1891 stream_putc(s, zclient->redist_default);
1892 /* instance */
1893 stream_putw(s, zclient->instance);
1894
1895 /* Put length at the first point of the stream. */
1896 stream_putw_at(s, 0, stream_get_endp(s));
1897
1898 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1899 if (ret < 0) {
1900 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
1901 close(zclient->sock);
1902 zclient->sock = -1;
1903 return -1;
1904 }
1905 if (ret == 0) {
1906 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
1907 close(zclient->sock);
1908 zclient->sock = -1;
1909 return -1;
1910 }
1911 if (zclient_debug)
1912 zlog_debug("LM connect request sent (%d bytes)", ret);
1913
1914 if (async)
1915 return 0;
1916
1917 /* read response */
1918 if (zclient_read_sync_response(zclient, cmd)
1919 != 0)
1920 return -1;
1921
1922 s = zclient->ibuf;
1923
1924 /* read instance and proto */
1925 uint8_t proto = stream_getc(s);
1926 uint16_t instance = stream_getw(s);
1927
1928 /* sanity */
1929 if (proto != zclient->redist_default)
1930 flog_err(
1931 EC_LIB_ZAPI_ENCODE,
1932 "Wrong proto (%u) in LM connect response. Should be %u",
1933 proto, zclient->redist_default);
1934 if (instance != zclient->instance)
1935 flog_err(
1936 EC_LIB_ZAPI_ENCODE,
1937 "Wrong instId (%u) in LM connect response. Should be %u",
1938 instance, zclient->instance);
1939
1940 /* result code */
1941 result = stream_getc(s);
1942 if (zclient_debug)
1943 zlog_debug("LM connect-response received, result %u", result);
1944
1945 return (int)result;
1946}
1947
1948/*
1949 * Asynchronous label chunk request
1950 *
1951 * @param zclient Zclient used to connect to label manager (zebra)
1952 * @param keep Avoid garbage collection
1953 * @param chunk_size Amount of labels requested
1954 * @result 0 on success, -1 otherwise
1955 */
1956int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep,
1957 uint32_t chunk_size)
1958{
1959 struct stream *s;
1960
1961 if (zclient_debug)
1962 zlog_debug("Getting Label Chunk");
1963
1964 if (zclient->sock < 0)
1965 return -1;
1966
1967 s = zclient->obuf;
1968 stream_reset(s);
1969
1970 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1971 /* proto */
1972 stream_putc(s, zclient->redist_default);
1973 /* instance */
1974 stream_putw(s, zclient->instance);
1975 stream_putc(s, keep);
1976 stream_putl(s, chunk_size);
1977
1978 /* Put length at the first point of the stream. */
1979 stream_putw_at(s, 0, stream_get_endp(s));
1980
1981 return zclient_send_message(zclient);
1982}
1983
1984/**
1985 * Function to request a label chunk in a syncronous way
1986 *
1987 * It first writes the request to zlcient output buffer and then
1988 * immediately reads the answer from the input buffer.
1989 *
1990 * @param zclient Zclient used to connect to label manager (zebra)
1991 * @param keep Avoid garbage collection
1992 * @param chunk_size Amount of labels requested
1993 * @param start To write first assigned chunk label to
1994 * @param end To write last assigned chunk label to
1995 * @result 0 on success, -1 otherwise
1996 */
1997int lm_get_label_chunk(struct zclient *zclient, uint8_t keep,
1998 uint32_t chunk_size, uint32_t *start, uint32_t *end)
1999{
2000 int ret;
2001 struct stream *s;
2002 uint8_t response_keep;
2003
2004 if (zclient_debug)
2005 zlog_debug("Getting Label Chunk");
2006
2007 if (zclient->sock < 0)
2008 return -1;
2009
2010 /* send request */
2011 s = zclient->obuf;
2012 stream_reset(s);
2013 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2014 /* proto */
2015 stream_putc(s, zclient->redist_default);
2016 /* instance */
2017 stream_putw(s, zclient->instance);
2018 /* keep */
2019 stream_putc(s, keep);
2020 /* chunk size */
2021 stream_putl(s, chunk_size);
2022 /* Put length at the first point of the stream. */
2023 stream_putw_at(s, 0, stream_get_endp(s));
2024
2025 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2026 if (ret < 0) {
2027 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2028 close(zclient->sock);
2029 zclient->sock = -1;
2030 return -1;
2031 }
2032 if (ret == 0) {
2033 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2034 close(zclient->sock);
2035 zclient->sock = -1;
2036 return -1;
2037 }
2038 if (zclient_debug)
2039 zlog_debug("Label chunk request (%d bytes) sent", ret);
2040
2041 /* read response */
2042 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
2043 return -1;
2044
2045 /* parse response */
2046 s = zclient->ibuf;
2047
2048 /* read proto and instance */
2049 uint8_t proto = stream_getc(s);
2050 uint16_t instance = stream_getw(s);
2051
2052 /* sanities */
2053 if (proto != zclient->redist_default)
2054 flog_err(EC_LIB_ZAPI_ENCODE,
2055 "Wrong proto (%u) in get chunk response. Should be %u",
2056 proto, zclient->redist_default);
2057 if (instance != zclient->instance)
2058 flog_err(EC_LIB_ZAPI_ENCODE,
2059 "Wrong instId (%u) in get chunk response Should be %u",
2060 instance, zclient->instance);
2061
2062 /* keep */
2063 response_keep = stream_getc(s);
2064 /* start and end labels */
2065 *start = stream_getl(s);
2066 *end = stream_getl(s);
2067
2068 /* not owning this response */
2069 if (keep != response_keep) {
2070 flog_err(
2071 EC_LIB_ZAPI_ENCODE,
2072 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2073 *start, *end, keep, response_keep);
2074 }
2075 /* sanity */
2076 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
2077 || *end > MPLS_LABEL_UNRESERVED_MAX) {
2078 flog_err(EC_LIB_ZAPI_ENCODE, "Invalid Label chunk: %u - %u",
2079 *start, *end);
2080 return -1;
2081 }
2082
2083 if (zclient_debug)
2084 zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end,
2085 response_keep);
2086
2087 return 0;
2088}
2089
2090/**
2091 * Function to release a label chunk
2092 *
2093 * @param zclient Zclient used to connect to label manager (zebra)
2094 * @param start First label of chunk
2095 * @param end Last label of chunk
2096 * @result 0 on success, -1 otherwise
2097 */
2098int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
2099 uint32_t end)
2100{
2101 int ret;
2102 struct stream *s;
2103
2104 if (zclient_debug)
2105 zlog_debug("Releasing Label Chunk %u - %u", start, end);
2106
2107 if (zclient->sock < 0)
2108 return -1;
2109
2110 /* send request */
2111 s = zclient->obuf;
2112 stream_reset(s);
2113 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
2114
2115 /* proto */
2116 stream_putc(s, zclient->redist_default);
2117 /* instance */
2118 stream_putw(s, zclient->instance);
2119 /* start */
2120 stream_putl(s, start);
2121 /* end */
2122 stream_putl(s, end);
2123
2124 /* Put length at the first point of the stream. */
2125 stream_putw_at(s, 0, stream_get_endp(s));
2126
2127 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2128 if (ret < 0) {
2129 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2130 close(zclient->sock);
2131 zclient->sock = -1;
2132 return -1;
2133 }
2134 if (ret == 0) {
2135 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock connection closed");
2136 close(zclient->sock);
2137 zclient->sock = -1;
2138 return -1;
2139 }
2140
2141 return 0;
2142}
2143
2144/**
2145 * Connect to table manager in a syncronous way
2146 *
2147 * It first writes the request to zcient output buffer and then
2148 * immediately reads the answer from the input buffer.
2149 *
2150 * @param zclient Zclient used to connect to table manager (zebra)
2151 * @result Result of response
2152 */
2153int tm_table_manager_connect(struct zclient *zclient)
2154{
2155 int ret;
2156 struct stream *s;
2157 uint8_t result;
2158
2159 if (zclient_debug)
2160 zlog_debug("Connecting to Table Manager");
2161
2162 if (zclient->sock < 0)
2163 return -1;
2164
2165 /* send request */
2166 s = zclient->obuf;
2167 stream_reset(s);
2168 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT);
2169
2170 /* proto */
2171 stream_putc(s, zclient->redist_default);
2172 /* instance */
2173 stream_putw(s, zclient->instance);
2174
2175 /* Put length at the first point of the stream. */
2176 stream_putw_at(s, 0, stream_get_endp(s));
2177
2178 ret = zclient_send_message(zclient);
2179 if (ret < 0)
2180 return -1;
2181
2182 if (zclient_debug)
2183 zlog_debug("%s: Table manager connect request sent", __func__);
2184
2185 /* read response */
2186 if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
2187 != 0)
2188 return -1;
2189
2190 /* result */
2191 s = zclient->ibuf;
2192 STREAM_GETC(s, result);
2193 if (zclient_debug)
2194 zlog_debug(
2195 "%s: Table Manager connect response received, result %u",
2196 __func__, result);
2197
2198 return (int)result;
2199stream_failure:
2200 return 0;
2201}
2202
2203/**
2204 * Function to request a table chunk in a syncronous way
2205 *
2206 * It first writes the request to zclient output buffer and then
2207 * immediately reads the answer from the input buffer.
2208 *
2209 * @param zclient Zclient used to connect to table manager (zebra)
2210 * @param chunk_size Amount of table requested
2211 * @param start to write first assigned chunk table RT ID to
2212 * @param end To write last assigned chunk table RT ID to
2213 * @result 0 on success, -1 otherwise
2214 */
2215int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
2216 uint32_t *start, uint32_t *end)
2217{
2218 int ret;
2219 struct stream *s;
2220
2221 if (zclient_debug)
2222 zlog_debug("Getting Table Chunk");
2223
2224 if (zclient->sock < 0)
2225 return -1;
2226
2227 /* send request */
2228 s = zclient->obuf;
2229 stream_reset(s);
2230 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT);
2231 /* chunk size */
2232 stream_putl(s, chunk_size);
2233 /* Put length at the first point of the stream. */
2234 stream_putw_at(s, 0, stream_get_endp(s));
2235
2236 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2237 if (ret < 0) {
2238 flog_err(EC_LIB_ZAPI_SOCKET, "%s: can't write to zclient->sock",
2239 __func__);
2240 close(zclient->sock);
2241 zclient->sock = -1;
2242 return -1;
2243 }
2244 if (ret == 0) {
2245 flog_err(EC_LIB_ZAPI_SOCKET,
2246 "%s: zclient->sock connection closed", __func__);
2247 close(zclient->sock);
2248 zclient->sock = -1;
2249 return -1;
2250 }
2251 if (zclient_debug)
2252 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__,
2253 ret);
2254
2255 /* read response */
2256 if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0)
2257 return -1;
2258
2259 s = zclient->ibuf;
2260 /* start and end table IDs */
2261 STREAM_GETL(s, *start);
2262 STREAM_GETL(s, *end);
2263
2264 if (zclient_debug)
2265 zlog_debug("Table Chunk assign: %u - %u ", *start, *end);
2266
2267stream_failure:
2268 return 0;
2269}
2270
2271/**
2272 * Function to release a table chunk
2273 *
2274 * @param zclient Zclient used to connect to table manager (zebra)
2275 * @param start First label of table
2276 * @param end Last label of chunk
2277 * @result 0 on success, -1 otherwise
2278 */
2279int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
2280 uint32_t end)
2281{
2282 struct stream *s;
2283
2284 if (zclient_debug)
2285 zlog_debug("Releasing Table Chunk");
2286
2287 if (zclient->sock < 0)
2288 return -1;
2289
2290 /* send request */
2291 s = zclient->obuf;
2292 stream_reset(s);
2293 zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT);
2294
2295 /* start */
2296 stream_putl(s, start);
2297 /* end */
2298 stream_putl(s, end);
2299
2300 /* Put length at the first point of the stream. */
2301 stream_putw_at(s, 0, stream_get_endp(s));
2302
2303 return zclient_send_message(zclient);
2304}
2305
2306
2307int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
2308{
2309 struct stream *s;
2310
2311 /* Reset stream. */
2312 s = zclient->obuf;
2313 stream_reset(s);
2314
2315 zclient_create_header(s, command, VRF_DEFAULT);
2316 stream_write(s, pw->ifname, IF_NAMESIZE);
2317 stream_putl(s, pw->ifindex);
2318
2319 /* Put type */
2320 stream_putl(s, pw->type);
2321
2322 /* Put nexthop */
2323 stream_putl(s, pw->af);
2324 switch (pw->af) {
2325 case AF_INET:
2326 stream_put_in_addr(s, &pw->nexthop.ipv4);
2327 break;
2328 case AF_INET6:
2329 stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16);
2330 break;
2331 default:
2332 flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__);
2333 return -1;
2334 }
2335
2336 /* Put labels */
2337 stream_putl(s, pw->local_label);
2338 stream_putl(s, pw->remote_label);
2339
2340 /* Put flags */
2341 stream_putc(s, pw->flags);
2342
2343 /* Protocol specific fields */
2344 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2345
2346 /* Put length at the first point of the stream. */
2347 stream_putw_at(s, 0, stream_get_endp(s));
2348
2349 return zclient_send_message(zclient);
2350}
2351
2352/*
2353 * Receive PW status update from Zebra and send it to LDE process.
2354 */
2355void zebra_read_pw_status_update(int command, struct zclient *zclient,
2356 zebra_size_t length, vrf_id_t vrf_id,
2357 struct zapi_pw_status *pw)
2358{
2359 struct stream *s;
2360
2361 memset(pw, 0, sizeof(struct zapi_pw_status));
2362 s = zclient->ibuf;
2363
2364 /* Get data. */
2365 stream_get(pw->ifname, s, IF_NAMESIZE);
2366 pw->ifindex = stream_getl(s);
2367 pw->status = stream_getl(s);
2368}
2369
2370static void zclient_capability_decode(int command, struct zclient *zclient,
2371 zebra_size_t length, vrf_id_t vrf_id)
2372{
2373 struct zclient_capabilities cap;
2374 struct stream *s = zclient->ibuf;
2375 uint8_t mpls_enabled;
2376
2377 memset(&cap, 0, sizeof(cap));
2378 STREAM_GETC(s, mpls_enabled);
2379 cap.mpls_enabled = !!mpls_enabled;
2380 STREAM_GETL(s, cap.ecmp);
2381 STREAM_GETC(s, cap.role);
2382
2383 if (zclient->zebra_capabilities)
2384 (*zclient->zebra_capabilities)(&cap);
2385
2386stream_failure:
2387 return;
2388}
2389
2390/* Zebra client message read function. */
2391static int zclient_read(struct thread *thread)
2392{
2393 size_t already;
2394 uint16_t length, command;
2395 uint8_t marker, version;
2396 vrf_id_t vrf_id;
2397 struct zclient *zclient;
2398
2399 /* Get socket to zebra. */
2400 zclient = THREAD_ARG(thread);
2401 zclient->t_read = NULL;
2402
2403 /* Read zebra header (if we don't have it already). */
2404 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2405 ssize_t nbyte;
2406 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2407 ZEBRA_HEADER_SIZE - already))
2408 == 0)
2409 || (nbyte == -1)) {
2410 if (zclient_debug)
2411 zlog_debug(
2412 "zclient connection closed socket [%d].",
2413 zclient->sock);
2414 return zclient_failed(zclient);
2415 }
2416 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2417 /* Try again later. */
2418 zclient_event(ZCLIENT_READ, zclient);
2419 return 0;
2420 }
2421 already = ZEBRA_HEADER_SIZE;
2422 }
2423
2424 /* Reset to read from the beginning of the incoming packet. */
2425 stream_set_getp(zclient->ibuf, 0);
2426
2427 /* Fetch header values. */
2428 length = stream_getw(zclient->ibuf);
2429 marker = stream_getc(zclient->ibuf);
2430 version = stream_getc(zclient->ibuf);
2431 vrf_id = stream_getl(zclient->ibuf);
2432 command = stream_getw(zclient->ibuf);
2433
2434 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2435 flog_err(
2436 EC_LIB_ZAPI_MISSMATCH,
2437 "%s: socket %d version mismatch, marker %d, version %d",
2438 __func__, zclient->sock, marker, version);
2439 return zclient_failed(zclient);
2440 }
2441
2442 if (length < ZEBRA_HEADER_SIZE) {
2443 flog_err(EC_LIB_ZAPI_MISSMATCH,
2444 "%s: socket %d message length %u is less than %d ",
2445 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2446 return zclient_failed(zclient);
2447 }
2448
2449 /* Length check. */
2450 if (length > STREAM_SIZE(zclient->ibuf)) {
2451 struct stream *ns;
2452 flog_err(
2453 EC_LIB_ZAPI_ENCODE,
2454 "%s: message size %u exceeds buffer size %lu, expanding...",
2455 __func__, length,
2456 (unsigned long)STREAM_SIZE(zclient->ibuf));
2457 ns = stream_new(length);
2458 stream_copy(ns, zclient->ibuf);
2459 stream_free(zclient->ibuf);
2460 zclient->ibuf = ns;
2461 }
2462
2463 /* Read rest of zebra packet. */
2464 if (already < length) {
2465 ssize_t nbyte;
2466 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2467 length - already))
2468 == 0)
2469 || (nbyte == -1)) {
2470 if (zclient_debug)
2471 zlog_debug(
2472 "zclient connection closed socket [%d].",
2473 zclient->sock);
2474 return zclient_failed(zclient);
2475 }
2476 if (nbyte != (ssize_t)(length - already)) {
2477 /* Try again later. */
2478 zclient_event(ZCLIENT_READ, zclient);
2479 return 0;
2480 }
2481 }
2482
2483 length -= ZEBRA_HEADER_SIZE;
2484
2485 if (zclient_debug)
2486 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2487 (void *)zclient, command, vrf_id);
2488
2489 switch (command) {
2490 case ZEBRA_CAPABILITIES:
2491 zclient_capability_decode(command, zclient, length, vrf_id);
2492 break;
2493 case ZEBRA_ROUTER_ID_UPDATE:
2494 if (zclient->router_id_update)
2495 (*zclient->router_id_update)(command, zclient, length,
2496 vrf_id);
2497 break;
2498 case ZEBRA_VRF_ADD:
2499 zclient_vrf_add(zclient, vrf_id);
2500 break;
2501 case ZEBRA_VRF_DELETE:
2502 zclient_vrf_delete(zclient, vrf_id);
2503 break;
2504 case ZEBRA_INTERFACE_ADD:
2505 if (zclient->interface_add)
2506 (*zclient->interface_add)(command, zclient, length,
2507 vrf_id);
2508 break;
2509 case ZEBRA_INTERFACE_DELETE:
2510 if (zclient->interface_delete)
2511 (*zclient->interface_delete)(command, zclient, length,
2512 vrf_id);
2513 break;
2514 case ZEBRA_INTERFACE_ADDRESS_ADD:
2515 if (zclient->interface_address_add)
2516 (*zclient->interface_address_add)(command, zclient,
2517 length, vrf_id);
2518 break;
2519 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2520 if (zclient->interface_address_delete)
2521 (*zclient->interface_address_delete)(command, zclient,
2522 length, vrf_id);
2523 break;
2524 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2525 if (zclient->interface_bfd_dest_update)
2526 (*zclient->interface_bfd_dest_update)(command, zclient,
2527 length, vrf_id);
2528 break;
2529 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2530 if (zclient->interface_nbr_address_add)
2531 (*zclient->interface_nbr_address_add)(command, zclient,
2532 length, vrf_id);
2533 break;
2534 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2535 if (zclient->interface_nbr_address_delete)
2536 (*zclient->interface_nbr_address_delete)(
2537 command, zclient, length, vrf_id);
2538 break;
2539 case ZEBRA_INTERFACE_UP:
2540 if (zclient->interface_up)
2541 (*zclient->interface_up)(command, zclient, length,
2542 vrf_id);
2543 break;
2544 case ZEBRA_INTERFACE_DOWN:
2545 if (zclient->interface_down)
2546 (*zclient->interface_down)(command, zclient, length,
2547 vrf_id);
2548 break;
2549 case ZEBRA_INTERFACE_VRF_UPDATE:
2550 if (zclient->interface_vrf_update)
2551 (*zclient->interface_vrf_update)(command, zclient,
2552 length, vrf_id);
2553 break;
2554 case ZEBRA_NEXTHOP_UPDATE:
2555 if (zclient_debug)
2556 zlog_debug("zclient rcvd nexthop update\n");
2557 if (zclient->nexthop_update)
2558 (*zclient->nexthop_update)(command, zclient, length,
2559 vrf_id);
2560 break;
2561 case ZEBRA_IMPORT_CHECK_UPDATE:
2562 if (zclient_debug)
2563 zlog_debug("zclient rcvd import check update\n");
2564 if (zclient->import_check_update)
2565 (*zclient->import_check_update)(command, zclient,
2566 length, vrf_id);
2567 break;
2568 case ZEBRA_BFD_DEST_REPLAY:
2569 if (zclient->bfd_dest_replay)
2570 (*zclient->bfd_dest_replay)(command, zclient, length,
2571 vrf_id);
2572 break;
2573 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2574 if (zclient->redistribute_route_add)
2575 (*zclient->redistribute_route_add)(command, zclient,
2576 length, vrf_id);
2577 break;
2578 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2579 if (zclient->redistribute_route_del)
2580 (*zclient->redistribute_route_del)(command, zclient,
2581 length, vrf_id);
2582 break;
2583 case ZEBRA_INTERFACE_LINK_PARAMS:
2584 if (zclient->interface_link_params)
2585 (*zclient->interface_link_params)(command, zclient,
2586 length);
2587 break;
2588 case ZEBRA_FEC_UPDATE:
2589 if (zclient_debug)
2590 zlog_debug("zclient rcvd fec update\n");
2591 if (zclient->fec_update)
2592 (*zclient->fec_update)(command, zclient, length);
2593 break;
2594 case ZEBRA_LOCAL_ES_ADD:
2595 if (zclient->local_es_add)
2596 (*zclient->local_es_add)(command, zclient, length,
2597 vrf_id);
2598 break;
2599 case ZEBRA_LOCAL_ES_DEL:
2600 if (zclient->local_es_del)
2601 (*zclient->local_es_del)(command, zclient, length,
2602 vrf_id);
2603 break;
2604 case ZEBRA_VNI_ADD:
2605 if (zclient->local_vni_add)
2606 (*zclient->local_vni_add)(command, zclient, length,
2607 vrf_id);
2608 break;
2609 case ZEBRA_VNI_DEL:
2610 if (zclient->local_vni_del)
2611 (*zclient->local_vni_del)(command, zclient, length,
2612 vrf_id);
2613 break;
2614 case ZEBRA_L3VNI_ADD:
2615 if (zclient->local_l3vni_add)
2616 (*zclient->local_l3vni_add)(command, zclient, length,
2617 vrf_id);
2618 break;
2619 case ZEBRA_L3VNI_DEL:
2620 if (zclient->local_l3vni_del)
2621 (*zclient->local_l3vni_del)(command, zclient, length,
2622 vrf_id);
2623 break;
2624 case ZEBRA_MACIP_ADD:
2625 if (zclient->local_macip_add)
2626 (*zclient->local_macip_add)(command, zclient, length,
2627 vrf_id);
2628 break;
2629 case ZEBRA_MACIP_DEL:
2630 if (zclient->local_macip_del)
2631 (*zclient->local_macip_del)(command, zclient, length,
2632 vrf_id);
2633 break;
2634 case ZEBRA_IP_PREFIX_ROUTE_ADD:
2635 if (zclient->local_ip_prefix_add)
2636 (*zclient->local_ip_prefix_add)(command, zclient,
2637 length, vrf_id);
2638 break;
2639 case ZEBRA_IP_PREFIX_ROUTE_DEL:
2640 if (zclient->local_ip_prefix_del)
2641 (*zclient->local_ip_prefix_del)(command, zclient,
2642 length, vrf_id);
2643 break;
2644 case ZEBRA_PW_STATUS_UPDATE:
2645 if (zclient->pw_status_update)
2646 (*zclient->pw_status_update)(command, zclient, length,
2647 vrf_id);
2648 break;
2649 case ZEBRA_ROUTE_NOTIFY_OWNER:
2650 if (zclient->route_notify_owner)
2651 (*zclient->route_notify_owner)(command, zclient, length,
2652 vrf_id);
2653 break;
2654 case ZEBRA_RULE_NOTIFY_OWNER:
2655 if (zclient->rule_notify_owner)
2656 (*zclient->rule_notify_owner)(command, zclient, length,
2657 vrf_id);
2658 break;
2659 case ZEBRA_GET_LABEL_CHUNK:
2660 if (zclient->label_chunk)
2661 (*zclient->label_chunk)(command, zclient, length,
2662 vrf_id);
2663 break;
2664 case ZEBRA_IPSET_NOTIFY_OWNER:
2665 if (zclient->ipset_notify_owner)
2666 (*zclient->ipset_notify_owner)(command, zclient, length,
2667 vrf_id);
2668 break;
2669 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER:
2670 if (zclient->ipset_entry_notify_owner)
2671 (*zclient->ipset_entry_notify_owner)(command,
2672 zclient, length,
2673 vrf_id);
2674 break;
2675 case ZEBRA_IPTABLE_NOTIFY_OWNER:
2676 if (zclient->iptable_notify_owner)
2677 (*zclient->iptable_notify_owner)(command,
2678 zclient, length,
2679 vrf_id);
2680 default:
2681 break;
2682 }
2683
2684 if (zclient->sock < 0)
2685 /* Connection was closed during packet processing. */
2686 return -1;
2687
2688 /* Register read thread. */
2689 stream_reset(zclient->ibuf);
2690 zclient_event(ZCLIENT_READ, zclient);
2691
2692 return 0;
2693}
2694
2695void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2696 int type, unsigned short instance, vrf_id_t vrf_id)
2697{
2698
2699 if (instance) {
2700 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2701 if (redist_check_instance(
2702 &zclient->mi_redist[afi][type], instance))
2703 return;
2704 redist_add_instance(&zclient->mi_redist[afi][type],
2705 instance);
2706 } else {
2707 if (!redist_check_instance(
2708 &zclient->mi_redist[afi][type], instance))
2709 return;
2710 redist_del_instance(&zclient->mi_redist[afi][type],
2711 instance);
2712 }
2713
2714 } else {
2715 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2716 if (vrf_bitmap_check(zclient->redist[afi][type],
2717 vrf_id))
2718 return;
2719 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2720 } else {
2721 if (!vrf_bitmap_check(zclient->redist[afi][type],
2722 vrf_id))
2723 return;
2724 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2725 }
2726 }
2727
2728 if (zclient->sock > 0)
2729 zebra_redistribute_send(command, zclient, afi, type, instance,
2730 vrf_id);
2731}
2732
2733
2734void zclient_redistribute_default(int command, struct zclient *zclient,
2735 afi_t afi, vrf_id_t vrf_id)
2736{
2737
2738 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2739 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
2740 return;
2741 vrf_bitmap_set(zclient->default_information[afi], vrf_id);
2742 } else {
2743 if (!vrf_bitmap_check(zclient->default_information[afi],
2744 vrf_id))
2745 return;
2746 vrf_bitmap_unset(zclient->default_information[afi], vrf_id);
2747 }
2748
2749 if (zclient->sock > 0)
2750 zebra_redistribute_default_send(command, zclient, afi, vrf_id);
2751}
2752
2753static void zclient_event(enum event event, struct zclient *zclient)
2754{
2755 switch (event) {
2756 case ZCLIENT_SCHEDULE:
2757 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2758 &zclient->t_connect);
2759 break;
2760 case ZCLIENT_CONNECT:
2761 if (zclient_debug)
2762 zlog_debug(
2763 "zclient connect failures: %d schedule interval is now %d",
2764 zclient->fail, zclient->fail < 3 ? 10 : 60);
2765 thread_add_timer(zclient->master, zclient_connect, zclient,
2766 zclient->fail < 3 ? 10 : 60,
2767 &zclient->t_connect);
2768 break;
2769 case ZCLIENT_READ:
2770 zclient->t_read = NULL;
2771 thread_add_read(zclient->master, zclient_read, zclient,
2772 zclient->sock, &zclient->t_read);
2773 break;
2774 }
2775}
2776
2777void zclient_interface_set_master(struct zclient *client,
2778 struct interface *master,
2779 struct interface *slave)
2780{
2781 struct stream *s;
2782
2783 s = client->obuf;
2784 stream_reset(s);
2785
2786 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2787
2788 stream_putl(s, master->vrf_id);
2789 stream_putl(s, master->ifindex);
2790 stream_putl(s, slave->vrf_id);
2791 stream_putl(s, slave->ifindex);
2792
2793 stream_putw_at(s, 0, stream_get_endp(s));
2794 zclient_send_message(client);
2795}