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