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