1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include <lib/version.h>
37 #include "zebra/rib.h"
38 #include "zebra/zserv.h"
39 #include "zebra/debug.h"
40 #include "zebra/router-id.h"
41 #include "zebra/irdp.h"
42 #include "zebra/rtadv.h"
43 #include "zebra/zebra_fpm.h"
46 struct zebra_t zebrad =
48 .rtm_table_default = 0,
54 /* Pacify zclient.o in libzebra, which expects this variable. */
55 struct thread_master *master;
57 /* Route retain mode flag. */
60 /* Don't delete kernel route. */
61 int keep_kernel_mode = 0;
64 /* Receive buffer size for netlink socket */
65 u_int32_t nl_rcvbufsize = 0;
66 #endif /* HAVE_NETLINK */
68 /* Command line options. */
69 struct option longopts[] =
71 { "batch", no_argument, NULL, 'b'},
72 { "daemon", no_argument, NULL, 'd'},
73 { "keep_kernel", no_argument, NULL, 'k'},
74 { "fpm_format", required_argument, NULL, 'F'},
75 { "config_file", required_argument, NULL, 'f'},
76 { "pid_file", required_argument, NULL, 'i'},
77 { "socket", required_argument, NULL, 'z'},
78 { "help", no_argument, NULL, 'h'},
79 { "vty_addr", required_argument, NULL, 'A'},
80 { "vty_port", required_argument, NULL, 'P'},
81 { "retain", no_argument, NULL, 'r'},
82 { "dryrun", no_argument, NULL, 'C'},
84 { "nl-bufsize", required_argument, NULL, 's'},
85 #endif /* HAVE_NETLINK */
86 { "user", required_argument, NULL, 'u'},
87 { "group", required_argument, NULL, 'g'},
88 { "version", no_argument, NULL, 'v'},
92 zebra_capabilities_t _caps_p [] =
99 /* zebra privileges to run with */
100 struct zebra_privs_t zserv_privs =
102 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
104 .group = QUAGGA_GROUP,
107 .vty_group = VTY_GROUP,
110 .cap_num_p = array_size(_caps_p),
114 /* Default configuration file path. */
115 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
117 /* Process ID saved for use by init system */
118 const char *pid_file = PATH_ZEBRA_PID;
120 /* Help information display. */
122 usage (char *progname, int status)
125 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
128 printf ("Usage : %s [OPTION...]\n\n"\
129 "Daemon which manages kernel routing table management and "\
130 "redistribution between different routing protocols.\n\n"\
131 "-b, --batch Runs in batch mode\n"\
132 "-d, --daemon Runs in daemon mode\n"\
133 "-f, --config_file Set configuration file name\n"\
134 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
135 "-i, --pid_file Set process identifier file name\n"\
136 "-z, --socket Set path of zebra socket\n"\
137 "-k, --keep_kernel Don't delete old routes which installed by "\
139 "-C, --dryrun Check configuration for validity and exit\n"\
140 "-A, --vty_addr Set vty's bind address\n"\
141 "-P, --vty_port Set vty's port number\n"\
142 "-r, --retain When program terminates, retain added route "\
144 "-u, --user User to run as\n"\
145 "-g, --group Group to run as\n", progname);
147 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
148 #endif /* HAVE_NETLINK */
149 printf ("-v, --version Print program version\n"\
150 "-h, --help Display this help and exit\n"\
152 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
158 /* SIGHUP handler. */
162 zlog_info ("SIGHUP received");
164 /* Reload of config file. */
168 /* SIGINT handler. */
172 zlog_notice ("Terminating on signal");
183 /* SIGUSR1 handler. */
190 struct quagga_signal_t zebra_signals[] =
210 /* Callback upon creating a new VRF. */
212 zebra_vrf_new (vrf_id_t vrf_id, void **info)
214 struct zebra_vrf *zvrf = *info;
218 zvrf = zebra_vrf_alloc (vrf_id);
219 *info = (void *)zvrf;
220 router_id_init (zvrf);
226 /* Callback upon enabling a VRF. */
228 zebra_vrf_enable (vrf_id_t vrf_id, void **info)
230 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
234 #if defined (HAVE_RTADV)
238 interface_list (zvrf);
244 /* Callback upon disabling a VRF. */
246 zebra_vrf_disable (vrf_id_t vrf_id, void **info)
248 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
249 struct listnode *list_node;
250 struct interface *ifp;
254 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
255 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
257 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp))
259 int operative = if_is_operative (ifp);
260 UNSET_FLAG (ifp->flags, IFF_UP);
265 #if defined (HAVE_RTADV)
266 rtadv_terminate (zvrf);
268 kernel_terminate (zvrf);
270 list_delete_all_node (zvrf->rid_all_sorted_list);
271 list_delete_all_node (zvrf->rid_lo_sorted_list);
276 /* Zebra VRF initialization. */
278 zebra_vrf_init (void)
280 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
281 vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable);
282 vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable);
286 /* Main startup routine. */
288 main (int argc, char **argv)
291 char *vty_addr = NULL;
292 int vty_port = ZEBRA_VTY_PORT;
296 char *config_file = NULL;
298 struct thread thread;
299 char *zserv_path = NULL;
300 char *fpm_format = NULL;
302 /* Set umask before anything for security */
305 /* preserve my name */
306 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
308 zlog_default = openzlog (progname, ZLOG_ZEBRA,
309 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
316 opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
318 opt = getopt_long (argc, argv, "bdkf:F:i:z:hA:P:ru:g:vC", longopts, 0);
319 #endif /* HAVE_NETLINK */
334 keep_kernel_mode = 1;
340 config_file = optarg;
355 /* Deal with atoi() returning 0 on failure, and zebra not
356 listening on zebra port... */
357 if (strcmp(optarg, "0") == 0)
362 vty_port = atoi (optarg);
363 if (vty_port <= 0 || vty_port > 0xffff)
364 vty_port = ZEBRA_VTY_PORT;
371 nl_rcvbufsize = atoi (optarg);
373 #endif /* HAVE_NETLINK */
375 zserv_privs.user = optarg;
378 zserv_privs.group = optarg;
381 print_version (progname);
393 /* Make master thread emulator. */
394 zebrad.master = thread_master_create ();
396 /* privs initialise */
397 zprivs_init (&zserv_privs);
399 /* Vty related initialize. */
400 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
402 vty_init (zebrad.master);
405 /* Zebra related initialize. */
410 router_id_cmd_init ();
414 #if defined (HAVE_RTADV)
421 /* For debug purpose. */
422 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
424 /* Initialize VRF module, and make kernel routing socket. */
429 #endif /* HAVE_SNMP */
432 zfpm_init (zebrad.master, 1, 0, fpm_format);
434 zfpm_init (zebrad.master, 0, 0, fpm_format);
437 /* Process the configuration file. Among other configuration
438 * directives we can meet those installing static routes. Such
439 * requests will not be executed immediately, but queued in
440 * zebra->ribq structure until we enter the main execution loop.
441 * The notifications from kernel will show originating PID equal
442 * to that after daemon() completes (if ever called).
444 vty_read_config (config_file, config_default);
446 /* Don't start execution if we are in dry-run mode */
450 /* Count up events for interfaces */
451 if_startup_count_up ();
456 /* Exit when zebra is working in batch mode. */
461 if (daemon_mode && daemon (0, 0) < 0)
463 zlog_err("Zebra daemon failed: %s", strerror(errno));
467 /* Output pid of zebra. */
468 pid_output (pid_file);
470 /* After we have successfully acquired the pidfile, we can be sure
471 * about being the only copy of zebra process, which is submitting
472 * changes to the FIB.
473 * Clean up zebra-originated routes. The requests will be sent to OS
474 * immediately, so originating PID in notifications from kernel
475 * will be equal to the current getpid(). To know about such routes,
476 * we have to have route_read() called before.
478 if (! keep_kernel_mode)
481 /* Needed for BSD routing socket. */
484 /* This must be done only after locking pidfile (bug #403). */
485 zebra_zserv_socket_init (zserv_path);
487 /* Make vty server socket. */
488 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
491 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
493 while (thread_fetch (zebrad.master, &thread))
494 thread_call (&thread);