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(
89 charon
->controller
, TRUE
);
90 while (enumerator
->enumerate(enumerator
, &ike_sa
))
92 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
94 job
= (job_t
*)rekey_ike_sa_job_create(ike_sa
->get_id(ike_sa
), FALSE
);
98 enumerator
->destroy(enumerator
);
102 DBG1(DBG_CFG
, "starting rekey of IKE_SA '%s'", config
);
103 lib
->processor
->queue_job(lib
->processor
, job
);
107 DBG1(DBG_CFG
, "rekeying '%s' failed, IKE_SA not found", config
);
109 return JOB_REQUEUE_NONE
;
115 static job_requeue_t
rekey_child(char *config
)
117 enumerator_t
*enumerator
;
118 iterator_t
*children
;
120 child_sa_t
*child_sa
;
121 u_int32_t reqid
= 0, spi
= 0;
122 protocol_id_t proto
= PROTO_ESP
;
124 enumerator
= charon
->controller
->create_ike_sa_enumerator(
125 charon
->controller
, TRUE
);
126 while (enumerator
->enumerate(enumerator
, &ike_sa
))
128 children
= ike_sa
->create_child_sa_iterator(ike_sa
);
129 while (children
->iterate(children
, (void**)&child_sa
))
131 if (streq(config
, child_sa
->get_name(child_sa
)))
133 reqid
= child_sa
->get_reqid(child_sa
);
134 proto
= child_sa
->get_protocol(child_sa
);
135 spi
= child_sa
->get_spi(child_sa
, TRUE
);
139 children
->destroy(children
);
141 enumerator
->destroy(enumerator
);
144 DBG1(DBG_CFG
, "starting rekey of CHILD_SA '%s'", config
);
145 lib
->processor
->queue_job(lib
->processor
,
146 (job_t
*)rekey_child_sa_job_create(reqid
, proto
, spi
));
150 DBG1(DBG_CFG
, "rekeying '%s' failed, CHILD_SA not found", config
);
152 return JOB_REQUEUE_NONE
;
156 * Do a liveness check
158 static job_requeue_t
liveness(char *config
)
160 enumerator_t
*enumerator
;
164 enumerator
= charon
->controller
->create_ike_sa_enumerator(
165 charon
->controller
, TRUE
);
166 while (enumerator
->enumerate(enumerator
, &ike_sa
))
168 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
170 job
= (job_t
*)send_dpd_job_create(ike_sa
->get_id(ike_sa
));
174 enumerator
->destroy(enumerator
);
178 DBG1(DBG_CFG
, "starting liveness check of IKE_SA '%s'", config
);
179 lib
->processor
->queue_job(lib
->processor
, job
);
183 DBG1(DBG_CFG
, "liveness check for '%s' failed, IKE_SA not found", config
);
185 return JOB_REQUEUE_NONE
;
189 * Close an IKE_SA with all CHILD_SAs
191 static job_requeue_t
close_ike(char *config
)
193 enumerator_t
*enumerator
;
197 enumerator
= charon
->controller
->create_ike_sa_enumerator(
198 charon
->controller
, TRUE
);
199 while (enumerator
->enumerate(enumerator
, &ike_sa
))
201 if (strcaseeq(config
, ike_sa
->get_name(ike_sa
)))
203 id
= ike_sa
->get_unique_id(ike_sa
);
207 enumerator
->destroy(enumerator
);
210 DBG1(DBG_CFG
, "closing IKE_SA '%s'", config
);
211 charon
->controller
->terminate_ike(charon
->controller
, id
, NULL
, NULL
);
215 DBG1(DBG_CFG
, "unable to close IKE_SA '%s', not found", config
);
217 return JOB_REQUEUE_NONE
;
223 static job_requeue_t
close_child(char *config
)
225 enumerator_t
*enumerator
;
226 iterator_t
*children
;
228 child_sa_t
*child_sa
;
231 enumerator
= charon
->controller
->create_ike_sa_enumerator(
232 charon
->controller
, TRUE
);
233 while (enumerator
->enumerate(enumerator
, &ike_sa
))
236 children
= ike_sa
->create_child_sa_iterator(ike_sa
);
237 while (children
->iterate(children
, (void**)&child_sa
))
239 if (streq(config
, child_sa
->get_name(child_sa
)))
241 id
= child_sa
->get_reqid(child_sa
);
245 children
->destroy(children
);
247 enumerator
->destroy(enumerator
);
250 DBG1(DBG_CFG
, "closing CHILD_SA '%s'", config
);
251 charon
->controller
->terminate_child(charon
->controller
, id
, NULL
, NULL
);
255 DBG1(DBG_CFG
, "unable to close CHILD_SA '%s', not found", config
);
257 return JOB_REQUEUE_NONE
;
261 * Load a single action
263 static void load_action(settings_t
*settings
, char *action
)
267 callback_job_cb_t cb
;
269 {"initiate", (void*)initiate
},
270 {"rekey_ike", (void*)rekey_ike
},
271 {"rekey_child", (void*)rekey_child
},
272 {"liveness", (void*)liveness
},
273 {"close_ike", (void*)close_ike
},
274 {"close_child", (void*)close_child
},
279 for (i
= 0; i
< countof(actions
); i
++)
281 if (strncaseeq(actions
[i
].name
, action
, strlen(actions
[i
].name
)))
287 delay
= settings
->get_int(settings
, "actions.%s.delay", 0, action
);
288 config
= settings
->get_str(settings
, "actions.%s.config",
292 DBG1(DBG_CFG
, "no config defined for action '%s'", action
);
295 lib
->scheduler
->schedule_job(lib
->scheduler
,
296 (job_t
*)callback_job_create(actions
[i
].cb
, config
, NULL
, NULL
),
302 DBG1(DBG_CFG
, "unknown action '%s', skipped", action
);
307 * Load configured actions
309 static void load_actions(settings_t
*settings
)
311 enumerator_t
*enumerator
;
314 enumerator
= settings
->create_section_enumerator(settings
, "actions");
315 while (enumerator
->enumerate(enumerator
, &action
))
317 load_action(settings
, action
);
319 enumerator
->destroy(enumerator
);
322 METHOD(actions_t
, destroy
, void,
323 private_actions_t
*this)
331 actions_t
*actions_create()
333 private_actions_t
*this;
341 load_actions(conftest
->test
);
343 return &this->public;