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