relay.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <regex.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/evp.h>
  14. #include "relay.h"
  15. #include "slitheen.h"
  16. #include "flow.h"
  17. #include "crypto.h"
  18. #define DEBUG
  19. /** Called when a TLS application record is received for a
  20. * tagged flow. Upstream packets will be checked for covert
  21. * requests to censored sites, downstream packets will be
  22. * replaced with data from the censored queue or with garbage
  23. *
  24. * Inputs:
  25. * f: the tagged flow
  26. * info: the processed received application packet
  27. *
  28. * Output:
  29. * 0 on success, 1 on failure
  30. */
  31. int replace_packet(flow *f, struct packet_info *info){
  32. if (info == NULL || info->tcp_hdr == NULL){
  33. return 0;
  34. }
  35. #ifdef DEBUG
  36. fprintf(stdout,"Flow: %x:%d > %x:%d (%s)\n", info->ip_hdr->src.s_addr, ntohs(info->tcp_hdr->src_port), info->ip_hdr->dst.s_addr, ntohs(info->tcp_hdr->dst_port), (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  37. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  38. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  39. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  40. fflush(stdout);
  41. #endif
  42. if(info->app_data_len <= 0){
  43. return 0;
  44. }
  45. /* if outgoing, decrypt and look at header */
  46. if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
  47. read_header(f, info);
  48. return 0;
  49. } else {
  50. #ifdef DEBUG
  51. printf("Current sequence number: %d\n", f->downstream_seq_num);
  52. printf("Received sequence number: %d\n", htonl(info->tcp_hdr->sequence_num));
  53. #endif
  54. uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->downstream_seq_num;
  55. if(offset == 0)
  56. f->downstream_seq_num += info->app_data_len;
  57. /* if incoming, replace with data from queue */
  58. //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
  59. process_downstream(f, offset, info);
  60. //}//TODO: need to do something about replaying packets (maybe store previously sent data??
  61. #ifdef DEBUG2 //TODO: fix
  62. uint8_t *p = (uint8_t *) info->tcp_hdr;
  63. fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
  64. fprintf(stdout, "Injecting the following packet:\n");
  65. for(int i=0; i< htons(info->ip_hdr->len)-1; i++){
  66. fprintf(stdout, "%02x ", p[i]);
  67. }
  68. fprintf(stdout, "\n");
  69. fflush(stdout);
  70. #endif
  71. }
  72. return 0;
  73. }
  74. /** Reads the HTTP header of upstream data and searches for
  75. * a covert request in an x-slitheen header. Sends this
  76. * request to the indicated site and saves the response to
  77. * the censored queue
  78. *
  79. * TODO: change this to take SOCKS5 proxy connection and then relay data
  80. * based on stream ID
  81. *
  82. * Inputs:
  83. * f: the tagged flow
  84. * info: the processed received packet
  85. *
  86. * Ouput:
  87. * 0 on success, 1 on failure
  88. */
  89. int read_header(flow *f, struct packet_info *info){
  90. uint8_t *p = info->app_data;
  91. if (info->tcp_hdr == NULL){
  92. return 0;
  93. }
  94. uint8_t *record_ptr = NULL;
  95. struct record_header *record_hdr;
  96. uint32_t record_length;
  97. if(f->upstream_remaining > 0){
  98. //check to see whether the previous record has finished
  99. if(f->upstream_remaining > info->app_data_len){
  100. //ignore entire packet for now
  101. queue_block *new_block = calloc(1, sizeof(queue_block));
  102. uint8_t *block_data = calloc(1, info->app_data_len);
  103. memcpy(block_data, p, info->app_data_len);
  104. new_block->len = info->app_data_len;
  105. new_block->offset = 0;
  106. new_block->data = block_data;
  107. new_block->next = NULL;
  108. //add block to upstream data chain
  109. if(f->upstream_queue == NULL){
  110. f->upstream_queue = new_block;
  111. } else {
  112. queue_block *last = f->upstream_queue;
  113. while(last->next != NULL){
  114. last = last->next;
  115. }
  116. last->next = new_block;
  117. }
  118. f->upstream_remaining -= info->app_data_len;
  119. return 0;
  120. } else {
  121. //process what we have
  122. record_hdr = (struct record_header*) f->upstream_queue->data;
  123. record_length = RECORD_LEN(record_hdr);
  124. record_ptr = calloc(1, record_length+ RECORD_HEADER_LEN);
  125. queue_block *current = f->upstream_queue;
  126. int32_t offset =0;
  127. while(f->upstream_queue != NULL){
  128. memcpy(record_ptr+offset, current->data, current->len);
  129. offset += current->len;
  130. free(current->data);
  131. f->upstream_queue = current->next;
  132. free(current);
  133. current = f->upstream_queue;
  134. }
  135. memcpy(record_ptr+offset, p, f->upstream_remaining);
  136. p = record_ptr;
  137. record_hdr = (struct record_header*) p;
  138. f->upstream_remaining = 0;
  139. }
  140. } else {
  141. //check to see if the new record is too long
  142. record_hdr = (struct record_header*) p;
  143. record_length = RECORD_LEN(record_hdr);
  144. if(record_length > info->app_data_len){
  145. //add info to upstream queue
  146. queue_block *new_block = calloc(1, sizeof(queue_block));
  147. uint8_t *block_data = calloc(1, info->app_data_len);
  148. memcpy(block_data, p, info->app_data_len);
  149. new_block->len = info->app_data_len;
  150. new_block->offset = record_length; //re-appropriate this for len of record
  151. new_block->data = block_data;
  152. new_block->next = NULL;
  153. //add block to upstream queue
  154. if(f->upstream_queue == NULL){
  155. f->upstream_queue = new_block;
  156. } else {
  157. queue_block *last = f->upstream_queue;
  158. while(last->next != NULL){
  159. last = last->next;
  160. }
  161. last->next = new_block;
  162. }
  163. f->upstream_remaining = record_length - new_block->len;
  164. return 0;
  165. }
  166. }
  167. p+= RECORD_HEADER_LEN;
  168. uint8_t *decrypted_data = calloc(1, record_length);
  169. memcpy(decrypted_data, p, record_length);
  170. int32_t decrypted_len = encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0);
  171. if(decrypted_len<0){
  172. if(record_ptr != NULL)
  173. free(record_ptr);
  174. free(decrypted_data);
  175. return 0;
  176. }
  177. if(record_hdr->type == 0x15){
  178. printf("received alert\n");
  179. for(int i=0; i<record_length; i++){
  180. printf("%02x ", decrypted_data[i]);
  181. }
  182. fflush(stdout);
  183. }
  184. #ifdef DEBUG
  185. printf("Upstream data: (%x:%d > %x:%d )\n",info->ip_hdr->src.s_addr,ntohs(info->tcp_hdr->src_port), info->ip_hdr->dst.s_addr, ntohs(info->tcp_hdr->dst_port));
  186. printf("%s\n", decrypted_data+EVP_GCM_TLS_EXPLICIT_IV_LEN);
  187. #endif
  188. /* search through decrypted data for x-ignore */
  189. char *header_ptr = strstr((const char *) decrypted_data+EVP_GCM_TLS_EXPLICIT_IV_LEN, "X-Slitheen");
  190. uint8_t *upstream_data;
  191. if(header_ptr == NULL){
  192. printf("Slitheen header not found(%x:%d > %x:%d) \n",info->ip_hdr->src.s_addr,info->tcp_hdr->src_port, info->ip_hdr->dst.s_addr, info->tcp_hdr->dst_port);
  193. fflush(stdout);
  194. if(record_ptr != NULL)
  195. free(record_ptr);
  196. free(decrypted_data);
  197. return 0;
  198. }
  199. #ifdef DEBUG
  200. printf("UPSTREAM: Found x-slitheen header\n");
  201. fflush(stdout);
  202. fprintf(stdout,"UPSTREAM Flow: %x:%d > %x:%d (%s)\n", info->ip_hdr->src.s_addr,ntohs(info->tcp_hdr->src_port), info->ip_hdr->dst.s_addr, ntohs(info->tcp_hdr->dst_port) ,(info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  203. fprintf(stdout, "Sequence number: %d\n", ntohs(info->tcp_hdr->sequence_num));
  204. #endif
  205. header_ptr += strlen("X-Slitheen: ");
  206. if(*header_ptr == '\r' || *header_ptr == '\0'){
  207. printf("No messages\n");
  208. free(decrypted_data);
  209. return 0;
  210. }
  211. int32_t num_messages = 1;
  212. char *messages[50]; //TODO:make not just 10?
  213. messages[0] = header_ptr;
  214. char *c = header_ptr;
  215. while(*c != '\r' && *c != '\0'){
  216. if(*c == ' '){
  217. *c = '\0';
  218. messages[num_messages] = c+1;
  219. num_messages ++;
  220. }
  221. c++;
  222. }
  223. c++;
  224. *c = '\0';
  225. printf("UPSTREAM: Found %d messages\n", num_messages);
  226. for(int i=0; i< num_messages-1; i++){
  227. printf("Decoding message: %s\n", messages[i]);
  228. char *message = messages[i];
  229. //b64 decode the data
  230. int32_t decode_len = strlen(message);
  231. if(message[decode_len-2] == '='){
  232. decode_len = decode_len*3/4 - 2;
  233. } else if(message[decode_len-1] == '='){
  234. decode_len = decode_len*3/4 - 1;
  235. } else {
  236. decode_len = decode_len*3/4;
  237. }
  238. upstream_data = calloc(1, decode_len + 1);
  239. BIO *bio, *b64;
  240. bio = BIO_new_mem_buf(message, -1);
  241. b64 = BIO_new(BIO_f_base64());
  242. bio = BIO_push(b64, bio);
  243. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  244. int32_t output_len = BIO_read(bio, upstream_data, strlen(message));
  245. BIO_free_all(bio);
  246. #ifdef DEBUG
  247. printf("Decoded to get %d bytes:\n", output_len);
  248. for(int j=0; j< output_len; j++){
  249. printf("%02x ", upstream_data[j]);
  250. }
  251. printf("\n");
  252. fflush(stdout);
  253. #endif
  254. p = upstream_data;
  255. if(i== 0){
  256. //this is the Slitheen ID
  257. printf("Slitheen ID:");
  258. for(int j=0; j< output_len; j++){
  259. printf("%02x ", p[j]);
  260. }
  261. printf("\n");
  262. //find stream table or create new one
  263. client *last = clients->first;
  264. while(last != NULL){
  265. if(!memcmp(last->slitheen_id, p, output_len)){
  266. f->streams = last->streams;
  267. f->downstream_queue = last->downstream_queue;
  268. printf("Found client in table!\n");
  269. break;
  270. } else {
  271. for(int j=0; j< output_len; j++){
  272. printf("%02x ", last->slitheen_id[j]);
  273. }
  274. printf(" != ");
  275. for(int j=0; j< output_len; j++){
  276. printf("%02x ", p[j]);
  277. }
  278. printf("\n");
  279. }
  280. last = last->next;
  281. }
  282. if(f->streams == NULL){
  283. printf("Making new client\n");
  284. //create new client
  285. client *new_client = calloc(1, sizeof(client));
  286. memcpy(new_client->slitheen_id, p, output_len);
  287. new_client->streams = calloc(1, sizeof(stream_table));
  288. new_client->streams->first = NULL;
  289. new_client->downstream_queue = calloc(1, sizeof(data_queue));
  290. new_client->downstream_queue->first_block = NULL;
  291. new_client->next = NULL;
  292. //add to client table
  293. if(clients->first == NULL){
  294. clients->first = new_client;
  295. } else {
  296. client *last = clients->first;
  297. while(last->next != NULL){
  298. last = last->next;
  299. }
  300. last->next = new_client;
  301. }
  302. //set f's stream table
  303. f->streams = new_client->streams;
  304. f->downstream_queue = new_client->downstream_queue;
  305. }
  306. free(upstream_data);
  307. continue;
  308. }
  309. while(output_len > 0){
  310. struct sl_up_hdr *sl_hdr = (struct sl_up_hdr *) p;
  311. uint8_t stream_id = sl_hdr->stream_id;
  312. uint16_t stream_len = ntohs(sl_hdr->len);
  313. p += sizeof(struct sl_up_hdr);
  314. output_len -= sizeof(struct sl_up_hdr);
  315. stream_table *streams = f->streams;
  316. //If a thread for this stream id exists, get the thread info and pipe data
  317. int32_t stream_pipe = -1;
  318. stream *last = streams->first;
  319. if(streams->first != NULL){
  320. if(last->stream_id == stream_id){
  321. stream_pipe = last->pipefd;
  322. } else {
  323. while(last->next != NULL){
  324. last = last->next;
  325. if(last->stream_id == stream_id){
  326. stream_pipe = last->pipefd;
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. if(stream_pipe != -1){
  333. //check to see if this is a close message
  334. //if(stream_len == 0){
  335. //close(stream_pipe);
  336. //remove from stream id table
  337. //if(last == streams->first){
  338. // streams->first = last->next;
  339. //} else {
  340. // prev->next = last->next;
  341. //}
  342. //printf("Freed (1) %p\n", last);
  343. //fflush(stdout);
  344. //free(last);
  345. //break;
  346. //}
  347. if(stream_len ==0){
  348. close(stream_pipe);
  349. break;
  350. }
  351. printf("Found stream id %d\n", last->stream_id);
  352. int32_t bytes_sent = write(stream_pipe, p, stream_len);
  353. if(bytes_sent < 0){
  354. printf("Error sending bytes to stream pipe\n");
  355. }
  356. } else if(stream_len > 0){
  357. /*Else, spawn a thread to handle the proxy to this site*/
  358. printf("creating new thread for stream id %d\n", stream_id);
  359. pthread_t proxy_thread;
  360. int32_t pipefd[2];
  361. if(pipe(pipefd) < 0){
  362. free(decrypted_data);
  363. if(record_ptr != NULL)
  364. free(record_ptr);
  365. return 1;
  366. }
  367. uint8_t *initial_data = calloc(1,stream_len);
  368. memcpy(initial_data, p, stream_len);
  369. struct proxy_thread_data *thread_data =
  370. calloc(1, sizeof(struct proxy_thread_data));
  371. thread_data->initial_data = initial_data;
  372. thread_data->initial_len = stream_len;
  373. thread_data->stream_id = stream_id;
  374. thread_data->pipefd = pipefd[0];
  375. thread_data->streams = f->streams;
  376. thread_data->downstream_queue = f->downstream_queue;
  377. pthread_create(&proxy_thread, NULL, proxy_covert_site, (void *) thread_data);
  378. pthread_detach(proxy_thread);
  379. //add stream to table
  380. stream *new_stream = calloc(1, sizeof(stream));
  381. new_stream->stream_id = stream_id;
  382. new_stream->pipefd = pipefd[1];
  383. new_stream->next = NULL;
  384. if(streams->first == NULL){
  385. streams->first = new_stream;
  386. } else {
  387. stream *last = streams->first;
  388. while(last->next != NULL){
  389. last = last->next;
  390. }
  391. last->next = new_stream;
  392. }
  393. } else{
  394. printf("Error, stream len 0\n");
  395. break;
  396. }
  397. output_len -= stream_len;
  398. p += stream_len;
  399. }
  400. free(upstream_data);
  401. }
  402. //save a reference to the proxy threads in a global table
  403. free(decrypted_data);
  404. if(record_ptr != NULL)
  405. free(record_ptr);
  406. return 0;
  407. }
  408. /** Called by spawned pthreads in read_header to send upstream
  409. * data to the censored site and receive responses. Downstream
  410. * data is stored in the slitheen id's downstream_queue. Function and
  411. * thread will terminate when the client closes the connection
  412. * to the covert destination
  413. *
  414. * Input:
  415. * A struct that contains the following information:
  416. * - the tagged flow
  417. * - the initial upstream data (including connect request)
  418. * - the read end of the pipe
  419. *
  420. */
  421. void *proxy_covert_site(void *data){
  422. struct proxy_thread_data *thread_data =
  423. (struct proxy_thread_data *) data;
  424. uint8_t *p = thread_data->initial_data;
  425. uint16_t data_len = thread_data->initial_len;
  426. uint8_t stream_id = thread_data->stream_id;
  427. int32_t bytes_sent;
  428. stream_table *streams = thread_data->streams;
  429. data_queue *downstream_queue = thread_data->downstream_queue;
  430. struct socks_req *clnt_req = (struct socks_req *) p;
  431. p += 4;
  432. data_len -= 4;
  433. int32_t handle = -1;
  434. //see if it's a connect request
  435. if(clnt_req->cmd != 0x01){
  436. goto err;
  437. }
  438. struct sockaddr_in dest;
  439. dest.sin_family = AF_INET;
  440. uint8_t domain_len;
  441. switch(clnt_req->addr_type){
  442. case 0x01:
  443. //IPv4
  444. dest.sin_addr.s_addr = *((uint32_t*) p);
  445. p += 4;
  446. data_len -= 4;
  447. break;
  448. case 0x03:
  449. //domain name
  450. domain_len = p[0];
  451. p++;
  452. data_len --;
  453. uint8_t *domain_name = calloc(1, domain_len+1);
  454. memcpy(domain_name, p, domain_len);
  455. domain_name[domain_len] = '\0';
  456. struct hostent *host;
  457. host = gethostbyname((const char *) domain_name);
  458. dest.sin_addr = *((struct in_addr *) host->h_addr);
  459. p += domain_len;
  460. data_len -= domain_len;
  461. free(domain_name);
  462. break;
  463. case 0x04:
  464. //IPv6
  465. goto err;//TODO: fix this
  466. break;
  467. }
  468. //now set the port
  469. dest.sin_port = *((uint16_t *) p);
  470. p += 2;
  471. data_len -= 2;
  472. handle = socket(AF_INET, SOCK_STREAM, 0);
  473. if(handle < 0){
  474. goto err;
  475. }
  476. struct sockaddr_in my_addr;
  477. socklen_t my_addr_len = sizeof(my_addr);
  478. int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
  479. if(error <0){
  480. goto err;
  481. }
  482. getsockname(handle, (struct sockaddr *) &my_addr, &my_addr_len);
  483. printf("Bound to %x:%d\n", my_addr.sin_addr.s_addr, ntohs(my_addr.sin_port));
  484. #ifdef OLD
  485. uint8_t *response = calloc(1, 11);
  486. //now send the reply to the client
  487. response[0] = 0x05;
  488. response[1] = 0x00;//TODO: make this accurate
  489. response[2] = 0x00;
  490. response[3] = 0x01;
  491. *((uint32_t *) (response + 4)) = my_addr.sin_addr.s_addr;
  492. *((uint16_t *) (response + 8)) = my_addr.sin_port;
  493. printf("Downstream response (id %d):\n", stream_id);
  494. for(int i=0; i< 10; i++){
  495. printf("%02x ", response[i]);
  496. }
  497. printf("\n");
  498. fflush(stdout);
  499. //No longer need to send response
  500. queue_block *new_block = calloc(1, sizeof(queue_block));
  501. new_block->len = 10;
  502. new_block->offset = 0;
  503. new_block->data = response;
  504. new_block->next = NULL;
  505. new_block->stream_id = stream_id;
  506. if(downstream_queue->first_block == NULL){
  507. downstream_queue->first_block = new_block;
  508. }
  509. else{
  510. queue_block *last = downstream_queue->first_block;
  511. while(last->next != NULL)
  512. last = last->next;
  513. last->next = new_block;
  514. }
  515. #endif
  516. //see if there were extra upstream bytes
  517. if(data_len > 0){
  518. printf("Data len is %d\n", data_len);
  519. printf("Upstream bytes: ");
  520. for(int i=0; i< data_len; i++){
  521. printf("%02x ", p[i]);
  522. }
  523. printf("\n");
  524. bytes_sent = send(handle, p,
  525. data_len, 0);
  526. if( bytes_sent <= 0){
  527. goto err;
  528. }
  529. }
  530. uint8_t *buffer = calloc(1, BUFSIZ);
  531. int32_t buffer_len = BUFSIZ;
  532. //now select on reading from the pipe and from the socket
  533. for(;;){
  534. fd_set readfds;
  535. fd_set writefds;
  536. int32_t nfds = (handle > thread_data->pipefd) ?
  537. handle +1 : thread_data->pipefd + 1;
  538. FD_ZERO(&readfds);
  539. FD_ZERO(&writefds);
  540. FD_SET(thread_data->pipefd, &readfds);
  541. FD_SET(handle, &readfds);
  542. FD_SET(handle, &writefds);
  543. if (select(nfds, &readfds, &writefds, NULL, NULL) < 0){
  544. printf("select error\n");
  545. break;
  546. }
  547. if(FD_ISSET(thread_data->pipefd, &readfds) && FD_ISSET(handle, &writefds)){
  548. //we have upstream data ready for writing
  549. int32_t bytes_read = read(thread_data->pipefd, buffer, buffer_len);
  550. if(bytes_read > 0){
  551. #ifdef DEBUG
  552. printf("PROXY (id %d): read %d bytes from pipe\n", stream_id, bytes_read);
  553. for(int i=0; i< bytes_read; i++){
  554. printf("%02x ", buffer[i]);
  555. }
  556. printf("\n");
  557. printf("%s\n", buffer);
  558. #endif
  559. bytes_sent = send(handle, buffer,
  560. bytes_read, 0);
  561. if( bytes_sent <= 0){
  562. break;
  563. } else if (bytes_sent < bytes_read){
  564. //TODO: should update buffer and keep
  565. //track of length of upstream data
  566. break;
  567. }
  568. } else {
  569. printf("PROXY (id %d): read %d bytes from pipe\n", stream_id, bytes_read);
  570. break;
  571. }
  572. }
  573. if (FD_ISSET(handle, &readfds)){
  574. //we have downstream data read for saving
  575. int32_t bytes_read;
  576. bytes_read = recv(handle, buffer, buffer_len, 0);
  577. if(bytes_read > 0){
  578. uint8_t *new_data = calloc(1, bytes_read);
  579. memcpy(new_data, buffer, bytes_read);
  580. #ifdef DEBUG
  581. printf("PROXY (id %d): read %d bytes from censored site\n",stream_id, bytes_read);
  582. for(int i=0; i< bytes_read; i++){
  583. printf("%02x ", buffer[i]);
  584. }
  585. printf("\n");
  586. #endif
  587. //make a new queue block
  588. queue_block *new_block = calloc(1, sizeof(queue_block));
  589. new_block->len = bytes_read;
  590. new_block->offset = 0;
  591. new_block->data = new_data;
  592. new_block->next = NULL;
  593. new_block->stream_id = stream_id;
  594. if(downstream_queue->first_block == NULL){
  595. downstream_queue->first_block = new_block;
  596. }
  597. else{
  598. queue_block *last = downstream_queue->first_block;
  599. while(last->next != NULL)
  600. last = last->next;
  601. last->next = new_block;
  602. }
  603. } else {
  604. printf("PROXY (id %d): read %d bytes from censored site\n",stream_id, bytes_read);
  605. break;
  606. }
  607. }
  608. }
  609. printf("Closing connection for stream %d\n", stream_id);
  610. //remove self from list
  611. stream *last = streams->first;
  612. stream *prev = last;
  613. if(streams->first != NULL){
  614. if(last->stream_id == stream_id){
  615. streams->first = last->next;
  616. printf("Freeing (2) %p\n", last);
  617. free(last);
  618. } else {
  619. while(last->next != NULL){
  620. prev = last;
  621. last = last->next;
  622. if(last->stream_id == stream_id){
  623. prev->next = last->next;
  624. printf("Freeing (2) %p\n", last);
  625. free(last);
  626. break;
  627. }
  628. }
  629. }
  630. }
  631. if(thread_data->initial_data != NULL){
  632. free(thread_data->initial_data);
  633. }
  634. free(thread_data);
  635. free(buffer);
  636. close(handle);
  637. pthread_detach(pthread_self());
  638. pthread_exit(NULL);
  639. return 0;
  640. err:
  641. //remove self from list
  642. last = streams->first;
  643. prev = last;
  644. if(streams->first != NULL){
  645. if(last->stream_id == stream_id){
  646. streams->first = last->next;
  647. free(last);
  648. } else {
  649. while(last->next != NULL){
  650. prev = last;
  651. last = last->next;
  652. if(last->stream_id == stream_id){
  653. prev->next = last->next;
  654. free(last);
  655. break;
  656. }
  657. }
  658. }
  659. }
  660. if(thread_data->initial_data != NULL){
  661. free(thread_data->initial_data);
  662. }
  663. free(thread_data);
  664. if(handle > 0){
  665. close(handle);
  666. }
  667. pthread_detach(pthread_self());
  668. pthread_exit(NULL);
  669. return 0;
  670. }
  671. /** Replaces downstream record contents with data from the
  672. * censored queue, padding with garbage bytes if no more
  673. * censored data exists.
  674. *
  675. * Inputs:
  676. * f: the tagged flow
  677. * data: a pointer to the received packet's application
  678. * data
  679. * data_len: the length of the packet's application data
  680. * offset: if the packet is misordered, the number of
  681. * application-level bytes in missing packets
  682. *
  683. * Output:
  684. * Returns 0 on sucess
  685. */
  686. int process_downstream(flow *f, int32_t offset, struct packet_info *info){
  687. uint8_t changed = 0;
  688. uint8_t *p = info->app_data;
  689. uint32_t remaining_packet_len = info->app_data_len;
  690. printf("Application data length: %d at %p\n", info->app_data_len, info->app_data);
  691. if(f->remaining_record_len > 0){
  692. //ignore bytes until the end of the record
  693. if(f->remaining_record_len > remaining_packet_len){ //ignore entire packet
  694. if(f->outbox_len > 0){
  695. changed = 1;
  696. memcpy(p, f->outbox + f->outbox_offset, remaining_packet_len);
  697. f->outbox_len -= remaining_packet_len;
  698. f->outbox_offset += remaining_packet_len;
  699. }
  700. f->remaining_record_len -= remaining_packet_len;
  701. remaining_packet_len -= remaining_packet_len;
  702. } else {
  703. if(f->outbox_len > 0){
  704. changed = 1;
  705. memcpy(p, f->outbox + f->outbox_offset, f->remaining_record_len);
  706. f->outbox_len = 0;
  707. f->outbox_offset=0;
  708. free(f->outbox);
  709. }
  710. p += f->remaining_record_len;
  711. remaining_packet_len -= f->remaining_record_len;
  712. f->remaining_record_len = 0;
  713. }
  714. }
  715. while(remaining_packet_len > 0){ //while bytes remain in the packet
  716. if(remaining_packet_len < RECORD_HEADER_LEN){
  717. #ifdef DEBUG
  718. printf("partial record header: \n");
  719. for(int i= 0; i< remaining_packet_len; i++){
  720. printf("%02x ", p[i]);
  721. }
  722. printf("\n");
  723. fflush(stdout);
  724. #endif
  725. f->partial_record_header = calloc(1, RECORD_HEADER_LEN);
  726. memcpy(f->partial_record_header, p, remaining_packet_len);
  727. f->partial_record_header_len = remaining_packet_len;
  728. remaining_packet_len -= remaining_packet_len;
  729. break;
  730. }
  731. struct record_header *record_hdr;
  732. if(f->partial_record_header_len > 0){
  733. memcpy(f->partial_record_header+ f->partial_record_header_len,
  734. p, RECORD_HEADER_LEN - f->partial_record_header_len);
  735. record_hdr = (struct record_header *) f->partial_record_header;
  736. } else {
  737. record_hdr = (struct record_header*) p;
  738. }
  739. uint32_t record_len = RECORD_LEN(record_hdr);
  740. #ifdef DEBUG
  741. fprintf(stdout,"Flow: %x > %x (%s)\n", info->ip_hdr->src.s_addr, info->ip_hdr->dst.s_addr, (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  742. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  743. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  744. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  745. fprintf(stdout, "Record:\n");
  746. for(int i=0; i< RECORD_HEADER_LEN; i++){
  747. printf("%02x ", ((uint8_t *) record_hdr)[i]);
  748. }
  749. printf("\n");
  750. #endif
  751. p += (RECORD_HEADER_LEN - f->partial_record_header_len);
  752. remaining_packet_len -= (RECORD_HEADER_LEN - f->partial_record_header_len);
  753. uint8_t *record_ptr = p; //points to the beginning of record data
  754. uint32_t remaining_record_len = record_len;
  755. if(record_len > remaining_packet_len){
  756. f->remaining_record_len = record_len - remaining_packet_len;
  757. if(f->httpstate == PARSE_HEADER || f->httpstate == BEGIN_CHUNK || f->httpstate == END_CHUNK){
  758. f->httpstate = FORFEIT_REST;
  759. } else if( f->httpstate == MID_CONTENT || f->httpstate == MID_CHUNK){
  760. f->remaining_response_len -= record_len - 24; //len of IV and padding
  761. if(f->remaining_response_len >= 0 && f->replace_response){
  762. //#ifdef nothing
  763. //make a huge record, encrypt it, and then place it in the outbox
  764. f->outbox = calloc(1, record_len+1);
  765. f->outbox_len = record_len;
  766. f->outbox_offset = 0;
  767. fill_with_downstream(f, f->outbox + EVP_GCM_TLS_EXPLICIT_IV_LEN , record_len - (EVP_GCM_TLS_EXPLICIT_IV_LEN+ 16)); //for now hard coded length of padding. TODO: fix this
  768. //encrypt
  769. int32_t n = encrypt(f, f->outbox, f->outbox,
  770. record_len - 16, 1,
  771. record_hdr->type, 1);
  772. if(n < 0){
  773. fprintf(stdout,"outbox encryption failed\n");
  774. } else {
  775. memcpy(p, f->outbox, remaining_packet_len);
  776. changed = 1;
  777. f->outbox_len -= remaining_packet_len;
  778. f->outbox_offset += remaining_packet_len;
  779. }
  780. //#endif
  781. }
  782. if(f->remaining_response_len == 0){
  783. if(f->httpstate == MID_CHUNK)
  784. f->httpstate = END_CHUNK;
  785. else {
  786. f->httpstate = PARSE_HEADER;
  787. }
  788. }
  789. if(f->remaining_response_len < 0){
  790. f->remaining_response_len = 0;
  791. f->httpstate = FORFEIT_REST;
  792. }
  793. }
  794. remaining_packet_len -= remaining_packet_len;
  795. if(f->partial_record_header_len > 0){
  796. f->partial_record_header_len = 0;
  797. free(f->partial_record_header);
  798. }
  799. break;
  800. }
  801. //now decrypt the record
  802. int32_t n = encrypt(f, record_ptr, record_ptr, record_len, 1,
  803. record_hdr->type, 0);
  804. if(n < 0){
  805. //do something smarter here
  806. if(f->partial_record_header_len > 0){
  807. f->partial_record_header_len = 0;
  808. free(f->partial_record_header);
  809. }
  810. return 0;
  811. }
  812. changed = 1;
  813. #ifdef DEBUG_DOWNSTREAM
  814. printf("Decryption succeeded\n");
  815. printf("Bytes:\n");
  816. for(int i=0; i< n; i++){
  817. printf("%02x ", record_ptr[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  818. }
  819. printf("\n");
  820. printf("Text:\n");
  821. printf("%s\n", record_ptr+EVP_GCM_TLS_EXPLICIT_IV_LEN);
  822. #endif
  823. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  824. char *len_ptr, *needle;
  825. remaining_record_len = n;
  826. while(remaining_record_len > 0){
  827. switch(f->httpstate){
  828. case PARSE_HEADER:
  829. //determine whether it's transfer encoded or otherwise
  830. //figure out what the content-type is
  831. len_ptr = strstr((const char *) p, "Content-Type: image");
  832. if(len_ptr != NULL){
  833. f->replace_response = 1;
  834. memcpy(len_ptr + 14, "slitheen", 8);
  835. char *c = len_ptr + 14+8;
  836. while(c[0] != '\r'){
  837. c[0] = ' ';
  838. c++;
  839. }
  840. } else {
  841. f->replace_response = 0;
  842. }
  843. len_ptr = strstr((const char *) p, "Transfer-Encoding");
  844. if(len_ptr != NULL){
  845. if(!memcmp(len_ptr + 19, "chunked", 7)){
  846. //now find end of header
  847. //printf("chunked encoding\n");
  848. len_ptr = strstr((const char *) p, "\r\n\r\n");
  849. if(len_ptr != NULL){
  850. f->httpstate = BEGIN_CHUNK;
  851. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  852. p = (uint8_t *) len_ptr + 4;
  853. }
  854. }
  855. } else {
  856. len_ptr = strstr((const char *) p, "Content-Length");
  857. if(len_ptr != NULL){
  858. len_ptr += 15;
  859. f->remaining_response_len = strtol((const char *) len_ptr, NULL, 10);
  860. //printf("content-length: %d\n", f->remaining_response_len);
  861. len_ptr = strstr((const char *) p, "\r\n\r\n");
  862. if(len_ptr != NULL){
  863. f->httpstate = MID_CONTENT;
  864. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  865. p = (uint8_t *) len_ptr + 4;
  866. } else {
  867. remaining_record_len = 0;
  868. //printf("Missing end of header. Sending to FORFEIT_REST\n");
  869. f->httpstate = FORFEIT_REST;
  870. }
  871. } else {
  872. //printf("No content length of transfer encoding field, sending to FORFEIT_REST\n");
  873. f->httpstate = FORFEIT_REST;
  874. remaining_record_len = 0;
  875. }
  876. }
  877. break;
  878. case MID_CONTENT:
  879. //check if content is replaceable
  880. if(f->remaining_response_len > remaining_record_len){
  881. if(f->replace_response){
  882. fill_with_downstream(f, p, remaining_record_len);
  883. #ifdef DEBUG
  884. printf("Replaced with:\n");
  885. for(int i=0; i< remaining_record_len; i++){
  886. printf("%02x ", p[i]);
  887. }
  888. printf("\n");
  889. #endif
  890. }
  891. f->remaining_response_len -= remaining_record_len;
  892. p += remaining_record_len;
  893. remaining_record_len = 0;
  894. } else {
  895. if(f->replace_response){
  896. fill_with_downstream(f, p, remaining_record_len);
  897. #ifdef DEBUG
  898. printf("Replaced with:\n");
  899. for(int i=0; i< remaining_record_len; i++){
  900. printf("%02x ", p[i]);
  901. }
  902. printf("\n");
  903. #endif
  904. }
  905. remaining_record_len -= f->remaining_response_len;
  906. p += f->remaining_response_len;
  907. f->httpstate = PARSE_HEADER;
  908. f->remaining_response_len = 0;
  909. }
  910. break;
  911. case BEGIN_CHUNK:
  912. {
  913. int32_t chunk_size = strtol((const char *) p, NULL, 16);
  914. if(chunk_size == 0){
  915. f->httpstate = END_BODY;
  916. } else {
  917. f->httpstate = MID_CHUNK;
  918. }
  919. f->remaining_response_len = chunk_size;
  920. needle = strstr((const char *) p, "\r\n");
  921. if(needle != NULL){
  922. remaining_record_len -= ((uint8_t *) needle - p + 2);
  923. p = (uint8_t *) needle + 2;
  924. } else {
  925. remaining_record_len = 0;
  926. printf("Couldn't find chunk, sending to FORFEIT_REST\n");
  927. f->httpstate = FORFEIT_REST;
  928. }
  929. }
  930. break;
  931. case MID_CHUNK:
  932. if(f->remaining_response_len > remaining_record_len){
  933. if(f->replace_response){
  934. fill_with_downstream(f, p, remaining_record_len);
  935. #ifdef DEBUG
  936. printf("Replaced with:\n");
  937. for(int i=0; i< remaining_record_len; i++){
  938. printf("%02x ", p[i]);
  939. }
  940. printf("\n");
  941. #endif
  942. }
  943. f->remaining_response_len -= remaining_record_len;
  944. p += remaining_record_len;
  945. remaining_record_len = 0;
  946. } else {
  947. if(f->replace_response){
  948. fill_with_downstream(f, p, f->remaining_response_len);
  949. #ifdef DEBUG
  950. printf("Replaced with:\n");
  951. for(int i=0; i< f->remaining_response_len; i++){
  952. printf("%02x ", p[i]);
  953. }
  954. printf("\n");
  955. #endif
  956. }
  957. remaining_record_len -= f->remaining_response_len;
  958. p += f->remaining_response_len;
  959. f->remaining_response_len = 0;
  960. f->httpstate = END_CHUNK;
  961. }
  962. break;
  963. case END_CHUNK:
  964. needle = strstr((const char *) p, "\r\n");
  965. if(needle != NULL){
  966. f->httpstate = BEGIN_CHUNK;
  967. p += 2;
  968. remaining_record_len -= 2;
  969. } else {
  970. remaining_record_len = 0;
  971. //printf("Couldn't find end of chunk, sending to FORFEIT_REST\n");
  972. f->httpstate = FORFEIT_REST;
  973. }
  974. break;
  975. case END_BODY:
  976. needle = strstr((const char *) p, "\r\n");
  977. if(needle != NULL){
  978. f->httpstate = PARSE_HEADER;
  979. p += 2;
  980. remaining_record_len -= 2;
  981. } else {
  982. remaining_record_len = 0;
  983. //printf("Couldn't find end of body, sending to FORFEIT_REST\n");
  984. f->httpstate = FORFEIT_REST;
  985. }
  986. break;
  987. case FORFEIT_REST:
  988. case USE_REST:
  989. remaining_record_len = 0;
  990. break;
  991. default:
  992. break;
  993. }
  994. }
  995. if((n = encrypt(f, record_ptr, record_ptr,
  996. n + EVP_GCM_TLS_EXPLICIT_IV_LEN, 1, record_hdr->type,
  997. 1)) < 0){
  998. if(f->partial_record_header_len > 0){
  999. f->partial_record_header_len = 0;
  1000. free(f->partial_record_header);
  1001. }
  1002. return 0;
  1003. }
  1004. p = record_ptr + record_len;
  1005. remaining_packet_len -= record_len;
  1006. if(f->partial_record_header_len > 0){
  1007. f->partial_record_header_len = 0;
  1008. free(f->partial_record_header);
  1009. }
  1010. }
  1011. if(changed){
  1012. tcp_checksum(info);
  1013. }
  1014. return 0;
  1015. }
  1016. /** Fills a given pointer with downstream data of the specified length. If no downstream data
  1017. * exists, pads it with garbage bytes. All downstream data is accompanied by a stream id and
  1018. * lengths of both the downstream data and garbage data
  1019. *
  1020. * Inputs:
  1021. * data: a pointer to where the downstream data should be entered
  1022. * length: The length of the downstream data required
  1023. *
  1024. */
  1025. int fill_with_downstream(flow *f, uint8_t *data, int32_t length){
  1026. uint8_t *p = data;
  1027. int32_t remaining = length;
  1028. struct slitheen_header *sl_hdr;
  1029. data_queue *downstream_queue = f->downstream_queue;
  1030. //Fill as much as we can from the censored_queue
  1031. while((remaining > SLITHEEN_HEADER_LEN) && downstream_queue->first_block != NULL){
  1032. queue_block *first_block = downstream_queue->first_block;
  1033. int32_t block_length = first_block->len;
  1034. int32_t offset = first_block->offset;
  1035. #ifdef DEBUG
  1036. printf("Censored queue is at %p.\n", first_block);
  1037. printf("This block has %d bytes left\n", block_length - offset);
  1038. printf("We need %d bytes\n", remaining - SLITHEEN_HEADER_LEN);
  1039. #endif
  1040. sl_hdr = (struct slitheen_header *) p;
  1041. sl_hdr->stream_id = first_block->stream_id;
  1042. sl_hdr->len = 0x00;
  1043. sl_hdr->garbage = 0x00;
  1044. p += SLITHEEN_HEADER_LEN;
  1045. remaining -= SLITHEEN_HEADER_LEN;
  1046. if(block_length > offset + remaining){
  1047. //use part of the block, update offset
  1048. memcpy(p, first_block->data+offset, remaining);
  1049. first_block->offset += remaining;
  1050. p += remaining;
  1051. sl_hdr->len = remaining;
  1052. remaining -= remaining;
  1053. } else {
  1054. //use all of the block and free it
  1055. memcpy(p, first_block->data+offset, block_length - offset);
  1056. free(first_block->data);
  1057. downstream_queue->first_block = first_block->next;
  1058. free(first_block);
  1059. p += (block_length - offset);
  1060. sl_hdr->len = (block_length - offset);
  1061. remaining -= (block_length - offset);
  1062. }
  1063. sl_hdr->len = htons(sl_hdr->len);
  1064. #ifdef DEBUG
  1065. printf("DWNSTRM: slitheen header: ");
  1066. for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
  1067. printf("%02x ",((uint8_t *) sl_hdr)[i]);
  1068. }
  1069. printf("\n");
  1070. printf("Sending %d downstream bytes:", ntohs(sl_hdr->len));
  1071. for(int i=0; i< ntohs(sl_hdr->len); i++){
  1072. printf("%02x ", ((uint8_t *) sl_hdr)[i+SLITHEEN_HEADER_LEN]);
  1073. }
  1074. printf("\n");
  1075. #endif
  1076. }
  1077. //now, if we need more data, fill with garbage
  1078. if(remaining > SLITHEEN_HEADER_LEN ){
  1079. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  1080. sl_hdr = (struct slitheen_header *) p;
  1081. sl_hdr->stream_id = 0x00;
  1082. remaining -= SLITHEEN_HEADER_LEN;
  1083. sl_hdr->len = htons(remaining);
  1084. sl_hdr->garbage = htons(remaining);
  1085. //#ifdef DEBUG
  1086. printf("DWNSTRM: slitheen header: ");
  1087. for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
  1088. printf("%02x ", p[i]);
  1089. }
  1090. printf("\n");
  1091. //#endif
  1092. p += SLITHEEN_HEADER_LEN;
  1093. memset(p, 'A', remaining);
  1094. }
  1095. return 0;
  1096. }
  1097. /** Computes the TCP checksum of the data according to RFC 793
  1098. * sum all 16-bit words in the segment, pad the last word if
  1099. * needed
  1100. *
  1101. * there is a pseudo-header prefixed to the segment and
  1102. * included in the checksum:
  1103. *
  1104. * +--------+--------+--------+--------+
  1105. * | Source Address |
  1106. * +--------+--------+--------+--------+
  1107. * | Destination Address |
  1108. * +--------+--------+--------+--------+
  1109. * | zero | PTCL | TCP Length |
  1110. * +--------+--------+--------+--------+
  1111. */
  1112. uint16_t tcp_checksum(struct packet_info *info){
  1113. uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
  1114. struct in_addr src = info->ip_hdr->src;
  1115. struct in_addr dst = info->ip_hdr->dst;
  1116. uint8_t proto = IPPROTO_TCP;
  1117. //set the checksum to zero
  1118. info->tcp_hdr->chksum = 0;
  1119. //sum pseudoheader
  1120. uint32_t sum = (ntohl(src.s_addr)) >> 16;
  1121. sum += (ntohl(src.s_addr)) &0xFFFF;
  1122. sum += (ntohl(dst.s_addr)) >> 16;
  1123. sum += (ntohl(dst.s_addr)) & 0xFFFF;
  1124. sum += proto;
  1125. sum += tcp_length;
  1126. //sum tcp header (with zero-d checksum)
  1127. uint8_t *p = (uint8_t *) info->tcp_hdr;
  1128. for(int i=0; i < info->size_tcp_hdr; i+=2){
  1129. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  1130. }
  1131. //now sum the application data
  1132. p = info->app_data;
  1133. for(int i=0; i< info->app_data_len-1; i+=2){
  1134. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  1135. }
  1136. if(info->app_data_len %2 != 0){
  1137. sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
  1138. }
  1139. //now add most significant to last significant bits
  1140. sum = (sum >> 16) + (sum & 0xFFFF);
  1141. sum += sum >>16;
  1142. //now subtract from 0xFF
  1143. sum = 0xFFFF - sum;
  1144. //set chksum to calculated value
  1145. info->tcp_hdr->chksum = ntohs(sum);
  1146. return (uint16_t) sum;
  1147. }