slitheen-proxy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* Name: slitheen-proxy.c
  2. * Author: Cecylia Bocovich
  3. *
  4. * This code runs the main functions for the Slitheen relay station to tap and
  5. * modify data.
  6. *
  7. */
  8. #include <pcap.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <pthread.h>
  14. #include <openssl/ssl.h>
  15. #include "util.h"
  16. #include "flow.h"
  17. #include "slitheen.h"
  18. #include "relay.h"
  19. #include "crypto.h"
  20. #include "cryptothread.h"
  21. void save_packet(flow *f, struct packet_info *info);
  22. void update_window_expiration(flow *f, struct packet_info *info);
  23. void retransmit(flow *f, struct packet_info *info, uint32_t data_to_fill);
  24. void usage(void){
  25. printf("Usage: slitheen-proxy [internal network interface] [NAT interface]\n");
  26. }
  27. int main(int argc, char *argv[]){
  28. pthread_t t1, t2;
  29. char *dev1 = NULL; /* Device that leads to the internal network */
  30. char *dev2 = NULL; /* Device that leads out to the world */
  31. struct sniff_args outbound;
  32. struct sniff_args inbound;
  33. if (argc != 3) {
  34. usage();
  35. return(2);
  36. }
  37. dev1 = argv[1];
  38. dev2 = argv[2];
  39. if(init_tables()){
  40. exit(1);
  41. }
  42. if(init_session_cache()){
  43. exit(1);
  44. }
  45. init_crypto_locks();
  46. /* Create threads */
  47. outbound.readdev = dev1;
  48. outbound.writedev = dev2;
  49. inbound.readdev = dev2;
  50. inbound.writedev = dev1;
  51. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  52. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  53. pthread_join(t1, NULL);
  54. pthread_join(t2, NULL);
  55. pthread_exit(NULL);
  56. crypto_locks_cleanup();
  57. return(0);
  58. }
  59. void *sniff_packets(void *args){
  60. pcap_t *rd_handle;
  61. pcap_t *wr_handle;
  62. char rd_errbuf[BUFSIZ];
  63. char wr_errbuf[BUFSIZ];
  64. bpf_u_int32 mask;
  65. bpf_u_int32 net;
  66. char *readdev, *writedev;
  67. struct sniff_args *arg_st = (struct sniff_args *) args;
  68. readdev = arg_st->readdev;
  69. writedev = arg_st->writedev;
  70. if (pcap_lookupnet(readdev, &net, &mask, rd_errbuf) == -1){
  71. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  72. exit(2);
  73. }
  74. rd_handle = pcap_open_live(readdev, BUFSIZ, 1, 0, rd_errbuf);
  75. if (rd_handle == NULL){
  76. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, rd_errbuf);
  77. }
  78. if(pcap_datalink(rd_handle) != DLT_EN10MB) {
  79. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  80. exit(2);
  81. }
  82. if(pcap_setdirection(rd_handle, PCAP_D_IN)){
  83. fprintf(stderr, "Platform does not support write direction. Update filters with MAC address\n");
  84. exit(2);
  85. }
  86. wr_handle = pcap_open_live(writedev, BUFSIZ, 1, 0, wr_errbuf);
  87. if (wr_handle == NULL){
  88. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, wr_errbuf);
  89. }
  90. /*callback function*/
  91. pcap_loop(rd_handle, -1, got_packet, (unsigned char *) wr_handle);
  92. /*Sniff a packet*/
  93. pcap_close(rd_handle);
  94. return NULL;
  95. }
  96. /**
  97. * Runs when pcap_loop receives a packet from the specified interface
  98. * If the received packet is a tcp packet, processes it and then writes it back out
  99. * to the interface
  100. *
  101. */
  102. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet){
  103. pcap_t *handle = (pcap_t *) args;
  104. struct packet_info *info = emalloc(sizeof(struct packet_info));
  105. uint8_t *tmp_packet = emalloc(header->len);
  106. //printf("Allocated %d bytes to %p\n", header->len, tmp_packet);
  107. memcpy(tmp_packet, packet, header->len);
  108. extract_packet_headers(tmp_packet, info);
  109. // Check to make sure it is a TCP packet
  110. if((info->ip_hdr == NULL) || (info->tcp_hdr == NULL))
  111. goto end;
  112. process_packet(info);
  113. end:
  114. if((pcap_inject(handle, tmp_packet, header->len)) < 0 ){
  115. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  116. }
  117. #ifdef DEBUG
  118. fprintf(stderr, "injected the following packet:\n");
  119. for(int i=0; i< header->len; i++){
  120. fprintf(stderr, "%02x ", tmp_packet[i]);
  121. }
  122. fprintf(stderr, "\n");
  123. if((info->tcp_hdr != NULL) && (info->ip_hdr != NULL)){
  124. fprintf(stdout,"Injected packet: %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));
  125. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  126. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  127. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  128. fflush(stdout);
  129. }
  130. #endif
  131. free(info);//Note: don't free this while a thread is using it
  132. free(tmp_packet);
  133. }
  134. /* This function receives a full ip packet and then:
  135. * 1) identifies the flow
  136. * 2) adds the packet to the flow's data chain
  137. * 3) updates the flow's state
  138. */
  139. void process_packet(struct packet_info *info){
  140. /* Checks to see if this is a possibly tagged hello msg */
  141. if ((info->record_hdr != NULL) && (info->record_hdr->type == HS)){ /* This is a TLS handshake */
  142. check_handshake(info);
  143. }
  144. /* Now if flow is in table, update state */
  145. flow *observed;
  146. if((observed = check_flow(info)) != NULL){
  147. //#ifdef DEBUG
  148. /*Check sequence number and replay application data if necessary*/
  149. 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 != observed->src_ip.s_addr)? "incoming":"outgoing");
  150. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  151. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  152. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  153. //#endif
  154. uint8_t incoming = (info->ip_hdr->src.s_addr != observed->src_ip.s_addr)? 1 : 0;
  155. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  156. uint32_t expected_seq = (incoming)? observed->downstream_seq_num : observed->upstream_seq_num;
  157. //#ifdef DEBUG
  158. fprintf(stdout,"Expected sequence number: %u\n", expected_seq);
  159. //#endif
  160. /* Remove acknowledged data from queue after TCP window is exceeded */
  161. update_window_expiration(observed, info);
  162. /* fill with retransmit data, process new data */
  163. uint32_t data_to_fill;
  164. uint32_t data_to_process;
  165. if(seq_num > expected_seq){
  166. data_to_process = info->app_data_len;
  167. data_to_fill = 0;
  168. } else if (seq_num + info->app_data_len > expected_seq){
  169. data_to_fill = expected_seq - seq_num;
  170. data_to_process = seq_num + info->app_data_len - expected_seq;
  171. } else {
  172. data_to_fill = info->app_data_len;
  173. data_to_process = 0;
  174. }
  175. uint8_t *p = info->app_data;
  176. if(data_to_fill){ //retransmit
  177. retransmit(observed, info, data_to_fill);
  178. }
  179. p += data_to_fill;
  180. if(data_to_process){
  181. if(p != info->app_data){
  182. printf("UH OH something weird might happen\n");
  183. }
  184. if(observed->application){
  185. if(seq_num > expected_seq){
  186. //For now, enters into FORFEIT state
  187. //TODO: change upstream behaviour to try to mask slitheen hdr
  188. printf("ERROR: future packet in app data, forfeiting flow\n");
  189. remove_flow(observed);
  190. return;
  191. }
  192. replace_packet(observed, info);
  193. } else {
  194. /* Pass data to packet chain */
  195. if(observed->stall){
  196. }
  197. if(add_packet(observed, info)){//removed_flow
  198. return;
  199. }
  200. }
  201. /* Update TCP state */
  202. if(info->tcp_hdr->flags & (FIN | RST) ){
  203. /* Remove flow from table, connection ended */
  204. remove_flow(observed);
  205. return;
  206. }
  207. /* add packet to application data queue */
  208. save_packet(observed, info);
  209. }
  210. observed->ref_ctr--;
  211. }
  212. }
  213. void save_packet(flow *f, struct packet_info *info){
  214. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  215. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  216. //add new app block
  217. packet *new_block = ecalloc(1, sizeof(packet));
  218. new_block->seq_num = htonl(info->tcp_hdr->sequence_num);
  219. new_block->data = ecalloc(1, info->app_data_len);
  220. memcpy(new_block->data, info->app_data, info->app_data_len);
  221. new_block->len = info->app_data_len;
  222. new_block->next = NULL;
  223. new_block->expiration = 0;
  224. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  225. f->upstream_app_data->first_packet;
  226. //put app data block in queue
  227. if(saved_data == NULL){
  228. if(incoming){
  229. f->downstream_app_data->first_packet = new_block;
  230. if(new_block->seq_num ==
  231. f->downstream_seq_num){
  232. f->downstream_seq_num += new_block->len;
  233. #ifdef DEBUG
  234. printf("Updated downstream expected seqnum to %u\n",
  235. f->downstream_seq_num );
  236. #endif
  237. }
  238. } else {
  239. f->upstream_app_data->first_packet = new_block;
  240. if(new_block->seq_num ==
  241. f->upstream_seq_num){
  242. f->upstream_seq_num += new_block->len;
  243. #ifdef DEBUG
  244. printf("Updated upstream expected seqnum to %u\n",
  245. f->upstream_seq_num );
  246. #endif
  247. }
  248. }
  249. } else {
  250. uint8_t saved = 0;
  251. while(saved_data->next != NULL){
  252. if(!saved && (saved_data->next->seq_num > seq_num)){
  253. new_block->next = saved_data->next;
  254. saved_data->next = new_block;
  255. saved = 1;
  256. }
  257. //update expected sequence number
  258. if(incoming){
  259. if(saved_data->next->seq_num ==
  260. f->downstream_seq_num){
  261. f->downstream_seq_num += saved_data->next->len;
  262. #ifdef DEBUG
  263. printf("Updated downstream expected seqnum to %u\n",
  264. f->downstream_seq_num );
  265. #endif
  266. }
  267. } else {//outgoing
  268. if(saved_data->next->seq_num ==
  269. f->upstream_seq_num){
  270. f->upstream_seq_num += saved_data->next->len;
  271. #ifdef DEBUG
  272. printf("Updated upstream expected seqnum to %u\n",
  273. f->upstream_seq_num );
  274. #endif
  275. }
  276. }
  277. saved_data = saved_data->next;
  278. }
  279. if(!saved){
  280. saved_data->next = new_block;
  281. //update expected sequence number
  282. if(incoming){
  283. if(saved_data->next->seq_num ==
  284. f->downstream_seq_num){
  285. f->downstream_seq_num += saved_data->next->len;
  286. #ifdef DEBUG
  287. printf("Updated downstream expected seqnum to %u\n",
  288. f->downstream_seq_num );
  289. #endif
  290. }
  291. } else {//outgoing
  292. if(saved_data->next->seq_num ==
  293. f->upstream_seq_num){
  294. f->upstream_seq_num += saved_data->next->len;
  295. #ifdef DEBUG
  296. printf("Updated upstream expected seqnum to %u\n",
  297. f->upstream_seq_num );
  298. #endif
  299. }
  300. }
  301. }
  302. }
  303. }
  304. /**
  305. * This function cleans up data that has been acked, after the TCP window of the recipient has been
  306. * exceeded. This ensures that a retransmisson of the data will no longer occur.
  307. *
  308. * Sets the expiration for recent data base on the TCP window
  309. */
  310. void update_window_expiration(flow *f, struct packet_info *info){
  311. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  312. uint32_t ack_num = htonl(info->tcp_hdr->ack_num);
  313. uint32_t end_seq = htonl(info->tcp_hdr->sequence_num) + info->app_data_len - 1;
  314. uint32_t window = ack_num + htons(info->tcp_hdr->win_size);
  315. //#ifdef DEBUG
  316. printf("Received sequence number %u\n", htonl(info->tcp_hdr->sequence_num));
  317. printf("Acknowledged up to %u with window expiring at %u\n", ack_num, window);
  318. printf("Removing all packets up to %u\n", end_seq);
  319. //#endif
  320. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  321. f->upstream_app_data->first_packet;
  322. while((saved_data != NULL) && (saved_data->expiration != 0) && (end_seq > saved_data->expiration)){
  323. //remove entire block
  324. if(incoming){
  325. f->downstream_app_data->first_packet = saved_data->next;
  326. } else {
  327. f->upstream_app_data->first_packet = saved_data->next;
  328. }
  329. free(saved_data->data);
  330. free(saved_data);
  331. saved_data = (incoming)? f->downstream_app_data->first_packet :
  332. f->upstream_app_data->first_packet;
  333. #ifdef DEBUG
  334. if(saved_data != NULL){
  335. printf("Currently saved seq_num is now %u\n", saved_data->seq_num);
  336. } else {
  337. printf("Acked all data, queue is empty\n");
  338. }
  339. #endif
  340. }
  341. /* Update expiration for packets based on TCP window size */
  342. saved_data = (incoming)? f->upstream_app_data->first_packet :
  343. f->downstream_app_data->first_packet;
  344. while((saved_data != NULL) && (ack_num > saved_data->seq_num)){
  345. //update window
  346. if(ack_num >= saved_data->seq_num + saved_data->len){
  347. //remove entire block
  348. saved_data->expiration = window;
  349. }
  350. saved_data = saved_data->next;
  351. }
  352. }
  353. /**
  354. * This function retransmits previously sent (and possibly modified) data
  355. *
  356. */
  357. void retransmit(flow *f, struct packet_info *info, uint32_t data_to_fill){
  358. uint8_t *p = info->app_data;
  359. uint32_t seq_num = htonl(info->tcp_hdr->sequence_num);
  360. uint8_t incoming = (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? 1 : 0;
  361. packet *saved_data = (incoming)? f->downstream_app_data->first_packet :
  362. f->upstream_app_data->first_packet;
  363. printf("Filling with %d retransmitted bytes\n", data_to_fill);
  364. while(data_to_fill > 0){
  365. if(saved_data == NULL){
  366. //have already acked all data
  367. p += data_to_fill;
  368. seq_num += data_to_fill;
  369. data_to_fill -= data_to_fill;
  370. continue;
  371. }
  372. if(seq_num < saved_data->seq_num){
  373. //we are missing a block. Use what was given
  374. if(saved_data->seq_num - seq_num > data_to_fill){
  375. //skip the rest
  376. p += data_to_fill;
  377. seq_num += data_to_fill;
  378. data_to_fill -= data_to_fill;
  379. } else {
  380. p += saved_data->seq_num - seq_num;
  381. data_to_fill -= saved_data->seq_num - seq_num;
  382. seq_num += saved_data->seq_num - seq_num;
  383. }
  384. } else if ( seq_num == saved_data->seq_num) {
  385. if(data_to_fill >= saved_data->len){
  386. //exhaust this block and move onto next one
  387. memcpy(p, saved_data->data, saved_data->len);
  388. p += saved_data->len;
  389. seq_num += saved_data->len;
  390. data_to_fill -= saved_data->len;
  391. saved_data = saved_data->next;
  392. } else {
  393. //fill with partial block
  394. memcpy(p, saved_data->data, data_to_fill);
  395. p += data_to_fill;
  396. seq_num += data_to_fill;
  397. data_to_fill -= data_to_fill;
  398. }
  399. } else { //seq_num > saved_data->seq_num
  400. uint32_t offset = seq_num - saved_data->seq_num;
  401. if(offset > saved_data->len){
  402. saved_data = saved_data->next;
  403. offset -= saved_data->len;
  404. } else {
  405. if(data_to_fill > saved_data->len - offset){
  406. memcpy(p, saved_data->data + offset, saved_data->len - offset);
  407. p += saved_data->len - offset;
  408. seq_num += saved_data->len - offset;
  409. data_to_fill -= saved_data->len - offset;
  410. saved_data = saved_data->next;
  411. } else {
  412. memcpy(p, saved_data->data + offset, data_to_fill);
  413. p += data_to_fill;
  414. seq_num += data_to_fill;
  415. data_to_fill -= data_to_fill;
  416. }
  417. }
  418. }
  419. }
  420. tcp_checksum(info);//update checksum
  421. }
  422. /** This function extracts the ip, tcp, and tls record headers
  423. * from a received packet (if they exist), and put them in
  424. * a packet_info struct
  425. *
  426. */
  427. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  428. /* First fill in IP header */
  429. uint8_t *p = packet;
  430. p += ETHER_HEADER_LEN; //skip ethernet header
  431. info->ip_hdr = (struct ip_header*) p;
  432. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  433. /* Verify this is an IP packet */
  434. if( (info->ip_hdr->versionihl >>4) != 4){
  435. info->ip_hdr = NULL;
  436. info->size_ip_hdr = 0;
  437. info->tcp_hdr = NULL;
  438. info->size_tcp_hdr = 0;
  439. info->record_hdr = NULL;
  440. return;
  441. }
  442. /* If this is a TCP segment, fill in TCP header */
  443. if (info->ip_hdr->proto == IPPROTO_TCP){
  444. p += info->size_ip_hdr; //skip IP header
  445. info->tcp_hdr = (struct tcp_header*) p;
  446. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  447. p += info->size_tcp_hdr;
  448. } else {
  449. info->tcp_hdr = NULL;
  450. info->size_tcp_hdr = 0;
  451. info->record_hdr = NULL;
  452. return;
  453. }
  454. /* If the application data contains a TLS record, fill in hdr */
  455. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  456. if(info->app_data_len > 0){
  457. info->app_data = p;
  458. info->record_hdr = (struct tls_header*) p;
  459. //check to see if this is a valid record
  460. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  461. info->record_hdr = NULL;
  462. }
  463. } else {
  464. info->record_hdr = NULL;
  465. info->app_data = NULL;
  466. }
  467. return;
  468. }
  469. /** Copies a packet_info structure and returns a pointer to the duplicate.
  470. */
  471. struct packet_info *copy_packet_info(struct packet_info *src_info){
  472. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  473. dst_info->ip_hdr = src_info->ip_hdr;
  474. dst_info->tcp_hdr = src_info->tcp_hdr;
  475. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  476. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  477. dst_info->app_data = src_info->app_data;
  478. dst_info->app_data_len = src_info->app_data_len;
  479. return dst_info;
  480. }