2 * Copyright (C) 2007-2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "controller.h"
19 #include <sys/types.h>
28 typedef struct private_controller_t private_controller_t
;
29 typedef struct interface_listener_t interface_listener_t
;
32 * Private data of an stroke_t object.
34 struct private_controller_t
{
37 * Public part of stroke_t object.
43 * helper struct to map listener callbacks to interface callbacks
45 struct interface_listener_t
{
48 * public bus listener interface
53 * status of the operation, return to method callers
58 * interface callback (listener gets redirected to here)
60 controller_cb_t callback
;
63 * user parameter to pass to callback
68 * child configuration, used for initiate
70 child_cfg_t
*child_cfg
;
73 * peer configuration, used for initiate
88 * unique ID, used for various methods
94 typedef struct interface_job_t interface_job_t
;
97 * job for asynchronous listen operations
99 struct interface_job_t
{
107 * associated listener
109 interface_listener_t listener
;
112 METHOD(listener_t
, listener_log
, bool,
113 interface_listener_t
*this, debug_t group
, level_t level
, int thread
,
114 ike_sa_t
*ike_sa
, char* format
, va_list args
)
116 if (this->ike_sa
== ike_sa
)
118 if (!this->callback(this->param
, group
, level
, ike_sa
, format
, args
))
126 METHOD(job_t
, get_priority_medium
, job_priority_t
,
129 return JOB_PRIO_MEDIUM
;
132 METHOD(listener_t
, ike_state_change
, bool,
133 interface_listener_t
*this, ike_sa_t
*ike_sa
, ike_sa_state_t state
)
135 if (this->ike_sa
== ike_sa
)
140 case IKE_ESTABLISHED
:
141 { /* mediation connections are complete without CHILD_SA */
142 peer_cfg_t
*peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
144 if (peer_cfg
->is_mediation(peer_cfg
))
146 this->status
= SUCCESS
;
153 if (ike_sa
->get_state(ike_sa
) == IKE_DELETING
)
154 { /* proper termination */
155 this->status
= SUCCESS
;
165 METHOD(listener_t
, child_state_change
, bool,
166 interface_listener_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*child_sa
,
167 child_sa_state_t state
)
169 if (this->ike_sa
== ike_sa
)
173 case CHILD_INSTALLED
:
174 this->status
= SUCCESS
;
176 case CHILD_DESTROYING
:
177 switch (child_sa
->get_state(child_sa
))
181 this->status
= SUCCESS
;
194 METHOD(job_t
, recheckin
, void,
195 interface_job_t
*job
)
197 if (job
->listener
.ike_sa
)
199 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
,
200 job
->listener
.ike_sa
);
204 METHOD(controller_t
, create_ike_sa_enumerator
, enumerator_t
*,
205 private_controller_t
*this, bool wait
)
207 return charon
->ike_sa_manager
->create_enumerator(charon
->ike_sa_manager
,
211 METHOD(job_t
, initiate_execute
, void,
212 interface_job_t
*job
)
215 interface_listener_t
*listener
= &job
->listener
;
216 peer_cfg_t
*peer_cfg
= listener
->peer_cfg
;
218 ike_sa
= charon
->ike_sa_manager
->checkout_by_config(charon
->ike_sa_manager
,
220 listener
->ike_sa
= ike_sa
;
222 if (ike_sa
->get_peer_cfg(ike_sa
) == NULL
)
224 ike_sa
->set_peer_cfg(ike_sa
, peer_cfg
);
226 peer_cfg
->destroy(peer_cfg
);
228 if (ike_sa
->initiate(ike_sa
, listener
->child_cfg
, 0, NULL
, NULL
) == SUCCESS
)
230 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
231 listener
->status
= SUCCESS
;
235 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
237 listener
->status
= FAILED
;
241 METHOD(controller_t
, initiate
, status_t
,
242 private_controller_t
*this, peer_cfg_t
*peer_cfg
, child_cfg_t
*child_cfg
,
243 controller_cb_t callback
, void *param
, u_int timeout
)
245 interface_job_t job
= {
248 .log
= _listener_log
,
249 .ike_state_change
= _ike_state_change
,
250 .child_state_change
= _child_state_change
,
252 .callback
= callback
,
255 .child_cfg
= child_cfg
,
256 .peer_cfg
= peer_cfg
,
259 .execute
= _initiate_execute
,
260 .get_priority
= _get_priority_medium
,
261 .destroy
= _recheckin
,
264 if (callback
== NULL
)
266 initiate_execute(&job
);
270 if (charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public,
273 job
.listener
.status
= OUT_OF_RES
;
276 return job
.listener
.status
;
279 METHOD(job_t
, terminate_ike_execute
, void,
280 interface_job_t
*job
)
282 interface_listener_t
*listener
= &job
->listener
;
283 ike_sa_t
*ike_sa
= listener
->ike_sa
;
285 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
287 if (ike_sa
->delete(ike_sa
) != DESTROY_ME
)
289 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
291 listener
->status
= FAILED
;
295 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
297 listener
->status
= SUCCESS
;
301 METHOD(controller_t
, terminate_ike
, status_t
,
302 controller_t
*this, u_int32_t unique_id
,
303 controller_cb_t callback
, void *param
, u_int timeout
)
306 interface_job_t job
= {
309 .log
= _listener_log
,
310 .ike_state_change
= _ike_state_change
,
311 .child_state_change
= _child_state_change
,
313 .callback
= callback
,
319 .execute
= _terminate_ike_execute
,
320 .get_priority
= _get_priority_medium
,
321 .destroy
= _recheckin
,
325 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
329 DBG1(DBG_IKE
, "unable to terminate IKE_SA: ID %d not found", unique_id
);
332 job
.listener
.ike_sa
= ike_sa
;
334 if (callback
== NULL
)
336 terminate_ike_execute(&job
);
340 if (charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public,
343 job
.listener
.status
= OUT_OF_RES
;
345 /* checkin of the ike_sa happened in the thread that executed the job */
346 charon
->bus
->set_sa(charon
->bus
, NULL
);
348 return job
.listener
.status
;
351 METHOD(job_t
, terminate_child_execute
, void,
352 interface_job_t
*job
)
354 interface_listener_t
*listener
= &job
->listener
;
355 ike_sa_t
*ike_sa
= listener
->ike_sa
;
356 child_sa_t
*child_sa
= listener
->child_sa
;
358 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
359 if (ike_sa
->delete_child_sa(ike_sa
, child_sa
->get_protocol(child_sa
),
360 child_sa
->get_spi(child_sa
, TRUE
)) != DESTROY_ME
)
362 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
363 listener
->status
= SUCCESS
;
367 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
369 listener
->status
= FAILED
;
373 METHOD(controller_t
, terminate_child
, status_t
,
374 controller_t
*this, u_int32_t reqid
,
375 controller_cb_t callback
, void *param
, u_int timeout
)
378 child_sa_t
*child_sa
;
379 enumerator_t
*enumerator
;
380 interface_job_t job
= {
383 .log
= _listener_log
,
384 .ike_state_change
= _ike_state_change
,
385 .child_state_change
= _child_state_change
,
387 .callback
= callback
,
393 .execute
= _terminate_child_execute
,
394 .get_priority
= _get_priority_medium
,
395 .destroy
= _recheckin
,
399 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
403 DBG1(DBG_IKE
, "unable to terminate, CHILD_SA with ID %d not found",
407 job
.listener
.ike_sa
= ike_sa
;
409 enumerator
= ike_sa
->create_child_sa_enumerator(ike_sa
);
410 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
412 if (child_sa
->get_state(child_sa
) != CHILD_ROUTED
&&
413 child_sa
->get_reqid(child_sa
) == reqid
)
419 enumerator
->destroy(enumerator
);
421 if (child_sa
== NULL
)
423 DBG1(DBG_IKE
, "unable to terminate, established "
424 "CHILD_SA with ID %d not found", reqid
);
425 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
428 job
.listener
.child_sa
= child_sa
;
430 if (callback
== NULL
)
432 terminate_child_execute(&job
);
436 if (charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public,
439 job
.listener
.status
= OUT_OF_RES
;
441 /* checkin of the ike_sa happened in the thread that executed the job */
442 charon
->bus
->set_sa(charon
->bus
, NULL
);
444 return job
.listener
.status
;
450 bool controller_cb_empty(void *param
, debug_t group
, level_t level
,
451 ike_sa_t
*ike_sa
, char *format
, va_list args
)
456 METHOD(controller_t
, destroy
, void,
457 private_controller_t
*this)
463 * Described in header-file
465 controller_t
*controller_create(void)
467 private_controller_t
*this;
471 .create_ike_sa_enumerator
= _create_ike_sa_enumerator
,
472 .initiate
= _initiate
,
473 .terminate_ike
= _terminate_ike
,
474 .terminate_child
= _terminate_child
,
479 return &this->public;