]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zserv.c
zebra: fix some memory errors, scheduling bugs
[mirror_frr.git] / zebra / zserv.c
1 /*
2 * Zebra API server.
3 * Portions:
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
6 * et al.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24
25 /* clang-format off */
26 #include <errno.h> /* for errno */
27 #include <netinet/in.h> /* for sockaddr_in */
28 #include <stdint.h> /* for uint8_t */
29 #include <stdio.h> /* for snprintf */
30 #include <sys/socket.h> /* for sockaddr_storage, AF_UNIX, accept... */
31 #include <sys/stat.h> /* for umask, mode_t */
32 #include <sys/un.h> /* for sockaddr_un */
33 #include <time.h> /* for NULL, tm, gmtime, time_t */
34 #include <unistd.h> /* for close, unlink, ssize_t */
35
36 #include "lib/buffer.h" /* for BUFFER_EMPTY, BUFFER_ERROR, BUFFE... */
37 #include "lib/command.h" /* for vty, install_element, CMD_SUCCESS... */
38 #include "lib/hook.h" /* for DEFINE_HOOK, DEFINE_KOOH, hook_call */
39 #include "lib/linklist.h" /* for ALL_LIST_ELEMENTS_RO, ALL_LIST_EL... */
40 #include "lib/libfrr.h" /* for frr_zclient_addr */
41 #include "lib/log.h" /* for zlog_warn, zlog_debug, safe_strerror */
42 #include "lib/memory.h" /* for MTYPE_TMP, XCALLOC, XFREE */
43 #include "lib/monotime.h" /* for monotime, ONE_DAY_SECOND, ONE_WEE... */
44 #include "lib/network.h" /* for set_nonblocking */
45 #include "lib/privs.h" /* for zebra_privs_t, ZPRIVS_LOWER, ZPRI... */
46 #include "lib/route_types.h" /* for ZEBRA_ROUTE_MAX */
47 #include "lib/sockopt.h" /* for setsockopt_so_recvbuf, setsockopt... */
48 #include "lib/sockunion.h" /* for sockopt_reuseaddr, sockopt_reuseport */
49 #include "lib/stream.h" /* for STREAM_SIZE, stream (ptr only), ... */
50 #include "lib/thread.h" /* for thread (ptr only), THREAD_ARG, ... */
51 #include "lib/vrf.h" /* for vrf_info_lookup, VRF_DEFAULT */
52 #include "lib/vty.h" /* for vty_out, vty (ptr only) */
53 #include "lib/zassert.h" /* for assert */
54 #include "lib/zclient.h" /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
55
56 #include "zebra/debug.h" /* for various debugging macros */
57 #include "zebra/rib.h" /* for rib_score_proto */
58 #include "zebra/zapi_msg.h" /* for zserv_handle_commands */
59 #include "zebra/zebra_vrf.h" /* for zebra_vrf_lookup_by_id, zvrf */
60 #include "zebra/zserv.h" /* for zserv */
61 /* clang-format on */
62
63 /* Event list of zebra. */
64 enum event { ZEBRA_READ, ZEBRA_WRITE };
65 /* privileges */
66 extern struct zebra_privs_t zserv_privs;
67 /* post event into client */
68 static void zebra_event(struct zserv *client, enum event event);
69
70
71 /* Public interface --------------------------------------------------------- */
72
73 int zebra_server_send_message(struct zserv *client, struct stream *msg)
74 {
75 pthread_mutex_lock(&client->obuf_mtx);
76 {
77 stream_fifo_push(client->obuf_fifo, msg);
78 zebra_event(client, ZEBRA_WRITE);
79 }
80 pthread_mutex_unlock(&client->obuf_mtx);
81 return 0;
82 }
83
84 /* Hooks for client connect / disconnect */
85 DEFINE_HOOK(zapi_client_connect, (struct zserv *client), (client));
86 DEFINE_KOOH(zapi_client_close, (struct zserv *client), (client));
87
88 /*
89 * Deinitialize zebra client.
90 *
91 * - Deregister and deinitialize related internal resources
92 * - Gracefully close socket
93 * - Free associated resources
94 * - Free client structure
95 *
96 * This does *not* take any action on the struct thread * fields. These are
97 * managed by the owning pthread and any tasks associated with them must have
98 * been stopped prior to invoking this function.
99 */
100 static void zebra_client_free(struct zserv *client)
101 {
102 hook_call(zapi_client_close, client);
103
104 /*
105 * Ensure these have been nulled. This does not equate to the
106 * associated task(s) being scheduled or unscheduled on the client
107 * pthread's threadmaster.
108 */
109 assert(!client->t_read);
110 assert(!client->t_write);
111
112 /*
113 * Ensure these have been nulled. This does not equate to the
114 * associated task(s) being scheduled or unscheduled on the client
115 * pthread's threadmaster.
116 */
117 assert(!client->t_read);
118 assert(!client->t_write);
119
120 /* Close file descriptor. */
121 if (client->sock) {
122 unsigned long nroutes;
123
124 close(client->sock);
125 nroutes = rib_score_proto(client->proto, client->instance);
126 zlog_notice(
127 "client %d disconnected. %lu %s routes removed from the rib",
128 client->sock, nroutes,
129 zebra_route_string(client->proto));
130 client->sock = -1;
131 }
132
133 /* Free stream buffers. */
134 if (client->ibuf_work)
135 stream_free(client->ibuf_work);
136 if (client->obuf_work)
137 stream_free(client->obuf_work);
138 if (client->ibuf_fifo)
139 stream_fifo_free(client->ibuf_fifo);
140 if (client->obuf_fifo)
141 stream_fifo_free(client->obuf_fifo);
142 if (client->wb)
143 buffer_free(client->wb);
144
145 /* Free buffer mutexes */
146 pthread_mutex_destroy(&client->obuf_mtx);
147 pthread_mutex_destroy(&client->ibuf_mtx);
148
149 /* Free bitmaps. */
150 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
151 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
152 vrf_bitmap_free(client->redist[afi][i]);
153
154 vrf_bitmap_free(client->redist_default);
155 vrf_bitmap_free(client->ifinfo);
156 vrf_bitmap_free(client->ridinfo);
157
158 XFREE(MTYPE_TMP, client);
159 }
160
161 /*
162 * Finish closing a client.
163 *
164 * This task is scheduled by a ZAPI client pthread on the main pthread when it
165 * wants to stop itself. When this executes, the client connection should
166 * already have been closed. This task's responsibility is to gracefully
167 * terminate the client thread, update relevant internal datastructures and
168 * free any resources allocated by the main thread.
169 */
170 static int zebra_client_handle_close(struct thread *thread)
171 {
172 struct zserv *client = THREAD_ARG(thread);
173 frr_pthread_stop(client->pthread, NULL);
174 listnode_delete(zebrad.client_list, client);
175 zebra_client_free(client);
176 return 0;
177 }
178
179 /*
180 * Gracefully shut down a client connection.
181 *
182 * Cancel any pending tasks for the client's thread. Then schedule a task on the
183 * main thread to shut down the calling thread.
184 *
185 * Must be called from the client pthread, never the main thread.
186 */
187 static void zebra_client_close(struct zserv *client)
188 {
189 THREAD_OFF(client->t_read);
190 THREAD_OFF(client->t_write);
191 thread_add_event(zebrad.master, zebra_client_handle_close, client, 0,
192 NULL);
193 }
194
195 /* Make new client. */
196 static void zebra_client_create(int sock)
197 {
198 struct zserv *client;
199 int i;
200 afi_t afi;
201
202 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
203
204 /* Make client input/output buffer. */
205 client->sock = sock;
206 client->ibuf_fifo = stream_fifo_new();
207 client->obuf_fifo = stream_fifo_new();
208 client->ibuf_work = stream_new(ZEBRA_MAX_PACKET_SIZ);
209 client->obuf_work = stream_new(ZEBRA_MAX_PACKET_SIZ);
210 pthread_mutex_init(&client->ibuf_mtx, NULL);
211 pthread_mutex_init(&client->obuf_mtx, NULL);
212 client->wb = buffer_new(0);
213
214 /* Set table number. */
215 client->rtm_table = zebrad.rtm_table_default;
216
217 client->connect_time = monotime(NULL);
218 /* Initialize flags */
219 for (afi = AFI_IP; afi < AFI_MAX; afi++)
220 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
221 client->redist[afi][i] = vrf_bitmap_init();
222 client->redist_default = vrf_bitmap_init();
223 client->ifinfo = vrf_bitmap_init();
224 client->ridinfo = vrf_bitmap_init();
225
226 /* by default, it's not a synchronous client */
227 client->is_synchronous = 0;
228
229 /* Add this client to linked list. */
230 listnode_add(zebrad.client_list, client);
231
232 struct frr_pthread_attr zclient_pthr_attrs = {
233 .id = frr_pthread_get_id(),
234 .start = frr_pthread_attr_default.start,
235 .stop = frr_pthread_attr_default.stop
236 };
237 client->pthread = frr_pthread_new(&zclient_pthr_attrs, "Zebra API client thread");
238
239 zebra_vrf_update_all(client);
240
241 /* start read loop */
242 zebra_event(client, ZEBRA_READ);
243
244 /* call callbacks */
245 hook_call(zapi_client_connect, client);
246
247 /* start pthread */
248 frr_pthread_run(client->pthread, NULL);
249 }
250
251 /*
252 * Log zapi message to zlog.
253 *
254 * errmsg (optional)
255 * Debugging message
256 *
257 * msg
258 * The message
259 *
260 * hdr (optional)
261 * The message header
262 */
263 static void zserv_log_message(const char *errmsg, struct stream *msg,
264 struct zmsghdr *hdr)
265 {
266 zlog_debug("Rx'd ZAPI message");
267 if (errmsg)
268 zlog_debug("%s", errmsg);
269 if (hdr) {
270 zlog_debug(" Length: %d", hdr->length);
271 zlog_debug("Command: %s", zserv_command_string(hdr->command));
272 zlog_debug(" VRF: %u", hdr->vrf_id);
273 }
274 zlog_hexdump(msg->data, STREAM_READABLE(msg));
275 }
276
277 static int zserv_flush_data(struct thread *thread)
278 {
279 struct zserv *client = THREAD_ARG(thread);
280
281 client->t_write = NULL;
282 switch (buffer_flush_available(client->wb, client->sock)) {
283 case BUFFER_ERROR:
284 zlog_warn(
285 "%s: buffer_flush_available failed on zserv client fd %d, closing",
286 __func__, client->sock);
287 zebra_client_close(client);
288 client = NULL;
289 break;
290 case BUFFER_PENDING:
291 client->t_write = NULL;
292 thread_add_write(client->pthread->master, zserv_flush_data, client,
293 client->sock, &client->t_write);
294 break;
295 case BUFFER_EMPTY:
296 break;
297 }
298
299 if (client)
300 client->last_write_time = monotime(NULL);
301 return 0;
302 }
303
304 /*
305 * Write a single packet.
306 */
307 static int zserv_write(struct thread *thread)
308 {
309 struct zserv *client = THREAD_ARG(thread);
310 struct stream *msg;
311 int writerv;
312
313 if (client->is_synchronous)
314 return 0;
315
316 pthread_mutex_lock(&client->obuf_mtx);
317 {
318 msg = stream_fifo_pop(client->obuf_fifo);
319 }
320 pthread_mutex_unlock(&client->obuf_mtx);
321
322 stream_set_getp(msg, 0);
323 client->last_write_cmd = stream_getw_from(msg, 6);
324
325 writerv = buffer_write(client->wb, client->sock, STREAM_DATA(msg),
326 stream_get_endp(msg));
327
328 stream_free(msg);
329
330 switch (writerv) {
331 case BUFFER_ERROR:
332 zlog_warn("%s: buffer_write failed to ZAPI client %s [fd = %d]",
333 __func__, zebra_route_string(client->proto),
334 client->sock);
335 zlog_warn("%s: closing connection to %s", __func__,
336 zebra_route_string(client->proto));
337 zebra_client_close(client);
338 return -1;
339 case BUFFER_PENDING:
340 thread_add_write(client->pthread->master, zserv_flush_data,
341 client, client->sock, &client->t_write);
342 break;
343 case BUFFER_EMPTY:
344 break;
345 }
346
347 pthread_mutex_lock(&client->obuf_mtx);
348 {
349 if (client->obuf_fifo->count)
350 zebra_event(client, ZEBRA_WRITE);
351 }
352 pthread_mutex_unlock(&client->obuf_mtx);
353
354 client->last_write_time = monotime(NULL);
355 return 0;
356 }
357
358 #if defined(HANDLE_ZAPI_FUZZING)
359 static void zserv_write_incoming(struct stream *orig, uint16_t command)
360 {
361 char fname[MAXPATHLEN];
362 struct stream *copy;
363 int fd = -1;
364
365 copy = stream_dup(orig);
366 stream_set_getp(copy, 0);
367
368 zserv_privs.change(ZPRIVS_RAISE);
369 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
370 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
371 stream_flush(copy, fd);
372 close(fd);
373 zserv_privs.change(ZPRIVS_LOWER);
374 stream_free(copy);
375 }
376 #endif
377
378 /*
379 * Read and process messages from a client.
380 *
381 * This task runs on the main pthread. It is scheduled by client pthreads when
382 * they have new messages available on their input queues. The client is passed
383 * as the task argument.
384 *
385 * Each message is popped off the client's input queue and the action associated
386 * with the message is executed. This proceeds until there are no more messages,
387 * an error occurs, or the processing limit is reached. In the last case, this
388 * task reschedules itself.
389 */
390 static int zserv_process_messages(struct thread *thread)
391 {
392 struct zserv *client = THREAD_ARG(thread);
393 struct zebra_vrf *zvrf;
394 struct zmsghdr hdr;
395 struct stream *msg;
396 bool hdrvalid;
397
398 int p2p = zebrad.packets_to_process;
399
400 do {
401 pthread_mutex_lock(&client->ibuf_mtx);
402 {
403 msg = stream_fifo_pop(client->ibuf_fifo);
404 }
405 pthread_mutex_unlock(&client->ibuf_mtx);
406
407 /* break if out of messages */
408 if (!msg)
409 continue;
410
411 /* read & check header */
412 hdrvalid = zapi_parse_header(msg, &hdr);
413 if (!hdrvalid && IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) {
414 const char *emsg = "Message has corrupt header";
415 zserv_log_message(emsg, msg, NULL);
416 }
417 if (!hdrvalid)
418 continue;
419
420 hdr.length -= ZEBRA_HEADER_SIZE;
421 /* lookup vrf */
422 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
423 if (!zvrf && IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) {
424 const char *emsg = "Message specifies unknown VRF";
425 zserv_log_message(emsg, msg, &hdr);
426 }
427 if (!zvrf)
428 continue;
429
430 /* process commands */
431 zserv_handle_commands(client, &hdr, msg, zvrf);
432
433 } while (msg && --p2p);
434
435 /* reschedule self if necessary */
436 pthread_mutex_lock(&client->ibuf_mtx);
437 {
438 if (client->ibuf_fifo->count)
439 thread_add_event(zebrad.master, &zserv_process_messages,
440 client, 0, NULL);
441 }
442 pthread_mutex_unlock(&client->ibuf_mtx);
443
444 return 0;
445 }
446
447 /*
448 * Read and process data from a client socket.
449 *
450 * The responsibilities here are to read raw data from the client socket,
451 * validate the header, encapsulate it into a single stream object, push it
452 * onto the input queue and then notify the main thread that there is new data
453 * available.
454 *
455 * This function first looks for any data in the client structure's working
456 * input buffer. If data is present, it is assumed that reading stopped in a
457 * previous invocation of this task and needs to be resumed to finish a message.
458 * Otherwise, the socket data stream is assumed to be at the beginning of a new
459 * ZAPI message (specifically at the header). The header is read and validated.
460 * If the header passed validation then the length field found in the header is
461 * used to compute the total length of the message. That much data is read (but
462 * not inspected), appended to the header, placed into a stream and pushed onto
463 * the client's input queue. A task is then scheduled on the main thread to
464 * process the client's input queue. Finally, if all of this was successful,
465 * this task reschedules itself.
466 *
467 * Any failure in any of these actions is handled by terminating the client.
468 */
469 static int zserv_read(struct thread *thread)
470 {
471 int sock;
472 struct zserv *client;
473 size_t already;
474 #if defined(HANDLE_ZAPI_FUZZING)
475 int p2p = 1;
476 #else
477 int p2p = zebrad.packets_to_process;
478 #endif
479 sock = THREAD_FD(thread);
480 client = THREAD_ARG(thread);
481
482 while (p2p--) {
483 struct zmsghdr hdr;
484 ssize_t nb;
485 bool hdrvalid;
486 char errmsg[256];
487
488 already = stream_get_endp(client->ibuf_work);
489
490 /* Read length and command (if we don't have it already). */
491 if (already < ZEBRA_HEADER_SIZE) {
492 nb = stream_read_try(client->ibuf_work, sock,
493 ZEBRA_HEADER_SIZE - already);
494 if ((nb == 0 || nb == -1) && IS_ZEBRA_DEBUG_EVENT)
495 zlog_debug("connection closed socket [%d]",
496 sock);
497 if ((nb == 0 || nb == -1))
498 goto zread_fail;
499 if (nb != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
500 /* Try again later. */
501 break;
502 }
503 already = ZEBRA_HEADER_SIZE;
504 }
505
506 /* Reset to read from the beginning of the incoming packet. */
507 stream_set_getp(client->ibuf_work, 0);
508
509 /* Fetch header values */
510 hdrvalid = zapi_parse_header(client->ibuf_work, &hdr);
511
512 if (!hdrvalid) {
513 snprintf(errmsg, sizeof(errmsg),
514 "%s: Message has corrupt header", __func__);
515 zserv_log_message(errmsg, client->ibuf_work, NULL);
516 goto zread_fail;
517 }
518
519 /* Validate header */
520 if (hdr.marker != ZEBRA_HEADER_MARKER
521 || hdr.version != ZSERV_VERSION) {
522 snprintf(
523 errmsg, sizeof(errmsg),
524 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
525 __func__, sock, hdr.marker, hdr.version);
526 zserv_log_message(errmsg, client->ibuf_work, &hdr);
527 goto zread_fail;
528 }
529 if (hdr.length < ZEBRA_HEADER_SIZE) {
530 snprintf(
531 errmsg, sizeof(errmsg),
532 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
533 __func__, sock, hdr.length, ZEBRA_HEADER_SIZE);
534 zserv_log_message(errmsg, client->ibuf_work, &hdr);
535 goto zread_fail;
536 }
537 if (hdr.length > STREAM_SIZE(client->ibuf_work)) {
538 snprintf(
539 errmsg, sizeof(errmsg),
540 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
541 __func__, sock, hdr.length,
542 (unsigned long)STREAM_SIZE(client->ibuf_work));
543 goto zread_fail;
544 }
545
546 /* Read rest of data. */
547 if (already < hdr.length) {
548 nb = stream_read_try(client->ibuf_work, sock,
549 hdr.length - already);
550 if ((nb == 0 || nb == -1) && IS_ZEBRA_DEBUG_EVENT)
551 zlog_debug(
552 "connection closed [%d] when reading zebra data",
553 sock);
554 if ((nb == 0 || nb == -1))
555 goto zread_fail;
556 if (nb != (ssize_t)(hdr.length - already)) {
557 /* Try again later. */
558 break;
559 }
560 }
561
562 #if defined(HANDLE_ZAPI_FUZZING)
563 zserv_write_incoming(client->ibuf_work, command);
564 #endif
565
566 /* Debug packet information. */
567 if (IS_ZEBRA_DEBUG_EVENT)
568 zlog_debug("zebra message comes from socket [%d]",
569 sock);
570
571 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
572 zserv_log_message(NULL, client->ibuf_work, &hdr);
573
574 client->last_read_time = monotime(NULL);
575 client->last_read_cmd = hdr.command;
576
577 stream_set_getp(client->ibuf_work, 0);
578 struct stream *msg = stream_dup(client->ibuf_work);
579
580 pthread_mutex_lock(&client->ibuf_mtx);
581 {
582 stream_fifo_push(client->ibuf_fifo, msg);
583 }
584 pthread_mutex_unlock(&client->ibuf_mtx);
585
586 stream_reset(client->ibuf_work);
587 }
588
589 if (IS_ZEBRA_DEBUG_PACKET)
590 zlog_debug("Read %d packets",
591 zebrad.packets_to_process - p2p);
592
593 /* Schedule job to process those packets */
594 thread_add_event(zebrad.master, &zserv_process_messages, client, 0,
595 NULL);
596
597 /* Reschedule ourselves */
598 zebra_event(client, ZEBRA_READ);
599
600 return 0;
601
602 zread_fail:
603 zebra_client_close(client);
604 return -1;
605 }
606
607 static void zebra_event(struct zserv *client, enum event event)
608 {
609 switch (event) {
610 case ZEBRA_READ:
611 thread_add_read(client->pthread->master, zserv_read, client,
612 client->sock, &client->t_read);
613 break;
614 case ZEBRA_WRITE:
615 thread_add_write(client->pthread->master, zserv_write, client,
616 client->sock, &client->t_write);
617 break;
618 }
619 }
620
621 /* Main thread lifecycle ----------------------------------------------------*/
622
623 /* Accept code of zebra server socket. */
624 static int zebra_accept(struct thread *thread)
625 {
626 int accept_sock;
627 int client_sock;
628 struct sockaddr_in client;
629 socklen_t len;
630
631 accept_sock = THREAD_FD(thread);
632
633 /* Reregister myself. */
634 thread_add_read(zebrad.master, zebra_accept, NULL, accept_sock, NULL);
635
636 len = sizeof(struct sockaddr_in);
637 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
638
639 if (client_sock < 0) {
640 zlog_warn("Can't accept zebra socket: %s",
641 safe_strerror(errno));
642 return -1;
643 }
644
645 /* Make client socket non-blocking. */
646 set_nonblocking(client_sock);
647
648 /* Create new zebra client. */
649 zebra_client_create(client_sock);
650
651 return 0;
652 }
653
654 /* Make zebra server socket, wiping any existing one (see bug #403). */
655 void zebra_zserv_socket_init(char *path)
656 {
657 int ret;
658 int sock;
659 mode_t old_mask;
660 struct sockaddr_storage sa;
661 socklen_t sa_len;
662
663 if (!frr_zclient_addr(&sa, &sa_len, path))
664 /* should be caught in zebra main() */
665 return;
666
667 /* Set umask */
668 old_mask = umask(0077);
669
670 /* Make UNIX domain socket. */
671 sock = socket(sa.ss_family, SOCK_STREAM, 0);
672 if (sock < 0) {
673 zlog_warn("Can't create zserv socket: %s",
674 safe_strerror(errno));
675 zlog_warn(
676 "zebra can't provide full functionality due to above error");
677 return;
678 }
679
680 if (sa.ss_family != AF_UNIX) {
681 sockopt_reuseaddr(sock);
682 sockopt_reuseport(sock);
683 } else {
684 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
685 if (suna->sun_path[0])
686 unlink(suna->sun_path);
687 }
688
689 zserv_privs.change(ZPRIVS_RAISE);
690 setsockopt_so_recvbuf(sock, 1048576);
691 setsockopt_so_sendbuf(sock, 1048576);
692 zserv_privs.change(ZPRIVS_LOWER);
693
694 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_RAISE))
695 zlog_err("Can't raise privileges");
696
697 ret = bind(sock, (struct sockaddr *)&sa, sa_len);
698 if (ret < 0) {
699 zlog_warn("Can't bind zserv socket on %s: %s", path,
700 safe_strerror(errno));
701 zlog_warn(
702 "zebra can't provide full functionality due to above error");
703 close(sock);
704 return;
705 }
706 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_LOWER))
707 zlog_err("Can't lower privileges");
708
709 ret = listen(sock, 5);
710 if (ret < 0) {
711 zlog_warn("Can't listen to zserv socket %s: %s", path,
712 safe_strerror(errno));
713 zlog_warn(
714 "zebra can't provide full functionality due to above error");
715 close(sock);
716 return;
717 }
718
719 umask(old_mask);
720
721 thread_add_read(zebrad.master, zebra_accept, NULL, sock, NULL);
722 }
723
724 #define ZEBRA_TIME_BUF 32
725 static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
726 {
727 struct tm *tm;
728 time_t now;
729
730 assert(buf != NULL);
731 assert(buflen >= ZEBRA_TIME_BUF);
732 assert(time1 != NULL);
733
734 if (!*time1) {
735 snprintf(buf, buflen, "never ");
736 return (buf);
737 }
738
739 now = monotime(NULL);
740 now -= *time1;
741 tm = gmtime(&now);
742
743 if (now < ONE_DAY_SECOND)
744 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
745 tm->tm_sec);
746 else if (now < ONE_WEEK_SECOND)
747 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
748 tm->tm_min);
749 else
750 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
751 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
752 return buf;
753 }
754
755 static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
756 {
757 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
758 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
759
760 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
761 if (client->instance)
762 vty_out(vty, " Instance: %d", client->instance);
763 vty_out(vty, "\n");
764
765 vty_out(vty, "------------------------ \n");
766 vty_out(vty, "FD: %d \n", client->sock);
767 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
768
769 vty_out(vty, "Connect Time: %s \n",
770 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
771 if (client->nh_reg_time) {
772 vty_out(vty, "Nexthop Registry Time: %s \n",
773 zserv_time_buf(&client->nh_reg_time, nhbuf,
774 ZEBRA_TIME_BUF));
775 if (client->nh_last_upd_time)
776 vty_out(vty, "Nexthop Last Update Time: %s \n",
777 zserv_time_buf(&client->nh_last_upd_time, mbuf,
778 ZEBRA_TIME_BUF));
779 else
780 vty_out(vty, "No Nexthop Update sent\n");
781 } else
782 vty_out(vty, "Not registered for Nexthop Updates\n");
783
784 vty_out(vty, "Last Msg Rx Time: %s \n",
785 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
786 vty_out(vty, "Last Msg Tx Time: %s \n",
787 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
788 if (client->last_read_time)
789 vty_out(vty, "Last Rcvd Cmd: %s \n",
790 zserv_command_string(client->last_read_cmd));
791 if (client->last_write_time)
792 vty_out(vty, "Last Sent Cmd: %s \n",
793 zserv_command_string(client->last_write_cmd));
794 vty_out(vty, "\n");
795
796 vty_out(vty, "Type Add Update Del \n");
797 vty_out(vty, "================================================== \n");
798 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
799 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
800 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
801 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
802 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
803 0, client->redist_v4_del_cnt);
804 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
805 0, client->redist_v6_del_cnt);
806 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
807 client->ifdel_cnt);
808 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
809 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
810 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
811 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
812 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
813 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
814 vty_out(vty, "L3-VNI add notifications: %d\n", client->l3vniadd_cnt);
815 vty_out(vty, "L3-VNI delete notifications: %d\n", client->l3vnidel_cnt);
816 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
817 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
818
819 vty_out(vty, "\n");
820 return;
821 }
822
823 static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
824 {
825 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
826 char wbuf[ZEBRA_TIME_BUF];
827
828 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
829 zebra_route_string(client->proto),
830 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
831 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
832 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
833 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
834 client->v4_route_del_cnt,
835 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
836 client->v6_route_del_cnt);
837 }
838
839 struct zserv *zebra_find_client(uint8_t proto, unsigned short instance)
840 {
841 struct listnode *node, *nnode;
842 struct zserv *client;
843
844 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
845 if (client->proto == proto && client->instance == instance)
846 return client;
847 }
848
849 return NULL;
850 }
851
852 /* This command is for debugging purpose. */
853 DEFUN (show_zebra_client,
854 show_zebra_client_cmd,
855 "show zebra client",
856 SHOW_STR
857 ZEBRA_STR
858 "Client information\n")
859 {
860 struct listnode *node;
861 struct zserv *client;
862
863 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
864 zebra_show_client_detail(vty, client);
865
866 return CMD_SUCCESS;
867 }
868
869 /* This command is for debugging purpose. */
870 DEFUN (show_zebra_client_summary,
871 show_zebra_client_summary_cmd,
872 "show zebra client summary",
873 SHOW_STR
874 ZEBRA_STR
875 "Client information brief\n"
876 "Brief Summary\n")
877 {
878 struct listnode *node;
879 struct zserv *client;
880
881 vty_out(vty,
882 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
883 vty_out(vty,
884 "--------------------------------------------------------------------------------\n");
885
886 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
887 zebra_show_client_brief(vty, client);
888
889 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
890 return CMD_SUCCESS;
891 }
892
893 #if defined(HANDLE_ZAPI_FUZZING)
894 void zserv_read_file(char *input)
895 {
896 int fd;
897 struct zserv *client = NULL;
898 struct thread t;
899
900 zebra_client_create(-1);
901 client = zebrad.client_list->head->data;
902 t.arg = client;
903
904 fd = open(input, O_RDONLY | O_NONBLOCK);
905 t.u.fd = fd;
906
907 zebra_client_read(&t);
908
909 close(fd);
910 }
911 #endif
912
913 void zserv_init(void)
914 {
915 /* Client list init. */
916 zebrad.client_list = list_new();
917 zebrad.client_list->del = (void (*)(void *))zebra_client_free;
918
919 install_element(ENABLE_NODE, &show_zebra_client_cmd);
920 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
921 }