blob: 1afe84a733afc12965657795ad3b292414a8ff34 [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"
Feng Lu126215c2015-05-22 11:39:58 +020038#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000039
40#include "ospf6d.h"
Denis Ovsienko87362ce2011-08-27 22:19:34 +040041#include "ospf6_top.h"
42#include "ospf6_message.h"
43#include "ospf6_asbr.h"
44#include "ospf6_lsa.h"
Christian Franked9628722013-03-08 21:47:35 +010045#include "ospf6_interface.h"
46#include "ospf6_zebra.h"
paul718e3742002-12-13 20:15:29 +000047
48/* Default configuration file name for ospf6d. */
49#define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
hasso508e53e2004-05-18 18:57:06 +000050
paul718e3742002-12-13 20:15:29 +000051/* Default port values. */
52#define OSPF6_VTY_PORT 2606
53
pauledd7c242003-06-04 13:59:38 +000054/* ospf6d privileges */
hasso508e53e2004-05-18 18:57:06 +000055zebra_capabilities_t _caps_p [] =
pauledd7c242003-06-04 13:59:38 +000056{
paulceacedb2005-09-29 14:39:32 +000057 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000058 ZCAP_BIND
59};
60
61struct zebra_privs_t ospf6d_privs =
62{
pauld81fadf2003-08-14 05:32:12 +000063#if defined(QUAGGA_USER)
64 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000065#endif
pauld81fadf2003-08-14 05:32:12 +000066#if defined QUAGGA_GROUP
67 .group = QUAGGA_GROUP,
68#endif
69#ifdef VTY_GROUP
70 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000071#endif
72 .caps_p = _caps_p,
73 .cap_num_p = 2,
74 .cap_num_i = 0
75};
76
paul718e3742002-12-13 20:15:29 +000077/* ospf6d options, we use GNU getopt library. */
78struct option longopts[] =
79{
80 { "daemon", no_argument, NULL, 'd'},
81 { "config_file", required_argument, NULL, 'f'},
82 { "pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040083 { "socket", required_argument, NULL, 'z'},
paul718e3742002-12-13 20:15:29 +000084 { "vty_addr", required_argument, NULL, 'A'},
85 { "vty_port", required_argument, NULL, 'P'},
hassoc0652302004-11-25 19:33:48 +000086 { "user", required_argument, NULL, 'u'},
87 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000088 { "version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000089 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000090 { "help", no_argument, NULL, 'h'},
91 { 0 }
92};
93
94/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000095char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
96
97/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000098char *progname;
paul718e3742002-12-13 20:15:29 +000099
100/* is daemon? */
101int daemon_mode = 0;
102
103/* Master of threads. */
104struct thread_master *master;
105
106/* Process ID saved for use by init system */
paul0c083ee2004-10-10 12:54:58 +0000107const char *pid_file = PATH_OSPF6D_PID;
paul718e3742002-12-13 20:15:29 +0000108
paul718e3742002-12-13 20:15:29 +0000109/* Help information display. */
110static void
111usage (char *progname, int status)
112{
113 if (status != 0)
114 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
115 else
116 {
117 printf ("Usage : %s [OPTION...]\n\n\
118Daemon which manages OSPF version 3.\n\n\
119-d, --daemon Runs in daemon mode\n\
120-f, --config_file Set configuration file name\n\
121-i, --pid_file Set process identifier file name\n\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400122-z, --socket Set path of zebra socket\n\
paul718e3742002-12-13 20:15:29 +0000123-A, --vty_addr Set vty's bind address\n\
124-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000125-u, --user User to run as\n\
126-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000127-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000128-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000129-h, --help Display this help and exit\n\
130\n\
Christian Franke4ff3bca2013-03-20 10:50:07 +0000131Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000132 }
133
134 exit (status);
135}
paul718e3742002-12-13 20:15:29 +0000136
Stephen Hemmingerc143c382011-12-07 01:25:46 +0400137static void __attribute__ ((noreturn))
Tom Goffae2254a2010-11-10 13:01:41 -0800138ospf6_exit (int status)
139{
Christian Franked9628722013-03-08 21:47:35 +0100140 struct listnode *node;
141 struct interface *ifp;
Tom Goffae2254a2010-11-10 13:01:41 -0800142
143 if (ospf6)
144 ospf6_delete (ospf6);
145
Christian Franked9628722013-03-08 21:47:35 +0100146 for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp))
147 if (ifp->info != NULL)
148 ospf6_interface_delete(ifp->info);
149
Tom Goffae2254a2010-11-10 13:01:41 -0800150 ospf6_message_terminate ();
151 ospf6_asbr_terminate ();
152 ospf6_lsa_terminate ();
153
Feng Lu126215c2015-05-22 11:39:58 +0200154 vrf_terminate ();
Tom Goffae2254a2010-11-10 13:01:41 -0800155 vty_terminate ();
156 cmd_terminate ();
157
158 if (zclient)
159 zclient_free (zclient);
160
161 if (master)
162 thread_master_free (master);
163
164 if (zlog_default)
165 closezlog (zlog_default);
166
167 exit (status);
168}
169
paul718e3742002-12-13 20:15:29 +0000170/* SIGHUP handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100171static void
hassoe42f5a32004-08-28 17:04:33 +0000172sighup (void)
paul718e3742002-12-13 20:15:29 +0000173{
174 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000175}
176
177/* SIGINT handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100178static void
hassoe42f5a32004-08-28 17:04:33 +0000179sigint (void)
paul718e3742002-12-13 20:15:29 +0000180{
ajs887c44a2004-12-03 16:36:46 +0000181 zlog_notice ("Terminating on signal SIGINT");
Tom Goffae2254a2010-11-10 13:01:41 -0800182 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000183}
184
185/* SIGTERM handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100186static void
hassoe42f5a32004-08-28 17:04:33 +0000187sigterm (void)
paul718e3742002-12-13 20:15:29 +0000188{
ajs887c44a2004-12-03 16:36:46 +0000189 zlog_notice ("Terminating on signal SIGTERM");
Phil Laverdiereef2d5d12012-01-02 20:04:26 +0400190 ospf6_clean();
Tom Goffae2254a2010-11-10 13:01:41 -0800191 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000192}
193
194/* SIGUSR1 handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100195static void
hassoe42f5a32004-08-28 17:04:33 +0000196sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000197{
198 zlog_info ("SIGUSR1 received");
199 zlog_rotate (NULL);
200}
201
hassoe42f5a32004-08-28 17:04:33 +0000202struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000203{
hassoe42f5a32004-08-28 17:04:33 +0000204 {
205 .signal = SIGHUP,
206 .handler = &sighup,
207 },
208 {
209 .signal = SIGINT,
210 .handler = &sigint,
211 },
212 {
213 .signal = SIGTERM,
214 .handler = &sigterm,
215 },
216 {
217 .signal = SIGUSR1,
218 .handler = &sigusr1,
219 },
220};
hasso508e53e2004-05-18 18:57:06 +0000221
222/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000223 state machine is handled here. */
224int
225main (int argc, char *argv[], char *envp[])
226{
227 char *p;
228 int opt;
229 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000230 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000231 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000232 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000233 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000234
235 /* Set umask before anything for security */
236 umask (0027);
237
238 /* Preserve name of myself. */
239 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
240
paul718e3742002-12-13 20:15:29 +0000241 /* Command line argument treatment. */
242 while (1)
243 {
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400244 opt = getopt_long (argc, argv, "df:i:z:hp:A:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000245
246 if (opt == EOF)
247 break;
248
249 switch (opt)
250 {
251 case 0:
252 break;
253 case 'd':
254 daemon_mode = 1;
255 break;
256 case 'f':
257 config_file = optarg;
258 break;
259 case 'A':
260 vty_addr = optarg;
261 break;
262 case 'i':
263 pid_file = optarg;
264 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400265 case 'z':
266 zclient_serv_path_set (optarg);
267 break;
paul718e3742002-12-13 20:15:29 +0000268 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000269 /* Deal with atoi() returning 0 on failure, and ospf6d not
270 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000271 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000272 {
273 vty_port = 0;
274 break;
hasso508e53e2004-05-18 18:57:06 +0000275 }
paul718e3742002-12-13 20:15:29 +0000276 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000277 if (vty_port <= 0 || vty_port > 0xffff)
278 vty_port = OSPF6_VTY_PORT;
hasso508e53e2004-05-18 18:57:06 +0000279 break;
pauledd7c242003-06-04 13:59:38 +0000280 case 'u':
hassoc0652302004-11-25 19:33:48 +0000281 ospf6d_privs.user = optarg;
pauledd7c242003-06-04 13:59:38 +0000282 break;
hassoc0652302004-11-25 19:33:48 +0000283 case 'g':
284 ospf6d_privs.group = optarg;
285 break;
paul718e3742002-12-13 20:15:29 +0000286 case 'v':
287 print_version (progname);
288 exit (0);
289 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000290 case 'C':
291 dryrun = 1;
292 break;
paul718e3742002-12-13 20:15:29 +0000293 case 'h':
294 usage (progname, 0);
295 break;
296 default:
297 usage (progname, 1);
298 break;
299 }
300 }
301
Vyacheslav Trushkin6fd16202011-12-20 20:52:31 +0400302 if (geteuid () != 0)
303 {
304 errno = EPERM;
305 perror (progname);
306 exit (1);
307 }
308
paul718e3742002-12-13 20:15:29 +0000309 /* thread master */
310 master = thread_master_create ();
311
312 /* Initializations. */
ajs274a4a42004-12-07 15:39:31 +0000313 zlog_default = openzlog (progname, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000314 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000315 LOG_DAEMON);
316 zprivs_init (&ospf6d_privs);
317 /* initialize zebra libraries */
Balaji.G837d16c2012-09-26 14:09:10 +0530318 signal_init (master, array_size(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000319 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000320 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000321 memory_init ();
Feng Lu126215c2015-05-22 11:39:58 +0200322 vrf_init ();
hasso508e53e2004-05-18 18:57:06 +0000323 access_list_init ();
324 prefix_list_init ();
325
326 /* initialize ospf6 */
327 ospf6_init ();
328
paul718e3742002-12-13 20:15:29 +0000329 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000330 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000331
Paul Jakma876b8be2006-10-15 23:35:57 +0000332 /* Start execution only if not in dry-run mode */
333 if (dryrun)
334 return(0);
335
Stephen Hemminger065de902009-08-07 11:13:49 -0700336 if (daemon_mode && daemon (0, 0) < 0)
337 {
338 zlog_err("OSPF6d daemon failed: %s", strerror(errno));
339 exit (1);
340 }
paul718e3742002-12-13 20:15:29 +0000341
342 /* pid file create */
paul718e3742002-12-13 20:15:29 +0000343 pid_output (pid_file);
paul718e3742002-12-13 20:15:29 +0000344
hasso508e53e2004-05-18 18:57:06 +0000345 /* Make ospf6 vty socket. */
ajs887c44a2004-12-03 16:36:46 +0000346 if (!vty_port)
347 vty_port = OSPF6_VTY_PORT;
348 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000349
paul718e3742002-12-13 20:15:29 +0000350 /* Print start message */
ajs887c44a2004-12-03 16:36:46 +0000351 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
352 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
paul718e3742002-12-13 20:15:29 +0000353
354 /* Start finite state machine, here we go! */
355 while (thread_fetch (master, &thread))
356 thread_call (&thread);
357
358 /* Log in case thread failed */
359 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000360
361 /* Not reached. */
Tom Goffae2254a2010-11-10 13:01:41 -0800362 ospf6_exit (0);
paul718e3742002-12-13 20:15:29 +0000363}
364
hasso508e53e2004-05-18 18:57:06 +0000365