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