packet.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Name: packet.c
  2. *
  3. * Slitheen - a decoy routing system for censorship resistance
  4. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 3.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Additional permission under GNU GPL version 3 section 7
  19. *
  20. * If you modify this Program, or any covered work, by linking or combining
  21. * it with the OpenSSL library (or a modified version of that library),
  22. * containing parts covered by the terms of the OpenSSL Licence and the
  23. * SSLeay license, the licensors of this Program grant you additional
  24. * permission to convey the resulting work. Corresponding Source for a
  25. * non-source form of such a combination shall include the source code
  26. * for the parts of the OpenSSL library used as well as that of the covered
  27. * work.
  28. */
  29. #include <pcap.h>
  30. #include "packet.h"
  31. #include "util.h"
  32. /** This function extracts the ip, tcp, and tls record headers
  33. * from a received packet (if they exist), and put them in
  34. * a packet_info struct
  35. */
  36. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  37. /* First fill in IP header */
  38. uint8_t *p = packet;
  39. p += ETHER_HEADER_LEN; //skip ethernet header
  40. info->ip_hdr = (struct ip_header*) p;
  41. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  42. /* Verify this is an IP packet */
  43. if( (info->ip_hdr->versionihl >>4) != 4){
  44. info->ip_hdr = NULL;
  45. info->size_ip_hdr = 0;
  46. info->tcp_hdr = NULL;
  47. info->size_tcp_hdr = 0;
  48. info->record_hdr = NULL;
  49. return;
  50. }
  51. /* If this is a TCP segment, fill in TCP header */
  52. if (info->ip_hdr->proto == IPPROTO_TCP){
  53. p += info->size_ip_hdr; //skip IP header
  54. info->tcp_hdr = (struct tcp_header*) p;
  55. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  56. p += info->size_tcp_hdr;
  57. } else {
  58. info->tcp_hdr = NULL;
  59. info->size_tcp_hdr = 0;
  60. info->record_hdr = NULL;
  61. return;
  62. }
  63. /* If the application data contains a TLS record, fill in hdr */
  64. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  65. if(info->app_data_len > 0){
  66. info->app_data = p;
  67. info->record_hdr = (struct tls_header*) p;
  68. //check to see if this is a valid record
  69. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  70. info->record_hdr = NULL;
  71. }
  72. } else {
  73. info->record_hdr = NULL;
  74. info->app_data = NULL;
  75. }
  76. return;
  77. }
  78. /** Copies a packet_info structure and returns a pointer to the duplicate.
  79. */
  80. struct packet_info *copy_packet_info(struct packet_info *src_info){
  81. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  82. dst_info->ip_hdr = src_info->ip_hdr;
  83. dst_info->tcp_hdr = src_info->tcp_hdr;
  84. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  85. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  86. dst_info->app_data = src_info->app_data;
  87. dst_info->app_data_len = src_info->app_data_len;
  88. return dst_info;
  89. }
  90. /*
  91. * Injects a packet back out the opposite interface
  92. */
  93. void inject_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet){
  94. pcap_t *handle = iargs->write_dev;
  95. //write back out to the MAC ADDR it came in on
  96. //memmove(packet, packet+ETHER_ADDR_LEN, ETHER_ADDR_LEN);
  97. //memcpy(packet+ETHER_ADDR_LEN, iargs->mac_addr, ETHER_ADDR_LEN);
  98. if((pcap_inject(handle, packet, header->len)) < 0 ){
  99. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  100. printf("Length: %d\n", header->len);
  101. }
  102. #ifdef DEBUG_EXTRA
  103. fprintf(stderr, "injected the following packet:\n");
  104. for(int i=0; i< header->len; i++){
  105. fprintf(stderr, "%02x ", packet[i]);
  106. }
  107. fprintf(stderr, "\n");
  108. #endif
  109. free(packet);
  110. }