slitheen.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 [name of library's license],
  21. * the licensors of this Program grant you additional permission to convey
  22. * the resulting work. {Corresponding Source for a non-source form of such
  23. * a combination shall include the source code for the parts of the OpenSSL
  24. * library used as well as that of the covered work.}
  25. */
  26. #ifndef _SLITHEEN_H_
  27. #define _SLITHEEN_H_
  28. #include <stdlib.h>
  29. #include <netinet/in.h>
  30. #include <pcap.h>
  31. /* Ethernet addresses are 6 bytes */
  32. #define ETHER_ADDR_LEN 6
  33. #define ETHER_HEADER_LEN 2*ETHER_ADDR_LEN + 2
  34. /* Definitions for parsing packet data */
  35. struct ip_header {
  36. u_char versionihl; /* Version >> 4 | IHL & 0x0f */
  37. u_char dscpecn; /* DSCP >> 2 | ECN & 0x03 */
  38. u_short len; /* Total Length */
  39. u_short id; /* Identification */
  40. u_short flagsoff; /* Flags >> 13 | Fragment Offset & 0x1fff */
  41. #define RF 0x8000 /* Reserved; must be zero */
  42. #define DF 0x4000 /* Dont Fragment */
  43. #define MF 0x2000 /* More Fragments */
  44. u_char ttl; /* Time To Live */
  45. u_char proto; /* Protocol */
  46. u_short chksum; /* Header Checksum */
  47. struct in_addr src, dst; /* Source and Destination addresses */
  48. };
  49. #define IP_HEADER_LEN(ip) (((ip)->versionihl) & 0x0f)*4
  50. struct tcp_header {
  51. u_short src_port; /* source port */
  52. u_short dst_port; /* destination port */
  53. u_int sequence_num; /* sequence number */
  54. u_int ack_num; /* acknowledgement number */
  55. u_char offset_res_ns; /*Data offset >> 4 | res >> 1 | NS 0x01 */
  56. u_char flags; /* Flags */
  57. #define FIN 0x01
  58. #define RST 0x04
  59. u_short win_size; /* Window size*/
  60. u_short chksum; /* Checksum */
  61. u_short urg; /* Urgent pointer */
  62. };
  63. #define TCP_HEADER_LEN(tcp) (((tcp)->offset_res_ns) >> 4)*4
  64. struct tls_header {
  65. u_char type; /* Content Type */
  66. #define CCS 0x14
  67. #define ALERT 0x15
  68. #define HS 0x16
  69. #define APP 0x17
  70. #define HB 0x18
  71. u_short version; /* Version */
  72. u_short len; /* Length */
  73. u_char msg; /* Message Type */
  74. #define CLIENT_HELLO 0x01
  75. #define FINISHED 0x14
  76. };
  77. #define RECORD_HEADER_LEN 5
  78. #define CLIENT_HELLO_HEADER_LEN 6
  79. struct packet_info {
  80. const struct ip_header *ip_hdr;
  81. struct tcp_header *tcp_hdr;
  82. const struct tls_header *record_hdr;
  83. uint32_t size_tcp_hdr;
  84. uint32_t size_ip_hdr;
  85. uint8_t *app_data;
  86. uint16_t app_data_len;
  87. };
  88. struct __attribute__((__packed__)) slitheen_header {
  89. uint64_t counter;
  90. uint16_t stream_id; /* determines which stream the data is from */
  91. uint16_t len;
  92. uint16_t garbage;
  93. uint16_t zeros;
  94. };
  95. #define SLITHEEN_HEADER_LEN 16
  96. struct __attribute__((__packed__)) record_header {
  97. u_char type;
  98. #define HS 0x16
  99. u_short version;
  100. u_short len;
  101. };
  102. #define RECORD_LEN(rec) (htons(rec->len))
  103. struct __attribute__((__packed__)) handshake_header {
  104. u_char type; /*Handshake message type */
  105. u_char len1;
  106. u_char len2;
  107. u_char len3;
  108. };
  109. #define HANDSHAKE_MESSAGE_LEN(hs) (((hs)->len1) << 16)+(((hs)->len2) << 8)+ ((hs)->len3)
  110. #define HANDSHAKE_HEADER_LEN 4
  111. struct sniff_args {
  112. char *readdev;
  113. char *writedev;
  114. };
  115. struct inject_args {
  116. uint8_t *mac_addr;
  117. pcap_t *write_dev;
  118. };
  119. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet);
  120. void *sniff_packets(void *);
  121. void process_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet);
  122. void extract_packet_headers(uint8_t *packet, struct packet_info *info);
  123. struct packet_info *copy_packet_info(struct packet_info *src_info);
  124. void inject_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet);
  125. #endif /* _SLITHEEN_H_ */