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