/
tunnat.c
299 lines (257 loc) · 7 KB
/
tunnat.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/ip.h>
#include <sys/types.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>
/* buffer for reading from tun/tap interface, must be >= 1500 */
#define BUFSIZE 2000
int debug;
char *progname;
static int tun_ifup(const char *ifname)
{
struct ifreq ifr;
int sk, err;
sk = socket(PF_INET, SOCK_DGRAM, 0);
if (sk < 0)
return -errno;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
err = -errno;
goto done;
}
if (ifr.ifr_flags & IFF_UP) {
err = -EALREADY;
goto done;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
err = -errno;
goto done;
}
err = 0;
done:
close(sk);
return err;
}
/**************************************************************************
* tun_alloc: allocates or reconnects to a tun/tap device. The caller *
* must reserve enough space in *dev. *
**************************************************************************/
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);
if (err = tun_ifup(ifr.ifr_name)) {
perror("ifup");
return err;
}
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);
}
unsigned short ip_checksum(unsigned short *buf, unsigned int len)
{
unsigned long csum = 0;
while (len > 1) {
csum += *buf++;
len -= sizeof(unsigned short);
}
if (len) {
csum += *(unsigned char *)buf;
}
csum = (csum >> 16) + (csum & 0xffff);
csum += (csum >> 16);
return (unsigned short)(~csum);
}
int main(int argc, char *argv[])
{
int tap_fd, option;
int flags = IFF_TUN;
char if_name[IFNAMSIZ] = "";
int maxfd;
uint16_t nread, nwrite ;
char buffer[BUFSIZE];
char from_ip[16] = "", to_ip[16] = "";
progname = argv[0];
while((option = getopt(argc, argv, "i:xo:m:uahd")) > 0) {
switch(option) {
case 'd':
debug = 1;
break;
case 'h':
usage();
break;
case 'i':
strncpy(if_name,optarg, IFNAMSIZ-1);
break;
case 'o':
strncpy(from_ip,optarg,15);
break;
case 'm':
strncpy(to_ip,optarg,15);
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_name == '\0') {
my_err("Must specify interface name!\n");
usage();
}
if ((tap_fd = tun_alloc(if_name, IFF_TUN | IFF_NO_PI)) < 0) {
my_err("Error connecting to tun/tap interface %s!\n", if_name);
exit(1);
}
do_debug("Successfully connected to interface %s\n", if_name);
/* use select() to handle two descriptors at once */
maxfd = tap_fd;
while (1) {
int ret;
fd_set rd_set;
FD_ZERO(&rd_set);
FD_SET(tap_fd, &rd_set);
ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
if (ret < 0 && errno == EINTR) {
continue;
}
if (ret < 0) {
perror("select()");
exit(1);
}
if (FD_ISSET(tap_fd, &rd_set)) {
struct iphdr *iph;
/* data from tun/tap: just read it and write it to the network */
unsigned int addr1 = inet_addr(from_ip);
unsigned int addr2 = inet_addr(to_ip);
nread = cread(tap_fd, buffer, BUFSIZE);
iph = (struct iphdr *)buffer;
if (addr2 == iph->daddr) {
iph->daddr = addr1;
} else if (addr1 == iph->saddr) {
iph->saddr = addr2;
} else {
continue;
}
iph->check = 0;
iph->check = ip_checksum((unsigned short *)iph, 20);
nwrite = cwrite(tap_fd, buffer, nread);
}
}
return(0);
}