2c1db2ad01b890e9e734d21104c8d7bdab2535fc
[strongswan.git] / src / charon / 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 * $Id$
16 */
17
18 #include "child_delete.h"
19
20 #include <daemon.h>
21 #include <encoding/payloads/delete_payload.h>
22
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 * Are we the initiator?
43 */
44 bool initiator;
45
46 /**
47 * CHILD_SAs which get deleted
48 */
49 linked_list_t *child_sas;
50 };
51
52 /**
53 * build the delete payloads from the listed child_sas
54 */
55 static void build_payloads(private_child_delete_t *this, message_t *message)
56 {
57 iterator_t *iterator;
58 delete_payload_t *ah = NULL, *esp = NULL;
59 u_int32_t spi;
60 child_sa_t *child_sa;
61
62 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
63 while (iterator->iterate(iterator, (void**)&child_sa))
64 {
65 spi = child_sa->get_spi(child_sa, TRUE);
66 switch (child_sa->get_protocol(child_sa))
67 {
68 case PROTO_ESP:
69 if (esp == NULL)
70 {
71 esp = delete_payload_create(PROTO_ESP);
72 message->add_payload(message, (payload_t*)esp);
73 }
74 esp->add_spi(esp, spi);
75 break;
76 case PROTO_AH:
77 if (ah == NULL)
78 {
79 ah = delete_payload_create(PROTO_AH);
80 message->add_payload(message, (payload_t*)ah);
81 }
82 ah->add_spi(ah, spi);
83 break;
84 default:
85 break;
86 }
87 child_sa->set_state(child_sa, CHILD_DELETING);
88 }
89 iterator->destroy(iterator);
90 }
91
92 /**
93 * read in payloads and find the children to delete
94 */
95 static void process_payloads(private_child_delete_t *this, message_t *message)
96 {
97 iterator_t *payloads, *spis;
98 payload_t *payload;
99 delete_payload_t *delete_payload;
100 u_int32_t *spi;
101 protocol_id_t protocol;
102 child_sa_t *child_sa;
103
104 payloads = message->get_payload_iterator(message);
105 while (payloads->iterate(payloads, (void**)&payload))
106 {
107 if (payload->get_type(payload) == DELETE)
108 {
109 delete_payload = (delete_payload_t*)payload;
110 protocol = delete_payload->get_protocol_id(delete_payload);
111 if (protocol != PROTO_ESP && protocol != PROTO_AH)
112 {
113 continue;
114 }
115 spis = delete_payload->create_spi_iterator(delete_payload);
116 while (spis->iterate(spis, (void**)&spi))
117 {
118 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
119 *spi, FALSE);
120 if (child_sa == NULL)
121 {
122 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI 0x%x, "
123 "but no such SA", protocol_id_names, protocol, ntohl(*spi));
124 continue;
125 }
126 DBG2(DBG_IKE, "received DELETE for %N CHILD_SA with SPI 0x%x",
127 protocol_id_names, protocol, ntohl(*spi));
128
129 switch (child_sa->get_state(child_sa))
130 {
131 case CHILD_REKEYING:
132 /* we reply as usual, rekeying will fail */
133 break;
134 case CHILD_DELETING:
135 /* we don't send back a delete if we initiated ourself */
136 if (!this->initiator)
137 {
138 this->ike_sa->destroy_child_sa(this->ike_sa,
139 protocol, *spi);
140 continue;
141 }
142 default:
143 break;
144 }
145
146 this->child_sas->insert_last(this->child_sas, child_sa);
147 }
148 spis->destroy(spis);
149 }
150 }
151 payloads->destroy(payloads);
152 }
153
154 /**
155 * destroy the children listed in this->child_sas
156 */
157 static void destroy_children(private_child_delete_t *this)
158 {
159 iterator_t *iterator;
160 child_sa_t *child_sa;
161 protocol_id_t protocol;
162 u_int32_t spi;
163
164 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
165 while (iterator->iterate(iterator, (void**)&child_sa))
166 {
167 spi = child_sa->get_spi(child_sa, TRUE);
168 protocol = child_sa->get_protocol(child_sa);
169 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
170 }
171 iterator->destroy(iterator);
172 }
173
174 /**
175 * send closing signals for all CHILD_SAs over the bus
176 */
177 static void log_children(private_child_delete_t *this)
178 {
179 iterator_t *iterator;
180 child_sa_t *child_sa;
181
182 iterator = this->child_sas->create_iterator(this->child_sas, TRUE);
183 while (iterator->iterate(iterator, (void**)&child_sa))
184 {
185 SIG(CHILD_DOWN_START, "closing CHILD_SA %#R=== %#R",
186 child_sa->get_traffic_selectors(child_sa, TRUE),
187 child_sa->get_traffic_selectors(child_sa, FALSE));
188 }
189 iterator->destroy(iterator);
190 }
191
192 /**
193 * Implementation of task_t.build for initiator
194 */
195 static status_t build_i(private_child_delete_t *this, message_t *message)
196 {
197 log_children(this);
198 build_payloads(this, message);
199 return NEED_MORE;
200 }
201
202 /**
203 * Implementation of task_t.process for initiator
204 */
205 static status_t process_i(private_child_delete_t *this, message_t *message)
206 {
207 /* flush the list before adding new SAs */
208 this->child_sas->destroy(this->child_sas);
209 this->child_sas = linked_list_create();
210
211 process_payloads(this, message);
212 destroy_children(this);
213 SIG(CHILD_DOWN_SUCCESS, "CHILD_SA closed");
214 return SUCCESS;
215 }
216
217 /**
218 * Implementation of task_t.process for initiator
219 */
220 static status_t process_r(private_child_delete_t *this, message_t *message)
221 {
222 process_payloads(this, message);
223 log_children(this);
224 return NEED_MORE;
225 }
226
227 /**
228 * Implementation of task_t.build for responder
229 */
230 static status_t build_r(private_child_delete_t *this, message_t *message)
231 {
232 /* if we are rekeying, we send an empty informational */
233 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
234 {
235 build_payloads(this, message);
236 }
237 destroy_children(this);
238 SIG(CHILD_DOWN_SUCCESS, "CHILD_SA closed");
239 return SUCCESS;
240 }
241
242 /**
243 * Implementation of task_t.get_type
244 */
245 static task_type_t get_type(private_child_delete_t *this)
246 {
247 return CHILD_DELETE;
248 }
249
250 /**
251 * Implementation of child_delete_t.get_child
252 */
253 static child_sa_t* get_child(private_child_delete_t *this)
254 {
255 child_sa_t *child_sa = NULL;
256 this->child_sas->get_first(this->child_sas, (void**)&child_sa);
257 return child_sa;
258 }
259
260 /**
261 * Implementation of task_t.migrate
262 */
263 static void migrate(private_child_delete_t *this, ike_sa_t *ike_sa)
264 {
265 this->ike_sa = ike_sa;
266
267 this->child_sas->destroy(this->child_sas);
268 this->child_sas = linked_list_create();
269 }
270
271 /**
272 * Implementation of task_t.destroy
273 */
274 static void destroy(private_child_delete_t *this)
275 {
276 this->child_sas->destroy(this->child_sas);
277 free(this);
278 }
279
280 /*
281 * Described in header.
282 */
283 child_delete_t *child_delete_create(ike_sa_t *ike_sa, child_sa_t *child_sa)
284 {
285 private_child_delete_t *this = malloc_thing(private_child_delete_t);
286
287 this->public.get_child = (child_sa_t*(*)(child_delete_t*))get_child;
288 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
289 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
290 this->public.task.destroy = (void(*)(task_t*))destroy;
291
292 this->ike_sa = ike_sa;
293 this->child_sas = linked_list_create();
294
295 if (child_sa != NULL)
296 {
297 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
298 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
299 this->initiator = TRUE;
300 this->child_sas->insert_last(this->child_sas, child_sa);
301 }
302 else
303 {
304 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
305 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
306 this->initiator = FALSE;
307 }
308 return &this->public;
309 }