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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 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 "ha_cache.h"
#include <utils/hashtable.h>
#include <utils/linked_list.h>
#include <threading/mutex.h>
#include <processing/jobs/callback_job.h>
typedef struct private_ha_cache_t private_ha_cache_t;
/**
* Private data of an ha_cache_t object.
*/
struct private_ha_cache_t {
/**
* Public ha_cache_t interface.
*/
ha_cache_t public;
/**
* Kernel helper functions
*/
ha_kernel_t *kernel;
/**
* Socket to send sync messages over
*/
ha_socket_t *socket;
/**
* Total number of segments
*/
u_int count;
/**
* cached entries (ike_sa_t, entry_t)
*/
hashtable_t *cache;
/**
* Mutex to lock cache
*/
mutex_t *mutex;
};
/**
* Hashtable hash function
*/
static u_int hash(void *key)
{
return (uintptr_t)key;
}
/**
* Hashtable equals function
*/
static bool equals(void *a, void *b)
{
return a == b;
}
/**
* Cache entry for an IKE_SA
*/
typedef struct {
/* segment this entry is associate to */
u_int segment;
/* ADD message */
ha_message_t *add;
/* list of updates UPDATE message */
linked_list_t *updates;
/* last initiator mid */
ha_message_t *midi;
/* last responder mid */
ha_message_t *midr;
} entry_t;
/**
* Create a entry with an add message
*/
static entry_t *entry_create(ha_message_t *add)
{
entry_t *entry;
INIT(entry,
.add = add,
.updates = linked_list_create(),
);
return entry;
}
/**
* clean up a entry
*/
static void entry_destroy(entry_t *entry)
{
entry->updates->destroy_offset(entry->updates,
offsetof(ha_message_t, destroy));
entry->add->destroy(entry->add);
DESTROY_IF(entry->midi);
DESTROY_IF(entry->midr);
free(entry);
}
METHOD(ha_cache_t, cache, void,
private_ha_cache_t *this, ike_sa_t *ike_sa, ha_message_t *message)
{
entry_t *entry;
this->mutex->lock(this->mutex);
switch (message->get_type(message))
{
case HA_IKE_ADD:
entry = entry_create(message);
entry = this->cache->put(this->cache, ike_sa, entry);
if (entry)
{
entry_destroy(entry);
}
break;
case HA_IKE_UPDATE:
entry = this->cache->get(this->cache, ike_sa);
if (entry)
{
entry->segment = this->kernel->get_segment(this->kernel,
ike_sa->get_other_host(ike_sa));
entry->updates->insert_last(entry->updates, message);
break;
}
message->destroy(message);
break;
case HA_IKE_MID_INITIATOR:
entry = this->cache->get(this->cache, ike_sa);
if (entry)
{
DESTROY_IF(entry->midi);
entry->midi = message;
break;
}
message->destroy(message);
break;
case HA_IKE_MID_RESPONDER:
entry = this->cache->get(this->cache, ike_sa);
if (entry)
{
DESTROY_IF(entry->midr);
entry->midr = message;
break;
}
message->destroy(message);
break;
case HA_IKE_DELETE:
entry = this->cache->remove(this->cache, ike_sa);
if (entry)
{
entry_destroy(entry);
}
message->destroy(message);
break;
default:
message->destroy(message);
break;
}
this->mutex->unlock(this->mutex);
}
METHOD(ha_cache_t, delete_, void,
private_ha_cache_t *this, ike_sa_t *ike_sa)
{
entry_t *entry;
entry = this->cache->remove(this->cache, ike_sa);
if (entry)
{
entry_destroy(entry);
}
}
/**
* Rekey all children of an IKE_SA
*/
static status_t rekey_children(ike_sa_t *ike_sa)
{
iterator_t *iterator;
child_sa_t *child_sa;
status_t status = SUCCESS;
iterator = ike_sa->create_child_sa_iterator(ike_sa);
while (iterator->iterate(iterator, (void**)&child_sa))
{
DBG1(DBG_CFG, "resyncing CHILD_SA");
status = ike_sa->rekey_child_sa(ike_sa, child_sa->get_protocol(child_sa),
child_sa->get_spi(child_sa, TRUE));
if (status == DESTROY_ME)
{
break;
}
}
iterator->destroy(iterator);
return status;
}
/**
* Trigger rekeying of CHILD_SA in segment
*/
static void rekey_segment(private_ha_cache_t *this, u_int segment)
{
ike_sa_t *ike_sa;
enumerator_t *enumerator;
linked_list_t *list;
ike_sa_id_t *id;
list = linked_list_create();
enumerator = charon->ike_sa_manager->create_enumerator(
charon->ike_sa_manager);
while (enumerator->enumerate(enumerator, &ike_sa))
{
if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED &&
this->kernel->get_segment(this->kernel,
ike_sa->get_other_host(ike_sa)) == segment)
{
id = ike_sa->get_id(ike_sa);
list->insert_last(list, id->clone(id));
}
}
enumerator->destroy(enumerator);
while (list->remove_last(list, (void**)&id) == SUCCESS)
{
ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
if (ike_sa)
{
if (rekey_children(ike_sa) != DESTROY_ME)
{
charon->ike_sa_manager->checkin(
charon->ike_sa_manager, ike_sa);
}
else
{
charon->ike_sa_manager->checkin_and_destroy(
charon->ike_sa_manager, ike_sa);
}
}
id->destroy(id);
}
list->destroy(list);
}
METHOD(ha_cache_t, resync, void,
private_ha_cache_t *this, u_int segment)
{
enumerator_t *enumerator, *updates;
ike_sa_t *ike_sa;
entry_t *entry;
ha_message_t *message;
DBG1(DBG_CFG, "resyncing HA segment %d", segment);
this->mutex->lock(this->mutex);
enumerator = this->cache->create_enumerator(this->cache);
while (enumerator->enumerate(enumerator, &ike_sa, &entry))
{
if (entry->segment == segment)
{
this->socket->push(this->socket, entry->add);
updates = entry->updates->create_enumerator(entry->updates);
while (updates->enumerate(updates, &message))
{
this->socket->push(this->socket, message);
}
updates->destroy(updates);
if (entry->midi)
{
this->socket->push(this->socket, entry->midi);
}
if (entry->midr)
{
this->socket->push(this->socket, entry->midr);
}
}
}
enumerator->destroy(enumerator);
this->mutex->unlock(this->mutex);
rekey_segment(this, segment);
}
/**
* Request a resync of all segments
*/
static job_requeue_t request_resync(private_ha_cache_t *this)
{
ha_message_t *message;
int i;
DBG1(DBG_CFG, "requesting HA resynchronization");
message = ha_message_create(HA_RESYNC);
for (i = 1; i <= this->count; i++)
{
message->add_attribute(message, HA_SEGMENT, i);
}
this->socket->push(this->socket, message);
message->destroy(message);
return JOB_REQUEUE_NONE;
}
METHOD(ha_cache_t, destroy, void,
private_ha_cache_t *this)
{
this->cache->destroy(this->cache);
this->mutex->destroy(this->mutex);
free(this);
}
/**
* See header
*/
ha_cache_t *ha_cache_create(ha_kernel_t *kernel, ha_socket_t *socket,
bool sync, u_int count)
{
private_ha_cache_t *this;
INIT(this,
.public = {
.cache = _cache,
.delete = _delete_,
.resync = _resync,
.destroy = _destroy,
},
.count = count,
.kernel = kernel,
.socket = socket,
.cache = hashtable_create(hash, equals, 8),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
);
if (sync)
{
/* request a resync as soon as we are up */
charon->scheduler->schedule_job(charon->scheduler, (job_t*)
callback_job_create((callback_job_cb_t)request_resync,
this, NULL, NULL), 1);
}
return &this->public;
}
|