packet.h 4.2 KB

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