5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
6 * Copyright (C) 2012-2014 BMW Car IT GmbH.
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
31 #include <sys/errno.h>
32 #include <sys/socket.h>
41 unsigned int use_count;
42 struct connman_ippool *pool;
45 struct connman_ippool {
46 unsigned int refcount;
48 struct address_info *info;
56 ippool_collision_cb_t collision_cb;
60 GSList *allocated_blocks;
61 GHashTable *pool_hash;
63 static uint32_t last_block;
64 static uint32_t block_16_bits;
65 static uint32_t block_20_bits;
66 static uint32_t block_24_bits;
67 static uint32_t subnet_mask_24;
69 struct connman_ippool *
70 __connman_ippool_ref_debug(struct connman_ippool *pool,
71 const char *file, int line, const char *caller)
73 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
76 __sync_fetch_and_add(&pool->refcount, 1);
81 void __connman_ippool_unref_debug(struct connman_ippool *pool,
82 const char *file, int line, const char *caller)
87 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
90 if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
93 g_hash_table_remove(pool_hash, pool);
96 static char *get_ip(uint32_t ip)
100 addr.s_addr = htonl(ip);
102 return g_strdup(inet_ntoa(addr));
105 static uint32_t next_block(uint32_t block)
110 * Return the next IP block within the private IP range
112 * 16-bit block 192.168.0.0 – 192.168.255.255
113 * 20-bit block 172.16.0.0 – 172.31.255.255
114 * 24-bit block 10.0.0.0 – 10.255.255.255
117 next = (block & 0x0000ff00) >> 8;
121 if ((block & 0xffff0000) == block_16_bits) {
123 * Reached the end of the 16 bit block, switch
124 * to the 20-bit block.
126 return block_20_bits;
129 if ((block & 0xffff0000) >= block_20_bits) {
130 next = (block & 0x00ff0000) >> 16;
131 if (next >= 16 && next < 32)
136 * Reached the end of the 20 bit
137 * block, switch to the 24-bit block.
139 return block_24_bits;
142 return (block & 0xff000000) |
143 ((next << 16) & 0x00ff0000);
146 if ((block & 0xff000000) == block_24_bits) {
147 next = (block & 0x00ff0000) >> 16;
153 * Reached the end of the 24 bit
154 * block, switch to the 16-bit block.
156 return block_16_bits;
159 return (block & 0xff000000) |
160 ((next << 16) & 0x00ff0000);
164 return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
167 static uint32_t get_free_block(unsigned int size)
169 struct address_info *info;
175 * Instead starting always from the 16 bit block, we start
176 * from the last assigned block. This is a simple optimimazion
177 * for the case where a lot of blocks have been assigned, e.g.
178 * the first half of the private IP pool is in use and a new
179 * we need to find a new block.
181 * To only thing we have to make sure is that we terminated if
182 * there is no block left.
185 block = block_16_bits;
187 block = next_block(last_block);
191 for (list = allocated_blocks; list; list = list->next) {
194 if (info->start <= block && block <= info->end) {
203 block = next_block(block);
204 } while (block != last_block);
209 static struct address_info *lookup_info(int index, uint32_t start)
213 for (list = allocated_blocks; list; list = list->next) {
214 struct address_info *info = list->data;
216 if (info->index == index && info->start == start)
223 static bool is_private_address(uint32_t address)
227 a = (address & 0xff000000) >> 24;
228 b = (address & 0x00ff0000) >> 16;
230 if (a == 10 || (a == 192 && b == 168) ||
231 (a == 172 && (b >= 16 && b <= 31)))
237 void __connman_ippool_newaddr(int index, const char *address,
238 unsigned char prefixlen)
240 struct address_info *info, *it;
242 uint32_t start, end, mask;
245 if (inet_aton(address, &inp) == 0)
248 start = ntohl(inp.s_addr);
249 if (!is_private_address(start))
255 mask = ~(0xffffffff >> prefixlen);
257 start = start & mask;
260 info = lookup_info(index, start);
264 info = g_try_new0(struct address_info, 1);
272 allocated_blocks = g_slist_prepend(allocated_blocks, info);
275 info->use_count = info->use_count + 1;
277 if (info->use_count > 1 || info->pool) {
279 * We need only to check for the first IP in a block for
285 for (list = allocated_blocks; list; list = list->next) {
291 if (!(info->start >= it->start && info->start <= it->end))
294 if (it->pool && it->pool->collision_cb)
295 it->pool->collision_cb(it->pool, it->pool->user_data);
301 void __connman_ippool_deladdr(int index, const char *address,
302 unsigned char prefixlen)
304 struct address_info *info;
306 uint32_t start, mask;
308 if (inet_aton(address, &inp) == 0)
311 start = ntohl(inp.s_addr);
312 if (!is_private_address(start))
315 mask = ~(0xffffffff >> prefixlen);
316 start = start & mask;
318 info = lookup_info(index, start);
320 /* In theory this should never happen */
321 connman_error("Inconsistent IP pool management (start not found)");
325 info->use_count = info->use_count - 1;
329 if (info->use_count > 0)
332 allocated_blocks = g_slist_remove(allocated_blocks, info);
336 struct connman_ippool *__connman_ippool_create(int index,
339 ippool_collision_cb_t collision_cb,
342 struct connman_ippool *pool;
343 struct address_info *info;
349 * The range is at max 255 and we don't support overlapping
352 if (start + range > 254) {
353 connman_error("IP pool does not support pool size larger than 254");
357 block = get_free_block(start + range);
359 connman_warn("Could not find a free IP block");
363 pool = g_try_new0(struct connman_ippool, 1);
367 info = g_try_new0(struct address_info, 1);
377 info->end = block + range;
381 pool->collision_cb = collision_cb;
382 pool->user_data = user_data;
389 pool->gateway = get_ip(info->start + 1);
390 pool->broadcast = get_ip(info->start + 255);
391 pool->subnet_mask = get_ip(subnet_mask_24);
392 pool->start_ip = get_ip(block + start);
393 pool->end_ip = get_ip(block + start + range);
395 allocated_blocks = g_slist_prepend(allocated_blocks, info);
396 g_hash_table_insert(pool_hash, pool, pool);
401 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
403 return pool->gateway;
406 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
408 return pool->broadcast;
411 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
413 return pool->start_ip;
416 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
421 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
423 return pool->subnet_mask;
426 static void pool_free(gpointer data)
428 struct connman_ippool *pool = data;
431 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
435 g_free(pool->gateway);
436 g_free(pool->broadcast);
437 g_free(pool->start_ip);
438 g_free(pool->end_ip);
439 g_free(pool->subnet_mask);
444 int __connman_ippool_init(void)
448 block_16_bits = ntohl(inet_addr("192.168.0.0"));
449 block_20_bits = ntohl(inet_addr("172.16.0.0"));
450 block_24_bits = ntohl(inet_addr("10.0.0.0"));
451 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
453 pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
459 void __connman_ippool_cleanup(void)
463 g_hash_table_destroy(pool_hash);
466 g_slist_free_full(allocated_blocks, g_free);
468 allocated_blocks = NULL;