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
|
/*
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include "fastd.h"
#include "peer.h"
#include <config.ll.h>
#include <config.yy.h>
#include <config.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <libgen.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
extern fastd_protocol fastd_protocol_null;
#ifdef WITH_PROTOCOL_ECFXP
extern fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
#endif
static void default_config(fastd_config *conf) {
conf->loglevel = LOG_INFO;
conf->keepalive_interval = 60;
conf->peer_stale_time = 300;
conf->peer_stale_time_temp = 30;
conf->eth_addr_stale_time = 300;
conf->ifname = NULL;
memset(&conf->bind_addr_in, 0, sizeof(struct sockaddr_in));
memset(&conf->bind_addr_in6, 0, sizeof(struct sockaddr_in6));
conf->mtu = 1500;
conf->mode = MODE_TAP;
conf->peer_to_peer = false;
conf->protocol = &fastd_protocol_null;
conf->secret = NULL;
conf->key_valid = 3600; /* 60 minutes */
conf->key_refresh = 3300; /* 55 minutes */
conf->peers = NULL;
conf->on_up = NULL;
conf->on_up_dir = NULL;
}
static bool config_match(const char *opt, ...) {
va_list ap;
bool match = false;
const char *str;
va_start(ap, opt);
while((str = va_arg(ap, const char*)) != NULL) {
if (strcmp(opt, str) == 0) {
match = true;
break;
}
}
va_end(ap);
return match;
}
void fastd_read_config_dir(fastd_context *ctx, fastd_config *conf, const char *dir, int depth) {
if (depth >= MAX_CONFIG_DEPTH)
exit_error(ctx, "maximum config include depth exceeded");
char *oldcwd = get_current_dir_name();
if (chdir(dir))
exit_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno));
DIR *dirh = opendir(".");
if (!dirh)
exit_error(ctx, "opendir for `%s' failed: %s", dir, strerror(errno));
while (true) {
struct dirent entry, *result;
int ret;
ret = readdir_r(dirh, &entry, &result);
if (ret)
exit_error(ctx, "readdir_r: %s", strerror(ret));
if (!result)
break;
if (result->d_name[0] == '.')
continue;
struct stat statbuf;
if (stat(result->d_name, &statbuf)) {
pr_info(ctx, "ignoring file `%s': stat failed: %s", result->d_name, strerror(errno));
continue;
}
if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
pr_info(ctx, "ignoring file `%s': no regular file", result->d_name);
continue;
}
fastd_peer_config_new(ctx, conf);
conf->peers->name = strdup(result->d_name);
fastd_read_config(ctx, conf, result->d_name, true, depth);
}
closedir(dirh);
chdir(oldcwd);
free(oldcwd);
}
void fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filename, bool peer_config, int depth) {
if (depth >= MAX_CONFIG_DEPTH)
exit_error(ctx, "maximum config include depth exceeded");
bool use_stdin = !strcmp(filename, "-");
FILE *file;
if (use_stdin) {
file = stdin;
}
else {
file = fopen(filename, "r");
if (!file)
exit_error(ctx, "can't open config file `%s': %s", filename, strerror(errno));
}
char *oldcwd = get_current_dir_name();
char *filename2 = strdup(filename);
char *dir = dirname(filename2);
if (chdir(dir))
exit_error(ctx, "change from directory `%s' to `%s' failed", oldcwd, dir);
yyscan_t scanner;
fastd_config_yylex_init(&scanner);
fastd_config_yyset_in(file, scanner);
fastd_config_pstate *ps = fastd_config_pstate_new();
int token;
YYSTYPE token_val;
YYLTYPE loc = {1, 0, 1, 0};
if (peer_config)
token = START_PEER_CONFIG;
else
token = START_CONFIG;
while(fastd_config_push_parse(ps, token, &token_val, &loc, ctx, conf, filename, depth+1) == YYPUSH_MORE) {
token = fastd_config_yylex(&token_val, &loc, scanner);
if (token < 0)
exit_error(ctx, "config error: %s at %s:%i:%i", token_val.str, filename, loc.first_line, loc.first_column);
}
fastd_config_pstate_delete(ps);
fastd_config_yylex_destroy(scanner);
chdir(oldcwd);
free(filename2);
free(oldcwd);
if (!use_stdin)
fclose(file);
}
#define IF_OPTION(args...) if(config_match(argv[i], args, NULL) && (++i))
#define IF_OPTION_ARG(args...) if(config_match(argv[i], args, NULL) && ({ \
arg = argv[i+1]; \
i+=2; \
if (i > argc) \
exit_error(ctx, "config error: option `%s' needs an argument", argv[i-2]); \
true; \
}))
#define IGNORE_OPTION (i++)
void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *const argv[]) {
default_config(conf);
fastd_peer_config *peer;
int i = 1;
const char *arg;
long l;
char *charptr;
char *endptr;
char *addrstr;
bool keygen = false;
while (i < argc) {
IF_OPTION_ARG("--log-level") {
if (!strcmp(arg, "fatal"))
conf->loglevel = LOG_FATAL;
else if (!strcmp(arg, "error"))
conf->loglevel = LOG_ERROR;
else if (!strcmp(arg, "warn"))
conf->loglevel = LOG_WARN;
else if (!strcmp(arg, "info"))
conf->loglevel = LOG_INFO;
else if (!strcmp(arg, "verbose"))
conf->loglevel = LOG_VERBOSE;
else if (!strcmp(arg, "debug"))
conf->loglevel = LOG_DEBUG;
else
exit_error(ctx, "invalid mode `%s'", arg);
continue;
}
IF_OPTION_ARG("-c", "--config") {
fastd_read_config(ctx, conf, arg, false, 0);
continue;
}
IF_OPTION_ARG("--config-peer") {
fastd_peer_config_new(ctx, conf);
fastd_read_config(ctx, conf, arg, true, 0);
continue;
}
IF_OPTION_ARG("--config-peer-dir") {
fastd_read_config_dir(ctx, conf, arg, 0);
continue;
}
IF_OPTION_ARG("-i", "--interface") {
free(conf->ifname);
conf->ifname = strdup(arg);
continue;
}
IF_OPTION_ARG("-b", "--bind") {
if (arg[0] == '[') {
charptr = strchr(arg, ']');
if (!charptr || (charptr[1] != ':' && charptr[1] != '\0'))
exit_error(ctx, "invalid bind address `%s'", arg);
addrstr = strndup(arg+1, charptr-arg-1);
if (charptr[1] == ':')
charptr++;
else
charptr = NULL;
}
else {
charptr = strchr(arg, ':');
if (charptr) {
addrstr = strndup(arg, charptr-arg);
}
else {
addrstr = strdup(arg);
}
}
if (charptr) {
l = strtol(charptr+1, &endptr, 10);
if (*endptr || l < 0 || l > 65535)
exit_error(ctx, "invalid bind port `%s'", charptr+1);
}
else {
l = 0;
}
if (strcmp(addrstr, "any") == 0) {
conf->bind_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
conf->bind_addr_in.sin_port = htons(l);
conf->bind_addr_in6.sin6_addr = in6addr_any;
conf->bind_addr_in6.sin6_port = htons(l);
}
else if (arg[0] == '[') {
conf->bind_addr_in6.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, addrstr, &conf->bind_addr_in6.sin6_addr) != 1)
exit_error(ctx, "invalid bind address `%s'", addrstr);
conf->bind_addr_in6.sin6_port = htons(l);
}
else {
conf->bind_addr_in.sin_family = AF_INET;
if (inet_pton(AF_INET, addrstr, &conf->bind_addr_in.sin_addr) != 1)
exit_error(ctx, "invalid bind address `%s'", addrstr);
conf->bind_addr_in.sin_port = htons(l);
}
free(addrstr);
continue;
}
IF_OPTION_ARG("-M", "--mtu") {
conf->mtu = strtol(arg, &endptr, 10);
if (*endptr || conf->mtu < 576)
exit_error(ctx, "invalid mtu `%s'", arg);
continue;
}
IF_OPTION_ARG("-m", "--mode") {
if (!strcmp(arg, "tap"))
conf->mode = MODE_TAP;
else if (!strcmp(arg, "tun"))
conf->mode = MODE_TUN;
else
exit_error(ctx, "invalid mode `%s'", arg);
continue;
}
IF_OPTION_ARG("-P", "--protocol") {
if (!strcmp(arg, "null"))
conf->protocol = &fastd_protocol_null;
#ifdef WITH_PROTOCOL_ECFXP
else if (!strcmp(arg, "ecfxp"))
conf->protocol = &fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
#endif
else
exit_error(ctx, "invalid protocol `%s'", arg);
continue;
}
IF_OPTION_ARG("-p", "--peer") {
peer = fastd_peer_config_new(ctx, conf);
if (strcmp(arg, "float") == 0)
continue;
if (arg[0] == '[') {
charptr = strchr(arg, ']');
if (!charptr || (charptr[1] != ':'))
exit_error(ctx, "invalid peer address `%s'", arg);
addrstr = strndup(arg+1, charptr-arg-1);
charptr++;
}
else {
charptr = strchr(arg, ':');
if (!charptr)
exit_error(ctx, "invalid peer address `%s'", arg);
addrstr = strndup(arg, charptr-arg);
}
l = strtol(charptr+1, &endptr, 10);
if (*endptr || l < 0 || l > 65535)
exit_error(ctx, "invalid peer port `%s'", charptr+1);
if (arg[0] == '[') {
peer->address.in6.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, addrstr, &peer->address.in6.sin6_addr) != 1)
exit_error(ctx, "invalid peer address `%s'", addrstr);
peer->address.in6.sin6_port = htons(l);
}
else {
peer->address.in.sin_family = AF_INET;
if (inet_pton(AF_INET, addrstr, &peer->address.in.sin_addr) != 1)
exit_error(ctx, "invalid peer address `%s'", addrstr);
peer->address.in.sin_port = htons(l);
}
free(addrstr);
continue;
}
IF_OPTION("--peer-to-peer") {
conf->peer_to_peer = true;
continue;
}
IF_OPTION_ARG("--on-up") {
free(conf->on_up);
free(conf->on_up_dir);
conf->on_up = strdup(arg);
conf->on_up_dir = get_current_dir_name();
continue;
}
IF_OPTION("--generate-key") {
keygen = true;
continue;
}
exit_error(ctx, "config error: unknown option `%s'", argv[i]);
}
if (keygen) {
ctx->conf = conf;
conf->protocol->generate_key(ctx);
exit(0);
}
conf->n_floating = 0;
conf->n_v4 = 0;
conf->n_v6 = 0;
for (peer = conf->peers; peer; peer = peer->next) {
switch (peer->address.sa.sa_family) {
case AF_UNSPEC:
conf->n_floating++;
break;
case AF_INET:
conf->n_v4++;
break;
case AF_INET6:
conf->n_v6++;
break;
default:
exit_bug(ctx, "invalid peer address family");
}
}
if (conf->n_floating && conf->bind_addr_in.sin_family == AF_UNSPEC
&& conf->bind_addr_in6.sin6_family == AF_UNSPEC) {
conf->bind_addr_in.sin_family = AF_INET;
conf->bind_addr_in6.sin6_family = AF_INET6;
}
else {
if (conf->n_v4)
conf->bind_addr_in.sin_family = AF_INET;
if (conf->n_v6)
conf->bind_addr_in6.sin6_family = AF_INET6;
}
if (conf->mode == MODE_TUN && (!conf->peers || conf->peers->next))
exit_error(ctx, "config error: for tun mode exactly one peer must be configured");
conf->protocol->init(ctx, conf);
}
|