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
)
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 charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public);
272 return job
.listener
.status
;
275 METHOD(job_t
, terminate_ike_execute
, void,
276 interface_job_t
*job
)
278 interface_listener_t
*listener
= &job
->listener
;
279 ike_sa_t
*ike_sa
= listener
->ike_sa
;
281 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
283 if (ike_sa
->delete(ike_sa
) != DESTROY_ME
)
285 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
287 listener
->status
= FAILED
;
291 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
293 listener
->status
= SUCCESS
;
297 METHOD(controller_t
, terminate_ike
, status_t
,
298 controller_t
*this, u_int32_t unique_id
,
299 controller_cb_t callback
, void *param
)
302 interface_job_t job
= {
305 .log
= _listener_log
,
306 .ike_state_change
= _ike_state_change
,
307 .child_state_change
= _child_state_change
,
309 .callback
= callback
,
315 .execute
= _terminate_ike_execute
,
316 .get_priority
= _get_priority_medium
,
317 .destroy
= _recheckin
,
321 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
325 DBG1(DBG_IKE
, "unable to terminate IKE_SA: ID %d not found", unique_id
);
328 job
.listener
.ike_sa
= ike_sa
;
330 if (callback
== NULL
)
332 terminate_ike_execute(&job
);
336 charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public);
337 /* checkin of the ike_sa happend in the thread that executed the job */
338 charon
->bus
->set_sa(charon
->bus
, NULL
);
340 return job
.listener
.status
;
343 METHOD(job_t
, terminate_child_execute
, void,
344 interface_job_t
*job
)
346 interface_listener_t
*listener
= &job
->listener
;
347 ike_sa_t
*ike_sa
= listener
->ike_sa
;
348 child_sa_t
*child_sa
= listener
->child_sa
;
350 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
351 if (ike_sa
->delete_child_sa(ike_sa
, child_sa
->get_protocol(child_sa
),
352 child_sa
->get_spi(child_sa
, TRUE
)) != DESTROY_ME
)
354 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
355 listener
->status
= SUCCESS
;
359 charon
->ike_sa_manager
->checkin_and_destroy(charon
->ike_sa_manager
,
361 listener
->status
= FAILED
;
365 METHOD(controller_t
, terminate_child
, status_t
,
366 controller_t
*this, u_int32_t reqid
, controller_cb_t callback
, void *param
)
369 child_sa_t
*child_sa
;
370 enumerator_t
*enumerator
;
371 interface_job_t job
= {
374 .log
= _listener_log
,
375 .ike_state_change
= _ike_state_change
,
376 .child_state_change
= _child_state_change
,
378 .callback
= callback
,
384 .execute
= _terminate_child_execute
,
385 .get_priority
= _get_priority_medium
,
386 .destroy
= _recheckin
,
390 ike_sa
= charon
->ike_sa_manager
->checkout_by_id(charon
->ike_sa_manager
,
394 DBG1(DBG_IKE
, "unable to terminate, CHILD_SA with ID %d not found",
398 job
.listener
.ike_sa
= ike_sa
;
400 enumerator
= ike_sa
->create_child_sa_enumerator(ike_sa
);
401 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
403 if (child_sa
->get_state(child_sa
) != CHILD_ROUTED
&&
404 child_sa
->get_reqid(child_sa
) == reqid
)
410 enumerator
->destroy(enumerator
);
412 if (child_sa
== NULL
)
414 DBG1(DBG_IKE
, "unable to terminate, established "
415 "CHILD_SA with ID %d not found", reqid
);
416 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, ike_sa
);
419 job
.listener
.child_sa
= child_sa
;
421 if (callback
== NULL
)
423 terminate_child_execute(&job
);
427 charon
->bus
->listen(charon
->bus
, &job
.listener
.public, &job
.public);
428 /* checkin of the ike_sa happend in the thread that executed the job */
429 charon
->bus
->set_sa(charon
->bus
, NULL
);
431 return job
.listener
.status
;
437 bool controller_cb_empty(void *param
, debug_t group
, level_t level
,
438 ike_sa_t
*ike_sa
, char *format
, va_list args
)
443 METHOD(controller_t
, destroy
, void,
444 private_controller_t
*this)
450 * Described in header-file
452 controller_t
*controller_create(void)
454 private_controller_t
*this;
458 .create_ike_sa_enumerator
= _create_ike_sa_enumerator
,
459 .initiate
= _initiate
,
460 .terminate_ike
= _terminate_ike
,
461 .terminate_child
= _terminate_child
,
466 return &this->public;