blob: d3ef0a6a82f1de29492b9c2fb243a2175fa694e7 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Copyright (C) 1999 Yasuhiro Ohara
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
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
hasso3b4cd3a2004-05-18 19:28:32 +000023#include <lib/version.h>
hasso508e53e2004-05-18 18:57:06 +000024
paul718e3742002-12-13 20:15:29 +000025#include "getopt.h"
26#include "thread.h"
27#include "log.h"
paul718e3742002-12-13 20:15:29 +000028#include "command.h"
29#include "vty.h"
30#include "memory.h"
hasso508e53e2004-05-18 18:57:06 +000031#include "if.h"
32#include "filter.h"
33#include "prefix.h"
34#include "plist.h"
pauledd7c242003-06-04 13:59:38 +000035#include "privs.h"
hassoe42f5a32004-08-28 17:04:33 +000036#include "sigevent.h"
Denis Ovsienko87362ce2011-08-27 22:19:34 +040037#include "zclient.h"
paul718e3742002-12-13 20:15:29 +000038
39#include "ospf6d.h"
Denis Ovsienko87362ce2011-08-27 22:19:34 +040040#include "ospf6_top.h"
41#include "ospf6_message.h"
42#include "ospf6_asbr.h"
43#include "ospf6_lsa.h"
paul718e3742002-12-13 20:15:29 +000044
45/* Default configuration file name for ospf6d. */
46#define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
hasso508e53e2004-05-18 18:57:06 +000047
paul718e3742002-12-13 20:15:29 +000048/* Default port values. */
49#define OSPF6_VTY_PORT 2606
50
pauledd7c242003-06-04 13:59:38 +000051/* ospf6d privileges */
hasso508e53e2004-05-18 18:57:06 +000052zebra_capabilities_t _caps_p [] =
pauledd7c242003-06-04 13:59:38 +000053{
paulceacedb2005-09-29 14:39:32 +000054 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000055 ZCAP_BIND
56};
57
58struct zebra_privs_t ospf6d_privs =
59{
pauld81fadf2003-08-14 05:32:12 +000060#if defined(QUAGGA_USER)
61 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000062#endif
pauld81fadf2003-08-14 05:32:12 +000063#if defined QUAGGA_GROUP
64 .group = QUAGGA_GROUP,
65#endif
66#ifdef VTY_GROUP
67 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000068#endif
69 .caps_p = _caps_p,
70 .cap_num_p = 2,
71 .cap_num_i = 0
72};
73
paul718e3742002-12-13 20:15:29 +000074/* ospf6d options, we use GNU getopt library. */
75struct option longopts[] =
76{
77 { "daemon", no_argument, NULL, 'd'},
78 { "config_file", required_argument, NULL, 'f'},
79 { "pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040080 { "socket", required_argument, NULL, 'z'},
paul718e3742002-12-13 20:15:29 +000081 { "vty_addr", required_argument, NULL, 'A'},
82 { "vty_port", required_argument, NULL, 'P'},
hassoc0652302004-11-25 19:33:48 +000083 { "user", required_argument, NULL, 'u'},
84 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000085 { "version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000086 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000087 { "help", no_argument, NULL, 'h'},
88 { 0 }
89};
90
91/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000092char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
93
94/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000095char *progname;
paul718e3742002-12-13 20:15:29 +000096
97/* is daemon? */
98int daemon_mode = 0;
99
100/* Master of threads. */
101struct thread_master *master;
102
103/* Process ID saved for use by init system */
paul0c083ee2004-10-10 12:54:58 +0000104const char *pid_file = PATH_OSPF6D_PID;
paul718e3742002-12-13 20:15:29 +0000105
paul718e3742002-12-13 20:15:29 +0000106/* Help information display. */
107static void
108usage (char *progname, int status)
109{
110 if (status != 0)
111 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
112 else
113 {
114 printf ("Usage : %s [OPTION...]\n\n\
115Daemon which manages OSPF version 3.\n\n\
116-d, --daemon Runs in daemon mode\n\
117-f, --config_file Set configuration file name\n\
118-i, --pid_file Set process identifier file name\n\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400119-z, --socket Set path of zebra socket\n\
paul718e3742002-12-13 20:15:29 +0000120-A, --vty_addr Set vty's bind address\n\
121-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000122-u, --user User to run as\n\
123-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000124-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000125-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000126-h, --help Display this help and exit\n\
127\n\
hasso508e53e2004-05-18 18:57:06 +0000128Report bugs to zebra@zebra.org\n", progname);
paul718e3742002-12-13 20:15:29 +0000129 }
130
131 exit (status);
132}
paul718e3742002-12-13 20:15:29 +0000133
Stephen Hemmingerc143c382011-12-07 01:25:46 +0400134static void __attribute__ ((noreturn))
Tom Goffae2254a2010-11-10 13:01:41 -0800135ospf6_exit (int status)
136{
137 extern struct ospf6 *ospf6;
138 extern struct zclient *zclient;
139
140 if (ospf6)
141 ospf6_delete (ospf6);
142
143 ospf6_message_terminate ();
144 ospf6_asbr_terminate ();
145 ospf6_lsa_terminate ();
146
147 if_terminate ();
148 vty_terminate ();
149 cmd_terminate ();
150
151 if (zclient)
152 zclient_free (zclient);
153
154 if (master)
155 thread_master_free (master);
156
157 if (zlog_default)
158 closezlog (zlog_default);
159
160 exit (status);
161}
162
paul718e3742002-12-13 20:15:29 +0000163/* SIGHUP handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100164static void
hassoe42f5a32004-08-28 17:04:33 +0000165sighup (void)
paul718e3742002-12-13 20:15:29 +0000166{
167 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000168}
169
170/* SIGINT handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100171static void
hassoe42f5a32004-08-28 17:04:33 +0000172sigint (void)
paul718e3742002-12-13 20:15:29 +0000173{
ajs887c44a2004-12-03 16:36:46 +0000174 zlog_notice ("Terminating on signal SIGINT");
Tom Goffae2254a2010-11-10 13:01:41 -0800175 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000176}
177
178/* SIGTERM handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100179static void
hassoe42f5a32004-08-28 17:04:33 +0000180sigterm (void)
paul718e3742002-12-13 20:15:29 +0000181{
ajs887c44a2004-12-03 16:36:46 +0000182 zlog_notice ("Terminating on signal SIGTERM");
Phil Laverdiereef2d5d12012-01-02 20:04:26 +0400183 ospf6_clean();
Tom Goffae2254a2010-11-10 13:01:41 -0800184 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000185}
186
187/* SIGUSR1 handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100188static void
hassoe42f5a32004-08-28 17:04:33 +0000189sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000190{
191 zlog_info ("SIGUSR1 received");
192 zlog_rotate (NULL);
193}
194
hassoe42f5a32004-08-28 17:04:33 +0000195struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000196{
hassoe42f5a32004-08-28 17:04:33 +0000197 {
198 .signal = SIGHUP,
199 .handler = &sighup,
200 },
201 {
202 .signal = SIGINT,
203 .handler = &sigint,
204 },
205 {
206 .signal = SIGTERM,
207 .handler = &sigterm,
208 },
209 {
210 .signal = SIGUSR1,
211 .handler = &sigusr1,
212 },
213};
hasso508e53e2004-05-18 18:57:06 +0000214
215/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000216 state machine is handled here. */
217int
218main (int argc, char *argv[], char *envp[])
219{
220 char *p;
221 int opt;
222 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000223 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000224 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000225 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000226 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000227
228 /* Set umask before anything for security */
229 umask (0027);
230
231 /* Preserve name of myself. */
232 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
233
paul718e3742002-12-13 20:15:29 +0000234 /* Command line argument treatment. */
235 while (1)
236 {
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400237 opt = getopt_long (argc, argv, "df:i:z:hp:A:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000238
239 if (opt == EOF)
240 break;
241
242 switch (opt)
243 {
244 case 0:
245 break;
246 case 'd':
247 daemon_mode = 1;
248 break;
249 case 'f':
250 config_file = optarg;
251 break;
252 case 'A':
253 vty_addr = optarg;
254 break;
255 case 'i':
256 pid_file = optarg;
257 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400258 case 'z':
259 zclient_serv_path_set (optarg);
260 break;
paul718e3742002-12-13 20:15:29 +0000261 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000262 /* Deal with atoi() returning 0 on failure, and ospf6d not
263 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000264 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000265 {
266 vty_port = 0;
267 break;
hasso508e53e2004-05-18 18:57:06 +0000268 }
paul718e3742002-12-13 20:15:29 +0000269 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000270 if (vty_port <= 0 || vty_port > 0xffff)
271 vty_port = OSPF6_VTY_PORT;
hasso508e53e2004-05-18 18:57:06 +0000272 break;
pauledd7c242003-06-04 13:59:38 +0000273 case 'u':
hassoc0652302004-11-25 19:33:48 +0000274 ospf6d_privs.user = optarg;
pauledd7c242003-06-04 13:59:38 +0000275 break;
hassoc0652302004-11-25 19:33:48 +0000276 case 'g':
277 ospf6d_privs.group = optarg;
278 break;
paul718e3742002-12-13 20:15:29 +0000279 case 'v':
280 print_version (progname);
281 exit (0);
282 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000283 case 'C':
284 dryrun = 1;
285 break;
paul718e3742002-12-13 20:15:29 +0000286 case 'h':
287 usage (progname, 0);
288 break;
289 default:
290 usage (progname, 1);
291 break;
292 }
293 }
294
Vyacheslav Trushkin6fd16202011-12-20 20:52:31 +0400295 if (geteuid () != 0)
296 {
297 errno = EPERM;
298 perror (progname);
299 exit (1);
300 }
301
paul718e3742002-12-13 20:15:29 +0000302 /* thread master */
303 master = thread_master_create ();
304
305 /* Initializations. */
ajs274a4a42004-12-07 15:39:31 +0000306 zlog_default = openzlog (progname, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000307 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000308 LOG_DAEMON);
309 zprivs_init (&ospf6d_privs);
310 /* initialize zebra libraries */
hassoe42f5a32004-08-28 17:04:33 +0000311 signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000312 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000313 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000314 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000315 if_init ();
316 access_list_init ();
317 prefix_list_init ();
318
319 /* initialize ospf6 */
320 ospf6_init ();
321
322 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000323 sort_node ();
324
325 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000326 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000327
Paul Jakma876b8be2006-10-15 23:35:57 +0000328 /* Start execution only if not in dry-run mode */
329 if (dryrun)
330 return(0);
331
Stephen Hemminger065de902009-08-07 11:13:49 -0700332 if (daemon_mode && daemon (0, 0) < 0)
333 {
334 zlog_err("OSPF6d daemon failed: %s", strerror(errno));
335 exit (1);
336 }
paul718e3742002-12-13 20:15:29 +0000337
338 /* pid file create */
paul718e3742002-12-13 20:15:29 +0000339 pid_output (pid_file);
paul718e3742002-12-13 20:15:29 +0000340
hasso508e53e2004-05-18 18:57:06 +0000341 /* Make ospf6 vty socket. */
ajs887c44a2004-12-03 16:36:46 +0000342 if (!vty_port)
343 vty_port = OSPF6_VTY_PORT;
344 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000345
paul718e3742002-12-13 20:15:29 +0000346 /* Print start message */
ajs887c44a2004-12-03 16:36:46 +0000347 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
348 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
paul718e3742002-12-13 20:15:29 +0000349
350 /* Start finite state machine, here we go! */
351 while (thread_fetch (master, &thread))
352 thread_call (&thread);
353
354 /* Log in case thread failed */
355 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000356
357 /* Not reached. */
Tom Goffae2254a2010-11-10 13:01:41 -0800358 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000359}
360
hasso508e53e2004-05-18 18:57:06 +0000361