forked from d2fl/d2fl
/
simpletun.c
415 lines (354 loc) · 12.4 KB
/
simpletun.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
/**************************************************************************
* simpletun.c *
* *
* A simplistic, simple-minded, naive tunnelling program using tun/tap *
* interfaces and TCP. DO NOT USE THIS PROGRAM FOR SERIOUS PURPOSES. *
* *
* You have been warned. *
* *
* (C) 2010 Davide Brini. *
* *
* DISCLAIMER AND WARNING: this is all work in progress. The code is *
* ugly, the algorithms are naive, error checking and input validation *
* are very basic, and of course there can be bugs. If that's not enough, *
* the program has not been thoroughly tested, so it might even fail at *
* the few simple things it should be supposed to do right. *
* Needless to say, I take no responsibility whatsoever for what the *
* program might do. The program has been written mostly for learning *
* purposes, and can be used in the hope that is useful, but everything *
* is to be taken "as is" and without any kind of warranty, implicit or *
* explicit. See the file LICENSE for further details. *
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <stdarg.h>
#include "packet.h"
#include "debug.h"
#ifdef DEBUG
#include<execinfo.h>
#include<signal.h>
#endif
/* buffer for reading from tun/tap interface, must be >= 1500 */
#define BUFSIZE 2000
#define CLIENT 0
#define SERVER 1
#define PORT 55555
#define UP_LIMIT 10
int debug;
char *progname;
#ifdef DEBUG
void debug_backtrace(int dunno)
{
#define SIZE 256
void* array[SIZE];
int size,i = 0;
char **strings;
if(dunno == SIGSEGV){
fprintf(stderr, "\r\nSegmentation faul\r\n");
size = backtrace(array,SIZE);
fprintf(stderr, "\r\nBacktrace (%d deep):\r\n",size);
strings = backtrace_symbols(array,size);
for(;i < size; i++){
fprintf(stderr, "%d:%s\n",i, strings[i]);
}
free(strings);
exit(-1);
}
}
#endif
/**************************************************************************
* tun_alloc: allocates or reconnects to a tun/tap device. The caller *
* must reserve enough space in *dev. *
**************************************************************************/
char local_mac_addr[] = {0x00,0xff,0xec,0xbb,0x1d,0xe3};
char local_ip_addr[] = "192.168.59.129";
char gate_mac_addr[] = {0x00,0x0c,0x29,0xa1,0x6c,0xbc};
char twohopnei_ip_addr[] = "192.168.58.132";
int tun_alloc(char *dev, int flags) {
struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";
if( (fd = open(clonedev , O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
perror("ioctl(TUNSETIFF)");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
/**************************************************************************
* cread: read routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cread(int fd, char *buf, int n){
int nread;
if((nread=read(fd, buf, n)) < 0){
perror("Reading data");
exit(1);
}
return nread;
}
/**************************************************************************
* cwrite: write routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cwrite(int fd, char *buf, int n){
int nwrite;
if((nwrite=write(fd, buf, n)) < 0){
perror("Writing data");
exit(1);
}
return nwrite;
}
/**************************************************************************
* read_n: ensures we read exactly n bytes, and puts them into "buf". *
* (unless EOF, of course) *
**************************************************************************/
int read_n(int fd, char *buf, int n) {
int nread, left = n;
while(left > 0) {
if ((nread = cread(fd, buf, left)) == 0){
return 0 ;
}else {
left -= nread;
buf += nread;
}
}
return n;
}
/**************************************************************************
* do_debug: prints debugging stuff (doh!) *
**************************************************************************/
void do_debug(char *msg, ...){
va_list argp;
//if(debug) {
//va_start(argp, msg);
//vfprintf(stderr, msg, argp);
//va_end(argp);
//}
}
/**************************************************************************
* my_err: prints custom error messages on stderr. *
**************************************************************************/
void my_err(char *msg, ...) {
va_list argp;
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
/**************************************************************************
* usage: prints usage and exits. *
**************************************************************************/
void usage(void) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s -i <ifacename> [-s|-c <serverIP>] [-p <port>] [-u|-a] [-d]\n", progname);
fprintf(stderr, "%s -h\n", progname);
fprintf(stderr, "\n");
fprintf(stderr, "-i <ifacename>: Name of interface to use (mandatory)\n");
fprintf(stderr, "-s|-c <serverIP>: run in server mode (-s), or specify server address (-c <serverIP>) (mandatory)\n");
fprintf(stderr, "-p <port>: port to listen on (if run in server mode) or to connect to (in client mode), default 55555\n");
fprintf(stderr, "-u|-a: use TUN (-u, default) or TAP (-a)\n");
fprintf(stderr, "-d: outputs debug information while running\n");
fprintf(stderr, "-h: prints this help text\n");
exit(1);
}
extern unsigned int send_counter;
int main(int argc, char *argv[]) {
#ifdef DEBUG
signal(SIGSEGV,debug_backtrace);
#endif
printf("begin!\n");
int tap_fd, tap_fd1, option;
int dummy = 0;
int flags = IFF_TUN;
char if_name0[IFNAMSIZ] = "tap0";
char if_name1[IFNAMSIZ] = "tap1";
int maxfd;
uint16_t nread, nwrite, plength;
char buffer[BUFSIZE];
struct sockaddr_in local, remote;
char remote_ip[16] = ""; /* dotted quad IP string */
unsigned short int port = PORT;
int sock_fd, net_fd, optval = 1;
socklen_t remotelen;
int cliserv = -1; /* must be specified on cmd line */
unsigned long int tap2net = 0, net2tap = 0;
struct in_addr addr;
struct host twohopneis[10];
inet_aton(twohopnei_ip_addr,&addr);
twohopneis[0].ip_addr = addr.s_addr;
memcpy(twohopneis[0].mac_addr,gate_mac_addr, ETHER_ADDR_LEN);
struct host localhost;
inet_aton(local_ip_addr,&addr);
localhost.ip_addr = addr.s_addr;
memcpy(localhost.mac_addr, local_mac_addr, ETHER_ADDR_LEN);
progname = argv[0];
/* Check command line options */
while((option = getopt(argc, argv, "i:sc:p:uahd")) > 0) {
switch(option) {
case 'd':
printf("Enter dummy mode\n");
dummy = 1;
break;
case 'h':
usage();
break;
case 'i':
strncpy(if_name0,optarg, IFNAMSIZ-1);
break;
case 's':
cliserv = SERVER;
break;
case 'c':
cliserv = CLIENT;
strncpy(remote_ip,optarg,15);
break;
case 'p':
port = atoi(optarg);
break;
case 'u':
flags = IFF_TUN;
break;
case 'a':
flags = IFF_TAP;
break;
default:
my_err("Unknown option %c\n", option);
usage();
}
}
argv += optind;
argc -= optind;
if(argc > 0) {
my_err("Too many options!\n");
usage();
}
if(*if_name0 == '\0') {
my_err("Must specify interface name!\n");
usage();
} else if(cliserv < 0) {
my_err("Must specify client or server mode!\n");
usage();
} else if((cliserv == CLIENT)&&(*remote_ip == '\0')) {
my_err("Must specify server address!\n");
usage();
}
printf("allocate tap device!\n");
/* initialize tun/tap interface */
if ( (tap_fd = tun_alloc(if_name0, flags | IFF_NO_PI)) < 0 ) {
my_err("Error connecting to tun/tap interface %s!\n", if_name0);
exit(1);
}
if ( (tap_fd1 = tun_alloc(if_name1, flags | IFF_NO_PI)) < 0 ) {
my_err("Error connecting to tun1/tap1 interface %s!\n", if_name1);
exit(1);
}
printf("allocate tap device success!\n");
//do_debug("Successfully connected to interface %s\n", if_name0);
/* use select() to handle two descriptors at once */
maxfd = (tap_fd > tap_fd1)?tap_fd:tap_fd1;
while(1) {
int ret;
fd_set rd_set;
FD_ZERO(&rd_set);
FD_SET(tap_fd, &rd_set); FD_SET(tap_fd1, &rd_set);
ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
if (ret < 0 && errno == EINTR){
do_debug("ret error");
continue;
}
if (ret < 0) {
perror("select()");
exit(1);
}
if(FD_ISSET(tap_fd, &rd_set)) {
/* data from tun/tap: just read it and write it to the network */
DEBUGMSG(2,"\n--Send a packet--\n");
nread = cread(tap_fd, buffer, BUFSIZE);
//append the trailer
if(!dummy){
if(isIP((unsigned char*)buffer) && isTCP((unsigned char*)buffer)){
++send_counter;
if(send_counter >= UP_LIMIT){
const int len = 14 + sizeof(struct ip) + 4;
unsigned char signal[14 + sizeof(struct ip) + 4] = {0};
ConstructSignal(signal, &(twohopneis[0]), &localhost);
DEBUGMSG(1,"Epoch end and send a signal\n");
nwrite = write(tap_fd1, signal, len);
send_counter = 0;
}
int newLen = extendPacket(TCP, (unsigned char*)buffer);
nread = newLen;
}
}
//DEBUGMSG(2,"route a packet(len: %i) to tap1\n", nread);
nwrite = write(tap_fd1, buffer, nread);
//DEBUGMSG(2,"Written %d bytes to the network\n", nwrite);
}
if(FD_ISSET(tap_fd1, &rd_set)) {
/* data from tun/tap: just read it and write it to the network */
nread = cread(tap_fd1, buffer, BUFSIZE);
/* write length + packet */
plength = htons(nread);
if(!dummy){
if(isEpochEnd((unsigned char*)buffer)){
DEBUGMSG(1,"Receive a Epoch end signal\n");
const int len = 14 + sizeof(struct ip) + 40;
unsigned char echobuffer [14 + sizeof(struct ip) + 4] = {0};
ConstructSignal(echobuffer, (unsigned char*)buffer);
nwrite = write(tap_fd1, echobuffer, len);
continue;
}
if(isIP((unsigned char*)buffer) && isTCP((unsigned char*)buffer)){
int newLen = 0;
verifyPacket(TCP, (unsigned char*)buffer, &newLen);
nread = newLen;
}
DEBUGMSG(2,"packet len: %i\n", nread);
}
nwrite = write(tap_fd, buffer, nread);
do_debug("TAP2NET %lu: Written %d bytes to the network\n", tap2net, nwrite);
}
//if(FD_ISSET(net_fd, &rd_set)) {
if(0) {
/* data from the network: read it, and write it to the tun/tap interface.
* We need to read the length first, and then the packet */
/* Read length */
nread = read_n(net_fd, (char *)&plength, sizeof(plength));
if(nread == 0) {
/* ctrl-c at the other end */
break;
}
net2tap++;
/* read packet */
nread = read_n(net_fd, buffer, ntohs(plength));
do_debug("NET2TAP %lu: Read %d bytes from the network\n", net2tap, nread);
/* now buffer[] contains a full packet or frame, write it into the tun/tap interface */
nwrite = cwrite(tap_fd, buffer, nread);
do_debug("NET2TAP %lu: Written %d bytes to the tap interface\n", net2tap, nwrite);
}
}
return(0);
}