0191a45a8dceb6f818df6fe4d9398938ace1b85b
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / quick_delete.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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 * Copyright (C) 2013 Oliver Smith
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 * THE SOFTWARE.
35 */
36
37 #include "quick_delete.h"
38
39 #include <daemon.h>
40 #include <encoding/payloads/delete_payload.h>
41
42 typedef struct private_quick_delete_t private_quick_delete_t;
43
44 /**
45 * Private members of a quick_delete_t task.
46 */
47 struct private_quick_delete_t {
48
49 /**
50 * Public methods and task_t interface.
51 */
52 quick_delete_t public;
53
54 /**
55 * Assigned IKE_SA.
56 */
57 ike_sa_t *ike_sa;
58
59 /**
60 * Are we the initiator?
61 */
62 bool initiator;
63
64 /**
65 * Protocol of CHILD_SA to delete
66 */
67 protocol_id_t protocol;
68
69 /**
70 * Inbound SPI of CHILD_SA to delete
71 */
72 uint32_t spi;
73
74 /**
75 * Send delete even if SA does not exist
76 */
77 bool force;
78
79 /**
80 * SA already expired?
81 */
82 bool expired;
83 };
84
85 /**
86 * Delete the specified CHILD_SA, if found
87 */
88 static bool delete_child(private_quick_delete_t *this, protocol_id_t protocol,
89 uint32_t spi, bool remote_close)
90 {
91 uint64_t bytes_in, bytes_out;
92 child_sa_t *child_sa;
93 linked_list_t *my_ts, *other_ts;
94 child_cfg_t *child_cfg;
95 bool rekeyed;
96
97 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
98 if (!child_sa)
99 { /* fallback and check for outbound SA */
100 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
101 if (!child_sa)
102 {
103 return FALSE;
104 }
105 this->spi = spi = child_sa->get_spi(child_sa, TRUE);
106 }
107
108 rekeyed = child_sa->get_state(child_sa) == CHILD_REKEYED;
109 child_sa->set_state(child_sa, CHILD_DELETING);
110
111 my_ts = linked_list_create_from_enumerator(
112 child_sa->create_ts_enumerator(child_sa, TRUE));
113 other_ts = linked_list_create_from_enumerator(
114 child_sa->create_ts_enumerator(child_sa, FALSE));
115 if (this->expired)
116 {
117 DBG0(DBG_IKE, "closing expired CHILD_SA %s{%d} "
118 "with SPIs %.8x_i %.8x_o and TS %#R === %#R",
119 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
120 ntohl(child_sa->get_spi(child_sa, TRUE)),
121 ntohl(child_sa->get_spi(child_sa, FALSE)), my_ts, other_ts);
122 }
123 else
124 {
125 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in, NULL);
126 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out, NULL);
127
128 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} with SPIs "
129 "%.8x_i (%llu bytes) %.8x_o (%llu bytes) and TS %#R === %#R",
130 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
131 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
132 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
133 my_ts, other_ts);
134 }
135 my_ts->destroy(my_ts);
136 other_ts->destroy(other_ts);
137
138 child_sa->set_state(child_sa, CHILD_DELETED);
139 if (!rekeyed)
140 {
141 charon->bus->child_updown(charon->bus, child_sa, FALSE);
142
143 if (remote_close)
144 {
145 child_cfg = child_sa->get_config(child_sa);
146 child_cfg->get_ref(child_cfg);
147
148 switch (child_sa->get_close_action(child_sa))
149 {
150 case ACTION_RESTART:
151 child_cfg->get_ref(child_cfg);
152 this->ike_sa->initiate(this->ike_sa, child_cfg,
153 child_sa->get_reqid(child_sa), NULL, NULL);
154 break;
155 case ACTION_ROUTE:
156 charon->traps->install(charon->traps,
157 this->ike_sa->get_peer_cfg(this->ike_sa),
158 child_cfg);
159 break;
160 default:
161 break;
162 }
163 child_cfg->destroy(child_cfg);
164 }
165 }
166 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
167
168 return TRUE;
169 }
170
171 METHOD(task_t, build_i, status_t,
172 private_quick_delete_t *this, message_t *message)
173 {
174 if (delete_child(this, this->protocol, this->spi, FALSE) || this->force)
175 {
176 delete_payload_t *delete_payload;
177
178 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
179 protocol_id_names, this->protocol, ntohl(this->spi));
180
181 delete_payload = delete_payload_create(PLV1_DELETE, this->protocol);
182 delete_payload->add_spi(delete_payload, this->spi);
183 message->add_payload(message, &delete_payload->payload_interface);
184
185 return SUCCESS;
186 }
187 this->ike_sa->flush_queue(this->ike_sa, TASK_QUEUE_ACTIVE);
188 return ALREADY_DONE;
189 }
190
191 METHOD(task_t, process_i, status_t,
192 private_quick_delete_t *this, message_t *message)
193 {
194 return FAILED;
195 }
196
197 METHOD(task_t, process_r, status_t,
198 private_quick_delete_t *this, message_t *message)
199 {
200 enumerator_t *payloads, *spis;
201 payload_t *payload;
202 delete_payload_t *delete_payload;
203 protocol_id_t protocol;
204 uint32_t spi;
205
206 payloads = message->create_payload_enumerator(message);
207 while (payloads->enumerate(payloads, &payload))
208 {
209 if (payload->get_type(payload) == PLV1_DELETE)
210 {
211 delete_payload = (delete_payload_t*)payload;
212 protocol = delete_payload->get_protocol_id(delete_payload);
213 if (protocol != PROTO_ESP && protocol != PROTO_AH)
214 {
215 continue;
216 }
217 spis = delete_payload->create_spi_enumerator(delete_payload);
218 while (spis->enumerate(spis, &spi))
219 {
220 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
221 protocol_id_names, protocol, ntohl(spi));
222 if (!delete_child(this, protocol, spi, TRUE))
223 {
224 DBG1(DBG_IKE, "CHILD_SA not found, ignored");
225 continue;
226 }
227 }
228 spis->destroy(spis);
229 }
230 }
231 payloads->destroy(payloads);
232
233 return SUCCESS;
234 }
235
236 METHOD(task_t, build_r, status_t,
237 private_quick_delete_t *this, message_t *message)
238 {
239 return FAILED;
240 }
241
242 METHOD(task_t, get_type, task_type_t,
243 private_quick_delete_t *this)
244 {
245 return TASK_QUICK_DELETE;
246 }
247
248 METHOD(task_t, migrate, void,
249 private_quick_delete_t *this, ike_sa_t *ike_sa)
250 {
251 this->ike_sa = ike_sa;
252 }
253
254 METHOD(task_t, destroy, void,
255 private_quick_delete_t *this)
256 {
257 free(this);
258 }
259
260 /*
261 * Described in header.
262 */
263 quick_delete_t *quick_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
264 uint32_t spi, bool force, bool expired)
265 {
266 private_quick_delete_t *this;
267
268 INIT(this,
269 .public = {
270 .task = {
271 .get_type = _get_type,
272 .migrate = _migrate,
273 .destroy = _destroy,
274 },
275 },
276 .ike_sa = ike_sa,
277 .protocol = protocol,
278 .spi = spi,
279 .force = force,
280 .expired = expired,
281 );
282
283 if (protocol != PROTO_NONE)
284 {
285 this->public.task.build = _build_i;
286 this->public.task.process = _process_i;
287 }
288 else
289 {
290 this->public.task.build = _build_r;
291 this->public.task.process = _process_r;
292 }
293 return &this->public;
294 }