Updated connman to version 1.35
[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
62 static uint32_t last_block;
63 static uint32_t block_16_bits;
64 static uint32_t block_20_bits;
65 static uint32_t block_24_bits;
66 static uint32_t subnet_mask_24;
67
68 struct connman_ippool *
69 __connman_ippool_ref_debug(struct connman_ippool *pool,
70                                 const char *file, int line, const char *caller)
71 {
72         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
73                 file, line, caller);
74
75         __sync_fetch_and_add(&pool->refcount, 1);
76
77         return pool;
78 }
79
80 void __connman_ippool_unref_debug(struct connman_ippool *pool,
81                                 const char *file, int line, const char *caller)
82 {
83         if (!pool)
84                 return;
85
86         DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
87                 file, line, caller);
88
89         if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
90                 return;
91
92         if (pool->info) {
93                 allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
94                 g_free(pool->info);
95         }
96
97         g_free(pool->gateway);
98         g_free(pool->broadcast);
99         g_free(pool->start_ip);
100         g_free(pool->end_ip);
101         g_free(pool->subnet_mask);
102
103         g_free(pool);
104 }
105
106 static char *get_ip(uint32_t ip)
107 {
108         struct in_addr addr;
109
110         addr.s_addr = htonl(ip);
111
112         return g_strdup(inet_ntoa(addr));
113 }
114
115 static uint32_t next_block(uint32_t block)
116 {
117         uint32_t next;
118
119         /*
120          * Return the next IP block within the private IP range
121          *
122          * 16-bit block 192.168.0.0 – 192.168.255.255
123          * 20-bit block  172.16.0.0 –  172.31.255.255
124          * 24-bit block    10.0.0.0 –  10.255.255.255
125          */
126
127         next = (block & 0x0000ff00) >> 8;
128         next += 1;
129
130         if (next == 255) {
131                 if ((block & 0xffff0000) == block_16_bits) {
132                         /*
133                          * Reached the end of the 16 bit block, switch
134                          * to the 20-bit block.
135                          */
136                         return block_20_bits;
137                 }
138
139                 if ((block & 0xffff0000) >= block_20_bits) {
140                         next = (block & 0x00ff0000) >> 16;
141                         if (next >= 16 && next < 32)
142                                 next += 1;
143
144                         if (next == 32) {
145                                 /*
146                                  * Reached the end of the 20 bit
147                                  * block, switch to the 24-bit block.
148                                  */
149                                 return block_24_bits;
150                         }
151
152                         return (block & 0xff000000) |
153                                 ((next << 16) & 0x00ff0000);
154                 }
155
156                 if ((block & 0xff000000) == block_24_bits) {
157                         next = (block & 0x00ff0000) >> 16;
158                         if (next < 255)
159                                 next += 1;
160
161                         if (next == 255) {
162                                 /*
163                                  * Reached the end of the 24 bit
164                                  * block, switch to the 16-bit block.
165                                  */
166                                 return block_16_bits;
167                         }
168
169                         return (block & 0xff000000) |
170                                 ((next << 16) & 0x00ff0000);
171                 }
172         }
173
174         return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
175 }
176
177 static uint32_t get_free_block(unsigned int size)
178 {
179         struct address_info *info;
180         uint32_t block;
181         GSList *list;
182         bool collision;
183
184         /*
185          * Instead starting always from the 16 bit block, we start
186          * from the last assigned block. This is a simple optimimazion
187          * for the case where a lot of blocks have been assigned, e.g.
188          * the first half of the private IP pool is in use and a new
189          * we need to find a new block.
190          *
191          * To only thing we have to make sure is that we terminated if
192          * there is no block left.
193          */
194         if (last_block)
195                 block = last_block;
196         else
197                 block = block_16_bits;
198
199         do {
200                 collision = false;
201                 for (list = allocated_blocks; list; list = list->next) {
202                         info = list->data;
203
204                         if (info->start <= block && block <= info->end) {
205                                 collision = true;
206                                 break;
207                         }
208                 }
209
210                 if (!collision)
211                         return block;
212
213                 block = next_block(block);
214         } while (block != last_block);
215
216         return 0;
217 }
218
219 static struct address_info *lookup_info(int index, uint32_t start)
220 {
221         GSList *list;
222
223         for (list = allocated_blocks; list; list = list->next) {
224                 struct address_info *info = list->data;
225
226                 if (info->index == index && info->start == start)
227                         return info;
228         }
229
230         return NULL;
231 }
232
233 static bool is_private_address(uint32_t address)
234 {
235         unsigned int a, b;
236
237         a = (address & 0xff000000) >> 24;
238         b = (address & 0x00ff0000) >> 16;
239
240         if (a == 10 || (a == 192 && b == 168) ||
241                                         (a == 172 && (b >= 16 && b <= 31)))
242                 return true;
243
244         return false;
245 }
246
247 void __connman_ippool_newaddr(int index, const char *address,
248                                 unsigned char prefixlen)
249 {
250         struct address_info *info, *it;
251         struct in_addr inp;
252         uint32_t start, end, mask;
253         GSList *list;
254
255         if (inet_aton(address, &inp) == 0)
256                 return;
257
258         start = ntohl(inp.s_addr);
259         if (!is_private_address(start))
260                 return;
261
262         if (prefixlen >= 32)
263                 mask = 0xffffffff;
264         else
265                 mask = ~(0xffffffff >> prefixlen);
266
267         start = start & mask;
268         end = start | ~mask;
269
270         info = lookup_info(index, start);
271         if (info)
272                 goto update;
273
274         info = g_try_new0(struct address_info, 1);
275         if (!info)
276                 return;
277
278         info->index = index;
279         info->start = start;
280         info->end = end;
281
282         allocated_blocks = g_slist_prepend(allocated_blocks, info);
283
284 update:
285         info->use_count = info->use_count + 1;
286
287         if (info->use_count > 1 || info->pool) {
288                 /*
289                  * We need only to check for the first IP in a block for
290                  * collisions.
291                  */
292                 return;
293         }
294
295         for (list = allocated_blocks; list; list = list->next) {
296                 it = list->data;
297
298                 if (it == info)
299                         continue;
300
301                 if (!(info->start >= it->start && info->start <= it->end))
302                         continue;
303
304                 if (it->pool && it->pool->collision_cb)
305                         it->pool->collision_cb(it->pool, it->pool->user_data);
306
307                 return;
308         }
309 }
310
311 void __connman_ippool_deladdr(int index, const char *address,
312                                 unsigned char prefixlen)
313 {
314         struct address_info *info;
315         struct in_addr inp;
316         uint32_t start, mask;
317
318         if (inet_aton(address, &inp) == 0)
319                 return;
320
321         start = ntohl(inp.s_addr);
322         if (!is_private_address(start))
323                 return;
324
325         mask = ~(0xffffffff >> prefixlen);
326         start = start & mask;
327
328         info = lookup_info(index, start);
329         if (!info) {
330                 /* In theory this should never happen */
331                 connman_error("Inconsistent IP pool management (start not found)");
332                 return;
333         }
334
335         info->use_count = info->use_count - 1;
336         if (info->pool)
337                 return;
338
339         if (info->use_count > 0)
340                 return;
341
342         allocated_blocks = g_slist_remove(allocated_blocks, info);
343         g_free(info);
344 }
345
346 struct connman_ippool *__connman_ippool_create(int index,
347                                         unsigned int start,
348                                         unsigned int range,
349                                         ippool_collision_cb_t collision_cb,
350                                         void *user_data)
351 {
352         struct connman_ippool *pool;
353         struct address_info *info;
354         uint32_t block;
355
356         DBG("");
357
358         /*
359          * The range is at max 255 and we don't support overlapping
360          * blocks.
361          */
362         if (start + range > 254) {
363                 connman_error("IP pool does not support pool size larger than 254");
364                 return NULL;
365         }
366
367         block = get_free_block(start + range);
368         if (block == 0) {
369                 connman_warn("Could not find a free IP block");
370                 return NULL;
371         }
372
373         pool = g_try_new0(struct connman_ippool, 1);
374         if (!pool)
375                 return NULL;
376
377         info = g_try_new0(struct address_info, 1);
378         if (!info) {
379                 g_free(pool);
380                 return NULL;
381         }
382
383         last_block = block;
384
385         info->index = index;
386         info->start = block;
387         info->end = block + range;
388
389         pool->refcount = 1;
390         pool->info = info;
391         pool->collision_cb = collision_cb;
392         pool->user_data = user_data;
393
394         info->pool = pool;
395
396         if (range == 0)
397                 range = 1;
398
399         pool->gateway = get_ip(info->start + 1);
400         pool->broadcast = get_ip(info->start + 255);
401         pool->subnet_mask = get_ip(subnet_mask_24);
402         pool->start_ip = get_ip(block + start);
403         pool->end_ip = get_ip(block + start + range);
404
405         allocated_blocks = g_slist_prepend(allocated_blocks, info);
406
407         return pool;
408 }
409
410 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
411 {
412         return pool->gateway;
413 }
414
415 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
416 {
417         return pool->broadcast;
418 }
419
420 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
421 {
422         return pool->start_ip;
423 }
424
425 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
426 {
427         return pool->end_ip;
428 }
429
430 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
431 {
432         return pool->subnet_mask;
433 }
434
435 int __connman_ippool_init(void)
436 {
437         DBG("");
438
439         block_16_bits = ntohl(inet_addr("192.168.0.0"));
440         block_20_bits = ntohl(inet_addr("172.16.0.0"));
441         block_24_bits = ntohl(inet_addr("10.0.0.0"));
442         subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
443
444         return 0;
445 }
446
447 void __connman_ippool_cleanup(void)
448 {
449         DBG("");
450
451         g_slist_free_full(allocated_blocks, g_free);
452         last_block = 0;
453         allocated_blocks = NULL;
454 }