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