Moving charon to libcharon.
[strongswan.git] / src / libcharon / sa / tasks / ike_reauth.c
1 /*
2 * Copyright (C) 2006-2008 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_reauth.h"
17
18 #include <daemon.h>
19 #include <sa/tasks/ike_delete.h>
20
21
22 typedef struct private_ike_reauth_t private_ike_reauth_t;
23
24 /**
25 * Private members of a ike_reauth_t task.
26 */
27 struct private_ike_reauth_t {
28
29 /**
30 * Public methods and task_t interface.
31 */
32 ike_reauth_t public;
33
34 /**
35 * Assigned IKE_SA.
36 */
37 ike_sa_t *ike_sa;
38
39 /**
40 * reused ike_delete task
41 */
42 ike_delete_t *ike_delete;
43 };
44
45 /**
46 * Implementation of task_t.build for initiator
47 */
48 static status_t build_i(private_ike_reauth_t *this, message_t *message)
49 {
50 return this->ike_delete->task.build(&this->ike_delete->task, message);
51 }
52
53 /**
54 * Implementation of task_t.process for initiator
55 */
56 static status_t process_i(private_ike_reauth_t *this, message_t *message)
57 {
58 ike_sa_t *new;
59 host_t *host;
60 iterator_t *iterator;
61 child_sa_t *child_sa;
62 peer_cfg_t *peer_cfg;
63
64 /* process delete response first */
65 this->ike_delete->task.process(&this->ike_delete->task, message);
66
67 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
68
69 /* reauthenticate only if we have children */
70 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
71 if (iterator->get_count(iterator) == 0
72 #ifdef ME
73 /* we allow peers to reauth mediation connections (without children) */
74 && !peer_cfg->is_mediation(peer_cfg)
75 #endif /* ME */
76 )
77 {
78 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA to recreate");
79 iterator->destroy(iterator);
80 return FAILED;
81 }
82
83 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, TRUE);
84
85 new->set_peer_cfg(new, peer_cfg);
86 host = this->ike_sa->get_other_host(this->ike_sa);
87 new->set_other_host(new, host->clone(host));
88 host = this->ike_sa->get_my_host(this->ike_sa);
89 new->set_my_host(new, host->clone(host));
90 /* if we already have a virtual IP, we reuse it */
91 host = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
92 if (host)
93 {
94 new->set_virtual_ip(new, TRUE, host);
95 }
96
97 #ifdef ME
98 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
99 if (peer_cfg->is_mediation(peer_cfg))
100 {
101 if (new->initiate(new, NULL, 0, NULL, NULL) == DESTROY_ME)
102 {
103 charon->ike_sa_manager->checkin_and_destroy(
104 charon->ike_sa_manager, new);
105 /* set threads active IKE_SA after checkin */
106 charon->bus->set_sa(charon->bus, this->ike_sa);
107 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
108 return FAILED;
109 }
110 }
111 #endif /* ME */
112
113 while (iterator->iterate(iterator, (void**)&child_sa))
114 {
115 switch (child_sa->get_state(child_sa))
116 {
117 case CHILD_ROUTED:
118 {
119 /* move routed child directly */
120 iterator->remove(iterator);
121 new->add_child_sa(new, child_sa);
122 break;
123 }
124 default:
125 {
126 /* initiate/queue all child SAs */
127 child_cfg_t *child_cfg = child_sa->get_config(child_sa);
128 child_cfg->get_ref(child_cfg);
129 if (new->initiate(new, child_cfg, 0, NULL, NULL) == DESTROY_ME)
130 {
131 iterator->destroy(iterator);
132 charon->ike_sa_manager->checkin_and_destroy(
133 charon->ike_sa_manager, new);
134 /* set threads active IKE_SA after checkin */
135 charon->bus->set_sa(charon->bus, this->ike_sa);
136 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
137 return FAILED;
138 }
139 break;
140 }
141 }
142 }
143 iterator->destroy(iterator);
144 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
145 /* set threads active IKE_SA after checkin */
146 charon->bus->set_sa(charon->bus, this->ike_sa);
147
148 /* we always return failed to delete the obsolete IKE_SA */
149 return FAILED;
150 }
151
152 /**
153 * Implementation of task_t.get_type
154 */
155 static task_type_t get_type(private_ike_reauth_t *this)
156 {
157 return IKE_REAUTH;
158 }
159
160 /**
161 * Implementation of task_t.migrate
162 */
163 static void migrate(private_ike_reauth_t *this, ike_sa_t *ike_sa)
164 {
165 this->ike_delete->task.migrate(&this->ike_delete->task, ike_sa);
166 this->ike_sa = ike_sa;
167 }
168
169 /**
170 * Implementation of task_t.destroy
171 */
172 static void destroy(private_ike_reauth_t *this)
173 {
174 this->ike_delete->task.destroy(&this->ike_delete->task);
175 free(this);
176 }
177
178 /*
179 * Described in header.
180 */
181 ike_reauth_t *ike_reauth_create(ike_sa_t *ike_sa)
182 {
183 private_ike_reauth_t *this = malloc_thing(private_ike_reauth_t);
184
185 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
186 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
187 this->public.task.destroy = (void(*)(task_t*))destroy;
188 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
189 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
190
191 this->ike_sa = ike_sa;
192 this->ike_delete = ike_delete_create(ike_sa, TRUE);
193
194 return &this->public;
195 }
196