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