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/socket.h>
40 unsigned int use_count;
41 struct connman_ippool *pool;
44 struct connman_ippool {
45 struct address_info *info;
53 ippool_collision_cb_t collision_cb;
57 GSList *allocated_blocks;
59 static uint32_t last_block;
60 static uint32_t block_16_bits;
61 static uint32_t block_20_bits;
62 static uint32_t block_24_bits;
63 static uint32_t subnet_mask_24;
65 void __connman_ippool_free(struct connman_ippool *pool)
71 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
75 g_free(pool->gateway);
76 g_free(pool->broadcast);
77 g_free(pool->start_ip);
79 g_free(pool->subnet_mask);
84 static char *get_ip(uint32_t ip)
88 addr.s_addr = htonl(ip);
90 return g_strdup(inet_ntoa(addr));
93 static uint32_t next_block(uint32_t block)
98 * Return the next IP block within the private IP range
100 * 16-bit block 192.168.0.0 – 192.168.255.255
101 * 20-bit block 172.16.0.0 – 172.31.255.255
102 * 24-bit block 10.0.0.0 – 10.255.255.255
105 next = (block & 0x0000ff00) >> 8;
109 if ((block & 0xffff0000) == block_16_bits) {
111 * Reached the end of the 16 bit block, switch
112 * to the 20-bit block.
114 return block_20_bits;
117 if ((block & 0xffff0000) >= block_20_bits) {
118 next = (block & 0x00ff0000) >> 16;
119 if (next >= 16 && next < 32)
124 * Reached the end of the 20 bit
125 * block, switch to the 24-bit block.
127 return block_24_bits;
130 return (block & 0xff000000) |
131 ((next << 16) & 0x00ff0000);
134 if ((block & 0xff000000) == block_24_bits) {
135 next = (block & 0x00ff0000) >> 16;
141 * Reached the end of the 24 bit
142 * block, switch to the 16-bit block.
144 return block_16_bits;
147 return (block & 0xff000000) |
148 ((next << 16) & 0x00ff0000);
152 return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
155 static uint32_t get_free_block(unsigned int size)
157 struct address_info *info;
163 * Instead starting always from the 16 bit block, we start
164 * from the last assigned block. This is a simple optimimazion
165 * for the case where a lot of blocks have been assigned, e.g.
166 * the first half of the private IP pool is in use and a new
167 * we need to find a new block.
169 * To only thing we have to make sure is that we terminated if
170 * there is no block left.
175 block = block_16_bits;
179 for (list = allocated_blocks; list; list = list->next) {
182 if (info->start <= block && block <= info->end) {
191 block = next_block(block);
192 } while (block != last_block);
197 static struct address_info *lookup_info(int index, uint32_t start)
201 for (list = allocated_blocks; list; list = list->next) {
202 struct address_info *info = list->data;
204 if (info->index == index && info->start == start)
211 static bool is_private_address(uint32_t address)
215 a = (address & 0xff000000) >> 24;
216 b = (address & 0x00ff0000) >> 16;
218 if (a == 10 || (a == 192 && b == 168) ||
219 (a == 172 && (b >= 16 && b <= 31)))
225 void __connman_ippool_newaddr(int index, const char *address,
226 unsigned char prefixlen)
228 struct address_info *info, *it;
230 uint32_t start, end, mask;
233 if (inet_aton(address, &inp) == 0)
236 start = ntohl(inp.s_addr);
237 if (!is_private_address(start))
243 mask = ~(0xffffffff >> prefixlen);
245 start = start & mask;
248 info = lookup_info(index, start);
252 info = g_try_new0(struct address_info, 1);
260 allocated_blocks = g_slist_prepend(allocated_blocks, info);
263 info->use_count = info->use_count + 1;
265 if (info->use_count > 1 || info->pool) {
267 * We need only to check for the first IP in a block for
273 for (list = allocated_blocks; list; list = list->next) {
279 if (!(info->start >= it->start && info->start <= it->end))
282 if (it->pool && it->pool->collision_cb)
283 it->pool->collision_cb(it->pool, it->pool->user_data);
289 void __connman_ippool_deladdr(int index, const char *address,
290 unsigned char prefixlen)
292 struct address_info *info;
294 uint32_t start, mask;
296 if (inet_aton(address, &inp) == 0)
299 start = ntohl(inp.s_addr);
300 if (!is_private_address(start))
303 mask = ~(0xffffffff >> prefixlen);
304 start = start & mask;
306 info = lookup_info(index, start);
308 /* In theory this should never happen */
309 connman_error("Inconsistent IP pool management (start not found)");
313 info->use_count = info->use_count - 1;
317 if (info->use_count > 0)
320 allocated_blocks = g_slist_remove(allocated_blocks, info);
324 struct connman_ippool *__connman_ippool_create(int index,
327 ippool_collision_cb_t collision_cb,
330 struct connman_ippool *pool;
331 struct address_info *info;
337 * The range is at max 255 and we don't support overlapping
340 if (start + range > 254) {
341 connman_error("IP pool does not support pool size larger than 254");
345 block = get_free_block(start + range);
347 connman_warn("Could not find a free IP block");
351 pool = g_try_new0(struct connman_ippool, 1);
355 info = g_try_new0(struct address_info, 1);
365 info->end = block + range;
368 pool->collision_cb = collision_cb;
369 pool->user_data = user_data;
376 pool->gateway = get_ip(info->start + 1);
377 pool->broadcast = get_ip(info->start + 255);
378 pool->subnet_mask = get_ip(subnet_mask_24);
379 pool->start_ip = get_ip(block + start);
380 pool->end_ip = get_ip(block + start + range);
382 allocated_blocks = g_slist_prepend(allocated_blocks, info);
387 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
389 return pool->gateway;
392 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
394 return pool->broadcast;
397 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
399 return pool->start_ip;
402 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
407 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
409 return pool->subnet_mask;
412 int __connman_ippool_init(void)
416 block_16_bits = ntohl(inet_addr("192.168.0.0"));
417 block_20_bits = ntohl(inet_addr("172.16.0.0"));
418 block_24_bits = ntohl(inet_addr("10.0.0.0"));
419 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
424 void __connman_ippool_cleanup(void)
428 g_slist_free_full(allocated_blocks, g_free);
430 allocated_blocks = NULL;