network.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  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. #include "misc.h"
  11. int g_fix_gro=0;
  12. int raw_recv_fd=-1;
  13. int raw_send_fd=-1;
  14. 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));
  15. int use_tcp_dummy_socket=0;
  16. int seq_mode=3;
  17. int max_seq_mode=4;
  18. int random_drop=0;
  19. int filter_port=-1;
  20. int disable_bpf_filter=0; //for test only,most time no need to disable this
  21. //u32_t bind_address_uint32=0;
  22. int lower_level=0;
  23. int lower_level_manual=0;
  24. int ifindex=-1;
  25. char if_name[100]="";
  26. char dev[100]="";
  27. unsigned short g_ip_id_counter=0;
  28. unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
  29. {0xff,0xff,0xff,0xff,0xff,0xff,0,0};
  30. //{0x00,0x23,0x45,0x67,0x89,0xb9};
  31. const u32_t receive_window_lower_bound=40960;
  32. const u32_t receive_window_random_range=512;
  33. const unsigned char wscale=0x05;
  34. char g_packet_buf[buf_len]; //looks dirty but works well
  35. int g_packet_buf_len=-1;
  36. int g_packet_buf_cnt=0;
  37. union
  38. {
  39. sockaddr_ll ll;
  40. sockaddr_in ipv4;
  41. sockaddr_in6 ipv6;
  42. }g_sockaddr;
  43. socklen_t g_sockaddr_len = -1;
  44. struct sock_filter code_tcp_old[] = {
  45. { 0x28, 0, 0, 0x0000000c },//0
  46. { 0x15, 0, 10, 0x00000800 },//1
  47. { 0x30, 0, 0, 0x00000017 },//2
  48. { 0x15, 0, 8, 0x00000006 },//3
  49. { 0x28, 0, 0, 0x00000014 },//4
  50. { 0x45, 6, 0, 0x00001fff },//5
  51. { 0xb1, 0, 0, 0x0000000e },//6
  52. { 0x48, 0, 0, 0x0000000e },//7
  53. { 0x15, 2, 0, 0x0000ef32 },//8
  54. { 0x48, 0, 0, 0x00000010 },//9
  55. { 0x15, 0, 1, 0x0000ef32 },//10
  56. { 0x6, 0, 0, 0x0000ffff },//11
  57. { 0x6, 0, 0, 0x00000000 },//12
  58. };
  59. struct sock_filter code_tcp[] = {
  60. //{ 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  61. //{ 0x5, 0, 0, 0x00000000 },//1
  62. { 0x30, 0, 0, 0x00000009 },//2
  63. { 0x15, 0, 6, 0x00000006 },//3
  64. { 0x28, 0, 0, 0x00000006 },//4
  65. { 0x45, 4, 0, 0x00001fff },//5
  66. { 0xb1, 0, 0, 0x00000000 },//6
  67. { 0x48, 0, 0, 0x00000002 },//7
  68. { 0x15, 0, 1, 0x0000fffe },//8 //modify this fffe to the port you listen on
  69. { 0x6, 0, 0, 0x0000ffff },//9
  70. { 0x6, 0, 0, 0x00000000 },//10
  71. };
  72. /*
  73. { 0x28, 0, 0, 0x0000000c },
  74. { 0x15, 0, 8, 0x00000800 },
  75. { 0x30, 0, 0, 0x00000017 },
  76. { 0x15, 0, 6, 0x00000006 },
  77. { 0x28, 0, 0, 0x00000014 },
  78. { 0x45, 4, 0, 0x00001fff },
  79. { 0xb1, 0, 0, 0x0000000e },
  80. { 0x48, 0, 0, 0x00000010 },
  81. { 0x15, 0, 1, 0x0000fffe },
  82. { 0x6, 0, 0, 0x0000ffff },
  83. { 0x6, 0, 0, 0x00000000 },
  84. */
  85. int code_tcp_port_index=6;
  86. //tcpdump -i ens33 ip6 and tcp and dst port 65534 -dd
  87. struct sock_filter code_tcp6[] = {
  88. //{ 0x28, 0, 0, 0x0000000c },//0
  89. //{ 0x15, 0, 5, 0x000086dd },//1
  90. { 0x30, 0, 0, 0x00000006 },//2
  91. { 0x15, 0, 3, 0x00000006 },//3
  92. { 0x28, 0, 0, 0x0000002a },//4
  93. { 0x15, 0, 1, 0x0000fffe },//5
  94. { 0x6, 0, 0, 0x00040000 },//6
  95. { 0x6, 0, 0, 0x00000000 },//7
  96. };//note: this filter doesnt support extension headers
  97. /*
  98. { 0x30, 0, 0, 0x00000014 },//2
  99. { 0x15, 0, 3, 0x00000006 },//3
  100. { 0x28, 0, 0, 0x00000038 },//4
  101. { 0x15, 0, 1, 0x0000fffe },//5
  102. { 0x6, 0, 0, 0x00040000 },//6
  103. { 0x6, 0, 0, 0x00000000 },//7
  104. */
  105. int code_tcp6_port_index=3;
  106. struct sock_filter code_udp[] = {
  107. //{ 0x5, 0, 0, 0x00000001 },
  108. //{ 0x5, 0, 0, 0x00000000 },
  109. { 0x30, 0, 0, 0x00000009 },
  110. { 0x15, 0, 6, 0x00000011 },
  111. { 0x28, 0, 0, 0x00000006 },
  112. { 0x45, 4, 0, 0x00001fff },
  113. { 0xb1, 0, 0, 0x00000000 },
  114. { 0x48, 0, 0, 0x00000002 },
  115. { 0x15, 0, 1, 0x0000fffe }, //modify this fffe to the port you listen on
  116. { 0x6, 0, 0, 0x0000ffff },
  117. { 0x6, 0, 0, 0x00000000 },
  118. };
  119. int code_udp_port_index=6;
  120. struct sock_filter code_udp6[] = {
  121. // { 0x28, 0, 0, 0x0000000c },
  122. // { 0x15, 0, 5, 0x000086dd },
  123. { 0x30, 0, 0, 0x00000006 },
  124. { 0x15, 0, 3, 0x00000011 },
  125. { 0x28, 0, 0, 0x0000002a },
  126. { 0x15, 0, 1, 0x0000fffe },
  127. { 0x6, 0, 0, 0x00040000 },
  128. { 0x6, 0, 0, 0x00000000 },
  129. };
  130. int code_udp6_port_index=3;
  131. struct sock_filter code_icmp[] = {
  132. //{ 0x5, 0, 0, 0x00000001 },
  133. //{ 0x5, 0, 0, 0x00000000 },
  134. { 0x30, 0, 0, 0x00000009 },
  135. { 0x15, 0, 1, 0x00000001 },
  136. { 0x6, 0, 0, 0x0000ffff },
  137. { 0x6, 0, 0, 0x00000000 },
  138. };
  139. struct sock_filter code_icmp6[] = {
  140. // { 0x28, 0, 0, 0x0000000c },
  141. // { 0x15, 0, 6, 0x000086dd },
  142. { 0x30, 0, 0, 0x00000006 },
  143. { 0x15, 3, 0, 0x0000003a },
  144. { 0x15, 0, 3, 0x0000002c },
  145. { 0x30, 0, 0, 0x00000028 },
  146. { 0x15, 0, 1, 0x0000003a },
  147. { 0x6, 0, 0, 0x00040000 },
  148. { 0x6, 0, 0, 0x00000000 },
  149. };
  150. /*
  151. tcpdump -i eth1 ip and icmp -d
  152. (000) ldh [12]
  153. (001) jeq #0x800 jt 2 jf 5
  154. (002) ldb [23]
  155. (003) jeq #0x1 jt 4 jf 5
  156. (004) ret #65535
  157. (005) ret #0
  158. tcpdump -i eth1 ip and icmp -dd
  159. { 0x28, 0, 0, 0x0000000c },
  160. { 0x15, 0, 3, 0x00000800 },
  161. { 0x30, 0, 0, 0x00000017 },
  162. { 0x15, 0, 1, 0x00000001 },
  163. { 0x6, 0, 0, 0x0000ffff },
  164. { 0x6, 0, 0, 0x00000000 },
  165. */
  166. /*
  167. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  168. { 0x28, 0, 0, 0x0000000c },
  169. { 0x15, 0, 8, 0x00000800 },
  170. { 0x30, 0, 0, 0x00000017 },
  171. { 0x15, 0, 6, 0x00000006 },
  172. { 0x28, 0, 0, 0x00000014 },
  173. { 0x45, 4, 0, 0x00001fff },
  174. { 0xb1, 0, 0, 0x0000000e },
  175. { 0x48, 0, 0, 0x00000010 },
  176. { 0x15, 0, 1, 0x0000fffe },
  177. { 0x6, 0, 0, 0x0000ffff },
  178. { 0x6, 0, 0, 0x00000000 },
  179. (000) ldh [12]
  180. (001) jeq #0x800 jt 2 jf 10
  181. (002) ldb [23]
  182. (003) jeq #0x6 jt 4 jf 10
  183. (004) ldh [20]
  184. (005) jset #0x1fff jt 10 jf 6
  185. (006) ldxb 4*([14]&0xf)
  186. (007) ldh [x + 16]
  187. (008) jeq #0xfffe jt 9 jf 10
  188. (009) ret #65535
  189. (010) ret #0
  190. */
  191. packet_info_t::packet_info_t()
  192. {
  193. src_port=0;
  194. dst_port=0;
  195. if (raw_mode == mode_faketcp)
  196. {
  197. protocol = IPPROTO_TCP;
  198. ack_seq = get_true_random_number();
  199. seq = get_true_random_number();
  200. has_ts=0;
  201. ts_ack=0;
  202. syn=0;
  203. ack=1;
  204. ack_seq_counter=0;
  205. //mylog(log_info,"<cons ,ts_ack= %u>\n",ts_ack);
  206. }
  207. else if (raw_mode == mode_udp)
  208. {
  209. protocol = IPPROTO_UDP;
  210. }
  211. else if (raw_mode == mode_icmp)
  212. {
  213. if(raw_ip_version==AF_INET)
  214. {
  215. protocol = IPPROTO_ICMP;
  216. }
  217. else
  218. {
  219. assert(raw_ip_version==AF_INET6);
  220. protocol = IPPROTO_ICMPV6;
  221. }
  222. my_icmp_seq=0;
  223. }
  224. }
  225. int init_raw_socket()
  226. {
  227. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  228. g_ip_id_counter=get_true_random_number()%65535;
  229. if(lower_level==0)
  230. {
  231. raw_send_fd = socket(raw_ip_version , SOCK_RAW , IPPROTO_RAW);// IPPROTO_TCP??
  232. if(raw_send_fd == -1) {
  233. mylog(log_fatal,"Failed to create raw_send_fd\n");
  234. //perror("Failed to create raw_send_fd");
  235. myexit(1);
  236. }
  237. /*ETH_P_IP
  238. int one = 1;
  239. const int *val = &one;
  240. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  241. mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
  242. //perror("Error setting IP_HDRINCL");
  243. myexit(2);
  244. }*/
  245. }
  246. else
  247. {
  248. raw_send_fd = socket(PF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));// todo how to create a recv only raw socket?
  249. if(raw_send_fd == -1) {
  250. mylog(log_fatal,"Failed to create raw_send_fd\n");
  251. //perror("Failed to create raw_send_fd");
  252. myexit(1);
  253. }
  254. //init_ifindex(if_name);
  255. }
  256. int opt = 0;
  257. assert(setsockopt(raw_send_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt))==0);// raw_send_fd is for send only, set its recv buffer to zero
  258. if(force_socket_buf)
  259. {
  260. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  261. {
  262. mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  263. myexit(1);
  264. }
  265. }
  266. else
  267. {
  268. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  269. {
  270. mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  271. myexit(1);
  272. }
  273. }
  274. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  275. if(raw_ip_version==AF_INET)
  276. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  277. else
  278. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  279. //ETH_P_IP doesnt read outgoing packets
  280. // https://stackoverflow.com/questions/20264895/eth-p-ip-is-not-working-as-expected-i-can-only-receive-incoming-packets
  281. // to capture both incoming and outgoing packets use ETH_P_ALL
  282. if(raw_recv_fd == -1) {
  283. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  284. //perror("");
  285. myexit(1);
  286. }
  287. if(strlen(dev)!=0)
  288. {
  289. struct sockaddr_ll bind_address;
  290. memset(&bind_address, 0, sizeof(bind_address));
  291. int index=-1;
  292. assert(init_ifindex(dev,raw_recv_fd,index)==0);
  293. bind_address.sll_family = AF_PACKET;
  294. if(raw_ip_version==AF_INET)
  295. bind_address.sll_protocol = htons(ETH_P_IP);
  296. else
  297. bind_address.sll_protocol = htons(ETH_P_IPV6);
  298. bind_address.sll_ifindex = index;
  299. if(bind(raw_recv_fd, (struct sockaddr *)&bind_address, sizeof(bind_address))==-1)
  300. {
  301. mylog(log_fatal,"bind to dev [%s] failed\n",dev);
  302. myexit(1);
  303. }
  304. }
  305. if(force_socket_buf)
  306. {
  307. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  308. {
  309. mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  310. myexit(1);
  311. }
  312. }
  313. else
  314. {
  315. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  316. {
  317. mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  318. myexit(1);
  319. }
  320. }
  321. //IP_HDRINCL to tell the kernel that headers are included in the packet
  322. setnonblocking(raw_send_fd); //not really necessary
  323. setnonblocking(raw_recv_fd);
  324. return 0;
  325. }
  326. void init_filter(int port)
  327. {
  328. sock_fprog bpf;
  329. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  330. if(raw_mode==mode_faketcp||raw_mode==mode_udp)
  331. {
  332. filter_port=port;
  333. }
  334. if(disable_bpf_filter) return;
  335. //if(raw_mode==mode_icmp) return ;
  336. //code_tcp[8].k=code_tcp[10].k=port;
  337. if(raw_mode==mode_faketcp)
  338. {
  339. if(raw_ip_version==AF_INET)
  340. {
  341. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  342. code_tcp[code_tcp_port_index].k=port;
  343. bpf.filter = code_tcp;
  344. }else
  345. {
  346. bpf.len = sizeof(code_tcp6)/sizeof(code_tcp6[0]);
  347. code_tcp6[code_tcp6_port_index].k=port;
  348. bpf.filter = code_tcp6;
  349. }
  350. }
  351. else if(raw_mode==mode_udp)
  352. {
  353. if(raw_ip_version==AF_INET)
  354. {
  355. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  356. code_udp[code_udp_port_index].k=port;
  357. bpf.filter = code_udp;
  358. }else
  359. {
  360. bpf.len = sizeof(code_udp6)/sizeof(code_udp6[0]);
  361. code_udp6[code_udp6_port_index].k=port;
  362. bpf.filter = code_udp6;
  363. }
  364. }
  365. else if(raw_mode==mode_icmp)
  366. {
  367. if(raw_ip_version==AF_INET)
  368. {
  369. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  370. bpf.filter = code_icmp;
  371. }else
  372. {
  373. bpf.len = sizeof(code_icmp6)/sizeof(code_icmp6[0]);
  374. bpf.filter = code_icmp6;
  375. }
  376. }
  377. int dummy;
  378. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  379. if (ret != 0)
  380. {
  381. mylog(log_debug,"error remove fiter\n");
  382. //perror("filter");
  383. //exit(-1);
  384. }
  385. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  386. if (ret != 0)
  387. {
  388. mylog(log_fatal,"error set fiter\n");
  389. //perror("filter");
  390. myexit(-1);
  391. }
  392. }
  393. void remove_filter()
  394. {
  395. filter_port=0;
  396. int dummy;
  397. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  398. if (ret != 0)
  399. {
  400. mylog(log_debug,"error remove fiter\n");
  401. //perror("filter");
  402. //exit(-1);
  403. }
  404. }
  405. int init_ifindex(const char * if_name,int fd,int &index)
  406. {
  407. struct ifreq ifr;
  408. size_t if_name_len=strlen(if_name);
  409. if (if_name_len<sizeof(ifr.ifr_name)) {
  410. memcpy(ifr.ifr_name,if_name,if_name_len);
  411. ifr.ifr_name[if_name_len]=0;
  412. } else {
  413. mylog(log_fatal,"interface name is too long\n");
  414. myexit(-1);
  415. }
  416. if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) {
  417. mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
  418. myexit(-1);
  419. }
  420. index=ifr.ifr_ifindex;
  421. mylog(log_info,"ifname:%s ifindex:%d\n",if_name,index);
  422. return 0;
  423. }
  424. bool interface_has_arp(const char * interface) {
  425. struct ifreq ifr;
  426. // int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
  427. int sock=raw_send_fd;
  428. memset(&ifr, 0, sizeof(ifr));
  429. strcpy(ifr.ifr_name, interface);
  430. if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
  431. //perror("SIOCGIFFLAGS");
  432. mylog(log_fatal,"ioctl(sock, SIOCGIFFLAGS, &ifr) failed for interface %s,errno %s\n",interface,strerror(errno));
  433. myexit(-1);
  434. }
  435. //close(sock);
  436. return !(ifr.ifr_flags & IFF_NOARP);
  437. }
  438. struct route_info_t
  439. {
  440. string if_name;
  441. u32_t dest;
  442. u32_t mask;
  443. u32_t gw;
  444. u32_t flag;
  445. };
  446. int dest_idx=1;
  447. int gw_idx=2;
  448. int if_idx=0;
  449. int mask_idx=7;
  450. int flag_idx=3;
  451. vector<int> find_route_entry(const vector<route_info_t> &route_info_vec,u32_t ip)
  452. {
  453. vector<int> res;
  454. for(u32_t i=0;i<=32;i++)
  455. {
  456. u32_t mask=0xffffffff;
  457. //mask >>=i;
  458. //if(i==32) mask=0; //why 0xffffffff>>32 equals 0xffffffff??
  459. mask <<=i;
  460. if(i==32) mask=0;
  461. log_bare(log_debug,"(mask:%x)",mask);
  462. for(u32_t j=0;j<route_info_vec.size();j++)
  463. {
  464. const route_info_t & info=route_info_vec[j];
  465. if(info.mask!=mask)
  466. continue;
  467. log_bare(log_debug,"<<%d,%d>>",i,j);
  468. if((info.dest&mask)==(ip&mask))
  469. {
  470. log_bare(log_debug,"found!");
  471. res.push_back(j);
  472. }
  473. }
  474. if(res.size()!=0)
  475. {
  476. return res;
  477. }
  478. }
  479. return res;
  480. }
  481. int find_direct_dest(const vector<route_info_t> &route_info_vec,u32_t ip,u32_t &dest_ip,string &if_name)
  482. {
  483. vector<int> res;
  484. for(int i=0;i<1000;i++)
  485. {
  486. res=find_route_entry(route_info_vec,ip);
  487. log_bare(log_debug,"<entry:%u>",(u32_t)res.size());
  488. if(res.size()==0)
  489. {
  490. mylog(log_error,"cant find route entry\n");
  491. return -1;
  492. }
  493. if(res.size()>1)
  494. {
  495. mylog(log_error,"found duplicated entries\n");
  496. return -1;
  497. }
  498. if((route_info_vec[res[0]].flag&2)==0)
  499. {
  500. dest_ip=ip;
  501. if_name=route_info_vec[res[0]].if_name;
  502. return 0;
  503. }
  504. else
  505. {
  506. ip=route_info_vec[res[0]].gw;
  507. }
  508. }
  509. mylog(log_error,"dead loop in find_direct_dest\n");
  510. return -1;
  511. }
  512. struct arp_info_t
  513. {
  514. u32_t ip;
  515. string hw;
  516. string if_name;
  517. };
  518. int arp_ip_idx=0;
  519. int arp_hw_idx=3;
  520. int arp_if_idx=5;
  521. int find_arp(const vector<arp_info_t> &arp_info_vec,u32_t ip,string if_name,string &hw)
  522. {
  523. int pos=-1;
  524. int count=0;
  525. for(u32_t i=0;i<arp_info_vec.size();i++)
  526. {
  527. const arp_info_t & info=arp_info_vec[i];
  528. if(info.if_name!=if_name) continue;
  529. if(info.ip==ip)
  530. {
  531. count++;
  532. pos=i;
  533. }
  534. }
  535. if(count==0)
  536. {
  537. //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());
  538. //hw="00:00:00:00:00:00";
  539. mylog(log_error,"cant find arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  540. return -1;
  541. }
  542. if(count>1)
  543. {
  544. mylog(log_error,"find multiple arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  545. return -1;
  546. }
  547. hw=arp_info_vec[pos].hw;
  548. return 0;
  549. }
  550. int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw)
  551. {
  552. ip=htonl(ip);
  553. if(ip==htonl(inet_addr("127.0.0.1")))
  554. {
  555. dest_ip=ntohl(ip);
  556. if_name="lo";
  557. hw="00:00:00:00:00:00";
  558. return 0;
  559. }
  560. string route_file;
  561. if(read_file("/proc/net/route",route_file)!=0) return -1;
  562. string arp_file;
  563. if(read_file("/proc/net/arp",arp_file)!=0) return -1;
  564. log_bare(log_debug,"/proc/net/route:<<%s>>\n",route_file.c_str());
  565. log_bare(log_debug,"/proc/net/arp:<<%s>>\n",route_file.c_str());
  566. auto route_vec2=string_to_vec2(route_file.c_str());
  567. vector<route_info_t> route_info_vec;
  568. for(u32_t i=1;i<route_vec2.size();i++)
  569. {
  570. log_bare(log_debug,"<size:%u>",(u32_t)route_vec2[i].size());
  571. if(route_vec2[i].size()!=11)
  572. {
  573. mylog(log_error,"route coloum %d !=11 \n",int(route_vec2[i].size()));
  574. return -1;
  575. }
  576. route_info_t tmp;
  577. tmp.if_name=route_vec2[i][if_idx];
  578. if(hex_to_u32_with_endian(route_vec2[i][dest_idx],tmp.dest)!=0) return -1;
  579. if(hex_to_u32_with_endian(route_vec2[i][gw_idx],tmp.gw)!=0) return -1;
  580. if(hex_to_u32_with_endian(route_vec2[i][mask_idx],tmp.mask)!=0) return -1;
  581. if(hex_to_u32(route_vec2[i][flag_idx],tmp.flag)!=0)return -1;
  582. route_info_vec.push_back(tmp);
  583. for(u32_t j=0;j<route_vec2[i].size();j++)
  584. {
  585. log_bare(log_debug,"<%s>",route_vec2[i][j].c_str());
  586. }
  587. 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);
  588. log_bare(log_debug,"\n");
  589. }
  590. if(find_direct_dest(route_info_vec,ip,dest_ip,if_name)!=0)
  591. {
  592. mylog(log_error,"find_direct_dest failed for ip %s\n",my_ntoa(ntohl(ip)));
  593. return -1;
  594. }
  595. log_bare(log_debug,"========\n");
  596. auto arp_vec2=string_to_vec2(arp_file.c_str());
  597. vector<arp_info_t> arp_info_vec;
  598. for(u32_t i=1;i<arp_vec2.size();i++)
  599. {
  600. log_bare(log_debug,"<<arp_vec2[i].size(): %d>>",(int)arp_vec2[i].size());
  601. for(u32_t j=0;j<arp_vec2[i].size();j++)
  602. {
  603. log_bare(log_debug,"<%s>",arp_vec2[i][j].c_str());
  604. }
  605. if(arp_vec2[i].size()!=6)
  606. {
  607. mylog(log_error,"arp coloum %d !=11 \n",int(arp_vec2[i].size()));
  608. return -1;
  609. }
  610. arp_info_t tmp;
  611. tmp.if_name=arp_vec2[i][arp_if_idx];
  612. tmp.hw=arp_vec2[i][arp_hw_idx];
  613. tmp.ip=htonl(inet_addr(arp_vec2[i][arp_ip_idx].c_str()));
  614. arp_info_vec.push_back(tmp);
  615. log_bare(log_debug,"\n");
  616. }
  617. if(!interface_has_arp(if_name.c_str()))
  618. {
  619. mylog(log_info,"%s is a noarp interface,using 00:00:00:00:00:00\n",if_name.c_str());
  620. hw="00:00:00:00:00:00";
  621. }
  622. else if(find_arp(arp_info_vec,dest_ip,if_name,hw)!=0)
  623. {
  624. mylog(log_error,"find_arp failed for dest_ip %s ,if_name %s\n",my_ntoa(ntohl(ip)),if_name.c_str());
  625. return -1;
  626. }
  627. //printf("%s\n",hw.c_str());
  628. dest_ip=ntohl(dest_ip);
  629. return 0;
  630. }
  631. int send_raw_packet(raw_info_t &raw_info,const char * packet,int len)
  632. {
  633. const packet_info_t &send_info=raw_info.send_info;
  634. const packet_info_t &recv_info=raw_info.recv_info;
  635. int ret;
  636. if(lower_level==0)
  637. {
  638. if(raw_ip_version==AF_INET)
  639. {
  640. struct sockaddr_in sin={0};
  641. sin.sin_family = raw_ip_version;
  642. //sin.sin_port = htons(info.dst_port); //dont need this
  643. sin.sin_addr.s_addr = send_info.new_dst_ip.v4;
  644. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &sin, sizeof (sin));
  645. }
  646. else if(raw_ip_version==AF_INET6)
  647. {
  648. struct sockaddr_in6 sin={0};
  649. sin.sin6_family = raw_ip_version;
  650. //sin.sin_port = htons(info.dst_port); //dont need this
  651. sin.sin6_addr = send_info.new_dst_ip.v6;
  652. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &sin, sizeof (sin));
  653. }else
  654. {
  655. assert(0==1);
  656. }
  657. }
  658. else
  659. {
  660. struct sockaddr_ll addr={0}; //={0} not necessary
  661. memcpy(&addr,&send_info.addr_ll,sizeof(addr));
  662. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &addr, sizeof (addr));
  663. }
  664. if(ret==-1)
  665. {
  666. mylog(log_trace,"sendto failed\n");
  667. //perror("why?");
  668. return -1;
  669. }
  670. else
  671. {
  672. //mylog(log_info,"sendto succ\n");
  673. }
  674. return 0;
  675. }
  676. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  677. {
  678. const packet_info_t &send_info=raw_info.send_info;
  679. const packet_info_t &recv_info=raw_info.recv_info;
  680. char send_raw_ip_buf[buf_len];
  681. if(raw_info.disabled)
  682. {
  683. mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",recv_info.new_src_ip.get_str1(),recv_info.src_port);
  684. assert(max_rst_allowed>=0);
  685. return 0;
  686. }
  687. uint16_t ip_tot_len;
  688. if(raw_ip_version==AF_INET)
  689. {
  690. struct my_iphdr *iph = (struct my_iphdr *) send_raw_ip_buf;
  691. memset(iph,0,sizeof(my_iphdr));
  692. iph->ihl = sizeof(my_iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  693. iph->version = 4;
  694. iph->tos = 0;
  695. if(lower_level)
  696. {
  697. //iph->id=0;
  698. iph->id = htons (g_ip_id_counter++); //Id of this packet
  699. }
  700. else//no need to else?
  701. {
  702. iph->id = htons (g_ip_id_counter++); //Id of this packet
  703. //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
  704. }
  705. iph->frag_off = htons(0x4000); //DF set,others are zero
  706. // iph->frag_off = htons(0x0000); //DF set,others are zero
  707. iph->ttl = (unsigned char)ttl_value;
  708. iph->protocol = send_info.protocol;
  709. iph->check = 0; //Set to 0 before calculating checksum
  710. iph->saddr = send_info.new_src_ip.v4; //Spoof the source ip address
  711. iph->daddr = send_info.new_dst_ip.v4;
  712. ip_tot_len=sizeof (struct my_iphdr)+payloadlen;
  713. 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
  714. else
  715. iph->tot_len = 0;
  716. memcpy(send_raw_ip_buf+sizeof(my_iphdr) , payload, payloadlen);
  717. if(lower_level) iph->check =
  718. csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
  719. else
  720. iph->check=0;
  721. }
  722. else
  723. {
  724. assert(raw_ip_version==AF_INET6);
  725. struct my_ip6hdr *ip6h = (struct my_ip6hdr *) send_raw_ip_buf;
  726. memset(ip6h,0,sizeof(my_ip6hdr));
  727. ip6h->version=6;
  728. ip6h->payload_len=htons(payloadlen);
  729. ip6h->next_header=send_info.protocol;
  730. ip6h->hop_limit=(unsigned char)ttl_value;
  731. ip6h->src=send_info.new_src_ip.v6;
  732. ip6h->dst=send_info.new_dst_ip.v6;
  733. ip_tot_len=sizeof (struct my_ip6hdr)+payloadlen;
  734. memcpy(send_raw_ip_buf+sizeof(my_ip6hdr) , payload, payloadlen);
  735. }
  736. return send_raw_packet(raw_info,send_raw_ip_buf,ip_tot_len);
  737. }
  738. int pre_recv_raw_packet()
  739. {
  740. assert(g_packet_buf_cnt==0);
  741. g_sockaddr_len=sizeof(g_sockaddr.ll);
  742. g_packet_buf_len = recvfrom(raw_recv_fd, g_packet_buf, max_data_len+1, 0 ,(sockaddr*)&g_sockaddr , &g_sockaddr_len);
  743. //assert(g_sockaddr_len==sizeof(g_sockaddr.ll)); //g_sockaddr_len=18, sizeof(g_sockaddr.ll)=20, why its not equal? maybe its bc sll_halen is 6?
  744. //assert(g_addr_ll_size==sizeof(g_addr_ll));
  745. if(g_packet_buf_len==max_data_len+1)
  746. {
  747. mylog(log_warn,"huge packet, data_len %d > %d(max_data_len),dropped\n",g_packet_buf_len,max_data_len);
  748. return -1;
  749. }
  750. if(g_packet_buf_len> single_max_data_len+1)
  751. {
  752. if(g_fix_gro==0)
  753. {
  754. mylog(log_warn, "huge packet, data_len %d > %d(single_max_data_len) dropped\n", g_packet_buf_len,
  755. single_max_data_len);
  756. return -1;
  757. }
  758. else
  759. {
  760. mylog(log_debug, "huge packet, data_len %d > %d(single_max_data_len) not dropped\n", g_packet_buf_len,
  761. single_max_data_len);
  762. //return -1;
  763. }
  764. }
  765. if(g_packet_buf_len<0)
  766. {
  767. mylog(log_trace,"recv_len %d\n",g_packet_buf_len);
  768. return -1;
  769. }
  770. g_packet_buf_cnt++;
  771. return 0;
  772. }
  773. int discard_raw_packet()
  774. {
  775. assert(g_packet_buf_cnt==1);
  776. g_packet_buf_cnt--;
  777. return 0;
  778. }
  779. int recv_raw_packet(char * &packet,int &len,int peek)
  780. {
  781. assert(g_packet_buf_cnt==1);
  782. if(!peek)
  783. g_packet_buf_cnt--;
  784. if(g_packet_buf_len<int(link_level_header_len))
  785. {
  786. mylog(log_trace,"packet len %d shorter than link_level_header_len %d\n",g_packet_buf_len,int(link_level_header_len));
  787. return -1;
  788. }
  789. if(link_level_header_len ==14)
  790. {
  791. unsigned char a=g_packet_buf[12];
  792. unsigned char b=g_packet_buf[13];
  793. if(! ((a==0x08&&b==0x00) ||(a==0x86&&b==0xdd)) )
  794. {
  795. mylog(log_trace,"not an ipv4 or ipv6 packet!\n");
  796. return -1;
  797. }
  798. }
  799. packet=g_packet_buf+int(link_level_header_len);
  800. len=g_packet_buf_len-int(link_level_header_len);
  801. return 0;
  802. }
  803. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  804. {
  805. char *raw_packet_buf;
  806. //static char recv_raw_ip_buf[buf_len];
  807. int raw_packet_len;
  808. if(recv_raw_packet(raw_packet_buf,raw_packet_len,raw_info.peek)!=0) return -1;
  809. //const packet_info_t &send_info=raw_info.send_info;
  810. packet_info_t &recv_info=raw_info.recv_info;
  811. my_iphdr * iph;
  812. my_ip6hdr * ip6h;
  813. int flag=0;
  814. //int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);
  815. char *ip_begin=raw_packet_buf; //14 is eth net header
  816. if(raw_packet_len<1)
  817. {
  818. mylog(log_trace,"raw_packet_len <1, dropped\n");
  819. return -1;
  820. }
  821. iph = (struct my_iphdr *) (ip_begin);
  822. ip6h= (struct my_ip6hdr *) (ip_begin);
  823. if(raw_ip_version==AF_INET)
  824. {
  825. if(iph->version!=4)
  826. {
  827. mylog(log_trace,"expect ipv4 packet, but got something else: %02x\n",iph->version);
  828. return -1;
  829. }
  830. if(raw_packet_len<(int)sizeof(my_iphdr))
  831. {
  832. mylog(log_trace,"raw_packet_len<sizeof(iphdr)\n");
  833. return -1;
  834. }
  835. }
  836. else
  837. {
  838. assert(raw_ip_version==AF_INET6);
  839. if(ip6h->version!=6)
  840. {
  841. mylog(log_trace,"expect ipv6 packet, but got something else: %02x\n",ip6h->version);
  842. return -1;
  843. }
  844. if(raw_packet_len<(int)sizeof(my_ip6hdr))
  845. {
  846. mylog(log_trace,"raw_packet_len<sizeof(ip6_hdr)\n");
  847. return -1;
  848. }
  849. }
  850. if(lower_level)
  851. {
  852. memcpy(&recv_info.addr_ll,&g_sockaddr.ll,sizeof(recv_info.addr_ll));
  853. }
  854. unsigned short iphdrlen;
  855. int ip_len;
  856. if(raw_ip_version==AF_INET)
  857. {
  858. recv_info.new_src_ip.v4=iph->saddr;
  859. recv_info.new_dst_ip.v4=iph->daddr;
  860. recv_info.protocol=iph->protocol;
  861. iphdrlen =iph->ihl*4;
  862. ip_len=ntohs(iph->tot_len);
  863. }
  864. else
  865. {
  866. //todo flow id
  867. assert(raw_ip_version==AF_INET6);
  868. recv_info.new_src_ip.v6=ip6h->src;
  869. recv_info.new_dst_ip.v6=ip6h->dst;
  870. iphdrlen=40;
  871. recv_info.protocol=ip6h->next_header; //todo handle extension headers;
  872. ip_len=ntohs(ip6h->payload_len)+iphdrlen;
  873. }
  874. if(bind_addr_used && !recv_info.new_dst_ip.equal(bind_addr))
  875. {
  876. mylog(log_trace,"bind adress doenst match %s %s, dropped\n",recv_info.new_dst_ip.get_str1(), bind_addr.get_str2());
  877. //printf(" bind adress doenst match, dropped\n");
  878. return -1;
  879. }
  880. //if (!(iph->ihl > 0 && iph->ihl <=60)) {
  881. // mylog(log_trace,"iph ihl error\n");
  882. // return -1;
  883. // }
  884. if(raw_packet_len <ip_len)
  885. {
  886. mylog(log_debug,"incomplete packet\n");
  887. return -1;
  888. }
  889. if(raw_ip_version==AF_INET)
  890. {
  891. if(raw_info.peek==0)// avoid cal it twice
  892. {
  893. u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  894. if(ip_chk!=0)
  895. {
  896. mylog(log_debug,"ip header error %x\n",ip_chk);
  897. return -1;
  898. }
  899. }
  900. }
  901. else
  902. {
  903. //do nothing
  904. }
  905. payload=ip_begin+iphdrlen;
  906. payloadlen=ip_len-iphdrlen;
  907. if(payloadlen<0)
  908. {
  909. mylog(log_warn,"error payload len\n");
  910. return -1;
  911. }
  912. return 0;
  913. }
  914. int peek_raw(raw_info_t &raw_info)
  915. {
  916. //static char peek_raw_buf[buf_len];
  917. //assert(g_packet_buf_cnt==1);
  918. //g_packet_buf_cnt--;
  919. //char * peek_raw_buf=g_packet_buf;
  920. //int recv_len=g_packet_buf_len;
  921. //char *ip_begin=peek_raw_buf+link_level_header_len;
  922. //struct sockaddr saddr={0};
  923. //socklen_t saddr_size=sizeof(saddr);
  924. //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
  925. //iphdr * iph = (struct iphdr *) (ip_begin);
  926. //mylog(log_info,"recv_len %d\n",recv_len);
  927. //if(recv_len<int(sizeof(iphdr)))
  928. //{
  929. // mylog(log_trace,"failed here %d %d\n",recv_len,int(sizeof(iphdr)));
  930. // mylog(log_trace,"%s\n ",strerror(errno));
  931. // return -1;
  932. //}
  933. //peek_info.new_src_ip.v4=iph->saddr;
  934. //unsigned short iphdrlen =iph->ihl*4;
  935. //char *payload=ip_begin+iphdrlen;
  936. packet_info_t &recv_info=raw_info.recv_info;
  937. char *payload;int payload_len;
  938. if(recv_raw_ip(raw_info,payload,payload_len)!=0)
  939. return -1;
  940. //mylog(log_info,"protocol %d\n",iph->protocol);
  941. switch(raw_mode)
  942. {
  943. case mode_faketcp:
  944. {
  945. if(recv_info.protocol!=IPPROTO_TCP)
  946. {
  947. mylog(log_trace,"failed here");
  948. return -1;
  949. }
  950. struct my_tcphdr *tcph=(my_tcphdr *)payload;
  951. if(payload_len<int(sizeof(my_tcphdr) ))
  952. {
  953. mylog(log_trace,"failed here");
  954. return -1;
  955. }
  956. recv_info.src_port=ntohs(tcph->source);
  957. recv_info.syn=tcph->syn;
  958. break;
  959. }
  960. case mode_udp:
  961. {
  962. if(recv_info.protocol!=IPPROTO_UDP) return -1;
  963. struct my_udphdr *udph=(my_udphdr *)payload;
  964. if(payload_len<int(sizeof(my_udphdr)))
  965. return -1;
  966. recv_info.src_port=ntohs(udph->source);
  967. break;
  968. }
  969. case mode_icmp:
  970. {
  971. if(raw_ip_version==AF_INET)
  972. {
  973. if(recv_info.protocol!=IPPROTO_ICMP) return -1;
  974. }else
  975. {
  976. assert(raw_ip_version==AF_INET6);
  977. if(recv_info.protocol!=IPPROTO_ICMPV6) return -1;
  978. }
  979. struct my_icmphdr *icmph=(my_icmphdr *)payload;
  980. if(payload_len<int(sizeof(my_udphdr)))
  981. return -1;
  982. recv_info.src_port=ntohs(icmph->id);
  983. break;
  984. }
  985. default:return -1;
  986. }
  987. return 0;
  988. }
  989. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  990. {
  991. const packet_info_t &send_info=raw_info.send_info;
  992. const packet_info_t &recv_info=raw_info.recv_info;
  993. char send_raw_icmp_buf[buf_len];
  994. my_icmphdr *icmph=(struct my_icmphdr *) (send_raw_icmp_buf);
  995. memset(icmph,0,sizeof(my_icmphdr));
  996. if(raw_ip_version==AF_INET)
  997. {
  998. if(program_mode==client_mode)
  999. {
  1000. icmph->type=8;
  1001. }
  1002. else
  1003. {
  1004. icmph->type=0;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. assert(raw_ip_version==AF_INET6);
  1010. if(program_mode==client_mode)
  1011. {
  1012. icmph->type=128;
  1013. }
  1014. else
  1015. {
  1016. icmph->type=129;
  1017. }
  1018. }
  1019. icmph->code=0;
  1020. icmph->id=htons(send_info.src_port);
  1021. icmph->seq=htons(send_info.my_icmp_seq); /////////////modify
  1022. memcpy(send_raw_icmp_buf+sizeof(my_icmphdr),payload,payloadlen);
  1023. if(raw_ip_version==AF_INET)
  1024. {
  1025. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
  1026. }
  1027. else
  1028. {
  1029. assert(raw_ip_version==AF_INET6);
  1030. pseudo_header6 v6;
  1031. struct pseudo_header6 *psh = &v6;
  1032. psh->src=send_info.new_src_ip.v6;
  1033. psh->dst=send_info.new_dst_ip.v6;
  1034. psh->next_header=IPPROTO_ICMPV6;
  1035. psh->tcp_length=htons(sizeof(my_icmphdr)+payloadlen);
  1036. psh->placeholder1 = 0;
  1037. psh->placeholder2 = 0;
  1038. icmph->check_sum = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
  1039. }
  1040. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(my_icmphdr)+payloadlen)!=0)
  1041. {
  1042. return -1;
  1043. }
  1044. /*if(program_mode==client_mode)
  1045. {
  1046. send_info.icmp_seq++;
  1047. }*/
  1048. return 0;
  1049. }
  1050. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  1051. {
  1052. const packet_info_t &send_info=raw_info.send_info;
  1053. const packet_info_t &recv_info=raw_info.recv_info;
  1054. char send_raw_udp_buf[buf_len];
  1055. my_udphdr *udph=(struct my_udphdr *) (send_raw_udp_buf);
  1056. memset(udph,0,sizeof(my_udphdr));
  1057. udph->source = htons(send_info.src_port);
  1058. udph->dest = htons(send_info.dst_port);
  1059. int udp_tot_len=payloadlen+sizeof(my_udphdr);
  1060. if(udp_tot_len>65535)
  1061. {
  1062. mylog(log_debug,"invalid len\n");
  1063. return -1;
  1064. }
  1065. mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
  1066. udph->len=htons(uint16_t(udp_tot_len));
  1067. memcpy(send_raw_udp_buf+sizeof(my_udphdr),payload,payloadlen);
  1068. if(raw_ip_version==AF_INET)
  1069. {
  1070. pseudo_header v4;
  1071. struct pseudo_header *psh = &v4;
  1072. psh->source_address = send_info.new_src_ip.v4;
  1073. psh->dest_address = send_info.new_dst_ip.v4;
  1074. psh->placeholder = 0;
  1075. psh->protocol = IPPROTO_UDP;
  1076. psh->tcp_length = htons(udp_tot_len);
  1077. udph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_udp_buf, udp_tot_len);
  1078. }
  1079. else
  1080. {
  1081. assert(raw_ip_version==AF_INET6);
  1082. pseudo_header6 v6;
  1083. struct pseudo_header6 *psh = &v6;
  1084. psh->src=send_info.new_src_ip.v6;
  1085. psh->dst=send_info.new_dst_ip.v6;
  1086. psh->next_header=IPPROTO_UDP;
  1087. psh->tcp_length=htons(udp_tot_len);
  1088. psh->placeholder1 = 0;
  1089. psh->placeholder2 = 0;
  1090. udph->check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_udp_buf, udp_tot_len);
  1091. }
  1092. if(send_raw_ip(raw_info,send_raw_udp_buf,udp_tot_len)!=0)
  1093. {
  1094. return -1;
  1095. }
  1096. return 0;
  1097. }
  1098. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  1099. const packet_info_t &send_info=raw_info.send_info;
  1100. const packet_info_t &recv_info=raw_info.recv_info;
  1101. //mylog(log_debug,"syn %d\n",send_info.syn);
  1102. char send_raw_tcp_buf[buf_len];
  1103. //char *send_raw_tcp_buf=send_raw_tcp_buf0;
  1104. struct my_tcphdr *tcph = (struct my_tcphdr *) (send_raw_tcp_buf);
  1105. memset(tcph,0,sizeof(my_tcphdr));
  1106. //TCP Header
  1107. tcph->source = htons(send_info.src_port);
  1108. tcph->dest = htons(send_info.dst_port);
  1109. tcph->seq = htonl(send_info.seq);
  1110. tcph->ack_seq = htonl(send_info.ack_seq);
  1111. tcph->fin = 0;
  1112. tcph->syn = send_info.syn;
  1113. tcph->rst = 0;
  1114. tcph->psh = send_info.psh;
  1115. tcph->ack = send_info.ack;
  1116. if (tcph->syn == 1) {
  1117. tcph->doff = 10; //tcp header size
  1118. int i = sizeof(my_tcphdr);
  1119. send_raw_tcp_buf[i++] = 0x02; //mss
  1120. send_raw_tcp_buf[i++] = 0x04;
  1121. send_raw_tcp_buf[i++] = 0x05;
  1122. send_raw_tcp_buf[i++] = (char)0xb4;
  1123. //raw_send_buf[i++]=0x01;
  1124. //raw_send_buf[i++]=0x01;
  1125. send_raw_tcp_buf[i++] = 0x04; //sack ok
  1126. send_raw_tcp_buf[i++] = 0x02; //sack ok
  1127. send_raw_tcp_buf[i++] = 0x08; //ts i=6
  1128. send_raw_tcp_buf[i++] = 0x0a; //i=7
  1129. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  1130. // (u32_t) get_current_time());
  1131. u32_t ts=htonl((u32_t) get_current_time());
  1132. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  1133. i += 4;
  1134. //mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
  1135. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  1136. u32_t ts_ack=htonl(send_info.ts_ack);
  1137. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  1138. i += 4;
  1139. send_raw_tcp_buf[i++] = 0x01;
  1140. send_raw_tcp_buf[i++] = 0x03;
  1141. send_raw_tcp_buf[i++] = 0x03;
  1142. send_raw_tcp_buf[i++] = wscale;
  1143. } else {
  1144. tcph->doff = 8;
  1145. int i = sizeof(my_tcphdr);
  1146. send_raw_tcp_buf[i++] = 0x01;
  1147. send_raw_tcp_buf[i++] = 0x01;
  1148. send_raw_tcp_buf[i++] = 0x08; //ts //i=2
  1149. send_raw_tcp_buf[i++] = 0x0a; //i=3;
  1150. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  1151. // (u32_t) get_current_time());
  1152. u32_t ts=htonl((u32_t) get_current_time());
  1153. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  1154. i += 4;
  1155. //mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
  1156. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  1157. u32_t ts_ack=htonl(send_info.ts_ack);
  1158. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  1159. i += 4;
  1160. }
  1161. tcph->urg = 0;
  1162. //tcph->window = htons((uint16_t)(1024));
  1163. tcph->window = htons((uint16_t) (receive_window_lower_bound + get_true_random_number() % receive_window_random_range));
  1164. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  1165. tcph->urg_ptr = 0;
  1166. char *tcp_data = send_raw_tcp_buf+ + tcph->doff * 4;
  1167. memcpy(tcp_data, payload, payloadlen);
  1168. int tcp_totlen=tcph->doff*4 + payloadlen;
  1169. if(raw_ip_version==AF_INET)
  1170. {
  1171. pseudo_header v4;
  1172. struct pseudo_header *psh = &v4;
  1173. psh->source_address = send_info.new_src_ip.v4;
  1174. psh->dest_address = send_info.new_dst_ip.v4;
  1175. psh->placeholder = 0;
  1176. psh->protocol = IPPROTO_TCP;
  1177. psh->tcp_length = htons(tcp_totlen);
  1178. tcph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_tcp_buf, tcp_totlen);
  1179. }
  1180. else
  1181. {
  1182. assert(raw_ip_version==AF_INET6);
  1183. pseudo_header6 v6;
  1184. struct pseudo_header6 *psh = &v6;
  1185. psh->src=send_info.new_src_ip.v6;
  1186. psh->dst=send_info.new_dst_ip.v6;
  1187. psh->next_header=IPPROTO_TCP;
  1188. psh->tcp_length=htons(tcp_totlen);
  1189. psh->placeholder1 = 0;
  1190. psh->placeholder2 = 0;
  1191. tcph->check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_tcp_buf, tcp_totlen);
  1192. }
  1193. if(send_raw_ip(raw_info,send_raw_tcp_buf,tcp_totlen)!=0)
  1194. {
  1195. return -1;
  1196. }
  1197. raw_info.send_info.data_len=payloadlen;
  1198. return 0;
  1199. }
  1200. /*
  1201. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  1202. {
  1203. static uint16_t ip_id=1;
  1204. char raw_send_buf[buf_len];
  1205. char raw_send_buf2[buf_len];
  1206. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  1207. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  1208. char *data;
  1209. memset(raw_send_buf,0,payloadlen+100);
  1210. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  1211. //TCP header
  1212. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  1213. struct sockaddr_in sin;
  1214. struct pseudo_header psh;
  1215. //some address resolution
  1216. sin.sin_family = AF_INET;
  1217. sin.sin_port = htons(info.dst_port);
  1218. sin.sin_addr.s_addr = info.dst_ip;
  1219. //Fill in the IP Header
  1220. iph->ihl = 5;
  1221. iph->version = 4;
  1222. iph->tos = 0;
  1223. iph->id = htonl (ip_id++); //Id of this packet
  1224. iph->frag_off = htons(0x4000); //DF set,others are zero
  1225. iph->ttl = (unsigned char)ttl_value;
  1226. iph->protocol = IPPROTO_TCP;
  1227. iph->check = 0; //Set to 0 before calculating checksum
  1228. iph->saddr = info.src_ip; //Spoof the source ip address
  1229. iph->daddr = info.dst_ip;
  1230. //TCP Header
  1231. tcph->source = htons(info.src_port);
  1232. tcph->dest = htons(info.dst_port);
  1233. tcph->seq =htonl(info.seq);
  1234. tcph->ack_seq = htonl(info.ack_seq);
  1235. tcph->fin=0;
  1236. tcph->syn=info.syn;
  1237. tcph->rst=0;
  1238. tcph->psh=info.psh;
  1239. tcph->ack=info.ack;
  1240. if(tcph->syn==1)
  1241. {
  1242. tcph->doff = 10; //tcp header size
  1243. int i=sizeof (struct iphdr)+20;
  1244. raw_send_buf[i++]=0x02;//mss
  1245. raw_send_buf[i++]=0x04;
  1246. raw_send_buf[i++]=0x05;
  1247. raw_send_buf[i++]=0xb4;
  1248. //raw_send_buf[i++]=0x01;
  1249. //raw_send_buf[i++]=0x01;
  1250. raw_send_buf[i++]=0x04; //sack ok
  1251. raw_send_buf[i++]=0x02; //sack ok
  1252. raw_send_buf[i++]=0x08; //i=6;
  1253. raw_send_buf[i++]=0x0a;
  1254. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  1255. i+=4;
  1256. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  1257. i+=4;
  1258. raw_send_buf[i++]=0x01;
  1259. raw_send_buf[i++]=0x03;
  1260. raw_send_buf[i++]=0x03;
  1261. raw_send_buf[i++]=0x05;
  1262. }
  1263. else
  1264. {
  1265. tcph->doff=8;
  1266. int i=sizeof (struct iphdr)+20;
  1267. raw_send_buf[i++]=0x01;
  1268. raw_send_buf[i++]=0x01;
  1269. raw_send_buf[i++]=0x08; //i=0;
  1270. raw_send_buf[i++]=0x0a;
  1271. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  1272. i+=4;
  1273. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  1274. i+=4;
  1275. }
  1276. tcph->urg=0;
  1277. //tcph->window = htons((uint16_t)(1024));
  1278. tcph->window = htons((uint16_t)(10240+random()%100));
  1279. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  1280. tcph->urg_ptr = 0;
  1281. //Data part
  1282. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  1283. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  1284. memcpy(data , payload, payloadlen);
  1285. psh.source_address = info.src_ip;
  1286. psh.dest_address = sin.sin_addr.s_addr;
  1287. psh.placeholder = 0;
  1288. psh.protocol = IPPROTO_TCP;
  1289. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  1290. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  1291. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1292. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  1293. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  1294. //Ip checksum
  1295. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  1296. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  1297. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  1298. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  1299. {
  1300. if(seq_mode==0)
  1301. {
  1302. }
  1303. else if(seq_mode==1)
  1304. {
  1305. g_packet_info_send.seq+=payloadlen;
  1306. }
  1307. else if(seq_mode==2)
  1308. {
  1309. if(random()% 5==3 )
  1310. g_packet_info_send.seq+=payloadlen;
  1311. }
  1312. }
  1313. mylog(log_trace,"<ret:%d>\n",ret);
  1314. if(ret<0)
  1315. {
  1316. mylog(log_fatal,"");
  1317. perror("raw send error\n");
  1318. //printf("send error\n");
  1319. }
  1320. return 0;
  1321. }
  1322. */
  1323. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  1324. {
  1325. const packet_info_t &send_info=raw_info.send_info;
  1326. packet_info_t &recv_info=raw_info.recv_info;
  1327. static char recv_raw_icmp_buf[buf_len];
  1328. char * ip_payload;
  1329. int ip_payloadlen;
  1330. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1331. {
  1332. mylog(log_debug,"recv_raw_ip error\n");
  1333. return -1;
  1334. }
  1335. mylog(log_trace,"ip_payloadlen=%d\n",ip_payloadlen);
  1336. if(raw_ip_version==AF_INET)
  1337. {
  1338. if(recv_info.protocol!=IPPROTO_ICMP)
  1339. {
  1340. //printf("not udp protocol\n");
  1341. return -1;
  1342. }
  1343. }
  1344. else
  1345. {
  1346. assert(raw_ip_version==AF_INET6);
  1347. if(recv_info.protocol!=IPPROTO_ICMPV6)
  1348. {
  1349. //printf("not udp protocol\n");
  1350. return -1;
  1351. }
  1352. }
  1353. if(ip_payloadlen<int( sizeof(my_icmphdr) ))
  1354. {
  1355. mylog(log_debug,"too short to hold icmp header\n");
  1356. return -1;
  1357. }
  1358. my_icmphdr *icmph=(struct my_icmphdr *) (ip_payload);
  1359. if(ntohs(icmph->id)!=send_info.src_port)
  1360. {
  1361. mylog(log_debug,"icmp id mis-match,ignored\n");
  1362. return -1;
  1363. }
  1364. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  1365. recv_info.my_icmp_seq=ntohs(icmph->seq);
  1366. if(icmph->code!=0)
  1367. return -1;
  1368. unsigned short check ;
  1369. if(raw_ip_version==AF_INET)
  1370. {
  1371. if(program_mode==client_mode)
  1372. {
  1373. if(icmph->type!=0)
  1374. return -1;
  1375. }
  1376. else
  1377. {
  1378. if(icmph->type!=8)
  1379. return -1;
  1380. }
  1381. check= csum( (unsigned short*) ip_payload, ip_payloadlen);
  1382. }
  1383. else
  1384. {
  1385. assert(raw_ip_version==AF_INET6);
  1386. if(program_mode==client_mode)
  1387. {
  1388. if(icmph->type!=129)
  1389. return -1;
  1390. }
  1391. else
  1392. {
  1393. if(icmph->type!=128)
  1394. return -1;
  1395. }
  1396. pseudo_header6 tmp_header;
  1397. struct pseudo_header6 *psh=&tmp_header ;
  1398. psh->src = recv_info.new_src_ip.v6;
  1399. psh->dst = recv_info.new_dst_ip.v6;
  1400. psh->placeholder1 = 0;
  1401. psh->placeholder2 = 0;
  1402. psh->next_header = IPPROTO_ICMPV6;
  1403. psh->tcp_length = htons(ip_payloadlen);
  1404. check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, ip_payloadlen);
  1405. }
  1406. if(check!=0)
  1407. {
  1408. mylog(log_debug,"icmp checksum fail %x\n",check);
  1409. return -1;
  1410. }
  1411. //mylog(log_info,"program_mode=%d\n",program_mode);
  1412. /*
  1413. if(program_mode==server_mode)
  1414. {
  1415. send_info.icmp_seq=ntohs(icmph->seq);
  1416. //mylog(log_info,"send_info.seq=%d\n",send_info.seq);
  1417. }*/
  1418. payload=ip_payload+sizeof(my_icmphdr);
  1419. payloadlen=ip_payloadlen-sizeof(my_icmphdr);
  1420. mylog(log_trace,"get a packet len=%d\n",payloadlen);
  1421. return 0;
  1422. }
  1423. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  1424. {
  1425. const packet_info_t &send_info=raw_info.send_info;
  1426. packet_info_t &recv_info=raw_info.recv_info;
  1427. //static char recv_raw_udp_buf[buf_len];
  1428. char * ip_payload;
  1429. int ip_payloadlen;
  1430. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1431. {
  1432. mylog(log_debug,"recv_raw_ip error\n");
  1433. return -1;
  1434. }
  1435. if(recv_info.protocol!=IPPROTO_UDP)
  1436. {
  1437. //printf("not udp protocol\n");
  1438. return -1;
  1439. }
  1440. if(ip_payloadlen<int( sizeof(my_udphdr) ))
  1441. {
  1442. mylog(log_debug,"too short to hold udpheader\n");
  1443. return -1;
  1444. }
  1445. my_udphdr *udph=(struct my_udphdr*)ip_payload;
  1446. if(int(ntohs(udph->len))!=ip_payloadlen)
  1447. {
  1448. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  1449. return -1;
  1450. }
  1451. if(udph->dest!=ntohs(uint16_t(filter_port)))
  1452. {
  1453. //printf("%x %x",tcph->dest,);
  1454. return -1;
  1455. }
  1456. //memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1457. /*
  1458. pseudo_header tmp_header={0};
  1459. struct pseudo_header *psh=&tmp_header ;
  1460. psh->source_address = recv_info.new_src_ip.v4;
  1461. psh->dest_address = recv_info.new_dst_ip.v4;
  1462. psh->placeholder = 0;
  1463. psh->protocol = IPPROTO_UDP;
  1464. psh->tcp_length = htons(ip_payloadlen);
  1465. int csum_len=ip_payloadlen;
  1466. uint16_t udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1467. */
  1468. uint16_t udp_chk;
  1469. int csum_len=ip_payloadlen;
  1470. if(raw_ip_version==AF_INET)
  1471. {
  1472. pseudo_header tmp_header;
  1473. struct pseudo_header *psh=&tmp_header ;
  1474. psh->source_address = recv_info.new_src_ip.v4;
  1475. psh->dest_address = recv_info.new_dst_ip.v4;
  1476. psh->placeholder = 0;
  1477. psh->protocol = IPPROTO_UDP;
  1478. psh->tcp_length = htons(ip_payloadlen);
  1479. udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1480. }
  1481. else
  1482. {
  1483. assert(raw_ip_version==AF_INET6);
  1484. pseudo_header6 tmp_header;
  1485. struct pseudo_header6 *psh=&tmp_header ;
  1486. psh->src = recv_info.new_src_ip.v6;
  1487. psh->dst = recv_info.new_dst_ip.v6;
  1488. psh->placeholder1 = 0;
  1489. psh->placeholder2 = 0;
  1490. psh->next_header = IPPROTO_UDP;
  1491. psh->tcp_length = htons(ip_payloadlen);
  1492. udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, csum_len);
  1493. }
  1494. if(udp_chk!=0)
  1495. {
  1496. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  1497. mylog(log_debug,"udp header error\n");
  1498. return -1;
  1499. }
  1500. char *udp_begin=ip_payload;
  1501. recv_info.src_port=ntohs(udph->source);
  1502. recv_info.dst_port=ntohs(udph->dest);
  1503. payloadlen = ip_payloadlen-sizeof(my_udphdr);
  1504. payload=udp_begin+sizeof(my_udphdr);
  1505. return 0;
  1506. }
  1507. int parse_tcp_option(char * option_begin,char * option_end,packet_info_t &recv_info)
  1508. {
  1509. recv_info.has_ts=0;
  1510. recv_info.ts=0;
  1511. char *ptr=option_begin;
  1512. //char *option_end=tcp_begin+tcp_hdr_len;
  1513. while(ptr<option_end)
  1514. {
  1515. if(*ptr==0)
  1516. {
  1517. return 0;
  1518. }
  1519. else if(*ptr==1)
  1520. {
  1521. ptr++;
  1522. }
  1523. else if(*ptr==8)
  1524. {
  1525. if(ptr+1>=option_end)
  1526. {
  1527. mylog(log_trace,"invaild option ptr+1==option_end,for ts\n");
  1528. return -1;
  1529. }
  1530. if(*(ptr+1)!=10)
  1531. {
  1532. mylog(log_trace,"invaild ts len\n");
  1533. return -1;
  1534. }
  1535. if(ptr+10>option_end)
  1536. {
  1537. mylog(log_trace,"ptr+10>option_end for ts\n");
  1538. return -1;
  1539. }
  1540. recv_info.has_ts=1;
  1541. recv_info.ts= read_u32(ptr+2);
  1542. recv_info.ts_ack=read_u32(ptr+6);
  1543. //printf("<%d %d>!\n",recv_info.ts,recv_info.ts_ack);
  1544. //return 0;//we currently only parse ts, so just return after its found
  1545. ptr+=8;
  1546. }
  1547. else
  1548. {
  1549. if(ptr+1>=option_end)
  1550. {
  1551. mylog(log_trace,"invaild option ptr+1==option_end\n");
  1552. return -1;
  1553. }
  1554. else
  1555. {
  1556. int len=(unsigned char)*(ptr+1);
  1557. if(len<=1)
  1558. {
  1559. mylog(log_trace,"invaild option len %d\n",len);
  1560. return -1;
  1561. }
  1562. //omit check
  1563. ptr+=len;
  1564. }
  1565. }
  1566. //printf("!");
  1567. }
  1568. //printf("\n");
  1569. return 0;
  1570. }
  1571. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1572. {
  1573. const packet_info_t &send_info=raw_info.send_info;
  1574. packet_info_t &recv_info=raw_info.recv_info;
  1575. //static char recv_raw_tcp_buf[buf_len];
  1576. char * ip_payload;
  1577. int ip_payloadlen;
  1578. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1579. {
  1580. mylog(log_debug,"recv_raw_ip error\n");
  1581. return -1;
  1582. }
  1583. if(recv_info.protocol!=IPPROTO_TCP)
  1584. {
  1585. //printf("not tcp protocol\n");
  1586. return -1;
  1587. }
  1588. my_tcphdr * tcph=(struct my_tcphdr*)ip_payload;
  1589. unsigned short tcphdrlen = tcph->doff*4;
  1590. if (!(tcphdrlen > 0 && tcphdrlen <=60)) {
  1591. mylog(log_debug,"tcph error\n");
  1592. return 0;
  1593. }
  1594. if (tcphdrlen >ip_payloadlen) {
  1595. mylog(log_debug,"error,tcphdrlen >ip_payloadlen\n");
  1596. return 0;
  1597. }
  1598. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1599. {
  1600. //printf("%x %x",tcph->dest,);
  1601. return -1;
  1602. }
  1603. // memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1604. uint16_t tcp_chk;
  1605. int csum_len=ip_payloadlen;
  1606. if(raw_ip_version==AF_INET)
  1607. {
  1608. pseudo_header tmp_header;
  1609. struct pseudo_header *psh=&tmp_header ;
  1610. psh->source_address = recv_info.new_src_ip.v4;
  1611. psh->dest_address = recv_info.new_dst_ip.v4;
  1612. psh->placeholder = 0;
  1613. psh->protocol = IPPROTO_TCP;
  1614. psh->tcp_length = htons(ip_payloadlen);
  1615. tcp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1616. }
  1617. else
  1618. {
  1619. assert(raw_ip_version==AF_INET6);
  1620. pseudo_header6 tmp_header;
  1621. struct pseudo_header6 *psh=&tmp_header ;
  1622. psh->src = recv_info.new_src_ip.v6;
  1623. psh->dst = recv_info.new_dst_ip.v6;
  1624. psh->placeholder1 = 0;
  1625. psh->placeholder2 = 0;
  1626. psh->next_header = IPPROTO_TCP;
  1627. psh->tcp_length = htons(ip_payloadlen);
  1628. tcp_chk = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, csum_len);
  1629. }
  1630. /*for(int i=0;i<csum_len;i++)
  1631. {
  1632. printf("<%d>",int(ip_payload[i]));
  1633. }
  1634. printf("\n");*/
  1635. if(tcp_chk!=0)
  1636. {
  1637. mylog(log_debug,"tcp_chk:%x, tcp checksum failed, ignored\n",tcp_chk);
  1638. //return -1;
  1639. }
  1640. char *tcp_begin=ip_payload; //ip packet's data part
  1641. char *tcp_option=ip_payload+sizeof(my_tcphdr);
  1642. char *option_end=ip_payload+tcphdrlen;
  1643. /*
  1644. //old ts parse code
  1645. recv_info.has_ts=0;
  1646. recv_info.ts=0;
  1647. if(tcph->doff==10)
  1648. {
  1649. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1650. {
  1651. recv_info.has_ts=1;
  1652. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[8]));
  1653. memcpy(&recv_info.ts,&tcp_option[8],sizeof(recv_info.ts));
  1654. recv_info.ts=ntohl(recv_info.ts);
  1655. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[12]));
  1656. memcpy(&recv_info.ts_ack,&tcp_option[12],sizeof(recv_info.ts_ack));
  1657. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1658. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1659. }
  1660. else
  1661. {
  1662. // mylog(log_info,"\n");
  1663. }
  1664. }
  1665. else if(tcph->doff==8)
  1666. {
  1667. if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
  1668. {
  1669. recv_info.has_ts=1;
  1670. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[4]));
  1671. memcpy(&recv_info.ts,&tcp_option[4],sizeof(recv_info.ts));
  1672. recv_info.ts=ntohl(recv_info.ts);
  1673. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[8]));
  1674. memcpy(&recv_info.ts_ack,&tcp_option[8],sizeof(recv_info.ts_ack));
  1675. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1676. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1677. }
  1678. else
  1679. {
  1680. //mylog(log_info,"!!!\n");
  1681. }
  1682. }
  1683. else
  1684. {
  1685. //mylog(log_info,"tcph->doff= %u\n",tcph->doff);
  1686. }
  1687. printf("<%d %d>\n",recv_info.ts,recv_info.ts_ack);
  1688. */
  1689. parse_tcp_option(tcp_option,option_end,recv_info);
  1690. recv_info.ack=tcph->ack;
  1691. recv_info.syn=tcph->syn;
  1692. recv_info.rst=tcph->rst;
  1693. recv_info.src_port=ntohs(tcph->source);
  1694. recv_info.dst_port=ntohs(tcph->dest);
  1695. recv_info.seq=ntohl(tcph->seq);
  1696. // recv_info.last_last_ack_seq=recv_info.last_ack_seq;
  1697. //recv_info.last_ack_seq=recv_info.ack_seq;
  1698. u32_t last_ack_seq=recv_info.ack_seq;
  1699. recv_info.ack_seq=ntohl(tcph->ack_seq);
  1700. if(recv_info.ack_seq==last_ack_seq)
  1701. {
  1702. recv_info.ack_seq_counter++;
  1703. }
  1704. else
  1705. {
  1706. recv_info.ack_seq_counter=0;
  1707. }
  1708. recv_info.psh=tcph->psh;
  1709. if(tcph->rst==1)
  1710. {
  1711. raw_info.rst_received++;
  1712. if(max_rst_to_show>0)
  1713. {
  1714. if(raw_info.rst_received < max_rst_to_show)
  1715. {
  1716. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1717. }
  1718. else if(raw_info.rst_received == max_rst_to_show)
  1719. {
  1720. mylog(log_warn,"[%s,%d]rst==1,cnt=%d >=max_rst_to_show, this log will be muted for current connection\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1721. }
  1722. else
  1723. {
  1724. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1725. }
  1726. }
  1727. else if(max_rst_to_show==0)
  1728. {
  1729. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1730. }
  1731. else
  1732. {
  1733. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1734. }
  1735. if(max_rst_allowed>=0 && raw_info.rst_received==max_rst_allowed+1 )
  1736. {
  1737. mylog(log_warn,"[%s,%d]connection disabled because of rst_received=%d > max_rst_allow=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received,(int)max_rst_allowed );
  1738. raw_info.disabled=1;
  1739. }
  1740. }
  1741. /* if(recv_info.has_ts)
  1742. {
  1743. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  1744. }*/
  1745. payloadlen = ip_payloadlen-tcphdrlen;
  1746. payload=tcp_begin+tcphdrlen;
  1747. /*if (recv_info.syn == 0 && recv_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  1748. {
  1749. send_info.ack_seq=recv_info.seq;
  1750. }*/
  1751. raw_info.recv_info.data_len=payloadlen;
  1752. return 0;
  1753. }
  1754. /*
  1755. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  1756. {
  1757. static char buf[buf_len];
  1758. char raw_recv_buf[buf_len];
  1759. char raw_recv_buf2[buf_len];
  1760. char raw_recv_buf3[buf_len];
  1761. iphdr * iph;
  1762. tcphdr * tcph;
  1763. int size;
  1764. struct sockaddr saddr;
  1765. socklen_t saddr_size;
  1766. saddr_size = sizeof(saddr);
  1767. mylog(log_trace,"raw!\n");
  1768. size = recvfrom(raw_recv_fd, buf, max_data_len, 0 ,&saddr , &saddr_size);
  1769. if(buf[12]!=8||buf[13]!=0)
  1770. {
  1771. mylog(log_debug,"not an ipv4 packet!\n");
  1772. return -1;
  1773. }
  1774. char *ip_begin=buf+14;
  1775. iph = (struct iphdr *) (ip_begin);
  1776. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1777. mylog(log_debug,"iph ihl error");
  1778. return -1;
  1779. }
  1780. if (iph->protocol != IPPROTO_TCP) {
  1781. mylog(log_debug,"iph protocal != tcp\n");
  1782. return -1;
  1783. }
  1784. int ip_len=ntohs(iph->tot_len);
  1785. unsigned short iphdrlen =iph->ihl*4;
  1786. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1787. unsigned short tcphdrlen = tcph->doff*4;
  1788. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1789. mylog(log_debug,"tcph error");
  1790. return 0;
  1791. }
  1792. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1793. {
  1794. //printf("%x %x",tcph->dest,);
  1795. return -1;
  1796. }
  1797. /////ip
  1798. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1799. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1800. /////ip end
  1801. ///tcp
  1802. struct pseudo_header psh;
  1803. psh.source_address = iph->saddr;
  1804. psh.dest_address = iph->daddr;
  1805. psh.placeholder = 0;
  1806. psh.protocol = IPPROTO_TCP;
  1807. psh.tcp_length = htons(ip_len-iphdrlen);
  1808. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1809. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1810. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1811. if(ip_chk!=0)
  1812. {
  1813. mylog(log_debug,"ip header error %d\n",ip_chk);
  1814. return -1;
  1815. }
  1816. if(tcp_chk!=0)
  1817. {
  1818. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1819. mylog(log_debug,"tcp header error\n");
  1820. return -1;
  1821. }
  1822. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  1823. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1824. info.has_ts=0;
  1825. if(tcph->doff==10)
  1826. {
  1827. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1828. {
  1829. info.has_ts=1;
  1830. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1831. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1832. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1833. }
  1834. }
  1835. else if(tcph->doff==8)
  1836. {
  1837. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1838. {
  1839. info.has_ts=1;
  1840. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1841. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1842. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1843. }
  1844. }
  1845. if(tcph->rst==1)
  1846. {
  1847. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1848. }
  1849. info.ack=tcph->ack;
  1850. info.syn=tcph->syn;
  1851. info.rst=tcph->rst;
  1852. info.src_port=ntohs(tcph->source);
  1853. info.src_ip=iph->saddr;
  1854. info.seq=ntohl(tcph->seq);
  1855. info.ack_seq=ntohl(tcph->ack_seq);
  1856. info.psh=tcph->psh;
  1857. if(info.has_ts)
  1858. {
  1859. g_packet_info_send.ts_ack=info.ts;
  1860. }
  1861. ////tcp end
  1862. payloadlen = ip_len-tcphdrlen-iphdrlen;
  1863. payload=ip_begin+tcphdrlen+iphdrlen;
  1864. if(payloadlen>0&&payload[0]=='h')
  1865. {
  1866. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  1867. }
  1868. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  1869. {
  1870. //if(seq_increse)
  1871. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  1872. }
  1873. //printf("%d\n",ip_len);
  1874. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  1875. return 0;
  1876. }*/
  1877. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
  1878. {
  1879. if (random_drop != 0) {
  1880. if (get_true_random_number() % 10000 < (u32_t) random_drop) {
  1881. return 0;
  1882. }
  1883. }
  1884. packet_info_t &send_info=raw_info.send_info;
  1885. packet_info_t &recv_info=raw_info.recv_info;
  1886. mylog(log_trace,"send_raw : from %s %d to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
  1887. switch(raw_mode)
  1888. {
  1889. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  1890. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  1891. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  1892. default:return -1;
  1893. }
  1894. }
  1895. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1896. {
  1897. packet_info_t &send_info=raw_info.send_info;
  1898. packet_info_t &recv_info=raw_info.recv_info;
  1899. switch(raw_mode)
  1900. {
  1901. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  1902. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  1903. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  1904. default: return -1;
  1905. }
  1906. }
  1907. int after_send_raw0(raw_info_t &raw_info)
  1908. {
  1909. packet_info_t &send_info=raw_info.send_info;
  1910. packet_info_t &recv_info=raw_info.recv_info;
  1911. if(raw_mode==mode_faketcp)
  1912. {
  1913. 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
  1914. {
  1915. if (seq_mode == 0)
  1916. {
  1917. } else if (seq_mode == 1)
  1918. {
  1919. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1920. } else if (seq_mode == 2)
  1921. {
  1922. if (get_true_random_number() % 5 == 3)
  1923. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1924. }
  1925. else if(seq_mode==3||seq_mode==4)
  1926. {
  1927. send_info.seq += raw_info.send_info.data_len;
  1928. u32_t window_size;
  1929. if(seq_mode==3)
  1930. {
  1931. window_size=(u32_t)((u32_t)receive_window_lower_bound<<(u32_t)wscale);
  1932. }
  1933. else//seq_mode==4
  1934. {
  1935. window_size=(u32_t)((u32_t)receive_window_lower_bound);
  1936. }
  1937. if(larger_than_u32(send_info.seq+max_data_len,recv_info.ack_seq+window_size))
  1938. {
  1939. send_info.seq=raw_info.recv_info.ack_seq;
  1940. }
  1941. if(recv_info.ack_seq_counter>=3) //simulate tcp fast re-transmit
  1942. {
  1943. recv_info.ack_seq_counter=0;
  1944. send_info.seq=raw_info.recv_info.ack_seq;
  1945. }
  1946. if(larger_than_u32(raw_info.recv_info.ack_seq,send_info.seq)) //for further use,currently no effect.
  1947. {
  1948. send_info.seq=raw_info.recv_info.ack_seq;
  1949. }
  1950. }
  1951. }
  1952. }
  1953. if(raw_mode==mode_icmp)
  1954. {
  1955. if(program_mode==client_mode)
  1956. {
  1957. send_info.my_icmp_seq++;
  1958. }
  1959. }
  1960. return 0;
  1961. }
  1962. int after_recv_raw0(raw_info_t &raw_info)
  1963. {
  1964. packet_info_t &send_info=raw_info.send_info;
  1965. packet_info_t &recv_info=raw_info.recv_info;
  1966. if(raw_mode==mode_faketcp)
  1967. {
  1968. if(recv_info.has_ts)
  1969. send_info.ts_ack=recv_info.ts;
  1970. 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
  1971. {
  1972. if(seq_mode==0||seq_mode==1||seq_mode==2)
  1973. {
  1974. if(larger_than_u32(recv_info.seq+raw_info.recv_info.data_len,send_info.ack_seq))
  1975. send_info.ack_seq = recv_info.seq+raw_info.recv_info.data_len;//TODO only update if its larger
  1976. }
  1977. else if(seq_mode==3||seq_mode==4)
  1978. {
  1979. if(recv_info.seq==send_info.ack_seq)
  1980. {
  1981. send_info.ack_seq=recv_info.seq+raw_info.recv_info.data_len;//currently we dont remembr tcp segments,this is the simplest way
  1982. //TODO implement tcp segment remembering and SACK.
  1983. }
  1984. }
  1985. }
  1986. }
  1987. if(raw_mode==mode_icmp)
  1988. {
  1989. if(program_mode==server_mode)
  1990. {
  1991. if(larger_than_u16(recv_info.my_icmp_seq,send_info.my_icmp_seq))
  1992. send_info.my_icmp_seq = recv_info.my_icmp_seq; //TODO only update if its larger
  1993. }
  1994. }
  1995. return 0;
  1996. }
  1997. /*
  1998. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  1999. {
  2000. packet_info_t &send_info=raw_info.send_info;
  2001. packet_info_t &recv_info=raw_info.recv_info;
  2002. int ret=send_raw0(raw_info,payload,payloadlen);
  2003. if(ret<0) return ret;
  2004. else
  2005. {
  2006. after_send_raw0(raw_info);
  2007. return ret;
  2008. }
  2009. }
  2010. int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
  2011. {
  2012. packet_info_t &send_info=raw_info.send_info;
  2013. packet_info_t &recv_info=raw_info.recv_info;
  2014. int ret=recv_raw0(raw_info,payload,payloadlen);
  2015. if(ret<0) return ret;
  2016. else
  2017. {
  2018. after_recv_raw0(raw_info);
  2019. return ret;
  2020. }
  2021. }*/
  2022. /*
  2023. int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port) //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
  2024. {
  2025. struct sockaddr_in remote_addr_in={0};
  2026. socklen_t slen = sizeof(sockaddr_in);
  2027. //memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  2028. remote_addr_in.sin_family = AF_INET;
  2029. remote_addr_in.sin_port = htons(remote_port);
  2030. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
  2031. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  2032. if(new_udp_fd<0)
  2033. {
  2034. mylog(log_warn,"create udp_fd error\n");
  2035. return -1;
  2036. }
  2037. //set_buf_size(new_udp_fd);
  2038. mylog(log_debug,"created new udp_fd %d\n",new_udp_fd);
  2039. int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  2040. if(ret!=0)
  2041. {
  2042. mylog(log_warn,"udp fd connect fail\n");
  2043. close(new_udp_fd);
  2044. return -1;
  2045. }
  2046. struct sockaddr_in my_addr={0};
  2047. socklen_t len=sizeof(my_addr);
  2048. if(getsockname(new_udp_fd, (struct sockaddr *) &my_addr, &len)!=0){close(new_udp_fd); return -1;}
  2049. ip=my_addr.sin_addr.s_addr;
  2050. close(new_udp_fd);
  2051. return 0;
  2052. }*/
  2053. int get_src_adress2(address_t &output_addr,address_t remote_addr)
  2054. {
  2055. int new_udp_fd=remote_addr.new_connected_udp_fd();
  2056. if(new_udp_fd<0)
  2057. {
  2058. mylog(log_warn,"create udp_fd error\n");
  2059. return -1;
  2060. }
  2061. socklen_t len=sizeof(output_addr.inner);
  2062. if(getsockname(new_udp_fd, (struct sockaddr *) &output_addr.inner, &len)!=0) {close(new_udp_fd); return -1;}
  2063. assert(output_addr.get_type()==remote_addr.get_type());
  2064. close(new_udp_fd);
  2065. return 0;
  2066. }
  2067. /*
  2068. int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port) //try to bind to a port,may fail.
  2069. {
  2070. int old_bind_fd=fd;
  2071. if(raw_mode==mode_faketcp)
  2072. {
  2073. fd=socket(AF_INET,SOCK_STREAM,0);
  2074. }
  2075. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  2076. {
  2077. fd=socket(AF_INET,SOCK_DGRAM,0);
  2078. }
  2079. if(old_bind_fd!=-1)
  2080. {
  2081. close(old_bind_fd);
  2082. }
  2083. struct sockaddr_in temp_bind_addr={0};
  2084. //bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  2085. temp_bind_addr.sin_family = AF_INET;
  2086. temp_bind_addr.sin_port = htons(port);
  2087. temp_bind_addr.sin_addr.s_addr = local_ip_uint32;
  2088. if (bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  2089. {
  2090. mylog(log_debug,"bind fail\n");
  2091. return -1;
  2092. }
  2093. if(raw_mode==mode_faketcp)
  2094. {
  2095. if (listen(fd, SOMAXCONN) != 0) {
  2096. mylog(log_warn,"listen fail\n");
  2097. return -1;
  2098. }
  2099. }
  2100. return 0;
  2101. }*/
  2102. int try_to_list_and_bind2(int &fd,address_t address) //try to bind to a port,may fail.
  2103. {
  2104. if(fd!=-1)
  2105. {
  2106. close(fd);
  2107. }
  2108. if(raw_mode==mode_faketcp)
  2109. {
  2110. fd=socket(address.get_type(),SOCK_STREAM,0);
  2111. }
  2112. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  2113. {
  2114. fd=socket(address.get_type(),SOCK_DGRAM,0);
  2115. }
  2116. if(fd==-1)
  2117. {
  2118. mylog(log_debug,"create fd fail\n");
  2119. return -1;
  2120. }
  2121. /*struct sockaddr_in temp_bind_addr={0};
  2122. //bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  2123. temp_bind_addr.sin_family = AF_INET;
  2124. temp_bind_addr.sin_port = htons(port);
  2125. temp_bind_addr.sin_addr.s_addr = local_ip_uint32;*/
  2126. if (bind(fd, (struct sockaddr*)&address.inner, address.get_len()) !=0)
  2127. {
  2128. mylog(log_debug,"bind fail\n");
  2129. return -1;
  2130. }
  2131. if(raw_mode==mode_faketcp&&!use_tcp_dummy_socket)
  2132. {
  2133. if (listen(fd, SOMAXCONN) != 0) {
  2134. mylog(log_warn,"listen fail\n");
  2135. return -1;
  2136. }
  2137. }
  2138. return 0;
  2139. }
  2140. /*
  2141. int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port and bind to it.
  2142. {
  2143. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  2144. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  2145. {
  2146. if (try_to_list_and_bind(fd,local_ip_uint32,raw_send_port)==0)
  2147. {
  2148. return raw_send_port;
  2149. }
  2150. }
  2151. mylog(log_fatal,"bind port fail\n");
  2152. myexit(-1);
  2153. return -1;////for compiler check
  2154. }*/
  2155. int client_bind_to_a_new_port2(int &fd,const address_t& address)//find a free port and bind to it.
  2156. {
  2157. address_t tmp=address;
  2158. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  2159. {
  2160. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  2161. tmp.set_port(raw_send_port);
  2162. if (try_to_list_and_bind2(fd,tmp)==0)
  2163. {
  2164. return raw_send_port;
  2165. }
  2166. }
  2167. mylog(log_fatal,"bind port fail\n");
  2168. myexit(-1);
  2169. return -1;////for compiler check
  2170. }