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