forked from wangyu-/tinyfecVPN
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtun_dev.cpp
More file actions
Latest commit
339 lines (283 loc) · 7.46 KB
/
Copy pathtun_dev.cpp
File metadata and controls
339 lines (283 loc) · 7.46 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
/*
* tun.cpp
*
* Created on: Oct 26, 2017
* Author: root
*/
#include"tun_dev.h"
my_time_t last_keep_alive_time=0;
intget_tun_fd(char * dev_name)
{
int tun_fd=open("/dev/net/tun",O_RDWR);
if(tun_fd <0)
{
mylog(log_fatal,"open /dev/net/tun failed");
myexit(-1);
}
structifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN|IFF_NO_PI;
strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
if(ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0)
{
mylog(log_fatal,"open /dev/net/tun failed");
myexit(-1);
}
if (persist_tun == 1) {
if (ioctl(tun_fd, TUNSETPERSIST, 1) != 0) {
mylog(log_warn,"failed to set tun persistent");
}
}
return tun_fd;
}
intset_tun(char *if_name,u32_t local_ip,u32_t remote_ip,int mtu)
{
if(manual_set_tun) return0;
//printf("i m here1\n");
structifreq ifr;
structsockaddr_in sai;
memset(&ifr,0,sizeof(ifr));
memset(&sai, 0, sizeof(structsockaddr));
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
sai.sin_family = AF_INET;
sai.sin_port = 0;
sai.sin_addr.s_addr = local_ip;
memcpy(&ifr.ifr_addr,&sai, sizeof(structsockaddr));
assert(ioctl(sockfd, SIOCSIFADDR, &ifr)==0); //set source ip
sai.sin_addr.s_addr = remote_ip;
memcpy(&ifr.ifr_addr,&sai, sizeof(structsockaddr));
assert(ioctl(sockfd, SIOCSIFDSTADDR, &ifr)==0);//set dest ip
ifr.ifr_mtu=mtu;
assert(ioctl(sockfd, SIOCSIFMTU, &ifr)==0);//set mtu
assert(ioctl(sockfd, SIOCGIFFLAGS, &ifr)==0);
// ifr.ifr_flags |= ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST );
ifr.ifr_flags = ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST );//set interface flags
assert(ioctl(sockfd, SIOCSIFFLAGS, &ifr)==0);
//printf("i m here2\n");
return0;
}
intput_header(char header,char * data,int &len)
{
assert(len>=0);
data[len]=header;
len+=1;
return0;
}
intget_header(char &header,char * data,int &len)
{
assert(len>=0);
if(len<1) return -1;
len-=1;
header=data[len];
return0;
}
intfrom_normal_to_fec2(conn_info_t & conn_info,dest_t &dest,char * data,int len,char header)
{
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
from_normal_to_fec(conn_info,data,len,out_n,out_arr,out_len,out_delay);
for(int i=0;i<out_n;i++)
{
char tmp_buf[buf_len];
int tmp_len=out_len[i];
memcpy(tmp_buf,out_arr[i],out_len[i]);
put_header(header,tmp_buf,tmp_len);
delay_send(out_delay[i],dest,tmp_buf,tmp_len);//this is slow but safer.just use this one
//put_header(header,out_arr[i],out_len[i]);//modify in place
//delay_send(out_delay[i],dest,out_arr[i],out_len[i]);//warning this is currently okay,but if you modified fec encoder,you may have to use the above code
}
return0;
}
intfrom_fec_to_normal2(conn_info_t & conn_info,dest_t &dest,char * data,int len)
{
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
from_fec_to_normal(conn_info,data,len,out_n,out_arr,out_len,out_delay);
for(int i=0;i<out_n;i++)
{
#ifndef NOLIMIT
if(program_mode==server_mode)
{
char * tmp_data=out_arr[i];
int tmp_len=out_len[i];
iphdr * iph;
iph = (structiphdr *) tmp_data;
if(tmp_len>=int(sizeof(iphdr))&&iph->version==4)
{
u32_t dest_ip=iph->daddr;
//printf("%s\n",my_ntoa(dest_ip));
if( ( ntohl(sub_net_uint32)&0xFFFFFF00 ) != ( ntohl (dest_ip) &0xFFFFFF00) )
{
string sub=my_ntoa(dest_ip);
string dst=my_ntoa( htonl( ntohl (sub_net_uint32) &0xFFFFFF00) );
mylog(log_warn,"[restriction]packet's dest ip [%s] not in subnet [%s],dropped, maybe you need to compile an un-restricted server\n", sub.c_str(), dst.c_str());
continue;
}
}
}
#endif
delay_send(out_delay[i],dest,out_arr[i],out_len[i]);
}
return0;
}
intdo_mssfix(char * s,int len)//currently only for ipv4
{
if(mssfix==0)
{
return0;
}
if(len<int(sizeof(iphdr)))
{
mylog(log_debug,"packet from tun len=%d <20\n",len);
return -1;
}
iphdr * iph;
iph = (structiphdr *) s;
if(iph->version!=4)
{
//mylog(log_trace,"not ipv4");
return0;
}
if(iph->protocol!=IPPROTO_TCP)
{
//mylog(log_trace,"not tcp");
return0;
}
int ip_len=ntohs(iph->tot_len);
int ip_hdr_len=iph->ihl*4;
if(len<ip_hdr_len)
{
mylog(log_debug,"len<ip_hdr_len,%d %d\n",len,ip_hdr_len);
return -1;
}
if(len<ip_len)
{
mylog(log_debug,"len<ip_len,%d %d\n",len,ip_len);
return -1;
}
if(ip_hdr_len>ip_len)
{
mylog(log_debug,"ip_hdr_len<ip_len,%d %d\n",ip_hdr_len,ip_len);
return -1;
}
if( ( ntohs(iph->frag_off) &(short)(0x1FFF) ) !=0 )
{
//not first segment
//printf("line=%d %x %x \n",__LINE__,(u32_t)ntohs(iph->frag_off),u32_t( ntohs(iph->frag_off) &0xFFF8));
return0;
}
if( ( ntohs(iph->frag_off) &(short)(0x80FF) ) !=0 )
{
//not whole segment
//printf("line=%d \n",__LINE__);
return0;
}
char * tcp_begin=s+ip_hdr_len;
int tcp_len=ip_len-ip_hdr_len;
if(tcp_len<20)
{
mylog(log_debug,"tcp_len<20,%d\n",tcp_len);
return -1;
}
tcphdr * tcph=(structtcphdr*)tcp_begin;
if(int(tcph->syn)==0) //fast fail
{
mylog(log_trace,"tcph->syn==0\n");
return0;
}
int tcp_hdr_len = tcph->doff*4;
if(tcp_len<tcp_hdr_len)
{
mylog(log_debug,"tcp_len <tcp_hdr_len, %d %d\n",tcp_len,tcp_hdr_len);
return -1;
}
/*
if(tcp_hdr_len==20)
{
//printf("line=%d\n",__LINE__);
mylog(log_trace,"no tcp option\n");
return 0;
}*/
char *ptr=tcp_begin+20;
char *option_end=tcp_begin+tcp_hdr_len;
while(ptr<option_end)
{
if(*ptr==0)
{
return0;
}
elseif(*ptr==1)
{
ptr++;
}
elseif(*ptr==2)
{
if(ptr+1>=option_end)
{
mylog(log_debug,"invaild option ptr+1==option_end,for mss\n");
return -1;
}
if(*(ptr+1)!=4)
{
mylog(log_debug,"invaild mss len\n");
return -1;
}
if(ptr+3>=option_end)
{
mylog(log_debug,"ptr+4>option_end for mss\n");
return -1;
}
int mss= read_u16(ptr+2);//uint8_t(ptr[2])*256+uint8_t(ptr[3]);
int new_mss=mss;
if(new_mss>::mssfix-40-10) //minus extra 10 for safe
{
new_mss=::mssfix-40-10;
}
write_u16(ptr+2,(unsignedshort)new_mss);
pseudo_header psh;
psh.source_address =iph->saddr;
psh.dest_address = iph->daddr;
psh.placeholder = 0;
psh.protocol = iph->protocol;
psh.tcp_length = htons(tcp_len);
tcph->check=0;
tcph->check=tcp_csum(psh,(unsignedshort *)tcph,tcp_len);
mylog(log_trace,"mss=%d syn=%d ack=%d, changed mss to %d \n",mss,(int)tcph->syn,(int)tcph->ack,new_mss);
//printf("test=%x\n",u32_t(1));
//printf("frag=%x\n",u32_t( ntohs(iph->frag_off) ));
return0;
}
else
{
if(ptr+1>=option_end)
{
mylog(log_debug,"invaild option ptr+1==option_end\n");
return -1;
}
else
{
int len=(unsignedchar)*(ptr+1);
if(len<=1)
{
mylog(log_debug,"invaild option len %d\n",len);
return -1;
}
ptr+=len;
}
}
}
return0;
}
intdo_keep_alive(dest_t & dest)
{
if(get_current_time()-last_keep_alive_time>u64_t(keep_alive_interval))
{
last_keep_alive_time=get_current_time();
char data[buf_len];int len;
data[0]=header_keep_alive;
len=1;
assert(dest.cook==1);
//do_cook(data,len);
delay_send(0,dest,data,len);
}
return0;
}