forked from mc36/freeRtr
/
tapInt.c
225 lines (205 loc) · 6.17 KB
/
tapInt.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include "utils.h"
char *ifaceName;
char *ifaceQuot;
int ifaceSock;
struct sockaddr_in addrLoc;
struct sockaddr_in addrRem;
int portLoc;
int portRem;
int commSock;
pthread_t threadUdp;
pthread_t threadTap;
pthread_t threadPrint;
long int byteRx;
long int packRx;
long int byteTx;
long int packTx;
void err(char*buf) {
printf("%s\n", buf);
_exit(1);
}
void doTapLoop() {
unsigned char bufD[16384];
int bufS;
for (;;) {
bufS = sizeof (bufD);
bufS = read(ifaceSock, bufD, bufS);
if (bufS < 0) break;
packRx++;
byteRx += bufS;
send(commSock, bufD, bufS, 0);
}
err("tap thread exited");
}
void doUdpLoop() {
unsigned char bufD[16384];
int bufS;
for (;;) {
bufS = sizeof (bufD);
bufS = recv(commSock, bufD, bufS, 0);
if (bufS < 0) break;
packTx++;
byteTx += bufS;
if (write(ifaceSock, bufD, bufS) < 0) break;
}
err("udp thread exited");
}
void doMainLoop() {
unsigned char buf[1024];
doer:
printf("> ");
buf[0] = 0;
int i = scanf("%1023s", buf);
if (i < 1) {
sleep(1);
goto doer;
}
switch (buf[0]) {
case 0:
goto doer;
break;
case 'H':
case 'h':
case '?':
printf("commands:\n");
printf("h - this help\n");
printf("q - exit process\n");
printf("d - display counters\n");
printf("c - clear counters\n");
break;
case 'Q':
case 'q':
err("exiting");
break;
case 'D':
case 'd':
printf("iface counters:\n");
printf(" packets bytes\n");
printf("received %20li %20li\n", packRx, byteRx);
printf("sent %20li %20li\n", packTx, byteTx);
break;
case 'C':
case 'c':
printf("counters cleared.\n");
byteRx = 0;
packRx = 0;
byteTx = 0;
packTx = 0;
break;
default:
printf("unknown command '%s', try ?\n", buf);
break;
}
printf("\n");
goto doer;
}
void doCmd(char *cmd) {
printf("running %s...\n", cmd);
if (system(cmd) < 0) err("error executing command");
}
char* quoteString(char*src) {
int o = strlen(src);
if (o > 128) err("parameter too long");
char* res=malloc((o*2) + 2);
if (res == NULL) err("error allocating memory");
for (int i=0; i<o; i++) {
res[(i*2)+0] = '\\';
res[(i*2)+1] = src[i];
}
res[o*2] = 0;
return res;
}
int main(int argc, char **argv) {
if (argc < 6) {
if (argc <= 1) goto help;
char*curr = argv[1];
if ((curr[0] == '-') || (curr[0] == '/')) curr++;
switch (curr[0]) {
case 'V':
case 'v':
err("tap interface driver v1.0\n");
break;
case '?':
case 'h':
case 'H':
help :
curr = argv[0];
printf("using: %s <iface> <lport> <raddr> <rport> <laddr> [addr/mask] [gw]\n", curr);
printf(" or: %s <command>\n", curr);
printf("commands: v=version\n");
printf(" h=this help\n");
_exit(1);
break;
default:
err("unknown command, try -h");
break;
}
_exit(1);
}
portLoc = atoi(argv[2]);
portRem = atoi(argv[4]);
memset(&addrLoc, 0, sizeof (addrLoc));
memset(&addrRem, 0, sizeof (addrRem));
if (inet_aton(argv[3], &addrRem.sin_addr) == 0) err("bad raddr address");
if (inet_aton(argv[5], &addrLoc.sin_addr) == 0) err("bad laddr address");
addrLoc.sin_family = AF_INET;
addrLoc.sin_port = htons(portLoc);
addrRem.sin_family = AF_INET;
addrRem.sin_port = htons(portRem);
if ((commSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) err("unable to open socket");
if (bind(commSock, (struct sockaddr *) &addrLoc, sizeof (addrLoc)) < 0) err("failed to bind socket");
printf("binded to local port %s %i.\n", inet_ntoa(addrLoc.sin_addr), portLoc);
if (connect(commSock, (struct sockaddr *) &addrRem, sizeof (addrRem)) < 0) err("failed to connect socket");
printf("will send to %s %i.\n", inet_ntoa(addrRem.sin_addr), portRem);
int sockOpt = 524288;
if (setsockopt(commSock, SOL_SOCKET, SO_RCVBUF, &sockOpt, sizeof(sockOpt)) < 0) err("failed to set socket rxbuf");
if (setsockopt(commSock, SOL_SOCKET, SO_SNDBUF, &sockOpt, sizeof(sockOpt)) < 0) err("failed to set socket txbuf");
ifaceName = malloc(strlen(argv[1]) + 1);
if (ifaceName == NULL) err("error allocating memory");
strcpy(ifaceName, argv[1]);
ifaceQuot = quoteString(ifaceName);
printf("creating interface %s.\n", ifaceName);
if ((ifaceSock = open("/dev/net/tun", O_RDWR)) < 0) err("unable to open interface");
struct ifreq ifr;
memset(&ifr, 0, sizeof (ifr));
strcpy(ifr.ifr_name, ifaceName);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl(ifaceSock, TUNSETIFF, &ifr) < 0) err("unable to create interface");
char buf[1024];
sprintf(buf, "ip link set %s address 00:00:%02x:%02x:%02x:%02x mtu 1500", ifaceQuot, portLoc >> 8, portLoc & 0xff, portRem >> 8, portRem & 0xff);
doCmd(buf);
sprintf(buf, "ip link set %s up", ifaceQuot);
doCmd(buf);
if (argc > 6) {
sprintf(buf, "ip addr add %s dev %s", quoteString(argv[6]), ifaceQuot);
doCmd(buf);
sprintf(buf, "echo 0 > /proc/sys/net/ipv6/conf/%s/disable_ipv6", ifaceQuot);
doCmd(buf);
}
if (argc > 7) {
sprintf(buf, "ip route add 0.0.0.0/0 via %s dev %s", quoteString(argv[7]), ifaceQuot);
doCmd(buf);
}
printf("serving others\n");
byteRx = 0;
packRx = 0;
byteTx = 0;
packTx = 0;
if (pthread_create(&threadTap, NULL, (void*) & doTapLoop, NULL)) err("error creating tap thread");
if (pthread_create(&threadUdp, NULL, (void*) & doUdpLoop, NULL)) err("error creating udp thread");
doMainLoop();
}