payload: Use common prefixes for all payload type identifiers
[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 u_int32_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 u_int32_t spi, bool remote_close)
90 {
91 u_int64_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_REKEYING;
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_reqid(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_reqid(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 if (!rekeyed)
139 {
140 charon->bus->child_updown(charon->bus, child_sa, FALSE);
141
142 if (remote_close)
143 {
144 child_cfg = child_sa->get_config(child_sa);
145 child_cfg->get_ref(child_cfg);
146
147 switch (child_sa->get_close_action(child_sa))
148 {
149 case ACTION_RESTART:
150 child_cfg->get_ref(child_cfg);
151 this->ike_sa->initiate(this->ike_sa, child_cfg,
152 child_sa->get_reqid(child_sa), NULL, NULL);
153 break;
154 case ACTION_ROUTE:
155 charon->traps->install(charon->traps,
156 this->ike_sa->get_peer_cfg(this->ike_sa),
157 child_cfg, child_sa->get_reqid(child_sa));
158 break;
159 default:
160 break;
161 }
162 child_cfg->destroy(child_cfg);
163 }
164 }
165 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
166
167 return TRUE;
168 }
169
170 METHOD(task_t, build_i, status_t,
171 private_quick_delete_t *this, message_t *message)
172 {
173 if (delete_child(this, this->protocol, this->spi, FALSE) || this->force)
174 {
175 delete_payload_t *delete_payload;
176
177 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
178 protocol_id_names, this->protocol, ntohl(this->spi));
179
180 delete_payload = delete_payload_create(PLV1_DELETE, this->protocol);
181 delete_payload->add_spi(delete_payload, this->spi);
182 message->add_payload(message, &delete_payload->payload_interface);
183
184 return SUCCESS;
185 }
186 this->ike_sa->flush_queue(this->ike_sa, TASK_QUEUE_ACTIVE);
187 return ALREADY_DONE;
188 }
189
190 METHOD(task_t, process_i, status_t,
191 private_quick_delete_t *this, message_t *message)
192 {
193 return FAILED;
194 }
195
196 METHOD(task_t, process_r, status_t,
197 private_quick_delete_t *this, message_t *message)
198 {
199 enumerator_t *payloads, *spis;
200 payload_t *payload;
201 delete_payload_t *delete_payload;
202 protocol_id_t protocol;
203 u_int32_t spi;
204
205 payloads = message->create_payload_enumerator(message);
206 while (payloads->enumerate(payloads, &payload))
207 {
208 if (payload->get_type(payload) == PLV1_DELETE)
209 {
210 delete_payload = (delete_payload_t*)payload;
211 protocol = delete_payload->get_protocol_id(delete_payload);
212 if (protocol != PROTO_ESP && protocol != PROTO_AH)
213 {
214 continue;
215 }
216 spis = delete_payload->create_spi_enumerator(delete_payload);
217 while (spis->enumerate(spis, &spi))
218 {
219 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
220 protocol_id_names, protocol, ntohl(spi));
221 if (!delete_child(this, protocol, spi, TRUE))
222 {
223 DBG1(DBG_IKE, "CHILD_SA not found, ignored");
224 continue;
225 }
226 }
227 spis->destroy(spis);
228 }
229 }
230 payloads->destroy(payloads);
231
232 return SUCCESS;
233 }
234
235 METHOD(task_t, build_r, status_t,
236 private_quick_delete_t *this, message_t *message)
237 {
238 return FAILED;
239 }
240
241 METHOD(task_t, get_type, task_type_t,
242 private_quick_delete_t *this)
243 {
244 return TASK_QUICK_DELETE;
245 }
246
247 METHOD(task_t, migrate, void,
248 private_quick_delete_t *this, ike_sa_t *ike_sa)
249 {
250 this->ike_sa = ike_sa;
251 }
252
253 METHOD(task_t, destroy, void,
254 private_quick_delete_t *this)
255 {
256 free(this);
257 }
258
259 /*
260 * Described in header.
261 */
262 quick_delete_t *quick_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
263 u_int32_t spi, bool force, bool expired)
264 {
265 private_quick_delete_t *this;
266
267 INIT(this,
268 .public = {
269 .task = {
270 .get_type = _get_type,
271 .migrate = _migrate,
272 .destroy = _destroy,
273 },
274 },
275 .ike_sa = ike_sa,
276 .protocol = protocol,
277 .spi = spi,
278 .force = force,
279 .expired = expired,
280 );
281
282 if (protocol != PROTO_NONE)
283 {
284 this->public.task.build = _build_i;
285 this->public.task.process = _process_i;
286 }
287 else
288 {
289 this->public.task.build = _build_r;
290 this->public.task.process = _process_r;
291 }
292 return &this->public;
293 }