slitheen.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Slitheen - a decoy routing system for censorship resistance
  2. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 3.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Additional permission under GNU GPL version 3 section 7
  17. *
  18. * If you modify this Program, or any covered work, by linking or combining
  19. * it with the OpenSSL library (or a modified version of that library),
  20. * containing parts covered by the terms of the OpenSSL Licence and the
  21. * SSLeay license, the licensors of this Program grant you additional
  22. * permission to convey the resulting work. Corresponding Source for a
  23. * non-source form of such a combination shall include the source code
  24. * for the parts of the OpenSSL library used as well as that of the covered
  25. * work.
  26. */
  27. #ifndef _SLITHEEN_H_
  28. #define _SLITHEEN_H_
  29. #include <stdlib.h>
  30. #include <netinet/in.h>
  31. #include <pcap.h>
  32. /* Ethernet addresses are 6 bytes */
  33. #define ETHER_ADDR_LEN 6
  34. #define ETHER_HEADER_LEN 2*ETHER_ADDR_LEN + 2
  35. /* Definitions for parsing packet data */
  36. struct ip_header {
  37. u_char versionihl; /* Version >> 4 | IHL & 0x0f */
  38. u_char dscpecn; /* DSCP >> 2 | ECN & 0x03 */
  39. u_short len; /* Total Length */
  40. u_short id; /* Identification */
  41. u_short flagsoff; /* Flags >> 13 | Fragment Offset & 0x1fff */
  42. #define RF 0x8000 /* Reserved; must be zero */
  43. #define DF 0x4000 /* Dont Fragment */
  44. #define MF 0x2000 /* More Fragments */
  45. u_char ttl; /* Time To Live */
  46. u_char proto; /* Protocol */
  47. u_short chksum; /* Header Checksum */
  48. struct in_addr src, dst; /* Source and Destination addresses */
  49. };
  50. #define IP_HEADER_LEN(ip) (((ip)->versionihl) & 0x0f)*4
  51. struct tcp_header {
  52. u_short src_port; /* source port */
  53. u_short dst_port; /* destination port */
  54. u_int sequence_num; /* sequence number */
  55. u_int ack_num; /* acknowledgement number */
  56. u_char offset_res_ns; /*Data offset >> 4 | res >> 1 | NS 0x01 */
  57. u_char flags; /* Flags */
  58. #define FIN 0x01
  59. #define RST 0x04
  60. u_short win_size; /* Window size*/
  61. u_short chksum; /* Checksum */
  62. u_short urg; /* Urgent pointer */
  63. };
  64. #define TCP_HEADER_LEN(tcp) (((tcp)->offset_res_ns) >> 4)*4
  65. struct tls_header {
  66. u_char type; /* Content Type */
  67. #define CCS 0x14
  68. #define ALERT 0x15
  69. #define HS 0x16
  70. #define APP 0x17
  71. #define HB 0x18
  72. u_short version; /* Version */
  73. u_short len; /* Length */
  74. u_char msg; /* Message Type */
  75. #define CLIENT_HELLO 0x01
  76. #define FINISHED 0x14
  77. };
  78. #define RECORD_HEADER_LEN 5
  79. #define CLIENT_HELLO_HEADER_LEN 6
  80. struct packet_info {
  81. const struct ip_header *ip_hdr;
  82. struct tcp_header *tcp_hdr;
  83. const struct tls_header *record_hdr;
  84. uint32_t size_tcp_hdr;
  85. uint32_t size_ip_hdr;
  86. uint8_t *app_data;
  87. uint16_t app_data_len;
  88. };
  89. struct __attribute__((__packed__)) slitheen_header {
  90. uint64_t counter;
  91. uint16_t stream_id; /* determines which stream the data is from */
  92. uint16_t len;
  93. uint16_t garbage;
  94. uint16_t zeros;
  95. };
  96. #define SLITHEEN_HEADER_LEN 16
  97. struct __attribute__((__packed__)) record_header {
  98. u_char type;
  99. #define HS 0x16
  100. u_short version;
  101. u_short len;
  102. };
  103. #define RECORD_LEN(rec) (htons(rec->len))
  104. struct __attribute__((__packed__)) handshake_header {
  105. u_char type; /*Handshake message type */
  106. u_char len1;
  107. u_char len2;
  108. u_char len3;
  109. };
  110. #define HANDSHAKE_MESSAGE_LEN(hs) (((hs)->len1) << 16)+(((hs)->len2) << 8)+ ((hs)->len3)
  111. #define HANDSHAKE_HEADER_LEN 4
  112. struct sniff_args {
  113. char *readdev;
  114. char *writedev;
  115. };
  116. struct inject_args {
  117. uint8_t *mac_addr;
  118. pcap_t *write_dev;
  119. };
  120. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet);
  121. void *sniff_packets(void *);
  122. void process_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet);
  123. void extract_packet_headers(uint8_t *packet, struct packet_info *info);
  124. struct packet_info *copy_packet_info(struct packet_info *src_info);
  125. void inject_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet);
  126. #endif /* _SLITHEEN_H_ */