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