network.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. packet_info_t &send_info=raw_info.send_info;
  218. 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. 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. 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++);
  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. packet_info_t &send_info=raw_info.send_info;
  389. 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
  494. && payloadlen != 0) {
  495. if (seq_mode == 0) {
  496. } else if (seq_mode == 1) {
  497. send_info.seq += payloadlen;
  498. } else if (seq_mode == 2) {
  499. if (random() % 5 == 3)
  500. send_info.seq += payloadlen;
  501. }
  502. }
  503. return 0;
  504. }
  505. /*
  506. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  507. {
  508. static uint16_t ip_id=1;
  509. char raw_send_buf[buf_len];
  510. char raw_send_buf2[buf_len];
  511. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  512. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  513. char *data;
  514. memset(raw_send_buf,0,payloadlen+100);
  515. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  516. //TCP header
  517. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  518. struct sockaddr_in sin;
  519. struct pseudo_header psh;
  520. //some address resolution
  521. sin.sin_family = AF_INET;
  522. sin.sin_port = htons(info.dst_port);
  523. sin.sin_addr.s_addr = info.dst_ip;
  524. //Fill in the IP Header
  525. iph->ihl = 5;
  526. iph->version = 4;
  527. iph->tos = 0;
  528. iph->id = htonl (ip_id++); //Id of this packet
  529. iph->frag_off = htons(0x4000); //DF set,others are zero
  530. iph->ttl = 64;
  531. iph->protocol = IPPROTO_TCP;
  532. iph->check = 0; //Set to 0 before calculating checksum
  533. iph->saddr = info.src_ip; //Spoof the source ip address
  534. iph->daddr = info.dst_ip;
  535. //TCP Header
  536. tcph->source = htons(info.src_port);
  537. tcph->dest = htons(info.dst_port);
  538. tcph->seq =htonl(info.seq);
  539. tcph->ack_seq = htonl(info.ack_seq);
  540. tcph->fin=0;
  541. tcph->syn=info.syn;
  542. tcph->rst=0;
  543. tcph->psh=info.psh;
  544. tcph->ack=info.ack;
  545. if(tcph->syn==1)
  546. {
  547. tcph->doff = 10; //tcp header size
  548. int i=sizeof (struct iphdr)+20;
  549. raw_send_buf[i++]=0x02;//mss
  550. raw_send_buf[i++]=0x04;
  551. raw_send_buf[i++]=0x05;
  552. raw_send_buf[i++]=0xb4;
  553. //raw_send_buf[i++]=0x01;
  554. //raw_send_buf[i++]=0x01;
  555. raw_send_buf[i++]=0x04; //sack ok
  556. raw_send_buf[i++]=0x02; //sack ok
  557. raw_send_buf[i++]=0x08; //i=6;
  558. raw_send_buf[i++]=0x0a;
  559. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  560. i+=4;
  561. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  562. i+=4;
  563. raw_send_buf[i++]=0x01;
  564. raw_send_buf[i++]=0x03;
  565. raw_send_buf[i++]=0x03;
  566. raw_send_buf[i++]=0x05;
  567. }
  568. else
  569. {
  570. tcph->doff=8;
  571. int i=sizeof (struct iphdr)+20;
  572. raw_send_buf[i++]=0x01;
  573. raw_send_buf[i++]=0x01;
  574. raw_send_buf[i++]=0x08; //i=0;
  575. raw_send_buf[i++]=0x0a;
  576. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  577. i+=4;
  578. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  579. i+=4;
  580. }
  581. tcph->urg=0;
  582. //tcph->window = htons((uint16_t)(1024));
  583. tcph->window = htons((uint16_t)(10240+random()%100));
  584. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  585. tcph->urg_ptr = 0;
  586. //Data part
  587. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  588. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  589. memcpy(data , payload, payloadlen);
  590. psh.source_address = info.src_ip;
  591. psh.dest_address = sin.sin_addr.s_addr;
  592. psh.placeholder = 0;
  593. psh.protocol = IPPROTO_TCP;
  594. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  595. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  596. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  597. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  598. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  599. //Ip checksum
  600. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  601. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  602. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  603. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  604. {
  605. if(seq_mode==0)
  606. {
  607. }
  608. else if(seq_mode==1)
  609. {
  610. g_packet_info_send.seq+=payloadlen;
  611. }
  612. else if(seq_mode==2)
  613. {
  614. if(random()% 5==3 )
  615. g_packet_info_send.seq+=payloadlen;
  616. }
  617. }
  618. mylog(log_trace,"<ret:%d>\n",ret);
  619. if(ret<0)
  620. {
  621. mylog(log_fatal,"");
  622. perror("raw send error\n");
  623. //printf("send error\n");
  624. }
  625. return 0;
  626. }
  627. */
  628. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  629. {
  630. packet_info_t &send_info=raw_info.send_info;
  631. packet_info_t &recv_info=raw_info.recv_info;
  632. static char recv_raw_icmp_buf[buf_len];
  633. char * ip_payload;
  634. int ip_payloadlen;
  635. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  636. {
  637. mylog(log_debug,"recv_raw_ip error\n");
  638. return -1;
  639. }
  640. if(recv_info.protocol!=IPPROTO_ICMP)
  641. {
  642. //printf("not udp protocol\n");
  643. return -1;
  644. }
  645. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  646. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  647. if(program_mode==client_mode)
  648. {
  649. if(icmph->type!=0)
  650. return -1;
  651. }
  652. else
  653. {
  654. if(icmph->type!=8)
  655. return -1;
  656. }
  657. if(icmph->code!=0)
  658. return -1;
  659. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  660. if(check!=0)
  661. {
  662. mylog(log_debug,"icmp checksum fail %x\n",check);
  663. return -1;
  664. }
  665. payload=ip_payload+sizeof(icmphdr);
  666. payloadlen=ip_payloadlen-sizeof(icmphdr);
  667. mylog(log_debug,"get a packet len=%d\n",payloadlen);
  668. return 0;
  669. }
  670. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  671. {
  672. packet_info_t &send_info=raw_info.send_info;
  673. packet_info_t &recv_info=raw_info.recv_info;
  674. static char recv_raw_udp_buf[buf_len];
  675. char * ip_payload;
  676. int ip_payloadlen;
  677. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  678. {
  679. mylog(log_debug,"recv_raw_ip error\n");
  680. return -1;
  681. }
  682. if(recv_info.protocol!=IPPROTO_UDP)
  683. {
  684. //printf("not udp protocol\n");
  685. return -1;
  686. }
  687. if(ip_payloadlen<int( sizeof(udphdr) ))
  688. {
  689. mylog(log_debug,"too short to hold udpheader\n");
  690. return -1;
  691. }
  692. udphdr *udph=(struct udphdr*)ip_payload;
  693. if(ntohs(udph->len)!=ip_payloadlen)
  694. {
  695. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  696. return -1;
  697. }
  698. if(udph->dest!=ntohs(uint16_t(filter_port)))
  699. {
  700. //printf("%x %x",tcph->dest,);
  701. return -1;
  702. }
  703. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  704. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  705. psh->source_address = recv_info.src_ip;
  706. psh->dest_address = recv_info.dst_ip;
  707. psh->placeholder = 0;
  708. psh->protocol = IPPROTO_UDP;
  709. psh->tcp_length = htons(ip_payloadlen);
  710. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  711. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  712. if(udp_chk!=0)
  713. {
  714. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  715. mylog(log_debug,"udp header error\n");
  716. return -1;
  717. }
  718. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  719. recv_info.src_port=ntohs(udph->source);
  720. recv_info.dst_port=ntohs(udph->dest);
  721. payloadlen = ip_payloadlen-sizeof(udphdr);
  722. payload=udp_begin+sizeof(udphdr);
  723. return 0;
  724. }
  725. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  726. {
  727. packet_info_t &send_info=raw_info.send_info;
  728. packet_info_t &recv_info=raw_info.recv_info;
  729. static char recv_raw_tcp_buf[buf_len];
  730. char * ip_payload;
  731. int ip_payloadlen;
  732. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  733. {
  734. mylog(log_debug,"recv_raw_ip error\n");
  735. return -1;
  736. }
  737. if(recv_info.protocol!=IPPROTO_TCP)
  738. {
  739. //printf("not tcp protocol\n");
  740. return -1;
  741. }
  742. tcphdr * tcph=(struct tcphdr*)ip_payload;
  743. unsigned short tcphdrlen = tcph->doff*4;
  744. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  745. mylog(log_debug,"tcph error\n");
  746. return 0;
  747. }
  748. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  749. {
  750. //printf("%x %x",tcph->dest,);
  751. return -1;
  752. }
  753. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  754. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  755. psh->source_address = recv_info.src_ip;
  756. psh->dest_address = recv_info.dst_ip;
  757. psh->placeholder = 0;
  758. psh->protocol = IPPROTO_TCP;
  759. psh->tcp_length = htons(ip_payloadlen);
  760. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  761. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  762. if(tcp_chk!=0)
  763. {
  764. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  765. mylog(log_debug,"tcp header error\n");
  766. return -1;
  767. }
  768. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  769. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  770. recv_info.has_ts=0;
  771. if(tcph->doff==10)
  772. {
  773. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  774. {
  775. recv_info.has_ts=1;
  776. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  777. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  778. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  779. }
  780. }
  781. else if(tcph->doff==8)
  782. {
  783. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  784. {
  785. recv_info.has_ts=1;
  786. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  787. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  788. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  789. }
  790. }
  791. if(tcph->rst==1)
  792. {
  793. mylog(log_warn,"%%%%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  794. }
  795. recv_info.ack=tcph->ack;
  796. recv_info.syn=tcph->syn;
  797. recv_info.rst=tcph->rst;
  798. recv_info.src_port=ntohs(tcph->source);
  799. recv_info.dst_port=ntohs(tcph->dest);
  800. recv_info.seq=ntohl(tcph->seq);
  801. recv_info.ack_seq=ntohl(tcph->ack_seq);
  802. recv_info.psh=tcph->psh;
  803. if(recv_info.has_ts)
  804. {
  805. send_info.ts_ack=recv_info.ts;
  806. }
  807. payloadlen = ip_payloadlen-tcphdrlen;
  808. payload=tcp_begin+tcphdrlen;
  809. return 0;
  810. }
  811. /*
  812. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  813. {
  814. static char buf[buf_len];
  815. char raw_recv_buf[buf_len];
  816. char raw_recv_buf2[buf_len];
  817. char raw_recv_buf3[buf_len];
  818. iphdr * iph;
  819. tcphdr * tcph;
  820. int size;
  821. struct sockaddr saddr;
  822. socklen_t saddr_size;
  823. saddr_size = sizeof(saddr);
  824. mylog(log_trace,"raw!\n");
  825. size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
  826. if(buf[12]!=8||buf[13]!=0)
  827. {
  828. mylog(log_debug,"not an ipv4 packet!\n");
  829. return -1;
  830. }
  831. char *ip_begin=buf+14;
  832. iph = (struct iphdr *) (ip_begin);
  833. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  834. mylog(log_debug,"iph ihl error");
  835. return -1;
  836. }
  837. if (iph->protocol != IPPROTO_TCP) {
  838. mylog(log_debug,"iph protocal != tcp\n");
  839. return -1;
  840. }
  841. int ip_len=ntohs(iph->tot_len);
  842. unsigned short iphdrlen =iph->ihl*4;
  843. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  844. unsigned short tcphdrlen = tcph->doff*4;
  845. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  846. mylog(log_debug,"tcph error");
  847. return 0;
  848. }
  849. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  850. {
  851. //printf("%x %x",tcph->dest,);
  852. return -1;
  853. }
  854. /////ip
  855. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  856. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  857. /////ip end
  858. ///tcp
  859. struct pseudo_header psh;
  860. psh.source_address = iph->saddr;
  861. psh.dest_address = iph->daddr;
  862. psh.placeholder = 0;
  863. psh.protocol = IPPROTO_TCP;
  864. psh.tcp_length = htons(ip_len-iphdrlen);
  865. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  866. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  867. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  868. if(ip_chk!=0)
  869. {
  870. mylog(log_debug,"ip header error %d\n",ip_chk);
  871. return -1;
  872. }
  873. if(tcp_chk!=0)
  874. {
  875. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  876. mylog(log_debug,"tcp header error\n");
  877. return -1;
  878. }
  879. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  880. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  881. info.has_ts=0;
  882. if(tcph->doff==10)
  883. {
  884. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  885. {
  886. info.has_ts=1;
  887. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  888. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  889. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  890. }
  891. }
  892. else if(tcph->doff==8)
  893. {
  894. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  895. {
  896. info.has_ts=1;
  897. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  898. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  899. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  900. }
  901. }
  902. if(tcph->rst==1)
  903. {
  904. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  905. }
  906. info.ack=tcph->ack;
  907. info.syn=tcph->syn;
  908. info.rst=tcph->rst;
  909. info.src_port=ntohs(tcph->source);
  910. info.src_ip=iph->saddr;
  911. info.seq=ntohl(tcph->seq);
  912. info.ack_seq=ntohl(tcph->ack_seq);
  913. info.psh=tcph->psh;
  914. if(info.has_ts)
  915. {
  916. g_packet_info_send.ts_ack=info.ts;
  917. }
  918. ////tcp end
  919. payloadlen = ip_len-tcphdrlen-iphdrlen;
  920. payload=ip_begin+tcphdrlen+iphdrlen;
  921. if(payloadlen>0&&payload[0]=='h')
  922. {
  923. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  924. }
  925. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  926. {
  927. //if(seq_increse)
  928. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  929. }
  930. //printf("%d\n",ip_len);
  931. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  932. return 0;
  933. }*/
  934. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  935. {
  936. packet_info_t &send_info=raw_info.send_info;
  937. packet_info_t &recv_info=raw_info.recv_info;
  938. 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);
  939. switch(raw_mode)
  940. {
  941. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  942. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  943. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  944. default:return -1;
  945. }
  946. }
  947. int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen)
  948. {
  949. packet_info_t &send_info=raw_info.send_info;
  950. packet_info_t &recv_info=raw_info.recv_info;
  951. switch(raw_mode)
  952. {
  953. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  954. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  955. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  956. default: return -1;
  957. }
  958. }