slitheen-proxy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 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. seq_num += saved_data->seq_num - seq_num;
  238. data_to_fill -= 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. if(p != info->app_data){
  280. printf("UH OH something weird might happen\n");
  281. }
  282. if(observed->application){
  283. replace_packet(observed, info);
  284. } else {
  285. /* Pass data to packet chain */
  286. add_packet(observed, info);
  287. }
  288. /* Update TCP state */
  289. if(info->tcp_hdr->flags & (FIN | RST) ){
  290. /* Remove flow from table, connection ended */
  291. remove_flow(observed);
  292. } else {
  293. /* add packet to application data queue */
  294. //add new app block
  295. packet *new_block = ecalloc(1, sizeof(packet));
  296. new_block->seq_num = seq_num;
  297. new_block->data = ecalloc(1, info->app_data_len);
  298. memcpy(new_block->data, info->app_data, info->app_data_len);
  299. new_block->len = info->app_data_len;
  300. new_block->next = NULL;
  301. packet *saved_data = (incoming)? observed->downstream_app_data->first_packet :
  302. observed->upstream_app_data->first_packet;
  303. //put app data block in queue
  304. if(saved_data == NULL){
  305. if(incoming){
  306. observed->downstream_app_data->first_packet = new_block;
  307. if(new_block->seq_num ==
  308. observed->downstream_seq_num){
  309. observed->downstream_seq_num += new_block->len;
  310. #ifdef DEBUG
  311. printf("Updated downstream expected seqnum to %u\n",
  312. observed->downstream_seq_num );
  313. #endif
  314. }
  315. } else {
  316. observed->upstream_app_data->first_packet = new_block;
  317. if(new_block->seq_num ==
  318. observed->upstream_seq_num){
  319. observed->upstream_seq_num += new_block->len;
  320. #ifdef DEBUG
  321. printf("Updated upstream expected seqnum to %u\n",
  322. observed->upstream_seq_num );
  323. #endif
  324. }
  325. }
  326. }
  327. else{
  328. uint8_t saved = 0;
  329. while(saved_data->next != NULL){
  330. if(!saved && (saved_data->next->seq_num > seq_num)){
  331. new_block->next = saved_data->next;
  332. saved_data->next = new_block;
  333. saved = 1;
  334. }
  335. //update expected sequence number
  336. if(incoming){
  337. if(saved_data->next->seq_num ==
  338. observed->downstream_seq_num){
  339. observed->downstream_seq_num += saved_data->next->len;
  340. #ifdef DEBUG
  341. printf("Updated downstream expected seqnum to %u\n",
  342. observed->downstream_seq_num );
  343. #endif
  344. }
  345. } else {//outgoing
  346. if(saved_data->next->seq_num ==
  347. observed->upstream_seq_num){
  348. observed->upstream_seq_num += saved_data->next->len;
  349. #ifdef DEBUG
  350. printf("Updated upstream expected seqnum to %u\n",
  351. observed->upstream_seq_num );
  352. #endif
  353. }
  354. }
  355. saved_data = saved_data->next;
  356. }
  357. if(!saved){
  358. saved_data->next = new_block;
  359. //update expected sequence number
  360. if(incoming){
  361. if(saved_data->next->seq_num ==
  362. observed->downstream_seq_num){
  363. observed->downstream_seq_num += saved_data->next->len;
  364. #ifdef DEBUG
  365. printf("Updated downstream expected seqnum to %u\n",
  366. observed->downstream_seq_num );
  367. #endif
  368. }
  369. } else {//outgoing
  370. if(saved_data->next->seq_num ==
  371. observed->upstream_seq_num){
  372. observed->upstream_seq_num += saved_data->next->len;
  373. #ifdef DEBUG
  374. printf("Updated upstream expected seqnum to %u\n",
  375. observed->upstream_seq_num );
  376. #endif
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. /** This function extracts the ip, tcp, and tls record headers
  386. * from a received packet (if they exist), and put them in
  387. * a packet_info struct
  388. *
  389. */
  390. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  391. /* First fill in IP header */
  392. uint8_t *p = packet;
  393. p += ETHER_HEADER_LEN; //skip ethernet header
  394. info->ip_hdr = (struct ip_header*) p;
  395. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  396. /* Verify this is an IP packet */
  397. if( (info->ip_hdr->versionihl >>4) != 4){
  398. info->ip_hdr = NULL;
  399. info->size_ip_hdr = 0;
  400. info->tcp_hdr = NULL;
  401. info->size_tcp_hdr = 0;
  402. info->record_hdr = NULL;
  403. return;
  404. }
  405. /* If this is a TCP segment, fill in TCP header */
  406. if (info->ip_hdr->proto == IPPROTO_TCP){
  407. p += info->size_ip_hdr; //skip IP header
  408. info->tcp_hdr = (struct tcp_header*) p;
  409. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  410. p += info->size_tcp_hdr;
  411. } else {
  412. info->tcp_hdr = NULL;
  413. info->size_tcp_hdr = 0;
  414. info->record_hdr = NULL;
  415. return;
  416. }
  417. /* If the application data contains a TLS record, fill in hdr */
  418. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  419. if(info->app_data_len > 0){
  420. info->app_data = p;
  421. info->record_hdr = (struct tls_header*) p;
  422. //check to see if this is a valid record
  423. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  424. info->record_hdr = NULL;
  425. }
  426. } else {
  427. info->record_hdr = NULL;
  428. info->app_data = NULL;
  429. }
  430. return;
  431. }
  432. /** Copies a packet_info structure and returns a pointer to the duplicate.
  433. */
  434. struct packet_info *copy_packet_info(struct packet_info *src_info){
  435. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  436. dst_info->ip_hdr = src_info->ip_hdr;
  437. dst_info->tcp_hdr = src_info->tcp_hdr;
  438. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  439. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  440. dst_info->app_data = src_info->app_data;
  441. dst_info->app_data_len = src_info->app_data_len;
  442. return dst_info;
  443. }