Do not query CHILD_SA during delete if they already expired
[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 #include "quick_delete.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/delete_payload.h>
20
21 typedef struct private_quick_delete_t private_quick_delete_t;
22
23 /**
24 * Private members of a quick_delete_t task.
25 */
26 struct private_quick_delete_t {
27
28 /**
29 * Public methods and task_t interface.
30 */
31 quick_delete_t public;
32
33 /**
34 * Assigned IKE_SA.
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the initiator?
40 */
41 bool initiator;
42
43 /**
44 * Protocol of CHILD_SA to delete
45 */
46 protocol_id_t protocol;
47
48 /**
49 * Inbound SPI of CHILD_SA to delete
50 */
51 u_int32_t spi;
52
53 /**
54 * Send delete even if SA does not exist
55 */
56 bool force;
57
58 /**
59 * SA already expired?
60 */
61 bool expired;
62 };
63
64 /**
65 * Delete the specified CHILD_SA, if found
66 */
67 static bool delete_child(private_quick_delete_t *this,
68 protocol_id_t protocol, u_int32_t spi)
69 {
70 u_int64_t bytes_in, bytes_out;
71 child_sa_t *child_sa;
72
73 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
74 if (!child_sa)
75 { /* fallback and check for outbound SA */
76 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
77 if (!child_sa)
78 {
79 return FALSE;
80 }
81 this->spi = spi = child_sa->get_spi(child_sa, TRUE);
82 }
83
84 child_sa->set_state(child_sa, CHILD_DELETING);
85
86 if (this->expired)
87 {
88 DBG0(DBG_IKE, "closing expired CHILD_SA %s{%d} "
89 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
90 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
91 ntohl(child_sa->get_spi(child_sa, TRUE)),
92 ntohl(child_sa->get_spi(child_sa, FALSE)),
93 child_sa->get_traffic_selectors(child_sa, TRUE),
94 child_sa->get_traffic_selectors(child_sa, FALSE));
95 }
96 else
97 {
98 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in);
99 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out);
100
101 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} with SPIs "
102 "%.8x_i (%llu bytes) %.8x_o (%llu bytes) and TS %#R=== %#R",
103 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
104 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
105 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
106 child_sa->get_traffic_selectors(child_sa, TRUE),
107 child_sa->get_traffic_selectors(child_sa, FALSE));
108 }
109
110 charon->bus->child_updown(charon->bus, child_sa, FALSE);
111
112 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
113
114 /* TODO-IKEv1: handle close action? */
115
116 return TRUE;
117 }
118
119 METHOD(task_t, build_i, status_t,
120 private_quick_delete_t *this, message_t *message)
121 {
122 if (delete_child(this, this->protocol, this->spi) || this->force)
123 {
124 delete_payload_t *delete_payload;
125
126 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
127 protocol_id_names, this->protocol, ntohl(this->spi));
128
129 delete_payload = delete_payload_create(DELETE_V1, PROTO_ESP);
130 delete_payload->add_spi(delete_payload, this->spi);
131 message->add_payload(message, &delete_payload->payload_interface);
132
133 return SUCCESS;
134 }
135 return ALREADY_DONE;
136 }
137
138 METHOD(task_t, process_i, status_t,
139 private_quick_delete_t *this, message_t *message)
140 {
141 return FAILED;
142 }
143
144 METHOD(task_t, process_r, status_t,
145 private_quick_delete_t *this, message_t *message)
146 {
147 enumerator_t *payloads, *spis;
148 payload_t *payload;
149 delete_payload_t *delete_payload;
150 protocol_id_t protocol;
151 u_int32_t spi;
152
153 payloads = message->create_payload_enumerator(message);
154 while (payloads->enumerate(payloads, &payload))
155 {
156 if (payload->get_type(payload) == DELETE_V1)
157 {
158 delete_payload = (delete_payload_t*)payload;
159 protocol = delete_payload->get_protocol_id(delete_payload);
160 if (protocol != PROTO_ESP && protocol != PROTO_AH)
161 {
162 continue;
163 }
164 spis = delete_payload->create_spi_enumerator(delete_payload);
165 while (spis->enumerate(spis, &spi))
166 {
167 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
168 protocol_id_names, protocol, ntohl(spi));
169 if (!delete_child(this, protocol, spi))
170 {
171 DBG1(DBG_IKE, "CHILD_SA not found, ignored");
172 continue;
173 }
174 }
175 spis->destroy(spis);
176 }
177 }
178 payloads->destroy(payloads);
179
180 return SUCCESS;
181 }
182
183 METHOD(task_t, build_r, status_t,
184 private_quick_delete_t *this, message_t *message)
185 {
186 return FAILED;
187 }
188
189 METHOD(task_t, get_type, task_type_t,
190 private_quick_delete_t *this)
191 {
192 return TASK_QUICK_DELETE;
193 }
194
195 METHOD(task_t, migrate, void,
196 private_quick_delete_t *this, ike_sa_t *ike_sa)
197 {
198 this->ike_sa = ike_sa;
199 }
200
201 METHOD(task_t, destroy, void,
202 private_quick_delete_t *this)
203 {
204 free(this);
205 }
206
207 /*
208 * Described in header.
209 */
210 quick_delete_t *quick_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
211 u_int32_t spi, bool force, bool expired)
212 {
213 private_quick_delete_t *this;
214
215 INIT(this,
216 .public = {
217 .task = {
218 .get_type = _get_type,
219 .migrate = _migrate,
220 .destroy = _destroy,
221 },
222 },
223 .ike_sa = ike_sa,
224 .protocol = protocol,
225 .spi = spi,
226 .force = force,
227 .expired = expired,
228 );
229
230 if (protocol != PROTO_NONE)
231 {
232 this->public.task.build = _build_i;
233 this->public.task.process = _process_i;
234 }
235 else
236 {
237 this->public.task.build = _build_r;
238 this->public.task.process = _process_r;
239 }
240 return &this->public;
241 }