Removed strayed code fragment
[strongswan.git] / src / charon / sa / tasks / ike_delete.c
1 /*
2 * Copyright (C) 2006-2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 "ike_delete.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/delete_payload.h>
20
21
22 typedef struct private_ike_delete_t private_ike_delete_t;
23
24 /**
25 * Private members of a ike_delete_t task.
26 */
27 struct private_ike_delete_t {
28
29 /**
30 * Public methods and task_t interface.
31 */
32 ike_delete_t public;
33
34 /**
35 * Assigned IKE_SA.
36 */
37 ike_sa_t *ike_sa;
38
39 /**
40 * Are we the initiator?
41 */
42 bool initiator;
43
44 /**
45 * are we deleting a rekeyed SA?
46 */
47 bool rekeyed;
48
49 /**
50 * are we responding to a delete, but have initated our own?
51 */
52 bool simultaneous;
53 };
54
55 /**
56 * Implementation of task_t.build for initiator
57 */
58 static status_t build_i(private_ike_delete_t *this, message_t *message)
59 {
60 delete_payload_t *delete_payload;
61
62 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
63 this->ike_sa->get_name(this->ike_sa),
64 this->ike_sa->get_unique_id(this->ike_sa),
65 this->ike_sa->get_my_host(this->ike_sa),
66 this->ike_sa->get_my_id(this->ike_sa),
67 this->ike_sa->get_other_host(this->ike_sa),
68 this->ike_sa->get_other_id(this->ike_sa));
69
70 delete_payload = delete_payload_create(PROTO_IKE);
71 message->add_payload(message, (payload_t*)delete_payload);
72
73 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING)
74 {
75 this->rekeyed = TRUE;
76 }
77 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
78
79 DBG1(DBG_IKE, "sending DELETE for IKE_SA %s[%d]",
80 this->ike_sa->get_name(this->ike_sa),
81 this->ike_sa->get_unique_id(this->ike_sa));
82
83 return NEED_MORE;
84 }
85
86 /**
87 * Implementation of task_t.process for initiator
88 */
89 static status_t process_i(private_ike_delete_t *this, message_t *message)
90 {
91 DBG0(DBG_IKE, "IKE_SA deleted");
92 if (!this->rekeyed)
93 { /* invoke ike_down() hook if SA has not been rekeyed */
94 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
95 }
96 /* completed, delete IKE_SA by returning DESTROY_ME */
97 return DESTROY_ME;
98 }
99
100 /**
101 * Implementation of task_t.process for responder
102 */
103 static status_t process_r(private_ike_delete_t *this, message_t *message)
104 {
105 /* we don't even scan the payloads, as the message wouldn't have
106 * come so far without being correct */
107 DBG1(DBG_IKE, "received DELETE for IKE_SA %s[%d]",
108 this->ike_sa->get_name(this->ike_sa),
109 this->ike_sa->get_unique_id(this->ike_sa));
110 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
111 this->ike_sa->get_name(this->ike_sa),
112 this->ike_sa->get_unique_id(this->ike_sa),
113 this->ike_sa->get_my_host(this->ike_sa),
114 this->ike_sa->get_my_id(this->ike_sa),
115 this->ike_sa->get_other_host(this->ike_sa),
116 this->ike_sa->get_other_id(this->ike_sa));
117
118 switch (this->ike_sa->get_state(this->ike_sa))
119 {
120 case IKE_ESTABLISHED:
121 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
122 this->ike_sa->reestablish(this->ike_sa);
123 return NEED_MORE;
124 case IKE_REKEYING:
125 this->rekeyed = TRUE;
126 break;
127 case IKE_DELETING:
128 this->simultaneous = TRUE;
129 break;
130 default:
131 break;
132 }
133 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
134 return NEED_MORE;
135 }
136
137 /**
138 * Implementation of task_t.build for responder
139 */
140 static status_t build_r(private_ike_delete_t *this, message_t *message)
141 {
142 DBG0(DBG_IKE, "IKE_SA deleted");
143
144 if (this->simultaneous)
145 {
146 /* wait for peer's response for our delete request, but set a timeout */
147 return SUCCESS;
148 }
149 if (!this->rekeyed)
150 { /* invoke ike_down() hook if SA has not been rekeyed */
151 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
152 }
153 /* completed, delete IKE_SA by returning DESTROY_ME */
154 return DESTROY_ME;
155 }
156
157 /**
158 * Implementation of task_t.get_type
159 */
160 static task_type_t get_type(private_ike_delete_t *this)
161 {
162 return IKE_DELETE;
163 }
164
165 /**
166 * Implementation of task_t.migrate
167 */
168 static void migrate(private_ike_delete_t *this, ike_sa_t *ike_sa)
169 {
170 this->ike_sa = ike_sa;
171 this->simultaneous = FALSE;
172 }
173
174 /**
175 * Implementation of task_t.destroy
176 */
177 static void destroy(private_ike_delete_t *this)
178 {
179 free(this);
180 }
181
182 /*
183 * Described in header.
184 */
185 ike_delete_t *ike_delete_create(ike_sa_t *ike_sa, bool initiator)
186 {
187 private_ike_delete_t *this = malloc_thing(private_ike_delete_t);
188
189 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
190 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
191 this->public.task.destroy = (void(*)(task_t*))destroy;
192
193 if (initiator)
194 {
195 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
196 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
197 }
198 else
199 {
200 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
201 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
202 }
203
204 this->ike_sa = ike_sa;
205 this->initiator = initiator;
206 this->rekeyed = FALSE;
207 this->simultaneous = FALSE;
208
209 return &this->public;
210 }