summaryrefslogtreecommitdiff
path: root/dynamic.c
blob: 830b22787ab2d44fc2940d3059dbc05836181a8e (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
/*
 *  dynamic.c -- dynamic address mapper
 *
 *  part of TAYGA <http://www.litech.org/tayga/>
 *  Copyright (C) 2010  Nathan Lutchansky <lutchann@litech.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 */

#include <tayga.h>

#define MAP_FILE	"dynamic.map"
#define TMP_MAP_FILE	"dynamic.map~~"

extern struct config *gcfg;
extern time_t now;

static struct map_dynamic *alloc_map_dynamic(const struct in6_addr *addr6,
		const struct in_addr *addr4, struct free_addr *f)
{
	struct map_dynamic *d;
	uint32_t a = ntohl(addr4->s_addr);

	d = (struct map_dynamic *)malloc(sizeof(struct map_dynamic));
	if (!d) {
		slog(LOG_CRIT, "Unable to allocate memory\n");
		return NULL;
	}
	memset(d, 0, sizeof(struct map_dynamic));
	INIT_LIST_HEAD(&d->list);

	d->map4.type = MAP_TYPE_DYNAMIC_HOST;
	d->map4.addr = *addr4;
	d->map4.prefix_len = 32;
	calc_ip4_mask(&d->map4.mask, NULL, 32);
	INIT_LIST_HEAD(&d->map4.list);

	d->map6.type = MAP_TYPE_DYNAMIC_HOST;
	d->map6.addr = *addr6;
	d->map6.prefix_len = 128;
	calc_ip6_mask(&d->map6.mask, NULL, 128);
	INIT_LIST_HEAD(&d->map6.list);

	d->free.addr = a;
	d->free.count = f->count - (a - f->addr);
	f->count = a - f->addr - 1;
	INIT_LIST_HEAD(&d->free.list);
	list_add(&d->free.list, &f->list);

	return d;
}

static void move_to_mapped(struct map_dynamic *d, struct dynamic_pool *pool)
{
	insert_map4(&d->map4, NULL);
	insert_map6(&d->map6, NULL);
	list_add(&d->list, &pool->mapped_list);
}

static void move_to_dormant(struct map_dynamic *d, struct dynamic_pool *pool)
{
	struct list_head *entry;
	struct map_dynamic *s;

	list_del(&d->map4.list);
	list_del(&d->map6.list);

	if (list_empty(&pool->dormant_list)) {
		list_add_tail(&d->list, &pool->dormant_list);
		return;
	}

	s = list_entry(pool->dormant_list.prev, struct map_dynamic, list);
	if (s->last_use >= d->last_use) {
		list_add_tail(&d->list, &pool->dormant_list);
		return;
	}

	list_for_each(entry, &pool->dormant_list) {
		s = list_entry(entry, struct map_dynamic, list);
		if (s->last_use < d->last_use)
			break;
	}
	list_add_tail(&d->list, entry);
}

static void print_dyn_change(char *str, struct map_dynamic *d)
{
	char addrbuf4[INET_ADDRSTRLEN];
	char addrbuf6[INET6_ADDRSTRLEN];

	inet_ntop(AF_INET, &d->map4.addr, addrbuf4, sizeof(addrbuf4));
	inet_ntop(AF_INET6, &d->map6.addr, addrbuf6, sizeof(addrbuf6));
	slog(LOG_DEBUG, "%s pool address %s (%s)\n", str, addrbuf4, addrbuf6);
}

struct map6 *assign_dynamic(const struct in6_addr *addr6)
{
	struct dynamic_pool *pool;
	struct list_head *entry;
	struct free_addr *f;
	uint32_t i, addr, base, max;
	struct in_addr addr4;
	struct map4 *m4;
	struct map_dynamic *d;

	pool = gcfg->dynamic_pool;
	if (!pool)
		return NULL;

	list_for_each(entry, &pool->dormant_list) {
		d = list_entry(entry, struct map_dynamic, list);
		if (IN6_ARE_ADDR_EQUAL(addr6, &d->map6.addr)) {
			print_dyn_change("reactivated dormant", d);
			goto activate;
		}
	}

	base = 0;
	max = (1 << (32 - pool->map4.prefix_len)) - 1;

	for (i = 0; i < 4; ++i) {
		base += addr6->s6_addr32[i];
		while (base & ~max)
			base = (base & max) +
				(base >> (32 - pool->map4.prefix_len));
	}

	for (i = 0, entry = NULL; i <= max; ++i) {
		addr = pool->free_head.addr | ((base + i) & max);
		if (!entry || addr == pool->free_head.addr)
			entry = pool->free_list.next;
		for (;;) {
			f = list_entry(entry, struct free_addr, list);
			if (f->addr + f->count >= addr)
				break;
			entry = entry->next;
		}
		if (f->addr >= addr)
			continue;
		addr4.s_addr = htonl(addr);
		m4 = find_map4(&addr4);
		if (m4 == &pool->map4) {
			d = alloc_map_dynamic(addr6, &addr4, f);
			if (!d)
				return NULL;
			print_dyn_change("assigned new", d);
			gcfg->map_write_pending = 1;
			goto activate;
		}
	}

	if (list_empty(&pool->dormant_list))
		return NULL;

	d = list_entry(pool->dormant_list.prev, struct map_dynamic, list);
	d->map6.addr = *addr6;
	print_dyn_change("reassigned dormant", d);
	gcfg->map_write_pending = 1;

activate:
	move_to_mapped(d, pool);
	return &d->map6;
}

static void load_map(struct dynamic_pool *pool, const struct in6_addr *addr6,
		const struct in_addr *addr4, long int last_use)
{
	struct list_head *entry;
	struct free_addr *f;
	uint32_t addr;
	struct map4 *m4;
	struct map_dynamic *d;
	char addrbuf4[INET_ADDRSTRLEN];
	char addrbuf6[INET6_ADDRSTRLEN];

	if (pool->map4.addr.s_addr != (addr4->s_addr &
					pool->map4.mask.s_addr)) {
		inet_ntop(AF_INET, addr4, addrbuf4, sizeof(addrbuf4));
		slog(LOG_NOTICE, "Ignoring map for %s from %s/%s that lies "
				"outside dynamic pool prefix\n", addrbuf4,
				gcfg->data_dir, MAP_FILE);
		return;
	}
	m4 = find_map4(addr4);
	if (m4 != &pool->map4) {
		inet_ntop(AF_INET, addr4, addrbuf4, sizeof(addrbuf4));
		slog(LOG_NOTICE, "Ignoring map for %s from %s/%s that "
				"conflicts with statically-configured map\n",
				addrbuf4, gcfg->data_dir, MAP_FILE);
		return;
	}
	if (validate_ip6_addr(addr6) < 0) {
		inet_ntop(AF_INET, addr4, addrbuf4, sizeof(addrbuf4));
		inet_ntop(AF_INET6, addr6, addrbuf6, sizeof(addrbuf6));
		slog(LOG_NOTICE, "Ignoring map for %s from %s/%s with "
				"invalid IPv6 address %s\n", addrbuf4,
				gcfg->data_dir, MAP_FILE, addrbuf6);
		return;
	}
	if (find_map6(addr6)) {
		inet_ntop(AF_INET6, addr6, addrbuf6, sizeof(addrbuf6));
		slog(LOG_NOTICE, "Ignoring map for %s from %s/%s that "
				"conflicts with statically-configured map\n",
				addrbuf6, gcfg->data_dir, MAP_FILE);
		return;
	}
	addr = ntohl(addr4->s_addr);
	list_for_each(entry, &pool->free_list) {
		f = list_entry(entry, struct free_addr, list);
		if (f->addr + f->count >= addr)
			break;
	}
	if (entry == &pool->free_list || f->addr >= addr) {
		inet_ntop(AF_INET, addr4, addrbuf4, sizeof(addrbuf4));
		slog(LOG_NOTICE, "Ignoring duplicate map for %s from %s/%s\n",
				addrbuf4, gcfg->data_dir, MAP_FILE);
		return;
	}
	d = alloc_map_dynamic(addr6, addr4, f);
	if (!d)
		return;
	d->last_use = last_use;
	move_to_dormant(d, pool);
}

void load_dynamic(struct dynamic_pool *pool)
{
	FILE *in;
	char line[512];
	char *s4, *s6, *stime, *end, *tokptr;
	struct in_addr addr4;
	struct in6_addr addr6;
	long int last_use;
	struct list_head *entry;
	struct map_dynamic *d;
	int count = 0;

	in = fopen(MAP_FILE, "r");
	if (!in) {
		if (errno != ENOENT)
			slog(LOG_ERR, "Unable to open %s/%s, ignoring: %s\n",
					gcfg->data_dir, MAP_FILE,
					strerror(errno));
		return;
	}
	while (fgets(line, sizeof(line), in)) {
		if (strlen(line) + 1 == sizeof(line)) {
			slog(LOG_ERR, "Ignoring oversized line in %s/%s\n",
					gcfg->data_dir, MAP_FILE);
			continue;
		}
		s4 = strtok_r(line, DELIM, &tokptr);
		if (!s4 || *s4 == '#')
			continue;
		s6 = strtok_r(NULL, DELIM, &tokptr);
		if (!s6)
			goto malformed;
		stime = strtok_r(NULL, DELIM, &tokptr);
		if (!stime)
			goto malformed;
		end = strtok_r(NULL, DELIM, &tokptr);
		if (end)
			goto malformed;
		if (!inet_pton(AF_INET, s4, &addr4) ||
				!inet_pton(AF_INET6, s6, &addr6))
			goto malformed;
		last_use = strtol(stime, &end, 10);
		if (last_use <= 0 || *end)
			goto malformed;
		load_map(pool, &addr6, &addr4, last_use);
		continue;
malformed:
		slog(LOG_ERR, "Ignoring malformed line in %s/%s\n",
				gcfg->data_dir, MAP_FILE);
	}
	fclose(in);

	time(&now);
	last_use = 0;
	list_for_each(entry, &pool->dormant_list) {
		d = list_entry(entry, struct map_dynamic, list);
		if (d->last_use > last_use)
			last_use = d->last_use;
		++count;
	}
	slog(LOG_INFO, "Loaded %d dynamic %s from %s/%s\n", count,
			count == 1 ? "map" : "maps",
			gcfg->data_dir, MAP_FILE);
	if (last_use > now) {
		slog(LOG_DEBUG, "Note: maps in %s/%s are dated in the future\n",
				gcfg->data_dir, MAP_FILE);
		list_for_each(entry, &pool->dormant_list) {
			d = list_entry(entry, struct map_dynamic, list);
			d->last_use = now - gcfg->dyn_min_lease;
		}
	} else {
		while (!list_empty(&pool->dormant_list)) {
			d = list_entry(pool->dormant_list.next,
					struct map_dynamic, list);
			if (d->last_use + gcfg->dyn_min_lease < now)
				break;
			move_to_mapped(d, pool);
		}
	}
}

static void write_to_file(struct dynamic_pool *pool)
{
	FILE *out;
	struct list_head *entry;
	struct map_dynamic *d;
	char addrbuf4[INET_ADDRSTRLEN];
	char addrbuf6[INET6_ADDRSTRLEN];

	out = fopen(TMP_MAP_FILE, "w");
	if (!out) {
		slog(LOG_ERR, "Unable to open %s/%s for writing: %s\n",
				gcfg->data_dir, TMP_MAP_FILE,
				strerror(errno));
		return;
	}
	fprintf(out, "###\n###\n### TAYGA dynamic map database\n###\n"
			"### You can edit this (carefully!) as long as "
			"you shut down TAYGA first\n###\n###\n"
			"### Last written: %s###\n###\n\n",
			asctime(gmtime(&now)));
	entry = pool->mapped_list.next;
	while (entry != &pool->dormant_list) {
		if (entry == &pool->mapped_list) {
			entry = pool->dormant_list.next;
			continue;
		}
		d = list_entry(entry, struct map_dynamic, list);
		inet_ntop(AF_INET, &d->map4.addr, addrbuf4, sizeof(addrbuf4));
		inet_ntop(AF_INET6, &d->map6.addr, addrbuf6, sizeof(addrbuf6));
		fprintf(out, "%s\t%s\t%ld\n", addrbuf4, addrbuf6,
				d->cache_entry ?
					d->cache_entry->last_use : d->last_use);
		entry = entry->next;
	}
	fclose(out);
	if (rename(TMP_MAP_FILE, MAP_FILE) < 0) {
		slog(LOG_ERR, "Unable to rename %s/%s to %s/%s: %s\n",
				gcfg->data_dir, TMP_MAP_FILE,
				gcfg->data_dir, MAP_FILE,
				strerror(errno));
		unlink(TMP_MAP_FILE);
	}
}

void dynamic_maint(struct dynamic_pool *pool, int shutdown)
{
	struct list_head *entry, *next;
	struct map_dynamic *d;
	struct free_addr *f;

	list_for_each_safe(entry, next, &pool->mapped_list) {
		d = list_entry(entry, struct map_dynamic, list);
		if (d->cache_entry)
			continue;
		if (d->last_use + gcfg->dyn_min_lease < now) {
			print_dyn_change("unmapped dormant", d);
			move_to_dormant(d, pool);
		}
	}
	while (!list_empty(&pool->dormant_list)) {
		d = list_entry(pool->dormant_list.prev,
				struct map_dynamic, list);
		if (d->last_use + gcfg->dyn_max_lease >= now)
			break;
		f = list_entry(d->free.list.prev, struct free_addr, list);
		f->count += d->free.count + 1;
		list_del(&d->free.list);
		list_del(&d->list);
		free(d);
	}
	if (gcfg->data_dir[0]) {
		if (shutdown || gcfg->map_write_pending ||
				gcfg->last_map_write +
					gcfg->max_commit_delay < now ||
				gcfg->last_map_write > now) {
			write_to_file(pool);
			gcfg->last_map_write = now;
			gcfg->map_write_pending = 0;
		}
	}
}