child-delete: Remove unnecessary call to destroy_child_sa()
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / child_delete.c
1 /*
2 * Copyright (C) 2006-2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "child_delete.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/delete_payload.h>
20 #include <sa/ikev2/tasks/child_create.h>
21
22
23 typedef struct private_child_delete_t private_child_delete_t;
24
25 /**
26 * Private members of a child_delete_t task.
27 */
28 struct private_child_delete_t {
29
30 /**
31 * Public methods and task_t interface.
32 */
33 child_delete_t public;
34
35 /**
36 * Assigned IKE_SA.
37 */
38 ike_sa_t *ike_sa;
39
40 /**
41 * Are we the initiator?
42 */
43 bool initiator;
44
45 /**
46 * Protocol of CHILD_SA to delete
47 */
48 protocol_id_t protocol;
49
50 /**
51 * Inbound SPI of CHILD_SA to delete
52 */
53 uint32_t spi;
54
55 /**
56 * whether to enforce delete action policy
57 */
58 bool check_delete_action;
59
60 /**
61 * is this delete exchange following a rekey?
62 */
63 bool rekeyed;
64
65 /**
66 * CHILD_SA already expired?
67 */
68 bool expired;
69
70 /**
71 * CHILD_SAs which get deleted
72 */
73 linked_list_t *child_sas;
74 };
75
76 /**
77 * build the delete payloads from the listed child_sas
78 */
79 static void build_payloads(private_child_delete_t *this, message_t *message)
80 {
81 delete_payload_t *ah = NULL, *esp = NULL;
82 enumerator_t *enumerator;
83 child_sa_t *child_sa;
84
85 enumerator = this->child_sas->create_enumerator(this->child_sas);
86 while (enumerator->enumerate(enumerator, (void**)&child_sa))
87 {
88 protocol_id_t protocol = child_sa->get_protocol(child_sa);
89 uint32_t spi = child_sa->get_spi(child_sa, TRUE);
90
91 switch (protocol)
92 {
93 case PROTO_ESP:
94 if (esp == NULL)
95 {
96 esp = delete_payload_create(PLV2_DELETE, PROTO_ESP);
97 message->add_payload(message, (payload_t*)esp);
98 }
99 esp->add_spi(esp, spi);
100 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
101 protocol_id_names, protocol, ntohl(spi));
102 break;
103 case PROTO_AH:
104 if (ah == NULL)
105 {
106 ah = delete_payload_create(PLV2_DELETE, PROTO_AH);
107 message->add_payload(message, (payload_t*)ah);
108 }
109 ah->add_spi(ah, spi);
110 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
111 protocol_id_names, protocol, ntohl(spi));
112 break;
113 default:
114 break;
115 }
116 child_sa->set_state(child_sa, CHILD_DELETING);
117 }
118 enumerator->destroy(enumerator);
119 }
120
121 /**
122 * read in payloads and find the children to delete
123 */
124 static void process_payloads(private_child_delete_t *this, message_t *message)
125 {
126 enumerator_t *payloads, *spis;
127 payload_t *payload;
128 delete_payload_t *delete_payload;
129 uint32_t spi;
130 protocol_id_t protocol;
131 child_sa_t *child_sa;
132
133 payloads = message->create_payload_enumerator(message);
134 while (payloads->enumerate(payloads, &payload))
135 {
136 if (payload->get_type(payload) == PLV2_DELETE)
137 {
138 delete_payload = (delete_payload_t*)payload;
139 protocol = delete_payload->get_protocol_id(delete_payload);
140 if (protocol != PROTO_ESP && protocol != PROTO_AH)
141 {
142 continue;
143 }
144 spis = delete_payload->create_spi_enumerator(delete_payload);
145 while (spis->enumerate(spis, &spi))
146 {
147 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
148 spi, FALSE);
149 if (child_sa == NULL)
150 {
151 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x, "
152 "but no such SA", protocol_id_names, protocol, ntohl(spi));
153 continue;
154 }
155 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
156 protocol_id_names, protocol, ntohl(spi));
157
158 switch (child_sa->get_state(child_sa))
159 {
160 case CHILD_REKEYING:
161 this->rekeyed = TRUE;
162 /* we reply as usual, rekeying will fail */
163 break;
164 case CHILD_DELETING:
165 /* we don't send back a delete if we initiated ourself */
166 if (!this->initiator)
167 {
168 continue;
169 }
170 /* fall through */
171 case CHILD_INSTALLED:
172 if (!this->initiator)
173 { /* reestablish installed children if required */
174 this->check_delete_action = TRUE;
175 }
176 default:
177 break;
178 }
179 if (this->child_sas->find_first(this->child_sas, NULL,
180 (void**)&child_sa) != SUCCESS)
181 {
182 this->child_sas->insert_last(this->child_sas, child_sa);
183 }
184 }
185 spis->destroy(spis);
186 }
187 }
188 payloads->destroy(payloads);
189 }
190
191 /**
192 * destroy the children listed in this->child_sas, reestablish by policy
193 */
194 static status_t destroy_and_reestablish(private_child_delete_t *this)
195 {
196 enumerator_t *enumerator;
197 child_sa_t *child_sa;
198 child_cfg_t *child_cfg;
199 protocol_id_t protocol;
200 uint32_t spi, reqid;
201 action_t action;
202 status_t status = SUCCESS;
203
204 enumerator = this->child_sas->create_enumerator(this->child_sas);
205 while (enumerator->enumerate(enumerator, (void**)&child_sa))
206 {
207 /* signal child down event if we are not rekeying */
208 if (!this->rekeyed)
209 {
210 charon->bus->child_updown(charon->bus, child_sa, FALSE);
211 }
212 spi = child_sa->get_spi(child_sa, TRUE);
213 reqid = child_sa->get_reqid(child_sa);
214 protocol = child_sa->get_protocol(child_sa);
215 child_cfg = child_sa->get_config(child_sa);
216 child_cfg->get_ref(child_cfg);
217 action = child_sa->get_close_action(child_sa);
218 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
219 if (this->check_delete_action)
220 { /* enforce child_cfg policy if deleted passively */
221 switch (action)
222 {
223 case ACTION_RESTART:
224 child_cfg->get_ref(child_cfg);
225 status = this->ike_sa->initiate(this->ike_sa, child_cfg,
226 reqid, NULL, NULL);
227 break;
228 case ACTION_ROUTE:
229 charon->traps->install(charon->traps,
230 this->ike_sa->get_peer_cfg(this->ike_sa), child_cfg,
231 reqid);
232 break;
233 default:
234 break;
235 }
236 }
237 child_cfg->destroy(child_cfg);
238 if (status != SUCCESS)
239 {
240 break;
241 }
242 }
243 enumerator->destroy(enumerator);
244 return status;
245 }
246
247 /**
248 * send closing signals for all CHILD_SAs over the bus
249 */
250 static void log_children(private_child_delete_t *this)
251 {
252 linked_list_t *my_ts, *other_ts;
253 enumerator_t *enumerator;
254 child_sa_t *child_sa;
255 uint64_t bytes_in, bytes_out;
256
257 enumerator = this->child_sas->create_enumerator(this->child_sas);
258 while (enumerator->enumerate(enumerator, (void**)&child_sa))
259 {
260 my_ts = linked_list_create_from_enumerator(
261 child_sa->create_ts_enumerator(child_sa, TRUE));
262 other_ts = linked_list_create_from_enumerator(
263 child_sa->create_ts_enumerator(child_sa, FALSE));
264 if (this->expired)
265 {
266 DBG0(DBG_IKE, "closing expired CHILD_SA %s{%d} "
267 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
268 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
269 ntohl(child_sa->get_spi(child_sa, TRUE)),
270 ntohl(child_sa->get_spi(child_sa, FALSE)), my_ts, other_ts);
271 }
272 else
273 {
274 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in, NULL);
275 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out, NULL);
276
277 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} with SPIs %.8x_i "
278 "(%llu bytes) %.8x_o (%llu bytes) and TS %#R === %#R",
279 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
280 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
281 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
282 my_ts, other_ts);
283 }
284 my_ts->destroy(my_ts);
285 other_ts->destroy(other_ts);
286 }
287 enumerator->destroy(enumerator);
288 }
289
290 METHOD(task_t, build_i, status_t,
291 private_child_delete_t *this, message_t *message)
292 {
293 child_sa_t *child_sa;
294
295 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
296 this->spi, TRUE);
297 if (!child_sa)
298 { /* check if it is an outbound sa */
299 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
300 this->spi, FALSE);
301 if (!child_sa)
302 { /* child does not exist anymore */
303 return SUCCESS;
304 }
305 /* we work only with the inbound SPI */
306 this->spi = child_sa->get_spi(child_sa, TRUE);
307 }
308 this->child_sas->insert_last(this->child_sas, child_sa);
309 if (child_sa->get_state(child_sa) == CHILD_REKEYING)
310 {
311 this->rekeyed = TRUE;
312 }
313 log_children(this);
314 build_payloads(this, message);
315
316 if (!this->rekeyed && this->expired)
317 {
318 child_cfg_t *child_cfg;
319
320 DBG1(DBG_IKE, "scheduling CHILD_SA recreate after hard expire");
321 child_cfg = child_sa->get_config(child_sa);
322 this->ike_sa->queue_task(this->ike_sa, (task_t*)
323 child_create_create(this->ike_sa, child_cfg->get_ref(child_cfg),
324 FALSE, NULL, NULL));
325 }
326 return NEED_MORE;
327 }
328
329 METHOD(task_t, process_i, status_t,
330 private_child_delete_t *this, message_t *message)
331 {
332 process_payloads(this, message);
333 DBG1(DBG_IKE, "CHILD_SA closed");
334 return destroy_and_reestablish(this);
335 }
336
337 METHOD(task_t, process_r, status_t,
338 private_child_delete_t *this, message_t *message)
339 {
340 process_payloads(this, message);
341 log_children(this);
342 return NEED_MORE;
343 }
344
345 METHOD(task_t, build_r, status_t,
346 private_child_delete_t *this, message_t *message)
347 {
348 /* if we are rekeying, we send an empty informational */
349 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
350 {
351 build_payloads(this, message);
352 }
353 DBG1(DBG_IKE, "CHILD_SA closed");
354 return destroy_and_reestablish(this);
355 }
356
357 METHOD(task_t, get_type, task_type_t,
358 private_child_delete_t *this)
359 {
360 return TASK_CHILD_DELETE;
361 }
362
363 METHOD(child_delete_t , get_child, child_sa_t*,
364 private_child_delete_t *this)
365 {
366 child_sa_t *child_sa = NULL;
367 this->child_sas->get_first(this->child_sas, (void**)&child_sa);
368 return child_sa;
369 }
370
371 METHOD(task_t, migrate, void,
372 private_child_delete_t *this, ike_sa_t *ike_sa)
373 {
374 this->check_delete_action = FALSE;
375 this->ike_sa = ike_sa;
376
377 this->child_sas->destroy(this->child_sas);
378 this->child_sas = linked_list_create();
379 }
380
381 METHOD(task_t, destroy, void,
382 private_child_delete_t *this)
383 {
384 this->child_sas->destroy(this->child_sas);
385 free(this);
386 }
387
388 /*
389 * Described in header.
390 */
391 child_delete_t *child_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
392 uint32_t spi, bool expired)
393 {
394 private_child_delete_t *this;
395
396 INIT(this,
397 .public = {
398 .task = {
399 .get_type = _get_type,
400 .migrate = _migrate,
401 .destroy = _destroy,
402 },
403 .get_child = _get_child,
404 },
405 .ike_sa = ike_sa,
406 .child_sas = linked_list_create(),
407 .protocol = protocol,
408 .spi = spi,
409 .expired = expired,
410 );
411
412 if (protocol != PROTO_NONE)
413 {
414 this->public.task.build = _build_i;
415 this->public.task.process = _process_i;
416 this->initiator = TRUE;
417 }
418 else
419 {
420 this->public.task.build = _build_r;
421 this->public.task.process = _process_r;
422 this->initiator = FALSE;
423 }
424 return &this->public;
425 }