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