tun_dev.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * tun.cpp
  3. *
  4. * Created on: Oct 26, 2017
  5. * Author: root
  6. */
  7. #include "tun_dev.h"
  8. my_time_t last_keep_alive_time=0;
  9. int get_tun_fd(char * dev_name)
  10. {
  11. int tun_fd=open("/dev/net/tun",O_RDWR);
  12. if(tun_fd <0)
  13. {
  14. mylog(log_fatal,"open /dev/net/tun failed");
  15. myexit(-1);
  16. }
  17. struct ifreq ifr;
  18. memset(&ifr, 0, sizeof(ifr));
  19. ifr.ifr_flags = IFF_TUN|IFF_NO_PI;
  20. strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
  21. if(ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0)
  22. {
  23. mylog(log_fatal,"open /dev/net/tun failed");
  24. myexit(-1);
  25. }
  26. if (persist_tun == 1) {
  27. if (ioctl(tun_fd, TUNSETPERSIST, 1) != 0) {
  28. mylog(log_warn,"failed to set tun persistent");
  29. }
  30. }
  31. return tun_fd;
  32. }
  33. int set_tun(char *if_name,u32_t local_ip,u32_t remote_ip,int mtu)
  34. {
  35. if(manual_set_tun) return 0;
  36. //printf("i m here1\n");
  37. struct ifreq ifr;
  38. struct sockaddr_in sai;
  39. memset(&ifr,0,sizeof(ifr));
  40. memset(&sai, 0, sizeof(struct sockaddr));
  41. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  42. strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
  43. sai.sin_family = AF_INET;
  44. sai.sin_port = 0;
  45. sai.sin_addr.s_addr = local_ip;
  46. memcpy(&ifr.ifr_addr,&sai, sizeof(struct sockaddr));
  47. assert(ioctl(sockfd, SIOCSIFADDR, &ifr)==0); //set source ip
  48. sai.sin_addr.s_addr = remote_ip;
  49. memcpy(&ifr.ifr_addr,&sai, sizeof(struct sockaddr));
  50. assert(ioctl(sockfd, SIOCSIFDSTADDR, &ifr)==0);//set dest ip
  51. ifr.ifr_mtu=mtu;
  52. assert(ioctl(sockfd, SIOCSIFMTU, &ifr)==0);//set mtu
  53. assert(ioctl(sockfd, SIOCGIFFLAGS, &ifr)==0);
  54. // ifr.ifr_flags |= ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST );
  55. ifr.ifr_flags = ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST );//set interface flags
  56. assert(ioctl(sockfd, SIOCSIFFLAGS, &ifr)==0);
  57. //printf("i m here2\n");
  58. return 0;
  59. }
  60. int put_header(char header,char * data,int &len)
  61. {
  62. assert(len>=0);
  63. data[len]=header;
  64. len+=1;
  65. return 0;
  66. }
  67. int get_header(char &header,char * data,int &len)
  68. {
  69. assert(len>=0);
  70. if(len<1) return -1;
  71. len-=1;
  72. header=data[len];
  73. return 0;
  74. }
  75. int from_normal_to_fec2(conn_info_t & conn_info,dest_t &dest,char * data,int len,char header)
  76. {
  77. int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
  78. from_normal_to_fec(conn_info,data,len,out_n,out_arr,out_len,out_delay);
  79. for(int i=0;i<out_n;i++)
  80. {
  81. char tmp_buf[buf_len];
  82. int tmp_len=out_len[i];
  83. memcpy(tmp_buf,out_arr[i],out_len[i]);
  84. put_header(header,tmp_buf,tmp_len);
  85. delay_send(out_delay[i],dest,tmp_buf,tmp_len);//this is slow but safer.just use this one
  86. //put_header(header,out_arr[i],out_len[i]);//modify in place
  87. //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
  88. }
  89. return 0;
  90. }
  91. int from_fec_to_normal2(conn_info_t & conn_info,dest_t &dest,char * data,int len)
  92. {
  93. int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
  94. from_fec_to_normal(conn_info,data,len,out_n,out_arr,out_len,out_delay);
  95. for(int i=0;i<out_n;i++)
  96. {
  97. #ifndef NOLIMIT
  98. if(program_mode==server_mode)
  99. {
  100. char * tmp_data=out_arr[i];
  101. int tmp_len=out_len[i];
  102. iphdr * iph;
  103. iph = (struct iphdr *) tmp_data;
  104. if(tmp_len>=int(sizeof(iphdr))&&iph->version==4)
  105. {
  106. u32_t dest_ip=iph->daddr;
  107. //printf("%s\n",my_ntoa(dest_ip));
  108. if( ( ntohl(sub_net_uint32)&0xFFFFFF00 ) != ( ntohl (dest_ip) &0xFFFFFF00) )
  109. {
  110. string sub=my_ntoa(dest_ip);
  111. string dst=my_ntoa( htonl( ntohl (sub_net_uint32) &0xFFFFFF00) );
  112. 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());
  113. continue;
  114. }
  115. }
  116. }
  117. #endif
  118. delay_send(out_delay[i],dest,out_arr[i],out_len[i]);
  119. }
  120. return 0;
  121. }
  122. int do_mssfix(char * s,int len)//currently only for ipv4
  123. {
  124. if(mssfix==0)
  125. {
  126. return 0;
  127. }
  128. if(len<int(sizeof(iphdr)))
  129. {
  130. mylog(log_debug,"packet from tun len=%d <20\n",len);
  131. return -1;
  132. }
  133. iphdr * iph;
  134. iph = (struct iphdr *) s;
  135. if(iph->version!=4)
  136. {
  137. //mylog(log_trace,"not ipv4");
  138. return 0;
  139. }
  140. if(iph->protocol!=IPPROTO_TCP)
  141. {
  142. //mylog(log_trace,"not tcp");
  143. return 0;
  144. }
  145. int ip_len=ntohs(iph->tot_len);
  146. int ip_hdr_len=iph->ihl*4;
  147. if(len<ip_hdr_len)
  148. {
  149. mylog(log_debug,"len<ip_hdr_len,%d %d\n",len,ip_hdr_len);
  150. return -1;
  151. }
  152. if(len<ip_len)
  153. {
  154. mylog(log_debug,"len<ip_len,%d %d\n",len,ip_len);
  155. return -1;
  156. }
  157. if(ip_hdr_len>ip_len)
  158. {
  159. mylog(log_debug,"ip_hdr_len<ip_len,%d %d\n",ip_hdr_len,ip_len);
  160. return -1;
  161. }
  162. if( ( ntohs(iph->frag_off) &(short)(0x1FFF) ) !=0 )
  163. {
  164. //not first segment
  165. //printf("line=%d %x %x \n",__LINE__,(u32_t)ntohs(iph->frag_off),u32_t( ntohs(iph->frag_off) &0xFFF8));
  166. return 0;
  167. }
  168. if( ( ntohs(iph->frag_off) &(short)(0x80FF) ) !=0 )
  169. {
  170. //not whole segment
  171. //printf("line=%d \n",__LINE__);
  172. return 0;
  173. }
  174. char * tcp_begin=s+ip_hdr_len;
  175. int tcp_len=ip_len-ip_hdr_len;
  176. if(tcp_len<20)
  177. {
  178. mylog(log_debug,"tcp_len<20,%d\n",tcp_len);
  179. return -1;
  180. }
  181. tcphdr * tcph=(struct tcphdr*)tcp_begin;
  182. if(int(tcph->syn)==0) //fast fail
  183. {
  184. mylog(log_trace,"tcph->syn==0\n");
  185. return 0;
  186. }
  187. int tcp_hdr_len = tcph->doff*4;
  188. if(tcp_len<tcp_hdr_len)
  189. {
  190. mylog(log_debug,"tcp_len <tcp_hdr_len, %d %d\n",tcp_len,tcp_hdr_len);
  191. return -1;
  192. }
  193. /*
  194. if(tcp_hdr_len==20)
  195. {
  196. //printf("line=%d\n",__LINE__);
  197. mylog(log_trace,"no tcp option\n");
  198. return 0;
  199. }*/
  200. char *ptr=tcp_begin+20;
  201. char *option_end=tcp_begin+tcp_hdr_len;
  202. while(ptr<option_end)
  203. {
  204. if(*ptr==0)
  205. {
  206. return 0;
  207. }
  208. else if(*ptr==1)
  209. {
  210. ptr++;
  211. }
  212. else if(*ptr==2)
  213. {
  214. if(ptr+1>=option_end)
  215. {
  216. mylog(log_debug,"invaild option ptr+1==option_end,for mss\n");
  217. return -1;
  218. }
  219. if(*(ptr+1)!=4)
  220. {
  221. mylog(log_debug,"invaild mss len\n");
  222. return -1;
  223. }
  224. if(ptr+3>=option_end)
  225. {
  226. mylog(log_debug,"ptr+4>option_end for mss\n");
  227. return -1;
  228. }
  229. int mss= read_u16(ptr+2);//uint8_t(ptr[2])*256+uint8_t(ptr[3]);
  230. int new_mss=mss;
  231. if(new_mss>::mssfix-40-10) //minus extra 10 for safe
  232. {
  233. new_mss=::mssfix-40-10;
  234. }
  235. write_u16(ptr+2,(unsigned short)new_mss);
  236. pseudo_header psh;
  237. psh.source_address =iph->saddr;
  238. psh.dest_address = iph->daddr;
  239. psh.placeholder = 0;
  240. psh.protocol = iph->protocol;
  241. psh.tcp_length = htons(tcp_len);
  242. tcph->check=0;
  243. tcph->check=tcp_csum(psh,(unsigned short *)tcph,tcp_len);
  244. mylog(log_trace,"mss=%d syn=%d ack=%d, changed mss to %d \n",mss,(int)tcph->syn,(int)tcph->ack,new_mss);
  245. //printf("test=%x\n",u32_t(1));
  246. //printf("frag=%x\n",u32_t( ntohs(iph->frag_off) ));
  247. return 0;
  248. }
  249. else
  250. {
  251. if(ptr+1>=option_end)
  252. {
  253. mylog(log_debug,"invaild option ptr+1==option_end\n");
  254. return -1;
  255. }
  256. else
  257. {
  258. int len=(unsigned char)*(ptr+1);
  259. if(len<=1)
  260. {
  261. mylog(log_debug,"invaild option len %d\n",len);
  262. return -1;
  263. }
  264. ptr+=len;
  265. }
  266. }
  267. }
  268. return 0;
  269. }
  270. int do_keep_alive(dest_t & dest)
  271. {
  272. if(get_current_time()-last_keep_alive_time>u64_t(keep_alive_interval))
  273. {
  274. last_keep_alive_time=get_current_time();
  275. char data[buf_len];int len;
  276. data[0]=header_keep_alive;
  277. len=1;
  278. assert(dest.cook==1);
  279. //do_cook(data,len);
  280. delay_send(0,dest,data,len);
  281. }
  282. return 0;
  283. }