2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "ha_sync_segments.h"
18 #include <utils/mutex.h>
19 #include <utils/linked_list.h>
21 typedef u_int32_t u32
;
24 #include <linux/jhash.h>
26 #define MAX_SEGMENTS 16
28 typedef struct private_ha_sync_segments_t private_ha_sync_segments_t
;
31 * Private data of an ha_sync_segments_t object.
33 struct private_ha_sync_segments_t
{
36 * Public ha_sync_segments_t interface.
38 ha_sync_segments_t
public;
41 * communication socket
43 ha_sync_socket_t
*socket
;
46 * read/write lock for segment manipulation
51 * Init value for jhash
56 * Total number of ClusterIP segments
61 * mask of active segments
67 * Check if a host address is in the CLUSTERIP segment
69 static bool in_segment(private_ha_sync_segments_t
*this,
70 host_t
*host
, u_int segment
)
72 if (host
->get_family(host
) == AF_INET
)
77 addr
= *(u_int32_t
*)host
->get_address(host
).ptr
;
78 hash
= jhash_1word(ntohl(addr
), this->initval
);
80 if ((((u_int64_t
)hash
* this->segment_count
) >> 32) + 1 == segment
)
89 * Log currently active segments
91 static void log_segments(private_ha_sync_segments_t
*this, bool activated
,
94 char buf
[64] = "none", *pos
= buf
;
98 for (i
= 0; i
< this->segment_count
; i
++)
100 if (this->active
& 0x01 << i
)
108 pos
+= snprintf(pos
, buf
+ sizeof(buf
) - pos
, ",");
110 pos
+= snprintf(pos
, buf
+ sizeof(buf
) - pos
, "%d", i
+1);
113 DBG1(DBG_CFG
, "HA sync segment %d %sactivated, now active: %s",
114 segment
, activated ?
"" : "de", buf
);
118 * Get the bit of the segment in the bitmask
120 static inline u_int16_t
bit_of(u_int segment
)
122 return 0x01 << (segment
- 1);
126 * Enable/Disable an an IKE_SA.
128 static void enable_disable(private_ha_sync_segments_t
*this, u_int segment
,
129 ike_sa_state_t old
, ike_sa_state_t
new, bool enable
)
132 enumerator_t
*enumerator
;
135 this->lock
->write_lock(this->lock
);
137 if (segment
== 0 || segment
<= this->segment_count
)
140 { /* loop once for single segment ... */
144 { /* or segment_count times for all segments */
145 limit
= this->segment_count
;
147 for (i
= segment
; i
< limit
; i
++)
151 this->active
|= bit_of(i
);
155 this->active
&= ~bit_of(i
);
158 enumerator
= charon
->ike_sa_manager
->create_enumerator(charon
->ike_sa_manager
);
159 while (enumerator
->enumerate(enumerator
, &ike_sa
))
161 if (ike_sa
->get_state(ike_sa
) == old
)
163 for (i
= segment
; i
< limit
; i
++)
165 if (in_segment(this, ike_sa
->get_other_host(ike_sa
), i
))
167 ike_sa
->set_state(ike_sa
, new);
172 enumerator
->destroy(enumerator
);
174 log_segments(this, enable
, segment
);
177 this->lock
->unlock(this->lock
);
181 * Implementation of ha_sync_segments_t.activate
183 static void activate(private_ha_sync_segments_t
*this, u_int segment
,
186 ha_sync_message_t
*message
;
188 enable_disable(this, segment
, IKE_PASSIVE
, IKE_ESTABLISHED
, TRUE
);
192 message
= ha_sync_message_create(HA_SYNC_SEGMENT_TAKE
);
193 message
->add_attribute(message
, HA_SYNC_SEGMENT
, segment
);
194 this->socket
->push(this->socket
, message
);
199 * Implementation of ha_sync_segments_t.deactivate
201 static void deactivate(private_ha_sync_segments_t
*this, u_int segment
,
204 ha_sync_message_t
*message
;
206 enable_disable(this, segment
, IKE_ESTABLISHED
, IKE_PASSIVE
, FALSE
);
210 message
= ha_sync_message_create(HA_SYNC_SEGMENT_DROP
);
211 message
->add_attribute(message
, HA_SYNC_SEGMENT
, segment
);
212 this->socket
->push(this->socket
, message
);
217 * Rekey all children of an IKE_SA
219 static status_t
rekey_children(ike_sa_t
*ike_sa
)
221 iterator_t
*iterator
;
222 child_sa_t
*child_sa
;
223 status_t status
= SUCCESS
;
225 iterator
= ike_sa
->create_child_sa_iterator(ike_sa
);
226 while (iterator
->iterate(iterator
, (void**)&child_sa
))
228 DBG1(DBG_CFG
, "resyncing CHILD_SA");
229 status
= ike_sa
->rekey_child_sa(ike_sa
, child_sa
->get_protocol(child_sa
),
230 child_sa
->get_spi(child_sa
, TRUE
));
231 if (status
== DESTROY_ME
)
236 iterator
->destroy(iterator
);
241 * Implementation of ha_sync_segments_t.resync
243 static void resync(private_ha_sync_segments_t
*this, u_int segment
)
246 enumerator_t
*enumerator
;
249 u_int16_t mask
= bit_of(segment
);
251 list
= linked_list_create();
252 this->lock
->read_lock(this->lock
);
254 if (segment
> 0 && segment
<= this->segment_count
&& (this->active
& mask
))
256 this->active
&= ~mask
;
258 DBG1(DBG_CFG
, "resyncing HA sync segment %d", segment
);
260 /* we do the actual rekeying in a seperate loop to avoid rekeying
262 enumerator
= charon
->ike_sa_manager
->create_enumerator(
263 charon
->ike_sa_manager
);
264 while (enumerator
->enumerate(enumerator
, &ike_sa
))
266 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
&&
267 in_segment(this, ike_sa
->get_other_host(ike_sa
), segment
))
269 id
= ike_sa
->get_id(ike_sa
);
270 list
->insert_last(list
, id
->clone(id
));
273 enumerator
->destroy(enumerator
);
275 this->lock
->unlock(this->lock
);
277 while (list
->remove_last(list
, (void**)&id
) == SUCCESS
)
279 ike_sa
= charon
->ike_sa_manager
->checkout(charon
->ike_sa_manager
, id
);
283 DBG1(DBG_CFG
, "resyncing IKE_SA");
284 if (ike_sa
->rekey(ike_sa
) != DESTROY_ME
)
286 if (rekey_children(ike_sa
) != DESTROY_ME
)
288 charon
->ike_sa_manager
->checkin(
289 charon
->ike_sa_manager
, ike_sa
);
293 charon
->ike_sa_manager
->checkin_and_destroy(
294 charon
->ike_sa_manager
, ike_sa
);
301 * Implementation of ha_sync_segments_t.destroy.
303 static void destroy(private_ha_sync_segments_t
*this)
305 this->lock
->destroy(this->lock
);
312 ha_sync_segments_t
*ha_sync_segments_create(ha_sync_socket_t
*socket
)
314 private_ha_sync_segments_t
*this = malloc_thing(private_ha_sync_segments_t
);
315 enumerator_t
*enumerator
;
319 this->public.activate
= (void(*)(ha_sync_segments_t
*, u_int segment
,bool))activate
;
320 this->public.deactivate
= (void(*)(ha_sync_segments_t
*, u_int segment
,bool))deactivate
;
321 this->public.resync
= (void(*)(ha_sync_segments_t
*, u_int segment
))resync
;
322 this->public.destroy
= (void(*)(ha_sync_segments_t
*))destroy
;
324 this->socket
= socket
;
325 this->lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
328 this->segment_count
= lib
->settings
->get_int(lib
->settings
,
329 "charon.plugins.ha_sync.segment_count", 1);
330 this->segment_count
= min(this->segment_count
, MAX_SEGMENTS
);
331 str
= lib
->settings
->get_str(lib
->settings
,
332 "charon.plugins.ha_sync.active_segments", "1");
333 enumerator
= enumerator_create_token(str
, ",", " ");
334 while (enumerator
->enumerate(enumerator
, &str
))
337 if (segment
> 0 && segment
< MAX_SEGMENTS
)
339 this->active
|= bit_of(segment
);
342 enumerator
->destroy(enumerator
);
344 return &this->public;