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