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
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* message
)
204 if (this->listener
->ike_sa
== ike_sa
)
206 if (!this->callback(this->param
, group
, level
, ike_sa
, message
))
208 this->listener
->status
= NEED_MORE
;
209 listener_done(this->listener
);
214 METHOD(logger_t
, listener_get_level
, level_t
,
215 interface_logger_t
*this, debug_t group
)
217 /* in order to allow callback listeners to decide what they want to log
218 * we request any log message, but only if we actually want logging */
219 return this->callback
== controller_cb_empty ? LEVEL_SILENT
: LEVEL_PRIVATE
;
222 METHOD(job_t
, get_priority_medium
, job_priority_t
,
225 return JOB_PRIO_MEDIUM
;
228 METHOD(listener_t
, ike_state_change
, bool,
229 interface_listener_t
*this, ike_sa_t
*ike_sa
, ike_sa_state_t state
)
231 if (this->ike_sa
== ike_sa
)
236 case IKE_ESTABLISHED
:
237 { /* mediation connections are complete without CHILD_SA */
238 peer_cfg_t
*peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
240 if (peer_cfg
->is_mediation(peer_cfg
))
242 this->status
= SUCCESS
;
243 return listener_done(this);
249 if (ike_sa
->get_state(ike_sa
) == IKE_DELETING
)
250 { /* proper termination */
251 this->status
= SUCCESS
;
253 return listener_done(this);
261 METHOD(listener_t
, child_state_change
, bool,
262 interface_listener_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*child_sa
,
263 child_sa_state_t state
)
265 if (this->ike_sa
== ike_sa
)
269 case CHILD_INSTALLED
:
270 this->status
= SUCCESS
;
271 return listener_done(this);
272 case CHILD_DESTROYING
:
273 switch (child_sa
->get_state(child_sa
))
277 this->status
= SUCCESS
;
282 return listener_done(this);
290 METHOD(job_t
, recheckin
, void,
291 interface_job_t
*job
)
293 if (job
->public.status
== JOB_STATUS_QUEUED
&&
294 job
->listener
.ike_sa
)
296 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
,
297 job
->listener
.ike_sa
);
301 METHOD(controller_t
, create_ike_sa_enumerator
, enumerator_t
*,
302 private_controller_t
*this, bool wait
)
304 return charon
->ike_sa_manager
->create_enumerator(charon
->ike_sa_manager
,
308 METHOD(job_t
, initiate_execute
, job_requeue_t
,
309 interface_job_t
*job
)
312 interface_listener_t
*listener
= &job
->listener
;
313 peer_cfg_t
*peer_cfg
= listener
->peer_cfg
;
315 ike_sa
= charon
->ike_sa_manager
->checkout_by_config(charon
->ike_sa_manager
,
319 listener
->child_cfg
->destroy(listener
->child_cfg
);
320 peer_cfg
->destroy(peer_cfg
);
321 /* trigger down event to release listener */
322 listener
->ike_sa
= charon
->ike_sa_manager
->checkout_new(
323 charon
->ike_sa_manager
, IKE_ANY
, TRUE
);
324 DESTROY_IF(listener
->ike_sa
);
325 listener
->status
= FAILED
;
326 return JOB_REQUEUE_NONE
;
328 listener
->ike_sa
= ike_sa
;
330 if (ike_sa
->get_peer_cfg(ike_sa
) == NULL
)
332 ike_sa
->set_peer_cfg(ike_sa
, peer_cfg
);
334 peer_cfg
->destroy(peer_cfg
);
336 if (ike_sa
->initiate(ike_sa
, listener
->child_cfg
, 0, NULL
, NULL
) == SUCCESS
)
338 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
339 listener
->status
= SUCCESS
;
343 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
345 listener
->status
= FAILED
;
347 return JOB_REQUEUE_NONE
;
350 METHOD(controller_t
, initiate
, status_t
,
351 private_controller_t
*this, peer_cfg_t
*peer_cfg
, child_cfg_t
*child_cfg
,
352 controller_cb_t callback
, void *param
, u_int timeout
)
354 interface_job_t job
= {
357 .ike_state_change
= _ike_state_change
,
358 .child_state_change
= _child_state_change
,
362 .log
= _listener_log
,
363 .get_level
= _listener_get_level
,
365 .callback
= callback
,
369 .child_cfg
= child_cfg
,
370 .peer_cfg
= peer_cfg
,
373 .execute
= _initiate_execute
,
374 .get_priority
= _get_priority_medium
,
375 .destroy
= _recheckin
,
378 job
.listener
.logger
.listener
= &job
.listener
;
380 if (callback
== NULL
)
382 initiate_execute(&job
);
386 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
388 job
.listener
.status
= OUT_OF_RES
;
391 return job
.listener
.status
;
394 METHOD(job_t
, terminate_ike_execute
, job_requeue_t
,
395 interface_job_t
*job
)
397 interface_listener_t
*listener
= &job
->listener
;
398 ike_sa_t
*ike_sa
= listener
->ike_sa
;
400 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
402 if (ike_sa
->delete(ike_sa
) != DESTROY_ME
)
404 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
406 listener
->status
= FAILED
;
410 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
412 listener
->status
= SUCCESS
;
414 return JOB_REQUEUE_NONE
;
417 METHOD(controller_t
, terminate_ike
, status_t
,
418 controller_t
*this, u_int32_t unique_id
,
419 controller_cb_t callback
, void *param
, u_int timeout
)
422 interface_job_t job
= {
425 .ike_state_change
= _ike_state_change
,
426 .child_state_change
= _child_state_change
,
430 .log
= _listener_log
,
431 .get_level
= _listener_get_level
,
433 .callback
= callback
,
440 .execute
= _terminate_ike_execute
,
441 .get_priority
= _get_priority_medium
,
442 .destroy
= _recheckin
,
445 job
.listener
.logger
.listener
= &job
.listener
;
447 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
451 DBG1(DBG_IKE
, "unable to terminate IKE_SA: ID %d not found", unique_id
);
454 job
.listener
.ike_sa
= ike_sa
;
456 if (callback
== NULL
)
458 terminate_ike_execute(&job
);
462 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
464 job
.listener
.status
= OUT_OF_RES
;
466 /* checkin of the ike_sa happened in the thread that executed the job */
467 charon
->bus
->set_sa(charon
->bus
, NULL
);
469 return job
.listener
.status
;
472 METHOD(job_t
, terminate_child_execute
, job_requeue_t
,
473 interface_job_t
*job
)
475 interface_listener_t
*listener
= &job
->listener
;
476 ike_sa_t
*ike_sa
= listener
->ike_sa
;
477 child_sa_t
*child_sa
= listener
->child_sa
;
479 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
480 if (ike_sa
->delete_child_sa(ike_sa
, child_sa
->get_protocol(child_sa
),
481 child_sa
->get_spi(child_sa
, TRUE
), FALSE
) != DESTROY_ME
)
483 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
484 listener
->status
= SUCCESS
;
488 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
490 listener
->status
= FAILED
;
492 return JOB_REQUEUE_NONE
;
495 METHOD(controller_t
, terminate_child
, status_t
,
496 controller_t
*this, u_int32_t reqid
,
497 controller_cb_t callback
, void *param
, u_int timeout
)
500 child_sa_t
*child_sa
;
501 enumerator_t
*enumerator
;
502 interface_job_t job
= {
505 .ike_state_change
= _ike_state_change
,
506 .child_state_change
= _child_state_change
,
510 .log
= _listener_log
,
511 .get_level
= _listener_get_level
,
513 .callback
= callback
,
520 .execute
= _terminate_child_execute
,
521 .get_priority
= _get_priority_medium
,
522 .destroy
= _recheckin
,
525 job
.listener
.logger
.listener
= &job
.listener
;
527 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
531 DBG1(DBG_IKE
, "unable to terminate, CHILD_SA with ID %d not found",
535 job
.listener
.ike_sa
= ike_sa
;
537 enumerator
= ike_sa
->create_child_sa_enumerator(ike_sa
);
538 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
540 if (child_sa
->get_state(child_sa
) != CHILD_ROUTED
&&
541 child_sa
->get_reqid(child_sa
) == reqid
)
547 enumerator
->destroy(enumerator
);
549 if (child_sa
== NULL
)
551 DBG1(DBG_IKE
, "unable to terminate, established "
552 "CHILD_SA with ID %d not found", reqid
);
553 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
556 job
.listener
.child_sa
= child_sa
;
558 if (callback
== NULL
)
560 terminate_child_execute(&job
);
564 if (wait_for_listener(&job
.listener
, &job
.public, timeout
))
566 job
.listener
.status
= OUT_OF_RES
;
568 /* checkin of the ike_sa happened in the thread that executed the job */
569 charon
->bus
->set_sa(charon
->bus
, NULL
);
571 return job
.listener
.status
;
577 bool controller_cb_empty(void *param
, debug_t group
, level_t level
,
578 ike_sa_t
*ike_sa
, char *message
)
583 METHOD(controller_t
, destroy
, void,
584 private_controller_t
*this)
590 * Described in header-file
592 controller_t
*controller_create(void)
594 private_controller_t
*this;
598 .create_ike_sa_enumerator
= _create_ike_sa_enumerator
,
599 .initiate
= _initiate
,
600 .terminate_ike
= _terminate_ike
,
601 .terminate_child
= _terminate_child
,
606 return &this->public;