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