Implement wait_for_listener in controller_t with semaphores.
[strongswan.git] / src / libcharon / control / controller.c
1 /*
2 * Copyright (C) 2011 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 #include <dlfcn.h>
24
25 #include <daemon.h>
26 #include <library.h>
27 #include <threading/thread.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
33 /**
34 * Private data of an stroke_t object.
35 */
36 struct private_controller_t {
37
38 /**
39 * Public part of stroke_t object.
40 */
41 controller_t public;
42 };
43
44 /**
45 * helper struct to map listener callbacks to interface callbacks
46 */
47 struct interface_listener_t {
48
49 /**
50 * public bus listener interface
51 */
52 listener_t public;
53
54 /**
55 * status of the operation, return to method callers
56 */
57 status_t status;
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 * child configuration, used for initiate
71 */
72 child_cfg_t *child_cfg;
73
74 /**
75 * peer configuration, used for initiate
76 */
77 peer_cfg_t *peer_cfg;
78
79 /**
80 * IKE_SA to handle
81 */
82 ike_sa_t *ike_sa;
83
84 /**
85 * CHILD_SA to handle
86 */
87 child_sa_t *child_sa;
88
89 /**
90 * unique ID, used for various methods
91 */
92 u_int32_t id;
93
94 /**
95 * semaphore to implement wait_for_listener()
96 */
97 semaphore_t *done;
98 };
99
100
101 typedef struct interface_job_t interface_job_t;
102
103 /**
104 * job for asynchronous listen operations
105 */
106 struct interface_job_t {
107
108 /**
109 * job interface
110 */
111 job_t public;
112
113 /**
114 * associated listener
115 */
116 interface_listener_t listener;
117 };
118
119 /**
120 * This function properly unregisters a listener that is used
121 * with wait_for_listener()
122 */
123 static inline bool listener_done(interface_listener_t *listener)
124 {
125 if (listener->done)
126 {
127 listener->done->post(listener->done);
128 }
129 return FALSE;
130 }
131
132 /**
133 * thread_cleanup_t handler to unregister and cleanup a listener
134 */
135 static void listener_cleanup(interface_listener_t *listener)
136 {
137 charon->bus->remove_listener(charon->bus, &listener->public);
138 listener->done->destroy(listener->done);
139 }
140
141 /**
142 * Registers the listener, executes the job and then waits synchronously until
143 * the listener is done or the timeout occured.
144 *
145 * @note Use 'return listener_done(listener)' to properly unregister a listener
146 *
147 * @param listener listener to register
148 * @param job job to execute asynchronously when registered, or NULL
149 * @param timeout max timeout in ms to listen for events, 0 to disable
150 * @return TRUE if timed out
151 */
152 static bool wait_for_listener(interface_listener_t *listener, job_t *job,
153 u_int timeout)
154 {
155 bool old, timed_out = FALSE;
156
157 listener->done = semaphore_create(0);
158
159 charon->bus->add_listener(charon->bus, &listener->public);
160 lib->processor->queue_job(lib->processor, job);
161
162 thread_cleanup_push((thread_cleanup_t)listener_cleanup, listener);
163 old = thread_cancelability(TRUE);
164 if (timeout)
165 {
166 timed_out = listener->done->timed_wait(listener->done, timeout);
167 }
168 else
169 {
170 listener->done->wait(listener->done);
171 }
172 thread_cancelability(old);
173 thread_cleanup_pop(TRUE);
174 return timed_out;
175 }
176
177 METHOD(listener_t, listener_log, bool,
178 interface_listener_t *this, debug_t group, level_t level, int thread,
179 ike_sa_t *ike_sa, char* format, va_list args)
180 {
181 if (this->ike_sa == ike_sa)
182 {
183 if (!this->callback(this->param, group, level, ike_sa, format, args))
184 {
185 return listener_done(this);
186 }
187 }
188 return TRUE;
189 }
190
191 METHOD(job_t, get_priority_medium, job_priority_t,
192 job_t *this)
193 {
194 return JOB_PRIO_MEDIUM;
195 }
196
197 METHOD(listener_t, ike_state_change, bool,
198 interface_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state)
199 {
200 if (this->ike_sa == ike_sa)
201 {
202 switch (state)
203 {
204 #ifdef ME
205 case IKE_ESTABLISHED:
206 { /* mediation connections are complete without CHILD_SA */
207 peer_cfg_t *peer_cfg = ike_sa->get_peer_cfg(ike_sa);
208
209 if (peer_cfg->is_mediation(peer_cfg))
210 {
211 this->status = SUCCESS;
212 return listener_done(this);
213 }
214 break;
215 }
216 #endif /* ME */
217 case IKE_DESTROYING:
218 if (ike_sa->get_state(ike_sa) == IKE_DELETING)
219 { /* proper termination */
220 this->status = SUCCESS;
221 }
222 return listener_done(this);
223 default:
224 break;
225 }
226 }
227 return TRUE;
228 }
229
230 METHOD(listener_t, child_state_change, bool,
231 interface_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
232 child_sa_state_t state)
233 {
234 if (this->ike_sa == ike_sa)
235 {
236 switch (state)
237 {
238 case CHILD_INSTALLED:
239 this->status = SUCCESS;
240 return listener_done(this);
241 case CHILD_DESTROYING:
242 switch (child_sa->get_state(child_sa))
243 {
244 case CHILD_DELETING:
245 /* proper delete */
246 this->status = SUCCESS;
247 break;
248 default:
249 break;
250 }
251 return listener_done(this);
252 default:
253 break;
254 }
255 }
256 return TRUE;
257 }
258
259 METHOD(job_t, recheckin, void,
260 interface_job_t *job)
261 {
262 if (job->listener.ike_sa)
263 {
264 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
265 job->listener.ike_sa);
266 }
267 }
268
269 METHOD(controller_t, create_ike_sa_enumerator, enumerator_t*,
270 private_controller_t *this, bool wait)
271 {
272 return charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager,
273 wait);
274 }
275
276 METHOD(job_t, initiate_execute, void,
277 interface_job_t *job)
278 {
279 ike_sa_t *ike_sa;
280 interface_listener_t *listener = &job->listener;
281 peer_cfg_t *peer_cfg = listener->peer_cfg;
282
283 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
284 peer_cfg);
285 if (!ike_sa)
286 {
287 listener->child_cfg->destroy(listener->child_cfg);
288 peer_cfg->destroy(peer_cfg);
289 /* trigger down event to release listener */
290 listener->ike_sa = charon->ike_sa_manager->checkout_new(
291 charon->ike_sa_manager, IKE_ANY, TRUE);
292 DESTROY_IF(listener->ike_sa);
293 listener->status = FAILED;
294 return;
295 }
296 listener->ike_sa = ike_sa;
297
298 if (ike_sa->get_peer_cfg(ike_sa) == NULL)
299 {
300 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
301 }
302 peer_cfg->destroy(peer_cfg);
303
304 if (ike_sa->initiate(ike_sa, listener->child_cfg, 0, NULL, NULL) == SUCCESS)
305 {
306 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
307 listener->status = SUCCESS;
308 }
309 else
310 {
311 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
312 ike_sa);
313 listener->status = FAILED;
314 }
315 }
316
317 METHOD(controller_t, initiate, status_t,
318 private_controller_t *this, peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
319 controller_cb_t callback, void *param, u_int timeout)
320 {
321 interface_job_t job = {
322 .listener = {
323 .public = {
324 .log = _listener_log,
325 .ike_state_change = _ike_state_change,
326 .child_state_change = _child_state_change,
327 },
328 .callback = callback,
329 .param = param,
330 .status = FAILED,
331 .child_cfg = child_cfg,
332 .peer_cfg = peer_cfg,
333 },
334 .public = {
335 .execute = _initiate_execute,
336 .get_priority = _get_priority_medium,
337 .destroy = _recheckin,
338 },
339 };
340 if (callback == NULL)
341 {
342 initiate_execute(&job);
343 }
344 else
345 {
346 if (wait_for_listener(&job.listener, &job.public, timeout))
347 {
348 job.listener.status = OUT_OF_RES;
349 }
350 }
351 return job.listener.status;
352 }
353
354 METHOD(job_t, terminate_ike_execute, void,
355 interface_job_t *job)
356 {
357 interface_listener_t *listener = &job->listener;
358 ike_sa_t *ike_sa = listener->ike_sa;
359
360 charon->bus->set_sa(charon->bus, ike_sa);
361
362 if (ike_sa->delete(ike_sa) != DESTROY_ME)
363 {
364 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
365 /* delete failed */
366 listener->status = FAILED;
367 }
368 else
369 {
370 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
371 ike_sa);
372 listener->status = SUCCESS;
373 }
374 }
375
376 METHOD(controller_t, terminate_ike, status_t,
377 controller_t *this, u_int32_t unique_id,
378 controller_cb_t callback, void *param, u_int timeout)
379 {
380 ike_sa_t *ike_sa;
381 interface_job_t job = {
382 .listener = {
383 .public = {
384 .log = _listener_log,
385 .ike_state_change = _ike_state_change,
386 .child_state_change = _child_state_change,
387 },
388 .callback = callback,
389 .param = param,
390 .status = FAILED,
391 .id = unique_id,
392 },
393 .public = {
394 .execute = _terminate_ike_execute,
395 .get_priority = _get_priority_medium,
396 .destroy = _recheckin,
397 },
398 };
399
400 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
401 unique_id, FALSE);
402 if (ike_sa == NULL)
403 {
404 DBG1(DBG_IKE, "unable to terminate IKE_SA: ID %d not found", unique_id);
405 return NOT_FOUND;
406 }
407 job.listener.ike_sa = ike_sa;
408
409 if (callback == NULL)
410 {
411 terminate_ike_execute(&job);
412 }
413 else
414 {
415 if (wait_for_listener(&job.listener, &job.public, timeout))
416 {
417 job.listener.status = OUT_OF_RES;
418 }
419 /* checkin of the ike_sa happened in the thread that executed the job */
420 charon->bus->set_sa(charon->bus, NULL);
421 }
422 return job.listener.status;
423 }
424
425 METHOD(job_t, terminate_child_execute, void,
426 interface_job_t *job)
427 {
428 interface_listener_t *listener = &job->listener;
429 ike_sa_t *ike_sa = listener->ike_sa;
430 child_sa_t *child_sa = listener->child_sa;
431
432 charon->bus->set_sa(charon->bus, ike_sa);
433 if (ike_sa->delete_child_sa(ike_sa, child_sa->get_protocol(child_sa),
434 child_sa->get_spi(child_sa, TRUE), FALSE) != DESTROY_ME)
435 {
436 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
437 listener->status = SUCCESS;
438 }
439 else
440 {
441 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
442 ike_sa);
443 listener->status = FAILED;
444 }
445 }
446
447 METHOD(controller_t, terminate_child, status_t,
448 controller_t *this, u_int32_t reqid,
449 controller_cb_t callback, void *param, u_int timeout)
450 {
451 ike_sa_t *ike_sa;
452 child_sa_t *child_sa;
453 enumerator_t *enumerator;
454 interface_job_t job = {
455 .listener = {
456 .public = {
457 .log = _listener_log,
458 .ike_state_change = _ike_state_change,
459 .child_state_change = _child_state_change,
460 },
461 .callback = callback,
462 .param = param,
463 .status = FAILED,
464 .id = reqid,
465 },
466 .public = {
467 .execute = _terminate_child_execute,
468 .get_priority = _get_priority_medium,
469 .destroy = _recheckin,
470 },
471 };
472
473 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
474 reqid, TRUE);
475 if (ike_sa == NULL)
476 {
477 DBG1(DBG_IKE, "unable to terminate, CHILD_SA with ID %d not found",
478 reqid);
479 return NOT_FOUND;
480 }
481 job.listener.ike_sa = ike_sa;
482
483 enumerator = ike_sa->create_child_sa_enumerator(ike_sa);
484 while (enumerator->enumerate(enumerator, (void**)&child_sa))
485 {
486 if (child_sa->get_state(child_sa) != CHILD_ROUTED &&
487 child_sa->get_reqid(child_sa) == reqid)
488 {
489 break;
490 }
491 child_sa = NULL;
492 }
493 enumerator->destroy(enumerator);
494
495 if (child_sa == NULL)
496 {
497 DBG1(DBG_IKE, "unable to terminate, established "
498 "CHILD_SA with ID %d not found", reqid);
499 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
500 return NOT_FOUND;
501 }
502 job.listener.child_sa = child_sa;
503
504 if (callback == NULL)
505 {
506 terminate_child_execute(&job);
507 }
508 else
509 {
510 if (wait_for_listener(&job.listener, &job.public, timeout))
511 {
512 job.listener.status = OUT_OF_RES;
513 }
514 /* checkin of the ike_sa happened in the thread that executed the job */
515 charon->bus->set_sa(charon->bus, NULL);
516 }
517 return job.listener.status;
518 }
519
520 /**
521 * See header
522 */
523 bool controller_cb_empty(void *param, debug_t group, level_t level,
524 ike_sa_t *ike_sa, char *format, va_list args)
525 {
526 return TRUE;
527 }
528
529 METHOD(controller_t, destroy, void,
530 private_controller_t *this)
531 {
532 free(this);
533 }
534
535 /*
536 * Described in header-file
537 */
538 controller_t *controller_create(void)
539 {
540 private_controller_t *this;
541
542 INIT(this,
543 .public = {
544 .create_ike_sa_enumerator = _create_ike_sa_enumerator,
545 .initiate = _initiate,
546 .terminate_ike = _terminate_ike,
547 .terminate_child = _terminate_child,
548 .destroy = _destroy,
549 },
550 );
551
552 return &this->public;
553 }
554