2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include <processing/jobs/callback_job.h>
21 #include <processing/jobs/rekey_ike_sa_job.h>
22 #include <processing/jobs/rekey_child_sa_job.h>
23 #include <processing/jobs/send_dpd_job.h>
25 typedef struct private_actions_t private_actions_t
;
28 * Private data of an actions_t object.
30 struct private_actions_t
{
33 * Public actions_t interface.
41 static job_requeue_t
initiate(char *config
)
44 child_cfg_t
*child_cfg
= NULL
, *current
;
45 enumerator_t
*enumerator
;
47 peer_cfg
= charon
->backends
->get_peer_cfg_by_name(charon
->backends
, config
);
50 DBG1(DBG_CFG
, "initiating '%s' failed, config not found", config
);
51 return JOB_REQUEUE_NONE
;
53 enumerator
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
54 while (enumerator
->enumerate(enumerator
, ¤t
))
56 if (streq(current
->get_name(current
), config
))
59 child_cfg
->get_ref(child_cfg
);
63 enumerator
->destroy(enumerator
);
66 DBG1(DBG_CFG
, "initiating IKE_SA for CHILD_SA config '%s'", config
);
67 charon
->controller
->initiate(charon
->controller
, peer_cfg
, child_cfg
,
72 DBG1(DBG_CFG
, "initiating '%s' failed, CHILD_SA config not found",
76 return JOB_REQUEUE_NONE
;
82 static job_requeue_t
rekey_ike(char *config
)
84 enumerator_t
*enumerator
;
88 enumerator
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
);
89 while (enumerator
->enumerate(enumerator
, &ike_sa
))
91 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
93 job
= (job_t
*)rekey_ike_sa_job_create(ike_sa
->get_id(ike_sa
), FALSE
);
97 enumerator
->destroy(enumerator
);
101 DBG1(DBG_CFG
, "starting rekey of IKE_SA '%s'", config
);
102 lib
->processor
->queue_job(lib
->processor
, job
);
106 DBG1(DBG_CFG
, "rekeying '%s' failed, IKE_SA not found", config
);
108 return JOB_REQUEUE_NONE
;
114 static job_requeue_t
rekey_child(char *config
)
116 enumerator_t
*enumerator
;
117 iterator_t
*children
;
119 child_sa_t
*child_sa
;
120 u_int32_t reqid
= 0, spi
= 0;
121 protocol_id_t proto
= PROTO_ESP
;
123 enumerator
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
);
124 while (enumerator
->enumerate(enumerator
, &ike_sa
))
126 children
= ike_sa
->create_child_sa_iterator(ike_sa
);
127 while (children
->iterate(children
, (void**)&child_sa
))
129 if (streq(config
, child_sa
->get_name(child_sa
)))
131 reqid
= child_sa
->get_reqid(child_sa
);
132 proto
= child_sa
->get_protocol(child_sa
);
133 spi
= child_sa
->get_spi(child_sa
, TRUE
);
137 children
->destroy(children
);
139 enumerator
->destroy(enumerator
);
142 DBG1(DBG_CFG
, "starting rekey of CHILD_SA '%s'", config
);
143 lib
->processor
->queue_job(lib
->processor
,
144 (job_t
*)rekey_child_sa_job_create(reqid
, proto
, spi
));
148 DBG1(DBG_CFG
, "rekeying '%s' failed, CHILD_SA not found", config
);
150 return JOB_REQUEUE_NONE
;
154 * Do a liveness check
156 static job_requeue_t
liveness(char *config
)
158 enumerator_t
*enumerator
;
162 enumerator
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
);
163 while (enumerator
->enumerate(enumerator
, &ike_sa
))
165 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
167 job
= (job_t
*)send_dpd_job_create(ike_sa
->get_id(ike_sa
));
171 enumerator
->destroy(enumerator
);
175 DBG1(DBG_CFG
, "starting liveness check of IKE_SA '%s'", config
);
176 lib
->processor
->queue_job(lib
->processor
, job
);
180 DBG1(DBG_CFG
, "liveness check for '%s' failed, IKE_SA not found", config
);
182 return JOB_REQUEUE_NONE
;
186 * Close an IKE_SA with all CHILD_SAs
188 static job_requeue_t
close_ike(char *config
)
190 enumerator_t
*enumerator
;
194 enumerator
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
);
195 while (enumerator
->enumerate(enumerator
, &ike_sa
))
197 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
199 id
= ike_sa
->get_unique_id(ike_sa
);
203 enumerator
->destroy(enumerator
);
206 DBG1(DBG_CFG
, "closing IKE_SA '%s'", config
);
207 charon
->controller
->terminate_ike(charon
->controller
, id
, NULL
, NULL
);
211 DBG1(DBG_CFG
, "unable to close IKE_SA '%s', not found", config
);
213 return JOB_REQUEUE_NONE
;
219 static job_requeue_t
close_child(char *config
)
221 enumerator_t
*enumerator
;
222 iterator_t
*children
;
224 child_sa_t
*child_sa
;
227 enumerator
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
);
228 while (enumerator
->enumerate(enumerator
, &ike_sa
))
231 children
= ike_sa
->create_child_sa_iterator(ike_sa
);
232 while (children
->iterate(children
, (void**)&child_sa
))
234 if (streq(config
, child_sa
->get_name(child_sa
)))
236 id
= child_sa
->get_reqid(child_sa
);
240 children
->destroy(children
);
242 enumerator
->destroy(enumerator
);
245 DBG1(DBG_CFG
, "closing CHILD_SA '%s'", config
);
246 charon
->controller
->terminate_child(charon
->controller
, id
, NULL
, NULL
);
250 DBG1(DBG_CFG
, "unable to close CHILD_SA '%s', not found", config
);
252 return JOB_REQUEUE_NONE
;
256 * Load a single action
258 static void load_action(settings_t
*settings
, char *action
)
262 callback_job_cb_t cb
;
264 {"initiate", (void*)initiate
},
265 {"rekey_ike", (void*)rekey_ike
},
266 {"rekey_child", (void*)rekey_child
},
267 {"liveness", (void*)liveness
},
268 {"close_ike", (void*)close_ike
},
269 {"close_child", (void*)close_child
},
274 for (i
= 0; i
< countof(actions
); i
++)
276 if (strncaseeq(actions
[i
].name
, action
, strlen(actions
[i
].name
)))
282 delay
= settings
->get_int(settings
, "actions.%s.delay", 0, action
);
283 config
= settings
->get_str(settings
, "actions.%s.config",
287 DBG1(DBG_CFG
, "no config defined for action '%s'", action
);
290 lib
->scheduler
->schedule_job(lib
->scheduler
,
291 (job_t
*)callback_job_create(actions
[i
].cb
, config
, NULL
, NULL
),
297 DBG1(DBG_CFG
, "unknown action '%s', skipped", action
);
302 * Load configured actions
304 static void load_actions(settings_t
*settings
)
306 enumerator_t
*enumerator
;
309 enumerator
= settings
->create_section_enumerator(settings
, "actions");
310 while (enumerator
->enumerate(enumerator
, &action
))
312 load_action(settings
, action
);
314 enumerator
->destroy(enumerator
);
317 METHOD(actions_t
, destroy
, void,
318 private_actions_t
*this)
326 actions_t
*actions_create()
328 private_actions_t
*this;
336 load_actions(conftest
->test
);
338 return &this->public;