Use version specific DELETE payload identifier in ike_delete task
[strongswan.git] / src / libcharon / 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 METHOD(task_t, build_i, status_t,
56 private_ike_delete_t *this, message_t *message)
57 {
58 delete_payload_t *delete_payload;
59
60 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
61 this->ike_sa->get_name(this->ike_sa),
62 this->ike_sa->get_unique_id(this->ike_sa),
63 this->ike_sa->get_my_host(this->ike_sa),
64 this->ike_sa->get_my_id(this->ike_sa),
65 this->ike_sa->get_other_host(this->ike_sa),
66 this->ike_sa->get_other_id(this->ike_sa));
67
68 if (this->ike_sa->get_version(this->ike_sa) == IKEV1)
69 {
70 delete_payload = delete_payload_create(DELETE_V1, PROTO_IKE);
71 }
72 else
73 {
74 delete_payload = delete_payload_create(DELETE, PROTO_IKE);
75 }
76 message->add_payload(message, (payload_t*)delete_payload);
77
78 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING)
79 {
80 this->rekeyed = TRUE;
81 }
82 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
83
84 DBG1(DBG_IKE, "sending DELETE for IKE_SA %s[%d]",
85 this->ike_sa->get_name(this->ike_sa),
86 this->ike_sa->get_unique_id(this->ike_sa));
87
88 return NEED_MORE;
89 }
90
91 METHOD(task_t, process_i, status_t,
92 private_ike_delete_t *this, message_t *message)
93 {
94 DBG0(DBG_IKE, "IKE_SA deleted");
95 if (!this->rekeyed)
96 { /* invoke ike_down() hook if SA has not been rekeyed */
97 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
98 }
99 /* completed, delete IKE_SA by returning DESTROY_ME */
100 return DESTROY_ME;
101 }
102
103 METHOD(task_t, process_r, status_t,
104 private_ike_delete_t *this, message_t *message)
105 {
106 /* we don't even scan the payloads, as the message wouldn't have
107 * come so far without being correct */
108 DBG1(DBG_IKE, "received DELETE for IKE_SA %s[%d]",
109 this->ike_sa->get_name(this->ike_sa),
110 this->ike_sa->get_unique_id(this->ike_sa));
111 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
112 this->ike_sa->get_name(this->ike_sa),
113 this->ike_sa->get_unique_id(this->ike_sa),
114 this->ike_sa->get_my_host(this->ike_sa),
115 this->ike_sa->get_my_id(this->ike_sa),
116 this->ike_sa->get_other_host(this->ike_sa),
117 this->ike_sa->get_other_id(this->ike_sa));
118
119 switch (this->ike_sa->get_state(this->ike_sa))
120 {
121 case IKE_ESTABLISHED:
122 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
123 this->ike_sa->reestablish(this->ike_sa);
124 if (this->ike_sa->get_version(this->ike_sa) == IKEV2)
125 {
126 return NEED_MORE;
127 }
128 else
129 {
130 /* Dont send message to other side */
131 return DESTROY_ME;
132 }
133 case IKE_REKEYING:
134 this->rekeyed = TRUE;
135 break;
136 case IKE_DELETING:
137 this->simultaneous = TRUE;
138 break;
139 default:
140 break;
141 }
142 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
143 return NEED_MORE;
144 }
145
146 METHOD(task_t, build_r, status_t,
147 private_ike_delete_t *this, message_t *message)
148 {
149 DBG0(DBG_IKE, "IKE_SA deleted");
150
151 if (this->simultaneous)
152 {
153 /* wait for peer's response for our delete request, but set a timeout */
154 return SUCCESS;
155 }
156 if (!this->rekeyed)
157 { /* invoke ike_down() hook if SA has not been rekeyed */
158 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
159 }
160 /* completed, delete IKE_SA by returning DESTROY_ME */
161 return DESTROY_ME;
162 }
163
164 METHOD(task_t, get_type, task_type_t,
165 private_ike_delete_t *this)
166 {
167 return TASK_IKE_DELETE;
168 }
169
170 METHOD(task_t, migrate, void,
171 private_ike_delete_t *this, ike_sa_t *ike_sa)
172 {
173 this->ike_sa = ike_sa;
174 this->simultaneous = FALSE;
175 }
176
177 METHOD(task_t, destroy, void,
178 private_ike_delete_t *this)
179 {
180 free(this);
181 }
182
183 /*
184 * Described in header.
185 */
186 ike_delete_t *ike_delete_create(ike_sa_t *ike_sa, bool initiator)
187 {
188 private_ike_delete_t *this;
189
190 INIT(this,
191 .public = {
192 .task = {
193 .get_type = _get_type,
194 .migrate = _migrate,
195 .destroy = _destroy,
196 },
197 },
198 .ike_sa = ike_sa,
199 .initiator = initiator,
200 );
201
202 if (initiator)
203 {
204 this->public.task.build = _build_i;
205 this->public.task.process = _process_i;
206 }
207 else
208 {
209 this->public.task.build = _build_r;
210 this->public.task.process = _process_r;
211 }
212
213 return &this->public;
214 }