daemon: Flush shunts before unloading plugins
[strongswan.git] / src / libcharon / control / controller.c
1 /*
2 * Copyright (C) 2011-2012 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Copyright (C) 2011 revosec AG
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program 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 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "controller.h"
19
20 #include <sys/types.h>
21 #include <dirent.h>
22 #include <sys/stat.h>
23
24 #include <daemon.h>
25 #include <library.h>
26 #include <threading/thread.h>
27 #include <threading/spinlock.h>
28 #include <threading/semaphore.h>
29
30 typedef struct private_controller_t private_controller_t;
31 typedef struct interface_listener_t interface_listener_t;
32 typedef struct interface_logger_t interface_logger_t;
33
34 /**
35 * Private data of an stroke_t object.
36 */
37 struct private_controller_t {
38
39 /**
40 * Public part of stroke_t object.
41 */
42 controller_t public;
43 };
44
45 /**
46 * helper struct for the logger interface
47 */
48 struct interface_logger_t {
49 /**
50 * public logger interface
51 */
52 logger_t public;
53
54 /**
55 * reference to the listener
56 */
57 interface_listener_t *listener;
58
59 /**
60 * interface callback (listener gets redirected to here)
61 */
62 controller_cb_t callback;
63
64 /**
65 * user parameter to pass to callback
66 */
67 void *param;
68 };
69
70 /**
71 * helper struct to map listener callbacks to interface callbacks
72 */
73 struct interface_listener_t {
74
75 /**
76 * public bus listener interface
77 */
78 listener_t public;
79
80 /**
81 * logger interface
82 */
83 interface_logger_t logger;
84
85 /**
86 * status of the operation, return to method callers
87 */
88 status_t status;
89
90 /**
91 * child configuration, used for initiate
92 */
93 child_cfg_t *child_cfg;
94
95 /**
96 * peer configuration, used for initiate
97 */
98 peer_cfg_t *peer_cfg;
99
100 /**
101 * IKE_SA to handle
102 */
103 ike_sa_t *ike_sa;
104
105 /**
106 * unique ID, used for various methods
107 */
108 u_int32_t id;
109
110 /**
111 * semaphore to implement wait_for_listener()
112 */
113 semaphore_t *done;
114
115 /**
116 * spinlock to update the IKE_SA handle properly
117 */
118 spinlock_t *lock;
119 };
120
121
122 typedef struct interface_job_t interface_job_t;
123
124 /**
125 * job for asynchronous listen operations
126 */
127 struct interface_job_t {
128
129 /**
130 * job interface
131 */
132 job_t public;
133
134 /**
135 * associated listener
136 */
137 interface_listener_t listener;
138
139 /**
140 * the job is reference counted as the thread executing a job as well as
141 * the thread waiting in wait_for_listener() require it but either of them
142 * could be done first
143 */
144 refcount_t refcount;
145 };
146
147 /**
148 * This function wakes a thread that is waiting in wait_for_listener(),
149 * either from a listener or from a job.
150 */
151 static inline bool listener_done(interface_listener_t *listener)
152 {
153 if (listener->done)
154 {
155 listener->done->post(listener->done);
156 }
157 return FALSE;
158 }
159
160 /**
161 * thread_cleanup_t handler to unregister a listener.
162 */
163 static void listener_unregister(interface_listener_t *listener)
164 {
165 charon->bus->remove_listener(charon->bus, &listener->public);
166 charon->bus->remove_logger(charon->bus, &listener->logger.public);
167 }
168
169 /**
170 * Registers the listener, executes the job and then waits synchronously until
171 * the listener is done or the timeout occurred.
172 *
173 * @note Use 'return listener_done(listener)' to properly unregister a listener
174 *
175 * @param listener listener to register
176 * @param job job to execute asynchronously when registered, or NULL
177 * @param timeout max timeout in ms to listen for events, 0 to disable
178 * @return TRUE if timed out
179 */
180 static bool wait_for_listener(interface_job_t *job, u_int timeout)
181 {
182 interface_listener_t *listener = &job->listener;
183 bool old, timed_out = FALSE;
184
185 /* avoid that the job is destroyed too early */
186 ref_get(&job->refcount);
187
188 listener->done = semaphore_create(0);
189
190 charon->bus->add_logger(charon->bus, &listener->logger.public);
191 charon->bus->add_listener(charon->bus, &listener->public);
192 lib->processor->queue_job(lib->processor, &job->public);
193
194 thread_cleanup_push((thread_cleanup_t)listener_unregister, listener);
195 old = thread_cancelability(TRUE);
196 if (timeout)
197 {
198 timed_out = listener->done->timed_wait(listener->done, timeout);
199 }
200 else
201 {
202 listener->done->wait(listener->done);
203 }
204 thread_cancelability(old);
205 thread_cleanup_pop(TRUE);
206 return timed_out;
207 }
208
209 METHOD(logger_t, listener_log, void,
210 interface_logger_t *this, debug_t group, level_t level, int thread,
211 ike_sa_t *ike_sa, const char *message)
212 {
213 ike_sa_t *target;
214
215 this->listener->lock->lock(this->listener->lock);
216 target = this->listener->ike_sa;
217 this->listener->lock->unlock(this->listener->lock);
218
219 if (target == ike_sa)
220 {
221 if (!this->callback(this->param, group, level, ike_sa, message))
222 {
223 this->listener->status = NEED_MORE;
224 listener_done(this->listener);
225 }
226 }
227 }
228
229 METHOD(logger_t, listener_get_level, level_t,
230 interface_logger_t *this, debug_t group)
231 {
232 /* in order to allow callback listeners to decide what they want to log
233 * we request any log message, but only if we actually want logging */
234 return this->callback == controller_cb_empty ? LEVEL_SILENT : LEVEL_PRIVATE;
235 }
236
237 METHOD(job_t, get_priority_medium, job_priority_t,
238 job_t *this)
239 {
240 return JOB_PRIO_MEDIUM;
241 }
242
243 METHOD(listener_t, ike_state_change, bool,
244 interface_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state)
245 {
246 ike_sa_t *target;
247
248 this->lock->lock(this->lock);
249 target = this->ike_sa;
250 this->lock->unlock(this->lock);
251
252 if (target == ike_sa)
253 {
254 switch (state)
255 {
256 #ifdef ME
257 case IKE_ESTABLISHED:
258 { /* mediation connections are complete without CHILD_SA */
259 peer_cfg_t *peer_cfg = ike_sa->get_peer_cfg(ike_sa);
260
261 if (peer_cfg->is_mediation(peer_cfg))
262 {
263 this->status = SUCCESS;
264 return listener_done(this);
265 }
266 break;
267 }
268 #endif /* ME */
269 case IKE_DESTROYING:
270 if (ike_sa->get_state(ike_sa) == IKE_DELETING)
271 { /* proper termination */
272 this->status = SUCCESS;
273 }
274 return listener_done(this);
275 default:
276 break;
277 }
278 }
279 return TRUE;
280 }
281
282 METHOD(listener_t, child_state_change, bool,
283 interface_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
284 child_sa_state_t state)
285 {
286 ike_sa_t *target;
287
288 this->lock->lock(this->lock);
289 target = this->ike_sa;
290 this->lock->unlock(this->lock);
291
292 if (target == ike_sa)
293 {
294 switch (state)
295 {
296 case CHILD_INSTALLED:
297 this->status = SUCCESS;
298 return listener_done(this);
299 case CHILD_DESTROYING:
300 switch (child_sa->get_state(child_sa))
301 {
302 case CHILD_DELETING:
303 /* proper delete */
304 this->status = SUCCESS;
305 break;
306 case CHILD_RETRYING:
307 /* retrying with a different DH group; survive another
308 * initiation round */
309 this->status = NEED_MORE;
310 return TRUE;
311 case CHILD_CREATED:
312 if (this->status == NEED_MORE)
313 {
314 this->status = FAILED;
315 return TRUE;
316 }
317 break;
318 default:
319 break;
320 }
321 return listener_done(this);
322 default:
323 break;
324 }
325 }
326 return TRUE;
327 }
328
329 METHOD(job_t, destroy_job, void,
330 interface_job_t *this)
331 {
332 if (ref_put(&this->refcount))
333 {
334 this->listener.lock->destroy(this->listener.lock);
335 DESTROY_IF(this->listener.done);
336 free(this);
337 }
338 }
339
340 METHOD(controller_t, create_ike_sa_enumerator, enumerator_t*,
341 private_controller_t *this, bool wait)
342 {
343 return charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager,
344 wait);
345 }
346
347 METHOD(job_t, initiate_execute, job_requeue_t,
348 interface_job_t *job)
349 {
350 ike_sa_t *ike_sa;
351 interface_listener_t *listener = &job->listener;
352 peer_cfg_t *peer_cfg = listener->peer_cfg;
353
354 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
355 peer_cfg);
356 if (!ike_sa)
357 {
358 listener->child_cfg->destroy(listener->child_cfg);
359 peer_cfg->destroy(peer_cfg);
360 listener->status = FAILED;
361 /* release listener */
362 listener_done(listener);
363 return JOB_REQUEUE_NONE;
364 }
365 listener->lock->lock(listener->lock);
366 listener->ike_sa = ike_sa;
367 listener->lock->unlock(listener->lock);
368
369 if (ike_sa->get_peer_cfg(ike_sa) == NULL)
370 {
371 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
372 }
373 peer_cfg->destroy(peer_cfg);
374
375 if (ike_sa->initiate(ike_sa, listener->child_cfg, 0, NULL, NULL) == SUCCESS)
376 {
377 if (!listener->logger.callback)
378 {
379 listener->status = SUCCESS;
380 }
381 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
382 }
383 else
384 {
385 listener->status = FAILED;
386 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
387 ike_sa);
388 }
389 return JOB_REQUEUE_NONE;
390 }
391
392 METHOD(controller_t, initiate, status_t,
393 private_controller_t *this, peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
394 controller_cb_t callback, void *param, u_int timeout)
395 {
396 interface_job_t *job;
397 status_t status;
398
399 INIT(job,
400 .listener = {
401 .public = {
402 .ike_state_change = _ike_state_change,
403 .child_state_change = _child_state_change,
404 },
405 .logger = {
406 .public = {
407 .log = _listener_log,
408 .get_level = _listener_get_level,
409 },
410 .callback = callback,
411 .param = param,
412 },
413 .status = FAILED,
414 .child_cfg = child_cfg,
415 .peer_cfg = peer_cfg,
416 .lock = spinlock_create(),
417 },
418 .public = {
419 .execute = _initiate_execute,
420 .get_priority = _get_priority_medium,
421 .destroy = _destroy_job,
422 },
423 .refcount = 1,
424 );
425 job->listener.logger.listener = &job->listener;
426 thread_cleanup_push((void*)destroy_job, job);
427
428 if (callback == NULL)
429 {
430 initiate_execute(job);
431 }
432 else
433 {
434 if (wait_for_listener(job, timeout))
435 {
436 job->listener.status = OUT_OF_RES;
437 }
438 }
439 status = job->listener.status;
440 thread_cleanup_pop(TRUE);
441 return status;
442 }
443
444 METHOD(job_t, terminate_ike_execute, job_requeue_t,
445 interface_job_t *job)
446 {
447 interface_listener_t *listener = &job->listener;
448 u_int32_t unique_id = listener->id;
449 ike_sa_t *ike_sa;
450
451 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
452 unique_id);
453 if (!ike_sa)
454 {
455 DBG1(DBG_IKE, "unable to terminate IKE_SA: ID %d not found", unique_id);
456 listener->status = NOT_FOUND;
457 /* release listener */
458 listener_done(listener);
459 return JOB_REQUEUE_NONE;
460 }
461 listener->lock->lock(listener->lock);
462 listener->ike_sa = ike_sa;
463 listener->lock->unlock(listener->lock);
464
465 if (ike_sa->delete(ike_sa) != DESTROY_ME)
466 { /* delete failed */
467 listener->status = FAILED;
468 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
469 }
470 else
471 {
472 if (!listener->logger.callback)
473 {
474 listener->status = SUCCESS;
475 }
476 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
477 ike_sa);
478 }
479 return JOB_REQUEUE_NONE;
480 }
481
482 METHOD(controller_t, terminate_ike, status_t,
483 controller_t *this, u_int32_t unique_id,
484 controller_cb_t callback, void *param, u_int timeout)
485 {
486 interface_job_t *job;
487 status_t status;
488
489 INIT(job,
490 .listener = {
491 .public = {
492 .ike_state_change = _ike_state_change,
493 .child_state_change = _child_state_change,
494 },
495 .logger = {
496 .public = {
497 .log = _listener_log,
498 .get_level = _listener_get_level,
499 },
500 .callback = callback,
501 .param = param,
502 },
503 .status = FAILED,
504 .id = unique_id,
505 .lock = spinlock_create(),
506 },
507 .public = {
508 .execute = _terminate_ike_execute,
509 .get_priority = _get_priority_medium,
510 .destroy = _destroy_job,
511 },
512 .refcount = 1,
513 );
514 job->listener.logger.listener = &job->listener;
515 thread_cleanup_push((void*)destroy_job, job);
516
517 if (callback == NULL)
518 {
519 terminate_ike_execute(job);
520 }
521 else
522 {
523 if (wait_for_listener(job, timeout))
524 {
525 job->listener.status = OUT_OF_RES;
526 }
527 }
528 status = job->listener.status;
529 thread_cleanup_pop(TRUE);
530 return status;
531 }
532
533 METHOD(job_t, terminate_child_execute, job_requeue_t,
534 interface_job_t *job)
535 {
536 interface_listener_t *listener = &job->listener;
537 u_int32_t id = listener->id;
538 child_sa_t *child_sa;
539 ike_sa_t *ike_sa;
540
541 ike_sa = charon->child_sa_manager->checkout_by_id(charon->child_sa_manager,
542 id, &child_sa);
543 if (!ike_sa)
544 {
545 DBG1(DBG_IKE, "unable to terminate, CHILD_SA with ID %d not found", id);
546 listener->status = NOT_FOUND;
547 /* release listener */
548 listener_done(listener);
549 return JOB_REQUEUE_NONE;
550 }
551 listener->lock->lock(listener->lock);
552 listener->ike_sa = ike_sa;
553 listener->lock->unlock(listener->lock);
554
555 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
556 {
557 DBG1(DBG_IKE, "unable to terminate, established "
558 "CHILD_SA with ID %d not found", id);
559 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
560 listener->status = NOT_FOUND;
561 /* release listener */
562 listener_done(listener);
563 return JOB_REQUEUE_NONE;
564 }
565
566 if (ike_sa->delete_child_sa(ike_sa, child_sa->get_protocol(child_sa),
567 child_sa->get_spi(child_sa, TRUE), FALSE) != DESTROY_ME)
568 {
569 if (!listener->logger.callback)
570 {
571 listener->status = SUCCESS;
572 }
573 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
574 }
575 else
576 {
577 listener->status = FAILED;
578 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
579 ike_sa);
580 }
581 return JOB_REQUEUE_NONE;
582 }
583
584 METHOD(controller_t, terminate_child, status_t,
585 controller_t *this, u_int32_t unique_id,
586 controller_cb_t callback, void *param, u_int timeout)
587 {
588 interface_job_t *job;
589 status_t status;
590
591 INIT(job,
592 .listener = {
593 .public = {
594 .ike_state_change = _ike_state_change,
595 .child_state_change = _child_state_change,
596 },
597 .logger = {
598 .public = {
599 .log = _listener_log,
600 .get_level = _listener_get_level,
601 },
602 .callback = callback,
603 .param = param,
604 },
605 .status = FAILED,
606 .id = unique_id,
607 .lock = spinlock_create(),
608 },
609 .public = {
610 .execute = _terminate_child_execute,
611 .get_priority = _get_priority_medium,
612 .destroy = _destroy_job,
613 },
614 .refcount = 1,
615 );
616 job->listener.logger.listener = &job->listener;
617 thread_cleanup_push((void*)destroy_job, job);
618
619 if (callback == NULL)
620 {
621 terminate_child_execute(job);
622 }
623 else
624 {
625 if (wait_for_listener(job, timeout))
626 {
627 job->listener.status = OUT_OF_RES;
628 }
629 }
630 status = job->listener.status;
631 thread_cleanup_pop(TRUE);
632 return status;
633 }
634
635 /**
636 * See header
637 */
638 bool controller_cb_empty(void *param, debug_t group, level_t level,
639 ike_sa_t *ike_sa, const char *message)
640 {
641 return TRUE;
642 }
643
644 METHOD(controller_t, destroy, void,
645 private_controller_t *this)
646 {
647 free(this);
648 }
649
650 /*
651 * Described in header-file
652 */
653 controller_t *controller_create(void)
654 {
655 private_controller_t *this;
656
657 INIT(this,
658 .public = {
659 .create_ike_sa_enumerator = _create_ike_sa_enumerator,
660 .initiate = _initiate,
661 .terminate_ike = _terminate_ike,
662 .terminate_child = _terminate_child,
663 .destroy = _destroy,
664 },
665 );
666
667 return &this->public;
668 }