network.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. u32_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=1;
  14. int filter_port=-1;
  15. int disable_bpf_filter=0; //for test only,most time no need to disable this
  16. u32_t bind_address_uint32=0;
  17. int lower_level=0;
  18. int lower_level_manual=0;
  19. int ifindex=-1;
  20. char if_name[100]="";
  21. unsigned short g_ip_id_counter=0;
  22. unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
  23. {0xff,0xff,0xff,0xff,0xff,0xff,0,0};
  24. //{0x00,0x23,0x45,0x67,0x89,0xb9};
  25. struct sock_filter code_tcp_old[] = {
  26. { 0x28, 0, 0, 0x0000000c },//0
  27. { 0x15, 0, 10, 0x00000800 },//1
  28. { 0x30, 0, 0, 0x00000017 },//2
  29. { 0x15, 0, 8, 0x00000006 },//3
  30. { 0x28, 0, 0, 0x00000014 },//4
  31. { 0x45, 6, 0, 0x00001fff },//5
  32. { 0xb1, 0, 0, 0x0000000e },//6
  33. { 0x48, 0, 0, 0x0000000e },//7
  34. { 0x15, 2, 0, 0x0000ef32 },//8
  35. { 0x48, 0, 0, 0x00000010 },//9
  36. { 0x15, 0, 1, 0x0000ef32 },//10
  37. { 0x6, 0, 0, 0x0000ffff },//11
  38. { 0x6, 0, 0, 0x00000000 },//12
  39. };
  40. struct sock_filter code_tcp[] = {
  41. //{ 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  42. //{ 0x5, 0, 0, 0x00000000 },//1
  43. { 0x30, 0, 0, 0x00000009 },//2
  44. { 0x15, 0, 6, 0x00000006 },//3
  45. { 0x28, 0, 0, 0x00000006 },//4
  46. { 0x45, 4, 0, 0x00001fff },//5
  47. { 0xb1, 0, 0, 0x00000000 },//6
  48. { 0x48, 0, 0, 0x00000002 },//7
  49. { 0x15, 0, 1, 0x0000fffe },//8 //modify this fffe to the port you listen on
  50. { 0x6, 0, 0, 0x0000ffff },//9
  51. { 0x6, 0, 0, 0x00000000 },//10
  52. };
  53. int code_tcp_port_index=6;
  54. struct sock_filter code_udp[] = {
  55. //{ 0x5, 0, 0, 0x00000001 },
  56. //{ 0x5, 0, 0, 0x00000000 },
  57. { 0x30, 0, 0, 0x00000009 },
  58. { 0x15, 0, 6, 0x00000011 },
  59. { 0x28, 0, 0, 0x00000006 },
  60. { 0x45, 4, 0, 0x00001fff },
  61. { 0xb1, 0, 0, 0x00000000 },
  62. { 0x48, 0, 0, 0x00000002 },
  63. { 0x15, 0, 1, 0x0000fffe }, //modify this fffe to the port you listen on
  64. { 0x6, 0, 0, 0x0000ffff },
  65. { 0x6, 0, 0, 0x00000000 },
  66. };
  67. int code_udp_port_index=6;
  68. struct sock_filter code_icmp[] = {
  69. //{ 0x5, 0, 0, 0x00000001 },
  70. //{ 0x5, 0, 0, 0x00000000 },
  71. { 0x30, 0, 0, 0x00000009 },
  72. { 0x15, 0, 1, 0x00000001 },
  73. { 0x6, 0, 0, 0x0000ffff },
  74. { 0x6, 0, 0, 0x00000000 },
  75. };
  76. /*
  77. tcpdump -i eth1 ip and icmp -d
  78. (000) ldh [12]
  79. (001) jeq #0x800 jt 2 jf 5
  80. (002) ldb [23]
  81. (003) jeq #0x1 jt 4 jf 5
  82. (004) ret #65535
  83. (005) ret #0
  84. tcpdump -i eth1 ip and icmp -dd
  85. { 0x28, 0, 0, 0x0000000c },
  86. { 0x15, 0, 3, 0x00000800 },
  87. { 0x30, 0, 0, 0x00000017 },
  88. { 0x15, 0, 1, 0x00000001 },
  89. { 0x6, 0, 0, 0x0000ffff },
  90. { 0x6, 0, 0, 0x00000000 },
  91. */
  92. /*
  93. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  94. { 0x28, 0, 0, 0x0000000c },
  95. { 0x15, 0, 8, 0x00000800 },
  96. { 0x30, 0, 0, 0x00000017 },
  97. { 0x15, 0, 6, 0x00000006 },
  98. { 0x28, 0, 0, 0x00000014 },
  99. { 0x45, 4, 0, 0x00001fff },
  100. { 0xb1, 0, 0, 0x0000000e },
  101. { 0x48, 0, 0, 0x00000010 },
  102. { 0x15, 0, 1, 0x0000fffe },
  103. { 0x6, 0, 0, 0x0000ffff },
  104. { 0x6, 0, 0, 0x00000000 },
  105. (000) ldh [12]
  106. (001) jeq #0x800 jt 2 jf 10
  107. (002) ldb [23]
  108. (003) jeq #0x6 jt 4 jf 10
  109. (004) ldh [20]
  110. (005) jset #0x1fff jt 10 jf 6
  111. (006) ldxb 4*([14]&0xf)
  112. (007) ldh [x + 16]
  113. (008) jeq #0xfffe jt 9 jf 10
  114. (009) ret #65535
  115. (010) ret #0
  116. */
  117. packet_info_t::packet_info_t()
  118. {
  119. src_port=0;
  120. dst_port=0;
  121. if (raw_mode == mode_faketcp)
  122. {
  123. protocol = IPPROTO_TCP;
  124. ack_seq = get_true_random_number();
  125. seq = get_true_random_number();
  126. has_ts=0;
  127. ts_ack=0;
  128. syn=0;
  129. ack=1;
  130. //mylog(log_info,"<cons ,ts_ack= %u>\n",ts_ack);
  131. }
  132. else if (raw_mode == mode_udp)
  133. {
  134. protocol = IPPROTO_UDP;
  135. }
  136. else if (raw_mode == mode_icmp)
  137. {
  138. protocol = IPPROTO_ICMP;
  139. icmp_seq=0;
  140. }
  141. }
  142. int init_raw_socket()
  143. {
  144. g_ip_id_counter=get_true_random_number()%65535;
  145. if(lower_level==0)
  146. {
  147. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  148. if(raw_send_fd == -1) {
  149. mylog(log_fatal,"Failed to create raw_send_fd\n");
  150. //perror("Failed to create raw_send_fd");
  151. myexit(1);
  152. }
  153. int one = 1;
  154. const int *val = &one;
  155. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  156. mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
  157. //perror("Error setting IP_HDRINCL");
  158. myexit(2);
  159. }
  160. }
  161. else
  162. {
  163. raw_send_fd = socket(PF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));
  164. if(raw_send_fd == -1) {
  165. mylog(log_fatal,"Failed to create raw_send_fd\n");
  166. //perror("Failed to create raw_send_fd");
  167. myexit(1);
  168. }
  169. //init_ifindex(if_name);
  170. }
  171. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  172. {
  173. mylog(log_fatal,"SO_SNDBUFFORCE fail\n");
  174. myexit(1);
  175. }
  176. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  177. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  178. if(raw_recv_fd == -1) {
  179. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  180. //perror("");
  181. myexit(1);
  182. }
  183. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  184. {
  185. mylog(log_fatal,"SO_RCVBUFFORCE fail\n");
  186. myexit(1);
  187. }
  188. //IP_HDRINCL to tell the kernel that headers are included in the packet
  189. setnonblocking(raw_send_fd); //not really necessary
  190. setnonblocking(raw_recv_fd);
  191. return 0;
  192. }
  193. void init_filter(int port)
  194. {
  195. sock_fprog bpf;
  196. if(raw_mode==mode_faketcp||raw_mode==mode_udp)
  197. {
  198. filter_port=port;
  199. }
  200. if(disable_bpf_filter) return;
  201. //if(raw_mode==mode_icmp) return ;
  202. //code_tcp[8].k=code_tcp[10].k=port;
  203. if(raw_mode==mode_faketcp)
  204. {
  205. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  206. code_tcp[code_tcp_port_index].k=port;
  207. bpf.filter = code_tcp;
  208. }
  209. else if(raw_mode==mode_udp)
  210. {
  211. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  212. code_udp[code_udp_port_index].k=port;
  213. bpf.filter = code_udp;
  214. }
  215. else if(raw_mode==mode_icmp)
  216. {
  217. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  218. bpf.filter = code_icmp;
  219. }
  220. int dummy;
  221. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  222. if (ret != 0)
  223. {
  224. mylog(log_debug,"error remove fiter\n");
  225. //perror("filter");
  226. //exit(-1);
  227. }
  228. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  229. if (ret != 0)
  230. {
  231. mylog(log_fatal,"error set fiter\n");
  232. //perror("filter");
  233. myexit(-1);
  234. }
  235. }
  236. void remove_filter()
  237. {
  238. filter_port=0;
  239. int dummy;
  240. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  241. if (ret != 0)
  242. {
  243. mylog(log_debug,"error remove fiter\n");
  244. //perror("filter");
  245. //exit(-1);
  246. }
  247. }
  248. int init_ifindex(const char * if_name,int &index)
  249. {
  250. struct ifreq ifr;
  251. size_t if_name_len=strlen(if_name);
  252. if (if_name_len<sizeof(ifr.ifr_name)) {
  253. memcpy(ifr.ifr_name,if_name,if_name_len);
  254. ifr.ifr_name[if_name_len]=0;
  255. } else {
  256. mylog(log_fatal,"interface name is too long\n");
  257. myexit(-1);
  258. }
  259. if (ioctl(raw_send_fd,SIOCGIFINDEX,&ifr)==-1) {
  260. mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
  261. myexit(-1);
  262. }
  263. index=ifr.ifr_ifindex;
  264. mylog(log_info,"ifname:%s ifindex:%d\n",if_name,index);
  265. return 0;
  266. }
  267. bool interface_has_arp(const char * interface) {
  268. struct ifreq ifr;
  269. // int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
  270. int sock=raw_send_fd;
  271. memset(&ifr, 0, sizeof(ifr));
  272. strcpy(ifr.ifr_name, interface);
  273. if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
  274. //perror("SIOCGIFFLAGS");
  275. mylog(log_fatal,"ioctl(sock, SIOCGIFFLAGS, &ifr) failed for interface %s,errno %s\n",interface,strerror(errno));
  276. myexit(-1);
  277. }
  278. //close(sock);
  279. return !(ifr.ifr_flags & IFF_NOARP);
  280. }
  281. struct route_info_t
  282. {
  283. string if_name;
  284. u32_t dest;
  285. u32_t mask;
  286. u32_t gw;
  287. u32_t flag;
  288. };
  289. int dest_idx=1;
  290. int gw_idx=2;
  291. int if_idx=0;
  292. int mask_idx=7;
  293. int flag_idx=3;
  294. vector<int> find_route_entry(const vector<route_info_t> &route_info_vec,u32_t ip)
  295. {
  296. vector<int> res;
  297. for(u32_t i=0;i<=32;i++)
  298. {
  299. u32_t mask=0xffffffff;
  300. //mask >>=i;
  301. //if(i==32) mask=0; //why 0xffffffff>>32 equals 0xffffffff??
  302. mask <<=i;
  303. if(i==32) mask=0;
  304. log_bare(log_debug,"(mask:%x)",mask);
  305. for(u32_t j=0;j<route_info_vec.size();j++)
  306. {
  307. const route_info_t & info=route_info_vec[j];
  308. if(info.mask!=mask)
  309. continue;
  310. log_bare(log_debug,"<<%d,%d>>",i,j);
  311. if((info.dest&mask)==(ip&mask))
  312. {
  313. log_bare(log_debug,"found!");
  314. res.push_back(j);
  315. }
  316. }
  317. if(res.size()!=0)
  318. {
  319. return res;
  320. }
  321. }
  322. return res;
  323. }
  324. int find_direct_dest(const vector<route_info_t> &route_info_vec,u32_t ip,u32_t &dest_ip,string &if_name)
  325. {
  326. vector<int> res;
  327. for(int i=0;i<1000;i++)
  328. {
  329. res=find_route_entry(route_info_vec,ip);
  330. log_bare(log_debug,"<entry:%u>",(u32_t)res.size());
  331. if(res.size()==0)
  332. {
  333. mylog(log_error,"cant find route entry\n");
  334. return -1;
  335. }
  336. if(res.size()>1)
  337. {
  338. mylog(log_error,"found duplicated entries\n");
  339. return -1;
  340. }
  341. if((route_info_vec[res[0]].flag&2)==0)
  342. {
  343. dest_ip=ip;
  344. if_name=route_info_vec[res[0]].if_name;
  345. return 0;
  346. }
  347. else
  348. {
  349. ip=route_info_vec[res[0]].gw;
  350. }
  351. }
  352. mylog(log_error,"dead loop in find_direct_dest\n");
  353. return -1;
  354. }
  355. struct arp_info_t
  356. {
  357. u32_t ip;
  358. string hw;
  359. string if_name;
  360. };
  361. int arp_ip_idx=0;
  362. int arp_hw_idx=3;
  363. int arp_if_idx=5;
  364. int find_arp(const vector<arp_info_t> &arp_info_vec,u32_t ip,string if_name,string &hw)
  365. {
  366. int pos=-1;
  367. int count=0;
  368. for(u32_t i=0;i<arp_info_vec.size();i++)
  369. {
  370. const arp_info_t & info=arp_info_vec[i];
  371. if(info.if_name!=if_name) continue;
  372. if(info.ip==ip)
  373. {
  374. count++;
  375. pos=i;
  376. }
  377. }
  378. if(count==0)
  379. {
  380. //mylog(log_warn,"cant find arp entry for %s %s,using 00:00:00:00:00:00\n",my_ntoa(ip),if_name.c_str());
  381. //hw="00:00:00:00:00:00";
  382. mylog(log_error,"cant find arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  383. return -1;
  384. }
  385. if(count>1)
  386. {
  387. mylog(log_error,"find multiple arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  388. return -1;
  389. }
  390. hw=arp_info_vec[pos].hw;
  391. return 0;
  392. }
  393. int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw)
  394. {
  395. ip=htonl(ip);
  396. if(ip==htonl(inet_addr("127.0.0.1")))
  397. {
  398. dest_ip=ntohl(ip);
  399. if_name="lo";
  400. hw="00:00:00:00:00:00";
  401. return 0;
  402. }
  403. string route_file;
  404. if(read_file("/proc/net/route",route_file)!=0) return -1;
  405. string arp_file;
  406. if(read_file("/proc/net/arp",arp_file)!=0) return -1;
  407. log_bare(log_debug,"/proc/net/route:<<%s>>\n",route_file.c_str());
  408. log_bare(log_debug,"/proc/net/arp:<<%s>>\n",route_file.c_str());
  409. auto route_vec2=string_to_vec2(route_file.c_str());
  410. vector<route_info_t> route_info_vec;
  411. for(u32_t i=1;i<route_vec2.size();i++)
  412. {
  413. log_bare(log_debug,"<size:%u>",(u32_t)route_vec2[i].size());
  414. if(route_vec2[i].size()!=11)
  415. {
  416. mylog(log_error,"route coloum %d !=11 \n",int(route_vec2[i].size()));
  417. return -1;
  418. }
  419. route_info_t tmp;
  420. tmp.if_name=route_vec2[i][if_idx];
  421. if(hex_to_u32_with_endian(route_vec2[i][dest_idx],tmp.dest)!=0) return -1;
  422. if(hex_to_u32_with_endian(route_vec2[i][gw_idx],tmp.gw)!=0) return -1;
  423. if(hex_to_u32_with_endian(route_vec2[i][mask_idx],tmp.mask)!=0) return -1;
  424. if(hex_to_u32(route_vec2[i][flag_idx],tmp.flag)!=0)return -1;
  425. route_info_vec.push_back(tmp);
  426. for(u32_t j=0;j<route_vec2[i].size();j++)
  427. {
  428. log_bare(log_debug,"<%s>",route_vec2[i][j].c_str());
  429. }
  430. log_bare(log_debug,"%s dest:%x mask:%x gw:%x flag:%x",tmp.if_name.c_str(),tmp.dest,tmp.mask,tmp.gw,tmp.flag);
  431. log_bare(log_debug,"\n");
  432. }
  433. if(find_direct_dest(route_info_vec,ip,dest_ip,if_name)!=0)
  434. {
  435. mylog(log_error,"find_direct_dest failed for ip %s\n",my_ntoa(ntohl(ip)));
  436. return -1;
  437. }
  438. log_bare(log_debug,"========\n");
  439. auto arp_vec2=string_to_vec2(arp_file.c_str());
  440. vector<arp_info_t> arp_info_vec;
  441. for(u32_t i=1;i<arp_vec2.size();i++)
  442. {
  443. log_bare(log_debug,"<<arp_vec2[i].size(): %d>>",(int)arp_vec2[i].size());
  444. for(u32_t j=0;j<arp_vec2[i].size();j++)
  445. {
  446. log_bare(log_debug,"<%s>",arp_vec2[i][j].c_str());
  447. }
  448. if(arp_vec2[i].size()!=6)
  449. {
  450. mylog(log_error,"arp coloum %d !=11 \n",int(arp_vec2[i].size()));
  451. return -1;
  452. }
  453. arp_info_t tmp;
  454. tmp.if_name=arp_vec2[i][arp_if_idx];
  455. tmp.hw=arp_vec2[i][arp_hw_idx];
  456. tmp.ip=htonl(inet_addr(arp_vec2[i][arp_ip_idx].c_str()));
  457. arp_info_vec.push_back(tmp);
  458. log_bare(log_debug,"\n");
  459. }
  460. if(!interface_has_arp(if_name.c_str()))
  461. {
  462. mylog(log_info,"%s is a noarp interface,using 00:00:00:00:00:00\n",if_name.c_str());
  463. hw="00:00:00:00:00:00";
  464. }
  465. else if(find_arp(arp_info_vec,dest_ip,if_name,hw)!=0)
  466. {
  467. mylog(log_error,"find_arp failed for dest_ip %s ,if_name %s\n",my_ntoa(ntohl(ip)),if_name.c_str());
  468. return -1;
  469. }
  470. //printf("%s\n",hw.c_str());
  471. dest_ip=ntohl(dest_ip);
  472. return 0;
  473. }
  474. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  475. {
  476. const packet_info_t &send_info=raw_info.send_info;
  477. const packet_info_t &recv_info=raw_info.recv_info;
  478. char send_raw_ip_buf[buf_len];
  479. struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
  480. memset(iph,0,sizeof(iphdr));
  481. iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  482. iph->version = 4;
  483. iph->tos = 0;
  484. if(lower_level)
  485. {
  486. //iph->id=0;
  487. iph->id = htons (g_ip_id_counter++); //Id of this packet
  488. }
  489. else
  490. iph->id = htons (g_ip_id_counter++); //Id of this packet
  491. //iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero ,or really?????// todo //seems like there is a problem
  492. iph->frag_off = htons(0x4000); //DF set,others are zero
  493. // iph->frag_off = htons(0x0000); //DF set,others are zero
  494. iph->ttl = 64;
  495. iph->protocol = send_info.protocol;
  496. iph->check = 0; //Set to 0 before calculating checksum
  497. iph->saddr = send_info.src_ip; //Spoof the source ip address
  498. iph->daddr = send_info.dst_ip;
  499. uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
  500. if(lower_level)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
  501. else
  502. iph->tot_len = 0;
  503. memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
  504. if(lower_level) iph->check =
  505. csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
  506. else
  507. iph->check=0;
  508. int ret;
  509. if(lower_level==0)
  510. {
  511. struct sockaddr_in sin={0};
  512. sin.sin_family = AF_INET;
  513. //sin.sin_port = htons(info.dst_port); //dont need this
  514. sin.sin_addr.s_addr = send_info.dst_ip;
  515. ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  516. }
  517. else
  518. {
  519. struct sockaddr_ll addr={0}; //={0} not necessary
  520. memcpy(&addr,&send_info.addr_ll,sizeof(addr));
  521. ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &addr, sizeof (addr));
  522. }
  523. if(ret==-1)
  524. {
  525. mylog(log_trace,"sendto failed\n");
  526. //perror("why?");
  527. return -1;
  528. }
  529. else
  530. {
  531. //mylog(log_info,"sendto succ\n");
  532. }
  533. return 0;
  534. }
  535. int peek_raw(packet_info_t &peek_info)
  536. { static char peek_raw_buf[buf_len];
  537. char *ip_begin=peek_raw_buf+link_level_header_len;
  538. struct sockaddr saddr={0};
  539. socklen_t saddr_size=sizeof(saddr);
  540. int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,max_data_len, MSG_PEEK ,&saddr , &saddr_size);//change max_data_len to something smaller,we only need header here
  541. iphdr * iph = (struct iphdr *) (ip_begin);
  542. //mylog(log_info,"recv_len %d\n",recv_len);
  543. if(recv_len<int(sizeof(iphdr)))
  544. {
  545. mylog(log_trace,"failed here %d %d\n",recv_len,int(sizeof(iphdr)));
  546. mylog(log_trace,"%s\n ",strerror(errno));
  547. return -1;
  548. }
  549. peek_info.src_ip=iph->saddr;
  550. unsigned short iphdrlen =iph->ihl*4;
  551. char *payload=ip_begin+iphdrlen;
  552. //mylog(log_info,"protocol %d\n",iph->protocol);
  553. switch(raw_mode)
  554. {
  555. case mode_faketcp:
  556. {
  557. if(iph->protocol!=IPPROTO_TCP)
  558. {
  559. mylog(log_trace,"failed here");
  560. return -1;
  561. }
  562. struct tcphdr *tcph=(tcphdr *)payload;
  563. if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
  564. {
  565. mylog(log_trace,"failed here");
  566. return -1;
  567. }
  568. peek_info.src_port=ntohs(tcph->source);
  569. peek_info.syn=tcph->syn;
  570. break;
  571. }
  572. case mode_udp:
  573. {
  574. if(iph->protocol!=IPPROTO_UDP) return -1;
  575. struct udphdr *udph=(udphdr *)payload;
  576. if(recv_len<int(iphdrlen+sizeof(udphdr)))
  577. return -1;
  578. peek_info.src_port=ntohs(udph->source);
  579. break;
  580. }
  581. case mode_icmp:
  582. {
  583. if(iph->protocol!=IPPROTO_ICMP) return -1;
  584. struct icmphdr *icmph=(icmphdr *)payload;
  585. if(recv_len<int( iphdrlen+sizeof(icmphdr) ))
  586. return -1;
  587. peek_info.src_port=ntohs(icmph->id);
  588. break;
  589. }
  590. default:return -1;
  591. }
  592. return 0;
  593. }
  594. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  595. {
  596. const packet_info_t &send_info=raw_info.send_info;
  597. packet_info_t &recv_info=raw_info.recv_info;
  598. static char recv_raw_ip_buf[buf_len];
  599. iphdr * iph;
  600. struct sockaddr_ll saddr={0};
  601. socklen_t saddr_size = sizeof(saddr);
  602. int flag=0;
  603. int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len, flag ,(sockaddr*)&saddr , &saddr_size);
  604. if(recv_len<0)
  605. {
  606. mylog(log_trace,"recv_len %d\n",recv_len);
  607. return -1;
  608. }
  609. if(recv_len<int(link_level_header_len))
  610. {
  611. mylog(log_trace,"length error\n");
  612. }
  613. if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
  614. {
  615. mylog(log_trace,"not an ipv4 packet!\n");
  616. return -1;
  617. }
  618. char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
  619. iph = (struct iphdr *) (ip_begin);
  620. recv_info.src_ip=iph->saddr;
  621. recv_info.dst_ip=iph->daddr;
  622. recv_info.protocol=iph->protocol;
  623. if(lower_level)
  624. {
  625. memcpy(&recv_info.addr_ll,&saddr,sizeof(recv_info.addr_ll));
  626. }
  627. if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32)
  628. {
  629. mylog(log_trace,"bind adress doenst match, dropped\n");
  630. //printf(" bind adress doenst match, dropped\n");
  631. return -1;
  632. }
  633. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  634. mylog(log_trace,"iph ihl error\n");
  635. return -1;
  636. }
  637. int ip_len=ntohs(iph->tot_len);
  638. if(recv_len-int(link_level_header_len) <ip_len)
  639. {
  640. mylog(log_debug,"incomplete packet\n");
  641. return -1;
  642. }
  643. unsigned short iphdrlen =iph->ihl*4;
  644. u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  645. if(ip_chk!=0)
  646. {
  647. mylog(log_debug,"ip header error %x\n",ip_chk);
  648. return -1;
  649. }
  650. payload=ip_begin+iphdrlen;
  651. payloadlen=ip_len-iphdrlen;
  652. if(payloadlen<0)
  653. {
  654. mylog(log_warn,"error payload len\n");
  655. return -1;
  656. }
  657. return 0;
  658. }
  659. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  660. {
  661. const packet_info_t &send_info=raw_info.send_info;
  662. const packet_info_t &recv_info=raw_info.recv_info;
  663. char send_raw_icmp_buf[buf_len];
  664. icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
  665. memset(icmph,0,sizeof(icmphdr));
  666. if(program_mode==client_mode)
  667. {
  668. icmph->type=8;
  669. }
  670. else
  671. {
  672. icmph->type=0;
  673. }
  674. icmph->code=0;
  675. icmph->id=htons(send_info.src_port);
  676. icmph->seq=htons(send_info.icmp_seq); /////////////modify
  677. memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
  678. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
  679. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
  680. {
  681. return -1;
  682. }
  683. /*if(program_mode==client_mode)
  684. {
  685. send_info.icmp_seq++;
  686. }*/
  687. return 0;
  688. }
  689. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  690. {
  691. const packet_info_t &send_info=raw_info.send_info;
  692. const packet_info_t &recv_info=raw_info.recv_info;
  693. char send_raw_udp_buf[buf_len];
  694. udphdr *udph=(struct udphdr *) (send_raw_udp_buf
  695. + sizeof(struct pseudo_header));
  696. memset(udph,0,sizeof(udphdr));
  697. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
  698. udph->source = htons(send_info.src_port);
  699. udph->dest = htons(send_info.dst_port);
  700. int udp_tot_len=payloadlen+sizeof(udphdr);
  701. if(udp_tot_len>65535)
  702. {
  703. mylog(log_debug,"invalid len\n");
  704. return -1;
  705. }
  706. mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
  707. udph->len=htons(uint16_t(udp_tot_len));
  708. memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udphdr),payload,payloadlen);
  709. psh->source_address = send_info.src_ip;
  710. psh->dest_address = send_info.dst_ip;
  711. psh->placeholder = 0;
  712. psh->protocol = IPPROTO_UDP;
  713. psh->tcp_length = htons(uint16_t(udp_tot_len));
  714. int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
  715. udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
  716. if(send_raw_ip(raw_info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
  717. {
  718. return -1;
  719. }
  720. return 0;
  721. }
  722. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  723. const packet_info_t &send_info=raw_info.send_info;
  724. const packet_info_t &recv_info=raw_info.recv_info;
  725. //mylog(log_debug,"syn %d\n",send_info.syn);
  726. char send_raw_tcp_buf0[buf_len];
  727. char *send_raw_tcp_buf=send_raw_tcp_buf0;
  728. struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
  729. + sizeof(struct pseudo_header));
  730. memset(tcph,0,sizeof(tcphdr));
  731. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
  732. //TCP Header
  733. tcph->source = htons(send_info.src_port);
  734. tcph->dest = htons(send_info.dst_port);
  735. tcph->seq = htonl(send_info.seq);
  736. tcph->ack_seq = htonl(send_info.ack_seq);
  737. tcph->fin = 0;
  738. tcph->syn = send_info.syn;
  739. tcph->rst = 0;
  740. tcph->psh = send_info.psh;
  741. tcph->ack = send_info.ack;
  742. if (tcph->syn == 1) {
  743. tcph->doff = 10; //tcp header size
  744. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  745. send_raw_tcp_buf[i++] = 0x02; //mss
  746. send_raw_tcp_buf[i++] = 0x04;
  747. send_raw_tcp_buf[i++] = 0x05;
  748. send_raw_tcp_buf[i++] = (char)0xb4;
  749. //raw_send_buf[i++]=0x01;
  750. //raw_send_buf[i++]=0x01;
  751. send_raw_tcp_buf[i++] = 0x04; //sack ok
  752. send_raw_tcp_buf[i++] = 0x02; //sack ok
  753. send_raw_tcp_buf[i++] = 0x08; //ts i=6
  754. send_raw_tcp_buf[i++] = 0x0a; //i=7
  755. *(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  756. (u32_t) get_current_time());
  757. i += 4;
  758. //mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
  759. *(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  760. i += 4;
  761. send_raw_tcp_buf[i++] = 0x01;
  762. send_raw_tcp_buf[i++] = 0x03;
  763. send_raw_tcp_buf[i++] = 0x03;
  764. send_raw_tcp_buf[i++] = 0x05;
  765. } else {
  766. tcph->doff = 8;
  767. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  768. send_raw_tcp_buf[i++] = 0x01;
  769. send_raw_tcp_buf[i++] = 0x01;
  770. send_raw_tcp_buf[i++] = 0x08; //ts //i=2
  771. send_raw_tcp_buf[i++] = 0x0a; //i=3;
  772. *(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  773. (u32_t) get_current_time());
  774. i += 4;
  775. //mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
  776. *(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  777. i += 4;
  778. }
  779. tcph->urg = 0;
  780. //tcph->window = htons((uint16_t)(1024));
  781. tcph->window = htons((uint16_t) (10240 + random() % 100));
  782. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  783. tcph->urg_ptr = 0;
  784. char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
  785. memcpy(tcp_data, payload, payloadlen);
  786. psh->source_address = send_info.src_ip;
  787. psh->dest_address = send_info.dst_ip;
  788. psh->placeholder = 0;
  789. psh->protocol = IPPROTO_TCP;
  790. psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
  791. int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  792. tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
  793. int tcp_totlen=tcph->doff*4 + payloadlen;
  794. if(send_raw_ip(raw_info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
  795. {
  796. return -1;
  797. }
  798. raw_info.last_send_len=payloadlen;
  799. return 0;
  800. }
  801. /*
  802. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  803. {
  804. static uint16_t ip_id=1;
  805. char raw_send_buf[buf_len];
  806. char raw_send_buf2[buf_len];
  807. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  808. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  809. char *data;
  810. memset(raw_send_buf,0,payloadlen+100);
  811. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  812. //TCP header
  813. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  814. struct sockaddr_in sin;
  815. struct pseudo_header psh;
  816. //some address resolution
  817. sin.sin_family = AF_INET;
  818. sin.sin_port = htons(info.dst_port);
  819. sin.sin_addr.s_addr = info.dst_ip;
  820. //Fill in the IP Header
  821. iph->ihl = 5;
  822. iph->version = 4;
  823. iph->tos = 0;
  824. iph->id = htonl (ip_id++); //Id of this packet
  825. iph->frag_off = htons(0x4000); //DF set,others are zero
  826. iph->ttl = 64;
  827. iph->protocol = IPPROTO_TCP;
  828. iph->check = 0; //Set to 0 before calculating checksum
  829. iph->saddr = info.src_ip; //Spoof the source ip address
  830. iph->daddr = info.dst_ip;
  831. //TCP Header
  832. tcph->source = htons(info.src_port);
  833. tcph->dest = htons(info.dst_port);
  834. tcph->seq =htonl(info.seq);
  835. tcph->ack_seq = htonl(info.ack_seq);
  836. tcph->fin=0;
  837. tcph->syn=info.syn;
  838. tcph->rst=0;
  839. tcph->psh=info.psh;
  840. tcph->ack=info.ack;
  841. if(tcph->syn==1)
  842. {
  843. tcph->doff = 10; //tcp header size
  844. int i=sizeof (struct iphdr)+20;
  845. raw_send_buf[i++]=0x02;//mss
  846. raw_send_buf[i++]=0x04;
  847. raw_send_buf[i++]=0x05;
  848. raw_send_buf[i++]=0xb4;
  849. //raw_send_buf[i++]=0x01;
  850. //raw_send_buf[i++]=0x01;
  851. raw_send_buf[i++]=0x04; //sack ok
  852. raw_send_buf[i++]=0x02; //sack ok
  853. raw_send_buf[i++]=0x08; //i=6;
  854. raw_send_buf[i++]=0x0a;
  855. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  856. i+=4;
  857. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  858. i+=4;
  859. raw_send_buf[i++]=0x01;
  860. raw_send_buf[i++]=0x03;
  861. raw_send_buf[i++]=0x03;
  862. raw_send_buf[i++]=0x05;
  863. }
  864. else
  865. {
  866. tcph->doff=8;
  867. int i=sizeof (struct iphdr)+20;
  868. raw_send_buf[i++]=0x01;
  869. raw_send_buf[i++]=0x01;
  870. raw_send_buf[i++]=0x08; //i=0;
  871. raw_send_buf[i++]=0x0a;
  872. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  873. i+=4;
  874. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  875. i+=4;
  876. }
  877. tcph->urg=0;
  878. //tcph->window = htons((uint16_t)(1024));
  879. tcph->window = htons((uint16_t)(10240+random()%100));
  880. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  881. tcph->urg_ptr = 0;
  882. //Data part
  883. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  884. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  885. memcpy(data , payload, payloadlen);
  886. psh.source_address = info.src_ip;
  887. psh.dest_address = sin.sin_addr.s_addr;
  888. psh.placeholder = 0;
  889. psh.protocol = IPPROTO_TCP;
  890. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  891. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  892. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  893. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  894. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  895. //Ip checksum
  896. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  897. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  898. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  899. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  900. {
  901. if(seq_mode==0)
  902. {
  903. }
  904. else if(seq_mode==1)
  905. {
  906. g_packet_info_send.seq+=payloadlen;
  907. }
  908. else if(seq_mode==2)
  909. {
  910. if(random()% 5==3 )
  911. g_packet_info_send.seq+=payloadlen;
  912. }
  913. }
  914. mylog(log_trace,"<ret:%d>\n",ret);
  915. if(ret<0)
  916. {
  917. mylog(log_fatal,"");
  918. perror("raw send error\n");
  919. //printf("send error\n");
  920. }
  921. return 0;
  922. }
  923. */
  924. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  925. {
  926. const packet_info_t &send_info=raw_info.send_info;
  927. packet_info_t &recv_info=raw_info.recv_info;
  928. static char recv_raw_icmp_buf[buf_len];
  929. char * ip_payload;
  930. int ip_payloadlen;
  931. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  932. {
  933. mylog(log_debug,"recv_raw_ip error\n");
  934. return -1;
  935. }
  936. if(recv_info.protocol!=IPPROTO_ICMP)
  937. {
  938. //printf("not udp protocol\n");
  939. return -1;
  940. }
  941. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  942. if(ntohs(icmph->id)!=send_info.src_port)
  943. {
  944. mylog(log_debug,"icmp id mis-match,ignored\n");
  945. return -1;
  946. }
  947. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  948. recv_info.icmp_seq=ntohs(icmph->seq);
  949. if(program_mode==client_mode)
  950. {
  951. if(icmph->type!=0)
  952. return -1;
  953. }
  954. else
  955. {
  956. if(icmph->type!=8)
  957. return -1;
  958. }
  959. if(icmph->code!=0)
  960. return -1;
  961. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  962. if(check!=0)
  963. {
  964. mylog(log_debug,"icmp checksum fail %x\n",check);
  965. return -1;
  966. }
  967. //mylog(log_info,"program_mode=%d\n",program_mode);
  968. /*
  969. if(program_mode==server_mode)
  970. {
  971. send_info.icmp_seq=ntohs(icmph->seq);
  972. //mylog(log_info,"send_info.seq=%d\n",send_info.seq);
  973. }*/
  974. payload=ip_payload+sizeof(icmphdr);
  975. payloadlen=ip_payloadlen-sizeof(icmphdr);
  976. mylog(log_trace,"get a packet len=%d\n",payloadlen);
  977. return 0;
  978. }
  979. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  980. {
  981. const packet_info_t &send_info=raw_info.send_info;
  982. packet_info_t &recv_info=raw_info.recv_info;
  983. static char recv_raw_udp_buf[buf_len];
  984. char * ip_payload;
  985. int ip_payloadlen;
  986. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  987. {
  988. mylog(log_debug,"recv_raw_ip error\n");
  989. return -1;
  990. }
  991. if(recv_info.protocol!=IPPROTO_UDP)
  992. {
  993. //printf("not udp protocol\n");
  994. return -1;
  995. }
  996. if(ip_payloadlen<int( sizeof(udphdr) ))
  997. {
  998. mylog(log_debug,"too short to hold udpheader\n");
  999. return -1;
  1000. }
  1001. udphdr *udph=(struct udphdr*)ip_payload;
  1002. if(int(ntohs(udph->len))!=ip_payloadlen)
  1003. {
  1004. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  1005. return -1;
  1006. }
  1007. if(udph->dest!=ntohs(uint16_t(filter_port)))
  1008. {
  1009. //printf("%x %x",tcph->dest,);
  1010. return -1;
  1011. }
  1012. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1013. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  1014. psh->source_address = recv_info.src_ip;
  1015. psh->dest_address = recv_info.dst_ip;
  1016. psh->placeholder = 0;
  1017. psh->protocol = IPPROTO_UDP;
  1018. psh->tcp_length = htons(ip_payloadlen);
  1019. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1020. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  1021. if(udp_chk!=0)
  1022. {
  1023. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  1024. mylog(log_debug,"udp header error\n");
  1025. return -1;
  1026. }
  1027. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  1028. recv_info.src_port=ntohs(udph->source);
  1029. recv_info.dst_port=ntohs(udph->dest);
  1030. payloadlen = ip_payloadlen-sizeof(udphdr);
  1031. payload=udp_begin+sizeof(udphdr);
  1032. return 0;
  1033. }
  1034. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1035. {
  1036. const packet_info_t &send_info=raw_info.send_info;
  1037. packet_info_t &recv_info=raw_info.recv_info;
  1038. static char recv_raw_tcp_buf[buf_len];
  1039. char * ip_payload;
  1040. int ip_payloadlen;
  1041. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1042. {
  1043. mylog(log_debug,"recv_raw_ip error\n");
  1044. return -1;
  1045. }
  1046. if(recv_info.protocol!=IPPROTO_TCP)
  1047. {
  1048. //printf("not tcp protocol\n");
  1049. return -1;
  1050. }
  1051. tcphdr * tcph=(struct tcphdr*)ip_payload;
  1052. unsigned short tcphdrlen = tcph->doff*4;
  1053. if (!(tcphdrlen > 0 && tcphdrlen <=60)) {
  1054. mylog(log_debug,"tcph error\n");
  1055. return 0;
  1056. }
  1057. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1058. {
  1059. //printf("%x %x",tcph->dest,);
  1060. return -1;
  1061. }
  1062. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1063. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  1064. psh->source_address = recv_info.src_ip;
  1065. psh->dest_address = recv_info.dst_ip;
  1066. psh->placeholder = 0;
  1067. psh->protocol = IPPROTO_TCP;
  1068. psh->tcp_length = htons(ip_payloadlen);
  1069. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1070. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  1071. if(tcp_chk!=0)
  1072. {
  1073. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1074. mylog(log_debug,"tcp header error\n");
  1075. return -1;
  1076. }
  1077. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  1078. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1079. recv_info.has_ts=0;
  1080. recv_info.ts=0;
  1081. if(tcph->doff==10)
  1082. {
  1083. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1084. {
  1085. recv_info.has_ts=1;
  1086. recv_info.ts=ntohl(*(u32_t*)(&tcp_option[8]));
  1087. recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[12]));
  1088. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1089. }
  1090. else
  1091. {
  1092. // mylog(log_info,"\n");
  1093. }
  1094. }
  1095. else if(tcph->doff==8)
  1096. {
  1097. if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
  1098. {
  1099. recv_info.has_ts=1;
  1100. recv_info.ts=ntohl(*(u32_t*)(&tcp_option[4]));
  1101. recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[8]));
  1102. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1103. }
  1104. else
  1105. {
  1106. //mylog(log_info,"!!!\n");
  1107. }
  1108. }
  1109. else
  1110. {
  1111. //mylog(log_info,"tcph->doff= %u\n",tcph->doff);
  1112. }
  1113. recv_info.ack=tcph->ack;
  1114. recv_info.syn=tcph->syn;
  1115. recv_info.rst=tcph->rst;
  1116. recv_info.src_port=ntohs(tcph->source);
  1117. recv_info.dst_port=ntohs(tcph->dest);
  1118. recv_info.seq=ntohl(tcph->seq);
  1119. recv_info.ack_seq=ntohl(tcph->ack_seq);
  1120. recv_info.psh=tcph->psh;
  1121. if(tcph->rst==1)
  1122. {
  1123. mylog(log_error,"[%s,%d]rst==1\n",my_ntoa(recv_info.src_ip),recv_info.src_port);
  1124. }
  1125. /* if(recv_info.has_ts)
  1126. {
  1127. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  1128. }*/
  1129. payloadlen = ip_payloadlen-tcphdrlen;
  1130. payload=tcp_begin+tcphdrlen;
  1131. /*if (recv_info.syn == 0 && recv_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  1132. {
  1133. send_info.ack_seq=recv_info.seq;
  1134. }*/
  1135. raw_info.last_recv_len=payloadlen;
  1136. return 0;
  1137. }
  1138. /*
  1139. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  1140. {
  1141. static char buf[buf_len];
  1142. char raw_recv_buf[buf_len];
  1143. char raw_recv_buf2[buf_len];
  1144. char raw_recv_buf3[buf_len];
  1145. iphdr * iph;
  1146. tcphdr * tcph;
  1147. int size;
  1148. struct sockaddr saddr;
  1149. socklen_t saddr_size;
  1150. saddr_size = sizeof(saddr);
  1151. mylog(log_trace,"raw!\n");
  1152. size = recvfrom(raw_recv_fd, buf, max_data_len, 0 ,&saddr , &saddr_size);
  1153. if(buf[12]!=8||buf[13]!=0)
  1154. {
  1155. mylog(log_debug,"not an ipv4 packet!\n");
  1156. return -1;
  1157. }
  1158. char *ip_begin=buf+14;
  1159. iph = (struct iphdr *) (ip_begin);
  1160. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1161. mylog(log_debug,"iph ihl error");
  1162. return -1;
  1163. }
  1164. if (iph->protocol != IPPROTO_TCP) {
  1165. mylog(log_debug,"iph protocal != tcp\n");
  1166. return -1;
  1167. }
  1168. int ip_len=ntohs(iph->tot_len);
  1169. unsigned short iphdrlen =iph->ihl*4;
  1170. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1171. unsigned short tcphdrlen = tcph->doff*4;
  1172. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1173. mylog(log_debug,"tcph error");
  1174. return 0;
  1175. }
  1176. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1177. {
  1178. //printf("%x %x",tcph->dest,);
  1179. return -1;
  1180. }
  1181. /////ip
  1182. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1183. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1184. /////ip end
  1185. ///tcp
  1186. struct pseudo_header psh;
  1187. psh.source_address = iph->saddr;
  1188. psh.dest_address = iph->daddr;
  1189. psh.placeholder = 0;
  1190. psh.protocol = IPPROTO_TCP;
  1191. psh.tcp_length = htons(ip_len-iphdrlen);
  1192. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1193. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1194. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1195. if(ip_chk!=0)
  1196. {
  1197. mylog(log_debug,"ip header error %d\n",ip_chk);
  1198. return -1;
  1199. }
  1200. if(tcp_chk!=0)
  1201. {
  1202. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1203. mylog(log_debug,"tcp header error\n");
  1204. return -1;
  1205. }
  1206. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  1207. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1208. info.has_ts=0;
  1209. if(tcph->doff==10)
  1210. {
  1211. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1212. {
  1213. info.has_ts=1;
  1214. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1215. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1216. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1217. }
  1218. }
  1219. else if(tcph->doff==8)
  1220. {
  1221. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1222. {
  1223. info.has_ts=1;
  1224. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1225. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1226. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1227. }
  1228. }
  1229. if(tcph->rst==1)
  1230. {
  1231. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1232. }
  1233. info.ack=tcph->ack;
  1234. info.syn=tcph->syn;
  1235. info.rst=tcph->rst;
  1236. info.src_port=ntohs(tcph->source);
  1237. info.src_ip=iph->saddr;
  1238. info.seq=ntohl(tcph->seq);
  1239. info.ack_seq=ntohl(tcph->ack_seq);
  1240. info.psh=tcph->psh;
  1241. if(info.has_ts)
  1242. {
  1243. g_packet_info_send.ts_ack=info.ts;
  1244. }
  1245. ////tcp end
  1246. payloadlen = ip_len-tcphdrlen-iphdrlen;
  1247. payload=ip_begin+tcphdrlen+iphdrlen;
  1248. if(payloadlen>0&&payload[0]=='h')
  1249. {
  1250. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  1251. }
  1252. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  1253. {
  1254. //if(seq_increse)
  1255. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  1256. }
  1257. //printf("%d\n",ip_len);
  1258. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  1259. return 0;
  1260. }*/
  1261. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
  1262. {
  1263. packet_info_t &send_info=raw_info.send_info;
  1264. packet_info_t &recv_info=raw_info.recv_info;
  1265. 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);
  1266. switch(raw_mode)
  1267. {
  1268. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  1269. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  1270. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  1271. default:return -1;
  1272. }
  1273. }
  1274. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1275. {
  1276. packet_info_t &send_info=raw_info.send_info;
  1277. packet_info_t &recv_info=raw_info.recv_info;
  1278. switch(raw_mode)
  1279. {
  1280. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  1281. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  1282. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  1283. default: return -1;
  1284. }
  1285. }
  1286. int after_send_raw0(raw_info_t &raw_info)
  1287. {
  1288. packet_info_t &send_info=raw_info.send_info;
  1289. packet_info_t &recv_info=raw_info.recv_info;
  1290. if(raw_mode==mode_faketcp)
  1291. {
  1292. if (send_info.syn == 0 && send_info.ack == 1&& raw_info.last_send_len != 0) //only modify send_info when the packet is not part of handshake
  1293. {
  1294. if (seq_mode == 0)
  1295. {
  1296. } else if (seq_mode == 1)
  1297. {
  1298. send_info.seq += raw_info.last_send_len; //////////////////modify
  1299. } else if (seq_mode == 2)
  1300. {
  1301. if (random() % 5 == 3)
  1302. send_info.seq += raw_info.last_send_len; //////////////////modify
  1303. }
  1304. }
  1305. }
  1306. if(raw_mode==mode_icmp)
  1307. {
  1308. if(program_mode==client_mode)
  1309. {
  1310. send_info.icmp_seq++;
  1311. }
  1312. }
  1313. return 0;
  1314. }
  1315. int after_recv_raw0(raw_info_t &raw_info)
  1316. {
  1317. packet_info_t &send_info=raw_info.send_info;
  1318. packet_info_t &recv_info=raw_info.recv_info;
  1319. if(raw_mode==mode_faketcp)
  1320. {
  1321. if(recv_info.has_ts)
  1322. send_info.ts_ack=recv_info.ts;
  1323. if (recv_info.syn == 0 && recv_info.ack == 1 && raw_info.last_recv_len != 0) //only modify send_info when the packet is not part of handshake
  1324. {
  1325. if(larger_than_u32(recv_info.seq+raw_info.last_recv_len,send_info.ack_seq))
  1326. send_info.ack_seq = recv_info.seq+raw_info.last_recv_len;//TODO only update if its larger
  1327. }
  1328. }
  1329. if(raw_mode==mode_icmp)
  1330. {
  1331. if(program_mode==server_mode)
  1332. {
  1333. if(larger_than_u16(recv_info.icmp_seq,send_info.icmp_seq))
  1334. send_info.icmp_seq = recv_info.icmp_seq; //TODO only update if its larger
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. /*
  1340. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  1341. {
  1342. packet_info_t &send_info=raw_info.send_info;
  1343. packet_info_t &recv_info=raw_info.recv_info;
  1344. int ret=send_raw0(raw_info,payload,payloadlen);
  1345. if(ret<0) return ret;
  1346. else
  1347. {
  1348. after_send_raw0(raw_info);
  1349. return ret;
  1350. }
  1351. }
  1352. int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
  1353. {
  1354. packet_info_t &send_info=raw_info.send_info;
  1355. packet_info_t &recv_info=raw_info.recv_info;
  1356. int ret=recv_raw0(raw_info,payload,payloadlen);
  1357. if(ret<0) return ret;
  1358. else
  1359. {
  1360. after_recv_raw0(raw_info);
  1361. return ret;
  1362. }
  1363. }*/