reimplemented CHILD_SA rekeying & delete
[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 request->set_message_id(request, this->message_id);
111 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
112 /* apply for caller */
113 *result = request;
114 /* store for retransmission */
115 this->message = request;
116
117 delete_payload = delete_payload_create(PROTO_IKE);
118 request->add_payload(request, (payload_t*)delete_payload);
119
120 /* transit to state SA_DELETING */
121 this->ike_sa->set_state(this->ike_sa, SA_DELETING);
122
123 return SUCCESS;
124 }
125
126 /**
127 * Implementation of transaction_t.get_response.
128 */
129 static status_t get_response(private_delete_ike_sa_t *this, message_t *request,
130 message_t **result, transaction_t **next)
131 {
132 host_t *me, *other;
133 message_t *response;
134 iterator_t *payloads;
135 delete_payload_t *delete_request = NULL;
136 connection_t *connection;
137
138 /* check if we already have built a response (retransmission)
139 * this only happens in special simultanous transaction cases,
140 * as we delete the IKE_SA after the response is sent. */
141 if (this->message)
142 {
143 *result = this->message;
144 return SUCCESS;
145 }
146
147 connection = this->ike_sa->get_connection(this->ike_sa);
148 me = connection->get_my_host(connection);
149 other = connection->get_other_host(connection);
150
151 /* set up response */
152 response = message_create();
153 response->set_source(response, me->clone(me));
154 response->set_destination(response, other->clone(other));
155 response->set_exchange_type(response, INFORMATIONAL);
156 response->set_request(response, FALSE);
157 response->set_message_id(response, this->message_id);
158 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
159 this->message = response;
160 *result = response;
161
162 /* check message type */
163 if (request->get_exchange_type(request) != INFORMATIONAL)
164 {
165 this->logger->log(this->logger, ERROR,
166 "INFORMATIONAL response of invalid type, deleting IKE_SA");
167 return DESTROY_ME;
168 }
169
170 /* iterate over all payloads */
171 payloads = request->get_payload_iterator(request);
172 while (payloads->has_next(payloads))
173 {
174 payload_t *payload;
175 payloads->current(payloads, (void**)&payload);
176
177 switch (payload->get_type(payload))
178 {
179 case DELETE:
180 {
181 delete_request = (delete_payload_t *)payload;
182 break;
183 }
184 default:
185 {
186 this->logger->log(this->logger, ERROR|LEVEL1, "ignoring payload %s (%d)",
187 mapping_find(payload_type_m, payload->get_type(payload)),
188 payload->get_type(payload));
189 break;
190 }
191 }
192 }
193 payloads->destroy(payloads);
194
195 if (delete_request &&
196 delete_request->get_protocol_id(delete_request) == PROTO_IKE)
197 {
198 this->logger->log(this->logger, CONTROL,
199 "DELETE request for IKE_SA received, deleting IKE_SA");
200 }
201 else
202 {
203 /* should not happen, as we preparsed this at transaction construction */
204 this->logger->log(this->logger, CONTROL,
205 "received a weird DELETE request for IKE_SA, deleting anyway");
206 }
207 if (this->ike_sa->get_state(this->ike_sa) == SA_DELETING)
208 {
209 /* if we are already deleting an IKE_SA, we do not destroy. We wait
210 * until we get the response for our initiated delete. */
211 return SUCCESS;
212 }
213 this->ike_sa->set_state(this->ike_sa, SA_DELETING);
214 return DESTROY_ME;
215 }
216
217
218 /**
219 * Implementation of transaction_t.conclude
220 */
221 static status_t conclude(private_delete_ike_sa_t *this, message_t *response,
222 transaction_t **transaction)
223 {
224 /* check message type */
225 if (response->get_exchange_type(response) != INFORMATIONAL)
226 {
227 this->logger->log(this->logger, ERROR,
228 "INFORMATIONAL response of invalid type, deleting IKE_SA");
229 return DESTROY_ME;
230 }
231 /* this is only an acknowledge. We can't do anything here, but delete
232 * the IKE_SA. */
233 return DESTROY_ME;
234 }
235
236 /**
237 * implements transaction_t.destroy
238 */
239 static void destroy(private_delete_ike_sa_t *this)
240 {
241 if (this->message)
242 {
243 this->message->destroy(this->message);
244 }
245 free(this);
246 }
247
248 /*
249 * Described in header.
250 */
251 delete_ike_sa_t *delete_ike_sa_create(ike_sa_t *ike_sa, u_int32_t message_id)
252 {
253 private_delete_ike_sa_t *this = malloc_thing(private_delete_ike_sa_t);
254
255 /* transaction interface functions */
256 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
257 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
258 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
259 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
260 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
261 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
262
263 /* private data */
264 this->ike_sa = ike_sa;
265 this->message_id = message_id;
266 this->message = NULL;
267 this->requested = 0;
268 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
269
270 return &this->public;
271 }