82a5bce23bdfe80e6b4a58d3f1b4f4b82078cc77
[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 = 0;
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 if (requested->get_family(requested) !=
206 pool->base->get_family(pool->base))
207 {
208 DBG1(DBG_CFG, "IP pool address family mismatch");
209 break;
210 }
211
212 /* handle %config case by mirroring requested address */
213 if (pool->size == 0)
214 {
215 this->mutex->unlock(this->mutex);
216 return requested->clone(requested);
217 }
218
219 /* check for a valid offline lease, refresh */
220 offset = (uintptr_t)pool->offline->remove(pool->offline, id);
221 if (offset)
222 {
223 id = pool->ids->get(pool->ids, id);
224 if (id)
225 {
226 DBG1(DBG_CFG, "reassigning offline lease to %D", id);
227 pool->online->put(pool->online, id, (void*)offset);
228 break;
229 }
230 }
231
232 /* check for a valid online lease, reassign */
233 offset = (uintptr_t)pool->online->get(pool->online, id);
234 if (offset && offset == host2offset(pool, requested))
235 {
236 DBG1(DBG_CFG, "reassigning online lease to %D", id);
237 break;
238 }
239
240 if (pool->unused < pool->size)
241 {
242 /* assigning offset, starting by 1. Handling 0 in hashtable
243 * is difficult. */
244 offset = ++pool->unused;
245 id = id->clone(id);
246 pool->ids->put(pool->ids, id, id);
247 pool->online->put(pool->online, id, (void*)offset);
248 DBG1(DBG_CFG, "assigning new lease to %D", id);
249 break;
250 }
251 /* no more addresses, replace the first found offline lease */
252 enumerator = pool->offline->create_enumerator(pool->offline);
253 if (enumerator->enumerate(enumerator, &old_id, &offset))
254 {
255 offset = (uintptr_t)pool->offline->remove(pool->offline, old_id);
256 if (offset)
257 {
258 /* destroy reference to old ID */
259 old_id = pool->ids->remove(pool->ids, old_id);
260 DBG1(DBG_CFG, "reassigning existing offline lease of %D to %D",
261 old_id, id);
262 if (old_id)
263 {
264 old_id->destroy(old_id);
265 }
266 id = id->clone(id);
267 pool->ids->put(pool->ids, id, id);
268 pool->online->put(pool->online, id, (void*)offset);
269 enumerator->destroy(enumerator);
270 break;
271 }
272 }
273 enumerator->destroy(enumerator);
274
275 DBG1(DBG_CFG, "pool '%s' is full, unable to assign address", pool->name);
276 break;
277 }
278 this->mutex->unlock(this->mutex);
279 if (offset)
280 {
281 return offset2host(pool, offset);
282 }
283 return NULL;
284 }
285
286 /**
287 * Implementation of attribute_provider_t.release_address
288 */
289 static bool release_address(private_stroke_attribute_t *this,
290 char *name, host_t *address, identification_t *id)
291 {
292 pool_t *pool;
293 bool found = FALSE;
294 uintptr_t offset;
295
296 this->mutex->lock(this->mutex);
297 pool = find_pool(this, name);
298 if (pool)
299 {
300 if (pool->size != 0)
301 {
302 offset = (uintptr_t)pool->online->remove(pool->online, id);
303 if (offset)
304 {
305 id = pool->ids->get(pool->ids, id);
306 if (id)
307 {
308 DBG1(DBG_CFG, "lease %H of %D went offline", address, id);
309 pool->offline->put(pool->offline, id, (void*)offset);
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_size)
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_size);
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_size;
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 *,auth_info_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.add_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))add_pool;
535 this->public.del_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))del_pool;
536 this->public.create_pool_enumerator = (enumerator_t*(*)(stroke_attribute_t*))create_pool_enumerator;
537 this->public.create_lease_enumerator = (enumerator_t*(*)(stroke_attribute_t*, char *pool))create_lease_enumerator;
538 this->public.destroy = (void(*)(stroke_attribute_t*))destroy;
539
540 this->pools = linked_list_create();
541 this->mutex = mutex_create(MUTEX_RECURSIVE);
542
543 return &this->public;
544 }
545