completed IKE_SA logging at the AUDIT level
[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 SIG_IKE(DOWN_SUCCESS, "IKE_SA deleted");
69
70 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
71
72 /* reauthenticate only if we have children */
73 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
74 if (iterator->get_count(iterator) == 0
75 #ifdef ME
76 /* we allow a peer to reauth a mediation connection (without CHILD_SA) */
77 && !peer_cfg->is_mediation(peer_cfg)
78 #endif /* ME */
79 )
80 {
81 DBG1(DBG_IKE, "unable to reauthenticate IKE_SA, no CHILD_SA to recreate");
82 iterator->destroy(iterator);
83 return FAILED;
84 }
85
86 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager, TRUE);
87
88 new->set_peer_cfg(new, peer_cfg);
89 host = this->ike_sa->get_other_host(this->ike_sa);
90 new->set_other_host(new, host->clone(host));
91 host = this->ike_sa->get_my_host(this->ike_sa);
92 new->set_my_host(new, host->clone(host));
93 /* if we already have a virtual IP, we reuse it */
94 host = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
95 if (host)
96 {
97 new->set_virtual_ip(new, TRUE, host);
98 }
99
100 #ifdef ME
101 /* we initiate the new IKE_SA of the mediation connection without CHILD_SA */
102 if (peer_cfg->is_mediation(peer_cfg))
103 {
104 if (new->initiate(new, NULL) == DESTROY_ME)
105 {
106 charon->ike_sa_manager->checkin_and_destroy(
107 charon->ike_sa_manager, new);
108 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
109 return FAILED;
110 }
111 }
112 #endif /* ME */
113
114 while (iterator->iterate(iterator, (void**)&child_sa))
115 {
116 switch (child_sa->get_state(child_sa))
117 {
118 case CHILD_ROUTED:
119 {
120 /* move routed child directly */
121 iterator->remove(iterator);
122 new->add_child_sa(new, child_sa);
123 break;
124 }
125 default:
126 {
127 /* initiate/queue all child SAs */
128 child_cfg_t *child_cfg = child_sa->get_config(child_sa);
129 child_cfg->get_ref(child_cfg);
130 if (new->initiate(new, child_cfg) == DESTROY_ME)
131 {
132 iterator->destroy(iterator);
133 charon->ike_sa_manager->checkin_and_destroy(
134 charon->ike_sa_manager, new);
135 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
136 return FAILED;
137 }
138 break;
139 }
140 }
141 }
142 iterator->destroy(iterator);
143 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
144
145 /* we always return failed to delete the obsolete IKE_SA */
146 return FAILED;
147 }
148
149 /**
150 * Implementation of task_t.get_type
151 */
152 static task_type_t get_type(private_ike_reauth_t *this)
153 {
154 return IKE_REAUTH;
155 }
156
157 /**
158 * Implementation of task_t.migrate
159 */
160 static void migrate(private_ike_reauth_t *this, ike_sa_t *ike_sa)
161 {
162 this->ike_delete->task.migrate(&this->ike_delete->task, ike_sa);
163 this->ike_sa = ike_sa;
164 }
165
166 /**
167 * Implementation of task_t.destroy
168 */
169 static void destroy(private_ike_reauth_t *this)
170 {
171 this->ike_delete->task.destroy(&this->ike_delete->task);
172 free(this);
173 }
174
175 /*
176 * Described in header.
177 */
178 ike_reauth_t *ike_reauth_create(ike_sa_t *ike_sa)
179 {
180 private_ike_reauth_t *this = malloc_thing(private_ike_reauth_t);
181
182 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
183 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
184 this->public.task.destroy = (void(*)(task_t*))destroy;
185 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
186 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
187
188 this->ike_sa = ike_sa;
189 this->ike_delete = ike_delete_create(ike_sa, TRUE);
190
191 return &this->public;
192 }
193