1 /*
2  * OSPFd main routine.
3  *   Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
4  *
5  * This file is part of GNU Zebra.
6  *
7  * GNU Zebra is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * GNU Zebra is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <zebra.h>
23 
24 #include <lib/version.h>
25 #include "getopt.h"
26 #include "thread.h"
27 #include "prefix.h"
28 #include "linklist.h"
29 #include "if.h"
30 #include "vector.h"
31 #include "vty.h"
32 #include "command.h"
33 #include "filter.h"
34 #include "plist.h"
35 #include "stream.h"
36 #include "log.h"
37 #include "memory.h"
38 #include "privs.h"
39 #include "sigevent.h"
40 #include "zclient.h"
41 #include "vrf.h"
42 #include "libfrr.h"
43 #include "routemap.h"
44 
45 #include "ospfd/ospfd.h"
46 #include "ospfd/ospf_interface.h"
47 #include "ospfd/ospf_asbr.h"
48 #include "ospfd/ospf_lsa.h"
49 #include "ospfd/ospf_lsdb.h"
50 #include "ospfd/ospf_neighbor.h"
51 #include "ospfd/ospf_dump.h"
52 #include "ospfd/ospf_route.h"
53 #include "ospfd/ospf_zebra.h"
54 #include "ospfd/ospf_vty.h"
55 #include "ospfd/ospf_bfd.h"
56 #include "ospfd/ospf_errors.h"
57 
58 /* ospfd privileges */
59 zebra_capabilities_t _caps_p[] = {ZCAP_NET_RAW, ZCAP_BIND, ZCAP_NET_ADMIN,
60 				  ZCAP_SYS_ADMIN};
61 
62 struct zebra_privs_t ospfd_privs = {
63 #if defined(FRR_USER) && defined(FRR_GROUP)
64 	.user = FRR_USER,
65 	.group = FRR_GROUP,
66 #endif
67 #if defined(VTY_GROUP)
68 	.vty_group = VTY_GROUP,
69 #endif
70 	.caps_p = _caps_p,
71 	.cap_num_p = array_size(_caps_p),
72 	.cap_num_i = 0};
73 
74 /* OSPFd options. */
75 const struct option longopts[] = {
76 	{"instance", required_argument, NULL, 'n'},
77 	{"apiserver", no_argument, NULL, 'a'},
78 	{0}
79 };
80 
81 /* OSPFd program name */
82 
83 /* Master of threads. */
84 struct thread_master *master;
85 
86 #ifdef SUPPORT_OSPF_API
87 extern int ospf_apiserver_enable;
88 #endif /* SUPPORT_OSPF_API */
89 
90 /* SIGHUP handler. */
sighup(void)91 static void sighup(void)
92 {
93 	zlog_info("SIGHUP received");
94 }
95 
96 /* SIGINT / SIGTERM handler. */
sigint(void)97 static void sigint(void)
98 {
99 	zlog_notice("Terminating on signal");
100 	ospf_terminate();
101 	exit(0);
102 }
103 
104 /* SIGUSR1 handler. */
sigusr1(void)105 static void sigusr1(void)
106 {
107 	zlog_rotate();
108 }
109 
110 struct quagga_signal_t ospf_signals[] = {
111 	{
112 		.signal = SIGHUP,
113 		.handler = &sighup,
114 	},
115 	{
116 		.signal = SIGUSR1,
117 		.handler = &sigusr1,
118 	},
119 	{
120 		.signal = SIGINT,
121 		.handler = &sigint,
122 	},
123 	{
124 		.signal = SIGTERM,
125 		.handler = &sigint,
126 	},
127 };
128 
129 static const struct frr_yang_module_info *const ospfd_yang_modules[] = {
130 	&frr_filter_info,
131 	&frr_interface_info,
132 	&frr_route_map_info,
133 	&frr_vrf_info,
134 };
135 
136 FRR_DAEMON_INFO(ospfd, OSPF, .vty_port = OSPF_VTY_PORT,
137 
138 		.proghelp = "Implementation of the OSPFv2 routing protocol.",
139 
140 		.signals = ospf_signals, .n_signals = array_size(ospf_signals),
141 
142 		.privs = &ospfd_privs, .yang_modules = ospfd_yang_modules,
143 		.n_yang_modules = array_size(ospfd_yang_modules), )
144 
145 /* OSPFd main routine. */
main(int argc,char ** argv)146 int main(int argc, char **argv)
147 {
148 	unsigned short instance = 0;
149 	bool created = false;
150 
151 #ifdef SUPPORT_OSPF_API
152 	/* OSPF apiserver is disabled by default. */
153 	ospf_apiserver_enable = 0;
154 #endif /* SUPPORT_OSPF_API */
155 
156 	frr_preinit(&ospfd_di, argc, argv);
157 	frr_opt_add("n:a", longopts,
158 		    "  -n, --instance     Set the instance id\n"
159 		    "  -a, --apiserver    Enable OSPF apiserver\n");
160 
161 	while (1) {
162 		int opt;
163 
164 		opt = frr_getopt(argc, argv, NULL);
165 
166 		if (opt == EOF)
167 			break;
168 
169 		switch (opt) {
170 		case 'n':
171 			ospfd_di.instance = instance = atoi(optarg);
172 			if (instance < 1)
173 				exit(0);
174 			break;
175 		case 0:
176 			break;
177 #ifdef SUPPORT_OSPF_API
178 		case 'a':
179 			ospf_apiserver_enable = 1;
180 			break;
181 #endif /* SUPPORT_OSPF_API */
182 		default:
183 			frr_help_exit(1);
184 			break;
185 		}
186 	}
187 
188 	/* Invoked by a priviledged user? -- endo. */
189 	if (geteuid() != 0) {
190 		errno = EPERM;
191 		perror(ospfd_di.progname);
192 		exit(1);
193 	}
194 
195 	/* OSPF master init. */
196 	ospf_master_init(frr_init());
197 
198 	/* Initializations. */
199 	master = om->master;
200 
201 	/* Library inits. */
202 	ospf_debug_init();
203 	ospf_vrf_init();
204 
205 	access_list_init();
206 	prefix_list_init();
207 
208 	/* OSPFd inits. */
209 	ospf_if_init();
210 	ospf_zebra_init(master, instance);
211 
212 	/* OSPF vty inits. */
213 	ospf_vty_init();
214 	ospf_vty_show_init();
215 	ospf_vty_clear_init();
216 
217 	/* OSPF BFD init */
218 	ospf_bfd_init();
219 
220 	ospf_route_map_init();
221 	ospf_opaque_init();
222 
223 	/* OSPF errors init */
224 	ospf_error_init();
225 
226 	/*
227 	 * Need to initialize the default ospf structure, so the interface mode
228 	 * commands can be duly processed if they are received before 'router
229 	 * ospf',  when ospfd is restarted
230 	 */
231 	if (instance && !ospf_get_instance(instance, &created)) {
232 		flog_err(EC_OSPF_INIT_FAIL, "OSPF instance init failed: %s",
233 			 strerror(errno));
234 		exit(1);
235 	}
236 
237 	frr_config_fork();
238 	frr_run(master);
239 
240 	/* Not reached. */
241 	return 0;
242 }
243