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