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"
44 #include "zebra/zebra_ptm.h"
46 #define ZEBRA_PTM_SUPPORT
49 struct zebra_t zebrad
=
51 .rtm_table_default
= 0,
57 /* Pacify zclient.o in libzebra, which expects this variable. */
58 struct thread_master
*master
;
60 /* Route retain mode flag. */
63 /* Allow non-quagga entities to delete quagga routes */
66 /* Don't delete kernel route. */
67 int keep_kernel_mode
= 0;
70 /* Receive buffer size for netlink socket */
71 u_int32_t nl_rcvbufsize
= 4194304;
72 #endif /* HAVE_NETLINK */
74 /* Command line options. */
75 struct option longopts
[] =
77 { "batch", no_argument
, NULL
, 'b'},
78 { "daemon", no_argument
, NULL
, 'd'},
79 { "allow_delete", no_argument
, NULL
, 'a'},
80 { "keep_kernel", no_argument
, NULL
, 'k'},
81 { "config_file", required_argument
, NULL
, 'f'},
82 { "pid_file", required_argument
, NULL
, 'i'},
83 { "socket", required_argument
, NULL
, 'z'},
84 { "help", no_argument
, NULL
, 'h'},
85 { "vty_addr", required_argument
, NULL
, 'A'},
86 { "vty_port", required_argument
, NULL
, 'P'},
87 { "retain", no_argument
, NULL
, 'r'},
88 { "dryrun", no_argument
, NULL
, 'C'},
90 { "nl-bufsize", required_argument
, NULL
, 's'},
91 #endif /* HAVE_NETLINK */
92 { "user", required_argument
, NULL
, 'u'},
93 { "group", required_argument
, NULL
, 'g'},
94 { "version", no_argument
, NULL
, 'v'},
98 zebra_capabilities_t _caps_p
[] =
105 /* zebra privileges to run with */
106 struct zebra_privs_t zserv_privs
=
108 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
110 .group
= QUAGGA_GROUP
,
113 .vty_group
= VTY_GROUP
,
116 .cap_num_p
= array_size(_caps_p
),
120 /* Default configuration file path. */
121 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
123 /* Process ID saved for use by init system */
124 const char *pid_file
= PATH_ZEBRA_PID
;
126 /* Help information display. */
128 usage (char *progname
, int status
)
131 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
134 printf ("Usage : %s [OPTION...]\n\n"\
135 "Daemon which manages kernel routing table management and "\
136 "redistribution between different routing protocols.\n\n"\
137 "-b, --batch Runs in batch mode\n"\
138 "-d, --daemon Runs in daemon mode\n"\
139 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
140 "-f, --config_file Set configuration file name\n"\
141 "-i, --pid_file Set process identifier file name\n"\
142 "-z, --socket Set path of zebra socket\n"\
143 "-k, --keep_kernel Don't delete old routes which installed by "\
145 "-C, --dryrun Check configuration for validity and exit\n"\
146 "-A, --vty_addr Set vty's bind address\n"\
147 "-P, --vty_port Set vty's port number\n"\
148 "-r, --retain When program terminates, retain added route "\
150 "-u, --user User to run as\n"\
151 "-g, --group Group to run as\n", progname
);
153 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
154 #endif /* HAVE_NETLINK */
155 printf ("-v, --version Print program version\n"\
156 "-h, --help Display this help and exit\n"\
158 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
164 /* SIGHUP handler. */
168 zlog_info ("SIGHUP received");
170 /* Reload of config file. */
174 /* SIGINT handler. */
178 zlog_notice ("Terminating on signal");
190 /* SIGUSR1 handler. */
197 struct quagga_signal_t zebra_signals
[] =
217 /* Callback upon creating a new VRF. */
219 zebra_vrf_new (vrf_id_t vrf_id
, void **info
)
221 struct zebra_vrf
*zvrf
= *info
;
225 zvrf
= zebra_vrf_alloc (vrf_id
);
226 *info
= (void *)zvrf
;
227 router_id_init (zvrf
);
233 /* Callback upon enabling a VRF. */
235 zebra_vrf_enable (vrf_id_t vrf_id
, void **info
)
237 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
245 interface_list (zvrf
);
251 /* Callback upon disabling a VRF. */
253 zebra_vrf_disable (vrf_id_t vrf_id
, void **info
)
255 struct zebra_vrf
*zvrf
= (struct zebra_vrf
*) (*info
);
256 struct listnode
*list_node
;
257 struct interface
*ifp
;
261 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
262 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
264 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id
), list_node
, ifp
))
266 int operative
= if_is_operative (ifp
);
267 UNSET_FLAG (ifp
->flags
, IFF_UP
);
273 rtadv_terminate (zvrf
);
275 kernel_terminate (zvrf
);
277 list_delete_all_node (zvrf
->rid_all_sorted_list
);
278 list_delete_all_node (zvrf
->rid_lo_sorted_list
);
283 /* Zebra VRF initialization. */
285 zebra_vrf_init (void)
287 vrf_add_hook (VRF_NEW_HOOK
, zebra_vrf_new
);
288 vrf_add_hook (VRF_ENABLE_HOOK
, zebra_vrf_enable
);
289 vrf_add_hook (VRF_DISABLE_HOOK
, zebra_vrf_disable
);
293 /* Main startup routine. */
295 main (int argc
, char **argv
)
298 char *vty_addr
= NULL
;
299 int vty_port
= ZEBRA_VTY_PORT
;
303 char *config_file
= NULL
;
305 struct thread thread
;
306 char *zserv_path
= NULL
;
308 /* Set umask before anything for security */
311 /* preserve my name */
312 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
314 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
315 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
322 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vs:C", longopts
, 0);
324 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vC", longopts
, 0);
325 #endif /* HAVE_NETLINK */
343 keep_kernel_mode
= 1;
349 config_file
= optarg
;
361 /* Deal with atoi() returning 0 on failure, and zebra not
362 listening on zebra port... */
363 if (strcmp(optarg
, "0") == 0)
368 vty_port
= atoi (optarg
);
369 if (vty_port
<= 0 || vty_port
> 0xffff)
370 vty_port
= ZEBRA_VTY_PORT
;
377 nl_rcvbufsize
= atoi (optarg
);
379 #endif /* HAVE_NETLINK */
381 zserv_privs
.user
= optarg
;
384 zserv_privs
.group
= optarg
;
387 print_version (progname
);
399 /* Make master thread emulator. */
400 zebrad
.master
= thread_master_create ();
402 /* privs initialise */
403 zprivs_init (&zserv_privs
);
405 /* Vty related initialize. */
406 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
408 vty_init (zebrad
.master
);
411 /* Zebra related initialize. */
416 router_id_cmd_init ();
427 #ifdef ZEBRA_PTM_SUPPORT
431 /* For debug purpose. */
432 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
434 /* Initialize VRF module, and make kernel routing socket. */
439 #endif /* HAVE_SNMP */
442 zfpm_init (zebrad
.master
, 1, 0);
444 zfpm_init (zebrad
.master
, 0, 0);
447 /* Process the configuration file. Among other configuration
448 * directives we can meet those installing static routes. Such
449 * requests will not be executed immediately, but queued in
450 * zebra->ribq structure until we enter the main execution loop.
451 * The notifications from kernel will show originating PID equal
452 * to that after daemon() completes (if ever called).
454 vty_read_config (config_file
, config_default
);
456 /* Don't start execution if we are in dry-run mode */
461 /* rib_weed_tables (); */
463 /* Exit when zebra is working in batch mode. */
468 if (daemon_mode
&& daemon (0, 0) < 0)
470 zlog_err("Zebra daemon failed: %s", strerror(errno
));
474 /* Output pid of zebra. */
475 pid_output (pid_file
);
477 /* After we have successfully acquired the pidfile, we can be sure
478 * about being the only copy of zebra process, which is submitting
479 * changes to the FIB.
480 * Clean up zebra-originated routes. The requests will be sent to OS
481 * immediately, so originating PID in notifications from kernel
482 * will be equal to the current getpid(). To know about such routes,
483 * we have to have route_read() called before.
485 if (! keep_kernel_mode
)
488 /* Needed for BSD routing socket. */
491 /* This must be done only after locking pidfile (bug #403). */
492 zebra_zserv_socket_init (zserv_path
);
494 /* Make vty server socket. */
495 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
498 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
500 while (thread_fetch (zebrad
.master
, &thread
))
501 thread_call (&thread
);