summaryrefslogtreecommitdiff
path: root/src/ippool.c
blob: bb8568d9c808280cb59b0f4b8b9871431917214f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
 *  Copyright (C) 2012-2014  BMW Car IT GmbH.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/socket.h>

#include "connman.h"

struct address_info {
	int index;
	uint32_t start;
	uint32_t end;

	unsigned int use_count;
	struct connman_ippool *pool;
};

struct connman_ippool {
	unsigned int refcount;

	struct address_info *info;

	char *gateway;
	char *broadcast;
	char *start_ip;
	char *end_ip;
	char *subnet_mask;

	ippool_collision_cb_t collision_cb;
	void *user_data;
};

GSList *allocated_blocks;
GHashTable *pool_hash;

static uint32_t last_block;
static uint32_t block_16_bits;
static uint32_t block_20_bits;
static uint32_t block_24_bits;
static uint32_t subnet_mask_24;

struct connman_ippool *
__connman_ippool_ref_debug(struct connman_ippool *pool,
				const char *file, int line, const char *caller)
{
	DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
		file, line, caller);

	__sync_fetch_and_add(&pool->refcount, 1);

	return pool;
}

void __connman_ippool_unref_debug(struct connman_ippool *pool,
				const char *file, int line, const char *caller)
{
	if (!pool)
		return;

	DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
		file, line, caller);

	if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
		return;

	g_hash_table_remove(pool_hash, pool);
}

static char *get_ip(uint32_t ip)
{
	struct in_addr addr;

	addr.s_addr = htonl(ip);

	return g_strdup(inet_ntoa(addr));
}

static uint32_t next_block(uint32_t block)
{
	uint32_t next;

	/*
	 * Return the next IP block within the private IP range
	 *
	 * 16-bit block 192.168.0.0 – 192.168.255.255
	 * 20-bit block  172.16.0.0 –  172.31.255.255
	 * 24-bit block    10.0.0.0 –  10.255.255.255
	 */

	next = (block & 0x0000ff00) >> 8;
	next += 1;

	if (next == 255) {
		if ((block & 0xffff0000) == block_16_bits) {
			/*
			 * Reached the end of the 16 bit block, switch
			 * to the 20-bit block.
			 */
			return block_20_bits;
		}

		if ((block & 0xffff0000) >= block_20_bits) {
			next = (block & 0x00ff0000) >> 16;
			if (next >= 16 && next < 32)
				next += 1;

			if (next == 32) {
				/*
				 * Reached the end of the 20 bit
				 * block, switch to the 24-bit block.
				 */
				return block_24_bits;
			}

			return (block & 0xff000000) |
				((next << 16) & 0x00ff0000);
		}

		if ((block & 0xff000000) == block_24_bits) {
			next = (block & 0x00ff0000) >> 16;
			if (next < 255)
				next += 1;

			if (next == 255) {
				/*
				 * Reached the end of the 24 bit
				 * block, switch to the 16-bit block.
				 */
				return block_16_bits;
			}

			return (block & 0xff000000) |
				((next << 16) & 0x00ff0000);
		}
	}

	return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
}

static uint32_t get_free_block(unsigned int size)
{
	struct address_info *info;
	uint32_t block;
	GSList *list;
	bool collision;

	/*
	 * Instead starting always from the 16 bit block, we start
	 * from the last assigned block. This is a simple optimimazion
	 * for the case where a lot of blocks have been assigned, e.g.
	 * the first half of the private IP pool is in use and a new
	 * we need to find a new block.
	 *
	 * To only thing we have to make sure is that we terminated if
	 * there is no block left.
	 */
	if (last_block == 0)
		block = block_16_bits;
	else
		block = next_block(last_block);

	do {
		collision = false;
		for (list = allocated_blocks; list; list = list->next) {
			info = list->data;

			if (info->start <= block && block <= info->end) {
				collision = true;
				break;
			}
		}

		if (!collision)
			return block;

		block = next_block(block);
	} while (block != last_block);

	return 0;
}

static struct address_info *lookup_info(int index, uint32_t start)
{
	GSList *list;

	for (list = allocated_blocks; list; list = list->next) {
		struct address_info *info = list->data;

		if (info->index == index && info->start == start)
			return info;
	}

	return NULL;
}

static bool is_private_address(uint32_t address)
{
	unsigned int a, b;

	a = (address & 0xff000000) >> 24;
	b = (address & 0x00ff0000) >> 16;

	if (a == 10 || (a == 192 && b == 168) ||
					(a == 172 && (b >= 16 && b <= 31)))
		return true;

	return false;
}

void __connman_ippool_newaddr(int index, const char *address,
				unsigned char prefixlen)
{
	struct address_info *info, *it;
	struct in_addr inp;
	uint32_t start, end, mask;
	GSList *list;

	if (inet_aton(address, &inp) == 0)
		return;

	start = ntohl(inp.s_addr);
	if (!is_private_address(start))
		return;

	if (prefixlen >= 32)
		mask = 0xffffffff;
	else
		mask = ~(0xffffffff >> prefixlen);

	start = start & mask;
	end = start | ~mask;

	info = lookup_info(index, start);
	if (info)
		goto update;

	info = g_try_new0(struct address_info, 1);
	if (!info)
		return;

	info->index = index;
	info->start = start;
	info->end = end;

	allocated_blocks = g_slist_prepend(allocated_blocks, info);

update:
	info->use_count = info->use_count + 1;

	if (info->use_count > 1 || info->pool) {
		/*
		 * We need only to check for the first IP in a block for
		 * collisions.
		 */
		return;
	}

	for (list = allocated_blocks; list; list = list->next) {
		it = list->data;

		if (it == info)
			continue;

		if (!(info->start >= it->start && info->start <= it->end))
			continue;

		if (it->pool && it->pool->collision_cb)
			it->pool->collision_cb(it->pool, it->pool->user_data);

		return;
	}
}

void __connman_ippool_deladdr(int index, const char *address,
				unsigned char prefixlen)
{
	struct address_info *info;
	struct in_addr inp;
	uint32_t start, mask;

	if (inet_aton(address, &inp) == 0)
		return;

	start = ntohl(inp.s_addr);
	if (!is_private_address(start))
		return;

	mask = ~(0xffffffff >> prefixlen);
	start = start & mask;

	info = lookup_info(index, start);
	if (!info) {
		/* In theory this should never happen */
		connman_error("Inconsistent IP pool management (start not found)");
		return;
	}

	info->use_count = info->use_count - 1;
	if (info->pool)
		return;

	if (info->use_count > 0)
		return;

	allocated_blocks = g_slist_remove(allocated_blocks, info);
	g_free(info);
}

struct connman_ippool *__connman_ippool_create(int index,
					unsigned int start,
					unsigned int range,
					ippool_collision_cb_t collision_cb,
					void *user_data)
{
	struct connman_ippool *pool;
	struct address_info *info;
	uint32_t block;

	DBG("");

	/*
	 * The range is at max 255 and we don't support overlapping
	 * blocks.
	 */
	if (start + range > 254) {
		connman_error("IP pool does not support pool size larger than 254");
		return NULL;
	}

	block = get_free_block(start + range);
	if (block == 0) {
		connman_warn("Could not find a free IP block");
		return NULL;
	}

	pool = g_try_new0(struct connman_ippool, 1);
	if (!pool)
		return NULL;

	info = g_try_new0(struct address_info, 1);
	if (!info) {
		g_free(pool);
		return NULL;
	}

	last_block = block;

	info->index = index;
	info->start = block;
	info->end = block + range;

	pool->refcount = 1;
	pool->info = info;
	pool->collision_cb = collision_cb;
	pool->user_data = user_data;

	info->pool = pool;

	if (range == 0)
		range = 1;

	pool->gateway = get_ip(info->start + 1);
	pool->broadcast = get_ip(info->start + 255);
	pool->subnet_mask = get_ip(subnet_mask_24);
	pool->start_ip = get_ip(block + start);
	pool->end_ip = get_ip(block + start + range);

	allocated_blocks = g_slist_prepend(allocated_blocks, info);
	g_hash_table_insert(pool_hash, pool, pool);

	return pool;
}

const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
{
	return pool->gateway;
}

const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
{
	return pool->broadcast;
}

const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
{
	return pool->start_ip;
}

const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
{
	return pool->end_ip;
}

const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
{
	return pool->subnet_mask;
}

static void pool_free(gpointer data)
{
	struct connman_ippool *pool = data;

	if (pool->info) {
		allocated_blocks = g_slist_remove(allocated_blocks, pool->info);
		g_free(pool->info);
	}

	g_free(pool->gateway);
	g_free(pool->broadcast);
	g_free(pool->start_ip);
	g_free(pool->end_ip);
	g_free(pool->subnet_mask);

	g_free(pool);
}

int __connman_ippool_init(void)
{
	DBG("");

	block_16_bits = ntohl(inet_addr("192.168.0.0"));
	block_20_bits = ntohl(inet_addr("172.16.0.0"));
	block_24_bits = ntohl(inet_addr("10.0.0.0"));
	subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));

	pool_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
					pool_free);

	return 0;
}

void __connman_ippool_cleanup(void)
{
	DBG("");

	g_hash_table_destroy(pool_hash);
	pool_hash = NULL;

	g_slist_free_full(allocated_blocks, g_free);
	last_block = 0;
	allocated_blocks = NULL;
}