forked from getlantern/tunio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtun2io.c
More file actions
724 lines (579 loc) · 16.4 KB
/
tun2io.c
File metadata and controls
724 lines (579 loc) · 16.4 KB
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
* Copyright (C) Ambroz Bizjak <ambrop7@gmail.com>
* Contributions:
* Transparent DNS: Copyright (C) Kerem Hadimli <kerem.hadimli@gmail.com>
*
* 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.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 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 AUTHOR 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.
*/
#include "tun2io.h"
// IP address of netif
BIPAddr netif_ipaddr;
// netmask of netif
BIPAddr netif_netmask;
// IP6 address of netif
struct ipv6_addr netif_ip6addr;
// reactor
BReactor ss;
// set to 1 by terminate
int quitting;
// TUN device
BTap device;
// device write buffer
uint8_t *device_write_buf;
// device reading
SinglePacketBuffer device_read_buffer;
PacketPassInterface device_read_interface;
// TCP timer
BTimer tcp_timer;
// job for initializing lwip
BPending lwip_init_job;
// lwip netif
int have_netif;
struct netif netif;
// lwip TCP listener
struct tcp_pcb *listener;
// lwip TCP/IPv6 listener
struct tcp_pcb *listener_ip6;
static int configure(char *tundev, char *ipaddr, char *netmask)
{
// open standard streams
open_standard_streams();
// parse command-line arguments
options.logger = LOGGER_STDOUT;
#ifndef BADVPN_USE_WINAPI
options.logger_syslog_facility = "daemon";
options.logger_syslog_ident = "tun2io";
#endif
options.loglevel = -1;
options.tundev = tundev;
options.netif_ipaddr = ipaddr;
options.netif_netmask = netmask;
// initialize logger
switch (options.logger) {
case LOGGER_STDOUT:
BLog_InitStdout();
break;
#ifndef BADVPN_USE_WINAPI
case LOGGER_SYSLOG:
if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
fprintf(stderr, "Failed to initialize syslog logger\n");
DebugObjectGlobal_Finish();
return 1;
}
break;
#endif
default:
ASSERT(0);
}
// resolve netif ipaddr
if (!BIPAddr_Resolve(&netif_ipaddr, options.netif_ipaddr, 0)) {
BLog(BLOG_ERROR, "netif ipaddr: BIPAddr_Resolve failed");
return 0;
}
if (netif_ipaddr.type != BADDR_TYPE_IPV4) {
BLog(BLOG_ERROR, "netif ipaddr: must be an IPv4 address");
return 0;
}
// resolve netif netmask
if (!BIPAddr_Resolve(&netif_netmask, options.netif_netmask, 0)) {
BLog(BLOG_ERROR, "netif netmask: BIPAddr_Resolve failed");
return 0;
}
if (netif_netmask.type != BADDR_TYPE_IPV4) {
BLog(BLOG_ERROR, "netif netmask: must be an IPv4 address");
return 0;
}
// parse IP6 address
if (options.netif_ip6addr) {
if (!ipaddr6_parse_ipv6_addr(options.netif_ip6addr, &netif_ip6addr)) {
BLog(BLOG_ERROR, "netif ip6addr: incorrect");
return 0;
}
}
return setup_listener(options);
}
static int setup_listener (options_t options)
{
BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
// initialize network
if (!BNetwork_GlobalInit()) {
BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
BLog_Free();
return 1;
}
// init time
BTime_Init();
// init reactor
if (!BReactor_Init(&ss)) {
BLog(BLOG_ERROR, "BReactor_Init failed");
BLog_Free();
return 1;
}
// set not quitting
quitting = 0;
// setup signal handler
if (!BSignal_Init(&ss, signal_handler, NULL)) {
BLog(BLOG_ERROR, "BSignal_Init failed");
BReactor_Free(&ss);
return 1;
}
// init TUN device
if (!BTap_Init(&device, &ss, options.tundev, device_error_handler, NULL, 1)) {
BLog(BLOG_ERROR, "BTap_Init failed");
BSignal_Finish();
return 1;
}
// NOTE: the order of the following is important:
// first device writing must evaluate,
// then lwip (so it can send packets to the device),
// then device reading (so it can pass received packets to lwip).
// init device reading
PacketPassInterface_Init(&device_read_interface, BTap_GetMTU(&device), device_read_handler_send, NULL, BReactor_PendingGroup(&ss));
if (!SinglePacketBuffer_Init(&device_read_buffer, BTap_GetOutput(&device), &device_read_interface, BReactor_PendingGroup(&ss))) {
BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
PacketPassInterface_Free(&device_read_interface);
BTap_Free(&device);
return 1;
}
// init lwip init job
BPending_Init(&lwip_init_job, BReactor_PendingGroup(&ss), lwip_init_job_hadler, NULL);
BPending_Set(&lwip_init_job);
// init device write buffer
if (!(device_write_buf = (uint8_t *)BAlloc(BTap_GetMTU(&device)))) {
BLog(BLOG_ERROR, "BAlloc failed");
BPending_Free(&lwip_init_job);
return 1;
}
// init TCP timer
// it won't trigger before lwip is initialized, becuase the lwip init is a job
BTimer_Init(&tcp_timer, TCP_TMR_INTERVAL, tcp_timer_handler, NULL);
BReactor_SetTimer(&ss, &tcp_timer);
// set no netif
have_netif = 0;
// set no listener
listener = NULL;
listener_ip6 = NULL;
// enter event loop
BLog(BLOG_NOTICE, "entering event loop");
BReactor_Exec(&ss);
BLog(BLOG_NOTICE, "exiting event loop");
// free listener
if (listener_ip6) {
tcp_close(listener_ip6);
}
if (listener) {
tcp_close(listener);
}
// free netif
if (have_netif) {
netif_remove(&netif);
}
BReactor_RemoveTimer(&ss, &tcp_timer);
BFree(device_write_buf);
return 0;
}
void terminate (void)
{
if (!quitting) {
BLog(BLOG_NOTICE, "tearing down");
// set quitting
quitting = 1;
// exit event loop
BReactor_Quit(&ss, 1);
}
}
void signal_handler (void *unused)
{
ASSERT(!quitting)
BLog(BLOG_NOTICE, "termination requested");
terminate();
}
BAddr baddr_from_lwip (int is_ipv6, const ipX_addr_t *ipx_addr, uint16_t port_hostorder)
{
BAddr addr;
if (is_ipv6) {
BAddr_InitIPv6(&addr, (uint8_t *)ipx_addr->ip6.addr, hton16(port_hostorder));
} else {
BAddr_InitIPv4(&addr, ipx_addr->ip4.addr, hton16(port_hostorder));
}
return addr;
}
void lwip_init_job_hadler (void *unused)
{
ASSERT(!quitting)
ASSERT(netif_ipaddr.type == BADDR_TYPE_IPV4)
ASSERT(netif_netmask.type == BADDR_TYPE_IPV4)
ASSERT(!have_netif)
ASSERT(!listener)
ASSERT(!listener_ip6)
BLog(BLOG_DEBUG, "lwip init");
// NOTE: the device may fail during this, but there's no harm in not checking
// for that at every step
// init lwip
lwip_init();
// make addresses for netif
ip_addr_t addr;
addr.addr = netif_ipaddr.ipv4;
ip_addr_t netmask;
netmask.addr = netif_netmask.ipv4;
ip_addr_t gw;
ip_addr_set_any(&gw);
// init netif
if (!netif_add(&netif, &addr, &netmask, &gw, NULL, netif_init_func, netif_input_func)) {
BLog(BLOG_ERROR, "netif_add failed");
terminate();
return;
}
have_netif = 1;
// set netif up
netif_set_up(&netif);
// set netif pretend TCP
netif_set_pretend_tcp(&netif, 1);
// set netif default
netif_set_default(&netif);
if (options.netif_ip6addr) {
// add IPv6 address
memcpy(netif_ip6_addr(&netif, 0), netif_ip6addr.bytes, sizeof(netif_ip6addr.bytes));
netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_VALID);
}
// init listener
struct tcp_pcb *l = tcp_new();
if (!l) {
BLog(BLOG_ERROR, "tcp_new failed");
terminate();
return;
}
// bind listener
if (tcp_bind_to_netif(l, "ho0") != ERR_OK) {
BLog(BLOG_ERROR, "tcp_bind_to_netif failed");
tcp_close(l);
terminate();
return;
}
// listen listener
if (!(listener = tcp_listen(l))) {
BLog(BLOG_ERROR, "tcp_listen failed");
tcp_close(l);
terminate();
return;
}
// setup listener accept handler
tcp_accept(listener, listener_accept_func);
if (options.netif_ip6addr) {
struct tcp_pcb *l_ip6 = tcp_new_ip6();
if (!l_ip6) {
BLog(BLOG_ERROR, "tcp_new_ip6 failed");
terminate();
return;
}
if (tcp_bind_to_netif(l_ip6, "ho0") != ERR_OK) {
BLog(BLOG_ERROR, "tcp_bind_to_netif failed");
tcp_close(l_ip6);
terminate();
return;
}
if (!(listener_ip6 = tcp_listen(l_ip6))) {
BLog(BLOG_ERROR, "tcp_listen failed");
tcp_close(l_ip6);
terminate();
return;
}
tcp_accept(listener_ip6, listener_accept_func);
}
BLog(BLOG_NOTICE, "lwip_init_job_hadler");
}
void tcp_timer_handler (void *unused)
{
ASSERT(!quitting)
BLog(BLOG_DEBUG, "TCP timer");
// schedule next timer
// TODO: calculate timeout so we don't drift
BReactor_SetTimer(&ss, &tcp_timer);
tcp_tmr();
return;
}
void device_error_handler (void *unused)
{
ASSERT(!quitting)
BLog(BLOG_ERROR, "device error");
terminate();
return;
}
void device_read_handler_send (void *unused, uint8_t *data, int data_len)
{
ASSERT(!quitting)
ASSERT(data_len >= 0)
// accept packet
PacketPassInterface_Done(&device_read_interface);
// obtain pbuf
if (data_len > UINT16_MAX) {
BLog(BLOG_WARNING, "device read: packet too large");
free(data);
return;
}
struct pbuf *p = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL);
if (!p) {
BLog(BLOG_WARNING, "device read: pbuf_alloc failed");
free(data);
return;
}
// write packet to pbuf
ASSERT_FORCE(pbuf_take(p, data, data_len) == ERR_OK)
// pass pbuf to input
if (netif.input(p, &netif) != ERR_OK) {
BLog(BLOG_WARNING, "device read: input failed");
pbuf_free(p);
}
}
err_t netif_init_func (struct netif *netif)
{
BLog(BLOG_DEBUG, "netif func init");
netif->name[0] = 'h';
netif->name[1] = 'o';
netif->output = netif_output_func;
netif->output_ip6 = netif_output_ip6_func;
return ERR_OK;
}
err_t netif_output_func (struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
{
return common_netif_output(netif, p);
}
err_t netif_output_ip6_func (struct netif *netif, struct pbuf *p, ip6_addr_t *ipaddr)
{
return common_netif_output(netif, p);
}
err_t common_netif_output (struct netif *netif, struct pbuf *p)
{
BLog(BLOG_DEBUG, "device write: send packet");
if (quitting) {
return ERR_OK;
}
// if there is just one chunk, send it directly, else via buffer
if (!p->next) {
if (p->len > BTap_GetMTU(&device)) {
BLog(BLOG_WARNING, "netif func output: no space left");
return ERR_OK;
}
BTap_Send(&device, (uint8_t *)p->payload, p->len);
} else {
int len = 0;
do {
if (p->len > BTap_GetMTU(&device) - len) {
BLog(BLOG_WARNING, "netif func output: no space left");
return ERR_OK;
}
memcpy(device_write_buf + len, p->payload, p->len);
len += p->len;
} while (p = p->next);
BTap_Send(&device, device_write_buf, len);
}
return ERR_OK;
}
err_t netif_input_func (struct pbuf *p, struct netif *inp)
{
uint8_t ip_version = 0;
if (p->len > 0) {
ip_version = (((uint8_t *)p->payload)[0] >> 4);
}
switch (ip_version) {
case 4: {
return ip_input(p, inp);
} break;
case 6: {
if (options.netif_ip6addr) {
return ip6_input(p, inp);
}
} break;
}
pbuf_free(p);
return ERR_OK;
}
err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err)
{
ASSERT(err == ERR_OK)
// signal accepted
struct tcp_pcb *this_listener = (PCB_ISIPV6(newpcb) ? listener_ip6 : listener);
tcp_accepted(this_listener);
// allocate client structure
struct tcp_client *client = (struct tcp_client *)malloc(sizeof(*client));
if (!client) {
BLog(BLOG_ERROR, "listener accept: malloc failed");
return ERR_MEM;
}
// read addresses
client->local_addr = baddr_from_lwip(PCB_ISIPV6(newpcb), &newpcb->local_ip, newpcb->local_port);
client->remote_addr = baddr_from_lwip(PCB_ISIPV6(newpcb), &newpcb->remote_ip, newpcb->remote_port);
// get destination address
BAddr addr = client->local_addr;
#ifdef OVERRIDE_DEST_ADDR
ASSERT_FORCE(BAddr_Parse2(&addr, OVERRIDE_DEST_ADDR, NULL, 0, 1))
#endif
// Init Go tunnel.
client->tunnel_id = 0;
#ifdef CGO
switch (client->remote_addr.type) {
case BADDR_TYPE_IPV4:
client->tunnel_id = goNewTunnel(client);
if (client->tunnel_id == 0) {
BLog(BLOG_ERROR, "could not create new tunnel.");
return ERR_MEM;
}
err_t err = goInitTunnel(client->tunnel_id);
if (err != ERR_OK) {
BLog(BLOG_ERROR, "could not initialize tunnel.");
return ERR_ABRT;
}
break;
}
#endif
// set pcb
client->pcb = newpcb;
// set client not closed
client->client_closed = 0;
// setup handler argument
tcp_arg(client->pcb, client);
// setup handlers
tcp_err(client->pcb, client_err_func);
tcp_recv(client->pcb, client_recv_func);
tcp_sent(client->pcb, client_sent_func);
// setup buffer
client->buf_used = 0;
return ERR_OK;
}
static void client_handle_freed_client(struct tcp_client *client)
{
if (client == NULL) {
return;
}
err_t err = goTunnelDestroy(client->tunnel_id);
if (err == ERR_OK) {
client->client_closed = 1;
client->tunnel_id = 0;
free(client);
}
}
void client_close(struct tcp_client *client)
{
if (client == NULL) {
return;
}
if (!client->client_closed) {
client_free_client(client);
}
}
static void client_free_client (struct tcp_client *client)
{
ASSERT(!client->client_closed)
// remove callbacks
tcp_err(client->pcb, NULL);
tcp_recv(client->pcb, NULL);
tcp_sent(client->pcb, NULL);
// free pcb
err_t err = tcp_close(client->pcb);
if (err != ERR_OK) {
tcp_abort(client->pcb);
}
client_handle_freed_client(client);
}
void client_err_func (void *arg, err_t err)
{
struct tcp_client *client = (struct tcp_client *)arg;
ASSERT(!client->client_closed)
client_handle_freed_client(client);
}
static err_t client_recv_func(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct tcp_client *client = (struct tcp_client *)arg;
if (client->client_closed) {
return ERR_ABRT;
}
if (err != ERR_OK) {
return ERR_ABRT;
}
if (!p) {
client_free_client(client);
return ERR_ABRT;
}
ASSERT(p->tot_len > 0)
err_t werr;
werr = goTunnelWrite(client->tunnel_id, p->payload, p->len);
if (werr == ERR_OK) {
tcp_recved(client->pcb, p->len);
}
pbuf_free(p);
return werr;
}
err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct tcp_client *client = (struct tcp_client *)arg;
ASSERT(!client->client_closed)
ASSERT(len > 0)
if (client == NULL) {
return ERR_ABRT;
}
if (client->client_closed) {
return ERR_ABRT;
}
return goTunnelSentACK(client->tunnel_id, len);
}
// dump_dest_addr dumps the client's local address into an string.
static char *dump_dest_addr(struct tcp_client *client) {
char *addr;
addr = malloc(sizeof(char)*BADDR_MAX_ADDR_LEN);
BAddr_Print(&client->local_addr, addr);
return addr;
}
static unsigned int tcp_client_sndbuf(struct tcp_client *client) {
return (unsigned int)tcp_sndbuf(client->pcb);
}
static void client_dealloc (struct tcp_client *client)
{
ASSERT(client->client_closed)
free(client);
}
static void client_abort_client (struct tcp_client *client)
{
ASSERT(!client->client_closed)
// remove callbacks
tcp_err(client->pcb, NULL);
tcp_recv(client->pcb, NULL);
tcp_sent(client->pcb, NULL);
// free pcb
tcp_abort(client->pcb);
client_handle_freed_client(client);
}
static int tcp_client_output(struct tcp_client *client) {
if (client == NULL) {
return ERR_ABRT;
}
if (client->client_closed) {
return ERR_ABRT;
}
if (client->pcb) {
err_t err = tcp_output(client->pcb);
if (err != ERR_OK) {
return -1;
}
}
return ERR_OK;
}