relay.c 36 KB

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