re-established all previous AUD level messages
[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 DBG0(DBG_IKE, "IKE_SA deleted");
84 /* completed, delete IKE_SA by returning FAILED */
85 return FAILED;
86 }
87
88 /**
89 * Implementation of task_t.process for responder
90 */
91 static status_t process_r(private_ike_delete_t *this, message_t *message)
92 {
93 /* we don't even scan the payloads, as the message wouldn't have
94 * come so far without being correct */
95 DBG1(DBG_IKE, "received DELETE for IKE_SA %s[%d]",
96 this->ike_sa->get_name(this->ike_sa),
97 this->ike_sa->get_unique_id(this->ike_sa));
98 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%D]...%H[%D]",
99 this->ike_sa->get_name(this->ike_sa),
100 this->ike_sa->get_unique_id(this->ike_sa),
101 this->ike_sa->get_my_host(this->ike_sa),
102 this->ike_sa->get_my_id(this->ike_sa),
103 this->ike_sa->get_other_host(this->ike_sa),
104 this->ike_sa->get_other_id(this->ike_sa));
105
106 switch (this->ike_sa->get_state(this->ike_sa))
107 {
108 case IKE_ESTABLISHED:
109 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
110 this->ike_sa->reestablish(this->ike_sa);
111 break;
112 case IKE_DELETING:
113 this->simultaneous = TRUE;
114 /* FALL */
115 default:
116 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
117 break;
118 }
119 return NEED_MORE;
120 }
121
122 /**
123 * Implementation of task_t.build for responder
124 */
125 static status_t build_r(private_ike_delete_t *this, message_t *message)
126 {
127 DBG0(DBG_IKE, "IKE_SA deleted");
128
129 if (this->simultaneous)
130 {
131 /* wait for peer's response for our delete request, but set a timeout */
132 return SUCCESS;
133 }
134 /* completed, delete IKE_SA by returning FAILED */
135 return FAILED;
136 }
137
138 /**
139 * Implementation of task_t.get_type
140 */
141 static task_type_t get_type(private_ike_delete_t *this)
142 {
143 return IKE_DELETE;
144 }
145
146 /**
147 * Implementation of task_t.migrate
148 */
149 static void migrate(private_ike_delete_t *this, ike_sa_t *ike_sa)
150 {
151 this->ike_sa = ike_sa;
152 this->simultaneous = FALSE;
153 }
154
155 /**
156 * Implementation of task_t.destroy
157 */
158 static void destroy(private_ike_delete_t *this)
159 {
160 free(this);
161 }
162
163 /*
164 * Described in header.
165 */
166 ike_delete_t *ike_delete_create(ike_sa_t *ike_sa, bool initiator)
167 {
168 private_ike_delete_t *this = malloc_thing(private_ike_delete_t);
169
170 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
171 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
172 this->public.task.destroy = (void(*)(task_t*))destroy;
173
174 if (initiator)
175 {
176 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
177 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
178 }
179 else
180 {
181 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
182 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
183 }
184
185 this->ike_sa = ike_sa;
186 this->initiator = initiator;
187 this->simultaneous = FALSE;
188
189 return &this->public;
190 }