readpcap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * PCAP capture file reader
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include <pcap.h>
  10. #include "utils/common.h"
  11. #include "wlantest.h"
  12. static void write_pcap_with_radiotap(struct wlantest *wt,
  13. const u8 *data, size_t data_len)
  14. {
  15. struct pcap_pkthdr h;
  16. u8 rtap[] = {
  17. 0x00 /* rev */,
  18. 0x00 /* pad */,
  19. 0x0a, 0x00, /* header len */
  20. 0x02, 0x00, 0x00, 0x00, /* present flags */
  21. 0x00, /* flags */
  22. 0x00 /* pad */
  23. };
  24. u8 *buf;
  25. size_t len;
  26. if (wt->assume_fcs)
  27. rtap[8] |= 0x10;
  28. os_memset(&h, 0, sizeof(h));
  29. h.ts = wt->write_pcap_time;
  30. len = sizeof(rtap) + data_len;
  31. buf = os_malloc(len);
  32. if (buf == NULL)
  33. return;
  34. os_memcpy(buf, rtap, sizeof(rtap));
  35. os_memcpy(buf + sizeof(rtap), data, data_len);
  36. h.caplen = len;
  37. h.len = len;
  38. pcap_dump(wt->write_pcap_dumper, &h, buf);
  39. os_free(buf);
  40. }
  41. int read_cap_file(struct wlantest *wt, const char *fname)
  42. {
  43. char errbuf[PCAP_ERRBUF_SIZE];
  44. pcap_t *pcap;
  45. unsigned int count = 0;
  46. struct pcap_pkthdr *hdr;
  47. const u_char *data;
  48. int res;
  49. int dlt;
  50. pcap = pcap_open_offline(fname, errbuf);
  51. if (pcap == NULL) {
  52. wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
  53. fname, errbuf);
  54. return -1;
  55. }
  56. dlt = pcap_datalink(pcap);
  57. if (dlt != DLT_IEEE802_11_RADIO && dlt != DLT_PRISM_HEADER &&
  58. dlt != DLT_IEEE802_11) {
  59. wpa_printf(MSG_ERROR, "Unsupported pcap datalink type: %d",
  60. dlt);
  61. pcap_close(pcap);
  62. return -1;
  63. }
  64. wpa_printf(MSG_DEBUG, "pcap datalink type: %d", dlt);
  65. for (;;) {
  66. clear_notes(wt);
  67. os_free(wt->decrypted);
  68. wt->decrypted = NULL;
  69. res = pcap_next_ex(pcap, &hdr, &data);
  70. if (res == -2)
  71. break; /* No more packets */
  72. if (res == -1) {
  73. wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
  74. pcap_geterr(pcap));
  75. break;
  76. }
  77. if (res != 1) {
  78. wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
  79. "value %d", res);
  80. break;
  81. }
  82. /* Packet was read without problems */
  83. wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
  84. "len=%u/%u",
  85. (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
  86. hdr->caplen, hdr->len);
  87. if (wt->write_pcap_dumper) {
  88. wt->write_pcap_time = hdr->ts;
  89. if (dlt == DLT_IEEE802_11)
  90. write_pcap_with_radiotap(wt, data, hdr->caplen);
  91. else
  92. pcap_dump(wt->write_pcap_dumper, hdr, data);
  93. if (wt->pcap_no_buffer)
  94. pcap_dump_flush(wt->write_pcap_dumper);
  95. }
  96. if (hdr->caplen < hdr->len) {
  97. add_note(wt, MSG_DEBUG, "pcap: Dropped incomplete "
  98. "frame (%u/%u captured)",
  99. hdr->caplen, hdr->len);
  100. write_pcapng_write_read(wt, dlt, hdr, data);
  101. continue;
  102. }
  103. count++;
  104. switch (dlt) {
  105. case DLT_IEEE802_11_RADIO:
  106. wlantest_process(wt, data, hdr->caplen);
  107. break;
  108. case DLT_PRISM_HEADER:
  109. wlantest_process_prism(wt, data, hdr->caplen);
  110. break;
  111. case DLT_IEEE802_11:
  112. wlantest_process_80211(wt, data, hdr->caplen);
  113. break;
  114. }
  115. write_pcapng_write_read(wt, dlt, hdr, data);
  116. }
  117. pcap_close(pcap);
  118. wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
  119. return 0;
  120. }
  121. int read_wired_cap_file(struct wlantest *wt, const char *fname)
  122. {
  123. char errbuf[PCAP_ERRBUF_SIZE];
  124. pcap_t *pcap;
  125. unsigned int count = 0;
  126. struct pcap_pkthdr *hdr;
  127. const u_char *data;
  128. int res;
  129. pcap = pcap_open_offline(fname, errbuf);
  130. if (pcap == NULL) {
  131. wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
  132. fname, errbuf);
  133. return -1;
  134. }
  135. for (;;) {
  136. res = pcap_next_ex(pcap, &hdr, &data);
  137. if (res == -2)
  138. break; /* No more packets */
  139. if (res == -1) {
  140. wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
  141. pcap_geterr(pcap));
  142. break;
  143. }
  144. if (res != 1) {
  145. wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
  146. "value %d", res);
  147. break;
  148. }
  149. /* Packet was read without problems */
  150. wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
  151. "len=%u/%u",
  152. (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
  153. hdr->caplen, hdr->len);
  154. if (hdr->caplen < hdr->len) {
  155. wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
  156. "(%u/%u captured)",
  157. hdr->caplen, hdr->len);
  158. continue;
  159. }
  160. count++;
  161. wlantest_process_wired(wt, data, hdr->caplen);
  162. }
  163. pcap_close(pcap);
  164. wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
  165. return 0;
  166. }