2 * Copyright (C) 2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Copyright (C) 2011 revosec AG
5 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include "controller.h"
20 #include <sys/types.h>
27 #include <threading/thread.h>
28 #include <threading/semaphore.h>
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
;
35 * Private data of an stroke_t object.
37 struct private_controller_t
{
40 * Public part of stroke_t object.
46 * helper struct for the logger interface
48 struct interface_logger_t
{
50 * public logger interface
55 * reference to the listener
57 interface_listener_t
*listener
;
60 * interface callback (listener gets redirected to here)
62 controller_cb_t callback
;
65 * user parameter to pass to callback
71 * helper struct to map listener callbacks to interface callbacks
73 struct interface_listener_t
{
76 * public bus listener interface
83 interface_logger_t logger
;
86 * status of the operation, return to method callers
91 * child configuration, used for initiate
93 child_cfg_t
*child_cfg
;
96 * peer configuration, used for initiate
108 child_sa_t
*child_sa
;
111 * unique ID, used for various methods
116 * semaphore to implement wait_for_listener()
122 typedef struct interface_job_t interface_job_t
;
125 * job for asynchronous listen operations
127 struct interface_job_t
{
135 * associated listener
137 interface_listener_t listener
;
141 * This function properly unregisters a listener that is used
142 * with wait_for_listener()
144 static inline bool listener_done(interface_listener_t
*listener
)
148 listener
->done
->post(listener
->done
);
154 * thread_cleanup_t handler to unregister and cleanup a listener
156 static void listener_cleanup(interface_listener_t
*listener
)
158 charon
->bus
->remove_listener(charon
->bus
, &listener
->public);
159 charon
->bus
->remove_logger(charon
->bus
, &listener
->logger
.public);
160 listener
->done
->destroy(listener
->done
);
164 * Registers the listener, executes the job and then waits synchronously until
165 * the listener is done or the timeout occured.
167 * @note Use 'return listener_done(listener)' to properly unregister a listener
169 * @param listener listener to register
170 * @param job job to execute asynchronously when registered, or NULL
171 * @param timeout max timeout in ms to listen for events, 0 to disable
172 * @return TRUE if timed out
174 static bool wait_for_listener(interface_listener_t
*listener
, job_t
*job
,
177 bool old
, timed_out
= FALSE
;
179 listener
->done
= semaphore_create(0);
181 charon
->bus
->add_logger(charon
->bus
, &listener
->logger
.public);
182 charon
->bus
->add_listener(charon
->bus
, &listener
->public);
183 lib
->processor
->queue_job(lib
->processor
, job
);
185 thread_cleanup_push((thread_cleanup_t
)listener_cleanup
, listener
);
186 old
= thread_cancelability(TRUE
);
189 timed_out
= listener
->done
->timed_wait(listener
->done
, timeout
);
193 listener
->done
->wait(listener
->done
);
195 thread_cancelability(old
);
196 thread_cleanup_pop(TRUE
);
200 METHOD(logger_t
, listener_log
, void,
201 interface_logger_t
*this, debug_t group
, level_t level
, int thread
,
202 ike_sa_t
*ike_sa
, char* format
, va_list args
)
204 if (this->listener
->ike_sa
== ike_sa
)
206 if (!this->callback(this->param
, group
, level
, ike_sa
, format
, args
))
208 this->listener
->status
= NEED_MORE
;
209 listener_done(this->listener
);
214 METHOD(job_t
, get_priority_medium
, job_priority_t
,
217 return JOB_PRIO_MEDIUM
;
220 METHOD(listener_t
, ike_state_change
, bool,
221 interface_listener_t
*this, ike_sa_t
*ike_sa
, ike_sa_state_t state
)
223 if (this->ike_sa
== ike_sa
)
228 case IKE_ESTABLISHED
:
229 { /* mediation connections are complete without CHILD_SA */
230 peer_cfg_t
*peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
232 if (peer_cfg
->is_mediation(peer_cfg
))
234 this->status
= SUCCESS
;
235 return listener_done(this);
241 if (ike_sa
->get_state(ike_sa
) == IKE_DELETING
)
242 { /* proper termination */
243 this->status
= SUCCESS
;
245 return listener_done(this);
253 METHOD(listener_t
, child_state_change
, bool,
254 interface_listener_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*child_sa
,
255 child_sa_state_t state
)
257 if (this->ike_sa
== ike_sa
)
261 case CHILD_INSTALLED
:
262 this->status
= SUCCESS
;
263 return listener_done(this);
264 case CHILD_DESTROYING
:
265 switch (child_sa
->get_state(child_sa
))
269 this->status
= SUCCESS
;
274 return listener_done(this);
282 METHOD(job_t
, recheckin
, void,
283 interface_job_t
*job
)
285 if (job
->listener
.ike_sa
)
287 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
,
288 job
->listener
.ike_sa
);
292 METHOD(controller_t
, create_ike_sa_enumerator
, enumerator_t
*,
293 private_controller_t
*this, bool wait
)
295 return charon
->ike_sa_manager
->create_enumerator(charon
->ike_sa_manager
,
299 METHOD(job_t
, initiate_execute
, void,
300 interface_job_t
*job
)
303 interface_listener_t
*listener
= &job
->listener
;
304 peer_cfg_t
*peer_cfg
= listener
->peer_cfg
;
306 ike_sa
= charon
->ike_sa_manager
->checkout_by_config(charon
->ike_sa_manager
,
310 listener
->child_cfg
->destroy(listener
->child_cfg
);
311 peer_cfg
->destroy(peer_cfg
);
312 /* trigger down event to release listener */
313 listener
->ike_sa
= charon
->ike_sa_manager
->checkout_new(
314 charon
->ike_sa_manager
, IKE_ANY
, TRUE
);
315 DESTROY_IF(listener
->ike_sa
);
316 listener
->status
= FAILED
;
319 listener
->ike_sa
= ike_sa
;
321 if (ike_sa
->get_peer_cfg(ike_sa
) == NULL
)
323 ike_sa
->set_peer_cfg(ike_sa
, peer_cfg
);
325 peer_cfg
->destroy(peer_cfg
);
327 if (ike_sa
->initiate(ike_sa
, listener
->child_cfg
, 0, NULL
, NULL
) == SUCCESS
)
329 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
330 listener
->status
= SUCCESS
;
334 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
336 listener
->status
= FAILED
;
340 METHOD(controller_t
, initiate
, status_t
,
341 private_controller_t
*this, peer_cfg_t
*peer_cfg
, child_cfg_t
*child_cfg
,
342 controller_cb_t callback
, void *param
, u_int timeout
)
344 interface_job_t job
= {
347 .ike_state_change
= _ike_state_change
,
348 .child_state_change
= _child_state_change
,
352 .log
= _listener_log
,
354 .callback
= callback
,
358 .child_cfg
= child_cfg
,
359 .peer_cfg
= peer_cfg
,
362 .execute
= _initiate_execute
,
363 .get_priority
= _get_priority_medium
,
364 .destroy
= _recheckin
,
367 job
.listener
.logger
.listener
= &job
.listener
;
369 if (callback
== NULL
)
371 initiate_execute(&job
);
375 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
377 job
.listener
.status
= OUT_OF_RES
;
380 return job
.listener
.status
;
383 METHOD(job_t
, terminate_ike_execute
, void,
384 interface_job_t
*job
)
386 interface_listener_t
*listener
= &job
->listener
;
387 ike_sa_t
*ike_sa
= listener
->ike_sa
;
389 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
391 if (ike_sa
->delete(ike_sa
) != DESTROY_ME
)
393 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
395 listener
->status
= FAILED
;
399 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
401 listener
->status
= SUCCESS
;
405 METHOD(controller_t
, terminate_ike
, status_t
,
406 controller_t
*this, u_int32_t unique_id
,
407 controller_cb_t callback
, void *param
, u_int timeout
)
410 interface_job_t job
= {
413 .ike_state_change
= _ike_state_change
,
414 .child_state_change
= _child_state_change
,
418 .log
= _listener_log
,
420 .callback
= callback
,
427 .execute
= _terminate_ike_execute
,
428 .get_priority
= _get_priority_medium
,
429 .destroy
= _recheckin
,
432 job
.listener
.logger
.listener
= &job
.listener
;
434 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
438 DBG1(DBG_IKE
, "unable to terminate IKE_SA: ID %d not found", unique_id
);
441 job
.listener
.ike_sa
= ike_sa
;
443 if (callback
== NULL
)
445 terminate_ike_execute(&job
);
449 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
451 job
.listener
.status
= OUT_OF_RES
;
453 /* checkin of the ike_sa happened in the thread that executed the job */
454 charon
->bus
->set_sa(charon
->bus
, NULL
);
456 return job
.listener
.status
;
459 METHOD(job_t
, terminate_child_execute
, void,
460 interface_job_t
*job
)
462 interface_listener_t
*listener
= &job
->listener
;
463 ike_sa_t
*ike_sa
= listener
->ike_sa
;
464 child_sa_t
*child_sa
= listener
->child_sa
;
466 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
467 if (ike_sa
->delete_child_sa(ike_sa
, child_sa
->get_protocol(child_sa
),
468 child_sa
->get_spi(child_sa
, TRUE
), FALSE
) != DESTROY_ME
)
470 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
471 listener
->status
= SUCCESS
;
475 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
477 listener
->status
= FAILED
;
481 METHOD(controller_t
, terminate_child
, status_t
,
482 controller_t
*this, u_int32_t reqid
,
483 controller_cb_t callback
, void *param
, u_int timeout
)
486 child_sa_t
*child_sa
;
487 enumerator_t
*enumerator
;
488 interface_job_t job
= {
491 .ike_state_change
= _ike_state_change
,
492 .child_state_change
= _child_state_change
,
496 .log
= _listener_log
,
498 .callback
= callback
,
505 .execute
= _terminate_child_execute
,
506 .get_priority
= _get_priority_medium
,
507 .destroy
= _recheckin
,
510 job
.listener
.logger
.listener
= &job
.listener
;
512 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
516 DBG1(DBG_IKE
, "unable to terminate, CHILD_SA with ID %d not found",
520 job
.listener
.ike_sa
= ike_sa
;
522 enumerator
= ike_sa
->create_child_sa_enumerator(ike_sa
);
523 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
525 if (child_sa
->get_state(child_sa
) != CHILD_ROUTED
&&
526 child_sa
->get_reqid(child_sa
) == reqid
)
532 enumerator
->destroy(enumerator
);
534 if (child_sa
== NULL
)
536 DBG1(DBG_IKE
, "unable to terminate, established "
537 "CHILD_SA with ID %d not found", reqid
);
538 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
541 job
.listener
.child_sa
= child_sa
;
543 if (callback
== NULL
)
545 terminate_child_execute(&job
);
549 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
551 job
.listener
.status
= OUT_OF_RES
;
553 /* checkin of the ike_sa happened in the thread that executed the job */
554 charon
->bus
->set_sa(charon
->bus
, NULL
);
556 return job
.listener
.status
;
562 bool controller_cb_empty(void *param
, debug_t group
, level_t level
,
563 ike_sa_t
*ike_sa
, char *format
, va_list args
)
568 METHOD(controller_t
, destroy
, void,
569 private_controller_t
*this)
575 * Described in header-file
577 controller_t
*controller_create(void)
579 private_controller_t
*this;
583 .create_ike_sa_enumerator
= _create_ike_sa_enumerator
,
584 .initiate
= _initiate
,
585 .terminate_ike
= _terminate_ike
,
586 .terminate_child
= _terminate_child
,
591 return &this->public;