Merge branch 'ikev1-clean' into ikev1-master
[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
181 this->child_sas->insert_last(this->child_sas, child_sa);
182 }
183 spis->destroy(spis);
184 }
185 }
186 payloads->destroy(payloads);
187 }
188
189 /**
190 * destroy the children listed in this->child_sas, reestablish by policy
191 */
192 static status_t destroy_and_reestablish(private_child_delete_t *this)
193 {
194 enumerator_t *enumerator;
195 child_sa_t *child_sa;
196 child_cfg_t *child_cfg;
197 protocol_id_t protocol;
198 u_int32_t spi;
199 action_t action;
200 status_t status = SUCCESS;
201
202 enumerator = this->child_sas->create_enumerator(this->child_sas);
203 while (enumerator->enumerate(enumerator, (void**)&child_sa))
204 {
205 /* signal child down event if we are not rekeying */
206 if (!this->rekeyed)
207 {
208 charon->bus->child_updown(charon->bus, child_sa, FALSE);
209 }
210 spi = child_sa->get_spi(child_sa, TRUE);
211 protocol = child_sa->get_protocol(child_sa);
212 child_cfg = child_sa->get_config(child_sa);
213 child_cfg->get_ref(child_cfg);
214 action = child_sa->get_close_action(child_sa);
215 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
216 if (this->check_delete_action)
217 { /* enforce child_cfg policy if deleted passively */
218 switch (action)
219 {
220 case ACTION_RESTART:
221 child_cfg->get_ref(child_cfg);
222 status = this->ike_sa->initiate(this->ike_sa, child_cfg, 0,
223 NULL, NULL);
224 break;
225 case ACTION_ROUTE:
226 charon->traps->install(charon->traps,
227 this->ike_sa->get_peer_cfg(this->ike_sa), child_cfg);
228 break;
229 default:
230 break;
231 }
232 }
233 child_cfg->destroy(child_cfg);
234 if (status != SUCCESS)
235 {
236 break;
237 }
238 }
239 enumerator->destroy(enumerator);
240 return status;
241 }
242
243 /**
244 * send closing signals for all CHILD_SAs over the bus
245 */
246 static void log_children(private_child_delete_t *this)
247 {
248 enumerator_t *enumerator;
249 child_sa_t *child_sa;
250 u_int64_t bytes_in, bytes_out;
251
252 enumerator = this->child_sas->create_enumerator(this->child_sas);
253 while (enumerator->enumerate(enumerator, (void**)&child_sa))
254 {
255 if (this->expired)
256 {
257 DBG0(DBG_IKE, "closing expired CHILD_SA %s{%d} "
258 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
259 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
260 ntohl(child_sa->get_spi(child_sa, TRUE)),
261 ntohl(child_sa->get_spi(child_sa, FALSE)),
262 child_sa->get_traffic_selectors(child_sa, TRUE),
263 child_sa->get_traffic_selectors(child_sa, FALSE));
264 }
265 else
266 {
267 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in);
268 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out);
269
270 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} with SPIs %.8x_i "
271 "(%llu bytes) %.8x_o (%llu bytes) and TS %#R=== %#R",
272 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
273 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
274 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
275 child_sa->get_traffic_selectors(child_sa, TRUE),
276 child_sa->get_traffic_selectors(child_sa, FALSE));
277 }
278 }
279 enumerator->destroy(enumerator);
280 }
281
282 METHOD(task_t, build_i, status_t,
283 private_child_delete_t *this, message_t *message)
284 {
285 child_sa_t *child_sa;
286
287 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
288 this->spi, TRUE);
289 if (!child_sa)
290 { /* check if it is an outbound sa */
291 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
292 this->spi, FALSE);
293 if (!child_sa)
294 { /* child does not exist anymore */
295 return SUCCESS;
296 }
297 /* we work only with the inbound SPI */
298 this->spi = child_sa->get_spi(child_sa, TRUE);
299 }
300 this->child_sas->insert_last(this->child_sas, child_sa);
301 if (child_sa->get_state(child_sa) == CHILD_REKEYING)
302 {
303 this->rekeyed = TRUE;
304 }
305 log_children(this);
306 build_payloads(this, message);
307 return NEED_MORE;
308 }
309
310 METHOD(task_t, process_i, status_t,
311 private_child_delete_t *this, message_t *message)
312 {
313 /* flush the list before adding new SAs */
314 this->child_sas->destroy(this->child_sas);
315 this->child_sas = linked_list_create();
316
317 process_payloads(this, message);
318 DBG1(DBG_IKE, "CHILD_SA closed");
319 return destroy_and_reestablish(this);
320 }
321
322 METHOD(task_t, process_r, status_t,
323 private_child_delete_t *this, message_t *message)
324 {
325 process_payloads(this, message);
326 log_children(this);
327 return NEED_MORE;
328 }
329
330 METHOD(task_t, build_r, status_t,
331 private_child_delete_t *this, message_t *message)
332 {
333 /* if we are rekeying, we send an empty informational */
334 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
335 {
336 build_payloads(this, message);
337 }
338 DBG1(DBG_IKE, "CHILD_SA closed");
339 return destroy_and_reestablish(this);
340 }
341
342 METHOD(task_t, get_type, task_type_t,
343 private_child_delete_t *this)
344 {
345 return TASK_CHILD_DELETE;
346 }
347
348 METHOD(child_delete_t , get_child, child_sa_t*,
349 private_child_delete_t *this)
350 {
351 child_sa_t *child_sa = NULL;
352 this->child_sas->get_first(this->child_sas, (void**)&child_sa);
353 return child_sa;
354 }
355
356 METHOD(task_t, migrate, void,
357 private_child_delete_t *this, ike_sa_t *ike_sa)
358 {
359 this->check_delete_action = FALSE;
360 this->ike_sa = ike_sa;
361
362 this->child_sas->destroy(this->child_sas);
363 this->child_sas = linked_list_create();
364 }
365
366 METHOD(task_t, destroy, void,
367 private_child_delete_t *this)
368 {
369 this->child_sas->destroy(this->child_sas);
370 free(this);
371 }
372
373 /*
374 * Described in header.
375 */
376 child_delete_t *child_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
377 u_int32_t spi, bool expired)
378 {
379 private_child_delete_t *this;
380
381 INIT(this,
382 .public = {
383 .task = {
384 .get_type = _get_type,
385 .migrate = _migrate,
386 .destroy = _destroy,
387 },
388 .get_child = _get_child,
389 },
390 .ike_sa = ike_sa,
391 .child_sas = linked_list_create(),
392 .protocol = protocol,
393 .spi = spi,
394 .expired = expired,
395 );
396
397 if (protocol != PROTO_NONE)
398 {
399 this->public.task.build = _build_i;
400 this->public.task.process = _process_i;
401 this->initiator = TRUE;
402 }
403 else
404 {
405 this->public.task.build = _build_r;
406 this->public.task.process = _process_r;
407 this->initiator = FALSE;
408 }
409 return &this->public;
410 }