Coverage Report

Created: 2025-08-28 06:29

/src/frr/zebra/main.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/* zebra daemon main routine.
3
 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4
 */
5
6
#include <zebra.h>
7
8
#include <lib/version.h>
9
#include "getopt.h"
10
#include "command.h"
11
#include "frrevent.h"
12
#include "filter.h"
13
#include "memory.h"
14
#include "prefix.h"
15
#include "log.h"
16
#include "plist.h"
17
#include "privs.h"
18
#include "sigevent.h"
19
#include "vrf.h"
20
#include "libfrr.h"
21
#include "affinitymap.h"
22
#include "routemap.h"
23
#include "routing_nb.h"
24
25
#include "fuzz.h"
26
#include "frr_pthread.h"
27
28
#include "zebra/zebra_router.h"
29
#include "zebra/zebra_errors.h"
30
#include "zebra/rib.h"
31
#include "zebra/zserv.h"
32
#include "zebra/debug.h"
33
#include "zebra/router-id.h"
34
#include "zebra/irdp.h"
35
#include "zebra/rtadv.h"
36
#include "zebra/zebra_ptm.h"
37
#include "zebra/zebra_ns.h"
38
#include "zebra/redistribute.h"
39
#include "zebra/zebra_mpls.h"
40
#include "zebra/label_manager.h"
41
#include "zebra/zebra_netns_notify.h"
42
#include "zebra/zebra_rnh.h"
43
#include "zebra/zebra_pbr.h"
44
#include "zebra/zebra_vxlan.h"
45
#include "zebra/zebra_routemap.h"
46
#include "zebra/zebra_nb.h"
47
#include "zebra/zebra_opaque.h"
48
#include "zebra/zebra_srte.h"
49
#include "zebra/zebra_srv6.h"
50
#include "zebra/zebra_srv6_vty.h"
51
#include "zebra/zapi_msg.h"
52
53
#ifdef FUZZING
54
#include "zebra/kernel_netlink.h"
55
#endif /* FUZZING */
56
57
#define ZEBRA_PTM_SUPPORT
58
59
/* process id. */
60
pid_t pid;
61
62
/* Pacify zclient.o in libfrr, which expects this variable. */
63
struct event_loop *master;
64
65
/* Route retain mode flag. */
66
int retain_mode = 0;
67
68
int graceful_restart;
69
70
bool v6_rr_semantics = false;
71
72
/* Receive buffer size for kernel control sockets */
73
#define RCVBUFSIZE_MIN 4194304
74
#ifdef HAVE_NETLINK
75
uint32_t rcvbufsize = RCVBUFSIZE_MIN;
76
#else
77
uint32_t rcvbufsize = 128 * 1024;
78
#endif
79
80
#define OPTION_V6_RR_SEMANTICS 2000
81
#define OPTION_ASIC_OFFLOAD    2001
82
83
/* Command line options. */
84
const struct option longopts[] = {
85
  {"batch", no_argument, NULL, 'b'},
86
  {"allow_delete", no_argument, NULL, 'a'},
87
  {"socket", required_argument, NULL, 'z'},
88
  {"ecmp", required_argument, NULL, 'e'},
89
  {"retain", no_argument, NULL, 'r'},
90
  {"graceful_restart", required_argument, NULL, 'K'},
91
  {"asic-offload", optional_argument, NULL, OPTION_ASIC_OFFLOAD},
92
#ifdef HAVE_NETLINK
93
  {"vrfwnetns", no_argument, NULL, 'n'},
94
  {"nl-bufsize", required_argument, NULL, 's'},
95
  {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
96
#endif /* HAVE_NETLINK */
97
  {0}};
98
99
zebra_capabilities_t _caps_p[] = {ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN,
100
          ZCAP_NET_RAW,
101
#ifdef HAVE_DPDK
102
          ZCAP_IPC_LOCK,  ZCAP_READ_SEARCH,
103
          ZCAP_SYS_RAWIO
104
#endif
105
};
106
107
/* zebra privileges to run with */
108
struct zebra_privs_t zserv_privs = {
109
#if defined(FRR_USER) && defined(FRR_GROUP)
110
  .user = FRR_USER,
111
  .group = FRR_GROUP,
112
#endif
113
#ifdef VTY_GROUP
114
  .vty_group = VTY_GROUP,
115
#endif
116
  .caps_p = _caps_p,
117
  .cap_num_p = array_size(_caps_p),
118
  .cap_num_i = 0};
119
120
/* SIGHUP handler. */
121
static void sighup(void)
122
0
{
123
0
  zlog_info("SIGHUP received");
124
125
  /* Reload of config file. */
126
0
  ;
127
0
}
128
129
/* SIGINT handler. */
130
static void sigint(void)
131
0
{
132
0
  struct vrf *vrf;
133
0
  struct zebra_vrf *zvrf;
134
0
  struct listnode *ln, *nn;
135
0
  struct zserv *client;
136
0
  static bool sigint_done;
137
138
0
  if (sigint_done)
139
0
    return;
140
141
0
  sigint_done = true;
142
143
0
  zlog_notice("Terminating on signal");
144
145
0
  atomic_store_explicit(&zrouter.in_shutdown, true,
146
0
            memory_order_relaxed);
147
148
  /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
149
0
  rtadv_stop_ra_all();
150
151
0
  frr_early_fini();
152
153
  /* Stop the opaque module pthread */
154
0
  zebra_opaque_stop();
155
156
0
  zebra_dplane_pre_finish();
157
158
  /* Clean up GR related info. */
159
0
  zebra_gr_stale_client_cleanup(zrouter.stale_client_list);
160
0
  list_delete_all_node(zrouter.stale_client_list);
161
162
  /* Clean up zapi clients and server module */
163
0
  for (ALL_LIST_ELEMENTS(zrouter.client_list, ln, nn, client))
164
0
    zserv_close_client(client);
165
166
0
  zserv_close();
167
0
  list_delete_all_node(zrouter.client_list);
168
169
  /* Once all the zclients are cleaned up, clean up the opaque module */
170
0
  zebra_opaque_finish();
171
172
0
  zebra_ptm_finish();
173
174
0
  if (retain_mode) {
175
0
    zebra_nhg_mark_keep();
176
0
    RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
177
0
      zvrf = vrf->info;
178
0
      if (zvrf)
179
0
        SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
180
0
    }
181
0
  }
182
183
0
  if (zrouter.lsp_process_q)
184
0
    work_queue_free_and_null(&zrouter.lsp_process_q);
185
186
0
  access_list_reset();
187
0
  prefix_list_reset();
188
  /*
189
   * zebra_routemap_finish will
190
   * 1 set rmap upd timer to 0 so that rmap update wont be scheduled again
191
   * 2 Put off the rmap update thread
192
   * 3 route_map_finish
193
   */
194
0
  zebra_routemap_finish();
195
196
0
  rib_update_finish();
197
198
0
  list_delete(&zrouter.client_list);
199
200
  /* Indicate that all new dplane work has been enqueued. When that
201
   * work is complete, the dataplane will enqueue an event
202
   * with the 'finalize' function.
203
   */
204
0
  zebra_dplane_finish();
205
0
}
206
207
/*
208
 * Final shutdown step for the zebra main thread. This is run after all
209
 * async update processing has completed.
210
 */
211
void zebra_finalize(struct event *dummy)
212
0
{
213
0
  zlog_info("Zebra final shutdown");
214
215
0
  vrf_terminate();
216
217
  /*
218
   * Stop dplane thread and finish any cleanup
219
   * This is before the zebra_ns_early_shutdown call
220
   * because sockets that the dplane depends on are closed
221
   * in those functions
222
   */
223
0
  zebra_dplane_shutdown();
224
225
0
  ns_walk_func(zebra_ns_early_shutdown, NULL, NULL);
226
0
  zebra_ns_notify_close();
227
228
  /* Final shutdown of ns resources */
229
0
  ns_walk_func(zebra_ns_final_shutdown, NULL, NULL);
230
231
0
  zebra_router_terminate();
232
233
0
  ns_terminate();
234
0
  frr_fini();
235
0
  exit(0);
236
0
}
237
238
/* SIGUSR1 handler. */
239
static void sigusr1(void)
240
0
{
241
0
  zlog_rotate();
242
0
}
243
244
struct frr_signal_t zebra_signals[] = {
245
  {
246
    .signal = SIGHUP,
247
    .handler = &sighup,
248
  },
249
  {
250
    .signal = SIGUSR1,
251
    .handler = &sigusr1,
252
  },
253
  {
254
    .signal = SIGINT,
255
    .handler = &sigint,
256
  },
257
  {
258
    .signal = SIGTERM,
259
    .handler = &sigint,
260
  },
261
};
262
263
/* clang-format off */
264
static const struct frr_yang_module_info *const zebra_yang_modules[] = {
265
  &frr_filter_info,
266
  &frr_interface_info,
267
  &frr_route_map_info,
268
  &frr_zebra_info,
269
  &frr_vrf_info,
270
  &frr_routing_info,
271
  &frr_affinity_map_info,
272
  &frr_zebra_route_map_info,
273
};
274
/* clang-format on */
275
276
FRR_DAEMON_INFO(
277
  zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
278
279
  .proghelp =
280
    "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
281
282
  .signals = zebra_signals, .n_signals = array_size(zebra_signals),
283
284
  .privs = &zserv_privs,
285
286
  .yang_modules = zebra_yang_modules,
287
  .n_yang_modules = array_size(zebra_yang_modules),
288
);
289
290
#ifdef FUZZING
291
292
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
293
294
static bool FuzzingInit(void)
295
0
{
296
0
  graceful_restart = 0;
297
0
  vrf_configure_backend(VRF_BACKEND_VRF_LITE);
298
299
0
  const char *name[] = { "zebra" };
300
301
0
  frr_preinit(&zebra_di, 1, (char **) name);
302
303
  /* Zebra related initialize. */
304
0
  zrouter.master = frr_init_fast();
305
306
0
  zebra_router_init(false, true);
307
0
  zserv_init();
308
0
  rib_init();
309
0
  zebra_if_init();
310
0
  zebra_debug_init();
311
0
  router_id_cmd_init();
312
0
  zebra_ns_init();
313
0
  zebra_vty_init();
314
0
  access_list_init();
315
0
  prefix_list_init();
316
0
  zebra_mpls_init();
317
0
  zebra_mpls_vty_init();
318
0
  zebra_pw_vty_init();
319
0
  zebra_pbr_init();
320
0
  zrouter.startup_time = monotime(NULL);
321
0
  label_manager_init();
322
0
  zebra_rnh_init();
323
0
  zebra_evpn_init();
324
0
  zebra_error_init();
325
0
  frr_pthread_init();
326
327
0
  return true;
328
0
}
329
330
#ifndef FUZZING_LIBFUZZER
331
static struct zserv *FuzzingZc;
332
#endif /* FUZZING_LIBFUZZER */
333
334
static struct stream_fifo *fifo;
335
336
static bool FuzzingInitialized;
337
338
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
339
{
340
  if (!FuzzingInitialized) {
341
    FuzzingInit();
342
    FuzzingInitialized = true;
343
    fifo = stream_fifo_new();
344
  }
345
346
  /*
347
   * In the AFL standalone case, the client will already be created for
348
   * us before __AFL_INIT() is called to speed things up. We can't pass
349
   * it as an argument because the function signature must match
350
   * libFuzzer's expectations.
351
   *
352
   * In the libFuzzer case, we need to create it each time.
353
   *
354
   * In both cases the client must be destroyed before we return..
355
   */
356
  struct zserv *zc;
357
#ifdef FUZZING_LIBFUZZER
358
  zc = zserv_client_create(69);
359
#else
360
  zc = FuzzingZc;
361
#endif /* FUZZING_LIBFUZZER */
362
363
364
#if (FUZZING_MODE == ZAPI_FUZZING)
365
  struct stream *s = stream_new(size + 1);
366
  stream_put(s, data, size);
367
  stream_fifo_push(fifo, s);
368
369
  zserv_handle_commands(zc, fifo);
370
#elif (FUZZING_MODE == NETLINK_FUZZING)
371
  netlink_fuzz(data, size);
372
#endif
373
374
done:
375
  zserv_close_client(zc);
376
377
  return 0;
378
}
379
#endif /* FUZZING */
380
381
#ifndef FUZZING_LIBFUZZER
382
383
CPP_NOTICE("Not using LibFuzzer, compiling in main symbol!")
384
385
/* Main startup routine. */
386
int main(int argc, char **argv)
387
{
388
  // int batch_mode = 0;
389
  char *zserv_path = NULL;
390
  struct sockaddr_storage dummy;
391
  socklen_t dummylen;
392
  bool asic_offload = false;
393
  bool notify_on_ack = true;
394
395
#ifdef FUZZING
396
  FuzzingInit();
397
  FuzzingZc = zserv_client_create(69);
398
399
#ifdef __AFL_HAVE_MANUAL_CONTROL
400
  __AFL_INIT();
401
#endif /* __AFL_HAVE_MANUAL_CONTROL */
402
403
  uint8_t *input = NULL;
404
  int r = frrfuzz_read_input(&input);
405
406
  int ret = LLVMFuzzerTestOneInput(input, r);
407
408
  if (r > 0 && input) {
409
    free(input);
410
  }
411
412
  return ret;
413
#endif /* FUZZING */
414
415
  graceful_restart = 0;
416
  vrf_configure_backend(VRF_BACKEND_VRF_LITE);
417
418
  frr_preinit(&zebra_di, argc, argv);
419
420
  frr_opt_add(
421
    "baz:e:rK:s:"
422
#ifdef HAVE_NETLINK
423
    "n"
424
#endif
425
    ,
426
    longopts,
427
    "  -b, --batch              Runs in batch mode\n"
428
    "  -a, --allow_delete       Allow other processes to delete zebra routes\n"
429
    "  -z, --socket             Set path of zebra socket\n"
430
    "  -e, --ecmp               Specify ECMP to use.\n"
431
    "  -r, --retain             When program terminates, retain added route by zebra.\n"
432
    "  -K, --graceful_restart   Graceful restart at the kernel level, timer in seconds for expiration\n"
433
    "  -A, --asic-offload       FRR is interacting with an asic underneath the linux kernel\n"
434
#ifdef HAVE_NETLINK
435
    "  -s, --nl-bufsize         Set netlink receive buffer size\n"
436
    "  -n, --vrfwnetns          Use NetNS as VRF backend\n"
437
    "      --v6-rr-semantics    Use v6 RR semantics\n"
438
#else
439
    "  -s,                      Set kernel socket receive buffer size\n"
440
#endif /* HAVE_NETLINK */
441
  );
442
443
  while (1) {
444
    int opt = frr_getopt(argc, argv, NULL);
445
446
    if (opt == EOF)
447
      break;
448
449
    switch (opt) {
450
    case 0:
451
      break;
452
    case 'b':
453
      // batch_mode = 1;
454
      break;
455
    case 'a':
456
      zrouter.allow_delete = true;
457
      break;
458
    case 'e': {
459
      unsigned long int parsed_multipath =
460
        strtoul(optarg, NULL, 10);
461
      if (parsed_multipath == 0
462
          || parsed_multipath > MULTIPATH_NUM
463
          || parsed_multipath > UINT32_MAX) {
464
        flog_err(
465
          EC_ZEBRA_BAD_MULTIPATH_NUM,
466
          "Multipath Number specified must be less than %u and greater than 0",
467
          MULTIPATH_NUM);
468
        return 1;
469
      }
470
      zrouter.multipath_num = parsed_multipath;
471
      break;
472
    }
473
    case 'z':
474
      zserv_path = optarg;
475
      if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
476
        fprintf(stderr,
477
          "Invalid zserv socket path: %s\n",
478
          optarg);
479
        exit(1);
480
      }
481
      break;
482
    case 'r':
483
      retain_mode = 1;
484
      break;
485
    case 'K':
486
      graceful_restart = atoi(optarg);
487
      break;
488
    case 's':
489
      rcvbufsize = atoi(optarg);
490
      if (rcvbufsize < RCVBUFSIZE_MIN)
491
        fprintf(stderr,
492
          "Rcvbufsize is smaller than recommended value: %d\n",
493
          RCVBUFSIZE_MIN);
494
      break;
495
#ifdef HAVE_NETLINK
496
    case 'n':
497
      vrf_configure_backend(VRF_BACKEND_NETNS);
498
      break;
499
    case OPTION_V6_RR_SEMANTICS:
500
      v6_rr_semantics = true;
501
      break;
502
    case OPTION_ASIC_OFFLOAD:
503
      if (!strcmp(optarg, "notify_on_offload"))
504
        notify_on_ack = false;
505
      if (!strcmp(optarg, "notify_on_ack"))
506
        notify_on_ack = true;
507
      asic_offload = true;
508
      break;
509
#endif /* HAVE_NETLINK */
510
    default:
511
      frr_help_exit(1);
512
    }
513
  }
514
515
  zrouter.master = frr_init();
516
517
  /* Zebra related initialize. */
518
  zebra_router_init(asic_offload, notify_on_ack);
519
  zserv_init();
520
  rib_init();
521
  zebra_if_init();
522
  zebra_debug_init();
523
524
  /*
525
   * Initialize NS( and implicitly the VRF module), and make kernel
526
   * routing socket. */
527
  zebra_ns_init();
528
  router_id_cmd_init();
529
  zebra_vty_init();
530
  access_list_init();
531
  prefix_list_init();
532
  rtadv_cmd_init();
533
/* PTM socket */
534
#ifdef ZEBRA_PTM_SUPPORT
535
  zebra_ptm_init();
536
#endif
537
538
  zebra_mpls_init();
539
  zebra_mpls_vty_init();
540
  zebra_pw_vty_init();
541
  zebra_pbr_init();
542
  zebra_opaque_init();
543
  zebra_srte_init();
544
  zebra_srv6_init();
545
  zebra_srv6_vty_init();
546
547
  /* For debug purpose. */
548
  /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
549
550
  /* Process the configuration file. Among other configuration
551
  *  directives we can meet those installing static routes. Such
552
  *  requests will not be executed immediately, but queued in
553
  *  zebra->ribq structure until we enter the main execution loop.
554
  *  The notifications from kernel will show originating PID equal
555
  *  to that after daemon() completes (if ever called).
556
  */
557
  frr_config_fork();
558
559
  /* After we have successfully acquired the pidfile, we can be sure
560
  *  about being the only copy of zebra process, which is submitting
561
  *  changes to the FIB.
562
  *  Clean up zebra-originated routes. The requests will be sent to OS
563
  *  immediately, so originating PID in notifications from kernel
564
  *  will be equal to the current getpid(). To know about such routes,
565
  * we have to have route_read() called before.
566
  */
567
  zrouter.startup_time = monotime(NULL);
568
  event_add_timer(zrouter.master, rib_sweep_route, NULL, graceful_restart,
569
      &zrouter.sweeper);
570
571
  /* Needed for BSD routing socket. */
572
  pid = getpid();
573
574
  /* Start dataplane system */
575
  zebra_dplane_start();
576
577
  /* Start the ted module, before zserv */
578
  zebra_opaque_start();
579
580
  /* Start Zebra API server */
581
  zserv_start(zserv_path);
582
583
  /* Init label manager */
584
  label_manager_init();
585
586
  /* RNH init */
587
  zebra_rnh_init();
588
589
  /* Config handler Init */
590
  zebra_evpn_init();
591
592
  /* Error init */
593
  zebra_error_init();
594
595
  frr_run(zrouter.master);
596
597
  /* Not reached... */
598
  return 0;
599
}
600
#endif /* FUZZING_LIBFUZZER */