]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
lib: Add set_socket_path() to sockopt.c to override the path element of a (vty) socke...
[mirror_frr.git] / zebra / main.c
CommitLineData
edd7c245 1/* zebra daemon main routine.
718e3744 2 * 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
5e4fa164 24#include <lib/version.h>
718e3744 25#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
4a1ab8e4 30#include "zebra_memory.h"
fc7948fa 31#include "memory_vty.h"
718e3744 32#include "prefix.h"
33#include "log.h"
7514fb77 34#include "plist.h"
edd7c245 35#include "privs.h"
2d75d052 36#include "sigevent.h"
b72ede27 37#include "vrf.h"
718e3744 38
39#include "zebra/rib.h"
40#include "zebra/zserv.h"
41#include "zebra/debug.h"
18a6dce6 42#include "zebra/router-id.h"
ca776988 43#include "zebra/irdp.h"
a1ac18c4 44#include "zebra/rtadv.h"
5adc2528 45#include "zebra/zebra_fpm.h"
244c1cdc 46#include "zebra/zebra_ptm.h"
fe18ee2d 47#include "zebra/zebra_ns.h"
e2b1be64 48#include "zebra/redistribute.h"
7758e3f3 49#include "zebra/zebra_mpls.h"
244c1cdc
DS
50
51#define ZEBRA_PTM_SUPPORT
718e3744 52
b21b19c5 53/* Zebra instance */
54struct zebra_t zebrad =
55{
56 .rtm_table_default = 0,
57};
718e3744 58
59/* process id. */
718e3744 60pid_t pid;
61
87efd646 62/* Pacify zclient.o in libzebra, which expects this variable. */
63struct thread_master *master;
64
718e3744 65/* Route retain mode flag. */
66int retain_mode = 0;
67
6baf7bb8
DS
68/* Allow non-quagga entities to delete quagga routes */
69int allow_delete = 0;
70
718e3744 71/* Don't delete kernel route. */
72int keep_kernel_mode = 0;
73
c34b6b57 74#ifdef HAVE_NETLINK
75/* Receive buffer size for netlink socket */
b6286c70 76u_int32_t nl_rcvbufsize = 4194304;
c34b6b57 77#endif /* HAVE_NETLINK */
78
718e3744 79/* Command line options. */
80struct option longopts[] =
81{
6baf7bb8
DS
82 { "batch", no_argument, NULL, 'b'},
83 { "daemon", no_argument, NULL, 'd'},
84 { "allow_delete", no_argument, NULL, 'a'},
85 { "keep_kernel", no_argument, NULL, 'k'},
fb0aa886 86 { "fpm_format", required_argument, NULL, 'F'},
6baf7bb8
DS
87 { "config_file", required_argument, NULL, 'f'},
88 { "pid_file", required_argument, NULL, 'i'},
89 { "socket", required_argument, NULL, 'z'},
90 { "help", no_argument, NULL, 'h'},
91 { "vty_addr", required_argument, NULL, 'A'},
92 { "vty_port", required_argument, NULL, 'P'},
93 { "retain", no_argument, NULL, 'r'},
94 { "dryrun", no_argument, NULL, 'C'},
c34b6b57 95#ifdef HAVE_NETLINK
6baf7bb8 96 { "nl-bufsize", required_argument, NULL, 's'},
c34b6b57 97#endif /* HAVE_NETLINK */
6baf7bb8
DS
98 { "user", required_argument, NULL, 'u'},
99 { "group", required_argument, NULL, 'g'},
100 { "version", no_argument, NULL, 'v'},
718e3744 101 { 0 }
102};
103
edd7c245 104zebra_capabilities_t _caps_p [] =
105{
ceacedba 106 ZCAP_NET_ADMIN,
edd7c245 107 ZCAP_SYS_ADMIN,
ceacedba 108 ZCAP_NET_RAW,
edd7c245 109};
110
111/* zebra privileges to run with */
112struct zebra_privs_t zserv_privs =
113{
b2f36157
DL
114#if defined(FRR_USER) && defined(FRR_GROUP)
115 .user = FRR_USER,
116 .group = FRR_GROUP,
edd7c245 117#endif
118#ifdef VTY_GROUP
119 .vty_group = VTY_GROUP,
120#endif
121 .caps_p = _caps_p,
837d16cc 122 .cap_num_p = array_size(_caps_p),
edd7c245 123 .cap_num_i = 0
124};
125
718e3744 126/* Default configuration file path. */
718e3744 127char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
128
129/* Process ID saved for use by init system */
fce954f8 130const char *pid_file = PATH_ZEBRA_PID;
718e3744 131
132/* Help information display. */
133static void
134usage (char *progname, int status)
135{
136 if (status != 0)
137 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
138 else
139 {
c34b6b57 140 printf ("Usage : %s [OPTION...]\n\n"\
141 "Daemon which manages kernel routing table management and "\
142 "redistribution between different routing protocols.\n\n"\
143 "-b, --batch Runs in batch mode\n"\
144 "-d, --daemon Runs in daemon mode\n"\
6baf7bb8 145 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
c34b6b57 146 "-f, --config_file Set configuration file name\n"\
fb0aa886 147 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
c34b6b57 148 "-i, --pid_file Set process identifier file name\n"\
b5114685 149 "-z, --socket Set path of zebra socket\n"\
c34b6b57 150 "-k, --keep_kernel Don't delete old routes which installed by "\
151 "zebra.\n"\
876b8be0 152 "-C, --dryrun Check configuration for validity and exit\n"\
c34b6b57 153 "-A, --vty_addr Set vty's bind address\n"\
154 "-P, --vty_port Set vty's port number\n"\
155 "-r, --retain When program terminates, retain added route "\
156 "by zebra.\n"\
c065230a 157 "-u, --user User to run as\n"\
19380819 158 "-g, --group Group to run as\n", progname);
c34b6b57 159#ifdef HAVE_NETLINK
160 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
161#endif /* HAVE_NETLINK */
162 printf ("-v, --version Print program version\n"\
163 "-h, --help Display this help and exit\n"\
164 "\n"\
b2f36157 165 "Report bugs to %s\n", FRR_BUG_ADDRESS);
718e3744 166 }
167
168 exit (status);
169}
6b0655a2 170
718e3744 171/* SIGHUP handler. */
a1ac18c4 172static void
2d75d052 173sighup (void)
718e3744 174{
175 zlog_info ("SIGHUP received");
176
177 /* Reload of config file. */
178 ;
179}
180
181/* SIGINT handler. */
a1ac18c4 182static void
2d75d052 183sigint (void)
718e3744 184{
5a8dfcd8
RW
185 struct vrf *vrf;
186 struct zebra_vrf *zvrf;
fe18ee2d
DS
187 struct zebra_ns *zns;
188
887c44a4 189 zlog_notice ("Terminating on signal");
718e3744 190
ca776988 191#ifdef HAVE_IRDP
192 irdp_finish();
193#endif
718e3744 194
c8ed14dd 195 zebra_ptm_finish();
5a8dfcd8
RW
196 list_delete_all_node (zebrad.client_list);
197
198 if (retain_mode)
199 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
200 {
201 zvrf = vrf->info;
202 if (zvrf)
203 SET_FLAG (zvrf->flags, ZEBRA_VRF_RETAIN);
204 }
205 vrf_terminate ();
fe18ee2d
DS
206
207 zns = zebra_ns_lookup (NS_DEFAULT);
208 zebra_ns_disable (0, (void **)&zns);
58ac32e2
RW
209
210 access_list_reset ();
211 prefix_list_reset ();
212 route_map_finish ();
213 cmd_terminate ();
214 vty_terminate ();
215 zprivs_terminate (&zserv_privs);
216 list_delete (zebrad.client_list);
5a8dfcd8
RW
217 work_queue_free (zebrad.ribq);
218 if (zebrad.lsp_process_q)
219 work_queue_free (zebrad.lsp_process_q);
220 meta_queue_free (zebrad.mq);
58ac32e2
RW
221 thread_master_free (zebrad.master);
222 if (zlog_default)
223 closezlog (zlog_default);
224
718e3744 225 exit (0);
226}
227
228/* SIGUSR1 handler. */
a1ac18c4 229static void
2d75d052 230sigusr1 (void)
718e3744 231{
232 zlog_rotate (NULL);
233}
234
2d75d052 235struct quagga_signal_t zebra_signals[] =
718e3744 236{
2d75d052 237 {
238 .signal = SIGHUP,
239 .handler = &sighup,
240 },
241 {
242 .signal = SIGUSR1,
243 .handler = &sigusr1,
244 },
245 {
246 .signal = SIGINT,
8c903fbb 247 .handler = &sigint,
2d75d052 248 },
f571dab0 249 {
250 .signal = SIGTERM,
251 .handler = &sigint,
252 },
2d75d052 253};
b72ede27 254
718e3744 255/* Main startup routine. */
256int
257main (int argc, char **argv)
258{
259 char *p;
260 char *vty_addr = NULL;
4fc4e7ab 261 int vty_port = ZEBRA_VTY_PORT;
876b8be0 262 int dryrun = 0;
718e3744 263 int batch_mode = 0;
264 int daemon_mode = 0;
265 char *config_file = NULL;
266 char *progname;
267 struct thread thread;
b5114685 268 char *zserv_path = NULL;
fb0aa886 269 char *fpm_format = NULL;
718e3744 270
271 /* Set umask before anything for security */
272 umask (0027);
273
274 /* preserve my name */
275 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
276
7c8ff89e 277 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
718e3744 278 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
2caa9b39 279 zprivs_init (&zserv_privs);
c05795b1
SK
280#if defined(HAVE_CUMULUS)
281 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
282#endif
718e3744 283
284 while (1)
285 {
286 int opt;
287
c34b6b57 288#ifdef HAVE_NETLINK
fb0aa886 289 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
c34b6b57 290#else
fb0aa886 291 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vC", longopts, 0);
c34b6b57 292#endif /* HAVE_NETLINK */
718e3744 293
294 if (opt == EOF)
295 break;
296
297 switch (opt)
298 {
299 case 0:
300 break;
301 case 'b':
302 batch_mode = 1;
303 case 'd':
304 daemon_mode = 1;
305 break;
6baf7bb8
DS
306 case 'a':
307 allow_delete = 1;
308 break;
718e3744 309 case 'k':
310 keep_kernel_mode = 1;
311 break;
876b8be0
PJ
312 case 'C':
313 dryrun = 1;
314 break;
718e3744 315 case 'f':
316 config_file = optarg;
317 break;
fb0aa886
AS
318 case 'F':
319 fpm_format = optarg;
320 break;
718e3744 321 case 'A':
322 vty_addr = optarg;
323 break;
324 case 'i':
325 pid_file = optarg;
326 break;
b5114685
VT
327 case 'z':
328 zserv_path = optarg;
329 break;
718e3744 330 case 'P':
4fc4e7ab 331 /* Deal with atoi() returning 0 on failure, and zebra not
332 listening on zebra port... */
333 if (strcmp(optarg, "0") == 0)
334 {
335 vty_port = 0;
336 break;
337 }
718e3744 338 vty_port = atoi (optarg);
0d6b2ee2
PJ
339 if (vty_port <= 0 || vty_port > 0xffff)
340 vty_port = ZEBRA_VTY_PORT;
718e3744 341 break;
342 case 'r':
343 retain_mode = 1;
344 break;
c34b6b57 345#ifdef HAVE_NETLINK
346 case 's':
347 nl_rcvbufsize = atoi (optarg);
348 break;
349#endif /* HAVE_NETLINK */
c065230a 350 case 'u':
351 zserv_privs.user = optarg;
352 break;
353 case 'g':
354 zserv_privs.group = optarg;
355 break;
718e3744 356 case 'v':
357 print_version (progname);
358 exit (0);
359 break;
360 case 'h':
361 usage (progname, 0);
362 break;
363 default:
364 usage (progname, 1);
365 break;
366 }
367 }
368
369 /* Make master thread emulator. */
b21b19c5 370 zebrad.master = thread_master_create ();
718e3744 371
372 /* Vty related initialize. */
837d16cc 373 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
718e3744 374 cmd_init (1);
3ddccf18 375 vty_config_lockless ();
b21b19c5 376 vty_init (zebrad.master);
718e3744 377 memory_init ();
378
379 /* Zebra related initialize. */
380 zebra_init ();
381 rib_init ();
382 zebra_if_init ();
383 zebra_debug_init ();
c6ffe645 384 router_id_cmd_init ();
718e3744 385 zebra_vty_init ();
386 access_list_init ();
7514fb77 387 prefix_list_init ();
8da4e946 388#if defined (HAVE_RTADV)
cd80d74f 389 rtadv_cmd_init ();
36735ed9 390#endif
ca776988 391#ifdef HAVE_IRDP
392 irdp_init();
393#endif
244c1cdc
DS
394 /* PTM socket */
395#ifdef ZEBRA_PTM_SUPPORT
396 zebra_ptm_init();
397#endif
718e3744 398
7758e3f3 399 zebra_mpls_init ();
fe6c7157 400 zebra_mpls_vty_init ();
7758e3f3 401
718e3744 402 /* For debug purpose. */
403 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
404
fe18ee2d
DS
405 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
406 zebra_ns_init ();
718e3744 407
718e3744 408#ifdef HAVE_SNMP
409 zebra_snmp_init ();
410#endif /* HAVE_SNMP */
411
5adc2528 412#ifdef HAVE_FPM
fb0aa886 413 zfpm_init (zebrad.master, 1, 0, fpm_format);
5adc2528 414#else
fb0aa886 415 zfpm_init (zebrad.master, 0, 0, fpm_format);
5adc2528
AS
416#endif
417
91b7351d
DO
418 /* Process the configuration file. Among other configuration
419 * directives we can meet those installing static routes. Such
420 * requests will not be executed immediately, but queued in
421 * zebra->ribq structure until we enter the main execution loop.
422 * The notifications from kernel will show originating PID equal
423 * to that after daemon() completes (if ever called).
424 */
320ec10a 425 vty_read_config (config_file, config_default);
718e3744 426
876b8be0
PJ
427 /* Don't start execution if we are in dry-run mode */
428 if (dryrun)
429 return(0);
430
718e3744 431 /* Clean up rib. */
7a4bb9c5 432 /* rib_weed_tables (); */
718e3744 433
434 /* Exit when zebra is working in batch mode. */
435 if (batch_mode)
436 exit (0);
437
718e3744 438 /* Daemonize. */
065de903
SH
439 if (daemon_mode && daemon (0, 0) < 0)
440 {
441 zlog_err("Zebra daemon failed: %s", strerror(errno));
442 exit (1);
443 }
718e3744 444
445 /* Output pid of zebra. */
446 pid_output (pid_file);
447
91b7351d
DO
448 /* After we have successfully acquired the pidfile, we can be sure
449 * about being the only copy of zebra process, which is submitting
450 * changes to the FIB.
451 * Clean up zebra-originated routes. The requests will be sent to OS
452 * immediately, so originating PID in notifications from kernel
453 * will be equal to the current getpid(). To know about such routes,
454 * we have to have route_read() called before.
455 */
456 if (! keep_kernel_mode)
457 rib_sweep_route ();
458
718e3744 459 /* Needed for BSD routing socket. */
460 pid = getpid ();
461
97be79f9 462 /* This must be done only after locking pidfile (bug #403). */
b5114685 463 zebra_zserv_socket_init (zserv_path);
97be79f9 464
718e3744 465 /* Make vty server socket. */
4fc4e7ab 466 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
718e3744 467
887c44a4 468 /* Print banner. */
b2f36157 469 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port);
887c44a4 470
b21b19c5 471 while (thread_fetch (zebrad.master, &thread))
718e3744 472 thread_call (&thread);
473
474 /* Not reached... */
e8e1946e 475 return 0;
718e3744 476}