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