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