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)
201 struct connman_ippool *pool;
207 if (inet_aton(address, &inp) == 0)
210 block = ntohl(inp.s_addr) & 0xffffff00;
212 key = GUINT_TO_POINTER(block);
213 count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
215 g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
217 pool = g_hash_table_lookup(hash_pool, key);
221 if (pool->index == index)
224 if (pool->collision_cb != NULL)
225 pool->collision_cb(pool, pool->user_data);
228 void __connman_ippool_deladdr(int index, const char *address)
235 if (inet_aton(address, &inp) == 0)
238 block = ntohl(inp.s_addr) & 0xffffff00;
240 key = GUINT_TO_POINTER(block);
241 count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
245 g_hash_table_remove(hash_addresses, key);
247 g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
250 struct connman_ippool *__connman_ippool_create(int index,
253 ippool_collision_cb_t collision_cb,
256 struct connman_ippool *pool;
260 * The range is at max 255 and we don't support overlapping
263 if (start + range > 254)
266 block = find_free_block();
270 pool = g_try_new0(struct connman_ippool, 1);
277 pool->collision_cb = collision_cb;
278 pool->user_data = user_data;
285 pool->gateway = get_ip(block + 1);
286 pool->broadcast = get_ip(block + 255);
287 pool->subnet_mask = get_ip(subnet_mask_24);
288 pool->start_ip = get_ip(block + start);
289 pool->end_ip = get_ip(block + start + range);
291 g_hash_table_insert(hash_pool, GUINT_TO_POINTER(pool->block), pool);
296 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
298 return pool->gateway;
301 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
303 return pool->broadcast;
306 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
308 return pool->start_ip;
311 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
316 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
318 return pool->subnet_mask;
321 static void pool_free(gpointer data)
323 struct connman_ippool *pool = data;
325 g_free(pool->gateway);
326 g_free(pool->broadcast);
327 g_free(pool->start_ip);
328 g_free(pool->end_ip);
329 g_free(pool->subnet_mask);
334 int __connman_ippool_init(void)
338 /* We start at 254 by default to avoid common addresses */
339 block_16_bits = ntohl(inet_addr("192.168.254.0"));
340 block_20_bits = ntohl(inet_addr("172.16.0.0"));
341 block_24_bits = ntohl(inet_addr("10.0.0.0"));
342 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
344 hash_pool = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
346 hash_addresses = g_hash_table_new_full(g_direct_hash, g_direct_equal,
352 void __connman_ippool_cleanup(void)
356 g_hash_table_destroy(hash_pool);
359 g_hash_table_destroy(hash_addresses);
360 hash_addresses = NULL;