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