network.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * network.cpp
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #include "common.h"
  8. #include "network.h"
  9. #include "log.h"
  10. int raw_recv_fd=-1;
  11. int raw_send_fd=-1;
  12. uint32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  13. int seq_mode=2;
  14. int filter_port=-1;
  15. int disable_bpf_filter=0; //for test only,most time no need to disable this
  16. uint32_t bind_address_uint32=0;
  17. struct sock_filter code_tcp_old[] = {
  18. { 0x28, 0, 0, 0x0000000c },//0
  19. { 0x15, 0, 10, 0x00000800 },//1
  20. { 0x30, 0, 0, 0x00000017 },//2
  21. { 0x15, 0, 8, 0x00000006 },//3
  22. { 0x28, 0, 0, 0x00000014 },//4
  23. { 0x45, 6, 0, 0x00001fff },//5
  24. { 0xb1, 0, 0, 0x0000000e },//6
  25. { 0x48, 0, 0, 0x0000000e },//7
  26. { 0x15, 2, 0, 0x0000ef32 },//8
  27. { 0x48, 0, 0, 0x00000010 },//9
  28. { 0x15, 0, 1, 0x0000ef32 },//10
  29. { 0x6, 0, 0, 0x0000ffff },//11
  30. { 0x6, 0, 0, 0x00000000 },//12
  31. };
  32. struct sock_filter code_tcp[] = {
  33. { 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  34. { 0x5, 0, 0, 0x00000000 },//1
  35. { 0x30, 0, 0, 0x00000009 },//2
  36. { 0x15, 0, 6, 0x00000006 },//3
  37. { 0x28, 0, 0, 0x00000006 },//4
  38. { 0x45, 4, 0, 0x00001fff },//5
  39. { 0xb1, 0, 0, 0x00000000 },//6
  40. { 0x48, 0, 0, 0x00000002 },//7
  41. { 0x15, 0, 1, 0x0000fffe },//8
  42. { 0x6, 0, 0, 0x0000ffff },//9
  43. { 0x6, 0, 0, 0x00000000 },//10
  44. };
  45. int code_tcp_port_index=8;
  46. struct sock_filter code_udp[] = {
  47. { 0x5, 0, 0, 0x00000001 },
  48. { 0x5, 0, 0, 0x00000000 },
  49. { 0x30, 0, 0, 0x00000009 },
  50. { 0x15, 0, 6, 0x00000011 },
  51. { 0x28, 0, 0, 0x00000006 },
  52. { 0x45, 4, 0, 0x00001fff },
  53. { 0xb1, 0, 0, 0x00000000 },
  54. { 0x48, 0, 0, 0x00000002 },
  55. { 0x15, 0, 1, 0x0000fffe },
  56. { 0x6, 0, 0, 0x0000ffff },
  57. { 0x6, 0, 0, 0x00000000 },
  58. };
  59. int code_udp_port_index=8;
  60. struct sock_filter code_icmp[] = {
  61. { 0x5, 0, 0, 0x00000001 },
  62. { 0x5, 0, 0, 0x00000000 },
  63. { 0x30, 0, 0, 0x00000009 },
  64. { 0x15, 0, 1, 0x00000001 },
  65. { 0x6, 0, 0, 0x0000ffff },
  66. { 0x6, 0, 0, 0x00000000 },
  67. };
  68. /*
  69. tcpdump -i eth1 ip and icmp -d
  70. (000) ldh [12]
  71. (001) jeq #0x800 jt 2 jf 5
  72. (002) ldb [23]
  73. (003) jeq #0x1 jt 4 jf 5
  74. (004) ret #65535
  75. (005) ret #0
  76. tcpdump -i eth1 ip and icmp -dd
  77. { 0x28, 0, 0, 0x0000000c },
  78. { 0x15, 0, 3, 0x00000800 },
  79. { 0x30, 0, 0, 0x00000017 },
  80. { 0x15, 0, 1, 0x00000001 },
  81. { 0x6, 0, 0, 0x0000ffff },
  82. { 0x6, 0, 0, 0x00000000 },
  83. */
  84. /*
  85. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  86. { 0x28, 0, 0, 0x0000000c },
  87. { 0x15, 0, 8, 0x00000800 },
  88. { 0x30, 0, 0, 0x00000017 },
  89. { 0x15, 0, 6, 0x00000006 },
  90. { 0x28, 0, 0, 0x00000014 },
  91. { 0x45, 4, 0, 0x00001fff },
  92. { 0xb1, 0, 0, 0x0000000e },
  93. { 0x48, 0, 0, 0x00000010 },
  94. { 0x15, 0, 1, 0x0000fffe },
  95. { 0x6, 0, 0, 0x0000ffff },
  96. { 0x6, 0, 0, 0x00000000 },
  97. (000) ldh [12]
  98. (001) jeq #0x800 jt 2 jf 10
  99. (002) ldb [23]
  100. (003) jeq #0x6 jt 4 jf 10
  101. (004) ldh [20]
  102. (005) jset #0x1fff jt 10 jf 6
  103. (006) ldxb 4*([14]&0xf)
  104. (007) ldh [x + 16]
  105. (008) jeq #0xfffe jt 9 jf 10
  106. (009) ret #65535
  107. (010) ret #0
  108. */
  109. packet_info_t::packet_info_t()
  110. {
  111. if(raw_mode==mode_faketcp)
  112. {
  113. protocol=IPPROTO_TCP;
  114. ack_seq=get_true_random_number();
  115. seq=get_true_random_number();
  116. }
  117. else if(raw_mode==mode_udp)
  118. {
  119. protocol=IPPROTO_UDP;
  120. }
  121. else if(raw_mode==mode_icmp)
  122. {
  123. protocol=IPPROTO_ICMP;
  124. }
  125. }
  126. int init_raw_socket()
  127. {
  128. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  129. if(raw_send_fd == -1) {
  130. mylog(log_fatal,"Failed to create raw_send_fd\n");
  131. //perror("Failed to create raw_send_fd");
  132. myexit(1);
  133. }
  134. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  135. {
  136. mylog(log_fatal,"SO_SNDBUFFORCE fail\n");
  137. myexit(1);
  138. }
  139. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  140. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  141. if(raw_recv_fd == -1) {
  142. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  143. //perror("");
  144. myexit(1);
  145. }
  146. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  147. {
  148. mylog(log_fatal,"SO_RCVBUFFORCE fail\n");
  149. myexit(1);
  150. }
  151. //IP_HDRINCL to tell the kernel that headers are included in the packet
  152. int one = 1;
  153. const int *val = &one;
  154. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  155. mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
  156. //perror("Error setting IP_HDRINCL");
  157. myexit(2);
  158. }
  159. setnonblocking(raw_send_fd); //not really necessary
  160. setnonblocking(raw_recv_fd);
  161. return 0;
  162. }
  163. void init_filter(int port)
  164. {
  165. sock_fprog bpf;
  166. filter_port=port;
  167. if(disable_bpf_filter) return;
  168. //if(raw_mode==mode_icmp) return ;
  169. //code_tcp[8].k=code_tcp[10].k=port;
  170. if(raw_mode==mode_faketcp)
  171. {
  172. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  173. code_tcp[code_tcp_port_index].k=port;
  174. bpf.filter = code_tcp;
  175. }
  176. else if(raw_mode==mode_udp)
  177. {
  178. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  179. code_udp[code_udp_port_index].k=port;
  180. bpf.filter = code_udp;
  181. }
  182. else if(raw_mode==mode_icmp)
  183. {
  184. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  185. bpf.filter = code_icmp;
  186. }
  187. int dummy;
  188. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  189. if (ret != 0)
  190. {
  191. mylog(log_debug,"error remove fiter\n");
  192. //perror("filter");
  193. //exit(-1);
  194. }
  195. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  196. if (ret != 0)
  197. {
  198. mylog(log_fatal,"error set fiter\n");
  199. //perror("filter");
  200. myexit(-1);
  201. }
  202. }
  203. void remove_filter()
  204. {
  205. filter_port=0;
  206. int dummy;
  207. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  208. if (ret != 0)
  209. {
  210. mylog(log_debug,"error remove fiter\n");
  211. //perror("filter");
  212. //exit(-1);
  213. }
  214. }
  215. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  216. {
  217. const packet_info_t &send_info=raw_info.send_info;
  218. const packet_info_t &recv_info=raw_info.recv_info;
  219. char send_raw_ip_buf[buf_len];
  220. struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
  221. memset(iph,0,sizeof(iphdr));
  222. struct sockaddr_in sin;
  223. sin.sin_family = AF_INET;
  224. //sin.sin_port = htons(info.dst_port); //dont need this
  225. sin.sin_addr.s_addr = send_info.dst_ip;
  226. iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  227. iph->version = 4;
  228. iph->tos = 0;
  229. // iph->id = htonl (ip_id++); //Id of this packet
  230. // iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero
  231. iph->frag_off = htons(0x4000); //DF set,others are zero
  232. iph->ttl = 64;
  233. iph->protocol = send_info.protocol;
  234. iph->check = 0; //Set to 0 before calculating checksum
  235. iph->saddr = send_info.src_ip; //Spoof the source ip address
  236. iph->daddr = send_info.dst_ip;
  237. uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
  238. // iph->tot_len = htons(ip_tot_len); //this is not necessary ,kernel will always auto fill this //http://man7.org/linux/man-pages/man7/raw.7.html
  239. //iph->tot_len = ip_tot_len;
  240. memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
  241. //iph->check = csum ((unsigned short *) send_raw_ip_buf, ip_tot_len); //this is not necessary ,kernel will always auto fill this
  242. int ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  243. if(ret==-1)
  244. {
  245. mylog(log_debug,"sendto failed\n");
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. int peek_raw(uint32_t &ip,uint16_t &port)
  251. { static char peek_raw_buf[buf_len];
  252. char *ip_begin=peek_raw_buf+link_level_header_len;
  253. struct sockaddr saddr;
  254. socklen_t saddr_size;
  255. int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,buf_len, MSG_PEEK ,&saddr , &saddr_size);//change buf_len to something smaller,we only need header here
  256. iphdr * iph = (struct iphdr *) (ip_begin);
  257. //mylog(log_info,"recv_len %d\n",recv_len);
  258. if(recv_len<int(sizeof(iphdr)))
  259. {
  260. return -1;
  261. }
  262. ip=iph->saddr;
  263. unsigned short iphdrlen =iph->ihl*4;
  264. char *payload=ip_begin+iphdrlen;
  265. //mylog(log_info,"protocol %d\n",iph->protocol);
  266. switch(raw_mode)
  267. {
  268. case mode_faketcp:
  269. {
  270. if(iph->protocol!=IPPROTO_TCP) return -1;
  271. struct tcphdr *tcph=(tcphdr *)payload;
  272. if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
  273. return -1;
  274. port=ntohs(tcph->source);
  275. break;
  276. }
  277. case mode_udp:
  278. {
  279. if(iph->protocol!=IPPROTO_UDP) return -1;
  280. struct udphdr *udph=(udphdr *)payload;
  281. if(recv_len<int(iphdrlen+sizeof(udphdr)))
  282. return -1;
  283. port=ntohs(udph->source);
  284. break;
  285. }
  286. case mode_icmp:
  287. {
  288. if(iph->protocol!=IPPROTO_ICMP) return -1;
  289. struct icmphdr *icmph=(icmphdr *)payload;
  290. if(recv_len<int( iphdrlen+sizeof(icmphdr) ))
  291. return -1;
  292. port=ntohs(icmph->id);
  293. break;
  294. }
  295. default:return -1;
  296. }
  297. return 0;
  298. }
  299. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  300. {
  301. const packet_info_t &send_info=raw_info.send_info;
  302. packet_info_t &recv_info=raw_info.recv_info;
  303. static char recv_raw_ip_buf[buf_len];
  304. iphdr * iph;
  305. struct sockaddr saddr;
  306. socklen_t saddr_size;
  307. saddr_size = sizeof(saddr);
  308. int flag=0;
  309. int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, buf_len, flag ,&saddr , &saddr_size);
  310. if(recv_len<0)
  311. {
  312. mylog(log_trace,"recv_len %d\n",recv_len);
  313. return -1;
  314. }
  315. if(recv_len<int(link_level_header_len))
  316. {
  317. mylog(log_trace,"length error\n");
  318. }
  319. if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
  320. {
  321. mylog(log_trace,"not an ipv4 packet!\n");
  322. return -1;
  323. }
  324. char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
  325. iph = (struct iphdr *) (ip_begin);
  326. recv_info.src_ip=iph->saddr;
  327. recv_info.dst_ip=iph->daddr;
  328. recv_info.protocol=iph->protocol;
  329. if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32)
  330. {
  331. //printf(" bind adress doenst match, dropped\n");
  332. return -1;
  333. }
  334. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  335. mylog(log_trace,"iph ihl error\n");
  336. return -1;
  337. }
  338. int ip_len=ntohs(iph->tot_len);
  339. if(recv_len-int(link_level_header_len) <ip_len)
  340. {
  341. mylog(log_debug,"incomplete packet\n");
  342. return -1;
  343. }
  344. unsigned short iphdrlen =iph->ihl*4;
  345. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  346. if(ip_chk!=0)
  347. {
  348. mylog(log_debug,"ip header error %d\n",ip_chk);
  349. return -1;
  350. }
  351. payload=ip_begin+iphdrlen;
  352. payloadlen=ip_len-iphdrlen;
  353. if(payloadlen<0)
  354. {
  355. mylog(log_warn,"error payload len\n");
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  361. {
  362. packet_info_t &send_info=raw_info.send_info;
  363. const packet_info_t &recv_info=raw_info.recv_info;
  364. char send_raw_icmp_buf[buf_len];
  365. icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
  366. memset(icmph,0,sizeof(icmphdr));
  367. if(program_mode==client_mode)
  368. {
  369. icmph->type=8;
  370. }
  371. else
  372. {
  373. icmph->type=0;
  374. }
  375. icmph->code=0;
  376. icmph->id=htons(send_info.src_port);
  377. icmph->seq=htons(send_info.icmp_seq++); /////////////modify
  378. memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
  379. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
  380. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
  381. {
  382. return -1;
  383. }
  384. return 0;
  385. }
  386. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  387. {
  388. const packet_info_t &send_info=raw_info.send_info;
  389. const packet_info_t &recv_info=raw_info.recv_info;
  390. char send_raw_udp_buf[buf_len];
  391. udphdr *udph=(struct udphdr *) (send_raw_udp_buf
  392. + sizeof(struct pseudo_header));
  393. memset(udph,0,sizeof(udphdr));
  394. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
  395. udph->source = htons(send_info.src_port);
  396. udph->dest = htons(send_info.dst_port);
  397. int udp_tot_len=payloadlen+sizeof(udphdr);
  398. if(udp_tot_len>65535)
  399. {
  400. mylog(log_debug,"invalid len\n");
  401. return -1;
  402. }
  403. mylog(log_debug,"udp_len:%d %d\n",udp_tot_len,udph->len);
  404. udph->len=htons(uint16_t(udp_tot_len));
  405. memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udphdr),payload,payloadlen);
  406. psh->source_address = send_info.src_ip;
  407. psh->dest_address = send_info.dst_ip;
  408. psh->placeholder = 0;
  409. psh->protocol = IPPROTO_UDP;
  410. psh->tcp_length = htons(uint16_t(udp_tot_len));
  411. int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
  412. udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
  413. if(send_raw_ip(raw_info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
  414. {
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  420. packet_info_t &send_info=raw_info.send_info;
  421. packet_info_t &recv_info=raw_info.recv_info;
  422. //mylog(log_debug,"syn %d\n",send_info.syn);
  423. char send_raw_tcp_buf0[buf_len];
  424. char *send_raw_tcp_buf=send_raw_tcp_buf0;
  425. struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
  426. + sizeof(struct pseudo_header));
  427. memset(tcph,0,sizeof(tcphdr));
  428. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
  429. //TCP Header
  430. tcph->source = htons(send_info.src_port);
  431. tcph->dest = htons(send_info.dst_port);
  432. tcph->seq = htonl(send_info.seq);
  433. tcph->ack_seq = htonl(send_info.ack_seq);
  434. tcph->fin = 0;
  435. tcph->syn = send_info.syn;
  436. tcph->rst = 0;
  437. tcph->psh = send_info.psh;
  438. tcph->ack = send_info.ack;
  439. if (tcph->syn == 1) {
  440. tcph->doff = 10; //tcp header size
  441. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  442. send_raw_tcp_buf[i++] = 0x02; //mss
  443. send_raw_tcp_buf[i++] = 0x04;
  444. send_raw_tcp_buf[i++] = 0x05;
  445. send_raw_tcp_buf[i++] = (char)0xb4;
  446. //raw_send_buf[i++]=0x01;
  447. //raw_send_buf[i++]=0x01;
  448. send_raw_tcp_buf[i++] = 0x04; //sack ok
  449. send_raw_tcp_buf[i++] = 0x02; //sack ok
  450. send_raw_tcp_buf[i++] = 0x08; //ts
  451. send_raw_tcp_buf[i++] = 0x0a;
  452. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  453. (uint32_t) get_current_time());
  454. i += 4;
  455. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  456. i += 4;
  457. send_raw_tcp_buf[i++] = 0x01;
  458. send_raw_tcp_buf[i++] = 0x03;
  459. send_raw_tcp_buf[i++] = 0x03;
  460. send_raw_tcp_buf[i++] = 0x05;
  461. } else {
  462. tcph->doff = 8;
  463. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  464. send_raw_tcp_buf[i++] = 0x01;
  465. send_raw_tcp_buf[i++] = 0x01;
  466. send_raw_tcp_buf[i++] = 0x08; //ts
  467. send_raw_tcp_buf[i++] = 0x0a;
  468. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  469. (uint32_t) get_current_time());
  470. i += 4;
  471. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  472. i += 4;
  473. }
  474. tcph->urg = 0;
  475. //tcph->window = htons((uint16_t)(1024));
  476. tcph->window = htons((uint16_t) (10240 + random() % 100));
  477. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  478. tcph->urg_ptr = 0;
  479. char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
  480. memcpy(tcp_data, payload, payloadlen);
  481. psh->source_address = send_info.src_ip;
  482. psh->dest_address = send_info.dst_ip;
  483. psh->placeholder = 0;
  484. psh->protocol = IPPROTO_TCP;
  485. psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
  486. int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  487. tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
  488. int tcp_totlen=tcph->doff*4 + payloadlen;
  489. if(send_raw_ip(raw_info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
  490. {
  491. return -1;
  492. }
  493. if (send_info.syn == 0 && send_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  494. {
  495. if (seq_mode == 0)
  496. {
  497. } else if (seq_mode == 1)
  498. {
  499. send_info.seq += payloadlen; //////////////////modify
  500. } else if (seq_mode == 2)
  501. {
  502. if (random() % 5 == 3)
  503. send_info.seq += payloadlen; //////////////////modify
  504. }
  505. }
  506. return 0;
  507. }
  508. /*
  509. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  510. {
  511. static uint16_t ip_id=1;
  512. char raw_send_buf[buf_len];
  513. char raw_send_buf2[buf_len];
  514. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  515. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  516. char *data;
  517. memset(raw_send_buf,0,payloadlen+100);
  518. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  519. //TCP header
  520. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  521. struct sockaddr_in sin;
  522. struct pseudo_header psh;
  523. //some address resolution
  524. sin.sin_family = AF_INET;
  525. sin.sin_port = htons(info.dst_port);
  526. sin.sin_addr.s_addr = info.dst_ip;
  527. //Fill in the IP Header
  528. iph->ihl = 5;
  529. iph->version = 4;
  530. iph->tos = 0;
  531. iph->id = htonl (ip_id++); //Id of this packet
  532. iph->frag_off = htons(0x4000); //DF set,others are zero
  533. iph->ttl = 64;
  534. iph->protocol = IPPROTO_TCP;
  535. iph->check = 0; //Set to 0 before calculating checksum
  536. iph->saddr = info.src_ip; //Spoof the source ip address
  537. iph->daddr = info.dst_ip;
  538. //TCP Header
  539. tcph->source = htons(info.src_port);
  540. tcph->dest = htons(info.dst_port);
  541. tcph->seq =htonl(info.seq);
  542. tcph->ack_seq = htonl(info.ack_seq);
  543. tcph->fin=0;
  544. tcph->syn=info.syn;
  545. tcph->rst=0;
  546. tcph->psh=info.psh;
  547. tcph->ack=info.ack;
  548. if(tcph->syn==1)
  549. {
  550. tcph->doff = 10; //tcp header size
  551. int i=sizeof (struct iphdr)+20;
  552. raw_send_buf[i++]=0x02;//mss
  553. raw_send_buf[i++]=0x04;
  554. raw_send_buf[i++]=0x05;
  555. raw_send_buf[i++]=0xb4;
  556. //raw_send_buf[i++]=0x01;
  557. //raw_send_buf[i++]=0x01;
  558. raw_send_buf[i++]=0x04; //sack ok
  559. raw_send_buf[i++]=0x02; //sack ok
  560. raw_send_buf[i++]=0x08; //i=6;
  561. raw_send_buf[i++]=0x0a;
  562. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  563. i+=4;
  564. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  565. i+=4;
  566. raw_send_buf[i++]=0x01;
  567. raw_send_buf[i++]=0x03;
  568. raw_send_buf[i++]=0x03;
  569. raw_send_buf[i++]=0x05;
  570. }
  571. else
  572. {
  573. tcph->doff=8;
  574. int i=sizeof (struct iphdr)+20;
  575. raw_send_buf[i++]=0x01;
  576. raw_send_buf[i++]=0x01;
  577. raw_send_buf[i++]=0x08; //i=0;
  578. raw_send_buf[i++]=0x0a;
  579. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  580. i+=4;
  581. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  582. i+=4;
  583. }
  584. tcph->urg=0;
  585. //tcph->window = htons((uint16_t)(1024));
  586. tcph->window = htons((uint16_t)(10240+random()%100));
  587. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  588. tcph->urg_ptr = 0;
  589. //Data part
  590. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  591. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  592. memcpy(data , payload, payloadlen);
  593. psh.source_address = info.src_ip;
  594. psh.dest_address = sin.sin_addr.s_addr;
  595. psh.placeholder = 0;
  596. psh.protocol = IPPROTO_TCP;
  597. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  598. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  599. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  600. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  601. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  602. //Ip checksum
  603. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  604. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  605. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  606. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  607. {
  608. if(seq_mode==0)
  609. {
  610. }
  611. else if(seq_mode==1)
  612. {
  613. g_packet_info_send.seq+=payloadlen;
  614. }
  615. else if(seq_mode==2)
  616. {
  617. if(random()% 5==3 )
  618. g_packet_info_send.seq+=payloadlen;
  619. }
  620. }
  621. mylog(log_trace,"<ret:%d>\n",ret);
  622. if(ret<0)
  623. {
  624. mylog(log_fatal,"");
  625. perror("raw send error\n");
  626. //printf("send error\n");
  627. }
  628. return 0;
  629. }
  630. */
  631. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  632. {
  633. const packet_info_t &send_info=raw_info.send_info;
  634. packet_info_t &recv_info=raw_info.recv_info;
  635. static char recv_raw_icmp_buf[buf_len];
  636. char * ip_payload;
  637. int ip_payloadlen;
  638. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  639. {
  640. mylog(log_debug,"recv_raw_ip error\n");
  641. return -1;
  642. }
  643. if(recv_info.protocol!=IPPROTO_ICMP)
  644. {
  645. //printf("not udp protocol\n");
  646. return -1;
  647. }
  648. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  649. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  650. if(program_mode==client_mode)
  651. {
  652. if(icmph->type!=0)
  653. return -1;
  654. }
  655. else
  656. {
  657. if(icmph->type!=8)
  658. return -1;
  659. }
  660. if(icmph->code!=0)
  661. return -1;
  662. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  663. if(check!=0)
  664. {
  665. mylog(log_debug,"icmp checksum fail %x\n",check);
  666. return -1;
  667. }
  668. payload=ip_payload+sizeof(icmphdr);
  669. payloadlen=ip_payloadlen-sizeof(icmphdr);
  670. mylog(log_debug,"get a packet len=%d\n",payloadlen);
  671. return 0;
  672. }
  673. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  674. {
  675. const packet_info_t &send_info=raw_info.send_info;
  676. packet_info_t &recv_info=raw_info.recv_info;
  677. static char recv_raw_udp_buf[buf_len];
  678. char * ip_payload;
  679. int ip_payloadlen;
  680. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  681. {
  682. mylog(log_debug,"recv_raw_ip error\n");
  683. return -1;
  684. }
  685. if(recv_info.protocol!=IPPROTO_UDP)
  686. {
  687. //printf("not udp protocol\n");
  688. return -1;
  689. }
  690. if(ip_payloadlen<int( sizeof(udphdr) ))
  691. {
  692. mylog(log_debug,"too short to hold udpheader\n");
  693. return -1;
  694. }
  695. udphdr *udph=(struct udphdr*)ip_payload;
  696. if(ntohs(udph->len)!=ip_payloadlen)
  697. {
  698. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  699. return -1;
  700. }
  701. if(udph->dest!=ntohs(uint16_t(filter_port)))
  702. {
  703. //printf("%x %x",tcph->dest,);
  704. return -1;
  705. }
  706. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  707. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  708. psh->source_address = recv_info.src_ip;
  709. psh->dest_address = recv_info.dst_ip;
  710. psh->placeholder = 0;
  711. psh->protocol = IPPROTO_UDP;
  712. psh->tcp_length = htons(ip_payloadlen);
  713. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  714. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  715. if(udp_chk!=0)
  716. {
  717. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  718. mylog(log_debug,"udp header error\n");
  719. return -1;
  720. }
  721. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  722. recv_info.src_port=ntohs(udph->source);
  723. recv_info.dst_port=ntohs(udph->dest);
  724. payloadlen = ip_payloadlen-sizeof(udphdr);
  725. payload=udp_begin+sizeof(udphdr);
  726. return 0;
  727. }
  728. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  729. {
  730. packet_info_t &send_info=raw_info.send_info;
  731. packet_info_t &recv_info=raw_info.recv_info;
  732. static char recv_raw_tcp_buf[buf_len];
  733. char * ip_payload;
  734. int ip_payloadlen;
  735. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  736. {
  737. mylog(log_debug,"recv_raw_ip error\n");
  738. return -1;
  739. }
  740. if(recv_info.protocol!=IPPROTO_TCP)
  741. {
  742. //printf("not tcp protocol\n");
  743. return -1;
  744. }
  745. tcphdr * tcph=(struct tcphdr*)ip_payload;
  746. unsigned short tcphdrlen = tcph->doff*4;
  747. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  748. mylog(log_debug,"tcph error\n");
  749. return 0;
  750. }
  751. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  752. {
  753. //printf("%x %x",tcph->dest,);
  754. return -1;
  755. }
  756. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  757. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  758. psh->source_address = recv_info.src_ip;
  759. psh->dest_address = recv_info.dst_ip;
  760. psh->placeholder = 0;
  761. psh->protocol = IPPROTO_TCP;
  762. psh->tcp_length = htons(ip_payloadlen);
  763. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  764. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  765. if(tcp_chk!=0)
  766. {
  767. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  768. mylog(log_debug,"tcp header error\n");
  769. return -1;
  770. }
  771. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  772. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  773. recv_info.has_ts=0;
  774. if(tcph->doff==10)
  775. {
  776. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  777. {
  778. recv_info.has_ts=1;
  779. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  780. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  781. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  782. }
  783. }
  784. else if(tcph->doff==8)
  785. {
  786. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  787. {
  788. recv_info.has_ts=1;
  789. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  790. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  791. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  792. }
  793. }
  794. if(tcph->rst==1)
  795. {
  796. mylog(log_warn,"%%%%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  797. }
  798. recv_info.ack=tcph->ack;
  799. recv_info.syn=tcph->syn;
  800. recv_info.rst=tcph->rst;
  801. recv_info.src_port=ntohs(tcph->source);
  802. recv_info.dst_port=ntohs(tcph->dest);
  803. recv_info.seq=ntohl(tcph->seq);
  804. recv_info.ack_seq=ntohl(tcph->ack_seq);
  805. recv_info.psh=tcph->psh;
  806. if(recv_info.has_ts)
  807. {
  808. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  809. }
  810. payloadlen = ip_payloadlen-tcphdrlen;
  811. payload=tcp_begin+tcphdrlen;
  812. return 0;
  813. }
  814. /*
  815. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  816. {
  817. static char buf[buf_len];
  818. char raw_recv_buf[buf_len];
  819. char raw_recv_buf2[buf_len];
  820. char raw_recv_buf3[buf_len];
  821. iphdr * iph;
  822. tcphdr * tcph;
  823. int size;
  824. struct sockaddr saddr;
  825. socklen_t saddr_size;
  826. saddr_size = sizeof(saddr);
  827. mylog(log_trace,"raw!\n");
  828. size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
  829. if(buf[12]!=8||buf[13]!=0)
  830. {
  831. mylog(log_debug,"not an ipv4 packet!\n");
  832. return -1;
  833. }
  834. char *ip_begin=buf+14;
  835. iph = (struct iphdr *) (ip_begin);
  836. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  837. mylog(log_debug,"iph ihl error");
  838. return -1;
  839. }
  840. if (iph->protocol != IPPROTO_TCP) {
  841. mylog(log_debug,"iph protocal != tcp\n");
  842. return -1;
  843. }
  844. int ip_len=ntohs(iph->tot_len);
  845. unsigned short iphdrlen =iph->ihl*4;
  846. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  847. unsigned short tcphdrlen = tcph->doff*4;
  848. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  849. mylog(log_debug,"tcph error");
  850. return 0;
  851. }
  852. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  853. {
  854. //printf("%x %x",tcph->dest,);
  855. return -1;
  856. }
  857. /////ip
  858. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  859. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  860. /////ip end
  861. ///tcp
  862. struct pseudo_header psh;
  863. psh.source_address = iph->saddr;
  864. psh.dest_address = iph->daddr;
  865. psh.placeholder = 0;
  866. psh.protocol = IPPROTO_TCP;
  867. psh.tcp_length = htons(ip_len-iphdrlen);
  868. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  869. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  870. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  871. if(ip_chk!=0)
  872. {
  873. mylog(log_debug,"ip header error %d\n",ip_chk);
  874. return -1;
  875. }
  876. if(tcp_chk!=0)
  877. {
  878. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  879. mylog(log_debug,"tcp header error\n");
  880. return -1;
  881. }
  882. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  883. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  884. info.has_ts=0;
  885. if(tcph->doff==10)
  886. {
  887. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  888. {
  889. info.has_ts=1;
  890. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  891. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  892. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  893. }
  894. }
  895. else if(tcph->doff==8)
  896. {
  897. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  898. {
  899. info.has_ts=1;
  900. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  901. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  902. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  903. }
  904. }
  905. if(tcph->rst==1)
  906. {
  907. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  908. }
  909. info.ack=tcph->ack;
  910. info.syn=tcph->syn;
  911. info.rst=tcph->rst;
  912. info.src_port=ntohs(tcph->source);
  913. info.src_ip=iph->saddr;
  914. info.seq=ntohl(tcph->seq);
  915. info.ack_seq=ntohl(tcph->ack_seq);
  916. info.psh=tcph->psh;
  917. if(info.has_ts)
  918. {
  919. g_packet_info_send.ts_ack=info.ts;
  920. }
  921. ////tcp end
  922. payloadlen = ip_len-tcphdrlen-iphdrlen;
  923. payload=ip_begin+tcphdrlen+iphdrlen;
  924. if(payloadlen>0&&payload[0]=='h')
  925. {
  926. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  927. }
  928. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  929. {
  930. //if(seq_increse)
  931. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  932. }
  933. //printf("%d\n",ip_len);
  934. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  935. return 0;
  936. }*/
  937. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  938. {
  939. packet_info_t &send_info=raw_info.send_info;
  940. packet_info_t &recv_info=raw_info.recv_info;
  941. mylog(log_trace,"send_raw : from %x %d to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
  942. switch(raw_mode)
  943. {
  944. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  945. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  946. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  947. default:return -1;
  948. }
  949. }
  950. int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen)
  951. {
  952. packet_info_t &send_info=raw_info.send_info;
  953. packet_info_t &recv_info=raw_info.recv_info;
  954. switch(raw_mode)
  955. {
  956. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  957. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  958. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  959. default: return -1;
  960. }
  961. }