blob: 7bb84d8fc2d2d1f5af3dcb94ed858d3ea147b2a0 [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"
jardineb5d44e2003-12-23 08:09:43 +000037
38#include "isisd/dict.h"
39#include "include-netbsd/iso.h"
40#include "isisd/isis_constants.h"
41#include "isisd/isis_common.h"
42#include "isisd/isis_flags.h"
43#include "isisd/isis_circuit.h"
44#include "isisd/isisd.h"
45#include "isisd/isis_dynhn.h"
Josh Bailey3f045a02012-03-24 08:35:20 -070046#include "isisd/isis_spf.h"
47#include "isisd/isis_route.h"
48#include "isisd/isis_zebra.h"
jardineb5d44e2003-12-23 08:09:43 +000049
50/* Default configuration file name */
51#define ISISD_DEFAULT_CONFIG "isisd.conf"
52/* Default vty port */
jardinfc58e872003-12-23 10:42:45 +000053#define ISISD_VTY_PORT 2608
jardineb5d44e2003-12-23 08:09:43 +000054
jardin9e867fe2003-12-23 08:56:18 +000055/* isisd privileges */
hassof390d2c2004-09-10 20:48:21 +000056zebra_capabilities_t _caps_p[] = {
paulceacedb2005-09-29 14:39:32 +000057 ZCAP_NET_RAW,
jardin9e867fe2003-12-23 08:56:18 +000058 ZCAP_BIND
59};
60
hassof390d2c2004-09-10 20:48:21 +000061struct zebra_privs_t isisd_privs = {
jardin9e867fe2003-12-23 08:56:18 +000062#if defined(QUAGGA_USER)
63 .user = QUAGGA_USER,
64#endif
65#if defined QUAGGA_GROUP
66 .group = QUAGGA_GROUP,
67#endif
68#ifdef VTY_GROUP
69 .vty_group = VTY_GROUP,
70#endif
71 .caps_p = _caps_p,
Josh Bailey3f045a02012-03-24 08:35:20 -070072 .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
jardin9e867fe2003-12-23 08:56:18 +000073 .cap_num_i = 0
74};
75
jardineb5d44e2003-12-23 08:09:43 +000076/* isisd options */
hassof390d2c2004-09-10 20:48:21 +000077struct option longopts[] = {
78 {"daemon", no_argument, NULL, 'd'},
79 {"config_file", required_argument, NULL, 'f'},
80 {"pid_file", required_argument, NULL, 'i'},
81 {"vty_addr", required_argument, NULL, 'A'},
82 {"vty_port", required_argument, NULL, 'P'},
83 {"user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000084 {"group", required_argument, NULL, 'g'},
hassof390d2c2004-09-10 20:48:21 +000085 {"version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000086 {"dryrun", no_argument, NULL, 'C'},
hassof390d2c2004-09-10 20:48:21 +000087 {"help", no_argument, NULL, 'h'},
88 {0}
jardineb5d44e2003-12-23 08:09:43 +000089};
90
91/* Configuration file and directory. */
jardineb5d44e2003-12-23 08:09:43 +000092char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
93char *config_file = NULL;
94
95/* isisd program name. */
96char *progname;
97
98int daemon_mode = 0;
99
100/* Master of threads. */
101struct thread_master *master;
102
hassoc3aac6f2004-02-20 18:44:21 +0000103/* Process ID saved for use by init system */
hasso1cd80842004-10-07 20:07:40 +0000104const char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000105
106/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000107char _cwd[MAXPATHLEN];
108char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000109int _argc;
110char **_argv;
111char **_envp;
112
Paul Jakma41b36e92006-12-08 01:09:50 +0000113/*
114 * Prototypes.
115 */
116void reload(void);
117void sighup(void);
118void sigint(void);
119void sigterm(void);
120void sigusr1(void);
121
122
jardineb5d44e2003-12-23 08:09:43 +0000123/* Help information display. */
124static void
125usage (int status)
126{
127 if (status != 0)
128 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
129 else
hassof390d2c2004-09-10 20:48:21 +0000130 {
jardineb5d44e2003-12-23 08:09:43 +0000131 printf ("Usage : %s [OPTION...]\n\n\
132Daemon which manages IS-IS routing\n\n\
133-d, --daemon Runs in daemon mode\n\
134-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000135-i, --pid_file Set process identifier file name\n\
136-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000137-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000138-u, --user User to run as\n\
139-g, --group Group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000140-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000141-C, --dryrun Check configuration for validity and exit\n\
jardineb5d44e2003-12-23 08:09:43 +0000142-h, --help Display this help and exit\n\
143\n\
hassoaa0b9f92004-09-28 15:05:56 +0000144Report bugs to http://bugzilla.quagga.net\n", progname);
jardineb5d44e2003-12-23 08:09:43 +0000145 }
146
147 exit (status);
148}
149
150
151void
152reload ()
153{
hasso529d65b2004-12-24 00:14:50 +0000154 zlog_debug ("Reload");
jardineb5d44e2003-12-23 08:09:43 +0000155 /* FIXME: Clean up func call here */
ajscdb6ee92005-02-23 15:48:32 +0000156 vty_reset ();
Josh Bailey3f045a02012-03-24 08:35:20 -0700157 (void) isisd_privs.change (ZPRIVS_RAISE);
jardineb5d44e2003-12-23 08:09:43 +0000158 execve (_progpath, _argv, _envp);
Josh Bailey3f045a02012-03-24 08:35:20 -0700159 zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
160 safe_strerror (errno));
jardineb5d44e2003-12-23 08:09:43 +0000161}
162
ajs887c44a2004-12-03 16:36:46 +0000163static void
jardineb5d44e2003-12-23 08:09:43 +0000164terminate (int i)
165{
166 exit (i);
167}
168
169/*
170 * Signal handlers
171 */
paul2d75d052004-01-19 21:31:15 +0000172
hassof390d2c2004-09-10 20:48:21 +0000173void
paul2d75d052004-01-19 21:31:15 +0000174sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000175{
hasso529d65b2004-12-24 00:14:50 +0000176 zlog_debug ("SIGHUP received");
jardineb5d44e2003-12-23 08:09:43 +0000177 reload ();
178
179 return;
180}
181
182void
paul2d75d052004-01-19 21:31:15 +0000183sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000184{
ajs887c44a2004-12-03 16:36:46 +0000185 zlog_notice ("Terminating on signal SIGINT");
jardineb5d44e2003-12-23 08:09:43 +0000186 terminate (0);
jardineb5d44e2003-12-23 08:09:43 +0000187}
188
189void
paul2d75d052004-01-19 21:31:15 +0000190sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000191{
ajs887c44a2004-12-03 16:36:46 +0000192 zlog_notice ("Terminating on signal SIGTERM");
jardineb5d44e2003-12-23 08:09:43 +0000193 terminate (0);
194}
195
196void
paul2d75d052004-01-19 21:31:15 +0000197sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000198{
hasso529d65b2004-12-24 00:14:50 +0000199 zlog_debug ("SIGUSR1 received");
jardineb5d44e2003-12-23 08:09:43 +0000200 zlog_rotate (NULL);
201}
202
paul2d75d052004-01-19 21:31:15 +0000203struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000204{
paul2d75d052004-01-19 21:31:15 +0000205 {
hassof390d2c2004-09-10 20:48:21 +0000206 .signal = SIGHUP,
207 .handler = &sighup,
208 },
paul2d75d052004-01-19 21:31:15 +0000209 {
hassof390d2c2004-09-10 20:48:21 +0000210 .signal = SIGUSR1,
211 .handler = &sigusr1,
212 },
paul2d75d052004-01-19 21:31:15 +0000213 {
hassof390d2c2004-09-10 20:48:21 +0000214 .signal = SIGINT,
215 .handler = &sigint,
216 },
217 {
218 .signal = SIGTERM,
219 .handler = &sigterm,
220 },
paul2d75d052004-01-19 21:31:15 +0000221};
jardineb5d44e2003-12-23 08:09:43 +0000222
223/*
224 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
225 */
hassof390d2c2004-09-10 20:48:21 +0000226int
jardineb5d44e2003-12-23 08:09:43 +0000227main (int argc, char **argv, char **envp)
228{
229 char *p;
230 int opt, vty_port = ISISD_VTY_PORT;
231 struct thread thread;
232 char *config_file = NULL;
233 char *vty_addr = NULL;
Paul Jakma876b8be2006-10-15 23:35:57 +0000234 int dryrun = 0;
jardineb5d44e2003-12-23 08:09:43 +0000235
236 /* Get the programname without the preceding path. */
237 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
238
ajs274a4a42004-12-07 15:39:31 +0000239 zlog_default = openzlog (progname, ZLOG_ISIS,
hassof390d2c2004-09-10 20:48:21 +0000240 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
jardineb5d44e2003-12-23 08:09:43 +0000241
jardineb5d44e2003-12-23 08:09:43 +0000242 /* for reload */
243 _argc = argc;
244 _argv = argv;
245 _envp = envp;
246 getcwd (_cwd, sizeof (_cwd));
247 if (*argv[0] == '.')
248 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
249 else
250 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
hassof390d2c2004-09-10 20:48:21 +0000251
jardineb5d44e2003-12-23 08:09:43 +0000252 /* Command line argument treatment. */
hassof390d2c2004-09-10 20:48:21 +0000253 while (1)
jardineb5d44e2003-12-23 08:09:43 +0000254 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000255 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:g:vC", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000256
hassof390d2c2004-09-10 20:48:21 +0000257 if (opt == EOF)
258 break;
259
260 switch (opt)
261 {
262 case 0:
jardin9e867fe2003-12-23 08:56:18 +0000263 break;
hassof390d2c2004-09-10 20:48:21 +0000264 case 'd':
265 daemon_mode = 1;
266 break;
267 case 'f':
268 config_file = optarg;
269 break;
270 case 'i':
271 pid_file = optarg;
272 break;
273 case 'A':
274 vty_addr = optarg;
275 break;
276 case 'P':
277 /* Deal with atoi() returning 0 on failure, and isisd not
278 listening on isisd port... */
279 if (strcmp (optarg, "0") == 0)
280 {
281 vty_port = 0;
282 break;
283 }
284 vty_port = atoi (optarg);
285 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
286 break;
287 case 'u':
hassoc0652302004-11-25 19:33:48 +0000288 isisd_privs.user = optarg;
hassof390d2c2004-09-10 20:48:21 +0000289 break;
hassoc0652302004-11-25 19:33:48 +0000290 case 'g':
291 isisd_privs.group = optarg;
hassof390d2c2004-09-10 20:48:21 +0000292 break;
293 case 'v':
294 printf ("ISISd version %s\n", ISISD_VERSION);
295 printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
296 " Ofer Wald and Hannes Gredler\n");
297 print_version ("Zebra");
298 exit (0);
299 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000300 case 'C':
301 dryrun = 1;
302 break;
hassof390d2c2004-09-10 20:48:21 +0000303 case 'h':
304 usage (0);
305 break;
306 default:
307 usage (1);
308 break;
309 }
jardineb5d44e2003-12-23 08:09:43 +0000310 }
hassof390d2c2004-09-10 20:48:21 +0000311
jardineb5d44e2003-12-23 08:09:43 +0000312 /* thread master */
313 master = thread_master_create ();
314
315 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000316 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000317
318 /*
319 * initializations
320 */
jardin9e867fe2003-12-23 08:56:18 +0000321 zprivs_init (&isisd_privs);
hassof390d2c2004-09-10 20:48:21 +0000322 signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000323 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000324 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000325 memory_init ();
hassoc729c652004-10-13 08:36:47 +0000326 access_list_init();
jardineb5d44e2003-12-23 08:09:43 +0000327 isis_init ();
Josh Bailey3f045a02012-03-24 08:35:20 -0700328 isis_circuit_init ();
329 isis_spf_cmds_init ();
330
331 /* create the global 'isis' instance */
332 isis_new (1);
333
334 isis_zebra_init ();
335
jardineb5d44e2003-12-23 08:09:43 +0000336 sort_node ();
337
hassof390d2c2004-09-10 20:48:21 +0000338 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000339 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000340 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000341
Paul Jakma876b8be2006-10-15 23:35:57 +0000342 /* Start execution only if not in dry-run mode */
343 if (dryrun)
344 return(0);
345
jardineb5d44e2003-12-23 08:09:43 +0000346 /* demonize */
Josh Bailey3f045a02012-03-24 08:35:20 -0700347 if (daemon_mode)
348 daemon (0, 0);
jardineb5d44e2003-12-23 08:09:43 +0000349
jardineb5d44e2003-12-23 08:09:43 +0000350 /* Process ID file creation. */
Josh Bailey3f045a02012-03-24 08:35:20 -0700351 if (pid_file[0] != '\0')
352 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000353
354 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000355 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000356
jardineb5d44e2003-12-23 08:09:43 +0000357 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000358 zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
hassoc89c05d2005-09-04 21:36:36 +0000359
jardineb5d44e2003-12-23 08:09:43 +0000360 /* Start finite state machine. */
361 while (thread_fetch (master, &thread))
362 thread_call (&thread);
363
364 /* Not reached. */
365 exit (0);
366}