added CHILD_SA states, which allows us to detect further simultaneous transactions
[strongswan.git] / src / charon / sa / transactions / delete_ike_sa.c
1 /**
2 * @file delete_ike_sa.c
3 *
4 * @brief Implementation of the delete_ike_sa transaction.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "delete_ike_sa.h"
24
25 #include <daemon.h>
26 #include <encoding/payloads/delete_payload.h>
27
28
29 typedef struct private_delete_ike_sa_t private_delete_ike_sa_t;
30
31 /**
32 * Private members of a delete_ike_sa_t object..
33 */
34 struct private_delete_ike_sa_t {
35
36 /**
37 * Public methods and transaction_t interface.
38 */
39 delete_ike_sa_t public;
40
41 /**
42 * Assigned IKE_SA.
43 */
44 ike_sa_t *ike_sa;
45
46 /**
47 * Message sent by our peer, if already generated
48 */
49 message_t *message;
50
51 /**
52 * Message ID this transaction uses
53 */
54 u_int32_t message_id;
55
56 /**
57 * Times we did send the request
58 */
59 u_int32_t requested;
60
61 /**
62 * Assigned logger.
63 */
64 logger_t *logger;
65 };
66
67 /**
68 * Implementation of transaction_t.get_message_id.
69 */
70 static u_int32_t get_message_id(private_delete_ike_sa_t *this)
71 {
72 return this->message_id;
73 }
74
75 /**
76 * Implementation of transaction_t.requested.
77 */
78 static u_int32_t requested(private_delete_ike_sa_t *this)
79 {
80 return this->requested++;
81 }
82
83 /**
84 * Implementation of transaction_t.get_request.
85 */
86 static status_t get_request(private_delete_ike_sa_t *this, message_t **result)
87 {
88 message_t *request;
89 connection_t *connection;
90 host_t *me, *other;
91 delete_payload_t *delete_payload;
92
93 /* check if we already have built a message (retransmission) */
94 if (this->message)
95 {
96 *result = this->message;
97 return SUCCESS;
98 }
99
100 connection = this->ike_sa->get_connection(this->ike_sa);
101 me = connection->get_my_host(connection);
102 other = connection->get_other_host(connection);
103
104 /* build the request */
105 request = message_create();
106 request->set_source(request, me->clone(me));
107 request->set_destination(request, other->clone(other));
108 request->set_exchange_type(request, INFORMATIONAL);
109 request->set_request(request, TRUE);
110 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
111 request->set_message_id(request, this->message_id);
112 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
113 /* apply for caller */
114 *result = request;
115 /* store for retransmission */
116 this->message = request;
117
118 delete_payload = delete_payload_create(PROTO_IKE);
119 request->add_payload(request, (payload_t*)delete_payload);
120
121 /* transit to state SA_DELETING */
122 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
123
124 return SUCCESS;
125 }
126
127 /**
128 * Implementation of transaction_t.get_response.
129 */
130 static status_t get_response(private_delete_ike_sa_t *this, message_t *request,
131 message_t **result, transaction_t **next)
132 {
133 host_t *me, *other;
134 message_t *response;
135 iterator_t *payloads;
136 delete_payload_t *delete_request = NULL;
137 connection_t *connection;
138
139 /* check if we already have built a response (retransmission)
140 * this only happens in special simultanous transaction cases,
141 * as we delete the IKE_SA after the response is sent. */
142 if (this->message)
143 {
144 *result = this->message;
145 return SUCCESS;
146 }
147
148 connection = this->ike_sa->get_connection(this->ike_sa);
149 me = connection->get_my_host(connection);
150 other = connection->get_other_host(connection);
151 this->message_id = request->get_message_id(request);
152
153 /* set up response */
154 response = message_create();
155 response->set_source(response, me->clone(me));
156 response->set_destination(response, other->clone(other));
157 response->set_exchange_type(response, INFORMATIONAL);
158 response->set_request(response, FALSE);
159 response->set_message_id(response, this->message_id);
160 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
161 this->message = response;
162 *result = response;
163
164 /* check message type */
165 if (request->get_exchange_type(request) != INFORMATIONAL)
166 {
167 this->logger->log(this->logger, ERROR,
168 "INFORMATIONAL response of invalid type, deleting IKE_SA");
169 return DESTROY_ME;
170 }
171
172 /* iterate over all payloads */
173 payloads = request->get_payload_iterator(request);
174 while (payloads->has_next(payloads))
175 {
176 payload_t *payload;
177 payloads->current(payloads, (void**)&payload);
178
179 switch (payload->get_type(payload))
180 {
181 case DELETE:
182 {
183 delete_request = (delete_payload_t *)payload;
184 break;
185 }
186 default:
187 {
188 this->logger->log(this->logger, ERROR|LEVEL1, "ignoring payload %s (%d)",
189 mapping_find(payload_type_m, payload->get_type(payload)),
190 payload->get_type(payload));
191 break;
192 }
193 }
194 }
195 payloads->destroy(payloads);
196
197 if (delete_request &&
198 delete_request->get_protocol_id(delete_request) == PROTO_IKE)
199 {
200 this->logger->log(this->logger, CONTROL,
201 "DELETE request for IKE_SA received, deleting IKE_SA");
202 }
203 else
204 {
205 /* should not happen, as we preparsed this at transaction construction */
206 this->logger->log(this->logger, CONTROL,
207 "received a weird DELETE request for IKE_SA, deleting anyway");
208 }
209 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
210 {
211 /* if we are already deleting an IKE_SA, we do not destroy. We wait
212 * until we get the response for our initiated delete. */
213 return SUCCESS;
214 }
215 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
216 return DESTROY_ME;
217 }
218
219
220 /**
221 * Implementation of transaction_t.conclude
222 */
223 static status_t conclude(private_delete_ike_sa_t *this, message_t *response,
224 transaction_t **transaction)
225 {
226 /* check message type */
227 if (response->get_exchange_type(response) != INFORMATIONAL)
228 {
229 this->logger->log(this->logger, ERROR,
230 "INFORMATIONAL response of invalid type, deleting IKE_SA");
231 return DESTROY_ME;
232 }
233 /* this is only an acknowledge. We can't do anything here, but delete
234 * the IKE_SA. */
235 return DESTROY_ME;
236 }
237
238 /**
239 * implements transaction_t.destroy
240 */
241 static void destroy(private_delete_ike_sa_t *this)
242 {
243 if (this->message)
244 {
245 this->message->destroy(this->message);
246 }
247 free(this);
248 }
249
250 /*
251 * Described in header.
252 */
253 delete_ike_sa_t *delete_ike_sa_create(ike_sa_t *ike_sa)
254 {
255 private_delete_ike_sa_t *this = malloc_thing(private_delete_ike_sa_t);
256
257 /* transaction interface functions */
258 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
259 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
260 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
261 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
262 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
263 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
264
265 /* private data */
266 this->ike_sa = ike_sa;
267 this->message_id = 0;
268 this->message = NULL;
269 this->requested = 0;
270 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
271
272 return &this->public;
273 }