1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* Copyright (C) 2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2012 Martin Willi
* Copyright (C) 2012 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "adopt_children_job.h"
#include <daemon.h>
#include <hydra.h>
#include <collections/array.h>
typedef struct private_adopt_children_job_t private_adopt_children_job_t;
/**
* Private data of an adopt_children_job_t object.
*/
struct private_adopt_children_job_t {
/**
* Public adopt_children_job_t interface.
*/
adopt_children_job_t public;
/**
* IKE_SA id to adopt children from
*/
ike_sa_id_t *id;
/**
* Tasks queued for execution
*/
array_t *tasks;
};
METHOD(job_t, destroy, void,
private_adopt_children_job_t *this)
{
array_destroy_offset(this->tasks, offsetof(task_t, destroy));
this->id->destroy(this->id);
free(this);
}
METHOD(job_t, execute, job_requeue_t,
private_adopt_children_job_t *this)
{
identification_t *my_id, *other_id, *xauth;
host_t *me, *other, *vip;
peer_cfg_t *cfg;
linked_list_t *children, *vips;
enumerator_t *enumerator, *subenum;
ike_sa_id_t *id;
ike_sa_t *ike_sa;
child_sa_t *child_sa;
ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, this->id);
if (ike_sa)
{
/* get what we need from new SA */
me = ike_sa->get_my_host(ike_sa);
me = me->clone(me);
other = ike_sa->get_other_host(ike_sa);
other = other->clone(other);
my_id = ike_sa->get_my_id(ike_sa);
my_id = my_id->clone(my_id);
other_id = ike_sa->get_other_id(ike_sa);
other_id = other_id->clone(other_id);
xauth = ike_sa->get_other_eap_id(ike_sa);
xauth = xauth->clone(xauth);
cfg = ike_sa->get_peer_cfg(ike_sa);
cfg->get_ref(cfg);
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
/* find old SA to adopt children and virtual IPs from */
vips = linked_list_create();
children = linked_list_create();
enumerator = charon->ike_sa_manager->create_id_enumerator(
charon->ike_sa_manager, my_id, xauth,
other->get_family(other));
while (enumerator->enumerate(enumerator, &id))
{
if (id->equals(id, this->id))
{ /* not from self */
continue;
}
ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
if (ike_sa)
{
if ((ike_sa->get_state(ike_sa) == IKE_ESTABLISHED ||
ike_sa->get_state(ike_sa) == IKE_PASSIVE) &&
me->equals(me, ike_sa->get_my_host(ike_sa)) &&
other->equals(other, ike_sa->get_other_host(ike_sa)) &&
other_id->equals(other_id, ike_sa->get_other_id(ike_sa)) &&
cfg->equals(cfg, ike_sa->get_peer_cfg(ike_sa)))
{
subenum = ike_sa->create_child_sa_enumerator(ike_sa);
while (subenum->enumerate(subenum, &child_sa))
{
ike_sa->remove_child_sa(ike_sa, subenum);
children->insert_last(children, child_sa);
}
subenum->destroy(subenum);
subenum = ike_sa->create_virtual_ip_enumerator(ike_sa, FALSE);
while (subenum->enumerate(subenum, &vip))
{
vips->insert_last(vips, vip->clone(vip));
}
subenum->destroy(subenum);
/* this does not release the addresses, which is good, but
* it does trigger an assign_vips(FALSE) event, so we also
* trigger one below */
ike_sa->clear_virtual_ips(ike_sa, FALSE);
if (children->get_count(children) || vips->get_count(vips))
{
DBG1(DBG_IKE, "detected reauth of existing IKE_SA, "
"adopting %d children and %d virtual IPs",
children->get_count(children), vips->get_count(vips));
}
ike_sa->set_state(ike_sa, IKE_DELETING);
charon->bus->ike_updown(charon->bus, ike_sa, FALSE);
charon->ike_sa_manager->checkin_and_destroy(
charon->ike_sa_manager, ike_sa);
}
else
{
charon->ike_sa_manager->checkin(
charon->ike_sa_manager, ike_sa);
}
if (children->get_count(children) || vips->get_count(vips))
{
break;
}
}
}
enumerator->destroy(enumerator);
me->destroy(me);
other->destroy(other);
my_id->destroy(my_id);
other_id->destroy(other_id);
xauth->destroy(xauth);
cfg->destroy(cfg);
if (children->get_count(children) || vips->get_count(vips))
{
/* adopt children by new SA */
ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
this->id);
if (ike_sa)
{
while (children->remove_last(children,
(void**)&child_sa) == SUCCESS)
{
ike_sa->add_child_sa(ike_sa, child_sa);
}
if (vips->get_count(vips))
{
while (vips->remove_first(vips, (void**)&vip) == SUCCESS)
{
ike_sa->add_virtual_ip(ike_sa, FALSE, vip);
vip->destroy(vip);
}
charon->bus->assign_vips(charon->bus, ike_sa, TRUE);
}
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}
}
children->destroy_offset(children, offsetof(child_sa_t, destroy));
/* FIXME: If we still have addresses here it means we weren't able to
* find the new SA anymore (while not very likely during a proper
* reauthentication, this theoretically could happen because the SA is
* not locked while we search for the old one). So the addresses here
* should be released properly to avoid leaking these leases. This is
* currently not possible, though, due to the changed interface of
* release_address(), which now takes a complete IKE_SA object. */
vips->destroy_offset(vips, offsetof(host_t, destroy));
if (array_count(this->tasks))
{
ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
this->id);
if (ike_sa)
{
task_t *task;
while (array_remove(this->tasks, ARRAY_HEAD, &task))
{
task->migrate(task, ike_sa);
ike_sa->queue_task(ike_sa, task);
}
if (ike_sa->initiate(ike_sa, NULL, 0, NULL, NULL) == DESTROY_ME)
{
charon->ike_sa_manager->checkin_and_destroy(
charon->ike_sa_manager, ike_sa);
}
else
{
charon->ike_sa_manager->checkin(charon->ike_sa_manager,
ike_sa);
}
}
}
}
return JOB_REQUEUE_NONE;
}
METHOD(job_t, get_priority, job_priority_t,
private_adopt_children_job_t *this)
{
return JOB_PRIO_HIGH;
}
METHOD(adopt_children_job_t, queue_task, void,
private_adopt_children_job_t *this, task_t *task)
{
array_insert_create(&this->tasks, ARRAY_TAIL, task);
}
/**
* See header
*/
adopt_children_job_t *adopt_children_job_create(ike_sa_id_t *id)
{
private_adopt_children_job_t *this;
INIT(this,
.public = {
.job_interface = {
.execute = _execute,
.get_priority = _get_priority,
.destroy = _destroy,
},
.queue_task = _queue_task,
},
.id = id->clone(id),
);
return &this->public;
}
|