67dddb01d5525f9e3bbe282d061f0b82a1906e5f
[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 delete_payload = delete_payload_create(PROTO_IKE);
60 message->add_payload(message, (payload_t*)delete_payload);
61
62 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
63 DBG1(DBG_IKE, "sending DELETE for IKE_SA %s[%d]",
64 this->ike_sa->get_name(this->ike_sa),
65 this->ike_sa->get_unique_id(this->ike_sa));
66 return NEED_MORE;
67 }
68
69 /**
70 * Implementation of task_t.process for initiator
71 */
72 static status_t process_i(private_ike_delete_t *this, message_t *message)
73 {
74 /* completed, delete IKE_SA by returning FAILED */
75 return FAILED;
76 }
77
78 /**
79 * Implementation of task_t.process for initiator
80 */
81 static status_t process_r(private_ike_delete_t *this, message_t *message)
82 {
83 /* we don't even scan the payloads, as the message wouldn't have
84 * come so far without being correct */
85 switch (this->ike_sa->get_state(this->ike_sa))
86 {
87 case IKE_ESTABLISHED:
88 DBG1(DBG_IKE, "received DELETE for IKE_SA %s[%d]",
89 this->ike_sa->get_name(this->ike_sa),
90 this->ike_sa->get_unique_id(this->ike_sa));
91 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
92 this->ike_sa->reestablish(this->ike_sa);
93 break;
94 case IKE_DELETING:
95 this->simultaneous = TRUE;
96 /* FALL */
97 default:
98 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
99 break;
100 }
101 return NEED_MORE;
102 }
103
104 /**
105 * Implementation of task_t.build for responder
106 */
107 static status_t build_r(private_ike_delete_t *this, message_t *message)
108 {
109 if (this->simultaneous)
110 {
111 /* wait for peers response for our delete request, but set a timeout */
112 return SUCCESS;
113 }
114 /* completed, delete IKE_SA by returning FAILED */
115 return FAILED;
116 }
117
118 /**
119 * Implementation of task_t.get_type
120 */
121 static task_type_t get_type(private_ike_delete_t *this)
122 {
123 return IKE_DELETE;
124 }
125
126 /**
127 * Implementation of task_t.migrate
128 */
129 static void migrate(private_ike_delete_t *this, ike_sa_t *ike_sa)
130 {
131 this->ike_sa = ike_sa;
132 this->simultaneous = FALSE;
133 }
134
135 /**
136 * Implementation of task_t.destroy
137 */
138 static void destroy(private_ike_delete_t *this)
139 {
140 free(this);
141 }
142
143 /*
144 * Described in header.
145 */
146 ike_delete_t *ike_delete_create(ike_sa_t *ike_sa, bool initiator)
147 {
148 private_ike_delete_t *this = malloc_thing(private_ike_delete_t);
149
150 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
151 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
152 this->public.task.destroy = (void(*)(task_t*))destroy;
153
154 if (initiator)
155 {
156 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
157 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
158 }
159 else
160 {
161 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
162 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
163 }
164
165 this->ike_sa = ike_sa;
166 this->initiator = initiator;
167 this->simultaneous = FALSE;
168
169 return &this->public;
170 }