relay.c 35 KB

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