blob: c7e3b68c3e3c8219f05cf25b99a505822e067de3 [file] [log] [blame]
pauledd7c242003-06-04 13:59:38 +00001/* zebra daemon main routine.
paul718e3742002-12-13 20:15:29 +00002 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
gdt5e4fa162004-03-16 14:38:36 +000024#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000025#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
30#include "prefix.h"
31#include "log.h"
Paul Jakma7514fb72007-05-02 16:05:35 +000032#include "plist.h"
pauledd7c242003-06-04 13:59:38 +000033#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000034#include "sigevent.h"
Feng Lu41f44a22015-05-22 11:39:56 +020035#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "zebra/rib.h"
38#include "zebra/zserv.h"
39#include "zebra/debug.h"
hasso18a6dce2004-10-03 18:18:34 +000040#include "zebra/router-id.h"
hassoca776982004-06-12 14:33:05 +000041#include "zebra/irdp.h"
paula1ac18c2005-06-28 17:17:12 +000042#include "zebra/rtadv.h"
Avneesh Sachdev5adc2522012-11-13 22:48:59 +000043#include "zebra/zebra_fpm.h"
paul718e3742002-12-13 20:15:29 +000044
paulb21b19c2003-06-15 01:28:29 +000045/* Zebra instance */
46struct zebra_t zebrad =
47{
48 .rtm_table_default = 0,
49};
paul718e3742002-12-13 20:15:29 +000050
51/* process id. */
paul718e3742002-12-13 20:15:29 +000052pid_t pid;
53
gdt87efd642004-06-30 17:36:11 +000054/* Pacify zclient.o in libzebra, which expects this variable. */
55struct thread_master *master;
56
paul718e3742002-12-13 20:15:29 +000057/* Route retain mode flag. */
58int retain_mode = 0;
59
60/* Don't delete kernel route. */
61int keep_kernel_mode = 0;
62
hassoc34b6b52004-08-31 13:41:49 +000063#ifdef HAVE_NETLINK
64/* Receive buffer size for netlink socket */
65u_int32_t nl_rcvbufsize = 0;
66#endif /* HAVE_NETLINK */
67
paul718e3742002-12-13 20:15:29 +000068/* Command line options. */
69struct option longopts[] =
70{
71 { "batch", no_argument, NULL, 'b'},
72 { "daemon", no_argument, NULL, 'd'},
73 { "keep_kernel", no_argument, NULL, 'k'},
paul718e3742002-12-13 20:15:29 +000074 { "config_file", required_argument, NULL, 'f'},
75 { "pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040076 { "socket", required_argument, NULL, 'z'},
paul718e3742002-12-13 20:15:29 +000077 { "help", no_argument, NULL, 'h'},
78 { "vty_addr", required_argument, NULL, 'A'},
79 { "vty_port", required_argument, NULL, 'P'},
80 { "retain", no_argument, NULL, 'r'},
Paul Jakma876b8be2006-10-15 23:35:57 +000081 { "dryrun", no_argument, NULL, 'C'},
hassoc34b6b52004-08-31 13:41:49 +000082#ifdef HAVE_NETLINK
hasso583d8002005-01-16 23:34:02 +000083 { "nl-bufsize", required_argument, NULL, 's'},
hassoc34b6b52004-08-31 13:41:49 +000084#endif /* HAVE_NETLINK */
pauledd7c242003-06-04 13:59:38 +000085 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000086 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000087 { "version", no_argument, NULL, 'v'},
88 { 0 }
89};
90
pauledd7c242003-06-04 13:59:38 +000091zebra_capabilities_t _caps_p [] =
92{
paulceacedb2005-09-29 14:39:32 +000093 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000094 ZCAP_SYS_ADMIN,
paulceacedb2005-09-29 14:39:32 +000095 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000096};
97
98/* zebra privileges to run with */
99struct zebra_privs_t zserv_privs =
100{
pauld81fadf2003-08-14 05:32:12 +0000101#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
102 .user = QUAGGA_USER,
103 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +0000104#endif
105#ifdef VTY_GROUP
106 .vty_group = VTY_GROUP,
107#endif
108 .caps_p = _caps_p,
Balaji.G837d16c2012-09-26 14:09:10 +0530109 .cap_num_p = array_size(_caps_p),
pauledd7c242003-06-04 13:59:38 +0000110 .cap_num_i = 0
111};
112
paul718e3742002-12-13 20:15:29 +0000113/* Default configuration file path. */
paul718e3742002-12-13 20:15:29 +0000114char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
115
116/* Process ID saved for use by init system */
hassofce954f2004-10-07 20:29:24 +0000117const char *pid_file = PATH_ZEBRA_PID;
paul718e3742002-12-13 20:15:29 +0000118
119/* Help information display. */
120static void
121usage (char *progname, int status)
122{
123 if (status != 0)
124 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
125 else
126 {
hassoc34b6b52004-08-31 13:41:49 +0000127 printf ("Usage : %s [OPTION...]\n\n"\
128 "Daemon which manages kernel routing table management and "\
129 "redistribution between different routing protocols.\n\n"\
130 "-b, --batch Runs in batch mode\n"\
131 "-d, --daemon Runs in daemon mode\n"\
132 "-f, --config_file Set configuration file name\n"\
133 "-i, --pid_file Set process identifier file name\n"\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400134 "-z, --socket Set path of zebra socket\n"\
hassoc34b6b52004-08-31 13:41:49 +0000135 "-k, --keep_kernel Don't delete old routes which installed by "\
136 "zebra.\n"\
Paul Jakma876b8be2006-10-15 23:35:57 +0000137 "-C, --dryrun Check configuration for validity and exit\n"\
hassoc34b6b52004-08-31 13:41:49 +0000138 "-A, --vty_addr Set vty's bind address\n"\
139 "-P, --vty_port Set vty's port number\n"\
140 "-r, --retain When program terminates, retain added route "\
141 "by zebra.\n"\
hassoc0652302004-11-25 19:33:48 +0000142 "-u, --user User to run as\n"\
143 "-g, --group Group to run as\n", progname);
hassoc34b6b52004-08-31 13:41:49 +0000144#ifdef HAVE_NETLINK
145 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
146#endif /* HAVE_NETLINK */
147 printf ("-v, --version Print program version\n"\
148 "-h, --help Display this help and exit\n"\
149 "\n"\
150 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000151 }
152
153 exit (status);
154}
David Lamparter6b0655a2014-06-04 06:53:35 +0200155
paul718e3742002-12-13 20:15:29 +0000156/* SIGHUP handler. */
paula1ac18c2005-06-28 17:17:12 +0000157static void
paul2d75d052004-01-19 21:31:15 +0000158sighup (void)
paul718e3742002-12-13 20:15:29 +0000159{
160 zlog_info ("SIGHUP received");
161
162 /* Reload of config file. */
163 ;
164}
165
166/* SIGINT handler. */
paula1ac18c2005-06-28 17:17:12 +0000167static void
paul2d75d052004-01-19 21:31:15 +0000168sigint (void)
paul718e3742002-12-13 20:15:29 +0000169{
ajs887c44a2004-12-03 16:36:46 +0000170 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000171
172 if (!retain_mode)
173 rib_close ();
hassoca776982004-06-12 14:33:05 +0000174#ifdef HAVE_IRDP
175 irdp_finish();
176#endif
paul718e3742002-12-13 20:15:29 +0000177
178 exit (0);
179}
180
181/* SIGUSR1 handler. */
paula1ac18c2005-06-28 17:17:12 +0000182static void
paul2d75d052004-01-19 21:31:15 +0000183sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000184{
185 zlog_rotate (NULL);
186}
187
paul2d75d052004-01-19 21:31:15 +0000188struct quagga_signal_t zebra_signals[] =
paul718e3742002-12-13 20:15:29 +0000189{
paul2d75d052004-01-19 21:31:15 +0000190 {
191 .signal = SIGHUP,
192 .handler = &sighup,
193 },
194 {
195 .signal = SIGUSR1,
196 .handler = &sigusr1,
197 },
198 {
199 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000200 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000201 },
hassof571dab2004-03-22 08:55:25 +0000202 {
203 .signal = SIGTERM,
204 .handler = &sigint,
205 },
paul2d75d052004-01-19 21:31:15 +0000206};
David Lamparter6b0655a2014-06-04 06:53:35 +0200207
Feng Lu41f44a22015-05-22 11:39:56 +0200208/* Callback upon creating a new VRF. */
209static int
210zebra_vrf_new (vrf_id_t vrf_id, void **info)
211{
212 struct zebra_vrf *zvrf = *info;
213
214 if (! zvrf)
215 {
216 zvrf = zebra_vrf_alloc (vrf_id);
217 *info = (void *)zvrf;
Feng Luac19a442015-05-22 11:40:07 +0200218 router_id_init (zvrf);
Feng Lu41f44a22015-05-22 11:39:56 +0200219 }
220
221 return 0;
222}
223
224/* Zebra VRF initialization. */
225static void
226zebra_vrf_init (void)
227{
228 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
229 vrf_init ();
230}
231
paul718e3742002-12-13 20:15:29 +0000232/* Main startup routine. */
233int
234main (int argc, char **argv)
235{
236 char *p;
237 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000238 int vty_port = ZEBRA_VTY_PORT;
Paul Jakma876b8be2006-10-15 23:35:57 +0000239 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000240 int batch_mode = 0;
241 int daemon_mode = 0;
242 char *config_file = NULL;
243 char *progname;
244 struct thread thread;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400245 char *zserv_path = NULL;
paul718e3742002-12-13 20:15:29 +0000246
247 /* Set umask before anything for security */
248 umask (0027);
249
250 /* preserve my name */
251 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
252
ajs274a4a42004-12-07 15:39:31 +0000253 zlog_default = openzlog (progname, ZLOG_ZEBRA,
paul718e3742002-12-13 20:15:29 +0000254 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
255
256 while (1)
257 {
258 int opt;
259
hassoc34b6b52004-08-31 13:41:49 +0000260#ifdef HAVE_NETLINK
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400261 opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000262#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400263 opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000264#endif /* HAVE_NETLINK */
paul718e3742002-12-13 20:15:29 +0000265
266 if (opt == EOF)
267 break;
268
269 switch (opt)
270 {
271 case 0:
272 break;
273 case 'b':
274 batch_mode = 1;
275 case 'd':
276 daemon_mode = 1;
277 break;
278 case 'k':
279 keep_kernel_mode = 1;
280 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000281 case 'C':
282 dryrun = 1;
283 break;
paul718e3742002-12-13 20:15:29 +0000284 case 'f':
285 config_file = optarg;
286 break;
287 case 'A':
288 vty_addr = optarg;
289 break;
290 case 'i':
291 pid_file = optarg;
292 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400293 case 'z':
294 zserv_path = optarg;
295 break;
paul718e3742002-12-13 20:15:29 +0000296 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000297 /* Deal with atoi() returning 0 on failure, and zebra not
298 listening on zebra port... */
299 if (strcmp(optarg, "0") == 0)
300 {
301 vty_port = 0;
302 break;
303 }
paul718e3742002-12-13 20:15:29 +0000304 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000305 if (vty_port <= 0 || vty_port > 0xffff)
306 vty_port = ZEBRA_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000307 break;
308 case 'r':
309 retain_mode = 1;
310 break;
hassoc34b6b52004-08-31 13:41:49 +0000311#ifdef HAVE_NETLINK
312 case 's':
313 nl_rcvbufsize = atoi (optarg);
314 break;
315#endif /* HAVE_NETLINK */
hassoc0652302004-11-25 19:33:48 +0000316 case 'u':
317 zserv_privs.user = optarg;
318 break;
319 case 'g':
320 zserv_privs.group = optarg;
321 break;
paul718e3742002-12-13 20:15:29 +0000322 case 'v':
323 print_version (progname);
324 exit (0);
325 break;
326 case 'h':
327 usage (progname, 0);
328 break;
329 default:
330 usage (progname, 1);
331 break;
332 }
333 }
334
335 /* Make master thread emulator. */
paulb21b19c2003-06-15 01:28:29 +0000336 zebrad.master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000337
pauledd7c242003-06-04 13:59:38 +0000338 /* privs initialise */
339 zprivs_init (&zserv_privs);
340
paul718e3742002-12-13 20:15:29 +0000341 /* Vty related initialize. */
Balaji.G837d16c2012-09-26 14:09:10 +0530342 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
paul718e3742002-12-13 20:15:29 +0000343 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000344 vty_init (zebrad.master);
paul718e3742002-12-13 20:15:29 +0000345 memory_init ();
346
347 /* Zebra related initialize. */
348 zebra_init ();
349 rib_init ();
350 zebra_if_init ();
351 zebra_debug_init ();
Feng Luac19a442015-05-22 11:40:07 +0200352 router_id_cmd_init ();
paul718e3742002-12-13 20:15:29 +0000353 zebra_vty_init ();
354 access_list_init ();
Paul Jakma7514fb72007-05-02 16:05:35 +0000355 prefix_list_init ();
Joachim Nilsson36735ed2012-05-09 13:38:36 +0200356#ifdef RTADV
paul718e3742002-12-13 20:15:29 +0000357 rtadv_init ();
Joachim Nilsson36735ed2012-05-09 13:38:36 +0200358#endif
hassoca776982004-06-12 14:33:05 +0000359#ifdef HAVE_IRDP
360 irdp_init();
361#endif
paul718e3742002-12-13 20:15:29 +0000362
363 /* For debug purpose. */
364 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
365
Feng Lu41f44a22015-05-22 11:39:56 +0200366 /* Initialize VRF module, and make kernel routing socket. */
367 zebra_vrf_init ();
paul718e3742002-12-13 20:15:29 +0000368 kernel_init ();
369 interface_list ();
370 route_read ();
371
paul718e3742002-12-13 20:15:29 +0000372#ifdef HAVE_SNMP
373 zebra_snmp_init ();
374#endif /* HAVE_SNMP */
375
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000376#ifdef HAVE_FPM
377 zfpm_init (zebrad.master, 1, 0);
378#else
379 zfpm_init (zebrad.master, 0, 0);
380#endif
381
Denis Ovsienko91b73512007-09-14 13:31:52 +0000382 /* Process the configuration file. Among other configuration
383 * directives we can meet those installing static routes. Such
384 * requests will not be executed immediately, but queued in
385 * zebra->ribq structure until we enter the main execution loop.
386 * The notifications from kernel will show originating PID equal
387 * to that after daemon() completes (if ever called).
388 */
hasso320ec102004-06-20 19:54:37 +0000389 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000390
Paul Jakma876b8be2006-10-15 23:35:57 +0000391 /* Don't start execution if we are in dry-run mode */
392 if (dryrun)
393 return(0);
394
paul718e3742002-12-13 20:15:29 +0000395 /* Clean up rib. */
396 rib_weed_tables ();
397
398 /* Exit when zebra is working in batch mode. */
399 if (batch_mode)
400 exit (0);
401
paul718e3742002-12-13 20:15:29 +0000402 /* Daemonize. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700403 if (daemon_mode && daemon (0, 0) < 0)
404 {
405 zlog_err("Zebra daemon failed: %s", strerror(errno));
406 exit (1);
407 }
paul718e3742002-12-13 20:15:29 +0000408
409 /* Output pid of zebra. */
410 pid_output (pid_file);
411
Denis Ovsienko91b73512007-09-14 13:31:52 +0000412 /* After we have successfully acquired the pidfile, we can be sure
413 * about being the only copy of zebra process, which is submitting
414 * changes to the FIB.
415 * Clean up zebra-originated routes. The requests will be sent to OS
416 * immediately, so originating PID in notifications from kernel
417 * will be equal to the current getpid(). To know about such routes,
418 * we have to have route_read() called before.
419 */
420 if (! keep_kernel_mode)
421 rib_sweep_route ();
422
paul718e3742002-12-13 20:15:29 +0000423 /* Needed for BSD routing socket. */
424 pid = getpid ();
425
Denis Ovsienko97be79f2009-07-24 20:45:31 +0400426 /* This must be done only after locking pidfile (bug #403). */
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400427 zebra_zserv_socket_init (zserv_path);
Denis Ovsienko97be79f2009-07-24 20:45:31 +0400428
paul718e3742002-12-13 20:15:29 +0000429 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000430 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000431
ajs887c44a2004-12-03 16:36:46 +0000432 /* Print banner. */
433 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
434
paulb21b19c2003-06-15 01:28:29 +0000435 while (thread_fetch (zebrad.master, &thread))
paul718e3742002-12-13 20:15:29 +0000436 thread_call (&thread);
437
438 /* Not reached... */
paule8e19462006-01-19 20:16:55 +0000439 return 0;
paul718e3742002-12-13 20:15:29 +0000440}