]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
lib, zebra: Send safi for rnh resolution
[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 #include "srte.h"
43 #include "printfrr.h"
44 #include "srv6.h"
45
46 DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient");
47 DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs");
48
49 /* Zebra client events. */
50 enum event { ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT };
51
52 /* Prototype for event manager. */
53 static void zclient_event(enum event, struct zclient *);
54
55 static void zebra_interface_if_set_value(struct stream *s,
56 struct interface *ifp);
57
58 struct zclient_options zclient_options_default = {.receive_notify = false,
59 .synchronous = false};
60
61 struct sockaddr_storage zclient_addr;
62 socklen_t zclient_addr_len;
63
64 /* This file local debug flag. */
65 static int zclient_debug;
66
67 /* Allocate zclient structure. */
68 struct zclient *zclient_new(struct thread_master *master,
69 struct zclient_options *opt)
70 {
71 struct zclient *zclient;
72 size_t stream_size =
73 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
74
75 zclient = XCALLOC(MTYPE_ZCLIENT, sizeof(struct zclient));
76
77 zclient->ibuf = stream_new(stream_size);
78 zclient->obuf = stream_new(stream_size);
79 zclient->wb = buffer_new(0);
80 zclient->master = master;
81
82 zclient->receive_notify = opt->receive_notify;
83 zclient->synchronous = opt->synchronous;
84
85 return zclient;
86 }
87
88 /* This function is only called when exiting, because
89 many parts of the code do not check for I/O errors, so they could
90 reference an invalid pointer if the structure was ever freed.
91
92 Free zclient structure. */
93 void zclient_free(struct zclient *zclient)
94 {
95 if (zclient->ibuf)
96 stream_free(zclient->ibuf);
97 if (zclient->obuf)
98 stream_free(zclient->obuf);
99 if (zclient->wb)
100 buffer_free(zclient->wb);
101
102 XFREE(MTYPE_ZCLIENT, zclient);
103 }
104
105 unsigned short *redist_check_instance(struct redist_proto *red,
106 unsigned short instance)
107 {
108 struct listnode *node;
109 unsigned short *id;
110
111 if (!red->instances)
112 return NULL;
113
114 for (ALL_LIST_ELEMENTS_RO(red->instances, node, id))
115 if (*id == instance)
116 return id;
117
118 return NULL;
119 }
120
121 void redist_add_instance(struct redist_proto *red, unsigned short instance)
122 {
123 unsigned short *in;
124
125 red->enabled = 1;
126
127 if (!red->instances)
128 red->instances = list_new();
129
130 in = XMALLOC(MTYPE_REDIST_INST, sizeof(unsigned short));
131 *in = instance;
132 listnode_add(red->instances, in);
133 }
134
135 void redist_del_instance(struct redist_proto *red, unsigned short instance)
136 {
137 unsigned short *id;
138
139 id = redist_check_instance(red, instance);
140 if (!id)
141 return;
142
143 listnode_delete(red->instances, id);
144 XFREE(MTYPE_REDIST_INST, id);
145 if (!red->instances->count) {
146 red->enabled = 0;
147 list_delete(&red->instances);
148 }
149 }
150
151 void redist_del_all_instances(struct redist_proto *red)
152 {
153 struct listnode *ln, *nn;
154 unsigned short *id;
155
156 if (!red->instances)
157 return;
158
159 for (ALL_LIST_ELEMENTS(red->instances, ln, nn, id))
160 redist_del_instance(red, *id);
161 }
162
163 /* Stop zebra client services. */
164 void zclient_stop(struct zclient *zclient)
165 {
166 afi_t afi;
167 int i;
168
169 if (zclient_debug)
170 zlog_debug("zclient %p stopped", zclient);
171
172 /* Stop threads. */
173 THREAD_OFF(zclient->t_read);
174 THREAD_OFF(zclient->t_connect);
175 THREAD_OFF(zclient->t_write);
176
177 /* Reset streams. */
178 stream_reset(zclient->ibuf);
179 stream_reset(zclient->obuf);
180
181 /* Empty the write buffer. */
182 buffer_reset(zclient->wb);
183
184 /* Close socket. */
185 if (zclient->sock >= 0) {
186 close(zclient->sock);
187 zclient->sock = -1;
188 }
189 zclient->fail = 0;
190
191 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
192 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
193 vrf_bitmap_free(zclient->redist[afi][i]);
194 zclient->redist[afi][i] = VRF_BITMAP_NULL;
195 }
196 redist_del_instance(
197 &zclient->mi_redist[afi][zclient->redist_default],
198 zclient->instance);
199
200 vrf_bitmap_free(zclient->default_information[afi]);
201 zclient->default_information[afi] = VRF_BITMAP_NULL;
202 }
203 }
204
205 void zclient_reset(struct zclient *zclient)
206 {
207 afi_t afi;
208
209 zclient_stop(zclient);
210
211 for (afi = AFI_IP; afi < AFI_MAX; afi++)
212 redist_del_instance(
213 &zclient->mi_redist[afi][zclient->redist_default],
214 zclient->instance);
215
216 zclient_init(zclient, zclient->redist_default, zclient->instance,
217 zclient->privs);
218 }
219
220 /**
221 * Connect to zebra daemon.
222 * @param zclient a pointer to zclient structure
223 * @return socket fd just to make sure that connection established
224 * @see zclient_init
225 * @see zclient_new
226 */
227 int zclient_socket_connect(struct zclient *zclient)
228 {
229 int sock;
230 int ret;
231
232 /* We should think about IPv6 connection. */
233 sock = socket(zclient_addr.ss_family, SOCK_STREAM, 0);
234 if (sock < 0)
235 return -1;
236
237 set_cloexec(sock);
238 setsockopt_so_sendbuf(sock, 1048576);
239
240 /* Connect to zebra. */
241 ret = connect(sock, (struct sockaddr *)&zclient_addr, zclient_addr_len);
242 if (ret < 0) {
243 if (zclient_debug)
244 zlog_debug("%s connect failure: %d(%s)", __func__,
245 errno, safe_strerror(errno));
246 close(sock);
247 return -1;
248 }
249
250 zclient->sock = sock;
251 return sock;
252 }
253
254 static enum zclient_send_status zclient_failed(struct zclient *zclient)
255 {
256 zclient->fail++;
257 zclient_stop(zclient);
258 zclient_event(ZCLIENT_CONNECT, zclient);
259 return ZCLIENT_SEND_FAILURE;
260 }
261
262 static int zclient_flush_data(struct thread *thread)
263 {
264 struct zclient *zclient = THREAD_ARG(thread);
265
266 zclient->t_write = NULL;
267 if (zclient->sock < 0)
268 return -1;
269 switch (buffer_flush_available(zclient->wb, zclient->sock)) {
270 case BUFFER_ERROR:
271 flog_err(
272 EC_LIB_ZAPI_SOCKET,
273 "%s: buffer_flush_available failed on zclient fd %d, closing",
274 __func__, zclient->sock);
275 return zclient_failed(zclient);
276 case BUFFER_PENDING:
277 zclient->t_write = NULL;
278 thread_add_write(zclient->master, zclient_flush_data, zclient,
279 zclient->sock, &zclient->t_write);
280 break;
281 case BUFFER_EMPTY:
282 if (zclient->zebra_buffer_write_ready)
283 (*zclient->zebra_buffer_write_ready)();
284 break;
285 }
286 return 0;
287 }
288
289 /*
290 * Returns:
291 * ZCLIENT_SEND_FAILED - is a failure
292 * ZCLIENT_SEND_SUCCESS - means we sent data to zebra
293 * ZCLIENT_SEND_BUFFERED - means we are buffering
294 */
295 enum zclient_send_status zclient_send_message(struct zclient *zclient)
296 {
297 if (zclient->sock < 0)
298 return ZCLIENT_SEND_FAILURE;
299 switch (buffer_write(zclient->wb, zclient->sock,
300 STREAM_DATA(zclient->obuf),
301 stream_get_endp(zclient->obuf))) {
302 case BUFFER_ERROR:
303 flog_err(EC_LIB_ZAPI_SOCKET,
304 "%s: buffer_write failed to zclient fd %d, closing",
305 __func__, zclient->sock);
306 return zclient_failed(zclient);
307 case BUFFER_EMPTY:
308 THREAD_OFF(zclient->t_write);
309 return ZCLIENT_SEND_SUCCESS;
310 case BUFFER_PENDING:
311 thread_add_write(zclient->master, zclient_flush_data, zclient,
312 zclient->sock, &zclient->t_write);
313 return ZCLIENT_SEND_BUFFERED;
314 }
315
316 /* should not get here */
317 return ZCLIENT_SEND_SUCCESS;
318 }
319
320 /*
321 * If we add more data to this structure please ensure that
322 * struct zmsghdr in lib/zclient.h is updated as appropriate.
323 */
324 void zclient_create_header(struct stream *s, uint16_t command, vrf_id_t vrf_id)
325 {
326 /* length placeholder, caller can update */
327 stream_putw(s, ZEBRA_HEADER_SIZE);
328 stream_putc(s, ZEBRA_HEADER_MARKER);
329 stream_putc(s, ZSERV_VERSION);
330 stream_putl(s, vrf_id);
331 stream_putw(s, command);
332 }
333
334 int zclient_read_header(struct stream *s, int sock, uint16_t *size,
335 uint8_t *marker, uint8_t *version, vrf_id_t *vrf_id,
336 uint16_t *cmd)
337 {
338 if (stream_read(s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
339 return -1;
340
341 STREAM_GETW(s, *size);
342 *size -= ZEBRA_HEADER_SIZE;
343 STREAM_GETC(s, *marker);
344 STREAM_GETC(s, *version);
345 STREAM_GETL(s, *vrf_id);
346 STREAM_GETW(s, *cmd);
347
348 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER) {
349 flog_err(
350 EC_LIB_ZAPI_MISSMATCH,
351 "%s: socket %d version mismatch, marker %d, version %d",
352 __func__, sock, *marker, *version);
353 return -1;
354 }
355
356 if (*size && stream_read(s, sock, *size) != *size)
357 return -1;
358
359 return 0;
360 stream_failure:
361 return -1;
362 }
363
364 bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr)
365 {
366 STREAM_GETW(zmsg, hdr->length);
367 STREAM_GETC(zmsg, hdr->marker);
368 STREAM_GETC(zmsg, hdr->version);
369 STREAM_GETL(zmsg, hdr->vrf_id);
370 STREAM_GETW(zmsg, hdr->command);
371 return true;
372 stream_failure:
373 return false;
374 }
375
376 /* Send simple Zebra message. */
377 static enum zclient_send_status zebra_message_send(struct zclient *zclient,
378 int command, vrf_id_t vrf_id)
379 {
380 struct stream *s;
381
382 /* Get zclient output buffer. */
383 s = zclient->obuf;
384 stream_reset(s);
385
386 /* Send very simple command only Zebra message. */
387 zclient_create_header(s, command, vrf_id);
388
389 return zclient_send_message(zclient);
390 }
391
392 enum zclient_send_status zclient_send_hello(struct zclient *zclient)
393 {
394 struct stream *s;
395
396 if (zclient->redist_default || zclient->synchronous) {
397 s = zclient->obuf;
398 stream_reset(s);
399
400 /* The VRF ID in the HELLO message is always 0. */
401 zclient_create_header(s, ZEBRA_HELLO, VRF_DEFAULT);
402 stream_putc(s, zclient->redist_default);
403 stream_putw(s, zclient->instance);
404 stream_putl(s, zclient->session_id);
405 if (zclient->receive_notify)
406 stream_putc(s, 1);
407 else
408 stream_putc(s, 0);
409 if (zclient->synchronous)
410 stream_putc(s, 1);
411 else
412 stream_putc(s, 0);
413
414 stream_putw_at(s, 0, stream_get_endp(s));
415 return zclient_send_message(zclient);
416 }
417
418 return ZCLIENT_SEND_SUCCESS;
419 }
420
421 enum zclient_send_status zclient_send_vrf_label(struct zclient *zclient,
422 vrf_id_t vrf_id, afi_t afi,
423 mpls_label_t label,
424 enum lsp_types_t ltype)
425 {
426 struct stream *s;
427
428 s = zclient->obuf;
429 stream_reset(s);
430
431 zclient_create_header(s, ZEBRA_VRF_LABEL, vrf_id);
432 stream_putl(s, label);
433 stream_putc(s, afi);
434 stream_putc(s, ltype);
435 stream_putw_at(s, 0, stream_get_endp(s));
436 return zclient_send_message(zclient);
437 }
438
439 enum zclient_send_status zclient_send_localsid(struct zclient *zclient,
440 const struct in6_addr *sid, ifindex_t oif,
441 enum seg6local_action_t action,
442 const struct seg6local_context *context)
443 {
444 struct prefix_ipv6 p = {};
445 struct zapi_route api = {};
446 struct nexthop nh = {};
447
448 p.family = AF_INET6;
449 p.prefixlen = IPV6_MAX_BITLEN;
450 p.prefix = *sid;
451
452 api.vrf_id = VRF_DEFAULT;
453 api.type = ZEBRA_ROUTE_BGP;
454 api.instance = 0;
455 api.safi = SAFI_UNICAST;
456 memcpy(&api.prefix, &p, sizeof(p));
457
458 if (action == ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
459 return zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
460
461 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
462 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
463
464 nh.type = NEXTHOP_TYPE_IFINDEX;
465 nh.ifindex = oif;
466 SET_FLAG(nh.flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL);
467 nexthop_add_srv6_seg6local(&nh, action, context);
468
469 zapi_nexthop_from_nexthop(&api.nexthops[0], &nh);
470 api.nexthop_num = 1;
471
472 return zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
473 }
474
475 /* Send register requests to zebra daemon for the information in a VRF. */
476 void zclient_send_reg_requests(struct zclient *zclient, vrf_id_t vrf_id)
477 {
478 int i;
479 afi_t afi;
480
481 /* If not connected to the zebra yet. */
482 if (zclient->sock < 0)
483 return;
484
485 if (zclient_debug)
486 zlog_debug("%s: send register messages for VRF %u", __func__,
487 vrf_id);
488
489 /* We need router-id information. */
490 zclient_send_router_id_update(zclient, ZEBRA_ROUTER_ID_ADD, AFI_IP,
491 vrf_id);
492
493 /* We need interface information. */
494 zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, vrf_id);
495
496 /* Set unwanted redistribute route. */
497 for (afi = AFI_IP; afi < AFI_MAX; afi++)
498 vrf_bitmap_set(zclient->redist[afi][zclient->redist_default],
499 vrf_id);
500
501 /* Flush all redistribute request. */
502 if (vrf_id == VRF_DEFAULT) {
503 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
504 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
505 if (!zclient->mi_redist[afi][i].enabled)
506 continue;
507
508 struct listnode *node;
509 unsigned short *id;
510
511 for (ALL_LIST_ELEMENTS_RO(
512 zclient->mi_redist[afi][i]
513 .instances,
514 node, id))
515 if (!(i == zclient->redist_default
516 && *id == zclient->instance))
517 zebra_redistribute_send(
518 ZEBRA_REDISTRIBUTE_ADD,
519 zclient, afi, i, *id,
520 VRF_DEFAULT);
521 }
522 }
523 }
524
525 /* Resend all redistribute request. */
526 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
527 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
528 if (i != zclient->redist_default
529 && vrf_bitmap_check(zclient->redist[afi][i],
530 vrf_id))
531 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD,
532 zclient, afi, i, 0,
533 vrf_id);
534
535 /* If default information is needed. */
536 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
537 zebra_redistribute_default_send(
538 ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient, afi,
539 vrf_id);
540 }
541 }
542
543 /* Send unregister requests to zebra daemon for the information in a VRF. */
544 void zclient_send_dereg_requests(struct zclient *zclient, vrf_id_t vrf_id)
545 {
546 int i;
547 afi_t afi;
548
549 /* If not connected to the zebra yet. */
550 if (zclient->sock < 0)
551 return;
552
553 if (zclient_debug)
554 zlog_debug("%s: send deregister messages for VRF %u", __func__,
555 vrf_id);
556
557 /* We need router-id information. */
558 zclient_send_router_id_update(zclient, ZEBRA_ROUTER_ID_DELETE, AFI_IP,
559 vrf_id);
560
561 zebra_message_send(zclient, ZEBRA_INTERFACE_DELETE, vrf_id);
562
563 /* Set unwanted redistribute route. */
564 for (afi = AFI_IP; afi < AFI_MAX; afi++)
565 vrf_bitmap_unset(zclient->redist[afi][zclient->redist_default],
566 vrf_id);
567
568 /* Flush all redistribute request. */
569 if (vrf_id == VRF_DEFAULT) {
570 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
571 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
572 if (!zclient->mi_redist[afi][i].enabled)
573 continue;
574
575 struct listnode *node;
576 unsigned short *id;
577
578 for (ALL_LIST_ELEMENTS_RO(
579 zclient->mi_redist[afi][i]
580 .instances,
581 node, id))
582 if (!(i == zclient->redist_default
583 && *id == zclient->instance))
584 zebra_redistribute_send(
585 ZEBRA_REDISTRIBUTE_DELETE,
586 zclient, afi, i, *id,
587 VRF_DEFAULT);
588 }
589 }
590 }
591
592 /* Flush all redistribute request. */
593 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
594 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
595 if (i != zclient->redist_default
596 && vrf_bitmap_check(zclient->redist[afi][i],
597 vrf_id))
598 zebra_redistribute_send(
599 ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
600 i, 0, vrf_id);
601
602 /* If default information is needed. */
603 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
604 zebra_redistribute_default_send(
605 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient, afi,
606 vrf_id);
607 }
608 }
609
610 enum zclient_send_status
611 zclient_send_router_id_update(struct zclient *zclient,
612 zebra_message_types_t type, afi_t afi,
613 vrf_id_t vrf_id)
614 {
615 struct stream *s = zclient->obuf;
616 stream_reset(s);
617 zclient_create_header(s, type, vrf_id);
618 stream_putw(s, afi);
619 stream_putw_at(s, 0, stream_get_endp(s));
620 return zclient_send_message(zclient);
621 }
622
623 /* Send request to zebra daemon to start or stop RA. */
624 enum zclient_send_status
625 zclient_send_interface_radv_req(struct zclient *zclient, vrf_id_t vrf_id,
626 struct interface *ifp, int enable,
627 uint32_t ra_interval)
628 {
629 struct stream *s;
630
631 /* If not connected to the zebra yet. */
632 if (zclient->sock < 0)
633 return ZCLIENT_SEND_FAILURE;
634
635 /* Form and send message. */
636 s = zclient->obuf;
637 stream_reset(s);
638
639 if (enable)
640 zclient_create_header(s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id);
641 else
642 zclient_create_header(s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id);
643
644 stream_putl(s, ifp->ifindex);
645 stream_putl(s, ra_interval);
646
647 stream_putw_at(s, 0, stream_get_endp(s));
648
649 return zclient_send_message(zclient);
650 }
651
652 enum zclient_send_status
653 zclient_send_interface_protodown(struct zclient *zclient, vrf_id_t vrf_id,
654 struct interface *ifp, bool down)
655 {
656 struct stream *s;
657
658 if (zclient->sock < 0)
659 return ZCLIENT_SEND_FAILURE;
660
661 s = zclient->obuf;
662 stream_reset(s);
663 zclient_create_header(s, ZEBRA_INTERFACE_SET_PROTODOWN, vrf_id);
664 stream_putl(s, ifp->ifindex);
665 stream_putc(s, !!down);
666 stream_putw_at(s, 0, stream_get_endp(s));
667 return zclient_send_message(zclient);
668 }
669
670 /* Make connection to zebra daemon. */
671 int zclient_start(struct zclient *zclient)
672 {
673 if (zclient_debug)
674 zlog_info("zclient_start is called");
675
676 /* If already connected to the zebra. */
677 if (zclient->sock >= 0)
678 return 0;
679
680 /* Check connect thread. */
681 if (zclient->t_connect)
682 return 0;
683
684 if (zclient_socket_connect(zclient) < 0) {
685 if (zclient_debug)
686 zlog_debug("zclient connection fail");
687 zclient->fail++;
688 zclient_event(ZCLIENT_CONNECT, zclient);
689 return -1;
690 }
691
692 if (set_nonblocking(zclient->sock) < 0)
693 flog_err(EC_LIB_ZAPI_SOCKET, "%s: set_nonblocking(%d) failed",
694 __func__, zclient->sock);
695
696 /* Clear fail count. */
697 zclient->fail = 0;
698 if (zclient_debug)
699 zlog_debug("zclient connect success with socket [%d]",
700 zclient->sock);
701
702 /* Create read thread. */
703 zclient_event(ZCLIENT_READ, zclient);
704
705 zclient_send_hello(zclient);
706
707 zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, VRF_DEFAULT);
708
709 /* Inform the successful connection. */
710 if (zclient->zebra_connected)
711 (*zclient->zebra_connected)(zclient);
712
713 return 0;
714 }
715
716 /* Initialize zebra client. Argument redist_default is unwanted
717 redistribute route type. */
718 void zclient_init(struct zclient *zclient, int redist_default,
719 unsigned short instance, struct zebra_privs_t *privs)
720 {
721 int afi, i;
722
723 /* Set -1 to the default socket value. */
724 zclient->sock = -1;
725 zclient->privs = privs;
726
727 /* Clear redistribution flags. */
728 for (afi = AFI_IP; afi < AFI_MAX; afi++)
729 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
730 zclient->redist[afi][i] = vrf_bitmap_init();
731
732 /* Set unwanted redistribute route. bgpd does not need BGP route
733 redistribution. */
734 zclient->redist_default = redist_default;
735 zclient->instance = instance;
736 /* Pending: make afi(s) an arg. */
737 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
738 redist_add_instance(&zclient->mi_redist[afi][redist_default],
739 instance);
740
741 /* Set default-information redistribute to zero. */
742 zclient->default_information[afi] = vrf_bitmap_init();
743 }
744
745 if (zclient_debug)
746 zlog_debug("scheduling zclient connection");
747
748 zclient_event(ZCLIENT_SCHEDULE, zclient);
749 }
750
751 /* This function is a wrapper function for calling zclient_start from
752 timer or event thread. */
753 static int zclient_connect(struct thread *t)
754 {
755 struct zclient *zclient;
756
757 zclient = THREAD_ARG(t);
758 zclient->t_connect = NULL;
759
760 if (zclient_debug)
761 zlog_debug("zclient_connect is called");
762
763 return zclient_start(zclient);
764 }
765
766 enum zclient_send_status zclient_send_rnh(struct zclient *zclient, int command,
767 const struct prefix *p,
768 bool connected,
769 bool resolve_via_def, vrf_id_t vrf_id)
770 {
771 struct stream *s;
772
773 s = zclient->obuf;
774 stream_reset(s);
775 zclient_create_header(s, command, vrf_id);
776 stream_putc(s, (connected) ? 1 : 0);
777 stream_putc(s, (resolve_via_def) ? 1 : 0);
778 stream_putw(s, SAFI_UNICAST);
779 stream_putw(s, PREFIX_FAMILY(p));
780 stream_putc(s, p->prefixlen);
781 switch (PREFIX_FAMILY(p)) {
782 case AF_INET:
783 stream_put_in_addr(s, &p->u.prefix4);
784 break;
785 case AF_INET6:
786 stream_put(s, &(p->u.prefix6), 16);
787 break;
788 default:
789 break;
790 }
791 stream_putw_at(s, 0, stream_get_endp(s));
792
793 return zclient_send_message(zclient);
794 }
795
796 /*
797 * "xdr_encode"-like interface that allows daemon (client) to send
798 * a message to zebra server for a route that needs to be
799 * added/deleted to the kernel. Info about the route is specified
800 * by the caller in a struct zapi_route. zapi_route_encode() then writes
801 * the info down the zclient socket using the stream_* functions.
802 *
803 * The corresponding read ("xdr_decode") function on the server
804 * side is zapi_route_decode().
805 *
806 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
807 * byte value.
808 *
809 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as a 4
810 * byte value.
811 *
812 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
813 *
814 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
815 *
816 * XXX: No attention paid to alignment.
817 */
818 enum zclient_send_status
819 zclient_route_send(uint8_t cmd, struct zclient *zclient, struct zapi_route *api)
820 {
821 if (zapi_route_encode(cmd, zclient->obuf, api) < 0)
822 return ZCLIENT_SEND_FAILURE;
823 return zclient_send_message(zclient);
824 }
825
826 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop *next1,
827 const struct zapi_nexthop *next2)
828 {
829 if (next1->label_num > next2->label_num)
830 return 1;
831
832 if (next1->label_num < next2->label_num)
833 return -1;
834
835 return memcmp(next1->labels, next2->labels, next1->label_num);
836 }
837
838 static int zapi_nexthop_srv6_cmp(const struct zapi_nexthop *next1,
839 const struct zapi_nexthop *next2)
840 {
841 int ret = 0;
842
843 ret = memcmp(&next1->seg6_segs, &next2->seg6_segs,
844 sizeof(struct in6_addr));
845 if (ret != 0)
846 return ret;
847
848 if (next1->seg6local_action > next2->seg6local_action)
849 return 1;
850
851 if (next1->seg6local_action < next2->seg6local_action)
852 return -1;
853
854 return memcmp(&next1->seg6local_ctx, &next2->seg6local_ctx,
855 sizeof(struct seg6local_context));
856 }
857
858 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop *next1,
859 const struct zapi_nexthop *next2)
860 {
861 int ret = 0;
862
863 if (next1->vrf_id < next2->vrf_id)
864 return -1;
865
866 if (next1->vrf_id > next2->vrf_id)
867 return 1;
868
869 if (next1->type < next2->type)
870 return -1;
871
872 if (next1->type > next2->type)
873 return 1;
874
875 if (next1->weight < next2->weight)
876 return -1;
877
878 if (next1->weight > next2->weight)
879 return 1;
880
881 switch (next1->type) {
882 case NEXTHOP_TYPE_IPV4:
883 case NEXTHOP_TYPE_IPV6:
884 ret = nexthop_g_addr_cmp(next1->type, &next1->gate,
885 &next2->gate);
886 if (ret != 0)
887 return ret;
888 break;
889 case NEXTHOP_TYPE_IPV4_IFINDEX:
890 case NEXTHOP_TYPE_IPV6_IFINDEX:
891 ret = nexthop_g_addr_cmp(next1->type, &next1->gate,
892 &next2->gate);
893 if (ret != 0)
894 return ret;
895 /* Intentional Fall-Through */
896 case NEXTHOP_TYPE_IFINDEX:
897 if (next1->ifindex < next2->ifindex)
898 return -1;
899
900 if (next1->ifindex > next2->ifindex)
901 return 1;
902 break;
903 case NEXTHOP_TYPE_BLACKHOLE:
904 if (next1->bh_type < next2->bh_type)
905 return -1;
906
907 if (next1->bh_type > next2->bh_type)
908 return 1;
909 break;
910 }
911
912 if (next1->srte_color < next2->srte_color)
913 return -1;
914 if (next1->srte_color > next2->srte_color)
915 return 1;
916
917 if (CHECK_FLAG(next1->flags, NEXTHOP_FLAG_HAS_BACKUP) ||
918 CHECK_FLAG(next2->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
919
920 if (!CHECK_FLAG(next1->flags, NEXTHOP_FLAG_HAS_BACKUP) &&
921 CHECK_FLAG(next2->flags, NEXTHOP_FLAG_HAS_BACKUP))
922 return -1;
923
924 if (CHECK_FLAG(next1->flags, NEXTHOP_FLAG_HAS_BACKUP) &&
925 !CHECK_FLAG(next2->flags, NEXTHOP_FLAG_HAS_BACKUP))
926 return 1;
927
928 if (next1->backup_num > 0 || next2->backup_num > 0) {
929
930 if (next1->backup_num < next2->backup_num)
931 return -1;
932
933 if (next1->backup_num > next2->backup_num)
934 return 1;
935
936 ret = memcmp(next1->backup_idx,
937 next2->backup_idx, next1->backup_num);
938 if (ret != 0)
939 return ret;
940 }
941 }
942
943 return 0;
944 }
945
946 static int zapi_nexthop_cmp(const void *item1, const void *item2)
947 {
948 int ret = 0;
949
950 const struct zapi_nexthop *next1 = item1;
951 const struct zapi_nexthop *next2 = item2;
952
953 ret = zapi_nexthop_cmp_no_labels(next1, next2);
954 if (ret != 0)
955 return ret;
956
957 ret = zapi_nexthop_labels_cmp(next1, next2);
958 if (ret != 0)
959 return ret;
960
961 ret = zapi_nexthop_srv6_cmp(next1, next2);
962
963 return ret;
964 }
965
966 static void zapi_nexthop_group_sort(struct zapi_nexthop *nh_grp,
967 uint16_t nexthop_num)
968 {
969 qsort(nh_grp, nexthop_num, sizeof(struct zapi_nexthop),
970 &zapi_nexthop_cmp);
971 }
972
973 /*
974 * Encode a single zapi nexthop
975 */
976 int zapi_nexthop_encode(struct stream *s, const struct zapi_nexthop *api_nh,
977 uint32_t api_flags, uint32_t api_message)
978 {
979 int i, ret = 0;
980 int nh_flags = api_nh->flags;
981
982 stream_putl(s, api_nh->vrf_id);
983 stream_putc(s, api_nh->type);
984
985 /* If needed, set 'labelled nexthop' flag */
986 if (api_nh->label_num > 0) {
987 SET_FLAG(nh_flags, ZAPI_NEXTHOP_FLAG_LABEL);
988
989 /* Validate label count */
990 if (api_nh->label_num > MPLS_MAX_LABELS) {
991 ret = -1;
992 goto done;
993 }
994 }
995
996 /* If present, set 'weight' flag before encoding flags */
997 if (api_nh->weight)
998 SET_FLAG(nh_flags, ZAPI_NEXTHOP_FLAG_WEIGHT);
999
1000 /* Note that we're only encoding a single octet */
1001 stream_putc(s, nh_flags);
1002
1003 switch (api_nh->type) {
1004 case NEXTHOP_TYPE_BLACKHOLE:
1005 stream_putc(s, api_nh->bh_type);
1006 break;
1007 case NEXTHOP_TYPE_IPV4:
1008 case NEXTHOP_TYPE_IPV4_IFINDEX:
1009 stream_put_in_addr(s, &api_nh->gate.ipv4);
1010 stream_putl(s, api_nh->ifindex);
1011 break;
1012 case NEXTHOP_TYPE_IFINDEX:
1013 stream_putl(s, api_nh->ifindex);
1014 break;
1015 case NEXTHOP_TYPE_IPV6:
1016 case NEXTHOP_TYPE_IPV6_IFINDEX:
1017 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
1018 16);
1019 stream_putl(s, api_nh->ifindex);
1020 break;
1021 }
1022
1023 /* We only encode labels if we have >0 - we use
1024 * the per-nexthop flag above to signal that the count
1025 * is present in the payload.
1026 */
1027 if (api_nh->label_num > 0) {
1028 stream_putc(s, api_nh->label_num);
1029 stream_put(s, &api_nh->labels[0],
1030 api_nh->label_num * sizeof(mpls_label_t));
1031 }
1032
1033 if (api_nh->weight)
1034 stream_putl(s, api_nh->weight);
1035
1036 /* Router MAC for EVPN routes. */
1037 if (CHECK_FLAG(api_flags, ZEBRA_FLAG_EVPN_ROUTE))
1038 stream_put(s, &(api_nh->rmac),
1039 sizeof(struct ethaddr));
1040
1041 /* Color for Segment Routing TE. */
1042 if (CHECK_FLAG(api_message, ZAPI_MESSAGE_SRTE))
1043 stream_putl(s, api_nh->srte_color);
1044
1045 /* Index of backup nexthop */
1046 if (CHECK_FLAG(nh_flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
1047 /* Validate backup count */
1048 if (api_nh->backup_num > NEXTHOP_MAX_BACKUPS) {
1049 ret = -1;
1050 goto done;
1051 }
1052
1053 stream_putc(s, api_nh->backup_num);
1054 for (i = 0; i < api_nh->backup_num; i++)
1055 stream_putc(s, api_nh->backup_idx[i]);
1056 }
1057
1058 if (CHECK_FLAG(nh_flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL)) {
1059 stream_putl(s, api_nh->seg6local_action);
1060 stream_write(s, &api_nh->seg6local_ctx,
1061 sizeof(struct seg6local_context));
1062 }
1063
1064 if (CHECK_FLAG(nh_flags, ZAPI_NEXTHOP_FLAG_SEG6))
1065 stream_write(s, &api_nh->seg6_segs,
1066 sizeof(struct in6_addr));
1067
1068 done:
1069 return ret;
1070 }
1071
1072 int zapi_srv6_locator_chunk_encode(struct stream *s,
1073 const struct srv6_locator_chunk *c)
1074 {
1075 stream_putw(s, strlen(c->locator_name));
1076 stream_put(s, c->locator_name, strlen(c->locator_name));
1077 stream_putw(s, c->prefix.prefixlen);
1078 stream_put(s, &c->prefix.prefix, sizeof(c->prefix.prefix));
1079 stream_putc(s, c->block_bits_length);
1080 stream_putc(s, c->node_bits_length);
1081 stream_putc(s, c->function_bits_length);
1082 stream_putc(s, c->argument_bits_length);
1083 return 0;
1084 }
1085
1086 int zapi_srv6_locator_chunk_decode(struct stream *s,
1087 struct srv6_locator_chunk *c)
1088 {
1089 uint16_t len = 0;
1090
1091 c->prefix.family = AF_INET6;
1092
1093 STREAM_GETW(s, len);
1094 if (len > SRV6_LOCNAME_SIZE)
1095 goto stream_failure;
1096
1097 STREAM_GET(c->locator_name, s, len);
1098 STREAM_GETW(s, c->prefix.prefixlen);
1099 STREAM_GET(&c->prefix.prefix, s, sizeof(c->prefix.prefix));
1100 STREAM_GETC(s, c->block_bits_length);
1101 STREAM_GETC(s, c->node_bits_length);
1102 STREAM_GETC(s, c->function_bits_length);
1103 STREAM_GETC(s, c->argument_bits_length);
1104 return 0;
1105
1106 stream_failure:
1107 return -1;
1108 }
1109
1110 int zapi_srv6_locator_encode(struct stream *s, const struct srv6_locator *l)
1111 {
1112 stream_putw(s, strlen(l->name));
1113 stream_put(s, l->name, strlen(l->name));
1114 stream_putw(s, l->prefix.prefixlen);
1115 stream_put(s, &l->prefix.prefix, sizeof(l->prefix.prefix));
1116 return 0;
1117 }
1118
1119 int zapi_srv6_locator_decode(struct stream *s, struct srv6_locator *l)
1120 {
1121 uint16_t len = 0;
1122
1123 STREAM_GETW(s, len);
1124 if (len > SRV6_LOCNAME_SIZE)
1125 goto stream_failure;
1126
1127 STREAM_GET(l->name, s, len);
1128 STREAM_GETW(s, l->prefix.prefixlen);
1129 STREAM_GET(&l->prefix.prefix, s, sizeof(l->prefix.prefix));
1130 l->prefix.family = AF_INET6;
1131 return 0;
1132
1133 stream_failure:
1134 return -1;
1135 }
1136
1137 static int zapi_nhg_encode(struct stream *s, int cmd, struct zapi_nhg *api_nhg)
1138 {
1139 int i;
1140
1141 if (cmd != ZEBRA_NHG_DEL && cmd != ZEBRA_NHG_ADD) {
1142 flog_err(EC_LIB_ZAPI_ENCODE,
1143 "%s: Specified zapi NHG command (%d) doesn't exist",
1144 __func__, cmd);
1145 return -1;
1146 }
1147
1148 if (api_nhg->nexthop_num >= MULTIPATH_NUM ||
1149 api_nhg->backup_nexthop_num >= MULTIPATH_NUM) {
1150 flog_err(EC_LIB_ZAPI_ENCODE,
1151 "%s: zapi NHG encode with invalid input", __func__);
1152 return -1;
1153 }
1154
1155 stream_reset(s);
1156 zclient_create_header(s, cmd, VRF_DEFAULT);
1157
1158 stream_putw(s, api_nhg->proto);
1159 stream_putl(s, api_nhg->id);
1160
1161 if (cmd == ZEBRA_NHG_ADD) {
1162 /* Nexthops */
1163 zapi_nexthop_group_sort(api_nhg->nexthops,
1164 api_nhg->nexthop_num);
1165
1166 stream_putw(s, api_nhg->nexthop_num);
1167
1168 for (i = 0; i < api_nhg->nexthop_num; i++)
1169 zapi_nexthop_encode(s, &api_nhg->nexthops[i], 0, 0);
1170
1171 /* Backup nexthops */
1172 stream_putw(s, api_nhg->backup_nexthop_num);
1173
1174 for (i = 0; i < api_nhg->backup_nexthop_num; i++)
1175 zapi_nexthop_encode(s, &api_nhg->backup_nexthops[i], 0,
1176 0);
1177 }
1178
1179 stream_putw_at(s, 0, stream_get_endp(s));
1180
1181 return 0;
1182 }
1183
1184 enum zclient_send_status zclient_nhg_send(struct zclient *zclient, int cmd,
1185 struct zapi_nhg *api_nhg)
1186 {
1187 api_nhg->proto = zclient->redist_default;
1188
1189 if (zapi_nhg_encode(zclient->obuf, cmd, api_nhg))
1190 return -1;
1191
1192 return zclient_send_message(zclient);
1193 }
1194
1195 int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api)
1196 {
1197 struct zapi_nexthop *api_nh;
1198 int i;
1199 int psize;
1200
1201 stream_reset(s);
1202 zclient_create_header(s, cmd, api->vrf_id);
1203
1204 if (api->type >= ZEBRA_ROUTE_MAX) {
1205 flog_err(EC_LIB_ZAPI_ENCODE,
1206 "%s: Specified route type (%u) is not a legal value",
1207 __func__, api->type);
1208 return -1;
1209 }
1210 stream_putc(s, api->type);
1211
1212 stream_putw(s, api->instance);
1213 stream_putl(s, api->flags);
1214 stream_putl(s, api->message);
1215
1216 if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) {
1217 flog_err(EC_LIB_ZAPI_ENCODE,
1218 "%s: Specified route SAFI (%u) is not a legal value",
1219 __func__, api->safi);
1220 return -1;
1221 }
1222 stream_putc(s, api->safi);
1223
1224 /* Put prefix information. */
1225 stream_putc(s, api->prefix.family);
1226 psize = PSIZE(api->prefix.prefixlen);
1227 stream_putc(s, api->prefix.prefixlen);
1228 stream_write(s, &api->prefix.u.prefix, psize);
1229
1230 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
1231 psize = PSIZE(api->src_prefix.prefixlen);
1232 stream_putc(s, api->src_prefix.prefixlen);
1233 stream_write(s, (uint8_t *)&api->src_prefix.prefix, psize);
1234 }
1235
1236 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NHG))
1237 stream_putl(s, api->nhgid);
1238
1239 /* Nexthops. */
1240 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
1241 /* limit the number of nexthops if necessary */
1242 if (api->nexthop_num > MULTIPATH_NUM) {
1243 flog_err(
1244 EC_LIB_ZAPI_ENCODE,
1245 "%s: prefix %pFX: can't encode %u nexthops (maximum is %u)",
1246 __func__, &api->prefix, api->nexthop_num,
1247 MULTIPATH_NUM);
1248 return -1;
1249 }
1250
1251 /* We canonicalize the nexthops by sorting them; this allows
1252 * zebra to resolve the list of nexthops to a nexthop-group
1253 * more efficiently.
1254 */
1255 zapi_nexthop_group_sort(api->nexthops, api->nexthop_num);
1256
1257 stream_putw(s, api->nexthop_num);
1258
1259 for (i = 0; i < api->nexthop_num; i++) {
1260 api_nh = &api->nexthops[i];
1261
1262 /* MPLS labels for BGP-LU or Segment Routing */
1263 if (api_nh->label_num > MPLS_MAX_LABELS) {
1264 flog_err(
1265 EC_LIB_ZAPI_ENCODE,
1266 "%s: prefix %pFX: can't encode %u labels (maximum is %u)",
1267 __func__, &api->prefix,
1268 api_nh->label_num, MPLS_MAX_LABELS);
1269 return -1;
1270 }
1271
1272 if (zapi_nexthop_encode(s, api_nh, api->flags,
1273 api->message)
1274 != 0)
1275 return -1;
1276 }
1277 }
1278
1279 /* Backup nexthops */
1280 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_BACKUP_NEXTHOPS)) {
1281 /* limit the number of nexthops if necessary */
1282 if (api->backup_nexthop_num > MULTIPATH_NUM) {
1283 flog_err(
1284 EC_LIB_ZAPI_ENCODE,
1285 "%s: prefix %pFX: can't encode %u backup nexthops (maximum is %u)",
1286 __func__, &api->prefix, api->backup_nexthop_num,
1287 MULTIPATH_NUM);
1288 return -1;
1289 }
1290
1291 /* Note that we do not sort the list of backup nexthops -
1292 * this list is treated as an array and indexed by each
1293 * primary nexthop that is associated with a backup.
1294 */
1295
1296 stream_putw(s, api->backup_nexthop_num);
1297
1298 for (i = 0; i < api->backup_nexthop_num; i++) {
1299 api_nh = &api->backup_nexthops[i];
1300
1301 /* MPLS labels for BGP-LU or Segment Routing */
1302 if (api_nh->label_num > MPLS_MAX_LABELS) {
1303 flog_err(
1304 EC_LIB_ZAPI_ENCODE,
1305 "%s: prefix %pFX: backup: can't encode %u labels (maximum is %u)",
1306 __func__, &api->prefix,
1307 api_nh->label_num, MPLS_MAX_LABELS);
1308 return -1;
1309 }
1310
1311 if (zapi_nexthop_encode(s, api_nh, api->flags,
1312 api->message)
1313 != 0)
1314 return -1;
1315 }
1316 }
1317
1318 /* Attributes. */
1319 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1320 stream_putc(s, api->distance);
1321 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1322 stream_putl(s, api->metric);
1323 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1324 stream_putl(s, api->tag);
1325 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1326 stream_putl(s, api->mtu);
1327 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1328 stream_putl(s, api->tableid);
1329
1330 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_OPAQUE)) {
1331 if (api->opaque.length > ZAPI_MESSAGE_OPAQUE_LENGTH) {
1332 flog_err(
1333 EC_LIB_ZAPI_ENCODE,
1334 "%s: opaque length %u is greater than allowed value",
1335 __func__, api->opaque.length);
1336 return -1;
1337 }
1338
1339 stream_putw(s, api->opaque.length);
1340 stream_write(s, api->opaque.data, api->opaque.length);
1341 }
1342 /* Put length at the first point of the stream. */
1343 stream_putw_at(s, 0, stream_get_endp(s));
1344
1345 return 0;
1346 }
1347
1348 /*
1349 * Decode a single zapi nexthop object
1350 */
1351 int zapi_nexthop_decode(struct stream *s, struct zapi_nexthop *api_nh,
1352 uint32_t api_flags, uint32_t api_message)
1353 {
1354 int i, ret = -1;
1355
1356 STREAM_GETL(s, api_nh->vrf_id);
1357 STREAM_GETC(s, api_nh->type);
1358
1359 /* Note that we're only using a single octet of flags */
1360 STREAM_GETC(s, api_nh->flags);
1361
1362 switch (api_nh->type) {
1363 case NEXTHOP_TYPE_BLACKHOLE:
1364 STREAM_GETC(s, api_nh->bh_type);
1365 break;
1366 case NEXTHOP_TYPE_IPV4:
1367 case NEXTHOP_TYPE_IPV4_IFINDEX:
1368 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
1369 IPV4_MAX_BYTELEN);
1370 STREAM_GETL(s, api_nh->ifindex);
1371 break;
1372 case NEXTHOP_TYPE_IFINDEX:
1373 STREAM_GETL(s, api_nh->ifindex);
1374 break;
1375 case NEXTHOP_TYPE_IPV6:
1376 case NEXTHOP_TYPE_IPV6_IFINDEX:
1377 STREAM_GET(&api_nh->gate.ipv6, s, 16);
1378 STREAM_GETL(s, api_nh->ifindex);
1379 break;
1380 }
1381
1382 /* MPLS labels for BGP-LU or Segment Routing */
1383 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)) {
1384 STREAM_GETC(s, api_nh->label_num);
1385 if (api_nh->label_num > MPLS_MAX_LABELS) {
1386 flog_err(
1387 EC_LIB_ZAPI_ENCODE,
1388 "%s: invalid number of MPLS labels (%u)",
1389 __func__, api_nh->label_num);
1390 return -1;
1391 }
1392
1393 STREAM_GET(&api_nh->labels[0], s,
1394 api_nh->label_num * sizeof(mpls_label_t));
1395 }
1396
1397 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT))
1398 STREAM_GETL(s, api_nh->weight);
1399
1400 /* Router MAC for EVPN routes. */
1401 if (CHECK_FLAG(api_flags, ZEBRA_FLAG_EVPN_ROUTE))
1402 STREAM_GET(&(api_nh->rmac), s,
1403 sizeof(struct ethaddr));
1404
1405 /* Color for Segment Routing TE. */
1406 if (CHECK_FLAG(api_message, ZAPI_MESSAGE_SRTE))
1407 STREAM_GETL(s, api_nh->srte_color);
1408
1409 /* Backup nexthop index */
1410 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
1411 STREAM_GETC(s, api_nh->backup_num);
1412
1413 if (api_nh->backup_num > NEXTHOP_MAX_BACKUPS)
1414 return -1;
1415
1416 for (i = 0; i < api_nh->backup_num; i++)
1417 STREAM_GETC(s, api_nh->backup_idx[i]);
1418 }
1419
1420 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL)) {
1421 STREAM_GETL(s, api_nh->seg6local_action);
1422 STREAM_GET(&api_nh->seg6local_ctx, s,
1423 sizeof(struct seg6local_context));
1424 }
1425
1426 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6))
1427 STREAM_GET(&api_nh->seg6_segs, s,
1428 sizeof(struct in6_addr));
1429
1430 /* Success */
1431 ret = 0;
1432
1433 stream_failure:
1434
1435 return ret;
1436 }
1437
1438 int zapi_route_decode(struct stream *s, struct zapi_route *api)
1439 {
1440 struct zapi_nexthop *api_nh;
1441 int i;
1442
1443 memset(api, 0, sizeof(*api));
1444
1445 /* Type, flags, message. */
1446 STREAM_GETC(s, api->type);
1447 if (api->type >= ZEBRA_ROUTE_MAX) {
1448 flog_err(EC_LIB_ZAPI_ENCODE,
1449 "%s: Specified route type: %d is not a legal value",
1450 __func__, api->type);
1451 return -1;
1452 }
1453
1454 STREAM_GETW(s, api->instance);
1455 STREAM_GETL(s, api->flags);
1456 STREAM_GETL(s, api->message);
1457 STREAM_GETC(s, api->safi);
1458 if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) {
1459 flog_err(EC_LIB_ZAPI_ENCODE,
1460 "%s: Specified route SAFI (%u) is not a legal value",
1461 __func__, api->safi);
1462 return -1;
1463 }
1464
1465 /* Prefix. */
1466 STREAM_GETC(s, api->prefix.family);
1467 STREAM_GETC(s, api->prefix.prefixlen);
1468 switch (api->prefix.family) {
1469 case AF_INET:
1470 if (api->prefix.prefixlen > IPV4_MAX_BITLEN) {
1471 flog_err(
1472 EC_LIB_ZAPI_ENCODE,
1473 "%s: V4 prefixlen is %d which should not be more than 32",
1474 __func__, api->prefix.prefixlen);
1475 return -1;
1476 }
1477 break;
1478 case AF_INET6:
1479 if (api->prefix.prefixlen > IPV6_MAX_BITLEN) {
1480 flog_err(
1481 EC_LIB_ZAPI_ENCODE,
1482 "%s: v6 prefixlen is %d which should not be more than 128",
1483 __func__, api->prefix.prefixlen);
1484 return -1;
1485 }
1486 break;
1487 default:
1488 flog_err(EC_LIB_ZAPI_ENCODE,
1489 "%s: Specified family %d is not v4 or v6", __func__,
1490 api->prefix.family);
1491 return -1;
1492 }
1493 STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen));
1494
1495 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
1496 api->src_prefix.family = AF_INET6;
1497 STREAM_GETC(s, api->src_prefix.prefixlen);
1498 if (api->src_prefix.prefixlen > IPV6_MAX_BITLEN) {
1499 flog_err(
1500 EC_LIB_ZAPI_ENCODE,
1501 "%s: SRC Prefix prefixlen received: %d is too large",
1502 __func__, api->src_prefix.prefixlen);
1503 return -1;
1504 }
1505 STREAM_GET(&api->src_prefix.prefix, s,
1506 PSIZE(api->src_prefix.prefixlen));
1507
1508 if (api->prefix.family != AF_INET6
1509 || api->src_prefix.prefixlen == 0) {
1510 flog_err(
1511 EC_LIB_ZAPI_ENCODE,
1512 "%s: SRC prefix specified in some manner that makes no sense",
1513 __func__);
1514 return -1;
1515 }
1516 }
1517
1518 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NHG))
1519 STREAM_GETL(s, api->nhgid);
1520
1521 /* Nexthops. */
1522 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
1523 STREAM_GETW(s, api->nexthop_num);
1524 if (api->nexthop_num > MULTIPATH_NUM) {
1525 flog_err(EC_LIB_ZAPI_ENCODE,
1526 "%s: invalid number of nexthops (%u)",
1527 __func__, api->nexthop_num);
1528 return -1;
1529 }
1530
1531 for (i = 0; i < api->nexthop_num; i++) {
1532 api_nh = &api->nexthops[i];
1533
1534 if (zapi_nexthop_decode(s, api_nh, api->flags,
1535 api->message)
1536 != 0)
1537 return -1;
1538 }
1539 }
1540
1541 /* Backup nexthops. */
1542 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_BACKUP_NEXTHOPS)) {
1543 STREAM_GETW(s, api->backup_nexthop_num);
1544 if (api->backup_nexthop_num > MULTIPATH_NUM) {
1545 flog_err(EC_LIB_ZAPI_ENCODE,
1546 "%s: invalid number of backup nexthops (%u)",
1547 __func__, api->backup_nexthop_num);
1548 return -1;
1549 }
1550
1551 for (i = 0; i < api->backup_nexthop_num; i++) {
1552 api_nh = &api->backup_nexthops[i];
1553
1554 if (zapi_nexthop_decode(s, api_nh, api->flags,
1555 api->message)
1556 != 0)
1557 return -1;
1558 }
1559 }
1560
1561 /* Attributes. */
1562 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1563 STREAM_GETC(s, api->distance);
1564 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1565 STREAM_GETL(s, api->metric);
1566 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1567 STREAM_GETL(s, api->tag);
1568 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1569 STREAM_GETL(s, api->mtu);
1570 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1571 STREAM_GETL(s, api->tableid);
1572
1573 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_OPAQUE)) {
1574 STREAM_GETW(s, api->opaque.length);
1575 if (api->opaque.length > ZAPI_MESSAGE_OPAQUE_LENGTH) {
1576 flog_err(
1577 EC_LIB_ZAPI_ENCODE,
1578 "%s: opaque length %u is greater than allowed value",
1579 __func__, api->opaque.length);
1580 return -1;
1581 }
1582
1583 STREAM_GET(api->opaque.data, s, api->opaque.length);
1584 }
1585
1586 return 0;
1587 stream_failure:
1588 return -1;
1589 }
1590
1591 static void zapi_encode_prefix(struct stream *s, struct prefix *p,
1592 uint8_t family)
1593 {
1594 struct prefix any;
1595
1596 if (!p) {
1597 memset(&any, 0, sizeof(any));
1598 any.family = family;
1599 p = &any;
1600 }
1601
1602 stream_putc(s, p->family);
1603 stream_putc(s, p->prefixlen);
1604 stream_put(s, &p->u.prefix, prefix_blen(p));
1605 }
1606
1607 int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s, struct pbr_rule *zrule)
1608 {
1609 stream_reset(s);
1610 zclient_create_header(s, cmd, zrule->vrf_id);
1611
1612 /*
1613 * We are sending one item at a time at the moment
1614 */
1615 stream_putl(s, 1);
1616
1617 stream_putl(s, zrule->seq);
1618 stream_putl(s, zrule->priority);
1619 stream_putl(s, zrule->unique);
1620
1621 zapi_encode_prefix(s, &(zrule->filter.src_ip),
1622 zrule->filter.src_ip.family);
1623 stream_putw(s, zrule->filter.src_port); /* src port */
1624 zapi_encode_prefix(s, &(zrule->filter.dst_ip),
1625 zrule->filter.src_ip.family);
1626 stream_putw(s, zrule->filter.dst_port); /* dst port */
1627 stream_putw(s, zrule->filter.fwmark); /* fwmark */
1628
1629 stream_putl(s, zrule->action.table);
1630 stream_put(s, zrule->ifname, INTERFACE_NAMSIZ);
1631
1632 /* Put length at the first point of the stream. */
1633 stream_putw_at(s, 0, stream_get_endp(s));
1634
1635 return 0;
1636 }
1637
1638 bool zapi_nhg_notify_decode(struct stream *s, uint32_t *id,
1639 enum zapi_nhg_notify_owner *note)
1640 {
1641 uint32_t read_id;
1642
1643 STREAM_GET(note, s, sizeof(*note));
1644 STREAM_GETL(s, read_id);
1645
1646 *id = read_id;
1647
1648 return true;
1649
1650 stream_failure:
1651 return false;
1652 }
1653
1654 bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
1655 uint32_t *tableid,
1656 enum zapi_route_notify_owner *note,
1657 afi_t *afi, safi_t *safi)
1658 {
1659 uint32_t t;
1660 afi_t afi_val;
1661 safi_t safi_val;
1662
1663 STREAM_GET(note, s, sizeof(*note));
1664
1665 STREAM_GETC(s, p->family);
1666 STREAM_GETC(s, p->prefixlen);
1667 STREAM_GET(&p->u.prefix, s, prefix_blen(p));
1668 STREAM_GETL(s, t);
1669 STREAM_GETC(s, afi_val);
1670 STREAM_GETC(s, safi_val);
1671
1672 *tableid = t;
1673
1674 if (afi)
1675 *afi = afi_val;
1676 if (safi)
1677 *safi = safi_val;
1678
1679 return true;
1680
1681 stream_failure:
1682 return false;
1683 }
1684
1685 bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
1686 uint32_t *priority, uint32_t *unique, char *ifname,
1687 enum zapi_rule_notify_owner *note)
1688 {
1689 uint32_t prio, seq, uni;
1690
1691 STREAM_GET(note, s, sizeof(*note));
1692
1693 STREAM_GETL(s, seq);
1694 STREAM_GETL(s, prio);
1695 STREAM_GETL(s, uni);
1696 STREAM_GET(ifname, s, INTERFACE_NAMSIZ);
1697
1698 if (zclient_debug)
1699 zlog_debug("%s: %u %u %u %s", __func__, seq, prio, uni, ifname);
1700 *seqno = seq;
1701 *priority = prio;
1702 *unique = uni;
1703
1704 return true;
1705
1706 stream_failure:
1707 return false;
1708 }
1709
1710 bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique,
1711 enum zapi_ipset_notify_owner *note)
1712 {
1713 uint32_t uni;
1714 uint16_t notew;
1715
1716 STREAM_GETW(s, notew);
1717
1718 STREAM_GETL(s, uni);
1719
1720 if (zclient_debug)
1721 zlog_debug("%s: %u", __func__, uni);
1722 *unique = uni;
1723 *note = (enum zapi_ipset_notify_owner)notew;
1724 return true;
1725
1726 stream_failure:
1727 return false;
1728 }
1729
1730 bool zapi_ipset_entry_notify_decode(struct stream *s, uint32_t *unique,
1731 char *ipset_name,
1732 enum zapi_ipset_entry_notify_owner *note)
1733 {
1734 uint32_t uni;
1735 uint16_t notew;
1736
1737 STREAM_GETW(s, notew);
1738
1739 STREAM_GETL(s, uni);
1740
1741 STREAM_GET(ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
1742
1743 if (zclient_debug)
1744 zlog_debug("%s: %u", __func__, uni);
1745 *unique = uni;
1746 *note = (enum zapi_ipset_entry_notify_owner)notew;
1747
1748 return true;
1749
1750 stream_failure:
1751 return false;
1752 }
1753
1754 bool zapi_iptable_notify_decode(struct stream *s,
1755 uint32_t *unique,
1756 enum zapi_iptable_notify_owner *note)
1757 {
1758 uint32_t uni;
1759 uint16_t notew;
1760
1761 STREAM_GETW(s, notew);
1762
1763 STREAM_GETL(s, uni);
1764
1765 if (zclient_debug)
1766 zlog_debug("%s: %u", __func__, uni);
1767 *unique = uni;
1768 *note = (enum zapi_iptable_notify_owner)notew;
1769
1770 return true;
1771
1772 stream_failure:
1773 return false;
1774 }
1775
1776 struct nexthop *nexthop_from_zapi_nexthop(const struct zapi_nexthop *znh)
1777 {
1778 struct nexthop *n = nexthop_new();
1779
1780 n->type = znh->type;
1781 n->vrf_id = znh->vrf_id;
1782 n->ifindex = znh->ifindex;
1783 n->gate = znh->gate;
1784 n->srte_color = znh->srte_color;
1785
1786 /*
1787 * This function currently handles labels
1788 */
1789 if (znh->label_num) {
1790 nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num,
1791 znh->labels);
1792 }
1793
1794 if (CHECK_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
1795 SET_FLAG(n->flags, NEXTHOP_FLAG_HAS_BACKUP);
1796 n->backup_num = znh->backup_num;
1797 memcpy(n->backup_idx, znh->backup_idx, n->backup_num);
1798 }
1799
1800 if (znh->seg6local_action != ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
1801 nexthop_add_srv6_seg6local(n, znh->seg6local_action,
1802 &znh->seg6local_ctx);
1803
1804 if (!sid_zero(&znh->seg6_segs))
1805 nexthop_add_srv6_seg6(n, &znh->seg6_segs);
1806
1807 return n;
1808 }
1809
1810 /*
1811 * Convert nexthop to zapi nexthop
1812 */
1813 int zapi_nexthop_from_nexthop(struct zapi_nexthop *znh,
1814 const struct nexthop *nh)
1815 {
1816 int i;
1817
1818 memset(znh, 0, sizeof(*znh));
1819
1820 znh->type = nh->type;
1821 znh->vrf_id = nh->vrf_id;
1822 znh->weight = nh->weight;
1823 znh->ifindex = nh->ifindex;
1824 znh->gate = nh->gate;
1825
1826 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK))
1827 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1828
1829 if (nh->nh_label && (nh->nh_label->num_labels > 0)) {
1830
1831 /* Validate */
1832 if (nh->nh_label->num_labels > MPLS_MAX_LABELS)
1833 return -1;
1834
1835 for (i = 0; i < nh->nh_label->num_labels; i++)
1836 znh->labels[i] = nh->nh_label->label[i];
1837
1838 znh->label_num = i;
1839 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
1840 }
1841
1842 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
1843 if (nh->backup_num > NEXTHOP_MAX_BACKUPS)
1844 return -1;
1845
1846 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
1847 znh->backup_num = nh->backup_num;
1848 memcpy(znh->backup_idx, nh->backup_idx, znh->backup_num);
1849 }
1850
1851 if (nh->nh_srv6) {
1852 if (nh->nh_srv6->seg6local_action !=
1853 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC) {
1854 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL);
1855 znh->seg6local_action = nh->nh_srv6->seg6local_action;
1856 memcpy(&znh->seg6local_ctx,
1857 &nh->nh_srv6->seg6local_ctx,
1858 sizeof(struct seg6local_context));
1859 }
1860
1861 if (!sid_zero(&nh->nh_srv6->seg6_segs)) {
1862 SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
1863 memcpy(&znh->seg6_segs, &nh->nh_srv6->seg6_segs,
1864 sizeof(struct in6_addr));
1865 }
1866 }
1867
1868 return 0;
1869 }
1870
1871 /*
1872 * Wrapper that converts backup nexthop
1873 */
1874 int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop *znh,
1875 const struct nexthop *nh)
1876 {
1877 int ret;
1878
1879 /* Ensure that zapi flags are correct: backups don't have backups */
1880 ret = zapi_nexthop_from_nexthop(znh, nh);
1881 if (ret == 0)
1882 UNSET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
1883
1884 return ret;
1885 }
1886
1887 /*
1888 * Format some info about a zapi nexthop, for debug or logging.
1889 */
1890 const char *zapi_nexthop2str(const struct zapi_nexthop *znh, char *buf,
1891 int bufsize)
1892 {
1893 char tmp[INET6_ADDRSTRLEN];
1894
1895 switch (znh->type) {
1896 case NEXTHOP_TYPE_IFINDEX:
1897 snprintf(buf, bufsize, "if %u", znh->ifindex);
1898 break;
1899 case NEXTHOP_TYPE_IPV4:
1900 case NEXTHOP_TYPE_IPV4_IFINDEX:
1901 inet_ntop(AF_INET, &znh->gate.ipv4, tmp, sizeof(tmp));
1902 snprintf(buf, bufsize, "%s if %u", tmp, znh->ifindex);
1903 break;
1904 case NEXTHOP_TYPE_IPV6:
1905 case NEXTHOP_TYPE_IPV6_IFINDEX:
1906 inet_ntop(AF_INET6, &znh->gate.ipv6, tmp, sizeof(tmp));
1907 snprintf(buf, bufsize, "%s if %u", tmp, znh->ifindex);
1908 break;
1909 case NEXTHOP_TYPE_BLACKHOLE:
1910 snprintf(buf, bufsize, "blackhole");
1911 break;
1912 default:
1913 snprintf(buf, bufsize, "unknown");
1914 break;
1915 }
1916
1917 return buf;
1918 }
1919
1920 /*
1921 * Decode the nexthop-tracking update message
1922 */
1923 bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr)
1924 {
1925 uint32_t i;
1926
1927 memset(nhr, 0, sizeof(*nhr));
1928
1929 STREAM_GETL(s, nhr->message);
1930 STREAM_GETW(s, nhr->safi);
1931 STREAM_GETW(s, nhr->prefix.family);
1932 STREAM_GETC(s, nhr->prefix.prefixlen);
1933 switch (nhr->prefix.family) {
1934 case AF_INET:
1935 STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1936 break;
1937 case AF_INET6:
1938 STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN);
1939 break;
1940 default:
1941 break;
1942 }
1943 if (CHECK_FLAG(nhr->message, ZAPI_MESSAGE_SRTE))
1944 STREAM_GETL(s, nhr->srte_color);
1945
1946 STREAM_GETC(s, nhr->type);
1947 STREAM_GETW(s, nhr->instance);
1948 STREAM_GETC(s, nhr->distance);
1949 STREAM_GETL(s, nhr->metric);
1950 STREAM_GETC(s, nhr->nexthop_num);
1951
1952 for (i = 0; i < nhr->nexthop_num; i++) {
1953 if (zapi_nexthop_decode(s, &(nhr->nexthops[i]), 0, 0) != 0)
1954 return false;
1955 }
1956
1957 return true;
1958 stream_failure:
1959 return false;
1960 }
1961
1962 bool zapi_error_decode(struct stream *s, enum zebra_error_types *error)
1963 {
1964 memset(error, 0, sizeof(*error));
1965
1966 STREAM_GET(error, s, sizeof(*error));
1967
1968 if (zclient_debug)
1969 zlog_debug("%s: type: %s", __func__,
1970 zebra_error_type2str(*error));
1971
1972 return true;
1973 stream_failure:
1974 return false;
1975 }
1976
1977 /*
1978 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1979 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1980 * then set/unset redist[type] in the client handle (a struct zserv) for the
1981 * sending client
1982 */
1983 enum zclient_send_status
1984 zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1985 int type, unsigned short instance, vrf_id_t vrf_id)
1986 {
1987 struct stream *s;
1988
1989 s = zclient->obuf;
1990 stream_reset(s);
1991
1992 zclient_create_header(s, command, vrf_id);
1993 stream_putc(s, afi);
1994 stream_putc(s, type);
1995 stream_putw(s, instance);
1996
1997 stream_putw_at(s, 0, stream_get_endp(s));
1998
1999 return zclient_send_message(zclient);
2000 }
2001
2002 enum zclient_send_status
2003 zebra_redistribute_default_send(int command, struct zclient *zclient, afi_t afi,
2004 vrf_id_t vrf_id)
2005 {
2006 struct stream *s;
2007
2008 s = zclient->obuf;
2009 stream_reset(s);
2010
2011 zclient_create_header(s, command, vrf_id);
2012 stream_putc(s, afi);
2013
2014 stream_putw_at(s, 0, stream_get_endp(s));
2015
2016 return zclient_send_message(zclient);
2017 }
2018
2019 /* Send route notify request to zebra */
2020 int zebra_route_notify_send(int command, struct zclient *zclient, bool set)
2021 {
2022 struct stream *s;
2023
2024 s = zclient->obuf;
2025 stream_reset(s);
2026
2027 zclient_create_header(s, command, 0);
2028 stream_putc(s, !!set);
2029
2030 stream_putw_at(s, 0, stream_get_endp(s));
2031
2032 return zclient_send_message(zclient);
2033 }
2034
2035 /* Get prefix in ZServ format; family should be filled in on prefix */
2036 static int zclient_stream_get_prefix(struct stream *s, struct prefix *p)
2037 {
2038 size_t plen = prefix_blen(p);
2039 uint8_t c;
2040 p->prefixlen = 0;
2041
2042 if (plen == 0)
2043 return -1;
2044
2045 STREAM_GET(&p->u.prefix, s, plen);
2046 STREAM_GETC(s, c);
2047 p->prefixlen = MIN(plen * 8, c);
2048
2049 return 0;
2050 stream_failure:
2051 return -1;
2052 }
2053
2054 /* Router-id update from zebra daemon. */
2055 int zebra_router_id_update_read(struct stream *s, struct prefix *rid)
2056 {
2057 /* Fetch interface address. */
2058 STREAM_GETC(s, rid->family);
2059
2060 return zclient_stream_get_prefix(s, rid);
2061
2062 stream_failure:
2063 return -1;
2064 }
2065
2066 /* Interface addition from zebra daemon. */
2067 /*
2068 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
2069 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
2070 * 0 1 2 3
2071 * 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
2072 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2073 * | ifname |
2074 * | |
2075 * | |
2076 * | |
2077 * | |
2078 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2079 * | ifindex |
2080 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2081 * | status |
2082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2083 * | if_flags |
2084 * | |
2085 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2086 * | metric |
2087 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2088 * | speed |
2089 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2090 * | ifmtu |
2091 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2092 * | ifmtu6 |
2093 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2094 * | bandwidth |
2095 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2096 * | parent ifindex |
2097 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2098 * | Link Layer Type |
2099 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2100 * | Harware Address Length |
2101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2102 * | Hardware Address if HW lenght different from 0 |
2103 * | ... max INTERFACE_HWADDR_MAX |
2104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2105 * | Link_params? | Whether a link-params follows: 1 or 0.
2106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2107 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
2108 * | .... (struct if_link_params). |
2109 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2110 */
2111
2112 static int zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
2113 {
2114 struct vrf *vrf;
2115 char vrfname_tmp[VRF_NAMSIZ + 1] = {};
2116 struct vrf_data data;
2117
2118 STREAM_GET(&data, zclient->ibuf, sizeof(struct vrf_data));
2119 /* Read interface name. */
2120 STREAM_GET(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
2121
2122 if (strlen(vrfname_tmp) == 0)
2123 goto stream_failure;
2124
2125 /* Lookup/create vrf by name, then vrf_id. */
2126 vrf = vrf_get(vrf_id, vrfname_tmp);
2127
2128 /* If there's already a VRF with this name, don't create vrf */
2129 if (!vrf)
2130 return 0;
2131
2132 vrf->data.l.table_id = data.l.table_id;
2133 memcpy(vrf->data.l.netns_name, data.l.netns_name, NS_NAMSIZ);
2134 /* overwrite default vrf */
2135 if (vrf_id == VRF_DEFAULT)
2136 vrf_set_default_name(vrfname_tmp, false);
2137 vrf_enable(vrf);
2138
2139 return 0;
2140 stream_failure:
2141 return -1;
2142 }
2143
2144 static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
2145 {
2146 struct vrf *vrf;
2147
2148 /* Lookup vrf by vrf_id. */
2149 vrf = vrf_lookup_by_id(vrf_id);
2150
2151 /*
2152 * If a routing protocol doesn't know about a
2153 * vrf that is about to be deleted. There is
2154 * no point in attempting to delete it.
2155 */
2156 if (!vrf)
2157 return;
2158
2159 vrf_delete(vrf);
2160 }
2161
2162 static int zclient_interface_add(struct zclient *zclient, vrf_id_t vrf_id)
2163 {
2164 struct interface *ifp;
2165 char ifname_tmp[INTERFACE_NAMSIZ + 1] = {};
2166 struct stream *s = zclient->ibuf;
2167
2168 /* Read interface name. */
2169 STREAM_GET(ifname_tmp, s, INTERFACE_NAMSIZ);
2170
2171 /* Lookup/create interface by name. */
2172 if (!vrf_get(vrf_id, NULL)) {
2173 zlog_debug(
2174 "Rx'd interface add from Zebra, but VRF %u does not exist",
2175 vrf_id);
2176 return -1;
2177 }
2178
2179 ifp = if_get_by_name(ifname_tmp, vrf_id);
2180
2181 zebra_interface_if_set_value(s, ifp);
2182
2183 if_new_via_zapi(ifp);
2184
2185 return 0;
2186 stream_failure:
2187 return -1;
2188 }
2189
2190 /*
2191 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
2192 * from zebra server. The format of this message is the same as
2193 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
2194 * except that no sockaddr_dl is sent at the tail of the message.
2195 */
2196 struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
2197 {
2198 struct interface *ifp;
2199 char ifname_tmp[INTERFACE_NAMSIZ + 1] = {};
2200
2201 /* Read interface name. */
2202 STREAM_GET(ifname_tmp, s, INTERFACE_NAMSIZ);
2203
2204 /* Lookup this by interface index. */
2205 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
2206 if (ifp == NULL) {
2207 flog_err(EC_LIB_ZAPI_ENCODE,
2208 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
2209 ifname_tmp, vrf_id);
2210 return NULL;
2211 }
2212
2213 zebra_interface_if_set_value(s, ifp);
2214
2215 return ifp;
2216 stream_failure:
2217 return NULL;
2218 }
2219
2220 static void zclient_interface_delete(struct zclient *zclient, vrf_id_t vrf_id)
2221 {
2222 struct interface *ifp;
2223 struct stream *s = zclient->ibuf;
2224
2225 ifp = zebra_interface_state_read(s, vrf_id);
2226
2227 if (ifp == NULL)
2228 return;
2229
2230 if_destroy_via_zapi(ifp);
2231 return;
2232 }
2233
2234 static void zclient_interface_up(struct zclient *zclient, vrf_id_t vrf_id)
2235 {
2236 struct interface *ifp;
2237 struct stream *s = zclient->ibuf;
2238
2239 ifp = zebra_interface_state_read(s, vrf_id);
2240
2241 if (!ifp)
2242 return;
2243
2244 if_up_via_zapi(ifp);
2245 }
2246
2247 static void zclient_interface_down(struct zclient *zclient, vrf_id_t vrf_id)
2248 {
2249 struct interface *ifp;
2250 struct stream *s = zclient->ibuf;
2251
2252 ifp = zebra_interface_state_read(s, vrf_id);
2253
2254 if (!ifp)
2255 return;
2256
2257 if_down_via_zapi(ifp);
2258 }
2259
2260 static void zclient_handle_error(ZAPI_CALLBACK_ARGS)
2261 {
2262 enum zebra_error_types error;
2263 struct stream *s = zclient->ibuf;
2264
2265 zapi_error_decode(s, &error);
2266
2267 if (zclient->handle_error)
2268 (*zclient->handle_error)(error);
2269 }
2270
2271 static int link_params_set_value(struct stream *s, struct if_link_params *iflp)
2272 {
2273
2274 if (iflp == NULL)
2275 return -1;
2276
2277 uint32_t bwclassnum;
2278
2279 STREAM_GETL(s, iflp->lp_status);
2280 STREAM_GETL(s, iflp->te_metric);
2281 STREAM_GETF(s, iflp->max_bw);
2282 STREAM_GETF(s, iflp->max_rsv_bw);
2283 STREAM_GETL(s, bwclassnum);
2284 {
2285 unsigned int i;
2286 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
2287 STREAM_GETF(s, iflp->unrsv_bw[i]);
2288 if (i < bwclassnum)
2289 flog_err(
2290 EC_LIB_ZAPI_MISSMATCH,
2291 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries - outdated library?",
2292 __func__, bwclassnum, MAX_CLASS_TYPE);
2293 }
2294 STREAM_GETL(s, iflp->admin_grp);
2295 STREAM_GETL(s, iflp->rmt_as);
2296 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
2297
2298 STREAM_GETL(s, iflp->av_delay);
2299 STREAM_GETL(s, iflp->min_delay);
2300 STREAM_GETL(s, iflp->max_delay);
2301 STREAM_GETL(s, iflp->delay_var);
2302
2303 STREAM_GETF(s, iflp->pkt_loss);
2304 STREAM_GETF(s, iflp->res_bw);
2305 STREAM_GETF(s, iflp->ava_bw);
2306 STREAM_GETF(s, iflp->use_bw);
2307
2308 return 0;
2309 stream_failure:
2310 return -1;
2311 }
2312
2313 struct interface *zebra_interface_link_params_read(struct stream *s,
2314 vrf_id_t vrf_id,
2315 bool *changed)
2316 {
2317 struct if_link_params *iflp;
2318 struct if_link_params iflp_copy;
2319 ifindex_t ifindex;
2320 bool params_changed = false;
2321
2322 STREAM_GETL(s, ifindex);
2323
2324 struct interface *ifp = if_lookup_by_index(ifindex, vrf_id);
2325
2326 if (ifp == NULL) {
2327 flog_err(EC_LIB_ZAPI_ENCODE,
2328 "%s: unknown ifindex %u, shouldn't happen", __func__,
2329 ifindex);
2330 return NULL;
2331 }
2332
2333 if (ifp->link_params == NULL)
2334 params_changed = true;
2335
2336 if ((iflp = if_link_params_get(ifp)) == NULL)
2337 return NULL;
2338
2339 memcpy(&iflp_copy, iflp, sizeof(iflp_copy));
2340
2341 if (link_params_set_value(s, iflp) != 0)
2342 goto stream_failure;
2343
2344 if (memcmp(&iflp_copy, iflp, sizeof(iflp_copy)))
2345 params_changed = true;
2346
2347 if (changed)
2348 *changed = params_changed;
2349
2350 return ifp;
2351
2352 stream_failure:
2353 return NULL;
2354 }
2355
2356 static void zebra_interface_if_set_value(struct stream *s,
2357 struct interface *ifp)
2358 {
2359 uint8_t link_params_status = 0;
2360 ifindex_t old_ifindex, new_ifindex;
2361
2362 old_ifindex = ifp->oldifindex;
2363 /* Read interface's index. */
2364 STREAM_GETL(s, new_ifindex);
2365 if_set_index(ifp, new_ifindex);
2366 STREAM_GETC(s, ifp->status);
2367
2368 /* Read interface's value. */
2369 STREAM_GETQ(s, ifp->flags);
2370 STREAM_GETC(s, ifp->ptm_enable);
2371 STREAM_GETC(s, ifp->ptm_status);
2372 STREAM_GETL(s, ifp->metric);
2373 STREAM_GETL(s, ifp->speed);
2374 STREAM_GETL(s, ifp->mtu);
2375 STREAM_GETL(s, ifp->mtu6);
2376 STREAM_GETL(s, ifp->bandwidth);
2377 STREAM_GETL(s, ifp->link_ifindex);
2378 STREAM_GETL(s, ifp->ll_type);
2379 STREAM_GETL(s, ifp->hw_addr_len);
2380 if (ifp->hw_addr_len)
2381 STREAM_GET(ifp->hw_addr, s,
2382 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
2383
2384 /* Read Traffic Engineering status */
2385 link_params_status = stream_getc(s);
2386 /* Then, Traffic Engineering parameters if any */
2387 if (link_params_status) {
2388 struct if_link_params *iflp = if_link_params_get(ifp);
2389 link_params_set_value(s, iflp);
2390 }
2391
2392 nexthop_group_interface_state_change(ifp, old_ifindex);
2393
2394 return;
2395 stream_failure:
2396 zlog_err("Could not parse interface values; aborting");
2397 assert(!"Failed to parse interface values");
2398 }
2399
2400 size_t zebra_interface_link_params_write(struct stream *s,
2401 struct interface *ifp)
2402 {
2403 size_t w;
2404 struct if_link_params *iflp;
2405 int i;
2406
2407 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
2408 return 0;
2409
2410 iflp = ifp->link_params;
2411 w = 0;
2412
2413 w += stream_putl(s, iflp->lp_status);
2414
2415 w += stream_putl(s, iflp->te_metric);
2416 w += stream_putf(s, iflp->max_bw);
2417 w += stream_putf(s, iflp->max_rsv_bw);
2418
2419 w += stream_putl(s, MAX_CLASS_TYPE);
2420 for (i = 0; i < MAX_CLASS_TYPE; i++)
2421 w += stream_putf(s, iflp->unrsv_bw[i]);
2422
2423 w += stream_putl(s, iflp->admin_grp);
2424 w += stream_putl(s, iflp->rmt_as);
2425 w += stream_put_in_addr(s, &iflp->rmt_ip);
2426
2427 w += stream_putl(s, iflp->av_delay);
2428 w += stream_putl(s, iflp->min_delay);
2429 w += stream_putl(s, iflp->max_delay);
2430 w += stream_putl(s, iflp->delay_var);
2431
2432 w += stream_putf(s, iflp->pkt_loss);
2433 w += stream_putf(s, iflp->res_bw);
2434 w += stream_putf(s, iflp->ava_bw);
2435 w += stream_putf(s, iflp->use_bw);
2436
2437 return w;
2438 }
2439
2440 /*
2441 * format of message for address additon is:
2442 * 0
2443 * 0 1 2 3 4 5 6 7
2444 * +-+-+-+-+-+-+-+-+
2445 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
2446 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
2447 * | |
2448 * + +
2449 * | ifindex |
2450 * + +
2451 * | |
2452 * + +
2453 * | |
2454 * +-+-+-+-+-+-+-+-+
2455 * | ifc_flags | flags for connected address
2456 * +-+-+-+-+-+-+-+-+
2457 * | addr_family |
2458 * +-+-+-+-+-+-+-+-+
2459 * | addr... |
2460 * : :
2461 * | |
2462 * +-+-+-+-+-+-+-+-+
2463 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
2464 * +-+-+-+-+-+-+-+-+
2465 * | daddr.. |
2466 * : :
2467 * | |
2468 * +-+-+-+-+-+-+-+-+
2469 */
2470
2471 static int memconstant(const void *s, int c, size_t n)
2472 {
2473 const uint8_t *p = s;
2474
2475 while (n-- > 0)
2476 if (*p++ != c)
2477 return 0;
2478 return 1;
2479 }
2480
2481
2482 struct connected *zebra_interface_address_read(int type, struct stream *s,
2483 vrf_id_t vrf_id)
2484 {
2485 ifindex_t ifindex;
2486 struct interface *ifp;
2487 struct connected *ifc;
2488 struct prefix p, d, *dp;
2489 int plen;
2490 uint8_t ifc_flags;
2491
2492 memset(&p, 0, sizeof(p));
2493 memset(&d, 0, sizeof(d));
2494
2495 /* Get interface index. */
2496 STREAM_GETL(s, ifindex);
2497
2498 /* Lookup index. */
2499 ifp = if_lookup_by_index(ifindex, vrf_id);
2500 if (ifp == NULL) {
2501 flog_err(EC_LIB_ZAPI_ENCODE,
2502 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
2503 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
2504 ifindex, vrf_id);
2505 return NULL;
2506 }
2507
2508 /* Fetch flag. */
2509 STREAM_GETC(s, ifc_flags);
2510
2511 /* Fetch interface address. */
2512 STREAM_GETC(s, d.family);
2513 p.family = d.family;
2514 plen = prefix_blen(&d);
2515
2516 if (zclient_stream_get_prefix(s, &p) != 0)
2517 goto stream_failure;
2518
2519 /* Fetch destination address. */
2520 STREAM_GET(&d.u.prefix, s, plen);
2521
2522 /* N.B. NULL destination pointers are encoded as all zeroes */
2523 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
2524
2525 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
2526 ifc = connected_lookup_prefix_exact(ifp, &p);
2527 if (!ifc) {
2528 /* N.B. NULL destination pointers are encoded as all
2529 * zeroes */
2530 ifc = connected_add_by_prefix(ifp, &p, dp);
2531 }
2532 if (ifc) {
2533 ifc->flags = ifc_flags;
2534 if (ifc->destination)
2535 ifc->destination->prefixlen =
2536 ifc->address->prefixlen;
2537 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
2538 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
2539 * "peer" */
2540 flog_err(
2541 EC_LIB_ZAPI_ENCODE,
2542 "interface %s address %pFX with peer flag set, but no peer address!",
2543 ifp->name, ifc->address);
2544 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
2545 }
2546 }
2547 } else {
2548 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
2549 ifc = connected_delete_by_prefix(ifp, &p);
2550 }
2551
2552 return ifc;
2553
2554 stream_failure:
2555 return NULL;
2556 }
2557
2558 /*
2559 * format of message for neighbor connected address is:
2560 * 0
2561 * 0 1 2 3 4 5 6 7
2562 * +-+-+-+-+-+-+-+-+
2563 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
2564 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2565 * | |
2566 * + +
2567 * | ifindex |
2568 * + +
2569 * | |
2570 * + +
2571 * | |
2572 * +-+-+-+-+-+-+-+-+
2573 * | addr_family |
2574 * +-+-+-+-+-+-+-+-+
2575 * | addr... |
2576 * : :
2577 * | |
2578 * +-+-+-+-+-+-+-+-+
2579 * | addr_len | len of addr.
2580 * +-+-+-+-+-+-+-+-+
2581 */
2582 struct nbr_connected *
2583 zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
2584 {
2585 unsigned int ifindex;
2586 struct interface *ifp;
2587 struct prefix p;
2588 struct nbr_connected *ifc;
2589
2590 /* Get interface index. */
2591 STREAM_GETL(s, ifindex);
2592
2593 /* Lookup index. */
2594 ifp = if_lookup_by_index(ifindex, vrf_id);
2595 if (ifp == NULL) {
2596 flog_err(EC_LIB_ZAPI_ENCODE,
2597 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2598 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
2599 : "DELETE",
2600 ifindex, vrf_id);
2601 return NULL;
2602 }
2603
2604 STREAM_GETC(s, p.family);
2605 STREAM_GET(&p.u.prefix, s, prefix_blen(&p));
2606 STREAM_GETC(s, p.prefixlen);
2607
2608 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
2609 /* Currently only supporting P2P links, so any new RA source
2610 address is
2611 considered as the replacement of the previously learnt
2612 Link-Local address. */
2613 if (!(ifc = listnode_head(ifp->nbr_connected))) {
2614 ifc = nbr_connected_new();
2615 ifc->address = prefix_new();
2616 ifc->ifp = ifp;
2617 listnode_add(ifp->nbr_connected, ifc);
2618 }
2619
2620 prefix_copy(ifc->address, &p);
2621 } else {
2622 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
2623
2624 ifc = nbr_connected_check(ifp, &p);
2625 if (ifc)
2626 listnode_delete(ifp->nbr_connected, ifc);
2627 }
2628
2629 return ifc;
2630
2631 stream_failure:
2632 return NULL;
2633 }
2634
2635 struct interface *zebra_interface_vrf_update_read(struct stream *s,
2636 vrf_id_t vrf_id,
2637 vrf_id_t *new_vrf_id)
2638 {
2639 char ifname[INTERFACE_NAMSIZ + 1] = {};
2640 struct interface *ifp;
2641 vrf_id_t new_id;
2642
2643 /* Read interface name. */
2644 STREAM_GET(ifname, s, INTERFACE_NAMSIZ);
2645
2646 /* Lookup interface. */
2647 ifp = if_lookup_by_name(ifname, vrf_id);
2648 if (ifp == NULL) {
2649 flog_err(EC_LIB_ZAPI_ENCODE,
2650 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2651 ifname, vrf_id);
2652 return NULL;
2653 }
2654
2655 /* Fetch new VRF Id. */
2656 STREAM_GETL(s, new_id);
2657
2658 *new_vrf_id = new_id;
2659 return ifp;
2660
2661 stream_failure:
2662 return NULL;
2663 }
2664
2665 /* filter unwanted messages until the expected one arrives */
2666 static int zclient_read_sync_response(struct zclient *zclient,
2667 uint16_t expected_cmd)
2668 {
2669 struct stream *s;
2670 uint16_t size = -1;
2671 uint8_t marker;
2672 uint8_t version;
2673 vrf_id_t vrf_id;
2674 uint16_t cmd;
2675 fd_set readfds;
2676 int ret;
2677
2678 ret = 0;
2679 cmd = expected_cmd + 1;
2680 while (ret == 0 && cmd != expected_cmd) {
2681 s = zclient->ibuf;
2682 stream_reset(s);
2683
2684 /* wait until response arrives */
2685 FD_ZERO(&readfds);
2686 FD_SET(zclient->sock, &readfds);
2687 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
2688 if (!FD_ISSET(zclient->sock, &readfds))
2689 continue;
2690 /* read response */
2691 ret = zclient_read_header(s, zclient->sock, &size, &marker,
2692 &version, &vrf_id, &cmd);
2693 if (zclient_debug)
2694 zlog_debug("%s: Response (%d bytes) received", __func__,
2695 size);
2696 }
2697 if (ret != 0) {
2698 flog_err(EC_LIB_ZAPI_ENCODE, "%s: Invalid Sync Message Reply",
2699 __func__);
2700 return -1;
2701 }
2702
2703 return 0;
2704 }
2705 /**
2706 * Connect to label manager in a syncronous way
2707 *
2708 * It first writes the request to zcient output buffer and then
2709 * immediately reads the answer from the input buffer.
2710 *
2711 * @param zclient Zclient used to connect to label manager (zebra)
2712 * @param async Synchronous (0) or asynchronous (1) operation
2713 * @result Result of response
2714 */
2715 int lm_label_manager_connect(struct zclient *zclient, int async)
2716 {
2717 int ret;
2718 struct stream *s;
2719 uint8_t result;
2720 uint16_t cmd = async ? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC :
2721 ZEBRA_LABEL_MANAGER_CONNECT;
2722
2723 if (zclient_debug)
2724 zlog_debug("Connecting to Label Manager (LM)");
2725
2726 if (zclient->sock < 0) {
2727 zlog_debug("%s: invalid zclient socket", __func__);
2728 return -1;
2729 }
2730
2731 /* send request */
2732 s = zclient->obuf;
2733 stream_reset(s);
2734 zclient_create_header(s, cmd, VRF_DEFAULT);
2735
2736 /* proto */
2737 stream_putc(s, zclient->redist_default);
2738 /* instance */
2739 stream_putw(s, zclient->instance);
2740
2741 /* Put length at the first point of the stream. */
2742 stream_putw_at(s, 0, stream_get_endp(s));
2743
2744 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2745 if (ret < 0) {
2746 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2747 close(zclient->sock);
2748 zclient->sock = -1;
2749 return -1;
2750 }
2751 if (ret == 0) {
2752 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2753 close(zclient->sock);
2754 zclient->sock = -1;
2755 return -1;
2756 }
2757 if (zclient_debug)
2758 zlog_debug("LM connect request sent (%d bytes)", ret);
2759
2760 if (async)
2761 return 0;
2762
2763 /* read response */
2764 if (zclient_read_sync_response(zclient, cmd)
2765 != 0)
2766 return -1;
2767
2768 s = zclient->ibuf;
2769
2770 /* read instance and proto */
2771 uint8_t proto;
2772 uint16_t instance;
2773
2774 STREAM_GETC(s, proto);
2775 STREAM_GETW(s, instance);
2776
2777 /* sanity */
2778 if (proto != zclient->redist_default)
2779 flog_err(
2780 EC_LIB_ZAPI_ENCODE,
2781 "Wrong proto (%u) in LM connect response. Should be %u",
2782 proto, zclient->redist_default);
2783 if (instance != zclient->instance)
2784 flog_err(
2785 EC_LIB_ZAPI_ENCODE,
2786 "Wrong instId (%u) in LM connect response. Should be %u",
2787 instance, zclient->instance);
2788
2789 /* result code */
2790 STREAM_GETC(s, result);
2791 if (zclient_debug)
2792 zlog_debug("LM connect-response received, result %u", result);
2793
2794 return (int)result;
2795
2796 stream_failure:
2797 return -1;
2798 }
2799
2800 /**
2801 * Function to request a srv6-locator chunk in an Asyncronous way
2802 *
2803 * @param zclient Zclient used to connect to table manager (zebra)
2804 * @param locator_name Name of SRv6-locator
2805 * @result 0 on success, -1 otherwise
2806 */
2807 int srv6_manager_get_locator_chunk(struct zclient *zclient,
2808 const char *locator_name)
2809 {
2810 struct stream *s;
2811 const size_t len = strlen(locator_name);
2812
2813 if (zclient_debug)
2814 zlog_debug("Getting SRv6-Locator Chunk %s", locator_name);
2815
2816 if (zclient->sock < 0)
2817 return -1;
2818
2819 /* send request */
2820 s = zclient->obuf;
2821 stream_reset(s);
2822 zclient_create_header(s, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK,
2823 VRF_DEFAULT);
2824
2825 /* locator_name */
2826 stream_putw(s, len);
2827 stream_put(s, locator_name, len);
2828
2829 /* Put length at the first point of the stream. */
2830 stream_putw_at(s, 0, stream_get_endp(s));
2831
2832 return zclient_send_message(zclient);
2833 }
2834
2835 /**
2836 * Function to release a srv6-locator chunk
2837 *
2838 * @param zclient Zclient used to connect to table manager (zebra)
2839 * @param locator_name Name of SRv6-locator
2840 * @result 0 on success, -1 otherwise
2841 */
2842 int srv6_manager_release_locator_chunk(struct zclient *zclient,
2843 const char *locator_name)
2844 {
2845 struct stream *s;
2846 const size_t len = strlen(locator_name);
2847
2848 if (zclient_debug)
2849 zlog_debug("Releasing SRv6-Locator Chunk %s", locator_name);
2850
2851 if (zclient->sock < 0)
2852 return -1;
2853
2854 /* send request */
2855 s = zclient->obuf;
2856 stream_reset(s);
2857 zclient_create_header(s, ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK,
2858 VRF_DEFAULT);
2859
2860 /* locator_name */
2861 stream_putw(s, len);
2862 stream_put(s, locator_name, len);
2863
2864 /* Put length at the first point of the stream. */
2865 stream_putw_at(s, 0, stream_get_endp(s));
2866
2867 return zclient_send_message(zclient);
2868 }
2869
2870 /*
2871 * Asynchronous label chunk request
2872 *
2873 * @param zclient Zclient used to connect to label manager (zebra)
2874 * @param keep Avoid garbage collection
2875 * @param chunk_size Amount of labels requested
2876 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2877 * @result 0 on success, -1 otherwise
2878 */
2879 enum zclient_send_status zclient_send_get_label_chunk(struct zclient *zclient,
2880 uint8_t keep,
2881 uint32_t chunk_size,
2882 uint32_t base)
2883 {
2884 struct stream *s;
2885
2886 if (zclient_debug)
2887 zlog_debug("Getting Label Chunk");
2888
2889 if (zclient->sock < 0)
2890 return ZCLIENT_SEND_FAILURE;
2891
2892 s = zclient->obuf;
2893 stream_reset(s);
2894
2895 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2896 /* proto */
2897 stream_putc(s, zclient->redist_default);
2898 /* instance */
2899 stream_putw(s, zclient->instance);
2900 stream_putc(s, keep);
2901 stream_putl(s, chunk_size);
2902 stream_putl(s, base);
2903
2904 /* Put length at the first point of the stream. */
2905 stream_putw_at(s, 0, stream_get_endp(s));
2906
2907 return zclient_send_message(zclient);
2908 }
2909
2910 /**
2911 * Function to request a label chunk in a syncronous way
2912 *
2913 * It first writes the request to zlcient output buffer and then
2914 * immediately reads the answer from the input buffer.
2915 *
2916 * @param zclient Zclient used to connect to label manager (zebra)
2917 * @param keep Avoid garbage collection
2918 * @param chunk_size Amount of labels requested
2919 * @param start To write first assigned chunk label to
2920 * @param end To write last assigned chunk label to
2921 * @result 0 on success, -1 otherwise
2922 */
2923 int lm_get_label_chunk(struct zclient *zclient, uint8_t keep, uint32_t base,
2924 uint32_t chunk_size, uint32_t *start, uint32_t *end)
2925 {
2926 int ret;
2927 struct stream *s;
2928 uint8_t response_keep;
2929
2930 if (zclient_debug)
2931 zlog_debug("Getting Label Chunk");
2932
2933 if (zclient->sock < 0)
2934 return -1;
2935
2936 /* send request */
2937 s = zclient->obuf;
2938 stream_reset(s);
2939 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2940 /* proto */
2941 stream_putc(s, zclient->redist_default);
2942 /* instance */
2943 stream_putw(s, zclient->instance);
2944 /* keep */
2945 stream_putc(s, keep);
2946 /* chunk size */
2947 stream_putl(s, chunk_size);
2948 /* requested chunk base */
2949 stream_putl(s, base);
2950 /* Put length at the first point of the stream. */
2951 stream_putw_at(s, 0, stream_get_endp(s));
2952
2953 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2954 if (ret < 0) {
2955 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
2956 close(zclient->sock);
2957 zclient->sock = -1;
2958 return -1;
2959 }
2960 if (ret == 0) {
2961 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed");
2962 close(zclient->sock);
2963 zclient->sock = -1;
2964 return -1;
2965 }
2966 if (zclient_debug)
2967 zlog_debug("Label chunk request (%d bytes) sent", ret);
2968
2969 /* read response */
2970 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
2971 return -1;
2972
2973 /* parse response */
2974 s = zclient->ibuf;
2975
2976 /* read proto and instance */
2977 uint8_t proto;
2978 uint8_t instance;
2979
2980 STREAM_GETC(s, proto);
2981 STREAM_GETW(s, instance);
2982
2983 /* sanities */
2984 if (proto != zclient->redist_default)
2985 flog_err(EC_LIB_ZAPI_ENCODE,
2986 "Wrong proto (%u) in get chunk response. Should be %u",
2987 proto, zclient->redist_default);
2988 if (instance != zclient->instance)
2989 flog_err(EC_LIB_ZAPI_ENCODE,
2990 "Wrong instId (%u) in get chunk response Should be %u",
2991 instance, zclient->instance);
2992
2993 /* if we requested a specific chunk and it could not be allocated, the
2994 * response message will end here
2995 */
2996 if (!STREAM_READABLE(s)) {
2997 zlog_info("Unable to assign Label Chunk to %s instance %u",
2998 zebra_route_string(proto), instance);
2999 return -1;
3000 }
3001
3002 /* keep */
3003 STREAM_GETC(s, response_keep);
3004 /* start and end labels */
3005 STREAM_GETL(s, *start);
3006 STREAM_GETL(s, *end);
3007
3008 /* not owning this response */
3009 if (keep != response_keep) {
3010 flog_err(
3011 EC_LIB_ZAPI_ENCODE,
3012 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
3013 *start, *end, keep, response_keep);
3014 }
3015 /* sanity */
3016 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
3017 || *end > MPLS_LABEL_UNRESERVED_MAX) {
3018 flog_err(EC_LIB_ZAPI_ENCODE, "Invalid Label chunk: %u - %u",
3019 *start, *end);
3020 return -1;
3021 }
3022
3023 if (zclient_debug)
3024 zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end,
3025 response_keep);
3026
3027 return 0;
3028
3029 stream_failure:
3030 return -1;
3031 }
3032
3033 /**
3034 * Function to release a label chunk
3035 *
3036 * @param zclient Zclient used to connect to label manager (zebra)
3037 * @param start First label of chunk
3038 * @param end Last label of chunk
3039 * @result 0 on success, -1 otherwise
3040 */
3041 int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
3042 uint32_t end)
3043 {
3044 int ret;
3045 struct stream *s;
3046
3047 if (zclient_debug)
3048 zlog_debug("Releasing Label Chunk %u - %u", start, end);
3049
3050 if (zclient->sock < 0)
3051 return -1;
3052
3053 /* send request */
3054 s = zclient->obuf;
3055 stream_reset(s);
3056 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
3057
3058 /* proto */
3059 stream_putc(s, zclient->redist_default);
3060 /* instance */
3061 stream_putw(s, zclient->instance);
3062 /* start */
3063 stream_putl(s, start);
3064 /* end */
3065 stream_putl(s, end);
3066
3067 /* Put length at the first point of the stream. */
3068 stream_putw_at(s, 0, stream_get_endp(s));
3069
3070 ret = writen(zclient->sock, s->data, stream_get_endp(s));
3071 if (ret < 0) {
3072 flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock");
3073 close(zclient->sock);
3074 zclient->sock = -1;
3075 return -1;
3076 }
3077 if (ret == 0) {
3078 flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock connection closed");
3079 close(zclient->sock);
3080 zclient->sock = -1;
3081 return -1;
3082 }
3083
3084 return 0;
3085 }
3086
3087 /**
3088 * Connect to table manager in a syncronous way
3089 *
3090 * It first writes the request to zcient output buffer and then
3091 * immediately reads the answer from the input buffer.
3092 *
3093 * @param zclient Zclient used to connect to table manager (zebra)
3094 * @result Result of response
3095 */
3096 int tm_table_manager_connect(struct zclient *zclient)
3097 {
3098 int ret;
3099 struct stream *s;
3100 uint8_t result;
3101
3102 if (zclient_debug)
3103 zlog_debug("Connecting to Table Manager");
3104
3105 if (zclient->sock < 0)
3106 return ZCLIENT_SEND_FAILURE;
3107
3108 /* send request */
3109 s = zclient->obuf;
3110 stream_reset(s);
3111 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT);
3112
3113 /* proto */
3114 stream_putc(s, zclient->redist_default);
3115 /* instance */
3116 stream_putw(s, zclient->instance);
3117
3118 /* Put length at the first point of the stream. */
3119 stream_putw_at(s, 0, stream_get_endp(s));
3120
3121 ret = zclient_send_message(zclient);
3122 if (ret == ZCLIENT_SEND_FAILURE)
3123 return -1;
3124
3125 if (zclient_debug)
3126 zlog_debug("%s: Table manager connect request sent", __func__);
3127
3128 /* read response */
3129 if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
3130 != 0)
3131 return -1;
3132
3133 /* result */
3134 s = zclient->ibuf;
3135 STREAM_GETC(s, result);
3136 if (zclient_debug)
3137 zlog_debug(
3138 "%s: Table Manager connect response received, result %u",
3139 __func__, result);
3140
3141 return (int)result;
3142 stream_failure:
3143 return -1;
3144 }
3145
3146 /**
3147 * Function to request a table chunk in a syncronous way
3148 *
3149 * It first writes the request to zclient output buffer and then
3150 * immediately reads the answer from the input buffer.
3151 *
3152 * @param zclient Zclient used to connect to table manager (zebra)
3153 * @param chunk_size Amount of table requested
3154 * @param start to write first assigned chunk table RT ID to
3155 * @param end To write last assigned chunk table RT ID to
3156 * @result 0 on success, -1 otherwise
3157 */
3158 int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
3159 uint32_t *start, uint32_t *end)
3160 {
3161 int ret;
3162 struct stream *s;
3163
3164 if (zclient_debug)
3165 zlog_debug("Getting Table Chunk");
3166
3167 if (zclient->sock < 0)
3168 return -1;
3169
3170 /* send request */
3171 s = zclient->obuf;
3172 stream_reset(s);
3173 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT);
3174 /* chunk size */
3175 stream_putl(s, chunk_size);
3176 /* Put length at the first point of the stream. */
3177 stream_putw_at(s, 0, stream_get_endp(s));
3178
3179 ret = writen(zclient->sock, s->data, stream_get_endp(s));
3180 if (ret < 0) {
3181 flog_err(EC_LIB_ZAPI_SOCKET, "%s: can't write to zclient->sock",
3182 __func__);
3183 close(zclient->sock);
3184 zclient->sock = -1;
3185 return -1;
3186 }
3187 if (ret == 0) {
3188 flog_err(EC_LIB_ZAPI_SOCKET,
3189 "%s: zclient->sock connection closed", __func__);
3190 close(zclient->sock);
3191 zclient->sock = -1;
3192 return -1;
3193 }
3194 if (zclient_debug)
3195 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__,
3196 ret);
3197
3198 /* read response */
3199 if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0)
3200 return -1;
3201
3202 s = zclient->ibuf;
3203 /* start and end table IDs */
3204 STREAM_GETL(s, *start);
3205 STREAM_GETL(s, *end);
3206
3207 if (zclient_debug)
3208 zlog_debug("Table Chunk assign: %u - %u ", *start, *end);
3209
3210 return 0;
3211 stream_failure:
3212 return -1;
3213 }
3214
3215 /**
3216 * Function to release a table chunk
3217 *
3218 * @param zclient Zclient used to connect to table manager (zebra)
3219 * @param start First label of table
3220 * @param end Last label of chunk
3221 * @result 0 on success, -1 otherwise
3222 */
3223 int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
3224 uint32_t end)
3225 {
3226 struct stream *s;
3227
3228 if (zclient_debug)
3229 zlog_debug("Releasing Table Chunk");
3230
3231 if (zclient->sock < 0)
3232 return -1;
3233
3234 /* send request */
3235 s = zclient->obuf;
3236 stream_reset(s);
3237 zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT);
3238
3239 /* start */
3240 stream_putl(s, start);
3241 /* end */
3242 stream_putl(s, end);
3243
3244 /* Put length at the first point of the stream. */
3245 stream_putw_at(s, 0, stream_get_endp(s));
3246
3247 if (zclient_send_message(zclient) == ZCLIENT_SEND_FAILURE)
3248 return -1;
3249
3250 return 0;
3251 }
3252
3253 enum zclient_send_status zebra_send_sr_policy(struct zclient *zclient, int cmd,
3254 struct zapi_sr_policy *zp)
3255 {
3256 if (zapi_sr_policy_encode(zclient->obuf, cmd, zp) < 0)
3257 return ZCLIENT_SEND_FAILURE;
3258 return zclient_send_message(zclient);
3259 }
3260
3261 int zapi_sr_policy_encode(struct stream *s, int cmd, struct zapi_sr_policy *zp)
3262 {
3263 struct zapi_srte_tunnel *zt = &zp->segment_list;
3264
3265 stream_reset(s);
3266
3267 zclient_create_header(s, cmd, VRF_DEFAULT);
3268 stream_putl(s, zp->color);
3269 stream_put_ipaddr(s, &zp->endpoint);
3270 stream_write(s, &zp->name, SRTE_POLICY_NAME_MAX_LENGTH);
3271
3272 stream_putc(s, zt->type);
3273 stream_putl(s, zt->local_label);
3274
3275 if (zt->label_num > MPLS_MAX_LABELS) {
3276 flog_err(EC_LIB_ZAPI_ENCODE,
3277 "%s: label %u: can't encode %u labels (maximum is %u)",
3278 __func__, zt->local_label, zt->label_num,
3279 MPLS_MAX_LABELS);
3280 return -1;
3281 }
3282 stream_putw(s, zt->label_num);
3283
3284 for (int i = 0; i < zt->label_num; i++)
3285 stream_putl(s, zt->labels[i]);
3286
3287 /* Put length at the first point of the stream. */
3288 stream_putw_at(s, 0, stream_get_endp(s));
3289
3290 return 0;
3291 }
3292
3293 int zapi_sr_policy_decode(struct stream *s, struct zapi_sr_policy *zp)
3294 {
3295 memset(zp, 0, sizeof(*zp));
3296
3297 struct zapi_srte_tunnel *zt = &zp->segment_list;
3298
3299 STREAM_GETL(s, zp->color);
3300 STREAM_GET_IPADDR(s, &zp->endpoint);
3301 STREAM_GET(&zp->name, s, SRTE_POLICY_NAME_MAX_LENGTH);
3302
3303 /* segment list of active candidate path */
3304 STREAM_GETC(s, zt->type);
3305 STREAM_GETL(s, zt->local_label);
3306 STREAM_GETW(s, zt->label_num);
3307 if (zt->label_num > MPLS_MAX_LABELS) {
3308 flog_err(EC_LIB_ZAPI_ENCODE,
3309 "%s: label %u: can't decode %u labels (maximum is %u)",
3310 __func__, zt->local_label, zt->label_num,
3311 MPLS_MAX_LABELS);
3312 return -1;
3313 }
3314 for (int i = 0; i < zt->label_num; i++)
3315 STREAM_GETL(s, zt->labels[i]);
3316
3317 return 0;
3318
3319 stream_failure:
3320 return -1;
3321 }
3322
3323 int zapi_sr_policy_notify_status_decode(struct stream *s,
3324 struct zapi_sr_policy *zp)
3325 {
3326 memset(zp, 0, sizeof(*zp));
3327
3328 STREAM_GETL(s, zp->color);
3329 STREAM_GET_IPADDR(s, &zp->endpoint);
3330 STREAM_GET(&zp->name, s, SRTE_POLICY_NAME_MAX_LENGTH);
3331 STREAM_GETL(s, zp->status);
3332
3333 return 0;
3334
3335 stream_failure:
3336 return -1;
3337 }
3338
3339 enum zclient_send_status zebra_send_mpls_labels(struct zclient *zclient,
3340 int cmd, struct zapi_labels *zl)
3341 {
3342 if (zapi_labels_encode(zclient->obuf, cmd, zl) < 0)
3343 return ZCLIENT_SEND_FAILURE;
3344 return zclient_send_message(zclient);
3345 }
3346
3347 int zapi_labels_encode(struct stream *s, int cmd, struct zapi_labels *zl)
3348 {
3349 struct zapi_nexthop *znh;
3350
3351 stream_reset(s);
3352
3353 zclient_create_header(s, cmd, VRF_DEFAULT);
3354 stream_putc(s, zl->message);
3355 stream_putc(s, zl->type);
3356 stream_putl(s, zl->local_label);
3357
3358 if (CHECK_FLAG(zl->message, ZAPI_LABELS_FTN)) {
3359 stream_putw(s, zl->route.prefix.family);
3360 stream_put_prefix(s, &zl->route.prefix);
3361 stream_putc(s, zl->route.type);
3362 stream_putw(s, zl->route.instance);
3363 }
3364
3365 if (zl->nexthop_num > MULTIPATH_NUM) {
3366 flog_err(
3367 EC_LIB_ZAPI_ENCODE,
3368 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3369 __func__, zl->local_label, zl->nexthop_num,
3370 MULTIPATH_NUM);
3371 return -1;
3372 }
3373 stream_putw(s, zl->nexthop_num);
3374
3375 for (int i = 0; i < zl->nexthop_num; i++) {
3376 znh = &zl->nexthops[i];
3377
3378 if (zapi_nexthop_encode(s, znh, 0, 0) < 0)
3379 return -1;
3380 }
3381
3382 if (CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS)) {
3383
3384 if (zl->backup_nexthop_num > MULTIPATH_NUM) {
3385 flog_err(
3386 EC_LIB_ZAPI_ENCODE,
3387 "%s: label %u: can't encode %u nexthops (maximum is %u)",
3388 __func__, zl->local_label, zl->nexthop_num,
3389 MULTIPATH_NUM);
3390 return -1;
3391 }
3392 stream_putw(s, zl->backup_nexthop_num);
3393
3394 for (int i = 0; i < zl->backup_nexthop_num; i++) {
3395 znh = &zl->backup_nexthops[i];
3396
3397 if (zapi_nexthop_encode(s, znh, 0, 0) < 0)
3398 return -1;
3399 }
3400
3401 }
3402
3403 /* Put length at the first point of the stream. */
3404 stream_putw_at(s, 0, stream_get_endp(s));
3405
3406 return 0;
3407 }
3408
3409 int zapi_labels_decode(struct stream *s, struct zapi_labels *zl)
3410 {
3411 struct zapi_nexthop *znh;
3412
3413 memset(zl, 0, sizeof(*zl));
3414
3415 /* Get data. */
3416 STREAM_GETC(s, zl->message);
3417 STREAM_GETC(s, zl->type);
3418 STREAM_GETL(s, zl->local_label);
3419
3420 if (CHECK_FLAG(zl->message, ZAPI_LABELS_FTN)) {
3421 size_t psize;
3422
3423 STREAM_GETW(s, zl->route.prefix.family);
3424 STREAM_GETC(s, zl->route.prefix.prefixlen);
3425
3426 psize = PSIZE(zl->route.prefix.prefixlen);
3427 switch (zl->route.prefix.family) {
3428 case AF_INET:
3429 if (zl->route.prefix.prefixlen > IPV4_MAX_BITLEN) {
3430 zlog_debug(
3431 "%s: Specified prefix length %d is greater than a v4 address can support",
3432 __func__, zl->route.prefix.prefixlen);
3433 return -1;
3434 }
3435 STREAM_GET(&zl->route.prefix.u.prefix4.s_addr, s,
3436 psize);
3437 break;
3438 case AF_INET6:
3439 if (zl->route.prefix.prefixlen > IPV6_MAX_BITLEN) {
3440 zlog_debug(
3441 "%s: Specified prefix length %d is greater than a v6 address can support",
3442 __func__, zl->route.prefix.prefixlen);
3443 return -1;
3444 }
3445 STREAM_GET(&zl->route.prefix.u.prefix6, s, psize);
3446 break;
3447 default:
3448 flog_err(EC_LIB_ZAPI_ENCODE,
3449 "%s: Specified family %u is not v4 or v6",
3450 __func__, zl->route.prefix.family);
3451 return -1;
3452 }
3453
3454 STREAM_GETC(s, zl->route.type);
3455 STREAM_GETW(s, zl->route.instance);
3456 }
3457
3458 STREAM_GETW(s, zl->nexthop_num);
3459
3460 if (zl->nexthop_num > MULTIPATH_NUM) {
3461 flog_warn(
3462 EC_LIB_ZAPI_ENCODE,
3463 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
3464 __func__, &zl->route.prefix, zl->nexthop_num,
3465 MULTIPATH_NUM);
3466 }
3467
3468 zl->nexthop_num = MIN(MULTIPATH_NUM, zl->nexthop_num);
3469
3470 for (int i = 0; i < zl->nexthop_num; i++) {
3471 znh = &zl->nexthops[i];
3472
3473 if (zapi_nexthop_decode(s, znh, 0, 0) < 0)
3474 return -1;
3475
3476 if (znh->type == NEXTHOP_TYPE_BLACKHOLE) {
3477 flog_warn(
3478 EC_LIB_ZAPI_ENCODE,
3479 "%s: Prefix %pFX has a blackhole nexthop which we cannot use for a label",
3480 __func__, &zl->route.prefix);
3481 return -1;
3482 }
3483 }
3484
3485 if (CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS)) {
3486 STREAM_GETW(s, zl->backup_nexthop_num);
3487
3488 if (zl->backup_nexthop_num > MULTIPATH_NUM) {
3489 flog_warn(
3490 EC_LIB_ZAPI_ENCODE,
3491 "%s: Prefix %pFX has %d backup nexthops, but we can only use the first %d",
3492 __func__, &zl->route.prefix,
3493 zl->backup_nexthop_num, MULTIPATH_NUM);
3494 }
3495
3496 zl->backup_nexthop_num = MIN(MULTIPATH_NUM,
3497 zl->backup_nexthop_num);
3498
3499 for (int i = 0; i < zl->backup_nexthop_num; i++) {
3500 znh = &zl->backup_nexthops[i];
3501
3502 if (zapi_nexthop_decode(s, znh, 0, 0) < 0)
3503 return -1;
3504
3505 if (znh->type == NEXTHOP_TYPE_BLACKHOLE) {
3506 flog_warn(
3507 EC_LIB_ZAPI_ENCODE,
3508 "%s: Prefix %pFX has a backup blackhole nexthop which we cannot use for a label",
3509 __func__, &zl->route.prefix);
3510 return -1;
3511 }
3512 }
3513 }
3514
3515 return 0;
3516 stream_failure:
3517 return -1;
3518 }
3519
3520 enum zclient_send_status zebra_send_pw(struct zclient *zclient, int command,
3521 struct zapi_pw *pw)
3522 {
3523 struct stream *s;
3524
3525 /* Reset stream. */
3526 s = zclient->obuf;
3527 stream_reset(s);
3528
3529 zclient_create_header(s, command, VRF_DEFAULT);
3530 stream_write(s, pw->ifname, IF_NAMESIZE);
3531 stream_putl(s, pw->ifindex);
3532
3533 /* Put type */
3534 stream_putl(s, pw->type);
3535
3536 /* Put nexthop */
3537 stream_putl(s, pw->af);
3538 switch (pw->af) {
3539 case AF_INET:
3540 stream_put_in_addr(s, &pw->nexthop.ipv4);
3541 break;
3542 case AF_INET6:
3543 stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16);
3544 break;
3545 default:
3546 flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__);
3547 return ZCLIENT_SEND_FAILURE;
3548 }
3549
3550 /* Put labels */
3551 stream_putl(s, pw->local_label);
3552 stream_putl(s, pw->remote_label);
3553
3554 /* Put flags */
3555 stream_putc(s, pw->flags);
3556
3557 /* Protocol specific fields */
3558 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
3559
3560 /* Put length at the first point of the stream. */
3561 stream_putw_at(s, 0, stream_get_endp(s));
3562
3563 return zclient_send_message(zclient);
3564 }
3565
3566 /*
3567 * Receive PW status update from Zebra and send it to LDE process.
3568 */
3569 int zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS, struct zapi_pw_status *pw)
3570 {
3571 struct stream *s;
3572
3573 memset(pw, 0, sizeof(struct zapi_pw_status));
3574 s = zclient->ibuf;
3575
3576 /* Get data. */
3577 stream_get(pw->ifname, s, IF_NAMESIZE);
3578 STREAM_GETL(s, pw->ifindex);
3579 STREAM_GETL(s, pw->status);
3580
3581 return 0;
3582 stream_failure:
3583 return -1;
3584 }
3585
3586 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS)
3587 {
3588 struct zclient_capabilities cap;
3589 struct stream *s = zclient->ibuf;
3590 int vrf_backend;
3591 uint8_t mpls_enabled;
3592
3593 STREAM_GETL(s, vrf_backend);
3594
3595 if (vrf_backend < 0 || vrf_configure_backend(vrf_backend)) {
3596 flog_err(EC_LIB_ZAPI_ENCODE,
3597 "%s: Garbage VRF backend type: %d", __func__,
3598 vrf_backend);
3599 goto stream_failure;
3600 }
3601
3602
3603 memset(&cap, 0, sizeof(cap));
3604 STREAM_GETC(s, mpls_enabled);
3605 cap.mpls_enabled = !!mpls_enabled;
3606 STREAM_GETL(s, cap.ecmp);
3607 STREAM_GETC(s, cap.role);
3608
3609 if (zclient->zebra_capabilities)
3610 (*zclient->zebra_capabilities)(&cap);
3611
3612 stream_failure:
3613 return;
3614 }
3615
3616 enum zclient_send_status zclient_send_mlag_register(struct zclient *client,
3617 uint32_t bit_map)
3618 {
3619 struct stream *s;
3620
3621 s = client->obuf;
3622 stream_reset(s);
3623
3624 zclient_create_header(s, ZEBRA_MLAG_CLIENT_REGISTER, VRF_DEFAULT);
3625 stream_putl(s, bit_map);
3626
3627 stream_putw_at(s, 0, stream_get_endp(s));
3628 return zclient_send_message(client);
3629 }
3630
3631 enum zclient_send_status zclient_send_mlag_deregister(struct zclient *client)
3632 {
3633 return zebra_message_send(client, ZEBRA_MLAG_CLIENT_UNREGISTER,
3634 VRF_DEFAULT);
3635 }
3636
3637 enum zclient_send_status zclient_send_mlag_data(struct zclient *client,
3638 struct stream *client_s)
3639 {
3640 struct stream *s;
3641
3642 s = client->obuf;
3643 stream_reset(s);
3644
3645 zclient_create_header(s, ZEBRA_MLAG_FORWARD_MSG, VRF_DEFAULT);
3646 stream_put(s, client_s->data, client_s->endp);
3647
3648 stream_putw_at(s, 0, stream_get_endp(s));
3649 return zclient_send_message(client);
3650 }
3651
3652 static void zclient_mlag_process_up(ZAPI_CALLBACK_ARGS)
3653 {
3654 if (zclient->mlag_process_up)
3655 (*zclient->mlag_process_up)();
3656 }
3657
3658 static void zclient_mlag_process_down(ZAPI_CALLBACK_ARGS)
3659 {
3660 if (zclient->mlag_process_down)
3661 (*zclient->mlag_process_down)();
3662 }
3663
3664 static void zclient_mlag_handle_msg(ZAPI_CALLBACK_ARGS)
3665 {
3666 if (zclient->mlag_handle_msg)
3667 (*zclient->mlag_handle_msg)(zclient->ibuf, length);
3668 }
3669
3670 /*
3671 * Send an OPAQUE message, contents opaque to zebra. The message header
3672 * is a message subtype.
3673 */
3674 enum zclient_send_status zclient_send_opaque(struct zclient *zclient,
3675 uint32_t type, const uint8_t *data,
3676 size_t datasize)
3677 {
3678 struct stream *s;
3679 uint16_t flags = 0;
3680
3681 /* Check buffer size */
3682 if (STREAM_SIZE(zclient->obuf) <
3683 (ZEBRA_HEADER_SIZE + sizeof(type) + datasize))
3684 return ZCLIENT_SEND_FAILURE;
3685
3686 s = zclient->obuf;
3687 stream_reset(s);
3688
3689 zclient_create_header(s, ZEBRA_OPAQUE_MESSAGE, VRF_DEFAULT);
3690
3691 /* Send sub-type and flags */
3692 stream_putl(s, type);
3693 stream_putw(s, flags);
3694
3695 /* Send opaque data */
3696 stream_write(s, data, datasize);
3697
3698 /* Put length into the header at the start of the stream. */
3699 stream_putw_at(s, 0, stream_get_endp(s));
3700
3701 return zclient_send_message(zclient);
3702 }
3703
3704 /*
3705 * Send an OPAQUE message to a specific zclient. The contents are opaque
3706 * to zebra.
3707 */
3708 enum zclient_send_status
3709 zclient_send_opaque_unicast(struct zclient *zclient, uint32_t type,
3710 uint8_t proto, uint16_t instance,
3711 uint32_t session_id, const uint8_t *data,
3712 size_t datasize)
3713 {
3714 struct stream *s;
3715 uint16_t flags = 0;
3716
3717 /* Check buffer size */
3718 if (STREAM_SIZE(zclient->obuf) <
3719 (ZEBRA_HEADER_SIZE + sizeof(struct zapi_opaque_msg) + datasize))
3720 return ZCLIENT_SEND_FAILURE;
3721
3722 s = zclient->obuf;
3723 stream_reset(s);
3724
3725 zclient_create_header(s, ZEBRA_OPAQUE_MESSAGE, VRF_DEFAULT);
3726
3727 /* Send sub-type and flags */
3728 SET_FLAG(flags, ZAPI_OPAQUE_FLAG_UNICAST);
3729 stream_putl(s, type);
3730 stream_putw(s, flags);
3731
3732 /* Send destination client info */
3733 stream_putc(s, proto);
3734 stream_putw(s, instance);
3735 stream_putl(s, session_id);
3736
3737 /* Send opaque data */
3738 stream_write(s, data, datasize);
3739
3740 /* Put length into the header at the start of the stream. */
3741 stream_putw_at(s, 0, stream_get_endp(s));
3742
3743 return zclient_send_message(zclient);
3744 }
3745
3746 /*
3747 * Decode incoming opaque message into info struct
3748 */
3749 int zclient_opaque_decode(struct stream *s, struct zapi_opaque_msg *info)
3750 {
3751 memset(info, 0, sizeof(*info));
3752
3753 /* Decode subtype and flags */
3754 STREAM_GETL(s, info->type);
3755 STREAM_GETW(s, info->flags);
3756
3757 /* Decode unicast client info if present */
3758 if (CHECK_FLAG(info->flags, ZAPI_OPAQUE_FLAG_UNICAST)) {
3759 STREAM_GETC(s, info->proto);
3760 STREAM_GETW(s, info->instance);
3761 STREAM_GETL(s, info->session_id);
3762 }
3763
3764 info->len = STREAM_READABLE(s);
3765
3766 return 0;
3767
3768 stream_failure:
3769
3770 return -1;
3771 }
3772
3773 /*
3774 * Send a registration request for opaque messages with a specified subtype.
3775 */
3776 enum zclient_send_status zclient_register_opaque(struct zclient *zclient,
3777 uint32_t type)
3778 {
3779 struct stream *s;
3780
3781 s = zclient->obuf;
3782 stream_reset(s);
3783
3784 zclient_create_header(s, ZEBRA_OPAQUE_REGISTER, VRF_DEFAULT);
3785
3786 /* Send sub-type */
3787 stream_putl(s, type);
3788
3789 /* Add zclient info */
3790 stream_putc(s, zclient->redist_default);
3791 stream_putw(s, zclient->instance);
3792 stream_putl(s, zclient->session_id);
3793
3794 /* Put length at the first point of the stream. */
3795 stream_putw_at(s, 0, stream_get_endp(s));
3796
3797 return zclient_send_message(zclient);
3798 }
3799
3800 /*
3801 * Send an un-registration request for a specified opaque subtype.
3802 */
3803 enum zclient_send_status zclient_unregister_opaque(struct zclient *zclient,
3804 uint32_t type)
3805 {
3806 struct stream *s;
3807
3808 s = zclient->obuf;
3809 stream_reset(s);
3810
3811 zclient_create_header(s, ZEBRA_OPAQUE_UNREGISTER, VRF_DEFAULT);
3812
3813 /* Send sub-type */
3814 stream_putl(s, type);
3815
3816 /* Add zclient info */
3817 stream_putc(s, zclient->redist_default);
3818 stream_putw(s, zclient->instance);
3819 stream_putl(s, zclient->session_id);
3820
3821 /* Put length at the first point of the stream. */
3822 stream_putw_at(s, 0, stream_get_endp(s));
3823
3824 return zclient_send_message(zclient);
3825 }
3826
3827 /* Utility to decode opaque registration info */
3828 int zapi_opaque_reg_decode(struct stream *s, struct zapi_opaque_reg_info *info)
3829 {
3830 STREAM_GETL(s, info->type);
3831 STREAM_GETC(s, info->proto);
3832 STREAM_GETW(s, info->instance);
3833 STREAM_GETL(s, info->session_id);
3834
3835 return 0;
3836
3837 stream_failure:
3838
3839 return -1;
3840 }
3841
3842 /* Utility to decode client close notify info */
3843 int zapi_client_close_notify_decode(struct stream *s,
3844 struct zapi_client_close_info *info)
3845 {
3846 memset(info, 0, sizeof(*info));
3847
3848 STREAM_GETC(s, info->proto);
3849 STREAM_GETW(s, info->instance);
3850 STREAM_GETL(s, info->session_id);
3851
3852 return 0;
3853
3854 stream_failure:
3855
3856 return -1;
3857 }
3858
3859 /* Zebra client message read function. */
3860 static int zclient_read(struct thread *thread)
3861 {
3862 size_t already;
3863 uint16_t length, command;
3864 uint8_t marker, version;
3865 vrf_id_t vrf_id;
3866 struct zclient *zclient;
3867
3868 /* Get socket to zebra. */
3869 zclient = THREAD_ARG(thread);
3870 zclient->t_read = NULL;
3871
3872 /* Read zebra header (if we don't have it already). */
3873 already = stream_get_endp(zclient->ibuf);
3874 if (already < ZEBRA_HEADER_SIZE) {
3875 ssize_t nbyte;
3876 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
3877 ZEBRA_HEADER_SIZE - already))
3878 == 0)
3879 || (nbyte == -1)) {
3880 if (zclient_debug)
3881 zlog_debug(
3882 "zclient connection closed socket [%d].",
3883 zclient->sock);
3884 return zclient_failed(zclient);
3885 }
3886 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
3887 zclient_event(ZCLIENT_READ, zclient);
3888 return 0;
3889 }
3890 already = ZEBRA_HEADER_SIZE;
3891 }
3892
3893 /* Reset to read from the beginning of the incoming packet. */
3894 stream_set_getp(zclient->ibuf, 0);
3895
3896 /* Fetch header values. */
3897 length = stream_getw(zclient->ibuf);
3898 marker = stream_getc(zclient->ibuf);
3899 version = stream_getc(zclient->ibuf);
3900 vrf_id = stream_getl(zclient->ibuf);
3901 command = stream_getw(zclient->ibuf);
3902
3903 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
3904 flog_err(
3905 EC_LIB_ZAPI_MISSMATCH,
3906 "%s: socket %d version mismatch, marker %d, version %d",
3907 __func__, zclient->sock, marker, version);
3908 return zclient_failed(zclient);
3909 }
3910
3911 if (length < ZEBRA_HEADER_SIZE) {
3912 flog_err(EC_LIB_ZAPI_MISSMATCH,
3913 "%s: socket %d message length %u is less than %d ",
3914 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
3915 return zclient_failed(zclient);
3916 }
3917
3918 /* Length check. */
3919 if (length > STREAM_SIZE(zclient->ibuf)) {
3920 struct stream *ns;
3921 flog_err(
3922 EC_LIB_ZAPI_ENCODE,
3923 "%s: message size %u exceeds buffer size %lu, expanding...",
3924 __func__, length,
3925 (unsigned long)STREAM_SIZE(zclient->ibuf));
3926 ns = stream_new(length);
3927 stream_copy(ns, zclient->ibuf);
3928 stream_free(zclient->ibuf);
3929 zclient->ibuf = ns;
3930 }
3931
3932 /* Read rest of zebra packet. */
3933 if (already < length) {
3934 ssize_t nbyte;
3935 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
3936 length - already))
3937 == 0)
3938 || (nbyte == -1)) {
3939 if (zclient_debug)
3940 zlog_debug(
3941 "zclient connection closed socket [%d].",
3942 zclient->sock);
3943 return zclient_failed(zclient);
3944 }
3945 if (nbyte != (ssize_t)(length - already)) {
3946 /* Try again later. */
3947 zclient_event(ZCLIENT_READ, zclient);
3948 return 0;
3949 }
3950 }
3951
3952 length -= ZEBRA_HEADER_SIZE;
3953
3954 if (zclient_debug)
3955 zlog_debug("zclient %p command %s VRF %u", zclient,
3956 zserv_command_string(command), vrf_id);
3957
3958 switch (command) {
3959 case ZEBRA_CAPABILITIES:
3960 zclient_capability_decode(command, zclient, length, vrf_id);
3961 break;
3962 case ZEBRA_ROUTER_ID_UPDATE:
3963 if (zclient->router_id_update)
3964 (*zclient->router_id_update)(command, zclient, length,
3965 vrf_id);
3966 break;
3967 case ZEBRA_VRF_ADD:
3968 zclient_vrf_add(zclient, vrf_id);
3969 break;
3970 case ZEBRA_VRF_DELETE:
3971 zclient_vrf_delete(zclient, vrf_id);
3972 break;
3973 case ZEBRA_INTERFACE_ADD:
3974 zclient_interface_add(zclient, vrf_id);
3975 break;
3976 case ZEBRA_INTERFACE_DELETE:
3977 zclient_interface_delete(zclient, vrf_id);
3978 break;
3979 case ZEBRA_INTERFACE_ADDRESS_ADD:
3980 if (zclient->interface_address_add)
3981 (*zclient->interface_address_add)(command, zclient,
3982 length, vrf_id);
3983 break;
3984 case ZEBRA_INTERFACE_ADDRESS_DELETE:
3985 if (zclient->interface_address_delete)
3986 (*zclient->interface_address_delete)(command, zclient,
3987 length, vrf_id);
3988 break;
3989 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
3990 if (zclient->interface_bfd_dest_update)
3991 (*zclient->interface_bfd_dest_update)(command, zclient,
3992 length, vrf_id);
3993 break;
3994 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
3995 if (zclient->interface_nbr_address_add)
3996 (*zclient->interface_nbr_address_add)(command, zclient,
3997 length, vrf_id);
3998 break;
3999 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
4000 if (zclient->interface_nbr_address_delete)
4001 (*zclient->interface_nbr_address_delete)(
4002 command, zclient, length, vrf_id);
4003 break;
4004 case ZEBRA_INTERFACE_UP:
4005 zclient_interface_up(zclient, vrf_id);
4006 break;
4007 case ZEBRA_INTERFACE_DOWN:
4008 zclient_interface_down(zclient, vrf_id);
4009 break;
4010 case ZEBRA_INTERFACE_VRF_UPDATE:
4011 if (zclient->interface_vrf_update)
4012 (*zclient->interface_vrf_update)(command, zclient,
4013 length, vrf_id);
4014 break;
4015 case ZEBRA_NEXTHOP_UPDATE:
4016 if (zclient_debug)
4017 zlog_debug("zclient rcvd nexthop update");
4018 if (zclient->nexthop_update)
4019 (*zclient->nexthop_update)(command, zclient, length,
4020 vrf_id);
4021 break;
4022 case ZEBRA_BFD_DEST_REPLAY:
4023 if (zclient->bfd_dest_replay)
4024 (*zclient->bfd_dest_replay)(command, zclient, length,
4025 vrf_id);
4026 break;
4027 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
4028 if (zclient->redistribute_route_add)
4029 (*zclient->redistribute_route_add)(command, zclient,
4030 length, vrf_id);
4031 break;
4032 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
4033 if (zclient->redistribute_route_del)
4034 (*zclient->redistribute_route_del)(command, zclient,
4035 length, vrf_id);
4036 break;
4037 case ZEBRA_INTERFACE_LINK_PARAMS:
4038 if (zclient->interface_link_params)
4039 (*zclient->interface_link_params)(command, zclient,
4040 length, vrf_id);
4041 break;
4042 case ZEBRA_FEC_UPDATE:
4043 if (zclient_debug)
4044 zlog_debug("zclient rcvd fec update");
4045 if (zclient->fec_update)
4046 (*zclient->fec_update)(command, zclient, length);
4047 break;
4048 case ZEBRA_LOCAL_ES_ADD:
4049 if (zclient->local_es_add)
4050 (*zclient->local_es_add)(command, zclient, length,
4051 vrf_id);
4052 break;
4053 case ZEBRA_LOCAL_ES_DEL:
4054 if (zclient->local_es_del)
4055 (*zclient->local_es_del)(command, zclient, length,
4056 vrf_id);
4057 break;
4058 case ZEBRA_LOCAL_ES_EVI_ADD:
4059 if (zclient->local_es_evi_add)
4060 (*zclient->local_es_evi_add)(command, zclient, length,
4061 vrf_id);
4062 break;
4063 case ZEBRA_LOCAL_ES_EVI_DEL:
4064 if (zclient->local_es_evi_del)
4065 (*zclient->local_es_evi_del)(command, zclient, length,
4066 vrf_id);
4067 break;
4068 case ZEBRA_VNI_ADD:
4069 if (zclient->local_vni_add)
4070 (*zclient->local_vni_add)(command, zclient, length,
4071 vrf_id);
4072 break;
4073 case ZEBRA_VNI_DEL:
4074 if (zclient->local_vni_del)
4075 (*zclient->local_vni_del)(command, zclient, length,
4076 vrf_id);
4077 break;
4078 case ZEBRA_L3VNI_ADD:
4079 if (zclient->local_l3vni_add)
4080 (*zclient->local_l3vni_add)(command, zclient, length,
4081 vrf_id);
4082 break;
4083 case ZEBRA_L3VNI_DEL:
4084 if (zclient->local_l3vni_del)
4085 (*zclient->local_l3vni_del)(command, zclient, length,
4086 vrf_id);
4087 break;
4088 case ZEBRA_MACIP_ADD:
4089 if (zclient->local_macip_add)
4090 (*zclient->local_macip_add)(command, zclient, length,
4091 vrf_id);
4092 break;
4093 case ZEBRA_MACIP_DEL:
4094 if (zclient->local_macip_del)
4095 (*zclient->local_macip_del)(command, zclient, length,
4096 vrf_id);
4097 break;
4098 case ZEBRA_IP_PREFIX_ROUTE_ADD:
4099 if (zclient->local_ip_prefix_add)
4100 (*zclient->local_ip_prefix_add)(command, zclient,
4101 length, vrf_id);
4102 break;
4103 case ZEBRA_IP_PREFIX_ROUTE_DEL:
4104 if (zclient->local_ip_prefix_del)
4105 (*zclient->local_ip_prefix_del)(command, zclient,
4106 length, vrf_id);
4107 break;
4108 case ZEBRA_PW_STATUS_UPDATE:
4109 if (zclient->pw_status_update)
4110 (*zclient->pw_status_update)(command, zclient, length,
4111 vrf_id);
4112 break;
4113 case ZEBRA_ROUTE_NOTIFY_OWNER:
4114 if (zclient->route_notify_owner)
4115 (*zclient->route_notify_owner)(command, zclient, length,
4116 vrf_id);
4117 break;
4118 case ZEBRA_RULE_NOTIFY_OWNER:
4119 if (zclient->rule_notify_owner)
4120 (*zclient->rule_notify_owner)(command, zclient, length,
4121 vrf_id);
4122 break;
4123 case ZEBRA_NHG_NOTIFY_OWNER:
4124 if (zclient->nhg_notify_owner)
4125 (*zclient->nhg_notify_owner)(command, zclient, length,
4126 vrf_id);
4127 break;
4128 case ZEBRA_GET_LABEL_CHUNK:
4129 if (zclient->label_chunk)
4130 (*zclient->label_chunk)(command, zclient, length,
4131 vrf_id);
4132 break;
4133 case ZEBRA_IPSET_NOTIFY_OWNER:
4134 if (zclient->ipset_notify_owner)
4135 (*zclient->ipset_notify_owner)(command, zclient, length,
4136 vrf_id);
4137 break;
4138 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER:
4139 if (zclient->ipset_entry_notify_owner)
4140 (*zclient->ipset_entry_notify_owner)(command,
4141 zclient, length,
4142 vrf_id);
4143 break;
4144 case ZEBRA_IPTABLE_NOTIFY_OWNER:
4145 if (zclient->iptable_notify_owner)
4146 (*zclient->iptable_notify_owner)(command,
4147 zclient, length,
4148 vrf_id);
4149 break;
4150 case ZEBRA_VXLAN_SG_ADD:
4151 if (zclient->vxlan_sg_add)
4152 (*zclient->vxlan_sg_add)(command, zclient, length,
4153 vrf_id);
4154 break;
4155 case ZEBRA_VXLAN_SG_DEL:
4156 if (zclient->vxlan_sg_del)
4157 (*zclient->vxlan_sg_del)(command, zclient, length,
4158 vrf_id);
4159 break;
4160 case ZEBRA_MLAG_PROCESS_UP:
4161 zclient_mlag_process_up(command, zclient, length, vrf_id);
4162 break;
4163 case ZEBRA_MLAG_PROCESS_DOWN:
4164 zclient_mlag_process_down(command, zclient, length, vrf_id);
4165 break;
4166 case ZEBRA_MLAG_FORWARD_MSG:
4167 zclient_mlag_handle_msg(command, zclient, length, vrf_id);
4168 break;
4169 case ZEBRA_SRV6_LOCATOR_ADD:
4170 if (zclient->srv6_locator_add)
4171 (*zclient->srv6_locator_add)(command, zclient, length,
4172 vrf_id);
4173 break;
4174 case ZEBRA_SRV6_LOCATOR_DELETE:
4175 if (zclient->srv6_locator_delete)
4176 (*zclient->srv6_locator_delete)(command, zclient,
4177 length, vrf_id);
4178 break;
4179 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK:
4180 if (zclient->process_srv6_locator_chunk)
4181 (*zclient->process_srv6_locator_chunk)(command, zclient,
4182 length, vrf_id);
4183 break;
4184 case ZEBRA_ERROR:
4185 zclient_handle_error(command, zclient, length, vrf_id);
4186 break;
4187 case ZEBRA_OPAQUE_MESSAGE:
4188 if (zclient->opaque_msg_handler)
4189 (*zclient->opaque_msg_handler)(command, zclient, length,
4190 vrf_id);
4191 break;
4192 case ZEBRA_OPAQUE_REGISTER:
4193 if (zclient->opaque_register_handler)
4194 (*zclient->opaque_register_handler)(command, zclient,
4195 length, vrf_id);
4196 break;
4197 case ZEBRA_OPAQUE_UNREGISTER:
4198 if (zclient->opaque_unregister_handler)
4199 (*zclient->opaque_unregister_handler)(command, zclient,
4200 length, vrf_id);
4201 break;
4202 case ZEBRA_SR_POLICY_NOTIFY_STATUS:
4203 if (zclient->sr_policy_notify_status)
4204 (*zclient->sr_policy_notify_status)(command, zclient,
4205 length, vrf_id);
4206 break;
4207 case ZEBRA_CLIENT_CLOSE_NOTIFY:
4208 if (zclient->zebra_client_close_notify)
4209 (*zclient->zebra_client_close_notify)(command, zclient,
4210 length, vrf_id);
4211 break;
4212 case ZEBRA_NHRP_NEIGH_ADDED:
4213 if (zclient->neighbor_added)
4214 (*zclient->neighbor_added)(command, zclient, length,
4215 vrf_id);
4216 break;
4217 case ZEBRA_NHRP_NEIGH_REMOVED:
4218 if (zclient->neighbor_removed)
4219 (*zclient->neighbor_removed)(command, zclient, length,
4220 vrf_id);
4221 break;
4222 case ZEBRA_NHRP_NEIGH_GET:
4223 if (zclient->neighbor_get)
4224 (*zclient->neighbor_get)(command, zclient, length,
4225 vrf_id);
4226 break;
4227 case ZEBRA_GRE_UPDATE:
4228 if (zclient->gre_update)
4229 (*zclient->gre_update)(command, zclient,
4230 length, vrf_id);
4231 break;
4232 default:
4233 break;
4234 }
4235
4236 if (zclient->sock < 0)
4237 /* Connection was closed during packet processing. */
4238 return -1;
4239
4240 /* Register read thread. */
4241 stream_reset(zclient->ibuf);
4242 zclient_event(ZCLIENT_READ, zclient);
4243
4244 return 0;
4245 }
4246
4247 void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
4248 int type, unsigned short instance, vrf_id_t vrf_id)
4249 {
4250
4251 if (instance) {
4252 if (command == ZEBRA_REDISTRIBUTE_ADD) {
4253 if (redist_check_instance(
4254 &zclient->mi_redist[afi][type], instance))
4255 return;
4256 redist_add_instance(&zclient->mi_redist[afi][type],
4257 instance);
4258 } else {
4259 if (!redist_check_instance(
4260 &zclient->mi_redist[afi][type], instance))
4261 return;
4262 redist_del_instance(&zclient->mi_redist[afi][type],
4263 instance);
4264 }
4265
4266 } else {
4267 if (command == ZEBRA_REDISTRIBUTE_ADD) {
4268 if (vrf_bitmap_check(zclient->redist[afi][type],
4269 vrf_id))
4270 return;
4271 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
4272 } else {
4273 if (!vrf_bitmap_check(zclient->redist[afi][type],
4274 vrf_id))
4275 return;
4276 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
4277 }
4278 }
4279
4280 if (zclient->sock > 0)
4281 zebra_redistribute_send(command, zclient, afi, type, instance,
4282 vrf_id);
4283 }
4284
4285
4286 void zclient_redistribute_default(int command, struct zclient *zclient,
4287 afi_t afi, vrf_id_t vrf_id)
4288 {
4289
4290 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
4291 if (vrf_bitmap_check(zclient->default_information[afi], vrf_id))
4292 return;
4293 vrf_bitmap_set(zclient->default_information[afi], vrf_id);
4294 } else {
4295 if (!vrf_bitmap_check(zclient->default_information[afi],
4296 vrf_id))
4297 return;
4298 vrf_bitmap_unset(zclient->default_information[afi], vrf_id);
4299 }
4300
4301 if (zclient->sock > 0)
4302 zebra_redistribute_default_send(command, zclient, afi, vrf_id);
4303 }
4304
4305 static void zclient_event(enum event event, struct zclient *zclient)
4306 {
4307 switch (event) {
4308 case ZCLIENT_SCHEDULE:
4309 thread_add_event(zclient->master, zclient_connect, zclient, 0,
4310 &zclient->t_connect);
4311 break;
4312 case ZCLIENT_CONNECT:
4313 if (zclient_debug)
4314 zlog_debug(
4315 "zclient connect failures: %d schedule interval is now %d",
4316 zclient->fail, zclient->fail < 3 ? 10 : 60);
4317 thread_add_timer(zclient->master, zclient_connect, zclient,
4318 zclient->fail < 3 ? 10 : 60,
4319 &zclient->t_connect);
4320 break;
4321 case ZCLIENT_READ:
4322 zclient->t_read = NULL;
4323 thread_add_read(zclient->master, zclient_read, zclient,
4324 zclient->sock, &zclient->t_read);
4325 break;
4326 }
4327 }
4328
4329 enum zclient_send_status zclient_interface_set_master(struct zclient *client,
4330 struct interface *master,
4331 struct interface *slave)
4332 {
4333 struct stream *s;
4334
4335 s = client->obuf;
4336 stream_reset(s);
4337
4338 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
4339
4340 stream_putl(s, master->vrf_id);
4341 stream_putl(s, master->ifindex);
4342 stream_putl(s, slave->vrf_id);
4343 stream_putl(s, slave->ifindex);
4344
4345 stream_putw_at(s, 0, stream_get_endp(s));
4346 return zclient_send_message(client);
4347 }
4348
4349 /*
4350 * Send capabilities message to zebra
4351 */
4352 enum zclient_send_status zclient_capabilities_send(uint32_t cmd,
4353 struct zclient *zclient,
4354 struct zapi_cap *api)
4355 {
4356
4357 struct stream *s;
4358
4359 if (zclient == NULL)
4360 return ZCLIENT_SEND_FAILURE;
4361
4362 s = zclient->obuf;
4363 stream_reset(s);
4364 zclient_create_header(s, cmd, 0);
4365 stream_putl(s, api->cap);
4366
4367 switch (api->cap) {
4368 case ZEBRA_CLIENT_GR_CAPABILITIES:
4369 case ZEBRA_CLIENT_RIB_STALE_TIME:
4370 stream_putl(s, api->stale_removal_time);
4371 stream_putl(s, api->vrf_id);
4372 break;
4373 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
4374 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
4375 stream_putl(s, api->afi);
4376 stream_putl(s, api->safi);
4377 stream_putl(s, api->vrf_id);
4378 break;
4379 case ZEBRA_CLIENT_GR_DISABLE:
4380 stream_putl(s, api->vrf_id);
4381 break;
4382 }
4383
4384 /* Put length at the first point of the stream */
4385 stream_putw_at(s, 0, stream_get_endp(s));
4386
4387 return zclient_send_message(zclient);
4388 }
4389
4390 /*
4391 * Process capabilities message from zebra
4392 */
4393 int32_t zapi_capabilities_decode(struct stream *s, struct zapi_cap *api)
4394 {
4395
4396 memset(api, 0, sizeof(*api));
4397
4398 STREAM_GETL(s, api->cap);
4399 switch (api->cap) {
4400 case ZEBRA_CLIENT_GR_CAPABILITIES:
4401 case ZEBRA_CLIENT_RIB_STALE_TIME:
4402 STREAM_GETL(s, api->stale_removal_time);
4403 STREAM_GETL(s, api->vrf_id);
4404 break;
4405 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
4406 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
4407 STREAM_GETL(s, api->afi);
4408 STREAM_GETL(s, api->safi);
4409 STREAM_GETL(s, api->vrf_id);
4410 break;
4411 case ZEBRA_CLIENT_GR_DISABLE:
4412 STREAM_GETL(s, api->vrf_id);
4413 break;
4414 }
4415 stream_failure:
4416 return 0;
4417 }
4418
4419 enum zclient_send_status
4420 zclient_send_neigh_discovery_req(struct zclient *zclient,
4421 const struct interface *ifp,
4422 const struct prefix *p)
4423 {
4424 struct stream *s;
4425
4426 s = zclient->obuf;
4427 stream_reset(s);
4428
4429 zclient_create_header(s, ZEBRA_NEIGH_DISCOVER, ifp->vrf_id);
4430 stream_putl(s, ifp->ifindex);
4431
4432 stream_putc(s, p->family);
4433 stream_putc(s, p->prefixlen);
4434 stream_put(s, &p->u.prefix, prefix_blen(p));
4435
4436 stream_putw_at(s, 0, stream_get_endp(s));
4437 return zclient_send_message(zclient);
4438 }
4439
4440 /*
4441 * Get a starting nhg point for a routing protocol
4442 */
4443 uint32_t zclient_get_nhg_start(uint32_t proto)
4444 {
4445 assert(proto < ZEBRA_ROUTE_MAX);
4446
4447 return ZEBRA_NHG_PROTO_SPACING * proto;
4448 }
4449
4450 char *zclient_dump_route_flags(uint32_t flags, char *buf, size_t len)
4451 {
4452 if (flags == 0) {
4453 snprintfrr(buf, len, "None ");
4454 return buf;
4455 }
4456
4457 snprintfrr(
4458 buf, len, "%s%s%s%s%s%s%s%s%s%s",
4459 CHECK_FLAG(flags, ZEBRA_FLAG_ALLOW_RECURSION) ? "Recursion "
4460 : "",
4461 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE) ? "Self " : "",
4462 CHECK_FLAG(flags, ZEBRA_FLAG_IBGP) ? "iBGP " : "",
4463 CHECK_FLAG(flags, ZEBRA_FLAG_SELECTED) ? "Selected " : "",
4464 CHECK_FLAG(flags, ZEBRA_FLAG_FIB_OVERRIDE) ? "Override " : "",
4465 CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE) ? "Evpn " : "",
4466 CHECK_FLAG(flags, ZEBRA_FLAG_RR_USE_DISTANCE) ? "RR Distance "
4467 : "",
4468 CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED) ? "Trapped " : "",
4469 CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED) ? "Offloaded " : "",
4470 CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED) ? "Offload Failed "
4471 : "");
4472 return buf;
4473 }
4474
4475 char *zclient_evpn_dump_macip_flags(uint8_t flags, char *buf, size_t len)
4476 {
4477 if (flags == 0) {
4478 snprintfrr(buf, len, "None ");
4479 return buf;
4480 }
4481
4482 snprintfrr(
4483 buf, len, "%s%s%s%s%s%s%s",
4484 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? "Sticky MAC " : "",
4485 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? "Gateway MAC " : "",
4486 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? "Router "
4487 : "",
4488 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_OVERRIDE_FLAG) ? "Override "
4489 : "",
4490 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP) ? "SVI MAC " : "",
4491 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT) ? "Proxy "
4492 : "",
4493 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH) ? "Sync " : "");
4494
4495 return buf;
4496 }
4497
4498 static int zclient_neigh_ip_read_entry(struct stream *s, struct ipaddr *add)
4499 {
4500 uint8_t family;
4501
4502 STREAM_GETC(s, family);
4503 if (family != AF_INET && family != AF_INET6)
4504 return -1;
4505
4506 STREAM_GET(&add->ip.addr, s, family2addrsize(family));
4507 add->ipa_type = family;
4508 return 0;
4509 stream_failure:
4510 return -1;
4511 }
4512
4513 int zclient_neigh_ip_encode(struct stream *s, uint16_t cmd, union sockunion *in,
4514 union sockunion *out, struct interface *ifp,
4515 int ndm_state)
4516 {
4517 int ret = 0;
4518
4519 zclient_create_header(s, cmd, ifp->vrf_id);
4520 stream_putc(s, sockunion_family(in));
4521 stream_write(s, sockunion_get_addr(in), sockunion_get_addrlen(in));
4522 if (out && sockunion_family(out) != AF_UNSPEC) {
4523 stream_putc(s, sockunion_family(out));
4524 stream_write(s, sockunion_get_addr(out),
4525 sockunion_get_addrlen(out));
4526 } else
4527 stream_putc(s, AF_UNSPEC);
4528 stream_putl(s, ifp->ifindex);
4529 if (out)
4530 stream_putl(s, ndm_state);
4531 else
4532 stream_putl(s, ZEBRA_NEIGH_STATE_FAILED);
4533 return ret;
4534 }
4535
4536 int zclient_neigh_ip_decode(struct stream *s, struct zapi_neigh_ip *api)
4537 {
4538 int ret;
4539
4540 ret = zclient_neigh_ip_read_entry(s, &api->ip_in);
4541 if (ret < 0)
4542 return -1;
4543 zclient_neigh_ip_read_entry(s, &api->ip_out);
4544
4545 STREAM_GETL(s, api->index);
4546 STREAM_GETL(s, api->ndm_state);
4547 return 0;
4548 stream_failure:
4549 return -1;
4550 }
4551
4552 int zclient_send_zebra_gre_request(struct zclient *client,
4553 struct interface *ifp)
4554 {
4555 struct stream *s;
4556
4557 if (!client || client->sock < 0) {
4558 zlog_err("%s : zclient not ready", __func__);
4559 return -1;
4560 }
4561 s = client->obuf;
4562 stream_reset(s);
4563 zclient_create_header(s,
4564 ZEBRA_GRE_GET,
4565 ifp->vrf_id);
4566 stream_putl(s, ifp->ifindex);
4567 stream_putw_at(s, 0, stream_get_endp(s));
4568 zclient_send_message(client);
4569 return 0;
4570 }