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