slitheen.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /* Name: slitheen.c
  2. *
  3. * Slitheen - a decoy routing system for censorship resistance
  4. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 3.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Additional permission under GNU GPL version 3 section 7
  19. *
  20. * If you modify this Program, or any covered work, by linking or combining
  21. * it with the OpenSSL library (or a modified version of that library),
  22. * containing parts covered by the terms of the OpenSSL Licence and the
  23. * SSLeay license, the licensors of this Program grant you additional
  24. * permission to convey the resulting work. Corresponding Source for a
  25. * non-source form of such a combination shall include the source code
  26. * for the parts of the OpenSSL library used as well as that of the covered
  27. * work.
  28. */
  29. #include <pcap.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <string.h>
  34. #include <pthread.h>
  35. #include <sys/ioctl.h>
  36. #include <net/if.h>
  37. #include <openssl/ssl.h>
  38. #include "util.h"
  39. #include "flow.h"
  40. #include "relay.h"
  41. #include "crypto.h"
  42. #include "cryptothread.h"
  43. #include "packet.h"
  44. struct sniff_args {
  45. char *readdev;
  46. char *writedev;
  47. };
  48. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet);
  49. void *sniff_packets(void *);
  50. void process_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet);
  51. struct packet_info *copy_packet_info(struct packet_info *src_info);
  52. void save_packet(flow *f, struct packet_info *info);
  53. void update_window_expiration(flow *f, struct packet_info *info);
  54. void retransmit(flow *f, struct packet_info *info, uint32_t data_to_fill);
  55. void usage(void){
  56. printf("Usage: slitheen [internal network interface] [NAT interface]\n");
  57. }
  58. int main(int argc, char *argv[]){
  59. pthread_t t1, t2;
  60. char *dev1 = NULL; /* Device that leads to the internal network */
  61. char *dev2 = NULL; /* Device that leads out to the world */
  62. struct sniff_args outbound;
  63. struct sniff_args inbound;
  64. if (argc != 3) {
  65. usage();
  66. return(2);
  67. }
  68. dev1 = argv[1];
  69. dev2 = argv[2];
  70. if(init_tables()){
  71. exit(1);
  72. }
  73. if(init_session_cache()){
  74. exit(1);
  75. }
  76. init_crypto_locks();
  77. /* Create threads */
  78. outbound.readdev = dev1;
  79. outbound.writedev = dev2;
  80. inbound.readdev = dev2;
  81. inbound.writedev = dev1;
  82. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  83. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  84. pthread_join(t1, NULL);
  85. pthread_join(t2, NULL);
  86. pthread_exit(NULL);
  87. crypto_locks_cleanup();
  88. return(0);
  89. }
  90. void *sniff_packets(void *args){
  91. pcap_t *rd_handle;
  92. pcap_t *wr_handle;
  93. char rd_errbuf[BUFSIZ];
  94. char wr_errbuf[BUFSIZ];
  95. uint8_t mac[ETHER_ADDR_LEN];
  96. bpf_u_int32 mask;
  97. bpf_u_int32 net;
  98. char *readdev, *writedev;
  99. struct sniff_args *arg_st = (struct sniff_args *) args;
  100. readdev = arg_st->readdev;
  101. writedev = arg_st->writedev;
  102. //Find MAC address of each interface
  103. struct ifreq ifr;
  104. int s = socket(AF_INET, SOCK_DGRAM, 0);
  105. strcpy(ifr.ifr_name, writedev);
  106. ioctl(s, SIOCGIFHWADDR, &ifr);
  107. memcpy(mac, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  108. close(s);
  109. if (pcap_lookupnet(readdev, &net, &mask, rd_errbuf) == -1){
  110. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  111. exit(2);
  112. }
  113. rd_handle = pcap_open_live(readdev, BUFSIZ, 0, 0, rd_errbuf);
  114. if (rd_handle == NULL){
  115. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, rd_errbuf);
  116. }
  117. if(pcap_datalink(rd_handle) != DLT_EN10MB) {
  118. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  119. exit(2);
  120. }
  121. if(pcap_setdirection(rd_handle, PCAP_D_IN)){
  122. fprintf(stderr, "Platform does not support write direction. Update filters with MAC address\n");
  123. exit(2);
  124. }
  125. wr_handle = pcap_open_live(writedev, BUFSIZ, 0, 0, wr_errbuf);
  126. if (wr_handle == NULL){
  127. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, wr_errbuf);
  128. }
  129. struct inject_args iargs;
  130. iargs.mac_addr = mac;
  131. iargs.write_dev = wr_handle;
  132. /*callback function*/
  133. pcap_loop(rd_handle, -1, got_packet, (unsigned char *) &iargs);
  134. /*Sniff a packet*/
  135. pcap_close(rd_handle);
  136. return NULL;
  137. }
  138. /**
  139. * Runs when pcap_loop receives a packet from the specified interface
  140. * If the received packet is a tcp packet, processes it and then writes it back out
  141. * to the interface
  142. *
  143. */
  144. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet){
  145. struct inject_args *iargs = (struct inject_args *) args;
  146. uint8_t *tmp_packet = smalloc(header->len);
  147. memcpy(tmp_packet, packet, header->len);
  148. process_packet(iargs, header, tmp_packet);
  149. }
  150. /* This function receives a full ip packet and then:
  151. * 1) identifies the flow
  152. * 2) adds the packet to the flow's data chain
  153. * 3) updates the flow's state
  154. */
  155. void process_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet){
  156. struct packet_info *info = smalloc(sizeof(struct packet_info));
  157. extract_packet_headers(packet, info);
  158. //Ignore non-TCP packets (shouldn't actually get any)
  159. if((info->ip_hdr == NULL) || (info->tcp_hdr == NULL)){
  160. //free(info);
  161. //free(packet);
  162. //return;
  163. goto err;
  164. }
  165. /* Checks to see if this is a possibly tagged hello msg */
  166. if ((info->record_hdr != NULL) && (info->record_hdr->type == HS)){ /* This is a TLS handshake */
  167. check_handshake(info);
  168. }
  169. /* Now if flow is in table, update state */
  170. flow *observed;
  171. if((observed = check_flow(info)) != NULL){
  172. /*Check sequence number and replay application data if necessary*/
  173. DEBUG_MSG(DEBUG_FLOW, "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 != observed->src_ip.s_addr)? "incoming":"outgoing");
  174. DEBUG_MSG(DEBUG_FLOW, "ID number: %u\n", htonl(info->ip_hdr->id));
  175. DEBUG_MSG(DEBUG_FLOW, "Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  176. DEBUG_MSG(DEBUG_FLOW, "Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  177. uint8_t incoming = (info->ip_hdr->src.s_addr != observed->src_ip.s_addr)? 1 : 0;
  178. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  179. uint32_t expected_seq = (incoming)? observed->downstream_seq_num : observed->upstream_seq_num;
  180. DEBUG_MSG(DEBUG_FLOW, "Expected sequence number: %u\n", expected_seq);
  181. /* Remove acknowledged data from queue after TCP window is exceeded */
  182. update_window_expiration(observed, info);
  183. /* fill with retransmit data, process new data */
  184. uint32_t data_to_fill;
  185. uint32_t data_to_process;
  186. if(seq_num > expected_seq){
  187. data_to_process = info->app_data_len;
  188. data_to_fill = 0;
  189. } else if (seq_num + info->app_data_len > expected_seq){
  190. data_to_fill = expected_seq - seq_num;
  191. data_to_process = seq_num + info->app_data_len - expected_seq;
  192. } else {
  193. data_to_fill = info->app_data_len;
  194. data_to_process = 0;
  195. }
  196. uint8_t *p = info->app_data;
  197. if(data_to_fill){ //retransmit
  198. printf("Retransmiting data (%u:%u)\n", seq_num, seq_num + info->app_data_len);
  199. retransmit(observed, info, data_to_fill);
  200. }
  201. p += data_to_fill;
  202. if(data_to_process){
  203. if(p != info->app_data){
  204. printf("UH OH something weird might happen\n");
  205. }
  206. if(observed->application){
  207. if(seq_num > expected_seq){
  208. /*For now, enters into FORFEIT state
  209. fprintf(stderr,"ERROR: future packet in app data, forfeiting flow\n");
  210. fflush(stderr);
  211. remove_flow(observed);
  212. goto err;*/
  213. //Delay and process later
  214. frame *new_frame = scalloc(1, sizeof(frame));
  215. new_frame->iargs = iargs;
  216. new_frame->packet = packet;
  217. new_frame->header = header;
  218. new_frame->seq_num = seq_num;
  219. new_frame->next = NULL;
  220. frame_queue *queue = (incoming) ? observed->ds_frame_queue : observed->us_frame_queue;
  221. printf("Delay processing of frame (seq = %u )\n", seq_num);
  222. //add to end of list
  223. if(queue->first_frame == NULL){
  224. queue->first_frame = new_frame;
  225. } else {
  226. frame *last = queue->first_frame;
  227. while(last->next != NULL){
  228. last = last->next;
  229. }
  230. last->next = new_frame;
  231. }
  232. free(info);
  233. observed->ref_ctr--;
  234. printf("Misordered packet. %p ref_ctr %d\n", observed, observed->ref_ctr);
  235. return; //TODO: fix terrible spaghetti returns
  236. }
  237. replace_packet(observed, info);
  238. } else {
  239. //We're still in the TLS handshake; hold packets misordered packets
  240. if(seq_num > expected_seq){
  241. //Delay and process later
  242. frame *new_frame = scalloc(1, sizeof(frame));
  243. new_frame->iargs = iargs;
  244. new_frame->packet = packet;
  245. new_frame->header = header;
  246. new_frame->seq_num = seq_num;
  247. new_frame->next = NULL;
  248. frame_queue *queue = (incoming) ? observed->ds_frame_queue : observed->us_frame_queue;
  249. printf("Delay processing of frame (seq = %u )\n", seq_num);
  250. //add to end of list
  251. if(queue->first_frame == NULL){
  252. queue->first_frame = new_frame;
  253. } else {
  254. frame *last = queue->first_frame;
  255. while(last->next != NULL){
  256. last = last->next;
  257. }
  258. last->next = new_frame;
  259. }
  260. free(info);
  261. observed->ref_ctr--;
  262. printf("Misordered packet. %p ref_ctr %d\n", observed, observed->ref_ctr);
  263. return; //TODO: fix terrible spaghetti returns
  264. }
  265. /* Pass data to packet chain */
  266. if(observed->stall){
  267. }
  268. if(add_packet(observed, info)){//removed_flow
  269. goto err;
  270. }
  271. }
  272. /* Update TCP state */
  273. if(info->tcp_hdr->flags & (FIN | RST) ){
  274. /* Remove flow from table, connection ended */
  275. remove_flow(observed);
  276. goto err;
  277. }
  278. /* add packet to application data queue */
  279. save_packet(observed, info);
  280. }
  281. /*process and release held frames with current sequence numbers*/
  282. frame_queue *queue = (incoming) ? observed->ds_frame_queue : observed->us_frame_queue;
  283. frame *first = queue->first_frame;
  284. frame *prev = queue->first_frame;
  285. expected_seq = (incoming)? observed->downstream_seq_num : observed->upstream_seq_num;
  286. while (first != NULL){
  287. if(first->seq_num <= expected_seq){
  288. //remove from queue and process
  289. if(first == queue->first_frame) {
  290. queue->first_frame = first->next;
  291. } else {
  292. prev->next = first->next;
  293. }
  294. printf("Now processing frame (seq = %u )\n", first->seq_num);
  295. process_packet(iargs, first->header, first->packet);
  296. free(first);
  297. first = queue->first_frame;
  298. prev = queue->first_frame;
  299. } else {
  300. prev = first;
  301. first = first->next;
  302. }
  303. }
  304. observed->ref_ctr--;
  305. }
  306. //TODO: figure out how to not need this
  307. tcp_checksum(info);//update checksum
  308. err:
  309. free(info);//Note: don't free this while a thread is using it
  310. inject_packet(iargs, header, packet);
  311. return;
  312. }
  313. //TODO: rewrite this function to remove bloat
  314. void save_packet(flow *f, struct packet_info *info){
  315. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  316. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  317. //add new app block
  318. packet *new_block = scalloc(1, sizeof(packet));
  319. new_block->seq_num = htonl(info->tcp_hdr->sequence_num);
  320. new_block->data = scalloc(1, info->app_data_len);
  321. memcpy(new_block->data, info->app_data, info->app_data_len);
  322. new_block->len = info->app_data_len;
  323. new_block->next = NULL;
  324. new_block->expiration = 0;
  325. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  326. f->upstream_app_data->first_packet;
  327. //put app data block in queue
  328. if(saved_data == NULL){
  329. if(incoming){
  330. f->downstream_app_data->first_packet = new_block;
  331. if(new_block->seq_num ==
  332. f->downstream_seq_num){
  333. f->downstream_seq_num += new_block->len;
  334. DEBUG_MSG(DEBUG_FLOW, "Updated downstream expected seqnum to %u\n",
  335. f->downstream_seq_num );
  336. }
  337. } else {
  338. f->upstream_app_data->first_packet = new_block;
  339. if(new_block->seq_num ==
  340. f->upstream_seq_num){
  341. f->upstream_seq_num += new_block->len;
  342. DEBUG_MSG(DEBUG_FLOW, "Updated upstream expected seqnum to %u\n",
  343. f->upstream_seq_num );
  344. }
  345. }
  346. } else {
  347. uint8_t saved = 0;
  348. while(saved_data->next != NULL){
  349. if(!saved && (saved_data->next->seq_num > seq_num)){
  350. new_block->next = saved_data->next;
  351. saved_data->next = new_block;
  352. saved = 1;
  353. }
  354. //update expected sequence number
  355. if(incoming){
  356. if(saved_data->next->seq_num ==
  357. f->downstream_seq_num){
  358. f->downstream_seq_num += saved_data->next->len;
  359. DEBUG_MSG(DEBUG_FLOW, "Updated downstream expected seqnum to %u\n",
  360. f->downstream_seq_num );
  361. }
  362. } else {//outgoing
  363. if(saved_data->next->seq_num ==
  364. f->upstream_seq_num){
  365. f->upstream_seq_num += saved_data->next->len;
  366. DEBUG_MSG(DEBUG_FLOW, "Updated upstream expected seqnum to %u\n",
  367. f->upstream_seq_num );
  368. }
  369. }
  370. saved_data = saved_data->next;
  371. }
  372. if(!saved){
  373. saved_data->next = new_block;
  374. //update expected sequence number
  375. if(incoming){
  376. if(saved_data->next->seq_num ==
  377. f->downstream_seq_num){
  378. f->downstream_seq_num += saved_data->next->len;
  379. DEBUG_MSG(DEBUG_FLOW, "Updated downstream expected seqnum to %u\n",
  380. f->downstream_seq_num );
  381. }
  382. } else {//outgoing
  383. if(saved_data->next->seq_num ==
  384. f->upstream_seq_num){
  385. f->upstream_seq_num += saved_data->next->len;
  386. DEBUG_MSG(DEBUG_FLOW, "Updated upstream expected seqnum to %u\n",
  387. f->upstream_seq_num );
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * This function cleans up data that has been acked, after the TCP window of the recipient has been
  395. * exceeded. This ensures that a retransmisson of the data will no longer occur.
  396. *
  397. * Sets the expiration for recent data base on the TCP window
  398. */
  399. void update_window_expiration(flow *f, struct packet_info *info){
  400. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  401. uint32_t ack_num = htonl(info->tcp_hdr->ack_num);
  402. uint32_t end_seq = htonl(info->tcp_hdr->sequence_num) + info->app_data_len - 1;
  403. uint32_t window = ack_num + htons(info->tcp_hdr->win_size);
  404. DEBUG_MSG(DEBUG_FLOW, "Received sequence number %u\n", htonl(info->tcp_hdr->sequence_num));
  405. DEBUG_MSG(DEBUG_FLOW, "Acknowledged up to %u with window expiring at %u\n", ack_num, window);
  406. DEBUG_MSG(DEBUG_FLOW, "Removing all packets up to %u\n", end_seq);
  407. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  408. f->upstream_app_data->first_packet;
  409. while((saved_data != NULL) && (saved_data->expiration != 0) && (end_seq > saved_data->expiration)){
  410. //remove entire block
  411. if(incoming){
  412. f->downstream_app_data->first_packet = saved_data->next;
  413. } else {
  414. f->upstream_app_data->first_packet = saved_data->next;
  415. }
  416. free(saved_data->data);
  417. free(saved_data);
  418. saved_data = (incoming)? f->downstream_app_data->first_packet :
  419. f->upstream_app_data->first_packet;
  420. if(saved_data != NULL){
  421. DEBUG_MSG(DEBUG_FLOW, "Currently saved seq_num is now %u\n", saved_data->seq_num);
  422. } else {
  423. DEBUG_MSG(DEBUG_FLOW, "Acked all data, queue is empty\n");
  424. }
  425. }
  426. /* Update expiration for packets based on TCP window size */
  427. saved_data = (incoming)? f->upstream_app_data->first_packet :
  428. f->downstream_app_data->first_packet;
  429. while((saved_data != NULL) && (ack_num > saved_data->seq_num)){
  430. //update window
  431. if(ack_num >= saved_data->seq_num + saved_data->len){
  432. //remove entire block
  433. saved_data->expiration = window;
  434. }
  435. saved_data = saved_data->next;
  436. }
  437. }
  438. /**
  439. * This function retransmits previously sent (and possibly modified) data
  440. *
  441. */
  442. void retransmit(flow *f, struct packet_info *info, uint32_t data_to_fill){
  443. uint8_t *p = info->app_data;
  444. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  445. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  446. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  447. f->upstream_app_data->first_packet;
  448. while(data_to_fill > 0){
  449. if(saved_data == NULL){
  450. //have already acked all data
  451. p += data_to_fill;
  452. seq_num += data_to_fill;
  453. data_to_fill -= data_to_fill;
  454. continue;
  455. }
  456. if(seq_num < saved_data->seq_num){
  457. //we are missing a block. Use what was given
  458. if(saved_data->seq_num - seq_num > data_to_fill){
  459. //skip the rest
  460. p += data_to_fill;
  461. seq_num += data_to_fill;
  462. data_to_fill -= data_to_fill;
  463. } else {
  464. p += saved_data->seq_num - seq_num;
  465. data_to_fill -= saved_data->seq_num - seq_num;
  466. seq_num += saved_data->seq_num - seq_num;
  467. }
  468. } else if ( seq_num == saved_data->seq_num) {
  469. if(data_to_fill >= saved_data->len){
  470. //exhaust this block and move onto next one
  471. memcpy(p, saved_data->data, saved_data->len);
  472. p += saved_data->len;
  473. seq_num += saved_data->len;
  474. data_to_fill -= saved_data->len;
  475. saved_data = saved_data->next;
  476. } else {
  477. //fill with partial block
  478. memcpy(p, saved_data->data, data_to_fill);
  479. p += data_to_fill;
  480. seq_num += data_to_fill;
  481. data_to_fill -= data_to_fill;
  482. }
  483. } else { //seq_num > saved_data->seq_num
  484. uint32_t offset = seq_num - saved_data->seq_num;
  485. if(offset > saved_data->len){
  486. saved_data = saved_data->next;
  487. offset -= saved_data->len;
  488. } else {
  489. if(data_to_fill > saved_data->len - offset){
  490. memcpy(p, saved_data->data + offset, saved_data->len - offset);
  491. p += saved_data->len - offset;
  492. seq_num += saved_data->len - offset;
  493. data_to_fill -= saved_data->len - offset;
  494. saved_data = saved_data->next;
  495. } else {
  496. memcpy(p, saved_data->data + offset, data_to_fill);
  497. p += data_to_fill;
  498. seq_num += data_to_fill;
  499. data_to_fill -= data_to_fill;
  500. }
  501. }
  502. }
  503. }
  504. tcp_checksum(info);//update checksum
  505. }