blob: 96816ebb20c9cc2977df61336b7e3252799d745d [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_main.c
3 *
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public Licenseas published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
jardineb5d44e2003-12-23 08:09:43 +000023#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000024
25#include "getopt.h"
26#include "thread.h"
27#include "log.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
jardineb5d44e2003-12-23 08:09:43 +000029#include "command.h"
30#include "vty.h"
31#include "memory.h"
32#include "stream.h"
33#include "if.h"
jardin9e867fe2003-12-23 08:56:18 +000034#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000035#include "sigevent.h"
hassoc729c652004-10-13 08:36:47 +000036#include "filter.h"
Vyacheslav Trushkin48d8bea2011-11-25 18:51:48 +040037#include "zclient.h"
jardineb5d44e2003-12-23 08:09:43 +000038
39#include "isisd/dict.h"
40#include "include-netbsd/iso.h"
41#include "isisd/isis_constants.h"
42#include "isisd/isis_common.h"
43#include "isisd/isis_flags.h"
44#include "isisd/isis_circuit.h"
45#include "isisd/isisd.h"
46#include "isisd/isis_dynhn.h"
Josh Bailey3f045a02012-03-24 08:35:20 -070047#include "isisd/isis_spf.h"
48#include "isisd/isis_route.h"
49#include "isisd/isis_zebra.h"
jardineb5d44e2003-12-23 08:09:43 +000050
51/* Default configuration file name */
52#define ISISD_DEFAULT_CONFIG "isisd.conf"
53/* Default vty port */
jardinfc58e872003-12-23 10:42:45 +000054#define ISISD_VTY_PORT 2608
jardineb5d44e2003-12-23 08:09:43 +000055
jardin9e867fe2003-12-23 08:56:18 +000056/* isisd privileges */
hassof390d2c2004-09-10 20:48:21 +000057zebra_capabilities_t _caps_p[] = {
paulceacedb2005-09-29 14:39:32 +000058 ZCAP_NET_RAW,
jardin9e867fe2003-12-23 08:56:18 +000059 ZCAP_BIND
60};
61
hassof390d2c2004-09-10 20:48:21 +000062struct zebra_privs_t isisd_privs = {
jardin9e867fe2003-12-23 08:56:18 +000063#if defined(QUAGGA_USER)
64 .user = QUAGGA_USER,
65#endif
66#if defined QUAGGA_GROUP
67 .group = QUAGGA_GROUP,
68#endif
69#ifdef VTY_GROUP
70 .vty_group = VTY_GROUP,
71#endif
72 .caps_p = _caps_p,
Josh Bailey3f045a02012-03-24 08:35:20 -070073 .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
jardin9e867fe2003-12-23 08:56:18 +000074 .cap_num_i = 0
75};
76
jardineb5d44e2003-12-23 08:09:43 +000077/* isisd options */
hassof390d2c2004-09-10 20:48:21 +000078struct option longopts[] = {
Vyacheslav Trushkin1627b202011-11-25 17:56:21 +040079 {"daemon", no_argument, NULL, 'd'},
hassof390d2c2004-09-10 20:48:21 +000080 {"config_file", required_argument, NULL, 'f'},
Vyacheslav Trushkin1627b202011-11-25 17:56:21 +040081 {"pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkin48d8bea2011-11-25 18:51:48 +040082 {"socket", required_argument, NULL, 'z'},
Vyacheslav Trushkin1627b202011-11-25 17:56:21 +040083 {"vty_addr", required_argument, NULL, 'A'},
84 {"vty_port", required_argument, NULL, 'P'},
85 {"user", required_argument, NULL, 'u'},
86 {"group", required_argument, NULL, 'g'},
87 {"version", no_argument, NULL, 'v'},
88 {"dryrun", no_argument, NULL, 'C'},
89 {"help", no_argument, NULL, 'h'},
hassof390d2c2004-09-10 20:48:21 +000090 {0}
jardineb5d44e2003-12-23 08:09:43 +000091};
92
93/* Configuration file and directory. */
jardineb5d44e2003-12-23 08:09:43 +000094char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
95char *config_file = NULL;
96
97/* isisd program name. */
98char *progname;
99
100int daemon_mode = 0;
101
102/* Master of threads. */
103struct thread_master *master;
104
hassoc3aac6f2004-02-20 18:44:21 +0000105/* Process ID saved for use by init system */
hasso1cd80842004-10-07 20:07:40 +0000106const char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000107
108/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000109char _cwd[MAXPATHLEN];
110char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000111int _argc;
112char **_argv;
113char **_envp;
114
Paul Jakma41b36e92006-12-08 01:09:50 +0000115/*
116 * Prototypes.
117 */
118void reload(void);
119void sighup(void);
120void sigint(void);
121void sigterm(void);
122void sigusr1(void);
123
124
jardineb5d44e2003-12-23 08:09:43 +0000125/* Help information display. */
126static void
127usage (int status)
128{
129 if (status != 0)
130 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
131 else
hassof390d2c2004-09-10 20:48:21 +0000132 {
jardineb5d44e2003-12-23 08:09:43 +0000133 printf ("Usage : %s [OPTION...]\n\n\
134Daemon which manages IS-IS routing\n\n\
135-d, --daemon Runs in daemon mode\n\
136-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000137-i, --pid_file Set process identifier file name\n\
Vyacheslav Trushkin48d8bea2011-11-25 18:51:48 +0400138-z, --socket Set path of zebra socket\n\
hassoc3aac6f2004-02-20 18:44:21 +0000139-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000140-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000141-u, --user User to run as\n\
142-g, --group Group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000143-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000144-C, --dryrun Check configuration for validity and exit\n\
jardineb5d44e2003-12-23 08:09:43 +0000145-h, --help Display this help and exit\n\
146\n\
Christian Franke4ff3bca2013-03-20 10:50:07 +0000147Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
jardineb5d44e2003-12-23 08:09:43 +0000148 }
149
150 exit (status);
151}
152
153
154void
155reload ()
156{
hasso529d65b2004-12-24 00:14:50 +0000157 zlog_debug ("Reload");
jardineb5d44e2003-12-23 08:09:43 +0000158 /* FIXME: Clean up func call here */
ajscdb6ee92005-02-23 15:48:32 +0000159 vty_reset ();
Josh Bailey3f045a02012-03-24 08:35:20 -0700160 (void) isisd_privs.change (ZPRIVS_RAISE);
jardineb5d44e2003-12-23 08:09:43 +0000161 execve (_progpath, _argv, _envp);
Josh Bailey3f045a02012-03-24 08:35:20 -0700162 zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
163 safe_strerror (errno));
jardineb5d44e2003-12-23 08:09:43 +0000164}
165
ajs887c44a2004-12-03 16:36:46 +0000166static void
jardineb5d44e2003-12-23 08:09:43 +0000167terminate (int i)
168{
169 exit (i);
170}
171
172/*
173 * Signal handlers
174 */
paul2d75d052004-01-19 21:31:15 +0000175
hassof390d2c2004-09-10 20:48:21 +0000176void
paul2d75d052004-01-19 21:31:15 +0000177sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000178{
hasso529d65b2004-12-24 00:14:50 +0000179 zlog_debug ("SIGHUP received");
jardineb5d44e2003-12-23 08:09:43 +0000180 reload ();
181
182 return;
183}
184
185void
paul2d75d052004-01-19 21:31:15 +0000186sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000187{
ajs887c44a2004-12-03 16:36:46 +0000188 zlog_notice ("Terminating on signal SIGINT");
jardineb5d44e2003-12-23 08:09:43 +0000189 terminate (0);
jardineb5d44e2003-12-23 08:09:43 +0000190}
191
192void
paul2d75d052004-01-19 21:31:15 +0000193sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000194{
ajs887c44a2004-12-03 16:36:46 +0000195 zlog_notice ("Terminating on signal SIGTERM");
jardineb5d44e2003-12-23 08:09:43 +0000196 terminate (0);
197}
198
199void
paul2d75d052004-01-19 21:31:15 +0000200sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000201{
hasso529d65b2004-12-24 00:14:50 +0000202 zlog_debug ("SIGUSR1 received");
jardineb5d44e2003-12-23 08:09:43 +0000203 zlog_rotate (NULL);
204}
205
paul2d75d052004-01-19 21:31:15 +0000206struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000207{
paul2d75d052004-01-19 21:31:15 +0000208 {
hassof390d2c2004-09-10 20:48:21 +0000209 .signal = SIGHUP,
210 .handler = &sighup,
211 },
paul2d75d052004-01-19 21:31:15 +0000212 {
hassof390d2c2004-09-10 20:48:21 +0000213 .signal = SIGUSR1,
214 .handler = &sigusr1,
215 },
paul2d75d052004-01-19 21:31:15 +0000216 {
hassof390d2c2004-09-10 20:48:21 +0000217 .signal = SIGINT,
218 .handler = &sigint,
219 },
220 {
221 .signal = SIGTERM,
222 .handler = &sigterm,
223 },
paul2d75d052004-01-19 21:31:15 +0000224};
jardineb5d44e2003-12-23 08:09:43 +0000225
226/*
227 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
228 */
hassof390d2c2004-09-10 20:48:21 +0000229int
jardineb5d44e2003-12-23 08:09:43 +0000230main (int argc, char **argv, char **envp)
231{
232 char *p;
233 int opt, vty_port = ISISD_VTY_PORT;
234 struct thread thread;
235 char *config_file = NULL;
236 char *vty_addr = NULL;
Paul Jakma876b8be2006-10-15 23:35:57 +0000237 int dryrun = 0;
jardineb5d44e2003-12-23 08:09:43 +0000238
239 /* Get the programname without the preceding path. */
240 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
241
ajs274a4a42004-12-07 15:39:31 +0000242 zlog_default = openzlog (progname, ZLOG_ISIS,
hassof390d2c2004-09-10 20:48:21 +0000243 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
jardineb5d44e2003-12-23 08:09:43 +0000244
jardineb5d44e2003-12-23 08:09:43 +0000245 /* for reload */
246 _argc = argc;
247 _argv = argv;
248 _envp = envp;
249 getcwd (_cwd, sizeof (_cwd));
250 if (*argv[0] == '.')
251 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
252 else
253 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
hassof390d2c2004-09-10 20:48:21 +0000254
jardineb5d44e2003-12-23 08:09:43 +0000255 /* Command line argument treatment. */
hassof390d2c2004-09-10 20:48:21 +0000256 while (1)
jardineb5d44e2003-12-23 08:09:43 +0000257 {
Vyacheslav Trushkin48d8bea2011-11-25 18:51:48 +0400258 opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000259
hassof390d2c2004-09-10 20:48:21 +0000260 if (opt == EOF)
261 break;
262
263 switch (opt)
264 {
265 case 0:
jardin9e867fe2003-12-23 08:56:18 +0000266 break;
hassof390d2c2004-09-10 20:48:21 +0000267 case 'd':
268 daemon_mode = 1;
269 break;
270 case 'f':
271 config_file = optarg;
272 break;
273 case 'i':
274 pid_file = optarg;
275 break;
Vyacheslav Trushkin48d8bea2011-11-25 18:51:48 +0400276 case 'z':
277 zclient_serv_path_set (optarg);
278 break;
hassof390d2c2004-09-10 20:48:21 +0000279 case 'A':
280 vty_addr = optarg;
281 break;
282 case 'P':
283 /* Deal with atoi() returning 0 on failure, and isisd not
284 listening on isisd port... */
285 if (strcmp (optarg, "0") == 0)
286 {
287 vty_port = 0;
288 break;
289 }
290 vty_port = atoi (optarg);
291 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
292 break;
293 case 'u':
hassoc0652302004-11-25 19:33:48 +0000294 isisd_privs.user = optarg;
hassof390d2c2004-09-10 20:48:21 +0000295 break;
hassoc0652302004-11-25 19:33:48 +0000296 case 'g':
297 isisd_privs.group = optarg;
hassof390d2c2004-09-10 20:48:21 +0000298 break;
299 case 'v':
300 printf ("ISISd version %s\n", ISISD_VERSION);
301 printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
302 " Ofer Wald and Hannes Gredler\n");
303 print_version ("Zebra");
304 exit (0);
305 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000306 case 'C':
307 dryrun = 1;
308 break;
hassof390d2c2004-09-10 20:48:21 +0000309 case 'h':
310 usage (0);
311 break;
312 default:
313 usage (1);
314 break;
315 }
jardineb5d44e2003-12-23 08:09:43 +0000316 }
hassof390d2c2004-09-10 20:48:21 +0000317
jardineb5d44e2003-12-23 08:09:43 +0000318 /* thread master */
319 master = thread_master_create ();
320
321 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000322 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000323
324 /*
325 * initializations
326 */
jardin9e867fe2003-12-23 08:56:18 +0000327 zprivs_init (&isisd_privs);
Balaji.G837d16c2012-09-26 14:09:10 +0530328 signal_init (master, array_size (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000329 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000330 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000331 memory_init ();
hassoc729c652004-10-13 08:36:47 +0000332 access_list_init();
jardineb5d44e2003-12-23 08:09:43 +0000333 isis_init ();
Josh Bailey3f045a02012-03-24 08:35:20 -0700334 isis_circuit_init ();
335 isis_spf_cmds_init ();
336
337 /* create the global 'isis' instance */
338 isis_new (1);
339
340 isis_zebra_init ();
341
jardineb5d44e2003-12-23 08:09:43 +0000342 sort_node ();
343
hassof390d2c2004-09-10 20:48:21 +0000344 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000345 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000346 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000347
Paul Jakma876b8be2006-10-15 23:35:57 +0000348 /* Start execution only if not in dry-run mode */
349 if (dryrun)
350 return(0);
351
jardineb5d44e2003-12-23 08:09:43 +0000352 /* demonize */
Josh Bailey3f045a02012-03-24 08:35:20 -0700353 if (daemon_mode)
354 daemon (0, 0);
jardineb5d44e2003-12-23 08:09:43 +0000355
jardineb5d44e2003-12-23 08:09:43 +0000356 /* Process ID file creation. */
Josh Bailey3f045a02012-03-24 08:35:20 -0700357 if (pid_file[0] != '\0')
358 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000359
360 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000361 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000362
jardineb5d44e2003-12-23 08:09:43 +0000363 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000364 zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
hassoc89c05d2005-09-04 21:36:36 +0000365
jardineb5d44e2003-12-23 08:09:43 +0000366 /* Start finite state machine. */
367 while (thread_fetch (master, &thread))
368 thread_call (&thread);
369
370 /* Not reached. */
371 exit (0);
372}