network.cpp 42 KB

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