ba613fb193cb0f10277f1e1a29cb636e342b44a9
[platform/upstream/connman.git] / src / ippool.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2012  BMW Car IT GmbH. All rights reserved.
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 <getopt.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/errno.h>
33 #include <sys/socket.h>
34
35 #include "connman.h"
36
37 struct address_info {
38         int index;
39         uint32_t start;
40         uint32_t end;
41
42         unsigned int use_count;
43         struct connman_ippool *pool;
44 };
45
46 struct connman_ippool {
47         unsigned int refcount;
48
49         struct address_info *info;
50
51         char *gateway;
52         char *broadcast;
53         char *start_ip;
54         char *end_ip;
55         char *subnet_mask;
56
57         ippool_collision_cb_t collision_cb;
58         void *user_data;
59 };
60
61 GSList *allocated_blocks;
62 GHashTable *pool_hash;
63
64 static uint32_t last_block;
65 static uint32_t block_16_bits;
66 static uint32_t block_20_bits;
67 static uint32_t block_24_bits;
68 static uint32_t subnet_mask_24;
69
70 struct connman_ippool *
71 __connman_ippool_ref_debug(struct connman_ippool *pool,
72                                 const char *file, int line, const char *caller)
73 {
74         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
75                 file, line, caller);
76
77         __sync_fetch_and_add(&pool->refcount, 1);
78
79         return pool;
80 }
81
82 void __connman_ippool_unref_debug(struct connman_ippool *pool,
83                                 const char *file, int line, const char *caller)
84 {
85         if (pool == NULL)
86                 return;
87
88         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
89                 file, line, caller);
90
91         if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
92                 return;
93
94         g_hash_table_remove(pool_hash, pool);
95 }
96
97 static char *get_ip(uint32_t ip)
98 {
99         struct in_addr addr;
100
101         addr.s_addr = htonl(ip);
102
103         return g_strdup(inet_ntoa(addr));
104 }
105
106 static uint32_t next_block(uint32_t block)
107 {
108         uint32_t next;
109
110         /*
111          * Return the next IP block within the private IP range
112          *
113          * 16-bit block 192.168.0.0 – 192.168.255.255
114          * 20-bit block  172.16.0.0 –  172.31.255.255
115          * 24-bit block    10.0.0.0 –  10.255.255.255
116          */
117
118         next = (block & 0x0000ff00) >> 8;
119         next += 1;
120
121         if (next == 255) {
122                 if ((block & 0xffff0000) == block_16_bits) {
123                         /*
124                          * Reached the end of the 16 bit block, switch
125                          * to the 20-bit block.
126                          */
127                         return block_20_bits;
128                 }
129
130                 if ((block & 0xffff0000) >= block_20_bits) {
131                         next = (block & 0x00ff0000) >> 16;
132                         if (next >= 16 && next < 32)
133                                 next += 1;
134
135                         if (next == 32) {
136                                 /*
137                                  * Reached the end of the 20 bit
138                                  * block, switch to the 24-bit block.
139                                  */
140                                 return block_24_bits;
141                         }
142
143                         return (block & 0xff000000) |
144                                 ((next << 16) & 0x00ff0000);
145                 }
146
147                 if ((block & 0xff000000) == block_24_bits) {
148                         next = (block & 0x00ff0000) >> 16;
149                         if (next < 255)
150                                 next += 1;
151
152                         if (next == 255) {
153                                 /*
154                                  * Reached the end of the 24 bit
155                                  * block, switch to the 16-bit block.
156                                  */
157                                 return block_16_bits;
158                         }
159
160                         return (block & 0xff000000) |
161                                 ((next << 16) & 0x00ff0000);
162                 }
163         }
164
165         return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
166 }
167
168 static uint32_t get_free_block(unsigned int size)
169 {
170         struct address_info *info;
171         uint32_t block;
172         GSList *list;
173         connman_bool_t collision;
174
175         /*
176          * Instead starting always from the 16 bit block, we start
177          * from the last assigned block. This is a simple optimimazion
178          * for the case where a lot of blocks have been assigned, e.g.
179          * the first half of the private IP pool is in use and a new
180          * we need to find a new block.
181          *
182          * To only thing we have to make sure is that we terminated if
183          * there is no block left.
184          */
185         if (last_block == 0)
186                 block = block_16_bits;
187         else
188                 block = next_block(last_block);
189
190         do {
191                 collision = FALSE;
192                 for (list = allocated_blocks; list != NULL; list = list->next) {
193                         info = list->data;
194
195                         if (info->start <= block && block <= info->end) {
196                                 collision = TRUE;
197                                 break;
198                         }
199                 }
200
201                 if (collision == FALSE)
202                         return block;
203
204                 block = next_block(block);
205         } while (block != last_block);
206
207         return 0;
208 }
209
210 static struct address_info *lookup_info(int index, uint32_t start)
211 {
212         GSList *list;
213
214         for (list = allocated_blocks; list != NULL; list = list->next) {
215                 struct address_info *info = list->data;
216
217                 if (info->index == index && info->start == start)
218                         return info;
219         }
220
221         return NULL;
222 }
223
224 static connman_bool_t is_private_address(uint32_t address)
225 {
226         unsigned int a, b;
227
228         a = (address & 0xff000000) >> 24;
229         b = (address & 0x00ff0000) >> 16;
230
231         if (a == 10 || (a == 192 && b == 168) ||
232                                         (a == 172 && (b >= 16 && b <= 31)))
233                 return TRUE;
234
235         return FALSE;
236 }
237
238 void __connman_ippool_newaddr(int index, const char *address,
239                                 unsigned char prefixlen)
240 {
241         struct address_info *info, *it;
242         struct in_addr inp;
243         uint32_t start, end, mask;
244         GSList *list;
245
246         if (inet_aton(address, &inp) == 0)
247                 return;
248
249         start = ntohl(inp.s_addr);
250         if (is_private_address(start) == FALSE)
251                 return;
252
253         if (prefixlen >= 32)
254                 mask = 0xffffffff;
255         else
256                 mask = ~(0xffffffff >> prefixlen);
257
258         start = start & mask;
259         end = start | ~mask;
260
261         info = lookup_info(index, start);
262         if (info != NULL)
263                 goto update;
264
265         info = g_try_new0(struct address_info, 1);
266         if (info == NULL)
267                 return;
268
269         info->index = index;
270         info->start = start;
271         info->end = end;
272
273         allocated_blocks = g_slist_prepend(allocated_blocks, info);
274
275 update:
276         info->use_count = info->use_count + 1;
277
278         if (info->use_count > 1 || info->pool != NULL) {
279                 /*
280                  * We need only to check for the first IP in a block for
281                  * collisions.
282                  */
283                 return;
284         }
285
286         for (list = allocated_blocks; list != NULL; list = list->next) {
287                 it = list->data;
288
289                 if (it == info)
290                         continue;
291
292                 if (!(it->start <= info->start || info->start <= it->end))
293                         continue;
294
295                 if (it->pool != NULL && it->pool->collision_cb != NULL)
296                         it->pool->collision_cb(it->pool, it->pool->user_data);
297
298                 return;
299         }
300 }
301
302 void __connman_ippool_deladdr(int index, const char *address,
303                                 unsigned char prefixlen)
304 {
305         struct address_info *info;
306         struct in_addr inp;
307         uint32_t start, mask;
308
309         if (inet_aton(address, &inp) == 0)
310                 return;
311
312         start = ntohl(inp.s_addr);
313         if (is_private_address(start) == FALSE)
314                 return;
315
316         mask = ~(0xffffffff >> prefixlen);
317         start = start & mask;
318
319         info = lookup_info(index, start);
320         if (info == NULL) {
321                 /* In theory this should never happen */
322                 connman_error("Inconsistent IP pool management (start not found)");
323                 return;
324         }
325
326         info->use_count = info->use_count - 1;
327         if (info->pool != NULL)
328                 return;
329
330         if (info->use_count > 0)
331                 return;
332
333         allocated_blocks = g_slist_remove(allocated_blocks, 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 == NULL)
365                 return NULL;
366
367         info = g_try_new0(struct address_info, 1);
368         if (info == NULL) {
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 != NULL) {
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(allocated_blocks);
467         last_block = 0;
468         allocated_blocks = NULL;
469 }