introduced new logging subsystem using bus:
[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 typedef struct private_delete_ike_sa_t private_delete_ike_sa_t;
29
30 /**
31 * Private members of a delete_ike_sa_t object..
32 */
33 struct private_delete_ike_sa_t {
34
35 /**
36 * Public methods and transaction_t interface.
37 */
38 delete_ike_sa_t public;
39
40 /**
41 * Assigned IKE_SA.
42 */
43 ike_sa_t *ike_sa;
44
45 /**
46 * Message sent by our peer, if already generated
47 */
48 message_t *message;
49
50 /**
51 * Message ID this transaction uses
52 */
53 u_int32_t message_id;
54
55 /**
56 * Times we did send the request
57 */
58 u_int32_t requested;
59 };
60
61 /**
62 * Implementation of transaction_t.get_message_id.
63 */
64 static u_int32_t get_message_id(private_delete_ike_sa_t *this)
65 {
66 return this->message_id;
67 }
68
69 /**
70 * Implementation of transaction_t.requested.
71 */
72 static u_int32_t requested(private_delete_ike_sa_t *this)
73 {
74 return this->requested++;
75 }
76
77 /**
78 * Implementation of transaction_t.get_request.
79 */
80 static status_t get_request(private_delete_ike_sa_t *this, message_t **result)
81 {
82 message_t *request;
83 host_t *me, *other;
84 delete_payload_t *delete_payload;
85
86 /* check if we already have built a message (retransmission) */
87 if (this->message)
88 {
89 *result = this->message;
90 return SUCCESS;
91 }
92
93 me = this->ike_sa->get_my_host(this->ike_sa);
94 other = this->ike_sa->get_other_host(this->ike_sa);
95
96 /* build the request */
97 request = message_create();
98 request->set_source(request, me->clone(me));
99 request->set_destination(request, other->clone(other));
100 request->set_exchange_type(request, INFORMATIONAL);
101 request->set_request(request, TRUE);
102 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
103 request->set_message_id(request, this->message_id);
104 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
105 /* apply for caller */
106 *result = request;
107 /* store for retransmission */
108 this->message = request;
109
110 delete_payload = delete_payload_create(PROTO_IKE);
111 request->add_payload(request, (payload_t*)delete_payload);
112
113 /* transit to state SA_DELETING */
114 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
115
116 return SUCCESS;
117 }
118
119 /**
120 * Implementation of transaction_t.get_response.
121 */
122 static status_t get_response(private_delete_ike_sa_t *this, message_t *request,
123 message_t **result, transaction_t **next)
124 {
125 host_t *me, *other;
126 message_t *response;
127 iterator_t *payloads;
128 delete_payload_t *delete_request = NULL;
129
130 /* check if we already have built a response (retransmission)
131 * this only happens in special simultanous transaction cases,
132 * as we delete the IKE_SA after the response is sent. */
133 if (this->message)
134 {
135 *result = this->message;
136 return SUCCESS;
137 }
138
139 me = this->ike_sa->get_my_host(this->ike_sa);
140 other = this->ike_sa->get_other_host(this->ike_sa);
141 this->message_id = request->get_message_id(request);
142
143 /* set up response */
144 response = message_create();
145 response->set_source(response, me->clone(me));
146 response->set_destination(response, other->clone(other));
147 response->set_exchange_type(response, INFORMATIONAL);
148 response->set_request(response, FALSE);
149 response->set_message_id(response, this->message_id);
150 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
151 this->message = response;
152 *result = response;
153
154 /* check message type */
155 if (request->get_exchange_type(request) != INFORMATIONAL)
156 {
157 DBG1(SIG_DBG_IKE, "INFORMATIONAL response of invalid type, deleting IKE_SA");
158 return DESTROY_ME;
159 }
160
161 /* iterate over all payloads */
162 payloads = request->get_payload_iterator(request);
163 while (payloads->has_next(payloads))
164 {
165 payload_t *payload;
166 payloads->current(payloads, (void**)&payload);
167
168 switch (payload->get_type(payload))
169 {
170 case DELETE:
171 {
172 delete_request = (delete_payload_t *)payload;
173 break;
174 }
175 default:
176 {
177 DBG1(SIG_DBG_IKE, "ignoring payload %N",
178 payload_type_names, payload->get_type(payload));
179 break;
180 }
181 }
182 }
183 payloads->destroy(payloads);
184
185 if (delete_request &&
186 delete_request->get_protocol_id(delete_request) == PROTO_IKE)
187 {
188 DBG1(SIG_DBG_IKE, "DELETE request for IKE_SA received, deleting IKE_SA");
189 }
190 else
191 {
192 /* should not happen, as we preparsed this at transaction construction */
193 DBG1(SIG_DBG_IKE, "received a weird DELETE request for IKE_SA, deleting anyway");
194 }
195 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
196 {
197 /* if we are already deleting an IKE_SA, we do not destroy. We wait
198 * until we get the response for our initiated delete. */
199 return SUCCESS;
200 }
201 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
202 return DESTROY_ME;
203 }
204
205
206 /**
207 * Implementation of transaction_t.conclude
208 */
209 static status_t conclude(private_delete_ike_sa_t *this, message_t *response,
210 transaction_t **transaction)
211 {
212 /* check message type */
213 if (response->get_exchange_type(response) != INFORMATIONAL)
214 {
215 DBG1(SIG_DBG_IKE, "INFORMATIONAL response of invalid type, deleting IKE_SA");
216 return DESTROY_ME;
217 }
218 /* this is only an acknowledge. We can't do anything here, but delete
219 * the IKE_SA. */
220 return DESTROY_ME;
221 }
222
223 /**
224 * implements transaction_t.destroy
225 */
226 static void destroy(private_delete_ike_sa_t *this)
227 {
228 DESTROY_IF(this->message);
229 free(this);
230 }
231
232 /*
233 * Described in header.
234 */
235 delete_ike_sa_t *delete_ike_sa_create(ike_sa_t *ike_sa)
236 {
237 private_delete_ike_sa_t *this = malloc_thing(private_delete_ike_sa_t);
238
239 /* transaction interface functions */
240 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
241 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
242 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
243 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
244 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
245 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
246
247 /* private data */
248 this->ike_sa = ike_sa;
249 this->message_id = 0;
250 this->message = NULL;
251 this->requested = 0;
252
253 return &this->public;
254 }