timeserver: Simplify timeserver IP address checking
[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         uint32_t val;
227
228         if ((address & 0xff000000) == block_24_bits)
229                 return TRUE;
230
231         if ((address & 0xffff0000) == block_20_bits) {
232                 val = (address & 0x00ff0000) >> 16;
233
234                 if (val < 16 || val > 31)
235                         return FALSE;
236
237                 return TRUE;
238         }
239
240         if ((address & 0xffffff00) == block_16_bits)
241                 return TRUE;
242
243         return FALSE;
244 }
245
246 void __connman_ippool_newaddr(int index, const char *address,
247                                 unsigned char prefixlen)
248 {
249         struct address_info *info, *it;
250         struct in_addr inp;
251         uint32_t start, end, mask;
252         GSList *list;
253
254         if (inet_aton(address, &inp) == 0)
255                 return;
256
257         start = ntohl(inp.s_addr);
258         if (is_private_address(start) == FALSE)
259                 return;
260
261         mask = ~(0xffffffff >> prefixlen);
262         start = start & mask;
263         end = start | ~mask;
264
265         info = lookup_info(index, start);
266         if (info != NULL)
267                 goto update;
268
269         info = g_try_new0(struct address_info, 1);
270         if (info == NULL)
271                 return;
272
273         info->index = index;
274         info->start = start;
275         info->end = end;
276
277         allocated_blocks = g_slist_prepend(allocated_blocks, info);
278
279 update:
280         info->use_count = info->use_count + 1;
281
282         if (info->use_count > 1 || info->pool != NULL) {
283                 /*
284                  * We need only to check for the first IP in a block for
285                  * collisions.
286                  */
287                 return;
288         }
289
290         for (list = allocated_blocks; list != NULL; list = list->next) {
291                 it = list->data;
292
293                 if (it == info)
294                         continue;
295
296                 if (!(it->start <= info->start || info->start <= it->end))
297                         continue;
298
299                 if (it->pool != NULL && it->pool->collision_cb != NULL)
300                         it->pool->collision_cb(it->pool, it->pool->user_data);
301
302                 return;
303         }
304 }
305
306 void __connman_ippool_deladdr(int index, const char *address,
307                                 unsigned char prefixlen)
308 {
309         struct address_info *info;
310         struct in_addr inp;
311         uint32_t start, mask;
312
313         if (inet_aton(address, &inp) == 0)
314                 return;
315
316         start = ntohl(inp.s_addr);
317         if (is_private_address(start) == FALSE)
318                 return;
319
320         mask = ~(0xffffffff >> prefixlen);
321         start = start & mask;
322
323         info = lookup_info(index, start);
324         if (info == NULL) {
325                 /* In theory this should never happen */
326                 connman_error("Inconsistent IP pool management (start not found)");
327                 return;
328         }
329
330         info->use_count = info->use_count - 1;
331         if (info->pool != NULL)
332                 return;
333
334         if (info->use_count > 0)
335                 return;
336
337         allocated_blocks = g_slist_remove(allocated_blocks, info);
338 }
339
340 struct connman_ippool *__connman_ippool_create(int index,
341                                         unsigned int start,
342                                         unsigned int range,
343                                         ippool_collision_cb_t collision_cb,
344                                         void *user_data)
345 {
346         struct connman_ippool *pool;
347         struct address_info *info;
348         uint32_t block;
349
350         DBG("");
351
352         /*
353          * The range is at max 255 and we don't support overlapping
354          * blocks.
355          */
356         if (start + range > 254) {
357                 connman_error("IP pool does not support pool size larger than 254");
358                 return NULL;
359         }
360
361         block = get_free_block(start + range);
362         if (block == 0) {
363                 connman_warn("Could not find a free IP block");
364                 return NULL;
365         }
366
367         pool = g_try_new0(struct connman_ippool, 1);
368         if (pool == NULL)
369                 return NULL;
370
371         info = g_try_new0(struct address_info, 1);
372         if (info == NULL) {
373                 g_free(pool);
374                 return NULL;
375         }
376
377         last_block = block;
378
379         info->index = index;
380         info->start = block;
381         info->end = block + range;
382
383         pool->refcount = 1;
384         pool->info = info;
385         pool->collision_cb = collision_cb;
386         pool->user_data = user_data;
387
388         info->pool = pool;
389
390         if (range == 0)
391                 range = 1;
392
393         pool->gateway = get_ip(info->start + 1);
394         pool->broadcast = get_ip(info->start + 255);
395         pool->subnet_mask = get_ip(subnet_mask_24);
396         pool->start_ip = get_ip(block + start);
397         pool->end_ip = get_ip(block + start + range);
398
399         allocated_blocks = g_slist_prepend(allocated_blocks, info);
400         g_hash_table_insert(pool_hash, pool, pool);
401
402         return pool;
403 }
404
405 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
406 {
407         return pool->gateway;
408 }
409
410 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
411 {
412         return pool->broadcast;
413 }
414
415 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
416 {
417         return pool->start_ip;
418 }
419
420 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
421 {
422         return pool->end_ip;
423 }
424
425 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
426 {
427         return pool->subnet_mask;
428 }
429
430 static void pool_free(gpointer data)
431 {
432         struct connman_ippool *pool = data;
433
434         if (pool->info != NULL) {
435                 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
436                 g_free(pool->info);
437         }
438
439         g_free(pool->gateway);
440         g_free(pool->broadcast);
441         g_free(pool->start_ip);
442         g_free(pool->end_ip);
443         g_free(pool->subnet_mask);
444
445         g_free(pool);
446 }
447
448 int __connman_ippool_init(void)
449 {
450         DBG("");
451
452         block_16_bits = ntohl(inet_addr("192.168.0.0"));
453         block_20_bits = ntohl(inet_addr("172.16.0.0"));
454         block_24_bits = ntohl(inet_addr("10.0.0.0"));
455         subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
456
457         pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
458                                         pool_free);
459
460         return 0;
461 }
462
463 void __connman_ippool_cleanup(void)
464 {
465         DBG("");
466
467         g_hash_table_destroy(pool_hash);
468         pool_hash = NULL;
469
470         g_slist_free(allocated_blocks);
471         last_block = 0;
472 }