-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdnstun.c
454 lines (420 loc) · 13.2 KB
/
rdnstun.c
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
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <threads.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/icmp6.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include "macro.h"
#include "utils.h"
#include "log.h"
#include "iface.h"
#include "chain.h"
#include "threadname.h"
#include "rdnstun.h"
volatile bool rdnstun_shutdown = false;
static void shutdown_rdnstun (int sig) {
(void) sig;
LOG(LOG_LEVEL_INFO, "Shutting down " RDNSTUN_NAME);
rdnstun_shutdown = true;
}
static int rdnstun (
int tunfd, const struct HostChain *v4_chains,
const struct HostChain *v6_chains, volatile bool *shutdown) {
threadname_format("fd %d", tunfd);
struct pollfd pollfd = {.fd = tunfd, .events = POLLIN};
while (1) {
int pollres = poll(&pollfd, 1, RDNSTUN_SLEEP_TIME * 1000);
break_if_fail (!*shutdown);
should (pollres >= 0) otherwise {
LOG_PERROR(LOG_LEVEL_WARNING, "poll()");
continue;
}
continue_if_not (pollres > 0);
if (LOG_WOULD_LOG(LOG_LEVEL_DEBUG)) {
puts("");
}
// data from tun/tap: read it
unsigned char packet[IP_MAXPACKET];
int pkt_receive_len = read(tunfd, packet, sizeof(packet));
should (pkt_receive_len >= 0) otherwise {
LOG_PERROR(LOG_LEVEL_WARNING, "read() failed");
}
continue_if_fail (pkt_receive_len > 0);
LOG(LOG_LEVEL_DEBUG, "Read %d bytes from fd %d", pkt_receive_len, tunfd);
unsigned short pkt_send_len = pkt_receive_len;
unsigned char ipver = ((struct ip *) packet)->ip_v;
switch (ipver) {
int ret;
case 4:
goto_if_fail (v4_chains != NULL) undefined_ipver;
goto_nonzero (HostChain4Array_reply(
v4_chains, packet, &pkt_send_len)) fail_reply;
break;
case 6:
goto_if_fail (v6_chains != NULL) undefined_ipver;
goto_nonzero (HostChain6Array_reply(
v6_chains, packet, &pkt_send_len)) fail_reply;
break;
default:
LOG(LOG_LEVEL_DEBUG, "Unknown IP version %d", ipver);
if (0) {
undefined_ipver:
LOG(LOG_LEVEL_DEBUG,
"Received IPv%d packet but no IPv%d chains defined",
ipver, ipver);
}
if (0) {
fail_reply:
switch (ret) {
case 17:
LOG(LOG_LEVEL_DEBUG, "No host to reply");
break;
case 18:
LOG(LOG_LEVEL_WARNING, "Received packet with TTL 0");
break;
case 19:
LOG(LOG_LEVEL_WARNING, "Host TTL too small, this is a bug");
break;
default:
LOG(LOG_LEVEL_WARNING, "Unknown error number %d", ret);
}
}
continue;
}
// write it into the tun/tap interface
if likely (pkt_send_len > 0) {
int n_write = write(tunfd, packet, pkt_send_len);
if unlikely (n_write < 0) {
LOG_PERROR(LOG_LEVEL_WARNING, "write() failed");
} else {
LOG(LOG_LEVEL_DEBUG, "Write %d bytes to fd %d", n_write, tunfd);
}
}
}
return 0;
}
struct RDnsTunArg {
int tunfd;
const struct HostChain *v4_chains;
const struct HostChain *v6_chains;
volatile bool *shutdown;
};
static int start_rdnstun (void *arg) {
struct RDnsTunArg *rdnstun_arg = arg;
return rdnstun(rdnstun_arg->tunfd, rdnstun_arg->v4_chains,
rdnstun_arg->v6_chains, rdnstun_arg->shutdown);
}
static void usage (const char *progname) {
fprintf(stderr, "Usage: %s [OPTIONS]... [<iface>]\n", progname);
fputs(
"\n"
"Address chain may be composed by the following tokens, separated by comma:\n"
" <addr>[-<addr>] Host address(es)\n"
" ttl=<ttl> TTL for fake host(s), default: 64\n"
" mtu=<mtu> MTU for fake host(s), default: 1500\n"
" Packets large than <mtu> will be silently dropped.\n"
" route=<network> Route for this chain, default: 0/0\n"
" If multiple chains with same route, it is undefined which\n"
" chain will be selected.\n"
" TTL and MTU will take effect until next TTL/MTU specification.\n"
"\n"
" -4 <v4addr_chain> IPv4 address chain\n"
" -6 <v6addr_chain> IPv6 address chain\n"
" -E <step>/<prefix>,<n> duplicates the previous chain by <n>, with interval of\n"
" <step>*2^<prefix>. All route and hosts will be shifted\n"
" -T <nthread> run <nthread> threads (0 for `nproc')\n"
" If <iface> is a persist tun device, it must be\n"
" created using multi_queue.\n"
" -D daemonize (run in background)\n"
" -d enables debugging messages\n"
" -h prints this help text\n", stderr);
}
int main (int argc, char *argv[]) {
if (argc <= 1) {
usage(argv[0]);
return EXIT_SUCCESS;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wanalyzer-write-to-const"
LOGGER_SET_ATTRIBUTE(domain, RDNSTUN_NAME);
LOGGER_SET_ATTRIBUTE(backtrace, true);
LOGGER_SET_ATTRIBUTE(debug, true);
#pragma GCC diagnostic pop
diagnose_sigsegv(true, 0, NULL);
struct HostChain *v4_chains = NULL;
struct HostChain *v6_chains = NULL;
unsigned int v4_chains_len = 0;
unsigned int v6_chains_len = 0;
char if_name[IF_NAMESIZE] = RDNSTUN_IFACE_NAME;
int nthread = -1;
bool background = false;
// Parse command line options
bool if_name_set = false;
bool last_chain_v6 = false;
for (int option; (option = getopt(argc, argv, "-4:6:E:T:Ddh")) != -1;) {
int ret;
switch (option) {
case 1:
should (!if_name_set) otherwise {
fprintf(stderr, "error: too many positional options\n");
goto fail_arg;
}
if_name_set = true;
should (strnlen(optarg, sizeof(if_name)) < sizeof(if_name)) otherwise {
fprintf(stderr, "error: iface name '%s' too long\n", optarg);
goto fail_arg;
}
strcpy(if_name, optarg);
break;
case '4': {
test_goto (irealloc(
&v4_chains, sizeof(struct HostChain) * (v4_chains_len + 2)
) != NULL, -1) fail_chain;
goto_nonzero (
HostChain_init(v4_chains + v4_chains_len, optarg, false)) fail_chain;
v4_chains_len++;
last_chain_v6 = false;
break;
}
case '6': {
test_goto (irealloc(
&v6_chains, sizeof(struct HostChain) * (v6_chains_len + 2)
) != NULL, -1) fail_chain;
goto_nonzero (
HostChain_init(v6_chains + v6_chains_len, optarg, true)) fail_chain;
v6_chains_len++;
last_chain_v6 = true;
break;
}
case 'E': {
test_goto (
(last_chain_v6 ? v6_chains_len : v4_chains_len) > 0, 1
) fail_duplicate;
char *step_end = strchr(optarg, '/');
char *prefix_end = strchr(optarg, ',');
test_goto (step_end != NULL && prefix_end != NULL, 2) fail_duplicate;
*step_end = '\0';
*prefix_end = '\0';
int step;
int prefix;
int n;
bool parsed_int =
argtoi(optarg, &step, 1, SHRT_MAX) == 0 &&
argtoi(step_end + 1, &prefix, 1, last_chain_v6 ? 128 : 32) == 0 &&
argtoi(prefix_end + 1, &n, 0, INT_MAX) == 0;
*step_end = '/';
*prefix_end = ',';
test_goto (parsed_int, 2) fail_duplicate;
test_goto (irealloc(
(void **) (last_chain_v6 ? &v6_chains : &v4_chains),
sizeof(struct HostChain) * (
(last_chain_v6 ? v6_chains_len : v4_chains_len) + n + 1)
) != NULL, -1) fail_duplicate;
struct HostChain *base = last_chain_v6 ?
v6_chains + v6_chains_len - 1 : v4_chains + v4_chains_len - 1;
test_goto (prefix <= base->prefix, 3) fail_duplicate;
const int af = last_chain_v6 ? AF_INET6 : AF_INET;
for (int i = 1; i <= n; i++) {
struct HostChain *self = base + i;
goto_nonzero (HostChain_copy(self, base)) fail_duplicate;
HostChain_shift(self, step * i, prefix);
if (LOG_WOULD_LOG(LOG_LEVEL_DEBUG)) {
char s_network[INET6_ADDRSTRLEN];
inet_ntop(af, self->network, s_network, sizeof(s_network));
LOG(LOG_LEVEL_DEBUG, "Duplicate # %d chain: %s/%d",
i, s_network, self->prefix);
}
if (last_chain_v6) {
v6_chains_len++;
} else {
v4_chains_len++;
}
}
break;
}
case 'T':
should (argtoi(optarg, &nthread, 0, 1024) == 0) otherwise {
fprintf(stderr, "error: number of threads not a positive number\n");
goto fail_arg;
}
if (nthread == 0) {
nthread = sysconf(_SC_NPROCESSORS_ONLN);
if (nthread <= 0) {
nthread = 1;
}
}
break;
case 'D':
background = true;
break;
case 'd':
LOGGER_SET_ATTRIBUTE(level, LOG_LEVEL_DEBUG);
break;
case 'h':
usage(argv[0]);
goto end;
default:
// getopt already print error for us
// fprintf(stderr, "error: unknown option '%c'\n", option);
if (0) {
const char *msg;
if (0) {
fail_chain:
msg = HostChain_strerror(ret);
}
if (0) {
fail_duplicate:
switch (ret) {
case 1:
msg = "'E' must be specified after a chain";
break;
case 2:
msg = "malformed duplication specification";
break;
case 3:
msg = "'prefix' must be less or equal than the prefix of "
"previous chain";
break;
default:
msg = Struct_strerror(ret);
}
}
should (msg != NULL) otherwise {
msg = "unknown error";
}
fprintf(stderr, "error when parsing '%s': %s\n", optarg, msg);
}
fail_arg:
goto fail;
}
}
should (v4_chains_len != 0 || v6_chains_len != 0) otherwise {
fprintf(stderr, "error: must specify at least one chain\n");
goto fail;
}
if (v4_chains != NULL) {
if (v4_chains_len == 0) {
free(v4_chains);
v4_chains = NULL;
} else {
memset(v4_chains + v4_chains_len, 0, sizeof(struct HostChain));
HostChainArray_sort(v4_chains);
}
}
if (v6_chains != NULL) {
if (v6_chains_len == 0) {
free(v6_chains);
v4_chains = NULL;
} else {
memset(v6_chains + v6_chains_len, 0, sizeof(struct HostChain));
HostChainArray_sort(v6_chains);
}
}
{
// initialize tun/tap interface
bool multithread = nthread > 0;
nthread = max(nthread, 1);
int tunfds[nthread];
if (!multithread) {
tunfds[0] = tun_alloc(if_name, IFF_TUN);
goto_if_fail (tunfds[0] >= 0) fail;
} else {
switch (tuns_alloc(if_name, IFF_TUN, nthread, tunfds)) {
case 0:
break;
case 2:
fprintf(stderr, "error: device type mismatch\n");
goto fail;
case 3:
fprintf(stderr, "error: device does not support multiqueue, but "
"multithread required\n");
goto fail;
default:
goto fail;
}
}
LOG(LOG_LEVEL_INFO, "Successfully connected to interface %s", if_name);
should (ifup(if_name) >= 0) otherwise {
LOG(LOG_LEVEL_NOTICE, "Failed to bring up interface %s", if_name);
}
// daemonize
if (background) {
should (daemon(0, 0) == 0) otherwise {
perror("daemon");
goto fail_tun;
}
}
// main loop
if (!background) {
LOGEVENT (LOG_LEVEL_NOTICE) {
LOGEVENT_PUTS("Start " RDNSTUN_NAME);
if (multithread) {
LOGEVENT_LOG(" with %d thread(s)", nthread);
}
}
}
signal(SIGINT, shutdown_rdnstun);
if (nthread == 1) {
char name[THREADNAME_SIZE];
threadname_get(name, sizeof(name));
rdnstun(tunfds[0], v4_chains, v6_chains, &rdnstun_shutdown);
threadname_set(name);
close(tunfds[0]);
} else {
thrd_t threads[nthread];
struct RDnsTunArg args[nthread];
for (int i = 0; i < nthread; i++) {
args[i].tunfd = tunfds[i];
args[i].v4_chains = v4_chains;
args[i].v6_chains = v6_chains;
args[i].shutdown = &rdnstun_shutdown;
should (thrd_create(
&threads[i], start_rdnstun, args + i) == 0) otherwise {
perror("thrd_create");
rdnstun_shutdown = true;
for (i--; i >= 0; i--) {
thrd_join(threads[i], NULL);
}
goto fail_tun;
}
}
for (int i = 0; i < nthread; i++) {
thrd_join(threads[i], NULL);
close(tunfds[i]);
}
}
if (0) {
fail_tun:
for (int i = 0; i < nthread; i++) {
close(tunfds[i]);
}
goto fail;
}
}
int ret;
end:
ret = EXIT_SUCCESS;
if (0) {
fail:
ret = EXIT_FAILURE;
}
if (v4_chains != NULL) {
HostChainArray_destroy_size(v4_chains, v4_chains_len);
free(v4_chains);
}
if (v6_chains != NULL) {
HostChainArray_destroy_size(v6_chains, v6_chains_len);
free(v6_chains);
}
return ret;
}