Handle incoming delete messages
[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 * CHILD_SAs which get deleted
71 */
72 payload_type_t payload_type;
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(this->payload_type, 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(this->payload_type, 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) == this->payload_type)
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 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in);
256 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out);
257
258 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} "
259 "with SPIs %.8x_i (%llu bytes) %.8x_o (%llu bytes) and TS %#R=== %#R",
260 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
261 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
262 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
263 child_sa->get_traffic_selectors(child_sa, TRUE),
264 child_sa->get_traffic_selectors(child_sa, FALSE));
265 }
266 enumerator->destroy(enumerator);
267 }
268
269 METHOD(task_t, build_i, status_t,
270 private_child_delete_t *this, message_t *message)
271 {
272 child_sa_t *child_sa;
273
274 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
275 this->spi, TRUE);
276 if (!child_sa)
277 { /* check if it is an outbound sa */
278 child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
279 this->spi, FALSE);
280 if (!child_sa)
281 { /* child does not exist anymore */
282 return SUCCESS;
283 }
284 /* we work only with the inbound SPI */
285 this->spi = child_sa->get_spi(child_sa, TRUE);
286 }
287 this->child_sas->insert_last(this->child_sas, child_sa);
288 if (child_sa->get_state(child_sa) == CHILD_REKEYING)
289 {
290 this->rekeyed = TRUE;
291 }
292 log_children(this);
293 build_payloads(this, message);
294 return NEED_MORE;
295 }
296
297 METHOD(task_t, process_i, status_t,
298 private_child_delete_t *this, message_t *message)
299 {
300 /* flush the list before adding new SAs */
301 this->child_sas->destroy(this->child_sas);
302 this->child_sas = linked_list_create();
303
304 process_payloads(this, message);
305 DBG1(DBG_IKE, "CHILD_SA closed");
306 return destroy_and_reestablish(this);
307 }
308
309 METHOD(task_t, process_r, status_t,
310 private_child_delete_t *this, message_t *message)
311 {
312 process_payloads(this, message);
313 log_children(this);
314 return NEED_MORE;
315 }
316
317 METHOD(task_t, build_r, status_t,
318 private_child_delete_t *this, message_t *message)
319 {
320 /* if we are rekeying, we send an empty informational */
321 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
322 {
323 build_payloads(this, message);
324 }
325 DBG1(DBG_IKE, "CHILD_SA closed");
326 return destroy_and_reestablish(this);
327 }
328
329 METHOD(task_t, get_type, task_type_t,
330 private_child_delete_t *this)
331 {
332 return TASK_CHILD_DELETE;
333 }
334
335 METHOD(child_delete_t , get_child, child_sa_t*,
336 private_child_delete_t *this)
337 {
338 child_sa_t *child_sa = NULL;
339 this->child_sas->get_first(this->child_sas, (void**)&child_sa);
340 return child_sa;
341 }
342
343 METHOD(task_t, migrate, void,
344 private_child_delete_t *this, ike_sa_t *ike_sa)
345 {
346 this->check_delete_action = FALSE;
347 this->ike_sa = ike_sa;
348
349 this->child_sas->destroy(this->child_sas);
350 this->child_sas = linked_list_create();
351 }
352
353 METHOD(task_t, destroy, void,
354 private_child_delete_t *this)
355 {
356 this->child_sas->destroy(this->child_sas);
357 free(this);
358 }
359
360 /*
361 * Described in header.
362 */
363 child_delete_t *child_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
364 u_int32_t spi)
365 {
366 private_child_delete_t *this;
367
368 INIT(this,
369 .public = {
370 .task = {
371 .get_type = _get_type,
372 .migrate = _migrate,
373 .destroy = _destroy,
374 },
375 .get_child = _get_child,
376 },
377 .ike_sa = ike_sa,
378 .child_sas = linked_list_create(),
379 .protocol = protocol,
380 .spi = spi,
381 );
382
383 if (protocol != PROTO_NONE)
384 {
385 this->public.task.build = _build_i;
386 this->public.task.process = _process_i;
387 this->initiator = TRUE;
388 }
389 else
390 {
391 this->public.task.build = _build_r;
392 this->public.task.process = _process_r;
393 this->initiator = FALSE;
394 }
395
396 if (ike_sa->get_version(ike_sa) == IKEV2)
397 {
398 this->payload_type = DELETE;
399 }
400 else
401 {
402 this->payload_type = DELETE_V1;
403 }
404 return &this->public;
405 }