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