summaryrefslogtreecommitdiff
path: root/conffile.c
blob: ec6433cdf42405483ce24c2dbcdd5f34d2dd7847 (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 *  conffile.c -- config file parser
 *
 *  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>

struct config *gcfg;

static int parse_prefix(int af, const char *src, void *prefix, int *prefix_len)
{
	char *p, *end;
	long int a;
	int r;

	p = strchr(src, '/');
	if (!p)
		return -1;
	*p = 0;
	a = strtol(p + 1, &end, 10);
	r = *end || !inet_pton(af, src, prefix);
	*p = '/';
	if (r)
		return -1;
	if (a < 0 || a > (af == AF_INET6 ? 128 : 32))
		return -1;

	*prefix_len = a;
	return 0;
}

static struct map_static *alloc_map_static(int ln)
{
	struct map_static *m;

	m = (struct map_static *)malloc(sizeof(struct map_static));
	if (!m) {
		slog(LOG_CRIT, "Unable to allocate config memory\n");
		exit(1);
	}
	memset(m, 0, sizeof(struct map_static));
	m->map4.type = MAP_TYPE_STATIC;
	m->map4.prefix_len = 32;
	calc_ip4_mask(&m->map4.mask, NULL, 32);
	INIT_LIST_HEAD(&m->map4.list);
	m->map6.type = MAP_TYPE_STATIC;
	m->map6.prefix_len = 128;
	calc_ip6_mask(&m->map6.mask, NULL, 128);
	INIT_LIST_HEAD(&m->map6.list);
	m->conffile_lineno = ln;
	return m;
}

static void abort_on_conflict4(char *msg, int ln, struct map4 *old)
{
	char oldaddr[INET_ADDRSTRLEN];
	char oldline[128] = "";
	struct map_static *s;

	if (old->type == MAP_TYPE_STATIC || old->type == MAP_TYPE_RFC6052) {
		s = container_of(old, struct map_static, map4);
		if (s->conffile_lineno)
			sprintf(oldline, " from line %d", s->conffile_lineno);
	}

	inet_ntop(AF_INET, &old->addr, oldaddr, sizeof(oldaddr));
	if (ln)
		slog(LOG_CRIT, "%s on line %d conflicts with earlier "
				"definition of %s/%d%s\n", msg, ln,
				oldaddr, old->prefix_len, oldline);
	else
		slog(LOG_CRIT, "%s conflicts with earlier "
				"definition of %s/%d%s\n", msg,
				oldaddr, old->prefix_len, oldline);
	exit(1);
}

static void abort_on_conflict6(char *msg, int ln, struct map6 *old)
{
	char oldaddr[INET6_ADDRSTRLEN];
	char oldline[128] = "";
	struct map_static *s;

	if (old->type == MAP_TYPE_STATIC || old->type == MAP_TYPE_RFC6052) {
		s = container_of(old, struct map_static, map6);
		if (s->conffile_lineno)
			sprintf(oldline, " from line %d", s->conffile_lineno);
	}

	inet_ntop(AF_INET6, &old->addr, oldaddr, sizeof(oldaddr));
	if (ln)
		slog(LOG_CRIT, "%s on line %d overlaps with earlier "
				"definition of %s/%d%s\n", msg, ln,
				oldaddr, old->prefix_len, oldline);
	else
		slog(LOG_CRIT, "%s overlaps with earlier "
				"definition of %s/%d%s\n", msg,
				oldaddr, old->prefix_len, oldline);
	exit(1);
}

static void config_ipv4_addr(int ln, int arg_count, char **args)
{
	if (gcfg->local_addr4.s_addr) {
		slog(LOG_CRIT, "Error: duplicate ipv4-addr directive on "
				"line %d\n", ln);
		exit(1);
	}
	if (!inet_pton(AF_INET, args[0], &gcfg->local_addr4)) {
		slog(LOG_CRIT, "Expected an IPv4 address but found \"%s\" on "
				"line %d\n", args[0], ln);
		exit(1);
	}
	if (validate_ip4_addr(&gcfg->local_addr4) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in ipv4-addr "
				"directive, aborting...\n", args[0]);
		exit(1);
	}
}

static void config_ipv6_addr(int ln, int arg_count, char **args)
{
	if (gcfg->local_addr6.s6_addr[0]) {
		slog(LOG_CRIT, "Error: duplicate ipv6-addr directive on line "
				"%d\n", ln);
		exit(1);
	}
	if (!inet_pton(AF_INET6, args[0], &gcfg->local_addr6)) {
		slog(LOG_CRIT, "Expected an IPv6 address but found \"%s\" on "
				"line %d\n", args[0], ln);
		exit(1);
	}
	if (validate_ip6_addr(&gcfg->local_addr6) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in ipv6-addr "
				"directive, aborting...\n", args[0]);
		exit(1);
	}
	if (gcfg->local_addr6.s6_addr32[0] == WKPF) {
		slog(LOG_CRIT, "Error: ipv6-addr directive cannot contain an "
				"address in the Well-Known Prefix "
				"(64:ff9b::/96)\n");
		exit(1);
	}
}

static void config_prefix(int ln, int arg_count, char **args)
{
	struct map_static *m;
	struct map6 *m6;

	m = alloc_map_static(ln);
	m->map4.prefix_len = 0;
	m->map4.mask.s_addr = 0;
	m->map4.type = MAP_TYPE_RFC6052;
	m->map6.type = MAP_TYPE_RFC6052;
	m6 = &m->map6;

	if (parse_prefix(AF_INET6, args[0], &m6->addr, &m6->prefix_len) ||
			calc_ip6_mask(&m6->mask, &m6->addr, m6->prefix_len)) {
		slog(LOG_CRIT, "Expected an IPv6 prefix but found \"%s\" on "
				"line %d\n", args[0], ln);
		exit(1);
	}
	if (validate_ip6_addr(&m6->addr) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in prefix "
				"directive, aborting...\n", args[0]);
		exit(1);
	}
	if (m6->prefix_len != 32 && m6->prefix_len != 40 &&
			m6->prefix_len != 48 && m6->prefix_len != 56 &&
			m6->prefix_len != 64 && m6->prefix_len != 96) {
		slog(LOG_CRIT, "NAT prefix length must be 32, 40, 48, 56, 64 "
				"or 96 only, aborting...\n");
		exit(1);
	}
	if (insert_map4(&m->map4, NULL) < 0) {
		slog(LOG_CRIT, "Error: duplicate prefix directive on line %d\n",
				ln);
		exit(1);
	}
	if (insert_map6(&m->map6, &m6) < 0)
		abort_on_conflict6("Error: NAT64 prefix", ln, m6);
}

static void config_tun_device(int ln, int arg_count, char **args)
{
	if (gcfg->tundev[0]) {
		slog(LOG_CRIT, "Error: duplicate tun-device directive on line "
				"%d\n", ln);
		exit(1);
	}
	if (strlen(args[0]) + 1 > sizeof(gcfg->tundev)) {
		slog(LOG_CRIT, "Device name \"%s\" is invalid on line %d\n",
				args[0], ln);
		exit(1);
	}
	strcpy(gcfg->tundev, args[0]);
}

static void config_map(int ln, int arg_count, char **args)
{
	struct map_static *m;
	struct map4 *m4;
	struct map6 *m6;

	m = alloc_map_static(ln);

	if (!inet_pton(AF_INET, args[0], &m->map4.addr)) {
		slog(LOG_CRIT, "Expected an IPv4 address but found \"%s\" on "
				"line %d\n", args[0], ln);
		exit(1);
	}
	if (!inet_pton(AF_INET6, args[1], &m->map6.addr)) {
		slog(LOG_CRIT, "Expected an IPv6 address but found \"%s\" on "
				"line %d\n", args[1], ln);
		exit(1);
	}
	if (validate_ip4_addr(&m->map4.addr) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in map "
				"directive, aborting...\n", args[0]);
		exit(1);
	}
	if (validate_ip6_addr(&m->map6.addr) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in map "
				"directive, aborting...\n", args[1]);
		exit(1);
	}
	if (m->map6.addr.s6_addr32[0] == WKPF) {
		slog(LOG_CRIT, "Cannot create single-host maps inside "
				"64:ff9b::/96, aborting...\n");
		exit(1);
	}
	if (insert_map4(&m->map4, &m4) < 0)
		abort_on_conflict4("Error: IPv4 address in map directive",
				ln, m4);
	if (insert_map6(&m->map6, &m6) < 0)
		abort_on_conflict6("Error: IPv6 address in map directive",
				ln, m6);
}

static void config_dynamic_pool(int ln, int arg_count, char **args)
{
	struct dynamic_pool *pool;
	struct map4 *m4;

	if (gcfg->dynamic_pool) {
		slog(LOG_CRIT, "Error: duplicate dynamic-pool directive on "
				"line %d\n", ln);
		exit(1);
	}

	pool = (struct dynamic_pool *)malloc(sizeof(struct dynamic_pool));
	if (!pool) {
		slog(LOG_CRIT, "Unable to allocate config memory\n");
		exit(1);
	}
	memset(pool, 0, sizeof(struct dynamic_pool));
	INIT_LIST_HEAD(&pool->mapped_list);
	INIT_LIST_HEAD(&pool->dormant_list);
	INIT_LIST_HEAD(&pool->free_list);

	m4 = &pool->map4;
	m4->type = MAP_TYPE_DYNAMIC_POOL;
	INIT_LIST_HEAD(&m4->list);

	if (parse_prefix(AF_INET, args[0], &m4->addr, &m4->prefix_len) ||
			calc_ip4_mask(&m4->mask, &m4->addr, m4->prefix_len)) {
		slog(LOG_CRIT, "Expected an IPv4 prefix but found \"%s\" on "
				"line %d\n", args[0], ln);
		exit(1);
	}
	if (validate_ip4_addr(&m4->addr) < 0) {
		slog(LOG_CRIT, "Cannot use reserved address %s in dynamic-pool "
				"directive, aborting...\n", args[0]);
		exit(1);
	}
	if (m4->prefix_len > 31) {
		slog(LOG_CRIT, "Cannot use a prefix longer than /31 in "
			       "dynamic-pool directive, aborting...\n");
		exit(1);
	}
	if (insert_map4(&pool->map4, &m4) < 0)
		abort_on_conflict4("Error: IPv4 prefix in dynamic-pool "
				"directive", ln, m4);

	pool->free_head.addr = ntohl(m4->addr.s_addr);
	pool->free_head.count = (1 << (32 - m4->prefix_len)) - 1;
	INIT_LIST_HEAD(&pool->free_head.list);
	list_add(&pool->free_head.list, &pool->free_list);

	gcfg->dynamic_pool = pool;
}

static void config_data_dir(int ln, int arg_count, char **args)
{
	if (gcfg->data_dir[0]) {
		slog(LOG_CRIT, "Error: duplicate data-dir directive on line "
				"%d\n", ln);
		exit(1);
	}
	if (args[0][0] != '/') {
		slog(LOG_CRIT, "Error: data-dir must be an absolute path\n");
		exit(1);
	}
	strcpy(gcfg->data_dir, args[0]);
}

static void config_strict_fh(int ln, int arg_count, char **args)
{
	if (!strcasecmp(args[0], "true") || !strcasecmp(args[0], "on") ||
			!strcasecmp(args[0], "1")) {
		gcfg->lazy_frag_hdr = 0;
	} else if (!strcasecmp(args[0], "false") ||
			!strcasecmp(args[0], "off") ||
			!strcasecmp(args[0], "0")) {
		gcfg->lazy_frag_hdr = 1;
	} else {
		slog(LOG_CRIT, "Error: invalid value for strict-frag-hdr\n");
		exit(1);
	}
}

struct {
	char *name;
	void (*config_func)(int ln, int arg_count, char **args);
	int need_args;
} config_directives[] = {
	{ "ipv4-addr", config_ipv4_addr, 1 },
	{ "ipv6-addr", config_ipv6_addr, 1 },
	{ "prefix", config_prefix, 1 },
	{ "tun-device", config_tun_device, 1 },
	{ "map", config_map, 2 },
	{ "dynamic-pool", config_dynamic_pool, 1 },
	{ "data-dir", config_data_dir, 1 },
	{ "strict-frag-hdr", config_strict_fh, 1 },
	{ NULL, NULL, 0 }
};

void read_config(char *conffile)
{
	FILE *in;
	int ln = 0;
	char line[512];
	char addrbuf[128];
	char *c, *tokptr;
#define MAX_ARGS 10
	char *args[MAX_ARGS];
	int arg_count;
	int i;
	struct map_static *m;
	struct map4 *m4;
	struct map6 *m6;

	gcfg = (struct config *)malloc(sizeof(struct config));
	if (!gcfg)
		goto malloc_fail;
	memset(gcfg, 0, sizeof(struct config));
	gcfg->recv_buf_size = 65536 + sizeof(struct tun_pi);
	INIT_LIST_HEAD(&gcfg->map4_list);
	INIT_LIST_HEAD(&gcfg->map6_list);
	gcfg->dyn_min_lease = 7200 + 4 * 60; /* just over two hours */
	gcfg->dyn_max_lease = 14 * 86400;
	gcfg->max_commit_delay = gcfg->dyn_max_lease / 4;
	gcfg->hash_bits = 7;
	gcfg->cache_size = 8192;
	gcfg->allow_ident_gen = 1;
	gcfg->ipv6_offlink_mtu = 1280;
	gcfg->lazy_frag_hdr = 1;
	INIT_LIST_HEAD(&gcfg->cache_pool);
	INIT_LIST_HEAD(&gcfg->cache_active);

	in = fopen(conffile, "r");
	if (!in) {
		slog(LOG_CRIT, "unable to open %s, aborting: %s\n", conffile,
				strerror(errno));
		exit(1);
	}
	while (fgets(line, sizeof(line), in)) {
		++ln;
		if (strlen(line) + 1 == sizeof(line)) {
			slog(LOG_CRIT, "Line %d of %s is too long, "
					"aborting...\n", ln, conffile);
			exit(1);
		}
		arg_count = 0;
		for (;;) {
			c = strtok_r(arg_count ? NULL : line, DELIM, &tokptr);
			if (!c || *c == '#')
				break;
			if (arg_count == MAX_ARGS) {
				slog(LOG_CRIT, "Line %d of %s is too long, "
						"aborting...\n", ln, conffile);
				exit(1);
			}
			args[arg_count++] = c;
		}
		if (arg_count == 0)
			continue;
		for (i = 0; config_directives[i].name; ++i)
			if (!strcasecmp(args[0], config_directives[i].name))
				break;
		if (!config_directives[i].name) {
			slog(LOG_CRIT, "Unknown directive \"%s\" on line %d of "
					"%s, aborting...\n", args[0],
					ln, conffile);
			exit(1);
		}
		--arg_count;
		if (config_directives[i].need_args >= 0 &&
				arg_count != config_directives[i].need_args) {
			slog(LOG_CRIT, "Incorrect number of arguments on "
					"line %d\n", ln);
			exit(1);
		}
		config_directives[i].config_func(ln, arg_count, &args[1]);
	}
	fclose(in);

	if (list_empty(&gcfg->map6_list)) {
		slog(LOG_CRIT, "Error: no translation maps or NAT64 prefix "
				"configured\n");
		exit(1);
	}

	m4 = list_entry(gcfg->map4_list.next, struct map4, list);
	m6 = list_entry(gcfg->map6_list.next, struct map6, list);

	if (m4->type == MAP_TYPE_RFC6052 && m6->type == MAP_TYPE_RFC6052 &&
			!gcfg->allow_ident_gen)
		gcfg->cache_size = 0;

	if (!gcfg->local_addr4.s_addr) {
		slog(LOG_CRIT, "Error: no ipv4-addr directive found\n");
		exit(1);
	}

	m = alloc_map_static(0);
	m->map4.addr = gcfg->local_addr4;
	if (insert_map4(&m->map4, &m4) < 0)
		abort_on_conflict4("Error: ipv4-addr", 0, m4);

	if (gcfg->local_addr6.s6_addr32[0]) {
		m->map6.addr = gcfg->local_addr6;
		if (insert_map6(&m->map6, &m6) < 0) {
			if (m6->type == MAP_TYPE_RFC6052) {
				inet_ntop(AF_INET6, &m6->addr,
						addrbuf, sizeof(addrbuf));
				slog(LOG_CRIT, "Error: ipv6-addr cannot reside "
						"within configured prefix "
						"%s/%d\n", addrbuf,
						m6->prefix_len);
				exit(1);
			} else {
				abort_on_conflict6("Error: ipv6-addr", 0, m6);
			}
		}
	} else {
		m6 = list_entry(gcfg->map6_list.prev, struct map6, list);
		if (m6->type != MAP_TYPE_RFC6052) {
			slog(LOG_CRIT, "Error: ipv6-addr directive must be "
					"specified if no NAT64 prefix is "
					"configured\n");
			exit(1);
		}
		if (append_to_prefix(&gcfg->local_addr6, &gcfg->local_addr4,
					&m6->addr, m6->prefix_len)) {
			slog(LOG_CRIT, "Error: ipv6-addr directive must be "
					"specified if prefix is 64:ff9b::/96 "
					"and ipv4-addr is a non-global "
					"(RFC 1918) address\n");
			exit(1);
		}
		m->map6.addr = gcfg->local_addr6;
	}
	return;

malloc_fail:
	slog(LOG_CRIT, "Unable to allocate config memory\n");
	exit(1);
}