slitheen-proxy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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", macaddr);
  39. snprintf(filter2, 33, "ether dst host %s", macaddr);
  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 acked data from opposite queue
  172. uint32_t ack_num = htonl(info->tcp_hdr->ack_num);
  173. packet *saved_data = (incoming)? observed->upstream_app_data->first_packet :
  174. observed->downstream_app_data->first_packet;
  175. while((saved_data != NULL) &&(ack_num > saved_data->seq_num)){
  176. //remove acked data
  177. if(ack_num >= saved_data->seq_num + saved_data->len){
  178. //remove entire block
  179. if(incoming){
  180. observed->upstream_app_data->first_packet = saved_data->next;
  181. } else {
  182. observed->downstream_app_data->first_packet = saved_data->next;
  183. }
  184. free(saved_data->data);
  185. free(saved_data);
  186. saved_data = (incoming)? observed->upstream_app_data->first_packet :
  187. observed->downstream_app_data->first_packet;
  188. } else {
  189. //remove partial block
  190. uint32_t amt_acked = ack_num - saved_data->seq_num;
  191. memmove(saved_data->data, saved_data->data+amt_acked, saved_data->len - amt_acked);
  192. saved_data->len -= amt_acked;
  193. saved_data->seq_num += amt_acked;
  194. }
  195. #ifdef DEBUG
  196. if(saved_data != NULL){
  197. printf("Currently saved seq_num is now %u\n", saved_data->seq_num);
  198. } else {
  199. printf("Acked all data, queue is empty\n");
  200. }
  201. #endif
  202. }
  203. //fill with retransmit data, process new data
  204. uint32_t data_to_fill;
  205. uint32_t data_to_process;
  206. if(seq_num > expected_seq){
  207. data_to_process = info->app_data_len;
  208. data_to_fill = 0;
  209. } else if (seq_num + info->app_data_len > expected_seq){
  210. data_to_fill = expected_seq - seq_num;
  211. data_to_process = seq_num + info->app_data_len - expected_seq;
  212. } else {
  213. data_to_fill = info->app_data_len;
  214. data_to_process = 0;
  215. }
  216. uint8_t *p = info->app_data;
  217. if(data_to_fill){ //retransmit
  218. packet *saved_data = (incoming)? observed->downstream_app_data->first_packet :
  219. observed->upstream_app_data->first_packet;
  220. while(data_to_fill > 0){
  221. if(saved_data == NULL){
  222. //have already acked all data
  223. p += data_to_fill;
  224. seq_num += data_to_fill;
  225. data_to_fill -= data_to_fill;
  226. continue;
  227. }
  228. if(seq_num < saved_data->seq_num){
  229. //we are missing a block. Use what was given
  230. if(saved_data->seq_num - seq_num > data_to_fill){
  231. //skip the rest
  232. p += data_to_fill;
  233. seq_num += data_to_fill;
  234. data_to_fill -= data_to_fill;
  235. } else {
  236. p += saved_data->seq_num - seq_num;
  237. data_to_fill -= saved_data->seq_num - seq_num;
  238. seq_num += saved_data->seq_num - seq_num;
  239. }
  240. } else if ( seq_num == saved_data->seq_num) {
  241. if(data_to_fill >= saved_data->len){
  242. //exhaust this block and move onto next one
  243. memcpy(p, saved_data->data, saved_data->len);
  244. p += saved_data->len;
  245. seq_num += saved_data->len;
  246. data_to_fill -= saved_data->len;
  247. saved_data = saved_data->next;
  248. } else {
  249. //fill with partial block
  250. memcpy(p, saved_data->data, data_to_fill);
  251. p += data_to_fill;
  252. seq_num += data_to_fill;
  253. data_to_fill -= data_to_fill;
  254. }
  255. } else { //seq_num > saved_data->seq_num
  256. uint32_t offset = seq_num - saved_data->seq_num;
  257. if(offset > saved_data->len){
  258. saved_data = saved_data->next;
  259. offset -= saved_data->len;
  260. } else {
  261. if(data_to_fill > saved_data->len - offset){
  262. memcpy(p, saved_data->data + offset, saved_data->len - offset);
  263. p += saved_data->len - offset;
  264. seq_num += saved_data->len - offset;
  265. data_to_fill -= saved_data->len - offset;
  266. saved_data = saved_data->next;
  267. } else {
  268. memcpy(p, saved_data->data + offset, data_to_fill);
  269. p += data_to_fill;
  270. seq_num += data_to_fill;
  271. data_to_fill -= data_to_fill;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. tcp_checksum(info);//update checksum
  278. if(data_to_process){
  279. uint8_t removed = 0;
  280. if(p != info->app_data){
  281. printf("UH OH something weird might happen\n");
  282. }
  283. if(observed->application){
  284. replace_packet(observed, info);
  285. } else {
  286. /* Pass data to packet chain */
  287. if(add_packet(observed, info)){//removed_flow
  288. removed = 1;
  289. }
  290. }
  291. /* Update TCP state */
  292. if(info->tcp_hdr->flags & (FIN | RST) ){
  293. /* Remove flow from table, connection ended */
  294. remove_flow(observed);
  295. } else {
  296. /* add packet to application data queue */
  297. //check if flow was removed
  298. if(removed){
  299. return;
  300. }
  301. //add new app block
  302. packet *new_block = ecalloc(1, sizeof(packet));
  303. new_block->seq_num = seq_num;
  304. new_block->data = ecalloc(1, info->app_data_len);
  305. memcpy(new_block->data, info->app_data, info->app_data_len);
  306. new_block->len = info->app_data_len;
  307. new_block->next = NULL;
  308. packet *saved_data = (incoming)? observed->downstream_app_data->first_packet :
  309. observed->upstream_app_data->first_packet;
  310. //put app data block in queue
  311. if(saved_data == NULL){
  312. if(incoming){
  313. observed->downstream_app_data->first_packet = new_block;
  314. if(new_block->seq_num ==
  315. observed->downstream_seq_num){
  316. observed->downstream_seq_num += new_block->len;
  317. #ifdef DEBUG
  318. printf("Updated downstream expected seqnum to %u\n",
  319. observed->downstream_seq_num );
  320. #endif
  321. }
  322. } else {
  323. observed->upstream_app_data->first_packet = new_block;
  324. if(new_block->seq_num ==
  325. observed->upstream_seq_num){
  326. observed->upstream_seq_num += new_block->len;
  327. #ifdef DEBUG
  328. printf("Updated upstream expected seqnum to %u\n",
  329. observed->upstream_seq_num );
  330. #endif
  331. }
  332. }
  333. }
  334. else{
  335. uint8_t saved = 0;
  336. while(saved_data->next != NULL){
  337. if(!saved && (saved_data->next->seq_num > seq_num)){
  338. new_block->next = saved_data->next;
  339. saved_data->next = new_block;
  340. saved = 1;
  341. }
  342. //update expected sequence number
  343. if(incoming){
  344. if(saved_data->next->seq_num ==
  345. observed->downstream_seq_num){
  346. observed->downstream_seq_num += saved_data->next->len;
  347. #ifdef DEBUG
  348. printf("Updated downstream expected seqnum to %u\n",
  349. observed->downstream_seq_num );
  350. #endif
  351. }
  352. } else {//outgoing
  353. if(saved_data->next->seq_num ==
  354. observed->upstream_seq_num){
  355. observed->upstream_seq_num += saved_data->next->len;
  356. #ifdef DEBUG
  357. printf("Updated upstream expected seqnum to %u\n",
  358. observed->upstream_seq_num );
  359. #endif
  360. }
  361. }
  362. saved_data = saved_data->next;
  363. }
  364. if(!saved){
  365. saved_data->next = new_block;
  366. //update expected sequence number
  367. if(incoming){
  368. if(saved_data->next->seq_num ==
  369. observed->downstream_seq_num){
  370. observed->downstream_seq_num += saved_data->next->len;
  371. #ifdef DEBUG
  372. printf("Updated downstream expected seqnum to %u\n",
  373. observed->downstream_seq_num );
  374. #endif
  375. }
  376. } else {//outgoing
  377. if(saved_data->next->seq_num ==
  378. observed->upstream_seq_num){
  379. observed->upstream_seq_num += saved_data->next->len;
  380. #ifdef DEBUG
  381. printf("Updated upstream expected seqnum to %u\n",
  382. observed->upstream_seq_num );
  383. #endif
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. /** This function extracts the ip, tcp, and tls record headers
  393. * from a received packet (if they exist), and put them in
  394. * a packet_info struct
  395. *
  396. */
  397. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  398. /* First fill in IP header */
  399. uint8_t *p = packet;
  400. p += ETHER_HEADER_LEN; //skip ethernet header
  401. info->ip_hdr = (struct ip_header*) p;
  402. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  403. /* Verify this is an IP packet */
  404. if( (info->ip_hdr->versionihl >>4) != 4){
  405. info->ip_hdr = NULL;
  406. info->size_ip_hdr = 0;
  407. info->tcp_hdr = NULL;
  408. info->size_tcp_hdr = 0;
  409. info->record_hdr = NULL;
  410. return;
  411. }
  412. /* If this is a TCP segment, fill in TCP header */
  413. if (info->ip_hdr->proto == IPPROTO_TCP){
  414. p += info->size_ip_hdr; //skip IP header
  415. info->tcp_hdr = (struct tcp_header*) p;
  416. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  417. p += info->size_tcp_hdr;
  418. } else {
  419. info->tcp_hdr = NULL;
  420. info->size_tcp_hdr = 0;
  421. info->record_hdr = NULL;
  422. return;
  423. }
  424. /* If the application data contains a TLS record, fill in hdr */
  425. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  426. if(info->app_data_len > 0){
  427. info->app_data = p;
  428. info->record_hdr = (struct tls_header*) p;
  429. //check to see if this is a valid record
  430. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  431. info->record_hdr = NULL;
  432. }
  433. } else {
  434. info->record_hdr = NULL;
  435. info->app_data = NULL;
  436. }
  437. return;
  438. }
  439. /** Copies a packet_info structure and returns a pointer to the duplicate.
  440. */
  441. struct packet_info *copy_packet_info(struct packet_info *src_info){
  442. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  443. dst_info->ip_hdr = src_info->ip_hdr;
  444. dst_info->tcp_hdr = src_info->tcp_hdr;
  445. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  446. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  447. dst_info->app_data = src_info->app_data;
  448. dst_info->app_data_len = src_info->app_data_len;
  449. return dst_info;
  450. }