4 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 #include <sys/types.h>
38 #include <config/credentials/local_credential_store.h>
39 #include <config/connections/local_connection_store.h>
40 #include <config/policies/local_policy_store.h>
43 typedef struct private_daemon_t private_daemon_t
;
46 * Private additions to daemon_t, contains threads and internal functions.
48 struct private_daemon_t
{
50 * Public members of daemon_t.
55 * A logger_t object assigned for daemon things.
60 * Signal set used for signal handling.
65 * The thread_id of main-thread.
67 pthread_t main_thread_id
;
72 * @param this calling object
74 void (*run
) (private_daemon_t
*this);
77 * Initialize the daemon.
79 * @param this calling object
80 * @param strict enforce a strict crl policy
82 void (*initialize
) (private_daemon_t
*this, bool strict
);
87 * @param this calling object
89 void (*destroy
) (private_daemon_t
*this);
93 * One and only instance of the daemon.
98 * Implementation of private_daemon_t.run.
100 static void run(private_daemon_t
*this)
102 /* reselect signals for this thread */
103 sigemptyset(&(this->signal_set
));
104 sigaddset(&(this->signal_set
), SIGINT
);
105 sigaddset(&(this->signal_set
), SIGHUP
);
106 sigaddset(&(this->signal_set
), SIGTERM
);
107 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
114 error
= sigwait(&(this->signal_set
), &signal_number
);
117 this->logger
->log(this->logger
, ERROR
, "Error %d when waiting for signal", error
);
120 switch (signal_number
)
124 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGHUP received. Do nothing");
129 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGINT received. Exit main loop");
133 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGTERM received. Exit main loop");
137 this->logger
->log(this->logger
, CONTROL
, "Unknown signal %d received. Do nothing", signal_number
);
145 * Implementation of daemon_t.kill.
147 static void kill_daemon(private_daemon_t
*this, char *reason
)
149 /* we send SIGTERM, so the daemon can cleanly shut down */
150 this->logger
->log(this->logger
, CONTROL
, "Killing daemon: %s", reason
);
151 if (this->main_thread_id
== pthread_self())
153 /* initialization failed, terminate daemon */
160 this->logger
->log(this->logger
, CONTROL
, "sending SIGTERM to ourself", reason
);
162 /* thread must die, since he produced a ciritcal failure and can't continue */
168 * Implementation of private_daemon_t.initialize.
170 static void initialize(private_daemon_t
*this, bool strict
)
172 credential_store_t
* credentials
;
174 this->public.configuration
= configuration_create();
175 this->public.socket
= socket_create(IKEV2_UDP_PORT
, IKEV2_NATT_PORT
);
176 this->public.interfaces
= interfaces_create(IKEV2_UDP_PORT
);
177 this->public.ike_sa_manager
= ike_sa_manager_create();
178 this->public.job_queue
= job_queue_create();
179 this->public.event_queue
= event_queue_create();
180 this->public.send_queue
= send_queue_create();
181 this->public.connections
= (connection_store_t
*)local_connection_store_create();
182 this->public.policies
= (policy_store_t
*)local_policy_store_create();
183 this->public.credentials
= (credential_store_t
*)local_credential_store_create(strict
);
185 /* load keys, ca certificates and crls */
186 credentials
= this->public.credentials
;
187 credentials
->load_ca_certificates(credentials
);
188 credentials
->load_crls(credentials
);
189 credentials
->load_private_keys(credentials
);
191 /* start building threads, we are multi-threaded NOW */
192 this->public.stroke
= stroke_create();
193 this->public.sender
= sender_create();
194 this->public.receiver
= receiver_create();
195 this->public.scheduler
= scheduler_create();
196 this->public.kernel_interface
= kernel_interface_create();
197 this->public.thread_pool
= thread_pool_create(NUMBER_OF_WORKING_THREADS
);
201 * Destory all initiated objects
203 static void destroy(private_daemon_t
*this)
205 /* destruction is a non trivial task, we need to follow
206 * a strict order to prevent threading issues!
207 * Kill active threads first, except the sender, as
208 * the killed IKE_SA want to send delete messages.
210 if (this->public.receiver
!= NULL
)
211 { /* we don't want to receive anything... */
212 this->public.receiver
->destroy(this->public.receiver
);
214 if (this->public.stroke
!= NULL
)
215 { /* ignore all incoming user requests */
216 this->public.stroke
->destroy(this->public.stroke
);
218 if (this->public.scheduler
!= NULL
)
219 { /* stop scheduing jobs */
220 this->public.scheduler
->destroy(this->public.scheduler
);
222 if (this->public.thread_pool
!= NULL
)
223 { /* stop processing jobs */
224 this->public.thread_pool
->destroy(this->public.thread_pool
);
226 if (this->public.ike_sa_manager
!= NULL
)
227 { /* shut down manager with all IKE SAs */
228 this->public.ike_sa_manager
->destroy(this->public.ike_sa_manager
);
230 if (this->public.kernel_interface
!= NULL
)
231 { /* all child SAs should be down now, so kill kernel interface */
232 this->public.kernel_interface
->destroy(this->public.kernel_interface
);
234 /* destroy other infrastructure */
235 if (this->public.job_queue
!= NULL
)
237 this->public.job_queue
->destroy(this->public.job_queue
);
239 if (this->public.event_queue
!= NULL
)
241 this->public.event_queue
->destroy(this->public.event_queue
);
243 if (this->public.interfaces
!= NULL
)
245 this->public.interfaces
->destroy(this->public.interfaces
);
247 if (this->public.configuration
!= NULL
)
249 this->public.configuration
->destroy(this->public.configuration
);
251 if (this->public.credentials
!= NULL
)
253 this->public.credentials
->destroy(this->public.credentials
);
255 if (this->public.connections
!= NULL
)
257 this->public.connections
->destroy(this->public.connections
);
259 if (this->public.policies
!= NULL
)
261 this->public.policies
->destroy(this->public.policies
);
263 /* we hope the sender could send the outstanding deletes, but
264 * we shut down here at any cost */
265 if (this->public.sender
!= NULL
)
267 this->public.sender
->destroy(this->public.sender
);
269 if (this->public.send_queue
!= NULL
)
271 this->public.send_queue
->destroy(this->public.send_queue
);
273 if (this->public.socket
!= NULL
)
275 this->public.socket
->destroy(this->public.socket
);
280 void signal_handler(int signal
)
288 size
= backtrace(array
, 20);
289 strings
= backtrace_symbols(array
, size
);
290 logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
292 logger
->log(logger
, ERROR
, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size
);
294 for (i
= 0; i
< size
; i
++)
296 logger
->log(logger
, ERROR
, " %s", strings
[i
]);
299 logger
->log(logger
, ERROR
, "Killing ourself hard after SIGSEGV");
304 * @brief Create the daemon.
306 * @return created daemon_t
308 private_daemon_t
*daemon_create(void)
310 private_daemon_t
*this = malloc_thing(private_daemon_t
);
311 struct sigaction action
;
315 this->destroy
= destroy
;
316 this->initialize
= initialize
;
317 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
319 /* NULL members for clean destruction */
320 this->public.socket
= NULL
;
321 this->public.interfaces
= NULL
;
322 this->public.ike_sa_manager
= NULL
;
323 this->public.job_queue
= NULL
;
324 this->public.event_queue
= NULL
;
325 this->public.send_queue
= NULL
;
326 this->public.configuration
= NULL
;
327 this->public.credentials
= NULL
;
328 this->public.connections
= NULL
;
329 this->public.policies
= NULL
;
330 this->public.sender
= NULL
;
331 this->public.receiver
= NULL
;
332 this->public.scheduler
= NULL
;
333 this->public.kernel_interface
= NULL
;
334 this->public.thread_pool
= NULL
;
335 this->public.stroke
= NULL
;
337 this->main_thread_id
= pthread_self();
339 /* setup signal handling for all threads */
340 sigemptyset(&(this->signal_set
));
341 sigaddset(&(this->signal_set
), SIGSEGV
);
342 sigaddset(&(this->signal_set
), SIGINT
);
343 sigaddset(&(this->signal_set
), SIGHUP
);
344 sigaddset(&(this->signal_set
), SIGTERM
);
345 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
347 /* setup SIGSEGV handler for all threads */
348 action
.sa_handler
= signal_handler
;
349 action
.sa_mask
= this->signal_set
;
351 if (sigaction(SIGSEGV
, &action
, NULL
) == -1)
353 this->logger
->log(this->logger
, ERROR
, "signal handler setup for SIGSEGV failed");
358 static void usage(const char *msg
)
360 if (msg
!= NULL
&& *msg
!= '\0')
361 fprintf(stderr
, "%s\n", msg
);
362 fprintf(stderr
, "Usage: charon"
366 " [--strictcrlpolicy]"
369 exit(msg
== NULL?
0 : 1);
374 * Main function, manages the daemon.
376 int main(int argc
, char *argv
[])
378 bool strict_crl_policy
= FALSE
;
380 private_daemon_t
*private_charon
;
384 /* handle arguments */
387 static const struct option long_opts
[] = {
388 { "help", no_argument
, NULL
, 'h' },
389 { "version", no_argument
, NULL
, 'v' },
390 { "use-syslog", no_argument
, NULL
, 'l' },
391 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
395 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
397 /* Note: "breaking" from case terminates loop */
400 case EOF
: /* end of flags */
404 break; /* not actually reached */
406 printf("Linux strongSwan %s\n", VERSION
);
409 logger_manager
->set_output(logger_manager
, ALL_LOGGERS
, NULL
);
412 strict_crl_policy
= TRUE
;
416 break; /* not actually reached */
421 private_charon
= daemon_create();
422 charon
= (daemon_t
*)private_charon
;
424 private_charon
->logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
426 private_charon
->logger
->log(private_charon
->logger
, CONTROL
,
427 "Starting Charon (strongSwan Version %s)", VERSION
);
429 /* initialize daemon */
430 private_charon
->initialize(private_charon
, strict_crl_policy
);
432 /* check/setup PID file */
433 if (stat(PID_FILE
, &stb
) == 0)
435 private_charon
->logger
->log(private_charon
->logger
, ERROR
,
436 "charon already running (\""PID_FILE
"\" exists)");
437 private_charon
->destroy(private_charon
);
440 pid_file
= fopen(PID_FILE
, "w");
443 fprintf(pid_file
, "%d\n", getpid());
448 private_charon
->run(private_charon
);
450 /* normal termination, cleanup and exit */
451 private_charon
->destroy(private_charon
);