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