relay.c 36 KB

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