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)
261 mask = ~(0xffffffff >> prefixlen);
262 start = start & mask;
265 info = lookup_info(index, start);
269 info = g_try_new0(struct address_info, 1);
277 allocated_blocks = g_slist_prepend(allocated_blocks, info);
280 info->use_count = info->use_count + 1;
282 if (info->use_count > 1 || info->pool != NULL) {
284 * We need only to check for the first IP in a block for
290 for (list = allocated_blocks; list != NULL; list = list->next) {
296 if (!(it->start <= info->start || info->start <= it->end))
299 if (it->pool != NULL && it->pool->collision_cb != NULL)
300 it->pool->collision_cb(it->pool, it->pool->user_data);
306 void __connman_ippool_deladdr(int index, const char *address,
307 unsigned char prefixlen)
309 struct address_info *info;
311 uint32_t start, mask;
313 if (inet_aton(address, &inp) == 0)
316 start = ntohl(inp.s_addr);
317 if (is_private_address(start) == FALSE)
320 mask = ~(0xffffffff >> prefixlen);
321 start = start & mask;
323 info = lookup_info(index, start);
325 /* In theory this should never happen */
326 connman_error("Inconsistent IP pool management (start not found)");
330 info->use_count = info->use_count - 1;
331 if (info->pool != NULL)
334 if (info->use_count > 0)
337 allocated_blocks = g_slist_remove(allocated_blocks, info);
340 struct connman_ippool *__connman_ippool_create(int index,
343 ippool_collision_cb_t collision_cb,
346 struct connman_ippool *pool;
347 struct address_info *info;
353 * The range is at max 255 and we don't support overlapping
356 if (start + range > 254) {
357 connman_error("IP pool does not support pool size larger than 254");
361 block = get_free_block(start + range);
363 connman_warn("Could not find a free IP block");
367 pool = g_try_new0(struct connman_ippool, 1);
371 info = g_try_new0(struct address_info, 1);
381 info->end = block + range;
385 pool->collision_cb = collision_cb;
386 pool->user_data = user_data;
393 pool->gateway = get_ip(info->start + 1);
394 pool->broadcast = get_ip(info->start + 255);
395 pool->subnet_mask = get_ip(subnet_mask_24);
396 pool->start_ip = get_ip(block + start);
397 pool->end_ip = get_ip(block + start + range);
399 allocated_blocks = g_slist_prepend(allocated_blocks, info);
400 g_hash_table_insert(pool_hash, pool, pool);
405 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
407 return pool->gateway;
410 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
412 return pool->broadcast;
415 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
417 return pool->start_ip;
420 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
425 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
427 return pool->subnet_mask;
430 static void pool_free(gpointer data)
432 struct connman_ippool *pool = data;
434 if (pool->info != NULL) {
435 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
439 g_free(pool->gateway);
440 g_free(pool->broadcast);
441 g_free(pool->start_ip);
442 g_free(pool->end_ip);
443 g_free(pool->subnet_mask);
448 int __connman_ippool_init(void)
452 block_16_bits = ntohl(inet_addr("192.168.0.0"));
453 block_20_bits = ntohl(inet_addr("172.16.0.0"));
454 block_24_bits = ntohl(inet_addr("10.0.0.0"));
455 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
457 pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
463 void __connman_ippool_cleanup(void)
467 g_hash_table_destroy(pool_hash);
470 g_slist_free(allocated_blocks);