5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2012 BMW Car IT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/errno.h>
33 #include <sys/socket.h>
42 unsigned int use_count;
43 struct connman_ippool *pool;
46 struct connman_ippool {
47 unsigned int refcount;
49 struct address_info *info;
57 ippool_collision_cb_t collision_cb;
61 GSList *allocated_blocks;
62 GHashTable *pool_hash;
64 static uint32_t last_block;
65 static uint32_t block_16_bits;
66 static uint32_t block_20_bits;
67 static uint32_t block_24_bits;
68 static uint32_t subnet_mask_24;
70 struct connman_ippool *
71 __connman_ippool_ref_debug(struct connman_ippool *pool,
72 const char *file, int line, const char *caller)
74 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
77 __sync_fetch_and_add(&pool->refcount, 1);
82 void __connman_ippool_unref_debug(struct connman_ippool *pool,
83 const char *file, int line, const char *caller)
88 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
91 if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
94 g_hash_table_remove(pool_hash, pool);
97 static char *get_ip(uint32_t ip)
101 addr.s_addr = htonl(ip);
103 return g_strdup(inet_ntoa(addr));
106 static uint32_t next_block(uint32_t block)
111 * Return the next IP block within the private IP range
113 * 16-bit block 192.168.0.0 – 192.168.255.255
114 * 20-bit block 172.16.0.0 – 172.31.255.255
115 * 24-bit block 10.0.0.0 – 10.255.255.255
118 next = (block & 0x0000ff00) >> 8;
122 if ((block & 0xffff0000) == block_16_bits) {
124 * Reached the end of the 16 bit block, switch
125 * to the 20-bit block.
127 return block_20_bits;
130 if ((block & 0xffff0000) >= block_20_bits) {
131 next = (block & 0x00ff0000) >> 16;
132 if (next >= 16 && next < 32)
137 * Reached the end of the 20 bit
138 * block, switch to the 24-bit block.
140 return block_24_bits;
143 return (block & 0xff000000) |
144 ((next << 16) & 0x00ff0000);
147 if ((block & 0xff000000) == block_24_bits) {
148 next = (block & 0x00ff0000) >> 16;
154 * Reached the end of the 24 bit
155 * block, switch to the 16-bit block.
157 return block_16_bits;
160 return (block & 0xff000000) |
161 ((next << 16) & 0x00ff0000);
165 return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
168 static uint32_t get_free_block(unsigned int size)
170 struct address_info *info;
173 connman_bool_t collision;
176 * Instead starting always from the 16 bit block, we start
177 * from the last assigned block. This is a simple optimimazion
178 * for the case where a lot of blocks have been assigned, e.g.
179 * the first half of the private IP pool is in use and a new
180 * we need to find a new block.
182 * To only thing we have to make sure is that we terminated if
183 * there is no block left.
186 block = block_16_bits;
188 block = next_block(last_block);
192 for (list = allocated_blocks; list != NULL; list = list->next) {
195 if (info->start <= block && block <= info->end) {
201 if (collision == FALSE)
204 block = next_block(block);
205 } while (block != last_block);
210 static struct address_info *lookup_info(int index, uint32_t start)
214 for (list = allocated_blocks; list != NULL; list = list->next) {
215 struct address_info *info = list->data;
217 if (info->index == index && info->start == start)
224 static connman_bool_t is_private_address(uint32_t address)
228 if ((address & 0xff000000) == block_24_bits)
231 if ((address & 0xffff0000) == block_20_bits) {
232 val = (address & 0x00ff0000) >> 16;
234 if (val < 16 || val > 31)
240 if ((address & 0xffffff00) == block_16_bits)
246 void __connman_ippool_newaddr(int index, const char *address,
247 unsigned char prefixlen)
249 struct address_info *info, *it;
251 uint32_t start, end, mask;
254 if (inet_aton(address, &inp) == 0)
257 start = ntohl(inp.s_addr);
258 if (is_private_address(start) == FALSE)
264 mask = ~(0xffffffff >> prefixlen);
266 start = start & mask;
269 info = lookup_info(index, start);
273 info = g_try_new0(struct address_info, 1);
281 allocated_blocks = g_slist_prepend(allocated_blocks, info);
284 info->use_count = info->use_count + 1;
286 if (info->use_count > 1 || info->pool != NULL) {
288 * We need only to check for the first IP in a block for
294 for (list = allocated_blocks; list != NULL; list = list->next) {
300 if (!(it->start <= info->start || info->start <= it->end))
303 if (it->pool != NULL && it->pool->collision_cb != NULL)
304 it->pool->collision_cb(it->pool, it->pool->user_data);
310 void __connman_ippool_deladdr(int index, const char *address,
311 unsigned char prefixlen)
313 struct address_info *info;
315 uint32_t start, mask;
317 if (inet_aton(address, &inp) == 0)
320 start = ntohl(inp.s_addr);
321 if (is_private_address(start) == FALSE)
324 mask = ~(0xffffffff >> prefixlen);
325 start = start & mask;
327 info = lookup_info(index, start);
329 /* In theory this should never happen */
330 connman_error("Inconsistent IP pool management (start not found)");
334 info->use_count = info->use_count - 1;
335 if (info->pool != NULL)
338 if (info->use_count > 0)
341 allocated_blocks = g_slist_remove(allocated_blocks, info);
344 struct connman_ippool *__connman_ippool_create(int index,
347 ippool_collision_cb_t collision_cb,
350 struct connman_ippool *pool;
351 struct address_info *info;
357 * The range is at max 255 and we don't support overlapping
360 if (start + range > 254) {
361 connman_error("IP pool does not support pool size larger than 254");
365 block = get_free_block(start + range);
367 connman_warn("Could not find a free IP block");
371 pool = g_try_new0(struct connman_ippool, 1);
375 info = g_try_new0(struct address_info, 1);
385 info->end = block + range;
389 pool->collision_cb = collision_cb;
390 pool->user_data = user_data;
397 pool->gateway = get_ip(info->start + 1);
398 pool->broadcast = get_ip(info->start + 255);
399 pool->subnet_mask = get_ip(subnet_mask_24);
400 pool->start_ip = get_ip(block + start);
401 pool->end_ip = get_ip(block + start + range);
403 allocated_blocks = g_slist_prepend(allocated_blocks, info);
404 g_hash_table_insert(pool_hash, pool, pool);
409 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
411 return pool->gateway;
414 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
416 return pool->broadcast;
419 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
421 return pool->start_ip;
424 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
429 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
431 return pool->subnet_mask;
434 static void pool_free(gpointer data)
436 struct connman_ippool *pool = data;
438 if (pool->info != NULL) {
439 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
443 g_free(pool->gateway);
444 g_free(pool->broadcast);
445 g_free(pool->start_ip);
446 g_free(pool->end_ip);
447 g_free(pool->subnet_mask);
452 int __connman_ippool_init(void)
456 block_16_bits = ntohl(inet_addr("192.168.0.0"));
457 block_20_bits = ntohl(inet_addr("172.16.0.0"));
458 block_24_bits = ntohl(inet_addr("10.0.0.0"));
459 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
461 pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
467 void __connman_ippool_cleanup(void)
471 g_hash_table_destroy(pool_hash);
474 g_slist_free(allocated_blocks);