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