list assigned leases using "ipsec leases"
[strongswan.git] / src / charon / plugins / stroke / stroke_attribute.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 * $Id$
16 */
17
18 #include "stroke_attribute.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <utils/hashtable.h>
23 #include <utils/mutex.h>
24
25 #define POOL_LIMIT (sizeof(uintptr_t)*8)
26
27 typedef struct private_stroke_attribute_t private_stroke_attribute_t;
28
29 /**
30 * private data of stroke_attribute
31 */
32 struct private_stroke_attribute_t {
33
34 /**
35 * public functions
36 */
37 stroke_attribute_t public;
38
39 /**
40 * list of pools, contains pool_t
41 */
42 linked_list_t *pools;
43
44 /**
45 * mutex to lock access to pools
46 */
47 mutex_t *mutex;
48 };
49
50 typedef struct {
51 /** name of the pool */
52 char *name;
53 /** base address of the pool */
54 host_t *base;
55 /** size of the pool */
56 int size;
57 /** next unused address */
58 int unused;
59 /** hashtable [identity => offset], for online leases */
60 hashtable_t *online;
61 /** hashtable [identity => offset], for offline leases */
62 hashtable_t *offline;
63 /** hashtable [identity => identity], handles identity references */
64 hashtable_t *ids;
65 } pool_t;
66
67 /**
68 * hashtable hash function for identities
69 */
70 static u_int id_hash(identification_t *id)
71 {
72 return chunk_hash(id->get_encoding(id));
73 }
74
75 /**
76 * hashtable equals function for identities
77 */
78 static bool id_equals(identification_t *a, identification_t *b)
79 {
80 return a->equals(a, b);
81 }
82
83 /**
84 * destroy a pool_t
85 */
86 static void pool_destroy(pool_t *this)
87 {
88 enumerator_t *enumerator;
89 identification_t *id;
90
91 enumerator = this->ids->create_enumerator(this->ids);
92 while (enumerator->enumerate(enumerator, &id, NULL))
93 {
94 id->destroy(id);
95 }
96 enumerator->destroy(enumerator);
97 this->ids->destroy(this->ids);
98 this->online->destroy(this->online);
99 this->offline->destroy(this->offline);
100 DESTROY_IF(this->base);
101 free(this->name);
102 free(this);
103 }
104
105 /**
106 * find a pool by name
107 */
108 static pool_t *find_pool(private_stroke_attribute_t *this, char *name)
109 {
110 enumerator_t *enumerator;
111 pool_t *current, *found = NULL;
112
113 enumerator = this->pools->create_enumerator(this->pools);
114 while (enumerator->enumerate(enumerator, &current))
115 {
116 if (streq(name, current->name))
117 {
118 found = current;
119 break;
120 }
121 }
122 enumerator->destroy(enumerator);
123 return found;
124 }
125
126 /**
127 * convert an pool offset to an address
128 */
129 host_t* offset2host(pool_t *pool, int offset)
130 {
131 chunk_t addr;
132 host_t *host;
133 u_int32_t *pos;
134
135 offset--;
136 if (offset > pool->size)
137 {
138 return NULL;
139 }
140
141 addr = chunk_clone(pool->base->get_address(pool->base));
142 if (pool->base->get_family(pool->base) == AF_INET6)
143 {
144 pos = (u_int32_t*)(addr.ptr + 12);
145 }
146 else
147 {
148 pos = (u_int32_t*)addr.ptr;
149 }
150 *pos = htonl(offset + ntohl(*pos));
151 host = host_create_from_chunk(pool->base->get_family(pool->base), addr, 0);
152 free(addr.ptr);
153 return host;
154 }
155
156 /**
157 * convert a host to a pool offset
158 */
159 int host2offset(pool_t *pool, host_t *addr)
160 {
161 chunk_t host, base;
162 u_int32_t hosti, basei;
163
164 if (addr->get_family(addr) != pool->base->get_family(pool->base))
165 {
166 return -1;
167 }
168 host = addr->get_address(addr);
169 base = pool->base->get_address(pool->base);
170 if (addr->get_family(addr) == AF_INET6)
171 {
172 /* only look at last /32 block */
173 if (!memeq(host.ptr, base.ptr, 12))
174 {
175 return -1;
176 }
177 host = chunk_skip(host, 12);
178 base = chunk_skip(base, 12);
179 }
180 hosti = ntohl(*(u_int32_t*)(host.ptr));
181 basei = ntohl(*(u_int32_t*)(base.ptr));
182 if (hosti > basei + pool->size)
183 {
184 return -1;
185 }
186 return hosti - basei + 1;
187 }
188
189 /**
190 * Implementation of attribute_provider_t.acquire_address
191 */
192 static host_t* acquire_address(private_stroke_attribute_t *this,
193 char *name, identification_t *id,
194 auth_info_t *auth, host_t *requested)
195 {
196 pool_t *pool;
197 uintptr_t offset;
198 enumerator_t *enumerator;
199 identification_t *old_id;
200
201 this->mutex->lock(this->mutex);
202 pool = find_pool(this, name);
203 while (pool)
204 {
205 /* check for a valid offline lease, refresh */
206 offset = (uintptr_t)pool->offline->remove(pool->offline, id);
207 if (offset)
208 {
209 id = pool->ids->get(pool->ids, id);
210 if (id)
211 {
212 DBG1(DBG_CFG, "reassigning offline lease to %D", id);
213 pool->online->put(pool->online, id, (void*)offset);
214 break;
215 }
216 }
217
218 /* check for a valid online lease, reassign */
219 offset = (uintptr_t)pool->online->get(pool->online, id);
220 if (offset && offset == host2offset(pool, requested))
221 {
222 DBG1(DBG_CFG, "reassigning online lease to %D", id);
223 break;
224 }
225
226 if (pool->unused < pool->size)
227 {
228 /* assigning offset, starting by 1. Handling 0 in hashtable
229 * is difficult. */
230 offset = ++pool->unused;
231 id = id->clone(id);
232 pool->ids->put(pool->ids, id, id);
233 pool->online->put(pool->online, id, (void*)offset);
234 DBG1(DBG_CFG, "assigning new lease to %D", id);
235 break;
236 }
237 /* no more addresses, replace the first found offline lease */
238 enumerator = pool->offline->create_enumerator(pool->offline);
239 if (enumerator->enumerate(enumerator, &old_id, &offset))
240 {
241 offset = (uintptr_t)pool->offline->remove(pool->offline, old_id);
242 if (offset)
243 {
244 /* destroy reference to old ID */
245 old_id = pool->ids->remove(pool->ids, old_id);
246 DBG1(DBG_CFG, "reassigning existing offline lease of %D to %D",
247 old_id, id);
248 if (old_id)
249 {
250 old_id->destroy(old_id);
251 }
252 id = id->clone(id);
253 pool->ids->put(pool->ids, id, id);
254 pool->online->put(pool->online, id, (void*)offset);
255 enumerator->destroy(enumerator);
256 break;
257 }
258 }
259 enumerator->destroy(enumerator);
260
261 DBG1(DBG_CFG, "pool %s is full, unable to assign address", pool);
262 break;
263 }
264 this->mutex->unlock(this->mutex);
265 if (offset)
266 {
267 return offset2host(pool, offset);
268 }
269 return NULL;
270 }
271
272 /**
273 * Implementation of attribute_provider_t.release_address
274 */
275 static bool release_address(private_stroke_attribute_t *this,
276 char *name, host_t *address, identification_t *id)
277 {
278 pool_t *pool;
279 bool found = FALSE;
280 uintptr_t offset;
281
282 this->mutex->lock(this->mutex);
283 pool = find_pool(this, name);
284 if (pool)
285 {
286 if (pool->size != 0)
287 {
288 offset = (uintptr_t)pool->online->remove(pool->online, id);
289 if (offset)
290 {
291 id = pool->ids->get(pool->ids, id);
292 if (id)
293 {
294 DBG1(DBG_CFG, "lease %H of %D gone offline", address, id);
295 pool->offline->put(pool->offline, id, (void*)offset);
296 }
297 }
298 }
299 }
300 this->mutex->unlock(this->mutex);
301 return found;
302 }
303
304 /**
305 * Implementation of stroke_attribute_t.add_pool.
306 */
307 static void add_pool(private_stroke_attribute_t *this, stroke_msg_t *msg)
308 {
309 if (msg->add_conn.other.sourceip_size)
310 {
311 pool_t *pool;
312
313 pool = malloc_thing(pool_t);
314 pool->base = NULL;
315 pool->size = 0;
316 pool->unused = 0;
317 pool->name = strdup(msg->add_conn.name);
318 pool->online = hashtable_create((hashtable_hash_t)id_hash,
319 (hashtable_equals_t)id_equals, 16);
320 pool->offline = hashtable_create((hashtable_hash_t)id_hash,
321 (hashtable_equals_t)id_equals, 16);
322 pool->ids = hashtable_create((hashtable_hash_t)id_hash,
323 (hashtable_equals_t)id_equals, 16);
324
325 /* if %config, add an empty pool, otherwise */
326 if (msg->add_conn.other.sourceip)
327 {
328 u_int32_t bits;
329 int family;
330
331 DBG1(DBG_CFG, "adding virtual IP address pool '%s': %s/%d",
332 msg->add_conn.name, msg->add_conn.other.sourceip,
333 msg->add_conn.other.sourceip_size);
334
335 pool->base = host_create_from_string(msg->add_conn.other.sourceip, 0);
336 if (!pool->base)
337 {
338 pool_destroy(pool);
339 DBG1(DBG_CFG, "virtual IP address invalid, discarded");
340 return;
341 }
342 family = pool->base->get_family(pool->base);
343 bits = (family == AF_INET ? 32 : 128) - msg->add_conn.other.sourceip_size;
344 if (bits > POOL_LIMIT)
345 {
346 bits = POOL_LIMIT;
347 DBG1(DBG_CFG, "virtual IP pool to large, limiting to %s/%d",
348 msg->add_conn.other.sourceip,
349 (family == AF_INET ? 32 : 128) - bits);
350 }
351 pool->size = 1 << (bits);
352
353 if (pool->size > 2)
354 { /* do not use first and last addresses of a block */
355 pool->unused++;
356 pool->size--;
357 }
358 }
359 this->mutex->lock(this->mutex);
360 this->pools->insert_last(this->pools, pool);
361 this->mutex->unlock(this->mutex);
362 }
363 }
364
365 /**
366 * Implementation of stroke_attribute_t.del_pool.
367 */
368 static void del_pool(private_stroke_attribute_t *this, stroke_msg_t *msg)
369 {
370 enumerator_t *enumerator;
371 pool_t *pool;
372
373 this->mutex->lock(this->mutex);
374 enumerator = this->pools->create_enumerator(this->pools);
375 while (enumerator->enumerate(enumerator, &pool))
376 {
377 if (streq(msg->del_conn.name, pool->name))
378 {
379 this->pools->remove_at(this->pools, enumerator);
380 pool_destroy(pool);
381 break;
382 }
383 }
384 enumerator->destroy(enumerator);
385 this->mutex->unlock(this->mutex);
386 }
387
388 /**
389 * Pool enumerator filter function, converts pool_t to name, size, ...
390 */
391 static bool pool_filter(void *mutex, pool_t **poolp, char **name,
392 void *d1, u_int *size, void *d2, u_int *online,
393 void *d3, u_int *offline)
394 {
395 pool_t *pool = *poolp;
396
397 *name = pool->name;
398 *size = pool->size;
399 *online = pool->online->get_count(pool->online);
400 *offline = pool->offline->get_count(pool->offline);
401 return TRUE;
402 }
403
404 /**
405 * Implementation of stroke_attribute_t.create_pool_enumerator
406 */
407 static enumerator_t* create_pool_enumerator(private_stroke_attribute_t *this)
408 {
409 this->mutex->lock(this->mutex);
410 return enumerator_create_filter(this->pools->create_enumerator(this->pools),
411 (void*)pool_filter,
412 this->mutex, (void*)this->mutex->unlock);
413 }
414
415 /**
416 * lease enumerator
417 */
418 typedef struct {
419 /** implemented enumerator interface */
420 enumerator_t public;
421 /** inner hash-table enumerator */
422 enumerator_t *inner;
423 /** enumerated pool */
424 pool_t *pool;
425 /** mutex to unlock on destruction */
426 mutex_t *mutex;
427 /** currently enumerated lease address */
428 host_t *current;
429 } lease_enumerator_t;
430
431 /**
432 * Implementation of lease_enumerator_t.enumerate
433 */
434 static bool lease_enumerate(lease_enumerator_t *this, identification_t **id_out,
435 host_t **addr_out, bool *online)
436 {
437 identification_t *id;
438 uintptr_t offset;
439
440 DESTROY_IF(this->current);
441 this->current = NULL;
442
443 if (this->inner->enumerate(this->inner, &id, NULL))
444 {
445 offset = (uintptr_t)this->pool->online->get(this->pool->online, id);
446 if (offset)
447 {
448 *id_out = id;
449 *addr_out = this->current = offset2host(this->pool, offset);
450 *online = TRUE;
451 return TRUE;
452 }
453 offset = (uintptr_t)this->pool->offline->get(this->pool->offline, id);
454 if (offset)
455 {
456 *id_out = id;
457 *addr_out = this->current = offset2host(this->pool, offset);
458 *online = FALSE;
459 return TRUE;
460 }
461 }
462 return FALSE;
463 }
464
465 /**
466 * Implementation of lease_enumerator_t.destroy
467 */
468 static void lease_enumerator_destroy(lease_enumerator_t *this)
469 {
470 DESTROY_IF(this->current);
471 this->inner->destroy(this->inner);
472 this->mutex->unlock(this->mutex);
473 free(this);
474 }
475
476 /**
477 * Implementation of stroke_attribute_t.create_lease_enumerator
478 */
479 static enumerator_t* create_lease_enumerator(private_stroke_attribute_t *this,
480 char *pool)
481 {
482 lease_enumerator_t *enumerator;
483
484 this->mutex->lock(this->mutex);
485 enumerator = malloc_thing(lease_enumerator_t);
486 enumerator->pool = find_pool(this, pool);
487 if (!enumerator->pool)
488 {
489 this->mutex->unlock(this->mutex);
490 free(enumerator);
491 return NULL;
492 }
493 enumerator->public.enumerate = (void*)lease_enumerate;
494 enumerator->public.destroy = (void*)lease_enumerator_destroy;
495 enumerator->inner = enumerator->pool->ids->create_enumerator(enumerator->pool->ids);
496 enumerator->mutex = this->mutex;
497 enumerator->current = NULL;
498 return &enumerator->public;
499 }
500
501 /**
502 * Implementation of stroke_attribute_t.destroy
503 */
504 static void destroy(private_stroke_attribute_t *this)
505 {
506 this->mutex->destroy(this->mutex);
507 this->pools->destroy_function(this->pools, (void*)pool_destroy);
508 free(this);
509 }
510
511 /*
512 * see header file
513 */
514 stroke_attribute_t *stroke_attribute_create()
515 {
516 private_stroke_attribute_t *this = malloc_thing(private_stroke_attribute_t);
517
518 this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *,auth_info_t *, host_t *))acquire_address;
519 this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))release_address;
520 this->public.add_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))add_pool;
521 this->public.del_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))del_pool;
522 this->public.create_pool_enumerator = (enumerator_t*(*)(stroke_attribute_t*))create_pool_enumerator;
523 this->public.create_lease_enumerator = (enumerator_t*(*)(stroke_attribute_t*, char *pool))create_lease_enumerator;
524 this->public.destroy = (void(*)(stroke_attribute_t*))destroy;
525
526 this->pools = linked_list_create();
527 this->mutex = mutex_create(MUTEX_RECURSIVE);
528
529 return &this->public;
530 }
531