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>
37 struct connman_ippool {
38 unsigned int refcount;
49 ippool_collision_cb_t collision_cb;
53 static GHashTable *hash_pool;
54 static GHashTable *hash_addresses;
55 static uint32_t last_block;
56 static uint32_t block_16_bits;
57 static uint32_t block_20_bits;
58 static uint32_t block_24_bits;
59 static uint32_t subnet_mask_24;
61 struct connman_ippool *
62 __connman_ippool_ref_debug(struct connman_ippool *pool,
63 const char *file, int line, const char *caller)
65 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
68 __sync_fetch_and_add(&pool->refcount, 1);
73 void __connman_ippool_unref_debug(struct connman_ippool *pool,
74 const char *file, int line, const char *caller)
79 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
82 if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
85 g_hash_table_remove(hash_pool, GUINT_TO_POINTER(pool->block));
88 static char *get_ip(uint32_t ip)
92 addr.s_addr = htonl(ip);
94 return g_strdup(inet_ntoa(addr));
97 static uint32_t next_block(uint32_t block)
102 * Return the next IP block within the private IP range
104 * 16-bit block 192.168.0.0 – 192.168.255.255
105 * 20-bit block 172.16.0.0 – 172.31.255.255
106 * 24-bit block 10.0.0.0 – 10.255.255.255
109 next = (block & 0x0000ff00) >> 8;
113 if ((block & 0xffffff00) == block_16_bits) {
115 * Reached the end of the 16 bit block, switch
116 * to the 20-bit block.
118 return block_20_bits;
121 if ((block & 0xffff0000) >= block_20_bits) {
122 next = (block & 0x00ff0000) >> 16;
123 if (next >= 16 && next < 32)
128 * Reached the end of the 20 bit
129 * block, switch to the 24-bit block.
131 return block_24_bits;
134 return (block & 0xff000000) |
135 ((next << 16) & 0x00ff0000);
138 if ((block & 0xff000000) == block_24_bits) {
139 next = (block & 0x00ff0000) >> 16;
145 * Reached the end of the 24 bit
146 * block, switch to the 16-bit block.
148 return block_16_bits;
151 return (block & 0xff000000) |
152 ((next << 16) & 0x00ff0000);
156 return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
159 static uint32_t find_free_block()
161 struct connman_ippool *pool;
167 return block_16_bits;
170 * Instead starting always from the 16 bit block, we start
171 * from the last assigned block. This is a simple optimimazion
172 * for the case where a lot of blocks have been assigned, e.g.
173 * the first half of the private IP pool is in use and a new
174 * we need to find a new block.
176 * To only thing we have to make sure is that we terminated if
177 * there is no block left.
181 block = next_block(start);
182 while (start != block) {
183 block = next_block(block);
185 key = GUINT_TO_POINTER(block);
186 pool = g_hash_table_lookup(hash_pool, key);
190 if (g_hash_table_lookup(hash_addresses, key) != NULL)
199 void __connman_ippool_newaddr(int index, const char *address,
200 unsigned char prefixlen)
202 struct connman_ippool *pool;
208 if (inet_aton(address, &inp) == 0)
211 block = ntohl(inp.s_addr) & 0xffffff00;
213 key = GUINT_TO_POINTER(block);
214 count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
216 g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
218 pool = g_hash_table_lookup(hash_pool, key);
222 if (pool->index == index)
225 if (pool->collision_cb != NULL)
226 pool->collision_cb(pool, pool->user_data);
229 void __connman_ippool_deladdr(int index, const char *address,
230 unsigned char prefixlen)
237 if (inet_aton(address, &inp) == 0)
240 block = ntohl(inp.s_addr) & 0xffffff00;
242 key = GUINT_TO_POINTER(block);
243 count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
247 g_hash_table_remove(hash_addresses, key);
249 g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
252 struct connman_ippool *__connman_ippool_create(int index,
255 ippool_collision_cb_t collision_cb,
258 struct connman_ippool *pool;
262 * The range is at max 255 and we don't support overlapping
265 if (start + range > 254)
268 block = find_free_block();
272 pool = g_try_new0(struct connman_ippool, 1);
279 pool->collision_cb = collision_cb;
280 pool->user_data = user_data;
287 pool->gateway = get_ip(block + 1);
288 pool->broadcast = get_ip(block + 255);
289 pool->subnet_mask = get_ip(subnet_mask_24);
290 pool->start_ip = get_ip(block + start);
291 pool->end_ip = get_ip(block + start + range);
293 g_hash_table_insert(hash_pool, GUINT_TO_POINTER(pool->block), pool);
298 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
300 return pool->gateway;
303 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
305 return pool->broadcast;
308 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
310 return pool->start_ip;
313 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
318 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
320 return pool->subnet_mask;
323 static void pool_free(gpointer data)
325 struct connman_ippool *pool = data;
327 g_free(pool->gateway);
328 g_free(pool->broadcast);
329 g_free(pool->start_ip);
330 g_free(pool->end_ip);
331 g_free(pool->subnet_mask);
336 int __connman_ippool_init(void)
340 /* We start at 254 by default to avoid common addresses */
341 block_16_bits = ntohl(inet_addr("192.168.254.0"));
342 block_20_bits = ntohl(inet_addr("172.16.0.0"));
343 block_24_bits = ntohl(inet_addr("10.0.0.0"));
344 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
346 hash_pool = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
348 hash_addresses = g_hash_table_new_full(g_direct_hash, g_direct_equal,
354 void __connman_ippool_cleanup(void)
358 g_hash_table_destroy(hash_pool);
361 g_hash_table_destroy(hash_addresses);
362 hash_addresses = NULL;