]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
Merge pull request #4849 from sworleys/Label-Append-Resolve_2
[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 int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api)
770 {
771 struct zapi_nexthop *api_nh;
772 int i;
773 int psize;
774
775 stream_reset(s);
776 zclient_create_header(s, cmd, api->vrf_id);
777
778 if (api->type >= ZEBRA_ROUTE_MAX) {
779 flog_err(EC_LIB_ZAPI_ENCODE,
780 "%s: Specified route type (%u) is not a legal value\n",
781 __PRETTY_FUNCTION__, api->type);
782 return -1;
783 }
784 stream_putc(s, api->type);
785
786 stream_putw(s, api->instance);
787 stream_putl(s, api->flags);
788 stream_putc(s, api->message);
789
790 if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) {
791 flog_err(EC_LIB_ZAPI_ENCODE,
792 "%s: Specified route SAFI (%u) is not a legal value\n",
793 __PRETTY_FUNCTION__, api->safi);
794 return -1;
795 }
796 stream_putc(s, api->safi);
797
798 /* Put prefix information. */
799 stream_putc(s, api->prefix.family);
800 psize = PSIZE(api->prefix.prefixlen);
801 stream_putc(s, api->prefix.prefixlen);
802 stream_write(s, (uint8_t *)&api->prefix.u.prefix, psize);
803
804 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
805 psize = PSIZE(api->src_prefix.prefixlen);
806 stream_putc(s, api->src_prefix.prefixlen);
807 stream_write(s, (uint8_t *)&api->src_prefix.prefix, psize);
808 }
809
810 /* Nexthops. */
811 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
812 /* limit the number of nexthops if necessary */
813 if (api->nexthop_num > MULTIPATH_NUM) {
814 char buf[PREFIX2STR_BUFFER];
815
816 prefix2str(&api->prefix, buf, sizeof(buf));
817 flog_err(
818 EC_LIB_ZAPI_ENCODE,
819 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
820 __func__, buf, api->nexthop_num, MULTIPATH_NUM);
821 return -1;
822 }
823
824 stream_putw(s, api->nexthop_num);
825
826 for (i = 0; i < api->nexthop_num; i++) {
827 api_nh = &api->nexthops[i];
828
829 stream_putl(s, api_nh->vrf_id);
830 stream_putc(s, api_nh->type);
831 stream_putc(s, api_nh->onlink);
832 switch (api_nh->type) {
833 case NEXTHOP_TYPE_BLACKHOLE:
834 stream_putc(s, api_nh->bh_type);
835 break;
836 case NEXTHOP_TYPE_IPV4:
837 stream_put_in_addr(s, &api_nh->gate.ipv4);
838 break;
839 case NEXTHOP_TYPE_IPV4_IFINDEX:
840 stream_put_in_addr(s, &api_nh->gate.ipv4);
841 stream_putl(s, api_nh->ifindex);
842 break;
843 case NEXTHOP_TYPE_IFINDEX:
844 stream_putl(s, api_nh->ifindex);
845 break;
846 case NEXTHOP_TYPE_IPV6:
847 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
848 16);
849 break;
850 case NEXTHOP_TYPE_IPV6_IFINDEX:
851 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
852 16);
853 stream_putl(s, api_nh->ifindex);
854 break;
855 }
856
857 /* MPLS labels for BGP-LU or Segment Routing */
858 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
859 if (api_nh->label_num > MPLS_MAX_LABELS) {
860 char buf[PREFIX2STR_BUFFER];
861 prefix2str(&api->prefix, buf,
862 sizeof(buf));
863 flog_err(EC_LIB_ZAPI_ENCODE,
864 "%s: prefix %s: can't encode "
865 "%u labels (maximum is %u)",
866 __func__, buf,
867 api_nh->label_num,
868 MPLS_MAX_LABELS);
869 return -1;
870 }
871
872 stream_putc(s, api_nh->label_num);
873 stream_put(s, &api_nh->labels[0],
874 api_nh->label_num
875 * sizeof(mpls_label_t));
876 }
877
878 /* Router MAC for EVPN routes. */
879 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
880 stream_put(s, &(api_nh->rmac),
881 sizeof(struct ethaddr));
882 }
883 }
884
885 /* Attributes. */
886 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
887 stream_putc(s, api->distance);
888 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
889 stream_putl(s, api->metric);
890 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
891 stream_putl(s, api->tag);
892 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
893 stream_putl(s, api->mtu);
894 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
895 stream_putl(s, api->tableid);
896
897 /* Put length at the first point of the stream. */
898 stream_putw_at(s, 0, stream_get_endp(s));
899
900 return 0;
901 }
902
903 int zapi_route_decode(struct stream *s, struct zapi_route *api)
904 {
905 struct zapi_nexthop *api_nh;
906 int i;
907
908 memset(api, 0, sizeof(*api));
909
910 /* Type, flags, message. */
911 STREAM_GETC(s, api->type);
912 if (api->type >= ZEBRA_ROUTE_MAX) {
913 flog_err(EC_LIB_ZAPI_ENCODE,
914 "%s: Specified route type: %d is not a legal value\n",
915 __PRETTY_FUNCTION__, api->type);
916 return -1;
917 }
918
919 STREAM_GETW(s, api->instance);
920 STREAM_GETL(s, api->flags);
921 STREAM_GETC(s, api->message);
922 STREAM_GETC(s, api->safi);
923 if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) {
924 flog_err(EC_LIB_ZAPI_ENCODE,
925 "%s: Specified route SAFI (%u) is not a legal value\n",
926 __PRETTY_FUNCTION__, api->safi);
927 return -1;
928 }
929
930 /* Prefix. */
931 STREAM_GETC(s, api->prefix.family);
932 STREAM_GETC(s, api->prefix.prefixlen);
933 switch (api->prefix.family) {
934 case AF_INET:
935 if (api->prefix.prefixlen > IPV4_MAX_PREFIXLEN) {
936 flog_err(
937 EC_LIB_ZAPI_ENCODE,
938 "%s: V4 prefixlen is %d which should not be more than 32",
939 __PRETTY_FUNCTION__, api->prefix.prefixlen);
940 return -1;
941 }
942 break;
943 case AF_INET6:
944 if (api->prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
945 flog_err(
946 EC_LIB_ZAPI_ENCODE,
947 "%s: v6 prefixlen is %d which should not be more than 128",
948 __PRETTY_FUNCTION__, api->prefix.prefixlen);
949 return -1;
950 }
951 break;
952 default:
953 flog_err(EC_LIB_ZAPI_ENCODE,
954 "%s: Specified family %d is not v4 or v6",
955 __PRETTY_FUNCTION__, api->prefix.family);
956 return -1;
957 }
958 STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen));
959
960 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
961 api->src_prefix.family = AF_INET6;
962 STREAM_GETC(s, api->src_prefix.prefixlen);
963 if (api->src_prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
964 flog_err(
965 EC_LIB_ZAPI_ENCODE,
966 "%s: SRC Prefix prefixlen received: %d is too large",
967 __PRETTY_FUNCTION__, api->src_prefix.prefixlen);
968 return -1;
969 }
970 STREAM_GET(&api->src_prefix.prefix, s,
971 PSIZE(api->src_prefix.prefixlen));
972
973 if (api->prefix.family != AF_INET6
974 || api->src_prefix.prefixlen == 0) {
975 flog_err(
976 EC_LIB_ZAPI_ENCODE,
977 "%s: SRC prefix specified in some manner that makes no sense",
978 __PRETTY_FUNCTION__);
979 return -1;
980 }
981 }
982
983 /* Nexthops. */
984 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
985 STREAM_GETW(s, api->nexthop_num);
986 if (api->nexthop_num > MULTIPATH_NUM) {
987 flog_err(EC_LIB_ZAPI_ENCODE,
988 "%s: invalid number of nexthops (%u)",
989 __func__, api->nexthop_num);
990 return -1;
991 }
992
993 for (i = 0; i < api->nexthop_num; i++) {
994 api_nh = &api->nexthops[i];
995
996 STREAM_GETL(s, api_nh->vrf_id);
997 STREAM_GETC(s, api_nh->type);
998 STREAM_GETC(s, api_nh->onlink);
999 switch (api_nh->type) {
1000 case NEXTHOP_TYPE_BLACKHOLE:
1001 STREAM_GETC(s, api_nh->bh_type);
1002 break;
1003 case NEXTHOP_TYPE_IPV4:
1004 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
1005 IPV4_MAX_BYTELEN);
1006 break;
1007 case NEXTHOP_TYPE_IPV4_IFINDEX:
1008 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
1009 IPV4_MAX_BYTELEN);
1010 STREAM_GETL(s, api_nh->ifindex);
1011 break;
1012 case NEXTHOP_TYPE_IFINDEX:
1013 STREAM_GETL(s, api_nh->ifindex);
1014 break;
1015 case NEXTHOP_TYPE_IPV6:
1016 STREAM_GET(&api_nh->gate.ipv6, s, 16);
1017 break;
1018 case NEXTHOP_TYPE_IPV6_IFINDEX:
1019 STREAM_GET(&api_nh->gate.ipv6, s, 16);
1020 STREAM_GETL(s, api_nh->ifindex);
1021 break;
1022 }
1023
1024 /* MPLS labels for BGP-LU or Segment Routing */
1025 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
1026 STREAM_GETC(s, api_nh->label_num);
1027
1028 if (api_nh->label_num > MPLS_MAX_LABELS) {
1029 flog_err(
1030 EC_LIB_ZAPI_ENCODE,
1031 "%s: invalid number of MPLS labels (%u)",
1032 __func__, api_nh->label_num);
1033 return -1;
1034 }
1035
1036 STREAM_GET(&api_nh->labels[0], s,
1037 api_nh->label_num
1038 * sizeof(mpls_label_t));
1039 }
1040
1041 /* Router MAC for EVPN routes. */
1042 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
1043 stream_get(&(api_nh->rmac), s,
1044 sizeof(struct ethaddr));
1045 }
1046 }
1047
1048 /* Attributes. */
1049 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1050 STREAM_GETC(s, api->distance);
1051 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1052 STREAM_GETL(s, api->metric);
1053 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1054 STREAM_GETL(s, api->tag);
1055 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1056 STREAM_GETL(s, api->mtu);
1057 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1058 STREAM_GETL(s, api->tableid);
1059
1060 return 0;
1061 stream_failure:
1062 return -1;
1063 }
1064
1065 static void zapi_encode_prefix(struct stream *s, struct prefix *p,
1066 uint8_t family)
1067 {
1068 struct prefix any;
1069
1070 if (!p) {
1071 memset(&any, 0, sizeof(any));
1072 any.family = family;
1073 p = &any;
1074 }
1075
1076 stream_putc(s, p->family);
1077 stream_putc(s, p->prefixlen);
1078 stream_put(s, &p->u.prefix, prefix_blen(p));
1079 }
1080
1081 int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s, struct pbr_rule *zrule)
1082 {
1083 stream_reset(s);
1084 zclient_create_header(s, cmd, zrule->vrf_id);
1085
1086 /*
1087 * We are sending one item at a time at the moment
1088 */
1089 stream_putl(s, 1);
1090
1091 stream_putl(s, zrule->seq);
1092 stream_putl(s, zrule->priority);
1093 stream_putl(s, zrule->unique);
1094
1095 zapi_encode_prefix(s, &(zrule->filter.src_ip),
1096 zrule->filter.src_ip.family);
1097 stream_putw(s, zrule->filter.src_port); /* src port */
1098 zapi_encode_prefix(s, &(zrule->filter.dst_ip),
1099 zrule->filter.src_ip.family);
1100 stream_putw(s, zrule->filter.dst_port); /* dst port */
1101 stream_putw(s, zrule->filter.fwmark); /* fwmark */
1102
1103 stream_putl(s, zrule->action.table);
1104 stream_putl(s, zrule->ifindex);
1105
1106 /* Put length at the first point of the stream. */
1107 stream_putw_at(s, 0, stream_get_endp(s));
1108
1109 return 0;
1110 }
1111
1112 bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
1113 uint32_t *tableid,
1114 enum zapi_route_notify_owner *note)
1115 {
1116 uint32_t t;
1117
1118 STREAM_GET(note, s, sizeof(*note));
1119
1120 STREAM_GETC(s, p->family);
1121 STREAM_GETC(s, p->prefixlen);
1122 STREAM_GET(&p->u.prefix, s, prefix_blen(p));
1123 STREAM_GETL(s, t);
1124
1125 *tableid = t;
1126
1127 return true;
1128
1129 stream_failure:
1130 return false;
1131 }
1132
1133 bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
1134 uint32_t *priority, uint32_t *unique,
1135 ifindex_t *ifindex,
1136 enum zapi_rule_notify_owner *note)
1137 {
1138 uint32_t prio, seq, uni;
1139 ifindex_t ifi;
1140
1141 STREAM_GET(note, s, sizeof(*note));
1142
1143 STREAM_GETL(s, seq);
1144 STREAM_GETL(s, prio);
1145 STREAM_GETL(s, uni);
1146 STREAM_GETL(s, ifi);
1147
1148 if (zclient_debug)
1149 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__, seq, prio,
1150 uni, ifi);
1151 *seqno = seq;
1152 *priority = prio;
1153 *unique = uni;
1154 *ifindex = ifi;
1155
1156 return true;
1157
1158 stream_failure:
1159 return false;
1160 }
1161
1162 bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique,
1163 enum zapi_ipset_notify_owner *note)
1164 {
1165 uint32_t uni;
1166
1167 STREAM_GET(note, s, sizeof(*note));
1168
1169 STREAM_GETL(s, uni);
1170
1171 if (zclient_debug)
1172 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1173 *unique = uni;
1174
1175 return true;
1176
1177 stream_failure:
1178 return false;
1179 }
1180
1181 bool zapi_ipset_entry_notify_decode(struct stream *s, uint32_t *unique,
1182 char *ipset_name,
1183 enum zapi_ipset_entry_notify_owner *note)
1184 {
1185 uint32_t uni;
1186
1187 STREAM_GET(note, s, sizeof(*note));
1188
1189 STREAM_GETL(s, uni);
1190
1191 STREAM_GET(ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
1192
1193 if (zclient_debug)
1194 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1195 *unique = uni;
1196
1197 return true;
1198
1199 stream_failure:
1200 return false;
1201 }
1202
1203 bool zapi_iptable_notify_decode(struct stream *s,
1204 uint32_t *unique,
1205 enum zapi_iptable_notify_owner *note)
1206 {
1207 uint32_t uni;
1208
1209 STREAM_GET(note, s, sizeof(*note));
1210
1211 STREAM_GETL(s, uni);
1212
1213 if (zclient_debug)
1214 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1215 *unique = uni;
1216
1217 return true;
1218
1219 stream_failure:
1220 return false;
1221 }
1222
1223 struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh)
1224 {
1225 struct nexthop *n = nexthop_new();
1226
1227 n->type = znh->type;
1228 n->vrf_id = znh->vrf_id;
1229 n->ifindex = znh->ifindex;
1230 n->gate = znh->gate;
1231
1232 /*
1233 * This function currently handles labels
1234 */
1235 if (znh->label_num) {
1236 nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num,
1237 znh->labels);
1238 }
1239
1240 return n;
1241 }
1242
1243 bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr)
1244 {
1245 uint32_t i;
1246
1247 memset(nhr, 0, sizeof(*nhr));
1248
1249 STREAM_GETW(s, nhr->prefix.family);
1250 STREAM_GETC(s, nhr->prefix.prefixlen);
1251 switch (nhr->prefix.family) {
1252 case AF_INET:
1253 STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1254 break;
1255 case AF_INET6:
1256 STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN);
1257 break;
1258 default:
1259 break;
1260 }
1261
1262 STREAM_GETC(s, nhr->type);
1263 STREAM_GETW(s, nhr->instance);
1264 STREAM_GETC(s, nhr->distance);
1265 STREAM_GETL(s, nhr->metric);
1266 STREAM_GETC(s, nhr->nexthop_num);
1267
1268 for (i = 0; i < nhr->nexthop_num; i++) {
1269 STREAM_GETL(s, nhr->nexthops[i].vrf_id);
1270 STREAM_GETC(s, nhr->nexthops[i].type);
1271 switch (nhr->nexthops[i].type) {
1272 case NEXTHOP_TYPE_IPV4:
1273 case NEXTHOP_TYPE_IPV4_IFINDEX:
1274 STREAM_GET(&nhr->nexthops[i].gate.ipv4.s_addr, s,
1275 IPV4_MAX_BYTELEN);
1276 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1277 break;
1278 case NEXTHOP_TYPE_IFINDEX:
1279 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1280 break;
1281 case NEXTHOP_TYPE_IPV6:
1282 case NEXTHOP_TYPE_IPV6_IFINDEX:
1283 STREAM_GET(&nhr->nexthops[i].gate.ipv6, s,
1284 IPV6_MAX_BYTELEN);
1285 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1286 break;
1287 case NEXTHOP_TYPE_BLACKHOLE:
1288 break;
1289 }
1290 STREAM_GETC(s, nhr->nexthops[i].label_num);
1291 if (nhr->nexthops[i].label_num > MPLS_MAX_LABELS) {
1292 flog_err(EC_LIB_ZAPI_ENCODE,
1293 "%s: invalid number of MPLS labels (%u)",
1294 __func__, nhr->nexthops[i].label_num);
1295 return false;
1296 }
1297 if (nhr->nexthops[i].label_num)
1298 STREAM_GET(&nhr->nexthops[i].labels[0], s,
1299 nhr->nexthops[i].label_num
1300 * sizeof(mpls_label_t));
1301 }
1302
1303 return true;
1304 stream_failure:
1305 return false;
1306 }
1307
1308 /*
1309 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1310 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1311 * then set/unset redist[type] in the client handle (a struct zserv) for the
1312 * sending client
1313 */
1314 int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1315 int type, unsigned short instance, vrf_id_t vrf_id)
1316 {
1317 struct stream *s;
1318
1319 s = zclient->obuf;
1320 stream_reset(s);
1321
1322 zclient_create_header(s, command, vrf_id);
1323 stream_putc(s, afi);
1324 stream_putc(s, type);
1325 stream_putw(s, instance);
1326
1327 stream_putw_at(s, 0, stream_get_endp(s));
1328
1329 return zclient_send_message(zclient);
1330 }
1331
1332 int zebra_redistribute_default_send(int command, struct zclient *zclient,
1333 afi_t afi, vrf_id_t vrf_id)
1334 {
1335 struct stream *s;
1336
1337 s = zclient->obuf;
1338 stream_reset(s);
1339
1340 zclient_create_header(s, command, vrf_id);
1341 stream_putc(s, afi);
1342
1343 stream_putw_at(s, 0, stream_get_endp(s));
1344
1345 return zclient_send_message(zclient);
1346 }
1347
1348 /* Get prefix in ZServ format; family should be filled in on prefix */
1349 static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
1350 {
1351 size_t plen = prefix_blen(p);
1352 uint8_t c;
1353 p->prefixlen = 0;
1354
1355 if (plen == 0)
1356 return;
1357
1358 stream_get(&p->u.prefix, s, plen);
1359 STREAM_GETC(s, c);
1360 p->prefixlen = MIN(plen * 8, c);
1361
1362 stream_failure:
1363 return;
1364 }
1365
1366 /* Router-id update from zebra daemon. */
1367 void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
1368 {
1369 /* Fetch interface address. */
1370 STREAM_GETC(s, rid->family);
1371
1372 zclient_stream_get_prefix(s, rid);
1373
1374 stream_failure:
1375 return;
1376 }
1377
1378 /* Interface addition from zebra daemon. */
1379 /*
1380 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1381 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1382 * 0 1 2 3
1383 * 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
1384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1385 * | ifname |
1386 * | |
1387 * | |
1388 * | |
1389 * | |
1390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 * | ifindex |
1392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1393 * | status |
1394 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1395 * | if_flags |
1396 * | |
1397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1398 * | metric |
1399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1400 * | speed |
1401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1402 * | ifmtu |
1403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1404 * | ifmtu6 |
1405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1406 * | bandwidth |
1407 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1408 * | parent ifindex |
1409 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1410 * | Link Layer Type |
1411 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1412 * | Harware Address Length |
1413 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1414 * | Hardware Address if HW lenght different from 0 |
1415 * | ... max INTERFACE_HWADDR_MAX |
1416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1417 * | Link_params? | Whether a link-params follows: 1 or 0.
1418 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1419 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1420 * | .... (struct if_link_params). |
1421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1422 */
1423
1424 static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1425 {
1426 struct vrf *vrf;
1427 char vrfname_tmp[VRF_NAMSIZ];
1428 struct vrf_data data;
1429
1430 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1431 /* Read interface name. */
1432 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1433
1434 /* Lookup/create vrf by vrf_id. */
1435 vrf = vrf_get(vrf_id, vrfname_tmp);
1436 vrf->data.l.table_id = data.l.table_id;
1437 memcpy(vrf->data.l.netns_name, data.l.netns_name, NS_NAMSIZ);
1438 /* overwrite default vrf */
1439 if (vrf_id == VRF_DEFAULT)
1440 vrf_set_default_name(vrfname_tmp, false);
1441 vrf_enable(vrf);
1442 }
1443
1444 static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1445 {
1446 struct vrf *vrf;
1447
1448 /* Lookup vrf by vrf_id. */
1449 vrf = vrf_lookup_by_id(vrf_id);
1450
1451 /*
1452 * If a routing protocol doesn't know about a
1453 * vrf that is about to be deleted. There is
1454 * no point in attempting to delete it.
1455 */
1456 if (!vrf)
1457 return;
1458
1459 vrf_delete(vrf);
1460 }
1461
1462 struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
1463 {
1464 struct interface *ifp;
1465 char ifname_tmp[INTERFACE_NAMSIZ];
1466
1467 /* Read interface name. */
1468 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1469
1470 /* Lookup/create interface by name. */
1471 ifp = if_get_by_name(ifname_tmp, vrf_id);
1472
1473 zebra_interface_if_set_value(s, ifp);
1474
1475 return ifp;
1476 }
1477
1478 /*
1479 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1480 * from zebra server. The format of this message is the same as
1481 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1482 * comments for zebra_interface_add_read), except that no sockaddr_dl
1483 * is sent at the tail of the message.
1484 */
1485 struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
1486 {
1487 struct interface *ifp;
1488 char ifname_tmp[INTERFACE_NAMSIZ];
1489
1490 /* Read interface name. */
1491 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1492
1493 /* Lookup this by interface index. */
1494 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
1495 if (ifp == NULL) {
1496 flog_err(EC_LIB_ZAPI_ENCODE,
1497 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1498 ifname_tmp, vrf_id);
1499 return NULL;
1500 }
1501
1502 zebra_interface_if_set_value(s, ifp);
1503
1504 return ifp;
1505 }
1506
1507 static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
1508 {
1509
1510 if (iflp == NULL)
1511 return;
1512
1513 iflp->lp_status = stream_getl(s);
1514 iflp->te_metric = stream_getl(s);
1515 iflp->max_bw = stream_getf(s);
1516 iflp->max_rsv_bw = stream_getf(s);
1517 uint32_t bwclassnum = stream_getl(s);
1518 {
1519 unsigned int i;
1520 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1521 iflp->unrsv_bw[i] = stream_getf(s);
1522 if (i < bwclassnum)
1523 flog_err(
1524 EC_LIB_ZAPI_MISSMATCH,
1525 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1526 " - outdated library?",
1527 __func__, bwclassnum, MAX_CLASS_TYPE);
1528 }
1529 iflp->admin_grp = stream_getl(s);
1530 iflp->rmt_as = stream_getl(s);
1531 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1532
1533 iflp->av_delay = stream_getl(s);
1534 iflp->min_delay = stream_getl(s);
1535 iflp->max_delay = stream_getl(s);
1536 iflp->delay_var = stream_getl(s);
1537
1538 iflp->pkt_loss = stream_getf(s);
1539 iflp->res_bw = stream_getf(s);
1540 iflp->ava_bw = stream_getf(s);
1541 iflp->use_bw = stream_getf(s);
1542 }
1543
1544 struct interface *zebra_interface_link_params_read(struct stream *s,
1545 vrf_id_t vrf_id)
1546 {
1547 struct if_link_params *iflp;
1548 ifindex_t ifindex;
1549
1550 assert(s);
1551
1552 ifindex = stream_getl(s);
1553
1554 struct interface *ifp = if_lookup_by_index(ifindex, vrf_id);
1555
1556 if (ifp == NULL) {
1557 flog_err(EC_LIB_ZAPI_ENCODE,
1558 "%s: unknown ifindex %u, shouldn't happen", __func__,
1559 ifindex);
1560 return NULL;
1561 }
1562
1563 if ((iflp = if_link_params_get(ifp)) == NULL)
1564 return NULL;
1565
1566 link_params_set_value(s, iflp);
1567
1568 return ifp;
1569 }
1570
1571 void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
1572 {
1573 uint8_t link_params_status = 0;
1574 ifindex_t old_ifindex;
1575
1576 old_ifindex = ifp->ifindex;
1577 /* Read interface's index. */
1578 if_set_index(ifp, stream_getl(s));
1579 ifp->status = stream_getc(s);
1580
1581 /* Read interface's value. */
1582 ifp->flags = stream_getq(s);
1583 ifp->ptm_enable = stream_getc(s);
1584 ifp->ptm_status = stream_getc(s);
1585 ifp->metric = stream_getl(s);
1586 ifp->speed = stream_getl(s);
1587 ifp->mtu = stream_getl(s);
1588 ifp->mtu6 = stream_getl(s);
1589 ifp->bandwidth = stream_getl(s);
1590 ifp->link_ifindex = stream_getl(s);
1591 ifp->ll_type = stream_getl(s);
1592 ifp->hw_addr_len = stream_getl(s);
1593 if (ifp->hw_addr_len)
1594 stream_get(ifp->hw_addr, s,
1595 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1596
1597 /* Read Traffic Engineering status */
1598 link_params_status = stream_getc(s);
1599 /* Then, Traffic Engineering parameters if any */
1600 if (link_params_status) {
1601 struct if_link_params *iflp = if_link_params_get(ifp);
1602 link_params_set_value(s, iflp);
1603 }
1604
1605 nexthop_group_interface_state_change(ifp, old_ifindex);
1606 }
1607
1608 size_t zebra_interface_link_params_write(struct stream *s,
1609 struct interface *ifp)
1610 {
1611 size_t w;
1612 struct if_link_params *iflp;
1613 int i;
1614
1615 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1616 return 0;
1617
1618 iflp = ifp->link_params;
1619 w = 0;
1620
1621 w += stream_putl(s, iflp->lp_status);
1622
1623 w += stream_putl(s, iflp->te_metric);
1624 w += stream_putf(s, iflp->max_bw);
1625 w += stream_putf(s, iflp->max_rsv_bw);
1626
1627 w += stream_putl(s, MAX_CLASS_TYPE);
1628 for (i = 0; i < MAX_CLASS_TYPE; i++)
1629 w += stream_putf(s, iflp->unrsv_bw[i]);
1630
1631 w += stream_putl(s, iflp->admin_grp);
1632 w += stream_putl(s, iflp->rmt_as);
1633 w += stream_put_in_addr(s, &iflp->rmt_ip);
1634
1635 w += stream_putl(s, iflp->av_delay);
1636 w += stream_putl(s, iflp->min_delay);
1637 w += stream_putl(s, iflp->max_delay);
1638 w += stream_putl(s, iflp->delay_var);
1639
1640 w += stream_putf(s, iflp->pkt_loss);
1641 w += stream_putf(s, iflp->res_bw);
1642 w += stream_putf(s, iflp->ava_bw);
1643 w += stream_putf(s, iflp->use_bw);
1644
1645 return w;
1646 }
1647
1648 /*
1649 * format of message for address additon is:
1650 * 0
1651 * 0 1 2 3 4 5 6 7
1652 * +-+-+-+-+-+-+-+-+
1653 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1654 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1655 * | |
1656 * + +
1657 * | ifindex |
1658 * + +
1659 * | |
1660 * + +
1661 * | |
1662 * +-+-+-+-+-+-+-+-+
1663 * | ifc_flags | flags for connected address
1664 * +-+-+-+-+-+-+-+-+
1665 * | addr_family |
1666 * +-+-+-+-+-+-+-+-+
1667 * | addr... |
1668 * : :
1669 * | |
1670 * +-+-+-+-+-+-+-+-+
1671 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1672 * +-+-+-+-+-+-+-+-+
1673 * | daddr.. |
1674 * : :
1675 * | |
1676 * +-+-+-+-+-+-+-+-+
1677 */
1678
1679 static int memconstant(const void *s, int c, size_t n)
1680 {
1681 const uint8_t *p = s;
1682
1683 while (n-- > 0)
1684 if (*p++ != c)
1685 return 0;
1686 return 1;
1687 }
1688
1689
1690 struct connected *zebra_interface_address_read(int type, struct stream *s,
1691 vrf_id_t vrf_id)
1692 {
1693 ifindex_t ifindex;
1694 struct interface *ifp;
1695 struct connected *ifc;
1696 struct prefix p, d, *dp;
1697 int plen;
1698 uint8_t ifc_flags;
1699
1700 memset(&p, 0, sizeof(p));
1701 memset(&d, 0, sizeof(d));
1702
1703 /* Get interface index. */
1704 ifindex = stream_getl(s);
1705
1706 /* Lookup index. */
1707 ifp = if_lookup_by_index(ifindex, vrf_id);
1708 if (ifp == NULL) {
1709 flog_err(EC_LIB_ZAPI_ENCODE,
1710 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1711 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1712 ifindex, vrf_id);
1713 return NULL;
1714 }
1715
1716 /* Fetch flag. */
1717 ifc_flags = stream_getc(s);
1718
1719 /* Fetch interface address. */
1720 d.family = p.family = stream_getc(s);
1721 plen = prefix_blen(&d);
1722
1723 zclient_stream_get_prefix(s, &p);
1724
1725 /* Fetch destination address. */
1726 stream_get(&d.u.prefix, s, plen);
1727
1728 /* N.B. NULL destination pointers are encoded as all zeroes */
1729 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1730
1731 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1732 ifc = connected_lookup_prefix_exact(ifp, &p);
1733 if (!ifc) {
1734 /* N.B. NULL destination pointers are encoded as all
1735 * zeroes */
1736 ifc = connected_add_by_prefix(ifp, &p, dp);
1737 }
1738 if (ifc) {
1739 ifc->flags = ifc_flags;
1740 if (ifc->destination)
1741 ifc->destination->prefixlen =
1742 ifc->address->prefixlen;
1743 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1744 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1745 * "peer" */
1746 char buf[PREFIX_STRLEN];
1747 flog_err(
1748 EC_LIB_ZAPI_ENCODE,
1749 "warning: interface %s address %s with peer flag set, but no peer address!",
1750 ifp->name,
1751 prefix2str(ifc->address, buf,
1752 sizeof buf));
1753 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1754 }
1755 }
1756 } else {
1757 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1758 ifc = connected_delete_by_prefix(ifp, &p);
1759 }
1760
1761 return ifc;
1762 }
1763
1764 /*
1765 * format of message for neighbor connected address is:
1766 * 0
1767 * 0 1 2 3 4 5 6 7
1768 * +-+-+-+-+-+-+-+-+
1769 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1770 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1771 * | |
1772 * + +
1773 * | ifindex |
1774 * + +
1775 * | |
1776 * + +
1777 * | |
1778 * +-+-+-+-+-+-+-+-+
1779 * | addr_family |
1780 * +-+-+-+-+-+-+-+-+
1781 * | addr... |
1782 * : :
1783 * | |
1784 * +-+-+-+-+-+-+-+-+
1785 * | addr_len | len of addr.
1786 * +-+-+-+-+-+-+-+-+
1787 */
1788 struct nbr_connected *
1789 zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
1790 {
1791 unsigned int ifindex;
1792 struct interface *ifp;
1793 struct prefix p;
1794 struct nbr_connected *ifc;
1795
1796 /* Get interface index. */
1797 ifindex = stream_getl(s);
1798
1799 /* Lookup index. */
1800 ifp = if_lookup_by_index(ifindex, vrf_id);
1801 if (ifp == NULL) {
1802 flog_err(EC_LIB_ZAPI_ENCODE,
1803 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1804 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1805 : "DELETE",
1806 ifindex, vrf_id);
1807 return NULL;
1808 }
1809
1810 p.family = stream_getc(s);
1811 stream_get(&p.u.prefix, s, prefix_blen(&p));
1812 p.prefixlen = stream_getc(s);
1813
1814 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1815 /* Currently only supporting P2P links, so any new RA source
1816 address is
1817 considered as the replacement of the previously learnt
1818 Link-Local address. */
1819 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1820 ifc = nbr_connected_new();
1821 ifc->address = prefix_new();
1822 ifc->ifp = ifp;
1823 listnode_add(ifp->nbr_connected, ifc);
1824 }
1825
1826 prefix_copy(ifc->address, &p);
1827 } else {
1828 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1829
1830 ifc = nbr_connected_check(ifp, &p);
1831 if (ifc)
1832 listnode_delete(ifp->nbr_connected, ifc);
1833 }
1834
1835 return ifc;
1836 }
1837
1838 struct interface *zebra_interface_vrf_update_read(struct stream *s,
1839 vrf_id_t vrf_id,
1840 vrf_id_t *new_vrf_id)
1841 {
1842 char ifname[INTERFACE_NAMSIZ];
1843 struct interface *ifp;
1844 vrf_id_t new_id;
1845
1846 /* Read interface name. */
1847 stream_get(ifname, s, INTERFACE_NAMSIZ);
1848
1849 /* Lookup interface. */
1850 ifp = if_lookup_by_name(ifname, vrf_id);
1851 if (ifp == NULL) {
1852 flog_err(EC_LIB_ZAPI_ENCODE,
1853 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1854 ifname, vrf_id);
1855 return NULL;
1856 }
1857
1858 /* Fetch new VRF Id. */
1859 new_id = stream_getw(s);
1860
1861 *new_vrf_id = new_id;
1862 return ifp;
1863 }
1864
1865 /* filter unwanted messages until the expected one arrives */
1866 static int zclient_read_sync_response(struct zclient *zclient,
1867 uint16_t expected_cmd)
1868 {
1869 struct stream *s;
1870 uint16_t size = -1;
1871 uint8_t marker;
1872 uint8_t version;
1873 vrf_id_t vrf_id;
1874 uint16_t cmd;
1875 fd_set readfds;
1876 int ret;
1877
1878 ret = 0;
1879 cmd = expected_cmd + 1;
1880 while (ret == 0 && cmd != expected_cmd) {
1881 s = zclient->ibuf;
1882 stream_reset(s);
1883
1884 /* wait until response arrives */
1885 FD_ZERO(&readfds);
1886 FD_SET(zclient->sock, &readfds);
1887 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1888 if (!FD_ISSET(zclient->sock, &readfds))
1889 continue;
1890 /* read response */
1891 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1892 &version, &vrf_id, &cmd);
1893 if (zclient_debug)
1894 zlog_debug("%s: Response (%d bytes) received", __func__,
1895 size);
1896 }
1897 if (ret != 0) {
1898 flog_err(EC_LIB_ZAPI_ENCODE, "%s: Invalid Sync Message Reply",
1899 __func__);
1900 return -1;
1901 }
1902
1903 return 0;
1904 }
1905 /**
1906 * Connect to label manager in a syncronous way
1907 *
1908 * It first writes the request to zcient output buffer and then
1909 * immediately reads the answer from the input buffer.
1910 *
1911 * @param zclient Zclient used to connect to label manager (zebra)
1912 * @param async Synchronous (0) or asynchronous (1) operation
1913 * @result Result of response
1914 */
1915 int lm_label_manager_connect(struct zclient *zclient, int async)
1916 {
1917 int ret;
1918 struct stream *s;
1919 uint8_t result;
1920 uint16_t cmd = async ? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC :
1921 ZEBRA_LABEL_MANAGER_CONNECT;
1922
1923 if (zclient_debug)
1924 zlog_debug("Connecting to Label Manager (LM)");
1925
1926 if (zclient->sock < 0) {
1927 zlog_debug("%s: invalid zclient socket", __func__);
1928 return -1;
1929 }
1930
1931 /* send request */
1932 s = zclient->obuf;
1933 stream_reset(s);
1934 zclient_create_header(s, cmd, VRF_DEFAULT);
1935
1936 /* proto */
1937 stream_putc(s, zclient->redist_default);
1938 /* instance */
1939 stream_putw(s, zclient->instance);
1940
1941 /* Put length at the first point of the stream. */
1942 stream_putw_at(s, 0, stream_get_endp(s));
1943
1944 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1945 if (ret < 0) {
1946 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
1947 close(zclient->sock);
1948 zclient->sock = -1;
1949 return -1;
1950 }
1951 if (ret == 0) {
1952 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
1953 close(zclient->sock);
1954 zclient->sock = -1;
1955 return -1;
1956 }
1957 if (zclient_debug)
1958 zlog_debug("LM connect request sent (%d bytes)", ret);
1959
1960 if (async)
1961 return 0;
1962
1963 /* read response */
1964 if (zclient_read_sync_response(zclient, cmd)
1965 != 0)
1966 return -1;
1967
1968 s = zclient->ibuf;
1969
1970 /* read instance and proto */
1971 uint8_t proto = stream_getc(s);
1972 uint16_t instance = stream_getw(s);
1973
1974 /* sanity */
1975 if (proto != zclient->redist_default)
1976 flog_err(
1977 EC_LIB_ZAPI_ENCODE,
1978 "Wrong proto (%u) in LM connect response. Should be %u",
1979 proto, zclient->redist_default);
1980 if (instance != zclient->instance)
1981 flog_err(
1982 EC_LIB_ZAPI_ENCODE,
1983 "Wrong instId (%u) in LM connect response. Should be %u",
1984 instance, zclient->instance);
1985
1986 /* result code */
1987 result = stream_getc(s);
1988 if (zclient_debug)
1989 zlog_debug("LM connect-response received, result %u", result);
1990
1991 return (int)result;
1992 }
1993
1994 /*
1995 * Asynchronous label chunk request
1996 *
1997 * @param zclient Zclient used to connect to label manager (zebra)
1998 * @param keep Avoid garbage collection
1999 * @param chunk_size Amount of labels requested
2000 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2001 * @result 0 on success, -1 otherwise
2002 */
2003 int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep,
2004 uint32_t chunk_size, uint32_t base)
2005 {
2006 struct stream *s;
2007
2008 if (zclient_debug)
2009 zlog_debug("Getting Label Chunk");
2010
2011 if (zclient->sock < 0)
2012 return -1;
2013
2014 s = zclient->obuf;
2015 stream_reset(s);
2016
2017 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2018 /* proto */
2019 stream_putc(s, zclient->redist_default);
2020 /* instance */
2021 stream_putw(s, zclient->instance);
2022 stream_putc(s, keep);
2023 stream_putl(s, chunk_size);
2024 stream_putl(s, base);
2025
2026 /* Put length at the first point of the stream. */
2027 stream_putw_at(s, 0, stream_get_endp(s));
2028
2029 return zclient_send_message(zclient);
2030 }
2031
2032 /**
2033 * Function to request a label chunk in a syncronous way
2034 *
2035 * It first writes the request to zlcient output buffer and then
2036 * immediately reads the answer from the input buffer.
2037 *
2038 * @param zclient Zclient used to connect to label manager (zebra)
2039 * @param keep Avoid garbage collection
2040 * @param chunk_size Amount of labels requested
2041 * @param start To write first assigned chunk label to
2042 * @param end To write last assigned chunk label to
2043 * @result 0 on success, -1 otherwise
2044 */
2045 int lm_get_label_chunk(struct zclient *zclient, uint8_t keep, uint32_t base,
2046 uint32_t chunk_size, uint32_t *start, uint32_t *end)
2047 {
2048 int ret;
2049 struct stream *s;
2050 uint8_t response_keep;
2051
2052 if (zclient_debug)
2053 zlog_debug("Getting Label Chunk");
2054
2055 if (zclient->sock < 0)
2056 return -1;
2057
2058 /* send request */
2059 s = zclient->obuf;
2060 stream_reset(s);
2061 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2062 /* proto */
2063 stream_putc(s, zclient->redist_default);
2064 /* instance */
2065 stream_putw(s, zclient->instance);
2066 /* keep */
2067 stream_putc(s, keep);
2068 /* chunk size */
2069 stream_putl(s, chunk_size);
2070 /* requested chunk base */
2071 stream_putl(s, base);
2072 /* Put length at the first point of the stream. */
2073 stream_putw_at(s, 0, stream_get_endp(s));
2074
2075 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2076 if (ret < 0) {
2077 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2078 close(zclient->sock);
2079 zclient->sock = -1;
2080 return -1;
2081 }
2082 if (ret == 0) {
2083 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2084 close(zclient->sock);
2085 zclient->sock = -1;
2086 return -1;
2087 }
2088 if (zclient_debug)
2089 zlog_debug("Label chunk request (%d bytes) sent", ret);
2090
2091 /* read response */
2092 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
2093 return -1;
2094
2095 /* parse response */
2096 s = zclient->ibuf;
2097
2098 /* read proto and instance */
2099 uint8_t proto = stream_getc(s);
2100 uint16_t instance = stream_getw(s);
2101
2102 /* sanities */
2103 if (proto != zclient->redist_default)
2104 flog_err(EC_LIB_ZAPI_ENCODE,
2105 "Wrong proto (%u) in get chunk response. Should be %u",
2106 proto, zclient->redist_default);
2107 if (instance != zclient->instance)
2108 flog_err(EC_LIB_ZAPI_ENCODE,
2109 "Wrong instId (%u) in get chunk response Should be %u",
2110 instance, zclient->instance);
2111
2112 /* if we requested a specific chunk and it could not be allocated, the
2113 * response message will end here
2114 */
2115 if (!STREAM_READABLE(s)) {
2116 zlog_info("Unable to assign Label Chunk to %s instance %u",
2117 zebra_route_string(proto), instance);
2118 return -1;
2119 }
2120
2121 /* keep */
2122 response_keep = stream_getc(s);
2123 /* start and end labels */
2124 *start = stream_getl(s);
2125 *end = stream_getl(s);
2126
2127 /* not owning this response */
2128 if (keep != response_keep) {
2129 flog_err(
2130 EC_LIB_ZAPI_ENCODE,
2131 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2132 *start, *end, keep, response_keep);
2133 }
2134 /* sanity */
2135 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
2136 || *end > MPLS_LABEL_UNRESERVED_MAX) {
2137 flog_err(EC_LIB_ZAPI_ENCODE, "Invalid Label chunk: %u - %u",
2138 *start, *end);
2139 return -1;
2140 }
2141
2142 if (zclient_debug)
2143 zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end,
2144 response_keep);
2145
2146 return 0;
2147 }
2148
2149 /**
2150 * Function to release a label chunk
2151 *
2152 * @param zclient Zclient used to connect to label manager (zebra)
2153 * @param start First label of chunk
2154 * @param end Last label of chunk
2155 * @result 0 on success, -1 otherwise
2156 */
2157 int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
2158 uint32_t end)
2159 {
2160 int ret;
2161 struct stream *s;
2162
2163 if (zclient_debug)
2164 zlog_debug("Releasing Label Chunk %u - %u", start, end);
2165
2166 if (zclient->sock < 0)
2167 return -1;
2168
2169 /* send request */
2170 s = zclient->obuf;
2171 stream_reset(s);
2172 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
2173
2174 /* proto */
2175 stream_putc(s, zclient->redist_default);
2176 /* instance */
2177 stream_putw(s, zclient->instance);
2178 /* start */
2179 stream_putl(s, start);
2180 /* end */
2181 stream_putl(s, end);
2182
2183 /* Put length at the first point of the stream. */
2184 stream_putw_at(s, 0, stream_get_endp(s));
2185
2186 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2187 if (ret < 0) {
2188 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2189 close(zclient->sock);
2190 zclient->sock = -1;
2191 return -1;
2192 }
2193 if (ret == 0) {
2194 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock connection closed");
2195 close(zclient->sock);
2196 zclient->sock = -1;
2197 return -1;
2198 }
2199
2200 return 0;
2201 }
2202
2203 /**
2204 * Connect to table manager in a syncronous way
2205 *
2206 * It first writes the request to zcient output buffer and then
2207 * immediately reads the answer from the input buffer.
2208 *
2209 * @param zclient Zclient used to connect to table manager (zebra)
2210 * @result Result of response
2211 */
2212 int tm_table_manager_connect(struct zclient *zclient)
2213 {
2214 int ret;
2215 struct stream *s;
2216 uint8_t result;
2217
2218 if (zclient_debug)
2219 zlog_debug("Connecting to Table Manager");
2220
2221 if (zclient->sock < 0)
2222 return -1;
2223
2224 /* send request */
2225 s = zclient->obuf;
2226 stream_reset(s);
2227 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT);
2228
2229 /* proto */
2230 stream_putc(s, zclient->redist_default);
2231 /* instance */
2232 stream_putw(s, zclient->instance);
2233
2234 /* Put length at the first point of the stream. */
2235 stream_putw_at(s, 0, stream_get_endp(s));
2236
2237 ret = zclient_send_message(zclient);
2238 if (ret < 0)
2239 return -1;
2240
2241 if (zclient_debug)
2242 zlog_debug("%s: Table manager connect request sent", __func__);
2243
2244 /* read response */
2245 if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
2246 != 0)
2247 return -1;
2248
2249 /* result */
2250 s = zclient->ibuf;
2251 STREAM_GETC(s, result);
2252 if (zclient_debug)
2253 zlog_debug(
2254 "%s: Table Manager connect response received, result %u",
2255 __func__, result);
2256
2257 return (int)result;
2258 stream_failure:
2259 return -1;
2260 }
2261
2262 /**
2263 * Function to request a table chunk in a syncronous way
2264 *
2265 * It first writes the request to zclient output buffer and then
2266 * immediately reads the answer from the input buffer.
2267 *
2268 * @param zclient Zclient used to connect to table manager (zebra)
2269 * @param chunk_size Amount of table requested
2270 * @param start to write first assigned chunk table RT ID to
2271 * @param end To write last assigned chunk table RT ID to
2272 * @result 0 on success, -1 otherwise
2273 */
2274 int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
2275 uint32_t *start, uint32_t *end)
2276 {
2277 int ret;
2278 struct stream *s;
2279
2280 if (zclient_debug)
2281 zlog_debug("Getting Table Chunk");
2282
2283 if (zclient->sock < 0)
2284 return -1;
2285
2286 /* send request */
2287 s = zclient->obuf;
2288 stream_reset(s);
2289 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT);
2290 /* chunk size */
2291 stream_putl(s, chunk_size);
2292 /* Put length at the first point of the stream. */
2293 stream_putw_at(s, 0, stream_get_endp(s));
2294
2295 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2296 if (ret < 0) {
2297 flog_err(EC_LIB_ZAPI_SOCKET, "%s: can't write to zclient->sock",
2298 __func__);
2299 close(zclient->sock);
2300 zclient->sock = -1;
2301 return -1;
2302 }
2303 if (ret == 0) {
2304 flog_err(EC_LIB_ZAPI_SOCKET,
2305 "%s: zclient->sock connection closed", __func__);
2306 close(zclient->sock);
2307 zclient->sock = -1;
2308 return -1;
2309 }
2310 if (zclient_debug)
2311 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__,
2312 ret);
2313
2314 /* read response */
2315 if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0)
2316 return -1;
2317
2318 s = zclient->ibuf;
2319 /* start and end table IDs */
2320 STREAM_GETL(s, *start);
2321 STREAM_GETL(s, *end);
2322
2323 if (zclient_debug)
2324 zlog_debug("Table Chunk assign: %u - %u ", *start, *end);
2325
2326 return 0;
2327 stream_failure:
2328 return -1;
2329 }
2330
2331 /**
2332 * Function to release a table chunk
2333 *
2334 * @param zclient Zclient used to connect to table manager (zebra)
2335 * @param start First label of table
2336 * @param end Last label of chunk
2337 * @result 0 on success, -1 otherwise
2338 */
2339 int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
2340 uint32_t end)
2341 {
2342 struct stream *s;
2343
2344 if (zclient_debug)
2345 zlog_debug("Releasing Table Chunk");
2346
2347 if (zclient->sock < 0)
2348 return -1;
2349
2350 /* send request */
2351 s = zclient->obuf;
2352 stream_reset(s);
2353 zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT);
2354
2355 /* start */
2356 stream_putl(s, start);
2357 /* end */
2358 stream_putl(s, end);
2359
2360 /* Put length at the first point of the stream. */
2361 stream_putw_at(s, 0, stream_get_endp(s));
2362
2363 return zclient_send_message(zclient);
2364 }
2365
2366
2367 int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
2368 {
2369 struct stream *s;
2370
2371 /* Reset stream. */
2372 s = zclient->obuf;
2373 stream_reset(s);
2374
2375 zclient_create_header(s, command, VRF_DEFAULT);
2376 stream_write(s, pw->ifname, IF_NAMESIZE);
2377 stream_putl(s, pw->ifindex);
2378
2379 /* Put type */
2380 stream_putl(s, pw->type);
2381
2382 /* Put nexthop */
2383 stream_putl(s, pw->af);
2384 switch (pw->af) {
2385 case AF_INET:
2386 stream_put_in_addr(s, &pw->nexthop.ipv4);
2387 break;
2388 case AF_INET6:
2389 stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16);
2390 break;
2391 default:
2392 flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__);
2393 return -1;
2394 }
2395
2396 /* Put labels */
2397 stream_putl(s, pw->local_label);
2398 stream_putl(s, pw->remote_label);
2399
2400 /* Put flags */
2401 stream_putc(s, pw->flags);
2402
2403 /* Protocol specific fields */
2404 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2405
2406 /* Put length at the first point of the stream. */
2407 stream_putw_at(s, 0, stream_get_endp(s));
2408
2409 return zclient_send_message(zclient);
2410 }
2411
2412 /*
2413 * Receive PW status update from Zebra and send it to LDE process.
2414 */
2415 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS, struct zapi_pw_status *pw)
2416 {
2417 struct stream *s;
2418
2419 memset(pw, 0, sizeof(struct zapi_pw_status));
2420 s = zclient->ibuf;
2421
2422 /* Get data. */
2423 stream_get(pw->ifname, s, IF_NAMESIZE);
2424 pw->ifindex = stream_getl(s);
2425 pw->status = stream_getl(s);
2426 }
2427
2428 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS)
2429 {
2430 struct zclient_capabilities cap;
2431 struct stream *s = zclient->ibuf;
2432 int vrf_backend;
2433 uint8_t mpls_enabled;
2434
2435 STREAM_GETL(s, vrf_backend);
2436 vrf_configure_backend(vrf_backend);
2437
2438 memset(&cap, 0, sizeof(cap));
2439 STREAM_GETC(s, mpls_enabled);
2440 cap.mpls_enabled = !!mpls_enabled;
2441 STREAM_GETL(s, cap.ecmp);
2442 STREAM_GETC(s, cap.role);
2443
2444 if (zclient->zebra_capabilities)
2445 (*zclient->zebra_capabilities)(&cap);
2446
2447 stream_failure:
2448 return;
2449 }
2450
2451 /* Zebra client message read function. */
2452 static int zclient_read(struct thread *thread)
2453 {
2454 size_t already;
2455 uint16_t length, command;
2456 uint8_t marker, version;
2457 vrf_id_t vrf_id;
2458 struct zclient *zclient;
2459
2460 /* Get socket to zebra. */
2461 zclient = THREAD_ARG(thread);
2462 zclient->t_read = NULL;
2463
2464 /* Read zebra header (if we don't have it already). */
2465 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2466 ssize_t nbyte;
2467 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2468 ZEBRA_HEADER_SIZE - already))
2469 == 0)
2470 || (nbyte == -1)) {
2471 if (zclient_debug)
2472 zlog_debug(
2473 "zclient connection closed socket [%d].",
2474 zclient->sock);
2475 return zclient_failed(zclient);
2476 }
2477 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2478 /* Try again later. */
2479 zclient_event(ZCLIENT_READ, zclient);
2480 return 0;
2481 }
2482 already = ZEBRA_HEADER_SIZE;
2483 }
2484
2485 /* Reset to read from the beginning of the incoming packet. */
2486 stream_set_getp(zclient->ibuf, 0);
2487
2488 /* Fetch header values. */
2489 length = stream_getw(zclient->ibuf);
2490 marker = stream_getc(zclient->ibuf);
2491 version = stream_getc(zclient->ibuf);
2492 vrf_id = stream_getl(zclient->ibuf);
2493 command = stream_getw(zclient->ibuf);
2494
2495 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2496 flog_err(
2497 EC_LIB_ZAPI_MISSMATCH,
2498 "%s: socket %d version mismatch, marker %d, version %d",
2499 __func__, zclient->sock, marker, version);
2500 return zclient_failed(zclient);
2501 }
2502
2503 if (length < ZEBRA_HEADER_SIZE) {
2504 flog_err(EC_LIB_ZAPI_MISSMATCH,
2505 "%s: socket %d message length %u is less than %d ",
2506 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2507 return zclient_failed(zclient);
2508 }
2509
2510 /* Length check. */
2511 if (length > STREAM_SIZE(zclient->ibuf)) {
2512 struct stream *ns;
2513 flog_err(
2514 EC_LIB_ZAPI_ENCODE,
2515 "%s: message size %u exceeds buffer size %lu, expanding...",
2516 __func__, length,
2517 (unsigned long)STREAM_SIZE(zclient->ibuf));
2518 ns = stream_new(length);
2519 stream_copy(ns, zclient->ibuf);
2520 stream_free(zclient->ibuf);
2521 zclient->ibuf = ns;
2522 }
2523
2524 /* Read rest of zebra packet. */
2525 if (already < length) {
2526 ssize_t nbyte;
2527 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2528 length - already))
2529 == 0)
2530 || (nbyte == -1)) {
2531 if (zclient_debug)
2532 zlog_debug(
2533 "zclient connection closed socket [%d].",
2534 zclient->sock);
2535 return zclient_failed(zclient);
2536 }
2537 if (nbyte != (ssize_t)(length - already)) {
2538 /* Try again later. */
2539 zclient_event(ZCLIENT_READ, zclient);
2540 return 0;
2541 }
2542 }
2543
2544 length -= ZEBRA_HEADER_SIZE;
2545
2546 if (zclient_debug)
2547 zlog_debug("zclient 0x%p command %s VRF %u",
2548 (void *)zclient, zserv_command_string(command),
2549 vrf_id);
2550
2551 switch (command) {
2552 case ZEBRA_CAPABILITIES:
2553 zclient_capability_decode(command, zclient, length, vrf_id);
2554 break;
2555 case ZEBRA_ROUTER_ID_UPDATE:
2556 if (zclient->router_id_update)
2557 (*zclient->router_id_update)(command, zclient, length,
2558 vrf_id);
2559 break;
2560 case ZEBRA_VRF_ADD:
2561 zclient_vrf_add(zclient, vrf_id);
2562 break;
2563 case ZEBRA_VRF_DELETE:
2564 zclient_vrf_delete(zclient, vrf_id);
2565 break;
2566 case ZEBRA_INTERFACE_ADD:
2567 if (zclient->interface_add)
2568 (*zclient->interface_add)(command, zclient, length,
2569 vrf_id);
2570 break;
2571 case ZEBRA_INTERFACE_DELETE:
2572 if (zclient->interface_delete)
2573 (*zclient->interface_delete)(command, zclient, length,
2574 vrf_id);
2575 break;
2576 case ZEBRA_INTERFACE_ADDRESS_ADD:
2577 if (zclient->interface_address_add)
2578 (*zclient->interface_address_add)(command, zclient,
2579 length, vrf_id);
2580 break;
2581 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2582 if (zclient->interface_address_delete)
2583 (*zclient->interface_address_delete)(command, zclient,
2584 length, vrf_id);
2585 break;
2586 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2587 if (zclient->interface_bfd_dest_update)
2588 (*zclient->interface_bfd_dest_update)(command, zclient,
2589 length, vrf_id);
2590 break;
2591 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2592 if (zclient->interface_nbr_address_add)
2593 (*zclient->interface_nbr_address_add)(command, zclient,
2594 length, vrf_id);
2595 break;
2596 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2597 if (zclient->interface_nbr_address_delete)
2598 (*zclient->interface_nbr_address_delete)(
2599 command, zclient, length, vrf_id);
2600 break;
2601 case ZEBRA_INTERFACE_UP:
2602 if (zclient->interface_up)
2603 (*zclient->interface_up)(command, zclient, length,
2604 vrf_id);
2605 break;
2606 case ZEBRA_INTERFACE_DOWN:
2607 if (zclient->interface_down)
2608 (*zclient->interface_down)(command, zclient, length,
2609 vrf_id);
2610 break;
2611 case ZEBRA_INTERFACE_VRF_UPDATE:
2612 if (zclient->interface_vrf_update)
2613 (*zclient->interface_vrf_update)(command, zclient,
2614 length, vrf_id);
2615 break;
2616 case ZEBRA_NEXTHOP_UPDATE:
2617 if (zclient_debug)
2618 zlog_debug("zclient rcvd nexthop update");
2619 if (zclient->nexthop_update)
2620 (*zclient->nexthop_update)(command, zclient, length,
2621 vrf_id);
2622 break;
2623 case ZEBRA_IMPORT_CHECK_UPDATE:
2624 if (zclient_debug)
2625 zlog_debug("zclient rcvd import check update");
2626 if (zclient->import_check_update)
2627 (*zclient->import_check_update)(command, zclient,
2628 length, vrf_id);
2629 break;
2630 case ZEBRA_BFD_DEST_REPLAY:
2631 if (zclient->bfd_dest_replay)
2632 (*zclient->bfd_dest_replay)(command, zclient, length,
2633 vrf_id);
2634 break;
2635 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2636 if (zclient->redistribute_route_add)
2637 (*zclient->redistribute_route_add)(command, zclient,
2638 length, vrf_id);
2639 break;
2640 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2641 if (zclient->redistribute_route_del)
2642 (*zclient->redistribute_route_del)(command, zclient,
2643 length, vrf_id);
2644 break;
2645 case ZEBRA_INTERFACE_LINK_PARAMS:
2646 if (zclient->interface_link_params)
2647 (*zclient->interface_link_params)(command, zclient,
2648 length, vrf_id);
2649 break;
2650 case ZEBRA_FEC_UPDATE:
2651 if (zclient_debug)
2652 zlog_debug("zclient rcvd fec update");
2653 if (zclient->fec_update)
2654 (*zclient->fec_update)(command, zclient, length);
2655 break;
2656 case ZEBRA_LOCAL_ES_ADD:
2657 if (zclient->local_es_add)
2658 (*zclient->local_es_add)(command, zclient, length,
2659 vrf_id);
2660 break;
2661 case ZEBRA_LOCAL_ES_DEL:
2662 if (zclient->local_es_del)
2663 (*zclient->local_es_del)(command, zclient, length,
2664 vrf_id);
2665 break;
2666 case ZEBRA_VNI_ADD:
2667 if (zclient->local_vni_add)
2668 (*zclient->local_vni_add)(command, zclient, length,
2669 vrf_id);
2670 break;
2671 case ZEBRA_VNI_DEL:
2672 if (zclient->local_vni_del)
2673 (*zclient->local_vni_del)(command, zclient, length,
2674 vrf_id);
2675 break;
2676 case ZEBRA_L3VNI_ADD:
2677 if (zclient->local_l3vni_add)
2678 (*zclient->local_l3vni_add)(command, zclient, length,
2679 vrf_id);
2680 break;
2681 case ZEBRA_L3VNI_DEL:
2682 if (zclient->local_l3vni_del)
2683 (*zclient->local_l3vni_del)(command, zclient, length,
2684 vrf_id);
2685 break;
2686 case ZEBRA_MACIP_ADD:
2687 if (zclient->local_macip_add)
2688 (*zclient->local_macip_add)(command, zclient, length,
2689 vrf_id);
2690 break;
2691 case ZEBRA_MACIP_DEL:
2692 if (zclient->local_macip_del)
2693 (*zclient->local_macip_del)(command, zclient, length,
2694 vrf_id);
2695 break;
2696 case ZEBRA_IP_PREFIX_ROUTE_ADD:
2697 if (zclient->local_ip_prefix_add)
2698 (*zclient->local_ip_prefix_add)(command, zclient,
2699 length, vrf_id);
2700 break;
2701 case ZEBRA_IP_PREFIX_ROUTE_DEL:
2702 if (zclient->local_ip_prefix_del)
2703 (*zclient->local_ip_prefix_del)(command, zclient,
2704 length, vrf_id);
2705 break;
2706 case ZEBRA_PW_STATUS_UPDATE:
2707 if (zclient->pw_status_update)
2708 (*zclient->pw_status_update)(command, zclient, length,
2709 vrf_id);
2710 break;
2711 case ZEBRA_ROUTE_NOTIFY_OWNER:
2712 if (zclient->route_notify_owner)
2713 (*zclient->route_notify_owner)(command, zclient, length,
2714 vrf_id);
2715 break;
2716 case ZEBRA_RULE_NOTIFY_OWNER:
2717 if (zclient->rule_notify_owner)
2718 (*zclient->rule_notify_owner)(command, zclient, length,
2719 vrf_id);
2720 break;
2721 case ZEBRA_GET_LABEL_CHUNK:
2722 if (zclient->label_chunk)
2723 (*zclient->label_chunk)(command, zclient, length,
2724 vrf_id);
2725 break;
2726 case ZEBRA_IPSET_NOTIFY_OWNER:
2727 if (zclient->ipset_notify_owner)
2728 (*zclient->ipset_notify_owner)(command, zclient, length,
2729 vrf_id);
2730 break;
2731 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER:
2732 if (zclient->ipset_entry_notify_owner)
2733 (*zclient->ipset_entry_notify_owner)(command,
2734 zclient, length,
2735 vrf_id);
2736 break;
2737 case ZEBRA_IPTABLE_NOTIFY_OWNER:
2738 if (zclient->iptable_notify_owner)
2739 (*zclient->iptable_notify_owner)(command,
2740 zclient, length,
2741 vrf_id);
2742 break;
2743 case ZEBRA_VXLAN_SG_ADD:
2744 if (zclient->vxlan_sg_add)
2745 (*zclient->vxlan_sg_add)(command, zclient, length,
2746 vrf_id);
2747 break;
2748 case ZEBRA_VXLAN_SG_DEL:
2749 if (zclient->vxlan_sg_del)
2750 (*zclient->vxlan_sg_del)(command, zclient, length,
2751 vrf_id);
2752 break;
2753 default:
2754 break;
2755 }
2756
2757 if (zclient->sock < 0)
2758 /* Connection was closed during packet processing. */
2759 return -1;
2760
2761 /* Register read thread. */
2762 stream_reset(zclient->ibuf);
2763 zclient_event(ZCLIENT_READ, zclient);
2764
2765 return 0;
2766 }
2767
2768 void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2769 int type, unsigned short instance, vrf_id_t vrf_id)
2770 {
2771
2772 if (instance) {
2773 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2774 if (redist_check_instance(
2775 &zclient->mi_redist[afi][type], instance))
2776 return;
2777 redist_add_instance(&zclient->mi_redist[afi][type],
2778 instance);
2779 } else {
2780 if (!redist_check_instance(
2781 &zclient->mi_redist[afi][type], instance))
2782 return;
2783 redist_del_instance(&zclient->mi_redist[afi][type],
2784 instance);
2785 }
2786
2787 } else {
2788 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2789 if (vrf_bitmap_check(zclient->redist[afi][type],
2790 vrf_id))
2791 return;
2792 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2793 } else {
2794 if (!vrf_bitmap_check(zclient->redist[afi][type],
2795 vrf_id))
2796 return;
2797 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2798 }
2799 }
2800
2801 if (zclient->sock > 0)
2802 zebra_redistribute_send(command, zclient, afi, type, instance,
2803 vrf_id);
2804 }
2805
2806
2807 void zclient_redistribute_default(int command, struct zclient *zclient,
2808 afi_t afi, vrf_id_t vrf_id)
2809 {
2810
2811 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2812 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
2813 return;
2814 vrf_bitmap_set(zclient->default_information[afi], vrf_id);
2815 } else {
2816 if (!vrf_bitmap_check(zclient->default_information[afi],
2817 vrf_id))
2818 return;
2819 vrf_bitmap_unset(zclient->default_information[afi], vrf_id);
2820 }
2821
2822 if (zclient->sock > 0)
2823 zebra_redistribute_default_send(command, zclient, afi, vrf_id);
2824 }
2825
2826 static void zclient_event(enum event event, struct zclient *zclient)
2827 {
2828 switch (event) {
2829 case ZCLIENT_SCHEDULE:
2830 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2831 &zclient->t_connect);
2832 break;
2833 case ZCLIENT_CONNECT:
2834 if (zclient_debug)
2835 zlog_debug(
2836 "zclient connect failures: %d schedule interval is now %d",
2837 zclient->fail, zclient->fail < 3 ? 10 : 60);
2838 thread_add_timer(zclient->master, zclient_connect, zclient,
2839 zclient->fail < 3 ? 10 : 60,
2840 &zclient->t_connect);
2841 break;
2842 case ZCLIENT_READ:
2843 zclient->t_read = NULL;
2844 thread_add_read(zclient->master, zclient_read, zclient,
2845 zclient->sock, &zclient->t_read);
2846 break;
2847 }
2848 }
2849
2850 void zclient_interface_set_master(struct zclient *client,
2851 struct interface *master,
2852 struct interface *slave)
2853 {
2854 struct stream *s;
2855
2856 s = client->obuf;
2857 stream_reset(s);
2858
2859 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2860
2861 stream_putl(s, master->vrf_id);
2862 stream_putl(s, master->ifindex);
2863 stream_putl(s, slave->vrf_id);
2864 stream_putl(s, slave->ifindex);
2865
2866 stream_putw_at(s, 0, stream_get_endp(s));
2867 zclient_send_message(client);
2868 }