]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
lib: introduce encode/decode functions for the MPLS zapi messages
[mirror_frr.git] / lib / zclient.c
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
43 DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient")
44 DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs")
45
46 /* Zebra client events. */
47 enum event { ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT };
48
49 /* Prototype for event manager. */
50 static void zclient_event(enum event, struct zclient *);
51
52 struct zclient_options zclient_options_default = {.receive_notify = false};
53
54 struct sockaddr_storage zclient_addr;
55 socklen_t zclient_addr_len;
56
57 /* This file local debug flag. */
58 static int zclient_debug;
59
60 /* Allocate zclient structure. */
61 struct 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. */
85 void 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
97 unsigned 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
113 void 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
127 void 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. */
144 void 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
185 void 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 */
207 int 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
235 static 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
243 static 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
269 int 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
293 void 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
303 int 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;
329 stream_failure:
330 return -1;
331 }
332
333 bool 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;
341 stream_failure:
342 return false;
343 }
344
345 /* Send simple Zebra message. */
346 static 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
361 static 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
385 void 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. */
402 void 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. */
469 void 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. */
535 void 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
562 int 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. */
582 int 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. */
629 void 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. */
664 static 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
677 int 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 */
761 int 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
769 static 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
781 static 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
832 static 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
848 static 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
855 int 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
991 int 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;
1149 stream_failure:
1150 return -1;
1151 }
1152
1153 static 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
1169 int 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
1200 bool 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
1217 stream_failure:
1218 return false;
1219 }
1220
1221 bool 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
1246 stream_failure:
1247 return false;
1248 }
1249
1250 bool 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
1265 stream_failure:
1266 return false;
1267 }
1268
1269 bool 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
1287 stream_failure:
1288 return false;
1289 }
1290
1291 bool 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
1307 stream_failure:
1308 return false;
1309 }
1310
1311 struct 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
1331 bool 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;
1392 stream_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 */
1402 int 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
1420 int 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 */
1437 static 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
1450 stream_failure:
1451 return;
1452 }
1453
1454 /* Router-id update from zebra daemon. */
1455 void 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
1462 stream_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
1512 static 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
1532 static 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
1550 struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
1551 {
1552 struct interface *ifp;
1553 char ifname_tmp[INTERFACE_NAMSIZ];
1554
1555 /* Read interface name. */
1556 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1557
1558 /* Lookup/create interface by name. */
1559 ifp = if_get_by_name(ifname_tmp, vrf_id);
1560
1561 zebra_interface_if_set_value(s, ifp);
1562
1563 return ifp;
1564 }
1565
1566 /*
1567 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1568 * from zebra server. The format of this message is the same as
1569 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1570 * comments for zebra_interface_add_read), except that no sockaddr_dl
1571 * is sent at the tail of the message.
1572 */
1573 struct 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
1595 static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
1596 {
1597
1598 if (iflp == NULL)
1599 return;
1600
1601 iflp->lp_status = stream_getl(s);
1602 iflp->te_metric = stream_getl(s);
1603 iflp->max_bw = stream_getf(s);
1604 iflp->max_rsv_bw = stream_getf(s);
1605 uint32_t bwclassnum = stream_getl(s);
1606 {
1607 unsigned int i;
1608 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1609 iflp->unrsv_bw[i] = stream_getf(s);
1610 if (i < bwclassnum)
1611 flog_err(
1612 EC_LIB_ZAPI_MISSMATCH,
1613 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1614 " - outdated library?",
1615 __func__, bwclassnum, MAX_CLASS_TYPE);
1616 }
1617 iflp->admin_grp = stream_getl(s);
1618 iflp->rmt_as = stream_getl(s);
1619 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1620
1621 iflp->av_delay = stream_getl(s);
1622 iflp->min_delay = stream_getl(s);
1623 iflp->max_delay = stream_getl(s);
1624 iflp->delay_var = stream_getl(s);
1625
1626 iflp->pkt_loss = stream_getf(s);
1627 iflp->res_bw = stream_getf(s);
1628 iflp->ava_bw = stream_getf(s);
1629 iflp->use_bw = stream_getf(s);
1630 }
1631
1632 struct interface *zebra_interface_link_params_read(struct stream *s,
1633 vrf_id_t vrf_id)
1634 {
1635 struct if_link_params *iflp;
1636 ifindex_t ifindex;
1637
1638 assert(s);
1639
1640 ifindex = stream_getl(s);
1641
1642 struct interface *ifp = if_lookup_by_index(ifindex, vrf_id);
1643
1644 if (ifp == NULL) {
1645 flog_err(EC_LIB_ZAPI_ENCODE,
1646 "%s: unknown ifindex %u, shouldn't happen", __func__,
1647 ifindex);
1648 return NULL;
1649 }
1650
1651 if ((iflp = if_link_params_get(ifp)) == NULL)
1652 return NULL;
1653
1654 link_params_set_value(s, iflp);
1655
1656 return ifp;
1657 }
1658
1659 void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
1660 {
1661 uint8_t link_params_status = 0;
1662 ifindex_t old_ifindex;
1663
1664 old_ifindex = ifp->ifindex;
1665 /* Read interface's index. */
1666 if_set_index(ifp, stream_getl(s));
1667 ifp->status = stream_getc(s);
1668
1669 /* Read interface's value. */
1670 ifp->flags = stream_getq(s);
1671 ifp->ptm_enable = stream_getc(s);
1672 ifp->ptm_status = stream_getc(s);
1673 ifp->metric = stream_getl(s);
1674 ifp->speed = stream_getl(s);
1675 ifp->mtu = stream_getl(s);
1676 ifp->mtu6 = stream_getl(s);
1677 ifp->bandwidth = stream_getl(s);
1678 ifp->link_ifindex = stream_getl(s);
1679 ifp->ll_type = stream_getl(s);
1680 ifp->hw_addr_len = stream_getl(s);
1681 if (ifp->hw_addr_len)
1682 stream_get(ifp->hw_addr, s,
1683 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1684
1685 /* Read Traffic Engineering status */
1686 link_params_status = stream_getc(s);
1687 /* Then, Traffic Engineering parameters if any */
1688 if (link_params_status) {
1689 struct if_link_params *iflp = if_link_params_get(ifp);
1690 link_params_set_value(s, iflp);
1691 }
1692
1693 nexthop_group_interface_state_change(ifp, old_ifindex);
1694 }
1695
1696 size_t zebra_interface_link_params_write(struct stream *s,
1697 struct interface *ifp)
1698 {
1699 size_t w;
1700 struct if_link_params *iflp;
1701 int i;
1702
1703 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1704 return 0;
1705
1706 iflp = ifp->link_params;
1707 w = 0;
1708
1709 w += stream_putl(s, iflp->lp_status);
1710
1711 w += stream_putl(s, iflp->te_metric);
1712 w += stream_putf(s, iflp->max_bw);
1713 w += stream_putf(s, iflp->max_rsv_bw);
1714
1715 w += stream_putl(s, MAX_CLASS_TYPE);
1716 for (i = 0; i < MAX_CLASS_TYPE; i++)
1717 w += stream_putf(s, iflp->unrsv_bw[i]);
1718
1719 w += stream_putl(s, iflp->admin_grp);
1720 w += stream_putl(s, iflp->rmt_as);
1721 w += stream_put_in_addr(s, &iflp->rmt_ip);
1722
1723 w += stream_putl(s, iflp->av_delay);
1724 w += stream_putl(s, iflp->min_delay);
1725 w += stream_putl(s, iflp->max_delay);
1726 w += stream_putl(s, iflp->delay_var);
1727
1728 w += stream_putf(s, iflp->pkt_loss);
1729 w += stream_putf(s, iflp->res_bw);
1730 w += stream_putf(s, iflp->ava_bw);
1731 w += stream_putf(s, iflp->use_bw);
1732
1733 return w;
1734 }
1735
1736 /*
1737 * format of message for address additon is:
1738 * 0
1739 * 0 1 2 3 4 5 6 7
1740 * +-+-+-+-+-+-+-+-+
1741 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1742 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1743 * | |
1744 * + +
1745 * | ifindex |
1746 * + +
1747 * | |
1748 * + +
1749 * | |
1750 * +-+-+-+-+-+-+-+-+
1751 * | ifc_flags | flags for connected address
1752 * +-+-+-+-+-+-+-+-+
1753 * | addr_family |
1754 * +-+-+-+-+-+-+-+-+
1755 * | addr... |
1756 * : :
1757 * | |
1758 * +-+-+-+-+-+-+-+-+
1759 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1760 * +-+-+-+-+-+-+-+-+
1761 * | daddr.. |
1762 * : :
1763 * | |
1764 * +-+-+-+-+-+-+-+-+
1765 */
1766
1767 static int memconstant(const void *s, int c, size_t n)
1768 {
1769 const uint8_t *p = s;
1770
1771 while (n-- > 0)
1772 if (*p++ != c)
1773 return 0;
1774 return 1;
1775 }
1776
1777
1778 struct connected *zebra_interface_address_read(int type, struct stream *s,
1779 vrf_id_t vrf_id)
1780 {
1781 ifindex_t ifindex;
1782 struct interface *ifp;
1783 struct connected *ifc;
1784 struct prefix p, d, *dp;
1785 int plen;
1786 uint8_t ifc_flags;
1787
1788 memset(&p, 0, sizeof(p));
1789 memset(&d, 0, sizeof(d));
1790
1791 /* Get interface index. */
1792 ifindex = stream_getl(s);
1793
1794 /* Lookup index. */
1795 ifp = if_lookup_by_index(ifindex, vrf_id);
1796 if (ifp == NULL) {
1797 flog_err(EC_LIB_ZAPI_ENCODE,
1798 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1799 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1800 ifindex, vrf_id);
1801 return NULL;
1802 }
1803
1804 /* Fetch flag. */
1805 ifc_flags = stream_getc(s);
1806
1807 /* Fetch interface address. */
1808 d.family = p.family = stream_getc(s);
1809 plen = prefix_blen(&d);
1810
1811 zclient_stream_get_prefix(s, &p);
1812
1813 /* Fetch destination address. */
1814 stream_get(&d.u.prefix, s, plen);
1815
1816 /* N.B. NULL destination pointers are encoded as all zeroes */
1817 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1818
1819 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1820 ifc = connected_lookup_prefix_exact(ifp, &p);
1821 if (!ifc) {
1822 /* N.B. NULL destination pointers are encoded as all
1823 * zeroes */
1824 ifc = connected_add_by_prefix(ifp, &p, dp);
1825 }
1826 if (ifc) {
1827 ifc->flags = ifc_flags;
1828 if (ifc->destination)
1829 ifc->destination->prefixlen =
1830 ifc->address->prefixlen;
1831 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1832 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1833 * "peer" */
1834 char buf[PREFIX_STRLEN];
1835 flog_err(
1836 EC_LIB_ZAPI_ENCODE,
1837 "warning: interface %s address %s with peer flag set, but no peer address!",
1838 ifp->name,
1839 prefix2str(ifc->address, buf,
1840 sizeof buf));
1841 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1842 }
1843 }
1844 } else {
1845 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1846 ifc = connected_delete_by_prefix(ifp, &p);
1847 }
1848
1849 return ifc;
1850 }
1851
1852 /*
1853 * format of message for neighbor connected address is:
1854 * 0
1855 * 0 1 2 3 4 5 6 7
1856 * +-+-+-+-+-+-+-+-+
1857 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1858 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1859 * | |
1860 * + +
1861 * | ifindex |
1862 * + +
1863 * | |
1864 * + +
1865 * | |
1866 * +-+-+-+-+-+-+-+-+
1867 * | addr_family |
1868 * +-+-+-+-+-+-+-+-+
1869 * | addr... |
1870 * : :
1871 * | |
1872 * +-+-+-+-+-+-+-+-+
1873 * | addr_len | len of addr.
1874 * +-+-+-+-+-+-+-+-+
1875 */
1876 struct nbr_connected *
1877 zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
1878 {
1879 unsigned int ifindex;
1880 struct interface *ifp;
1881 struct prefix p;
1882 struct nbr_connected *ifc;
1883
1884 /* Get interface index. */
1885 ifindex = stream_getl(s);
1886
1887 /* Lookup index. */
1888 ifp = if_lookup_by_index(ifindex, vrf_id);
1889 if (ifp == NULL) {
1890 flog_err(EC_LIB_ZAPI_ENCODE,
1891 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1892 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1893 : "DELETE",
1894 ifindex, vrf_id);
1895 return NULL;
1896 }
1897
1898 p.family = stream_getc(s);
1899 stream_get(&p.u.prefix, s, prefix_blen(&p));
1900 p.prefixlen = stream_getc(s);
1901
1902 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1903 /* Currently only supporting P2P links, so any new RA source
1904 address is
1905 considered as the replacement of the previously learnt
1906 Link-Local address. */
1907 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1908 ifc = nbr_connected_new();
1909 ifc->address = prefix_new();
1910 ifc->ifp = ifp;
1911 listnode_add(ifp->nbr_connected, ifc);
1912 }
1913
1914 prefix_copy(ifc->address, &p);
1915 } else {
1916 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1917
1918 ifc = nbr_connected_check(ifp, &p);
1919 if (ifc)
1920 listnode_delete(ifp->nbr_connected, ifc);
1921 }
1922
1923 return ifc;
1924 }
1925
1926 struct interface *zebra_interface_vrf_update_read(struct stream *s,
1927 vrf_id_t vrf_id,
1928 vrf_id_t *new_vrf_id)
1929 {
1930 char ifname[INTERFACE_NAMSIZ];
1931 struct interface *ifp;
1932 vrf_id_t new_id;
1933
1934 /* Read interface name. */
1935 stream_get(ifname, s, INTERFACE_NAMSIZ);
1936
1937 /* Lookup interface. */
1938 ifp = if_lookup_by_name(ifname, vrf_id);
1939 if (ifp == NULL) {
1940 flog_err(EC_LIB_ZAPI_ENCODE,
1941 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1942 ifname, vrf_id);
1943 return NULL;
1944 }
1945
1946 /* Fetch new VRF Id. */
1947 new_id = stream_getw(s);
1948
1949 *new_vrf_id = new_id;
1950 return ifp;
1951 }
1952
1953 /* filter unwanted messages until the expected one arrives */
1954 static int zclient_read_sync_response(struct zclient *zclient,
1955 uint16_t expected_cmd)
1956 {
1957 struct stream *s;
1958 uint16_t size = -1;
1959 uint8_t marker;
1960 uint8_t version;
1961 vrf_id_t vrf_id;
1962 uint16_t cmd;
1963 fd_set readfds;
1964 int ret;
1965
1966 ret = 0;
1967 cmd = expected_cmd + 1;
1968 while (ret == 0 && cmd != expected_cmd) {
1969 s = zclient->ibuf;
1970 stream_reset(s);
1971
1972 /* wait until response arrives */
1973 FD_ZERO(&readfds);
1974 FD_SET(zclient->sock, &readfds);
1975 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1976 if (!FD_ISSET(zclient->sock, &readfds))
1977 continue;
1978 /* read response */
1979 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1980 &version, &vrf_id, &cmd);
1981 if (zclient_debug)
1982 zlog_debug("%s: Response (%d bytes) received", __func__,
1983 size);
1984 }
1985 if (ret != 0) {
1986 flog_err(EC_LIB_ZAPI_ENCODE, "%s: Invalid Sync Message Reply",
1987 __func__);
1988 return -1;
1989 }
1990
1991 return 0;
1992 }
1993 /**
1994 * Connect to label manager in a syncronous way
1995 *
1996 * It first writes the request to zcient output buffer and then
1997 * immediately reads the answer from the input buffer.
1998 *
1999 * @param zclient Zclient used to connect to label manager (zebra)
2000 * @param async Synchronous (0) or asynchronous (1) operation
2001 * @result Result of response
2002 */
2003 int lm_label_manager_connect(struct zclient *zclient, int async)
2004 {
2005 int ret;
2006 struct stream *s;
2007 uint8_t result;
2008 uint16_t cmd = async ? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC :
2009 ZEBRA_LABEL_MANAGER_CONNECT;
2010
2011 if (zclient_debug)
2012 zlog_debug("Connecting to Label Manager (LM)");
2013
2014 if (zclient->sock < 0) {
2015 zlog_debug("%s: invalid zclient socket", __func__);
2016 return -1;
2017 }
2018
2019 /* send request */
2020 s = zclient->obuf;
2021 stream_reset(s);
2022 zclient_create_header(s, cmd, VRF_DEFAULT);
2023
2024 /* proto */
2025 stream_putc(s, zclient->redist_default);
2026 /* instance */
2027 stream_putw(s, zclient->instance);
2028
2029 /* Put length at the first point of the stream. */
2030 stream_putw_at(s, 0, stream_get_endp(s));
2031
2032 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2033 if (ret < 0) {
2034 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2035 close(zclient->sock);
2036 zclient->sock = -1;
2037 return -1;
2038 }
2039 if (ret == 0) {
2040 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2041 close(zclient->sock);
2042 zclient->sock = -1;
2043 return -1;
2044 }
2045 if (zclient_debug)
2046 zlog_debug("LM connect request sent (%d bytes)", ret);
2047
2048 if (async)
2049 return 0;
2050
2051 /* read response */
2052 if (zclient_read_sync_response(zclient, cmd)
2053 != 0)
2054 return -1;
2055
2056 s = zclient->ibuf;
2057
2058 /* read instance and proto */
2059 uint8_t proto = stream_getc(s);
2060 uint16_t instance = stream_getw(s);
2061
2062 /* sanity */
2063 if (proto != zclient->redist_default)
2064 flog_err(
2065 EC_LIB_ZAPI_ENCODE,
2066 "Wrong proto (%u) in LM connect response. Should be %u",
2067 proto, zclient->redist_default);
2068 if (instance != zclient->instance)
2069 flog_err(
2070 EC_LIB_ZAPI_ENCODE,
2071 "Wrong instId (%u) in LM connect response. Should be %u",
2072 instance, zclient->instance);
2073
2074 /* result code */
2075 result = stream_getc(s);
2076 if (zclient_debug)
2077 zlog_debug("LM connect-response received, result %u", result);
2078
2079 return (int)result;
2080 }
2081
2082 /*
2083 * Asynchronous label chunk request
2084 *
2085 * @param zclient Zclient used to connect to label manager (zebra)
2086 * @param keep Avoid garbage collection
2087 * @param chunk_size Amount of labels requested
2088 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2089 * @result 0 on success, -1 otherwise
2090 */
2091 int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep,
2092 uint32_t chunk_size, uint32_t base)
2093 {
2094 struct stream *s;
2095
2096 if (zclient_debug)
2097 zlog_debug("Getting Label Chunk");
2098
2099 if (zclient->sock < 0)
2100 return -1;
2101
2102 s = zclient->obuf;
2103 stream_reset(s);
2104
2105 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2106 /* proto */
2107 stream_putc(s, zclient->redist_default);
2108 /* instance */
2109 stream_putw(s, zclient->instance);
2110 stream_putc(s, keep);
2111 stream_putl(s, chunk_size);
2112 stream_putl(s, base);
2113
2114 /* Put length at the first point of the stream. */
2115 stream_putw_at(s, 0, stream_get_endp(s));
2116
2117 return zclient_send_message(zclient);
2118 }
2119
2120 /**
2121 * Function to request a label chunk in a syncronous way
2122 *
2123 * It first writes the request to zlcient output buffer and then
2124 * immediately reads the answer from the input buffer.
2125 *
2126 * @param zclient Zclient used to connect to label manager (zebra)
2127 * @param keep Avoid garbage collection
2128 * @param chunk_size Amount of labels requested
2129 * @param start To write first assigned chunk label to
2130 * @param end To write last assigned chunk label to
2131 * @result 0 on success, -1 otherwise
2132 */
2133 int lm_get_label_chunk(struct zclient *zclient, uint8_t keep, uint32_t base,
2134 uint32_t chunk_size, uint32_t *start, uint32_t *end)
2135 {
2136 int ret;
2137 struct stream *s;
2138 uint8_t response_keep;
2139
2140 if (zclient_debug)
2141 zlog_debug("Getting Label Chunk");
2142
2143 if (zclient->sock < 0)
2144 return -1;
2145
2146 /* send request */
2147 s = zclient->obuf;
2148 stream_reset(s);
2149 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2150 /* proto */
2151 stream_putc(s, zclient->redist_default);
2152 /* instance */
2153 stream_putw(s, zclient->instance);
2154 /* keep */
2155 stream_putc(s, keep);
2156 /* chunk size */
2157 stream_putl(s, chunk_size);
2158 /* requested chunk base */
2159 stream_putl(s, base);
2160 /* Put length at the first point of the stream. */
2161 stream_putw_at(s, 0, stream_get_endp(s));
2162
2163 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2164 if (ret < 0) {
2165 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2166 close(zclient->sock);
2167 zclient->sock = -1;
2168 return -1;
2169 }
2170 if (ret == 0) {
2171 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2172 close(zclient->sock);
2173 zclient->sock = -1;
2174 return -1;
2175 }
2176 if (zclient_debug)
2177 zlog_debug("Label chunk request (%d bytes) sent", ret);
2178
2179 /* read response */
2180 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
2181 return -1;
2182
2183 /* parse response */
2184 s = zclient->ibuf;
2185
2186 /* read proto and instance */
2187 uint8_t proto = stream_getc(s);
2188 uint16_t instance = stream_getw(s);
2189
2190 /* sanities */
2191 if (proto != zclient->redist_default)
2192 flog_err(EC_LIB_ZAPI_ENCODE,
2193 "Wrong proto (%u) in get chunk response. Should be %u",
2194 proto, zclient->redist_default);
2195 if (instance != zclient->instance)
2196 flog_err(EC_LIB_ZAPI_ENCODE,
2197 "Wrong instId (%u) in get chunk response Should be %u",
2198 instance, zclient->instance);
2199
2200 /* if we requested a specific chunk and it could not be allocated, the
2201 * response message will end here
2202 */
2203 if (!STREAM_READABLE(s)) {
2204 zlog_info("Unable to assign Label Chunk to %s instance %u",
2205 zebra_route_string(proto), instance);
2206 return -1;
2207 }
2208
2209 /* keep */
2210 response_keep = stream_getc(s);
2211 /* start and end labels */
2212 *start = stream_getl(s);
2213 *end = stream_getl(s);
2214
2215 /* not owning this response */
2216 if (keep != response_keep) {
2217 flog_err(
2218 EC_LIB_ZAPI_ENCODE,
2219 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2220 *start, *end, keep, response_keep);
2221 }
2222 /* sanity */
2223 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
2224 || *end > MPLS_LABEL_UNRESERVED_MAX) {
2225 flog_err(EC_LIB_ZAPI_ENCODE, "Invalid Label chunk: %u - %u",
2226 *start, *end);
2227 return -1;
2228 }
2229
2230 if (zclient_debug)
2231 zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end,
2232 response_keep);
2233
2234 return 0;
2235 }
2236
2237 /**
2238 * Function to release a label chunk
2239 *
2240 * @param zclient Zclient used to connect to label manager (zebra)
2241 * @param start First label of chunk
2242 * @param end Last label of chunk
2243 * @result 0 on success, -1 otherwise
2244 */
2245 int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
2246 uint32_t end)
2247 {
2248 int ret;
2249 struct stream *s;
2250
2251 if (zclient_debug)
2252 zlog_debug("Releasing Label Chunk %u - %u", start, end);
2253
2254 if (zclient->sock < 0)
2255 return -1;
2256
2257 /* send request */
2258 s = zclient->obuf;
2259 stream_reset(s);
2260 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
2261
2262 /* proto */
2263 stream_putc(s, zclient->redist_default);
2264 /* instance */
2265 stream_putw(s, zclient->instance);
2266 /* start */
2267 stream_putl(s, start);
2268 /* end */
2269 stream_putl(s, end);
2270
2271 /* Put length at the first point of the stream. */
2272 stream_putw_at(s, 0, stream_get_endp(s));
2273
2274 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2275 if (ret < 0) {
2276 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2277 close(zclient->sock);
2278 zclient->sock = -1;
2279 return -1;
2280 }
2281 if (ret == 0) {
2282 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock connection closed");
2283 close(zclient->sock);
2284 zclient->sock = -1;
2285 return -1;
2286 }
2287
2288 return 0;
2289 }
2290
2291 /**
2292 * Connect to table manager in a syncronous way
2293 *
2294 * It first writes the request to zcient output buffer and then
2295 * immediately reads the answer from the input buffer.
2296 *
2297 * @param zclient Zclient used to connect to table manager (zebra)
2298 * @result Result of response
2299 */
2300 int tm_table_manager_connect(struct zclient *zclient)
2301 {
2302 int ret;
2303 struct stream *s;
2304 uint8_t result;
2305
2306 if (zclient_debug)
2307 zlog_debug("Connecting to Table Manager");
2308
2309 if (zclient->sock < 0)
2310 return -1;
2311
2312 /* send request */
2313 s = zclient->obuf;
2314 stream_reset(s);
2315 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT);
2316
2317 /* proto */
2318 stream_putc(s, zclient->redist_default);
2319 /* instance */
2320 stream_putw(s, zclient->instance);
2321
2322 /* Put length at the first point of the stream. */
2323 stream_putw_at(s, 0, stream_get_endp(s));
2324
2325 ret = zclient_send_message(zclient);
2326 if (ret < 0)
2327 return -1;
2328
2329 if (zclient_debug)
2330 zlog_debug("%s: Table manager connect request sent", __func__);
2331
2332 /* read response */
2333 if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
2334 != 0)
2335 return -1;
2336
2337 /* result */
2338 s = zclient->ibuf;
2339 STREAM_GETC(s, result);
2340 if (zclient_debug)
2341 zlog_debug(
2342 "%s: Table Manager connect response received, result %u",
2343 __func__, result);
2344
2345 return (int)result;
2346 stream_failure:
2347 return -1;
2348 }
2349
2350 /**
2351 * Function to request a table chunk in a syncronous way
2352 *
2353 * It first writes the request to zclient output buffer and then
2354 * immediately reads the answer from the input buffer.
2355 *
2356 * @param zclient Zclient used to connect to table manager (zebra)
2357 * @param chunk_size Amount of table requested
2358 * @param start to write first assigned chunk table RT ID to
2359 * @param end To write last assigned chunk table RT ID to
2360 * @result 0 on success, -1 otherwise
2361 */
2362 int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
2363 uint32_t *start, uint32_t *end)
2364 {
2365 int ret;
2366 struct stream *s;
2367
2368 if (zclient_debug)
2369 zlog_debug("Getting Table Chunk");
2370
2371 if (zclient->sock < 0)
2372 return -1;
2373
2374 /* send request */
2375 s = zclient->obuf;
2376 stream_reset(s);
2377 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT);
2378 /* chunk size */
2379 stream_putl(s, chunk_size);
2380 /* Put length at the first point of the stream. */
2381 stream_putw_at(s, 0, stream_get_endp(s));
2382
2383 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2384 if (ret < 0) {
2385 flog_err(EC_LIB_ZAPI_SOCKET, "%s: can't write to zclient->sock",
2386 __func__);
2387 close(zclient->sock);
2388 zclient->sock = -1;
2389 return -1;
2390 }
2391 if (ret == 0) {
2392 flog_err(EC_LIB_ZAPI_SOCKET,
2393 "%s: zclient->sock connection closed", __func__);
2394 close(zclient->sock);
2395 zclient->sock = -1;
2396 return -1;
2397 }
2398 if (zclient_debug)
2399 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__,
2400 ret);
2401
2402 /* read response */
2403 if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0)
2404 return -1;
2405
2406 s = zclient->ibuf;
2407 /* start and end table IDs */
2408 STREAM_GETL(s, *start);
2409 STREAM_GETL(s, *end);
2410
2411 if (zclient_debug)
2412 zlog_debug("Table Chunk assign: %u - %u ", *start, *end);
2413
2414 return 0;
2415 stream_failure:
2416 return -1;
2417 }
2418
2419 /**
2420 * Function to release a table chunk
2421 *
2422 * @param zclient Zclient used to connect to table manager (zebra)
2423 * @param start First label of table
2424 * @param end Last label of chunk
2425 * @result 0 on success, -1 otherwise
2426 */
2427 int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
2428 uint32_t end)
2429 {
2430 struct stream *s;
2431
2432 if (zclient_debug)
2433 zlog_debug("Releasing Table Chunk");
2434
2435 if (zclient->sock < 0)
2436 return -1;
2437
2438 /* send request */
2439 s = zclient->obuf;
2440 stream_reset(s);
2441 zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT);
2442
2443 /* start */
2444 stream_putl(s, start);
2445 /* end */
2446 stream_putl(s, end);
2447
2448 /* Put length at the first point of the stream. */
2449 stream_putw_at(s, 0, stream_get_endp(s));
2450
2451 return zclient_send_message(zclient);
2452 }
2453
2454 int zebra_send_mpls_labels(struct zclient *zclient, int cmd,
2455 struct zapi_labels *zl)
2456 {
2457 if (zapi_labels_encode(zclient->obuf, cmd, zl) < 0)
2458 return -1;
2459 return zclient_send_message(zclient);
2460 }
2461
2462 int zapi_labels_encode(struct stream *s, int cmd, struct zapi_labels *zl)
2463 {
2464 stream_reset(s);
2465
2466 zclient_create_header(s, cmd, VRF_DEFAULT);
2467 stream_putc(s, zl->type);
2468 stream_putw(s, zl->prefix.family);
2469 stream_put_prefix(s, &zl->prefix);
2470 switch (zl->prefix.family) {
2471 case AF_INET:
2472 stream_put_in_addr(s, &zl->nexthop.ipv4);
2473 break;
2474 case AF_INET6:
2475 stream_write(s, (uint8_t *)&zl->nexthop.ipv6, 16);
2476 break;
2477 default:
2478 flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__);
2479 return -1;
2480 }
2481 stream_putl(s, zl->ifindex);
2482 stream_putc(s, zl->distance);
2483 stream_putl(s, zl->local_label);
2484 stream_putl(s, zl->remote_label);
2485
2486 /* Put length at the first point of the stream. */
2487 stream_putw_at(s, 0, stream_get_endp(s));
2488
2489 return 0;
2490 }
2491
2492 int zapi_labels_decode(struct stream *s, struct zapi_labels *zl)
2493 {
2494 size_t psize;
2495
2496 memset(zl, 0, sizeof(*zl));
2497
2498 /* Get data. */
2499 STREAM_GETC(s, zl->type);
2500 STREAM_GETW(s, zl->prefix.family);
2501 STREAM_GETC(s, zl->prefix.prefixlen);
2502 psize = PSIZE(zl->prefix.prefixlen);
2503
2504 switch (zl->prefix.family) {
2505 case AF_INET:
2506 if (zl->prefix.prefixlen > IPV4_MAX_BITLEN) {
2507 zlog_debug(
2508 "%s: Specified prefix length %d is greater than a v4 address can support",
2509 __PRETTY_FUNCTION__, zl->prefix.prefixlen);
2510 return -1;
2511 }
2512 STREAM_GET(&zl->prefix.u.prefix4.s_addr, s, psize);
2513 STREAM_GET(&zl->nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2514 break;
2515 case AF_INET6:
2516 if (zl->prefix.prefixlen > IPV6_MAX_BITLEN) {
2517 zlog_debug(
2518 "%s: Specified prefix length %d is greater than a v6 address can support",
2519 __PRETTY_FUNCTION__, zl->prefix.prefixlen);
2520 return -1;
2521 }
2522 STREAM_GET(&zl->prefix.u.prefix6, s, psize);
2523 STREAM_GET(&zl->nexthop.ipv6, s, 16);
2524 break;
2525 default:
2526 zlog_debug("%s: Specified AF %d is not supported for this call",
2527 __PRETTY_FUNCTION__, zl->prefix.family);
2528 return -1;
2529 }
2530 STREAM_GETL(s, zl->ifindex);
2531 STREAM_GETC(s, zl->distance);
2532 STREAM_GETL(s, zl->local_label);
2533 STREAM_GETL(s, zl->remote_label);
2534
2535 return 0;
2536 stream_failure:
2537 return -1;
2538 }
2539
2540 int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
2541 {
2542 struct stream *s;
2543
2544 /* Reset stream. */
2545 s = zclient->obuf;
2546 stream_reset(s);
2547
2548 zclient_create_header(s, command, VRF_DEFAULT);
2549 stream_write(s, pw->ifname, IF_NAMESIZE);
2550 stream_putl(s, pw->ifindex);
2551
2552 /* Put type */
2553 stream_putl(s, pw->type);
2554
2555 /* Put nexthop */
2556 stream_putl(s, pw->af);
2557 switch (pw->af) {
2558 case AF_INET:
2559 stream_put_in_addr(s, &pw->nexthop.ipv4);
2560 break;
2561 case AF_INET6:
2562 stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16);
2563 break;
2564 default:
2565 flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__);
2566 return -1;
2567 }
2568
2569 /* Put labels */
2570 stream_putl(s, pw->local_label);
2571 stream_putl(s, pw->remote_label);
2572
2573 /* Put flags */
2574 stream_putc(s, pw->flags);
2575
2576 /* Protocol specific fields */
2577 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2578
2579 /* Put length at the first point of the stream. */
2580 stream_putw_at(s, 0, stream_get_endp(s));
2581
2582 return zclient_send_message(zclient);
2583 }
2584
2585 /*
2586 * Receive PW status update from Zebra and send it to LDE process.
2587 */
2588 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS, struct zapi_pw_status *pw)
2589 {
2590 struct stream *s;
2591
2592 memset(pw, 0, sizeof(struct zapi_pw_status));
2593 s = zclient->ibuf;
2594
2595 /* Get data. */
2596 stream_get(pw->ifname, s, IF_NAMESIZE);
2597 pw->ifindex = stream_getl(s);
2598 pw->status = stream_getl(s);
2599 }
2600
2601 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS)
2602 {
2603 struct zclient_capabilities cap;
2604 struct stream *s = zclient->ibuf;
2605 int vrf_backend;
2606 uint8_t mpls_enabled;
2607
2608 STREAM_GETL(s, vrf_backend);
2609 vrf_configure_backend(vrf_backend);
2610
2611 memset(&cap, 0, sizeof(cap));
2612 STREAM_GETC(s, mpls_enabled);
2613 cap.mpls_enabled = !!mpls_enabled;
2614 STREAM_GETL(s, cap.ecmp);
2615 STREAM_GETC(s, cap.role);
2616
2617 if (zclient->zebra_capabilities)
2618 (*zclient->zebra_capabilities)(&cap);
2619
2620 stream_failure:
2621 return;
2622 }
2623
2624 /* Zebra client message read function. */
2625 static int zclient_read(struct thread *thread)
2626 {
2627 size_t already;
2628 uint16_t length, command;
2629 uint8_t marker, version;
2630 vrf_id_t vrf_id;
2631 struct zclient *zclient;
2632
2633 /* Get socket to zebra. */
2634 zclient = THREAD_ARG(thread);
2635 zclient->t_read = NULL;
2636
2637 /* Read zebra header (if we don't have it already). */
2638 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2639 ssize_t nbyte;
2640 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2641 ZEBRA_HEADER_SIZE - already))
2642 == 0)
2643 || (nbyte == -1)) {
2644 if (zclient_debug)
2645 zlog_debug(
2646 "zclient connection closed socket [%d].",
2647 zclient->sock);
2648 return zclient_failed(zclient);
2649 }
2650 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2651 /* Try again later. */
2652 zclient_event(ZCLIENT_READ, zclient);
2653 return 0;
2654 }
2655 already = ZEBRA_HEADER_SIZE;
2656 }
2657
2658 /* Reset to read from the beginning of the incoming packet. */
2659 stream_set_getp(zclient->ibuf, 0);
2660
2661 /* Fetch header values. */
2662 length = stream_getw(zclient->ibuf);
2663 marker = stream_getc(zclient->ibuf);
2664 version = stream_getc(zclient->ibuf);
2665 vrf_id = stream_getl(zclient->ibuf);
2666 command = stream_getw(zclient->ibuf);
2667
2668 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2669 flog_err(
2670 EC_LIB_ZAPI_MISSMATCH,
2671 "%s: socket %d version mismatch, marker %d, version %d",
2672 __func__, zclient->sock, marker, version);
2673 return zclient_failed(zclient);
2674 }
2675
2676 if (length < ZEBRA_HEADER_SIZE) {
2677 flog_err(EC_LIB_ZAPI_MISSMATCH,
2678 "%s: socket %d message length %u is less than %d ",
2679 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2680 return zclient_failed(zclient);
2681 }
2682
2683 /* Length check. */
2684 if (length > STREAM_SIZE(zclient->ibuf)) {
2685 struct stream *ns;
2686 flog_err(
2687 EC_LIB_ZAPI_ENCODE,
2688 "%s: message size %u exceeds buffer size %lu, expanding...",
2689 __func__, length,
2690 (unsigned long)STREAM_SIZE(zclient->ibuf));
2691 ns = stream_new(length);
2692 stream_copy(ns, zclient->ibuf);
2693 stream_free(zclient->ibuf);
2694 zclient->ibuf = ns;
2695 }
2696
2697 /* Read rest of zebra packet. */
2698 if (already < length) {
2699 ssize_t nbyte;
2700 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2701 length - already))
2702 == 0)
2703 || (nbyte == -1)) {
2704 if (zclient_debug)
2705 zlog_debug(
2706 "zclient connection closed socket [%d].",
2707 zclient->sock);
2708 return zclient_failed(zclient);
2709 }
2710 if (nbyte != (ssize_t)(length - already)) {
2711 /* Try again later. */
2712 zclient_event(ZCLIENT_READ, zclient);
2713 return 0;
2714 }
2715 }
2716
2717 length -= ZEBRA_HEADER_SIZE;
2718
2719 if (zclient_debug)
2720 zlog_debug("zclient 0x%p command %s VRF %u",
2721 (void *)zclient, zserv_command_string(command),
2722 vrf_id);
2723
2724 switch (command) {
2725 case ZEBRA_CAPABILITIES:
2726 zclient_capability_decode(command, zclient, length, vrf_id);
2727 break;
2728 case ZEBRA_ROUTER_ID_UPDATE:
2729 if (zclient->router_id_update)
2730 (*zclient->router_id_update)(command, zclient, length,
2731 vrf_id);
2732 break;
2733 case ZEBRA_VRF_ADD:
2734 zclient_vrf_add(zclient, vrf_id);
2735 break;
2736 case ZEBRA_VRF_DELETE:
2737 zclient_vrf_delete(zclient, vrf_id);
2738 break;
2739 case ZEBRA_INTERFACE_ADD:
2740 if (zclient->interface_add)
2741 (*zclient->interface_add)(command, zclient, length,
2742 vrf_id);
2743 break;
2744 case ZEBRA_INTERFACE_DELETE:
2745 if (zclient->interface_delete)
2746 (*zclient->interface_delete)(command, zclient, length,
2747 vrf_id);
2748 break;
2749 case ZEBRA_INTERFACE_ADDRESS_ADD:
2750 if (zclient->interface_address_add)
2751 (*zclient->interface_address_add)(command, zclient,
2752 length, vrf_id);
2753 break;
2754 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2755 if (zclient->interface_address_delete)
2756 (*zclient->interface_address_delete)(command, zclient,
2757 length, vrf_id);
2758 break;
2759 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2760 if (zclient->interface_bfd_dest_update)
2761 (*zclient->interface_bfd_dest_update)(command, zclient,
2762 length, vrf_id);
2763 break;
2764 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2765 if (zclient->interface_nbr_address_add)
2766 (*zclient->interface_nbr_address_add)(command, zclient,
2767 length, vrf_id);
2768 break;
2769 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2770 if (zclient->interface_nbr_address_delete)
2771 (*zclient->interface_nbr_address_delete)(
2772 command, zclient, length, vrf_id);
2773 break;
2774 case ZEBRA_INTERFACE_UP:
2775 if (zclient->interface_up)
2776 (*zclient->interface_up)(command, zclient, length,
2777 vrf_id);
2778 break;
2779 case ZEBRA_INTERFACE_DOWN:
2780 if (zclient->interface_down)
2781 (*zclient->interface_down)(command, zclient, length,
2782 vrf_id);
2783 break;
2784 case ZEBRA_INTERFACE_VRF_UPDATE:
2785 if (zclient->interface_vrf_update)
2786 (*zclient->interface_vrf_update)(command, zclient,
2787 length, vrf_id);
2788 break;
2789 case ZEBRA_NEXTHOP_UPDATE:
2790 if (zclient_debug)
2791 zlog_debug("zclient rcvd nexthop update");
2792 if (zclient->nexthop_update)
2793 (*zclient->nexthop_update)(command, zclient, length,
2794 vrf_id);
2795 break;
2796 case ZEBRA_IMPORT_CHECK_UPDATE:
2797 if (zclient_debug)
2798 zlog_debug("zclient rcvd import check update");
2799 if (zclient->import_check_update)
2800 (*zclient->import_check_update)(command, zclient,
2801 length, vrf_id);
2802 break;
2803 case ZEBRA_BFD_DEST_REPLAY:
2804 if (zclient->bfd_dest_replay)
2805 (*zclient->bfd_dest_replay)(command, zclient, length,
2806 vrf_id);
2807 break;
2808 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2809 if (zclient->redistribute_route_add)
2810 (*zclient->redistribute_route_add)(command, zclient,
2811 length, vrf_id);
2812 break;
2813 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2814 if (zclient->redistribute_route_del)
2815 (*zclient->redistribute_route_del)(command, zclient,
2816 length, vrf_id);
2817 break;
2818 case ZEBRA_INTERFACE_LINK_PARAMS:
2819 if (zclient->interface_link_params)
2820 (*zclient->interface_link_params)(command, zclient,
2821 length, vrf_id);
2822 break;
2823 case ZEBRA_FEC_UPDATE:
2824 if (zclient_debug)
2825 zlog_debug("zclient rcvd fec update");
2826 if (zclient->fec_update)
2827 (*zclient->fec_update)(command, zclient, length);
2828 break;
2829 case ZEBRA_LOCAL_ES_ADD:
2830 if (zclient->local_es_add)
2831 (*zclient->local_es_add)(command, zclient, length,
2832 vrf_id);
2833 break;
2834 case ZEBRA_LOCAL_ES_DEL:
2835 if (zclient->local_es_del)
2836 (*zclient->local_es_del)(command, zclient, length,
2837 vrf_id);
2838 break;
2839 case ZEBRA_VNI_ADD:
2840 if (zclient->local_vni_add)
2841 (*zclient->local_vni_add)(command, zclient, length,
2842 vrf_id);
2843 break;
2844 case ZEBRA_VNI_DEL:
2845 if (zclient->local_vni_del)
2846 (*zclient->local_vni_del)(command, zclient, length,
2847 vrf_id);
2848 break;
2849 case ZEBRA_L3VNI_ADD:
2850 if (zclient->local_l3vni_add)
2851 (*zclient->local_l3vni_add)(command, zclient, length,
2852 vrf_id);
2853 break;
2854 case ZEBRA_L3VNI_DEL:
2855 if (zclient->local_l3vni_del)
2856 (*zclient->local_l3vni_del)(command, zclient, length,
2857 vrf_id);
2858 break;
2859 case ZEBRA_MACIP_ADD:
2860 if (zclient->local_macip_add)
2861 (*zclient->local_macip_add)(command, zclient, length,
2862 vrf_id);
2863 break;
2864 case ZEBRA_MACIP_DEL:
2865 if (zclient->local_macip_del)
2866 (*zclient->local_macip_del)(command, zclient, length,
2867 vrf_id);
2868 break;
2869 case ZEBRA_IP_PREFIX_ROUTE_ADD:
2870 if (zclient->local_ip_prefix_add)
2871 (*zclient->local_ip_prefix_add)(command, zclient,
2872 length, vrf_id);
2873 break;
2874 case ZEBRA_IP_PREFIX_ROUTE_DEL:
2875 if (zclient->local_ip_prefix_del)
2876 (*zclient->local_ip_prefix_del)(command, zclient,
2877 length, vrf_id);
2878 break;
2879 case ZEBRA_PW_STATUS_UPDATE:
2880 if (zclient->pw_status_update)
2881 (*zclient->pw_status_update)(command, zclient, length,
2882 vrf_id);
2883 break;
2884 case ZEBRA_ROUTE_NOTIFY_OWNER:
2885 if (zclient->route_notify_owner)
2886 (*zclient->route_notify_owner)(command, zclient, length,
2887 vrf_id);
2888 break;
2889 case ZEBRA_RULE_NOTIFY_OWNER:
2890 if (zclient->rule_notify_owner)
2891 (*zclient->rule_notify_owner)(command, zclient, length,
2892 vrf_id);
2893 break;
2894 case ZEBRA_GET_LABEL_CHUNK:
2895 if (zclient->label_chunk)
2896 (*zclient->label_chunk)(command, zclient, length,
2897 vrf_id);
2898 break;
2899 case ZEBRA_IPSET_NOTIFY_OWNER:
2900 if (zclient->ipset_notify_owner)
2901 (*zclient->ipset_notify_owner)(command, zclient, length,
2902 vrf_id);
2903 break;
2904 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER:
2905 if (zclient->ipset_entry_notify_owner)
2906 (*zclient->ipset_entry_notify_owner)(command,
2907 zclient, length,
2908 vrf_id);
2909 break;
2910 case ZEBRA_IPTABLE_NOTIFY_OWNER:
2911 if (zclient->iptable_notify_owner)
2912 (*zclient->iptable_notify_owner)(command,
2913 zclient, length,
2914 vrf_id);
2915 break;
2916 case ZEBRA_VXLAN_SG_ADD:
2917 if (zclient->vxlan_sg_add)
2918 (*zclient->vxlan_sg_add)(command, zclient, length,
2919 vrf_id);
2920 break;
2921 case ZEBRA_VXLAN_SG_DEL:
2922 if (zclient->vxlan_sg_del)
2923 (*zclient->vxlan_sg_del)(command, zclient, length,
2924 vrf_id);
2925 break;
2926 default:
2927 break;
2928 }
2929
2930 if (zclient->sock < 0)
2931 /* Connection was closed during packet processing. */
2932 return -1;
2933
2934 /* Register read thread. */
2935 stream_reset(zclient->ibuf);
2936 zclient_event(ZCLIENT_READ, zclient);
2937
2938 return 0;
2939 }
2940
2941 void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2942 int type, unsigned short instance, vrf_id_t vrf_id)
2943 {
2944
2945 if (instance) {
2946 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2947 if (redist_check_instance(
2948 &zclient->mi_redist[afi][type], instance))
2949 return;
2950 redist_add_instance(&zclient->mi_redist[afi][type],
2951 instance);
2952 } else {
2953 if (!redist_check_instance(
2954 &zclient->mi_redist[afi][type], instance))
2955 return;
2956 redist_del_instance(&zclient->mi_redist[afi][type],
2957 instance);
2958 }
2959
2960 } else {
2961 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2962 if (vrf_bitmap_check(zclient->redist[afi][type],
2963 vrf_id))
2964 return;
2965 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2966 } else {
2967 if (!vrf_bitmap_check(zclient->redist[afi][type],
2968 vrf_id))
2969 return;
2970 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2971 }
2972 }
2973
2974 if (zclient->sock > 0)
2975 zebra_redistribute_send(command, zclient, afi, type, instance,
2976 vrf_id);
2977 }
2978
2979
2980 void zclient_redistribute_default(int command, struct zclient *zclient,
2981 afi_t afi, vrf_id_t vrf_id)
2982 {
2983
2984 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2985 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
2986 return;
2987 vrf_bitmap_set(zclient->default_information[afi], vrf_id);
2988 } else {
2989 if (!vrf_bitmap_check(zclient->default_information[afi],
2990 vrf_id))
2991 return;
2992 vrf_bitmap_unset(zclient->default_information[afi], vrf_id);
2993 }
2994
2995 if (zclient->sock > 0)
2996 zebra_redistribute_default_send(command, zclient, afi, vrf_id);
2997 }
2998
2999 static void zclient_event(enum event event, struct zclient *zclient)
3000 {
3001 switch (event) {
3002 case ZCLIENT_SCHEDULE:
3003 thread_add_event(zclient->master, zclient_connect, zclient, 0,
3004 &zclient->t_connect);
3005 break;
3006 case ZCLIENT_CONNECT:
3007 if (zclient_debug)
3008 zlog_debug(
3009 "zclient connect failures: %d schedule interval is now %d",
3010 zclient->fail, zclient->fail < 3 ? 10 : 60);
3011 thread_add_timer(zclient->master, zclient_connect, zclient,
3012 zclient->fail < 3 ? 10 : 60,
3013 &zclient->t_connect);
3014 break;
3015 case ZCLIENT_READ:
3016 zclient->t_read = NULL;
3017 thread_add_read(zclient->master, zclient_read, zclient,
3018 zclient->sock, &zclient->t_read);
3019 break;
3020 }
3021 }
3022
3023 void zclient_interface_set_master(struct zclient *client,
3024 struct interface *master,
3025 struct interface *slave)
3026 {
3027 struct stream *s;
3028
3029 s = client->obuf;
3030 stream_reset(s);
3031
3032 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
3033
3034 stream_putl(s, master->vrf_id);
3035 stream_putl(s, master->ifindex);
3036 stream_putl(s, slave->vrf_id);
3037 stream_putl(s, slave->ifindex);
3038
3039 stream_putw_at(s, 0, stream_get_endp(s));
3040 zclient_send_message(client);
3041 }