Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / src / ippool.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2012-2014  BMW Car IT GmbH.
7  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/errno.h>
32 #include <sys/socket.h>
33
34 #include "connman.h"
35
36 struct address_info {
37         int index;
38         uint32_t start;
39         uint32_t end;
40
41         unsigned int use_count;
42         struct connman_ippool *pool;
43 };
44
45 struct connman_ippool {
46         unsigned int refcount;
47
48         struct address_info *info;
49
50         char *gateway;
51         char *broadcast;
52         char *start_ip;
53         char *end_ip;
54         char *subnet_mask;
55
56         ippool_collision_cb_t collision_cb;
57         void *user_data;
58 };
59
60 GSList *allocated_blocks;
61 GHashTable *pool_hash;
62
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;
68
69 struct connman_ippool *
70 __connman_ippool_ref_debug(struct connman_ippool *pool,
71                                 const char *file, int line, const char *caller)
72 {
73         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
74                 file, line, caller);
75
76         __sync_fetch_and_add(&pool->refcount, 1);
77
78         return pool;
79 }
80
81 void __connman_ippool_unref_debug(struct connman_ippool *pool,
82                                 const char *file, int line, const char *caller)
83 {
84         if (!pool)
85                 return;
86
87         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
88                 file, line, caller);
89
90         if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
91                 return;
92
93         g_hash_table_remove(pool_hash, pool);
94 }
95
96 static char *get_ip(uint32_t ip)
97 {
98         struct in_addr addr;
99
100         addr.s_addr = htonl(ip);
101
102         return g_strdup(inet_ntoa(addr));
103 }
104
105 static uint32_t next_block(uint32_t block)
106 {
107         uint32_t next;
108
109         /*
110          * Return the next IP block within the private IP range
111          *
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
115          */
116
117         next = (block & 0x0000ff00) >> 8;
118         next += 1;
119
120         if (next == 255) {
121                 if ((block & 0xffff0000) == block_16_bits) {
122                         /*
123                          * Reached the end of the 16 bit block, switch
124                          * to the 20-bit block.
125                          */
126                         return block_20_bits;
127                 }
128
129                 if ((block & 0xffff0000) >= block_20_bits) {
130                         next = (block & 0x00ff0000) >> 16;
131                         if (next >= 16 && next < 32)
132                                 next += 1;
133
134                         if (next == 32) {
135                                 /*
136                                  * Reached the end of the 20 bit
137                                  * block, switch to the 24-bit block.
138                                  */
139                                 return block_24_bits;
140                         }
141
142                         return (block & 0xff000000) |
143                                 ((next << 16) & 0x00ff0000);
144                 }
145
146                 if ((block & 0xff000000) == block_24_bits) {
147                         next = (block & 0x00ff0000) >> 16;
148                         if (next < 255)
149                                 next += 1;
150
151                         if (next == 255) {
152                                 /*
153                                  * Reached the end of the 24 bit
154                                  * block, switch to the 16-bit block.
155                                  */
156                                 return block_16_bits;
157                         }
158
159                         return (block & 0xff000000) |
160                                 ((next << 16) & 0x00ff0000);
161                 }
162         }
163
164         return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
165 }
166
167 static uint32_t get_free_block(unsigned int size)
168 {
169         struct address_info *info;
170         uint32_t block;
171         GSList *list;
172         bool collision;
173
174         /*
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.
180          *
181          * To only thing we have to make sure is that we terminated if
182          * there is no block left.
183          */
184         if (last_block == 0)
185                 block = block_16_bits;
186         else
187                 block = next_block(last_block);
188
189         do {
190                 collision = false;
191                 for (list = allocated_blocks; list; list = list->next) {
192                         info = list->data;
193
194                         if (info->start <= block && block <= info->end) {
195                                 collision = true;
196                                 break;
197                         }
198                 }
199
200                 if (!collision)
201                         return block;
202
203                 block = next_block(block);
204         } while (block != last_block);
205
206         return 0;
207 }
208
209 static struct address_info *lookup_info(int index, uint32_t start)
210 {
211         GSList *list;
212
213         for (list = allocated_blocks; list; list = list->next) {
214                 struct address_info *info = list->data;
215
216                 if (info->index == index && info->start == start)
217                         return info;
218         }
219
220         return NULL;
221 }
222
223 static bool is_private_address(uint32_t address)
224 {
225         unsigned int a, b;
226
227         a = (address & 0xff000000) >> 24;
228         b = (address & 0x00ff0000) >> 16;
229
230         if (a == 10 || (a == 192 && b == 168) ||
231                                         (a == 172 && (b >= 16 && b <= 31)))
232                 return true;
233
234         return false;
235 }
236
237 void __connman_ippool_newaddr(int index, const char *address,
238                                 unsigned char prefixlen)
239 {
240         struct address_info *info, *it;
241         struct in_addr inp;
242         uint32_t start, end, mask;
243         GSList *list;
244
245         if (inet_aton(address, &inp) == 0)
246                 return;
247
248         start = ntohl(inp.s_addr);
249         if (!is_private_address(start))
250                 return;
251
252         if (prefixlen >= 32)
253                 mask = 0xffffffff;
254         else
255                 mask = ~(0xffffffff >> prefixlen);
256
257         start = start & mask;
258         end = start | ~mask;
259
260         info = lookup_info(index, start);
261         if (info)
262                 goto update;
263
264         info = g_try_new0(struct address_info, 1);
265         if (!info)
266                 return;
267
268         info->index = index;
269         info->start = start;
270         info->end = end;
271
272         allocated_blocks = g_slist_prepend(allocated_blocks, info);
273
274 update:
275         info->use_count = info->use_count + 1;
276
277         if (info->use_count > 1 || info->pool) {
278                 /*
279                  * We need only to check for the first IP in a block for
280                  * collisions.
281                  */
282                 return;
283         }
284
285         for (list = allocated_blocks; list; list = list->next) {
286                 it = list->data;
287
288                 if (it == info)
289                         continue;
290
291                 if (!(info->start >= it->start && info->start <= it->end))
292                         continue;
293
294                 if (it->pool && it->pool->collision_cb)
295                         it->pool->collision_cb(it->pool, it->pool->user_data);
296
297                 return;
298         }
299 }
300
301 void __connman_ippool_deladdr(int index, const char *address,
302                                 unsigned char prefixlen)
303 {
304         struct address_info *info;
305         struct in_addr inp;
306         uint32_t start, mask;
307
308         if (inet_aton(address, &inp) == 0)
309                 return;
310
311         start = ntohl(inp.s_addr);
312         if (!is_private_address(start))
313                 return;
314
315         mask = ~(0xffffffff >> prefixlen);
316         start = start & mask;
317
318         info = lookup_info(index, start);
319         if (!info) {
320                 /* In theory this should never happen */
321                 connman_error("Inconsistent IP pool management (start not found)");
322                 return;
323         }
324
325         info->use_count = info->use_count - 1;
326         if (info->pool)
327                 return;
328
329         if (info->use_count > 0)
330                 return;
331
332         allocated_blocks = g_slist_remove(allocated_blocks, info);
333         g_free(info);
334 }
335
336 struct connman_ippool *__connman_ippool_create(int index,
337                                         unsigned int start,
338                                         unsigned int range,
339                                         ippool_collision_cb_t collision_cb,
340                                         void *user_data)
341 {
342         struct connman_ippool *pool;
343         struct address_info *info;
344         uint32_t block;
345
346         DBG("");
347
348         /*
349          * The range is at max 255 and we don't support overlapping
350          * blocks.
351          */
352         if (start + range > 254) {
353                 connman_error("IP pool does not support pool size larger than 254");
354                 return NULL;
355         }
356
357         block = get_free_block(start + range);
358         if (block == 0) {
359                 connman_warn("Could not find a free IP block");
360                 return NULL;
361         }
362
363         pool = g_try_new0(struct connman_ippool, 1);
364         if (!pool)
365                 return NULL;
366
367         info = g_try_new0(struct address_info, 1);
368         if (!info) {
369                 g_free(pool);
370                 return NULL;
371         }
372
373         last_block = block;
374
375         info->index = index;
376         info->start = block;
377         info->end = block + range;
378
379         pool->refcount = 1;
380         pool->info = info;
381         pool->collision_cb = collision_cb;
382         pool->user_data = user_data;
383
384         info->pool = pool;
385
386         if (range == 0)
387                 range = 1;
388
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);
394
395         allocated_blocks = g_slist_prepend(allocated_blocks, info);
396         g_hash_table_insert(pool_hash, pool, pool);
397
398         return pool;
399 }
400
401 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
402 {
403         return pool->gateway;
404 }
405
406 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
407 {
408         return pool->broadcast;
409 }
410
411 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
412 {
413         return pool->start_ip;
414 }
415
416 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
417 {
418         return pool->end_ip;
419 }
420
421 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
422 {
423         return pool->subnet_mask;
424 }
425
426 static void pool_free(gpointer data)
427 {
428         struct connman_ippool *pool = data;
429
430         if (pool->info) {
431                 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
432                 g_free(pool->info);
433         }
434
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);
440
441         g_free(pool);
442 }
443
444 int __connman_ippool_init(void)
445 {
446         DBG("");
447
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"));
452
453         pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
454                                         pool_free);
455
456         return 0;
457 }
458
459 void __connman_ippool_cleanup(void)
460 {
461         DBG("");
462
463         g_hash_table_destroy(pool_hash);
464         pool_hash = NULL;
465
466         g_slist_free_full(allocated_blocks, g_free);
467         last_block = 0;
468         allocated_blocks = NULL;
469 }