Moved segment configuration parsing to ha_sync_plugin
[strongswan.git] / src / charon / plugins / ha_sync / ha_sync_segments.c
1 /*
2 * Copyright (C) 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 "ha_sync_segments.h"
17
18 #include <utils/mutex.h>
19 #include <utils/linked_list.h>
20
21 typedef u_int32_t u32;
22 typedef u_int8_t u8;
23
24 #include <linux/jhash.h>
25
26 typedef struct private_ha_sync_segments_t private_ha_sync_segments_t;
27
28 /**
29 * Private data of an ha_sync_segments_t object.
30 */
31 struct private_ha_sync_segments_t {
32
33 /**
34 * Public ha_sync_segments_t interface.
35 */
36 ha_sync_segments_t public;
37
38 /**
39 * communication socket
40 */
41 ha_sync_socket_t *socket;
42
43 /**
44 * read/write lock for segment manipulation
45 */
46 rwlock_t *lock;
47
48 /**
49 * Init value for jhash
50 */
51 u_int initval;
52
53 /**
54 * Total number of ClusterIP segments
55 */
56 u_int segment_count;
57
58 /**
59 * mask of active segments
60 */
61 segment_mask_t active;
62 };
63
64 /**
65 * Check if a host address is in the CLUSTERIP segment
66 */
67 static bool in_segment(private_ha_sync_segments_t *this,
68 host_t *host, u_int segment)
69 {
70 if (host->get_family(host) == AF_INET)
71 {
72 unsigned long hash;
73 u_int32_t addr;
74
75 addr = *(u_int32_t*)host->get_address(host).ptr;
76 hash = jhash_1word(ntohl(addr), this->initval);
77
78 if ((((u_int64_t)hash * this->segment_count) >> 32) + 1 == segment)
79 {
80 return TRUE;
81 }
82 }
83 return FALSE;
84 }
85
86 /**
87 * Log currently active segments
88 */
89 static void log_segments(private_ha_sync_segments_t *this, bool activated,
90 u_int segment)
91 {
92 char buf[64] = "none", *pos = buf;
93 int i;
94 bool first = TRUE;
95
96 for (i = 0; i < this->segment_count; i++)
97 {
98 if (this->active & 0x01 << i)
99 {
100 if (first)
101 {
102 first = FALSE;
103 }
104 else
105 {
106 pos += snprintf(pos, buf + sizeof(buf) - pos, ",");
107 }
108 pos += snprintf(pos, buf + sizeof(buf) - pos, "%d", i+1);
109 }
110 }
111 DBG1(DBG_CFG, "HA sync segment %d %sactivated, now active: %s",
112 segment, activated ? "" : "de", buf);
113 }
114
115 /**
116 * Enable/Disable an an IKE_SA.
117 */
118 static void enable_disable(private_ha_sync_segments_t *this, u_int segment,
119 ike_sa_state_t old, ike_sa_state_t new, bool enable)
120 {
121 ike_sa_t *ike_sa;
122 enumerator_t *enumerator;
123 u_int i, limit;
124
125 this->lock->write_lock(this->lock);
126
127 if (segment == 0 || segment <= this->segment_count)
128 {
129 if (segment)
130 { /* loop once for single segment ... */
131 limit = segment + 1;
132 }
133 else
134 { /* or segment_count times for all segments */
135 limit = this->segment_count;
136 }
137 for (i = segment; i < limit; i++)
138 {
139 if (enable)
140 {
141 this->active |= SEGMENTS_BIT(i);
142 }
143 else
144 {
145 this->active &= ~SEGMENTS_BIT(i);
146 }
147 }
148 enumerator = charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager);
149 while (enumerator->enumerate(enumerator, &ike_sa))
150 {
151 if (ike_sa->get_state(ike_sa) == old)
152 {
153 for (i = segment; i < limit; i++)
154 {
155 if (in_segment(this, ike_sa->get_other_host(ike_sa), i))
156 {
157 ike_sa->set_state(ike_sa, new);
158 }
159 }
160 }
161 }
162 enumerator->destroy(enumerator);
163
164 log_segments(this, enable, segment);
165 }
166
167 this->lock->unlock(this->lock);
168 }
169
170 /**
171 * Implementation of ha_sync_segments_t.activate
172 */
173 static void activate(private_ha_sync_segments_t *this, u_int segment,
174 bool notify)
175 {
176 ha_sync_message_t *message;
177
178 enable_disable(this, segment, IKE_PASSIVE, IKE_ESTABLISHED, TRUE);
179
180 if (notify)
181 {
182 message = ha_sync_message_create(HA_SYNC_SEGMENT_TAKE);
183 message->add_attribute(message, HA_SYNC_SEGMENT, segment);
184 this->socket->push(this->socket, message);
185 }
186 }
187
188 /**
189 * Implementation of ha_sync_segments_t.deactivate
190 */
191 static void deactivate(private_ha_sync_segments_t *this, u_int segment,
192 bool notify)
193 {
194 ha_sync_message_t *message;
195
196 enable_disable(this, segment, IKE_ESTABLISHED, IKE_PASSIVE, FALSE);
197
198 if (notify)
199 {
200 message = ha_sync_message_create(HA_SYNC_SEGMENT_DROP);
201 message->add_attribute(message, HA_SYNC_SEGMENT, segment);
202 this->socket->push(this->socket, message);
203 }
204 }
205
206 /**
207 * Rekey all children of an IKE_SA
208 */
209 static status_t rekey_children(ike_sa_t *ike_sa)
210 {
211 iterator_t *iterator;
212 child_sa_t *child_sa;
213 status_t status = SUCCESS;
214
215 iterator = ike_sa->create_child_sa_iterator(ike_sa);
216 while (iterator->iterate(iterator, (void**)&child_sa))
217 {
218 DBG1(DBG_CFG, "resyncing CHILD_SA");
219 status = ike_sa->rekey_child_sa(ike_sa, child_sa->get_protocol(child_sa),
220 child_sa->get_spi(child_sa, TRUE));
221 if (status == DESTROY_ME)
222 {
223 break;
224 }
225 }
226 iterator->destroy(iterator);
227 return status;
228 }
229
230 /**
231 * Implementation of ha_sync_segments_t.resync
232 */
233 static void resync(private_ha_sync_segments_t *this, u_int segment)
234 {
235 ike_sa_t *ike_sa;
236 enumerator_t *enumerator;
237 linked_list_t *list;
238 ike_sa_id_t *id;
239 u_int16_t mask = SEGMENTS_BIT(segment);
240
241 list = linked_list_create();
242 this->lock->read_lock(this->lock);
243
244 if (segment > 0 && segment <= this->segment_count && (this->active & mask))
245 {
246 this->active &= ~mask;
247
248 DBG1(DBG_CFG, "resyncing HA sync segment %d", segment);
249
250 /* we do the actual rekeying in a seperate loop to avoid rekeying
251 * an SA twice. */
252 enumerator = charon->ike_sa_manager->create_enumerator(
253 charon->ike_sa_manager);
254 while (enumerator->enumerate(enumerator, &ike_sa))
255 {
256 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED &&
257 in_segment(this, ike_sa->get_other_host(ike_sa), segment))
258 {
259 id = ike_sa->get_id(ike_sa);
260 list->insert_last(list, id->clone(id));
261 }
262 }
263 enumerator->destroy(enumerator);
264 }
265 this->lock->unlock(this->lock);
266
267 while (list->remove_last(list, (void**)&id) == SUCCESS)
268 {
269 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
270 id->destroy(id);
271 if (ike_sa)
272 {
273 DBG1(DBG_CFG, "resyncing IKE_SA");
274 if (ike_sa->rekey(ike_sa) != DESTROY_ME)
275 {
276 if (rekey_children(ike_sa) != DESTROY_ME)
277 {
278 charon->ike_sa_manager->checkin(
279 charon->ike_sa_manager, ike_sa);
280 continue;
281 }
282 }
283 charon->ike_sa_manager->checkin_and_destroy(
284 charon->ike_sa_manager, ike_sa);
285 }
286 }
287 list->destroy(list);
288 }
289
290 /**
291 * Implementation of ha_sync_segments_t.destroy.
292 */
293 static void destroy(private_ha_sync_segments_t *this)
294 {
295 this->lock->destroy(this->lock);
296 free(this);
297 }
298
299 /**
300 * See header
301 */
302 ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket,
303 u_int count, segment_mask_t active)
304 {
305 private_ha_sync_segments_t *this = malloc_thing(private_ha_sync_segments_t);
306
307 this->public.activate = (void(*)(ha_sync_segments_t*, u_int segment,bool))activate;
308 this->public.deactivate = (void(*)(ha_sync_segments_t*, u_int segment,bool))deactivate;
309 this->public.resync = (void(*)(ha_sync_segments_t*, u_int segment))resync;
310 this->public.destroy = (void(*)(ha_sync_segments_t*))destroy;
311
312 this->socket = socket;
313 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
314 this->initval = 0;
315 this->active = active;
316 this->segment_count = count;
317
318 return &this->public;
319 }
320