blob: ced2ef2a2615c7f1410606bcb17713eb4b760956 [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 Ovsienko33083542011-08-27 22:19:34 +040037#include "zclient.h"
paul718e3742002-12-13 20:15:29 +000038
39#include "ospf6d.h"
Denis Ovsienko33083542011-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'},
80 { "vty_addr", required_argument, NULL, 'A'},
81 { "vty_port", required_argument, NULL, 'P'},
hassoc0652302004-11-25 19:33:48 +000082 { "user", required_argument, NULL, 'u'},
83 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000084 { "version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000085 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000086 { "help", no_argument, NULL, 'h'},
87 { 0 }
88};
89
90/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000091char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
92
93/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000094char *progname;
paul718e3742002-12-13 20:15:29 +000095
96/* is daemon? */
97int daemon_mode = 0;
98
99/* Master of threads. */
100struct thread_master *master;
101
102/* Process ID saved for use by init system */
paul0c083ee2004-10-10 12:54:58 +0000103const char *pid_file = PATH_OSPF6D_PID;
paul718e3742002-12-13 20:15:29 +0000104
paul718e3742002-12-13 20:15:29 +0000105/* Help information display. */
106static void
107usage (char *progname, int status)
108{
109 if (status != 0)
110 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
111 else
112 {
113 printf ("Usage : %s [OPTION...]\n\n\
114Daemon which manages OSPF version 3.\n\n\
115-d, --daemon Runs in daemon mode\n\
116-f, --config_file Set configuration file name\n\
117-i, --pid_file Set process identifier file name\n\
118-A, --vty_addr Set vty's bind address\n\
119-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000120-u, --user User to run as\n\
121-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000122-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000123-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000124-h, --help Display this help and exit\n\
125\n\
hasso508e53e2004-05-18 18:57:06 +0000126Report bugs to zebra@zebra.org\n", progname);
paul718e3742002-12-13 20:15:29 +0000127 }
128
129 exit (status);
130}
paul718e3742002-12-13 20:15:29 +0000131
132/* SIGHUP handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100133static void
hassoe42f5a32004-08-28 17:04:33 +0000134sighup (void)
paul718e3742002-12-13 20:15:29 +0000135{
136 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000137}
138
139/* SIGINT handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100140static void
hassoe42f5a32004-08-28 17:04:33 +0000141sigint (void)
paul718e3742002-12-13 20:15:29 +0000142{
ajs887c44a2004-12-03 16:36:46 +0000143 zlog_notice ("Terminating on signal SIGINT");
hasso508e53e2004-05-18 18:57:06 +0000144 exit (0);
paul718e3742002-12-13 20:15:29 +0000145}
146
147/* SIGTERM handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100148static void
hassoe42f5a32004-08-28 17:04:33 +0000149sigterm (void)
paul718e3742002-12-13 20:15:29 +0000150{
ajs887c44a2004-12-03 16:36:46 +0000151 zlog_notice ("Terminating on signal SIGTERM");
hasso508e53e2004-05-18 18:57:06 +0000152 exit (0);
paul718e3742002-12-13 20:15:29 +0000153}
154
155/* SIGUSR1 handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100156static void
hassoe42f5a32004-08-28 17:04:33 +0000157sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000158{
159 zlog_info ("SIGUSR1 received");
160 zlog_rotate (NULL);
161}
162
hassoe42f5a32004-08-28 17:04:33 +0000163struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000164{
hassoe42f5a32004-08-28 17:04:33 +0000165 {
166 .signal = SIGHUP,
167 .handler = &sighup,
168 },
169 {
170 .signal = SIGINT,
171 .handler = &sigint,
172 },
173 {
174 .signal = SIGTERM,
175 .handler = &sigterm,
176 },
177 {
178 .signal = SIGUSR1,
179 .handler = &sigusr1,
180 },
181};
hasso508e53e2004-05-18 18:57:06 +0000182
183/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000184 state machine is handled here. */
185int
186main (int argc, char *argv[], char *envp[])
187{
188 char *p;
189 int opt;
190 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000191 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000192 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000193 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000194 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000195
196 /* Set umask before anything for security */
197 umask (0027);
198
199 /* Preserve name of myself. */
200 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201
paul718e3742002-12-13 20:15:29 +0000202 /* Command line argument treatment. */
203 while (1)
204 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000205 opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000206
207 if (opt == EOF)
208 break;
209
210 switch (opt)
211 {
212 case 0:
213 break;
214 case 'd':
215 daemon_mode = 1;
216 break;
217 case 'f':
218 config_file = optarg;
219 break;
220 case 'A':
221 vty_addr = optarg;
222 break;
223 case 'i':
224 pid_file = optarg;
225 break;
226 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000227 /* Deal with atoi() returning 0 on failure, and ospf6d not
228 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000229 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000230 {
231 vty_port = 0;
232 break;
hasso508e53e2004-05-18 18:57:06 +0000233 }
paul718e3742002-12-13 20:15:29 +0000234 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000235 if (vty_port <= 0 || vty_port > 0xffff)
236 vty_port = OSPF6_VTY_PORT;
hasso508e53e2004-05-18 18:57:06 +0000237 break;
pauledd7c242003-06-04 13:59:38 +0000238 case 'u':
hassoc0652302004-11-25 19:33:48 +0000239 ospf6d_privs.user = optarg;
pauledd7c242003-06-04 13:59:38 +0000240 break;
hassoc0652302004-11-25 19:33:48 +0000241 case 'g':
242 ospf6d_privs.group = optarg;
243 break;
paul718e3742002-12-13 20:15:29 +0000244 case 'v':
245 print_version (progname);
246 exit (0);
247 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000248 case 'C':
249 dryrun = 1;
250 break;
paul718e3742002-12-13 20:15:29 +0000251 case 'h':
252 usage (progname, 0);
253 break;
254 default:
255 usage (progname, 1);
256 break;
257 }
258 }
259
260 /* thread master */
261 master = thread_master_create ();
262
263 /* Initializations. */
ajs274a4a42004-12-07 15:39:31 +0000264 zlog_default = openzlog (progname, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000265 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000266 LOG_DAEMON);
267 zprivs_init (&ospf6d_privs);
268 /* initialize zebra libraries */
hassoe42f5a32004-08-28 17:04:33 +0000269 signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000270 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000271 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000272 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000273 if_init ();
274 access_list_init ();
275 prefix_list_init ();
276
277 /* initialize ospf6 */
278 ospf6_init ();
279
280 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000281 sort_node ();
282
283 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000284 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000285
Paul Jakma876b8be2006-10-15 23:35:57 +0000286 /* Start execution only if not in dry-run mode */
287 if (dryrun)
288 return(0);
289
Stephen Hemminger065de902009-08-07 11:13:49 -0700290 if (daemon_mode && daemon (0, 0) < 0)
291 {
292 zlog_err("OSPF6d daemon failed: %s", strerror(errno));
293 exit (1);
294 }
paul718e3742002-12-13 20:15:29 +0000295
296 /* pid file create */
paul718e3742002-12-13 20:15:29 +0000297 pid_output (pid_file);
paul718e3742002-12-13 20:15:29 +0000298
hasso508e53e2004-05-18 18:57:06 +0000299 /* Make ospf6 vty socket. */
ajs887c44a2004-12-03 16:36:46 +0000300 if (!vty_port)
301 vty_port = OSPF6_VTY_PORT;
302 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000303
paul718e3742002-12-13 20:15:29 +0000304 /* Print start message */
ajs887c44a2004-12-03 16:36:46 +0000305 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
306 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
paul718e3742002-12-13 20:15:29 +0000307
308 /* Start finite state machine, here we go! */
309 while (thread_fetch (master, &thread))
310 thread_call (&thread);
311
312 /* Log in case thread failed */
313 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000314
315 /* Not reached. */
316 exit (0);
317}
318
hasso508e53e2004-05-18 18:57:06 +0000319