child-sa: Remove state to track installation of half the SA again
[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 * Whether we are the initiator of the exchange
43 */
44 bool initiator;
45
46 /**
47 * Protocol of CHILD_SA to delete (as initiator)
48 */
49 protocol_id_t protocol;
50
51 /**
52 * Inbound SPI of CHILD_SA to delete (as initiator)
53 */
54 uint32_t spi;
55
56 /**
57 * CHILD_SA already expired (as initiator)
58 */
59 bool expired;
60
61 /**
62 * CHILD_SAs which get deleted, entry_t*
63 */
64 linked_list_t *child_sas;
65 };
66
67 /**
68 * Information about a deleted CHILD_SA
69 */
70 typedef struct {
71 /** Deleted CHILD_SA */
72 child_sa_t *child_sa;
73 /** Whether the CHILD_SA was rekeyed */
74 bool rekeyed;
75 /** Whether to enforce any delete action policy */
76 bool check_delete_action;
77 } entry_t;
78
79 /**
80 * Check if the given entry is for the same CHILD_SA
81 */
82 static bool match_child(entry_t *entry, child_sa_t *child_sa)
83 {
84 return entry->child_sa == child_sa;
85 }
86
87 /**
88 * build the delete payloads from the listed child_sas
89 */
90 static void build_payloads(private_child_delete_t *this, message_t *message)
91 {
92 delete_payload_t *ah = NULL, *esp = NULL;
93 enumerator_t *enumerator;
94 entry_t *entry;
95 protocol_id_t protocol;
96 uint32_t spi;
97
98 enumerator = this->child_sas->create_enumerator(this->child_sas);
99 while (enumerator->enumerate(enumerator, (void**)&entry))
100 {
101 protocol = entry->child_sa->get_protocol(entry->child_sa);
102 spi = entry->child_sa->get_spi(entry->child_sa, TRUE);
103
104 switch (protocol)
105 {
106 case PROTO_ESP:
107 if (!esp)
108 {
109 esp = delete_payload_create(PLV2_DELETE, PROTO_ESP);
110 message->add_payload(message, (payload_t*)esp);
111 }
112 esp->add_spi(esp, spi);
113 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
114 protocol_id_names, protocol, ntohl(spi));
115 break;
116 case PROTO_AH:
117 if (ah == NULL)
118 {
119 ah = delete_payload_create(PLV2_DELETE, PROTO_AH);
120 message->add_payload(message, (payload_t*)ah);
121 }
122 ah->add_spi(ah, spi);
123 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
124 protocol_id_names, protocol, ntohl(spi));
125 break;
126 default:
127 break;
128 }
129 entry->child_sa->set_state(entry->child_sa, CHILD_DELETING);
130 }
131 enumerator->destroy(enumerator);
132 }
133
134 /**
135 * Check if the given CHILD_SA is the redundant SA created in a rekey collision.
136 */
137 static bool is_redundant(private_child_delete_t *this, child_sa_t *child)
138 {
139 enumerator_t *tasks;
140 task_t *task;
141
142 tasks = this->ike_sa->create_task_enumerator(this->ike_sa,
143 TASK_QUEUE_ACTIVE);
144 while (tasks->enumerate(tasks, &task))
145 {
146 if (task->get_type(task) == TASK_CHILD_REKEY)
147 {
148 child_rekey_t *rekey = (child_rekey_t*)task;
149
150 if (rekey->is_redundant(rekey, child))
151 {
152 tasks->destroy(tasks);
153 return TRUE;
154 }
155 }
156 }
157 tasks->destroy(tasks);
158 return FALSE;
159 }
160
161 /**
162 * Install the outbound CHILD_SA with the given SPI
163 */
164 static void install_outbound(private_child_delete_t *this,
165 protocol_id_t protocol, uint32_t spi)
166 {
167 child_sa_t *child_sa;
168 linked_list_t *my_ts, *other_ts;
169 status_t status;
170
171 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
172 spi, FALSE);
173 if (!child_sa)
174 {
175 DBG1(DBG_IKE, "CHILD_SA not found after rekeying");
176 return;
177 }
178 if (this->initiator && is_redundant(this, child_sa))
179 { /* if we won the rekey collision we don't want to install the
180 * redundant SA created by the peer */
181 return;
182 }
183
184 status = child_sa->install_outbound(child_sa);
185 if (status != SUCCESS)
186 {
187 DBG1(DBG_IKE, "unable to install outbound IPsec SA (SAD) in kernel");
188 charon->bus->alert(charon->bus, ALERT_INSTALL_CHILD_SA_FAILED,
189 child_sa);
190 /* FIXME: delete the new child_sa? */
191 return;
192 }
193 child_sa->set_state(child_sa, CHILD_INSTALLED);
194
195 my_ts = linked_list_create_from_enumerator(
196 child_sa->create_ts_enumerator(child_sa, TRUE));
197 other_ts = linked_list_create_from_enumerator(
198 child_sa->create_ts_enumerator(child_sa, FALSE));
199
200 DBG0(DBG_IKE, "outbound CHILD_SA %s{%d} established "
201 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
202 child_sa->get_name(child_sa),
203 child_sa->get_unique_id(child_sa),
204 ntohl(child_sa->get_spi(child_sa, TRUE)),
205 ntohl(child_sa->get_spi(child_sa, FALSE)),
206 my_ts, other_ts);
207
208 my_ts->destroy(my_ts);
209 other_ts->destroy(other_ts);
210 }
211
212 /**
213 * read in payloads and find the children to delete
214 */
215 static void process_payloads(private_child_delete_t *this, message_t *message)
216 {
217 enumerator_t *payloads, *spis;
218 payload_t *payload;
219 delete_payload_t *delete_payload;
220 uint32_t spi;
221 protocol_id_t protocol;
222 child_sa_t *child_sa;
223 entry_t *entry;
224
225 payloads = message->create_payload_enumerator(message);
226 while (payloads->enumerate(payloads, &payload))
227 {
228 if (payload->get_type(payload) == PLV2_DELETE)
229 {
230 delete_payload = (delete_payload_t*)payload;
231 protocol = delete_payload->get_protocol_id(delete_payload);
232 if (protocol != PROTO_ESP && protocol != PROTO_AH)
233 {
234 continue;
235 }
236 spis = delete_payload->create_spi_enumerator(delete_payload);
237 while (spis->enumerate(spis, &spi))
238 {
239 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
240 spi, FALSE);
241 if (!child_sa)
242 {
243 DBG1(DBG_IKE, "received DELETE for unknown %N CHILD_SA with"
244 " SPI %.8x", protocol_id_names, protocol, ntohl(spi));
245 continue;
246 }
247 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
248 protocol_id_names, protocol, ntohl(spi));
249
250 if (this->child_sas->find_first(this->child_sas,
251 (void*)match_child, NULL, child_sa) == SUCCESS)
252 {
253 continue;
254 }
255 INIT(entry,
256 .child_sa = child_sa
257 );
258 switch (child_sa->get_state(child_sa))
259 {
260 case CHILD_REKEYED:
261 entry->rekeyed = TRUE;
262 break;
263 case CHILD_DELETING:
264 /* we don't send back a delete if we already initiated
265 * a delete ourself */
266 if (!this->initiator)
267 {
268 free(entry);
269 continue;
270 }
271 break;
272 case CHILD_REKEYING:
273 /* we reply as usual, rekeying will fail */
274 case CHILD_INSTALLED:
275 if (!this->initiator)
276 {
277 if (is_redundant(this, child_sa))
278 {
279 entry->rekeyed = TRUE;
280 }
281 else
282 {
283 entry->check_delete_action = TRUE;
284 }
285 }
286 break;
287 default:
288 break;
289 }
290 this->child_sas->insert_last(this->child_sas, entry);
291 }
292 spis->destroy(spis);
293 }
294 }
295 payloads->destroy(payloads);
296 }
297
298 /**
299 * destroy the children listed in this->child_sas, reestablish by policy
300 */
301 static status_t destroy_and_reestablish(private_child_delete_t *this)
302 {
303 enumerator_t *enumerator;
304 entry_t *entry;
305 child_sa_t *child_sa;
306 child_cfg_t *child_cfg;
307 protocol_id_t protocol;
308 uint32_t spi, reqid, rekey_spi;
309 action_t action;
310 status_t status = SUCCESS;
311
312 enumerator = this->child_sas->create_enumerator(this->child_sas);
313 while (enumerator->enumerate(enumerator, (void**)&entry))
314 {
315 child_sa = entry->child_sa;
316 /* signal child down event if we weren't rekeying */
317 protocol = child_sa->get_protocol(child_sa);
318 if (!entry->rekeyed)
319 {
320 charon->bus->child_updown(charon->bus, child_sa, FALSE);
321 }
322 else
323 {
324 rekey_spi = child_sa->get_rekey_spi(child_sa);
325 if (rekey_spi)
326 {
327 install_outbound(this, protocol, rekey_spi);
328 }
329 }
330 spi = child_sa->get_spi(child_sa, TRUE);
331 reqid = child_sa->get_reqid(child_sa);
332 child_cfg = child_sa->get_config(child_sa);
333 child_cfg->get_ref(child_cfg);
334 action = child_sa->get_close_action(child_sa);
335 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
336 if (entry->check_delete_action)
337 { /* enforce child_cfg policy if deleted passively */
338 switch (action)
339 {
340 case ACTION_RESTART:
341 child_cfg->get_ref(child_cfg);
342 status = this->ike_sa->initiate(this->ike_sa, child_cfg,
343 reqid, NULL, NULL);
344 break;
345 case ACTION_ROUTE:
346 charon->traps->install(charon->traps,
347 this->ike_sa->get_peer_cfg(this->ike_sa), child_cfg,
348 reqid);
349 break;
350 default:
351 break;
352 }
353 }
354 child_cfg->destroy(child_cfg);
355 if (status != SUCCESS)
356 {
357 break;
358 }
359 }
360 enumerator->destroy(enumerator);
361 return status;
362 }
363
364 /**
365 * send closing signals for all CHILD_SAs over the bus
366 */
367 static void log_children(private_child_delete_t *this)
368 {
369 linked_list_t *my_ts, *other_ts;
370 enumerator_t *enumerator;
371 entry_t *entry;
372 child_sa_t *child_sa;
373 uint64_t bytes_in, bytes_out;
374
375 enumerator = this->child_sas->create_enumerator(this->child_sas);
376 while (enumerator->enumerate(enumerator, (void**)&entry))
377 {
378 child_sa = entry->child_sa;
379 my_ts = linked_list_create_from_enumerator(
380 child_sa->create_ts_enumerator(child_sa, TRUE));
381 other_ts = linked_list_create_from_enumerator(
382 child_sa->create_ts_enumerator(child_sa, FALSE));
383 if (this->expired)
384 {
385 DBG0(DBG_IKE, "closing expired CHILD_SA %s{%d} "
386 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
387 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
388 ntohl(child_sa->get_spi(child_sa, TRUE)),
389 ntohl(child_sa->get_spi(child_sa, FALSE)), my_ts, other_ts);
390 }
391 else
392 {
393 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in, NULL);
394 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out, NULL);
395
396 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} with SPIs %.8x_i "
397 "(%llu bytes) %.8x_o (%llu bytes) and TS %#R === %#R",
398 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
399 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
400 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
401 my_ts, other_ts);
402 }
403 my_ts->destroy(my_ts);
404 other_ts->destroy(other_ts);
405 }
406 enumerator->destroy(enumerator);
407 }
408
409 METHOD(task_t, build_i, status_t,
410 private_child_delete_t *this, message_t *message)
411 {
412 child_sa_t *child_sa;
413 entry_t *entry;
414
415 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
416 this->spi, TRUE);
417 if (!child_sa)
418 { /* check if it is an outbound sa */
419 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
420 this->spi, FALSE);
421 if (!child_sa)
422 { /* child does not exist anymore */
423 return SUCCESS;
424 }
425 /* we work only with the inbound SPI */
426 this->spi = child_sa->get_spi(child_sa, TRUE);
427 }
428 INIT(entry,
429 .child_sa = child_sa,
430 );
431 this->child_sas->insert_last(this->child_sas, entry);
432 if (child_sa->get_state(child_sa) == CHILD_REKEYED)
433 {
434 entry->rekeyed = TRUE;
435 }
436 log_children(this);
437 build_payloads(this, message);
438
439 if (!entry->rekeyed && this->expired)
440 {
441 child_cfg_t *child_cfg;
442
443 DBG1(DBG_IKE, "scheduling CHILD_SA recreate after hard expire");
444 child_cfg = child_sa->get_config(child_sa);
445 this->ike_sa->queue_task(this->ike_sa, (task_t*)
446 child_create_create(this->ike_sa, child_cfg->get_ref(child_cfg),
447 FALSE, NULL, NULL));
448 }
449 return NEED_MORE;
450 }
451
452 METHOD(task_t, process_i, status_t,
453 private_child_delete_t *this, message_t *message)
454 {
455 process_payloads(this, message);
456 DBG1(DBG_IKE, "CHILD_SA closed");
457 return destroy_and_reestablish(this);
458 }
459
460 METHOD(task_t, process_r, status_t,
461 private_child_delete_t *this, message_t *message)
462 {
463 process_payloads(this, message);
464 log_children(this);
465 return NEED_MORE;
466 }
467
468 METHOD(task_t, build_r, status_t,
469 private_child_delete_t *this, message_t *message)
470 {
471 build_payloads(this, message);
472 DBG1(DBG_IKE, "CHILD_SA closed");
473 return destroy_and_reestablish(this);
474 }
475
476 METHOD(task_t, get_type, task_type_t,
477 private_child_delete_t *this)
478 {
479 return TASK_CHILD_DELETE;
480 }
481
482 METHOD(child_delete_t , get_child, child_sa_t*,
483 private_child_delete_t *this)
484 {
485 child_sa_t *child_sa = NULL;
486 entry_t *entry;
487
488 if (this->child_sas->get_first(this->child_sas, (void**)&entry) == SUCCESS)
489 {
490 child_sa = entry->child_sa;
491 }
492 return child_sa;
493 }
494
495 METHOD(task_t, migrate, void,
496 private_child_delete_t *this, ike_sa_t *ike_sa)
497 {
498 this->ike_sa = ike_sa;
499
500 this->child_sas->destroy_function(this->child_sas, free);
501 this->child_sas = linked_list_create();
502 }
503
504 METHOD(task_t, destroy, void,
505 private_child_delete_t *this)
506 {
507 this->child_sas->destroy_function(this->child_sas, free);
508 free(this);
509 }
510
511 /*
512 * Described in header.
513 */
514 child_delete_t *child_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
515 uint32_t spi, bool expired)
516 {
517 private_child_delete_t *this;
518
519 INIT(this,
520 .public = {
521 .task = {
522 .get_type = _get_type,
523 .migrate = _migrate,
524 .destroy = _destroy,
525 },
526 .get_child = _get_child,
527 },
528 .ike_sa = ike_sa,
529 .child_sas = linked_list_create(),
530 .protocol = protocol,
531 .spi = spi,
532 .expired = expired,
533 );
534
535 if (protocol != PROTO_NONE)
536 {
537 this->public.task.build = _build_i;
538 this->public.task.process = _process_i;
539 this->initiator = TRUE;
540 }
541 else
542 {
543 this->public.task.build = _build_r;
544 this->public.task.process = _process_r;
545 this->initiator = FALSE;
546 }
547 return &this->public;
548 }