slitheen-proxy.c 15 KB

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