2010-11-06 15:20:45 +01:00
|
|
|
/*
|
|
|
|
* PCAP capture file reader
|
|
|
|
* Copyright (c) 2010, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-11-06 15:20:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/includes.h"
|
2010-12-17 07:06:52 +01:00
|
|
|
#include <pcap.h>
|
2010-11-06 15:20:45 +01:00
|
|
|
|
|
|
|
#include "utils/common.h"
|
|
|
|
#include "wlantest.h"
|
|
|
|
|
|
|
|
|
2013-01-17 15:22:41 +01:00
|
|
|
static void write_pcap_with_radiotap(struct wlantest *wt,
|
|
|
|
const u8 *data, size_t data_len)
|
|
|
|
{
|
|
|
|
struct pcap_pkthdr h;
|
|
|
|
u8 rtap[] = {
|
|
|
|
0x00 /* rev */,
|
|
|
|
0x00 /* pad */,
|
|
|
|
0x0a, 0x00, /* header len */
|
|
|
|
0x02, 0x00, 0x00, 0x00, /* present flags */
|
|
|
|
0x00, /* flags */
|
|
|
|
0x00 /* pad */
|
|
|
|
};
|
|
|
|
u8 *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (wt->assume_fcs)
|
|
|
|
rtap[8] |= 0x10;
|
|
|
|
|
|
|
|
os_memset(&h, 0, sizeof(h));
|
|
|
|
h.ts = wt->write_pcap_time;
|
|
|
|
len = sizeof(rtap) + data_len;
|
|
|
|
buf = os_malloc(len);
|
|
|
|
if (buf == NULL)
|
|
|
|
return;
|
|
|
|
os_memcpy(buf, rtap, sizeof(rtap));
|
|
|
|
os_memcpy(buf + sizeof(rtap), data, data_len);
|
|
|
|
h.caplen = len;
|
|
|
|
h.len = len;
|
|
|
|
pcap_dump(wt->write_pcap_dumper, &h, buf);
|
|
|
|
os_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-06 15:20:45 +01:00
|
|
|
int read_cap_file(struct wlantest *wt, const char *fname)
|
|
|
|
{
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
pcap_t *pcap;
|
|
|
|
unsigned int count = 0;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const u_char *data;
|
|
|
|
int res;
|
2010-12-02 21:56:37 +01:00
|
|
|
int dlt;
|
2010-11-06 15:20:45 +01:00
|
|
|
|
|
|
|
pcap = pcap_open_offline(fname, errbuf);
|
|
|
|
if (pcap == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
|
|
|
|
fname, errbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-12-02 21:56:37 +01:00
|
|
|
dlt = pcap_datalink(pcap);
|
2010-12-04 20:18:21 +01:00
|
|
|
if (dlt != DLT_IEEE802_11_RADIO && dlt != DLT_PRISM_HEADER &&
|
|
|
|
dlt != DLT_IEEE802_11) {
|
2010-12-02 21:56:37 +01:00
|
|
|
wpa_printf(MSG_ERROR, "Unsupported pcap datalink type: %d",
|
|
|
|
dlt);
|
|
|
|
pcap_close(pcap);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "pcap datalink type: %d", dlt);
|
2010-11-06 15:20:45 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2013-05-26 18:39:48 +02:00
|
|
|
clear_notes(wt);
|
|
|
|
os_free(wt->decrypted);
|
|
|
|
wt->decrypted = NULL;
|
|
|
|
|
2010-11-06 15:20:45 +01:00
|
|
|
res = pcap_next_ex(pcap, &hdr, &data);
|
|
|
|
if (res == -2)
|
|
|
|
break; /* No more packets */
|
|
|
|
if (res == -1) {
|
|
|
|
wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
|
|
|
|
pcap_geterr(pcap));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res != 1) {
|
|
|
|
wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
|
|
|
|
"value %d", res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet was read without problems */
|
2020-03-14 16:31:57 +01:00
|
|
|
wt->frame_num++;
|
2010-11-06 15:20:45 +01:00
|
|
|
wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
|
|
|
|
"len=%u/%u",
|
|
|
|
(int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
|
|
|
|
hdr->caplen, hdr->len);
|
2010-11-12 00:00:31 +01:00
|
|
|
if (wt->write_pcap_dumper) {
|
|
|
|
wt->write_pcap_time = hdr->ts;
|
2013-01-17 15:22:41 +01:00
|
|
|
if (dlt == DLT_IEEE802_11)
|
|
|
|
write_pcap_with_radiotap(wt, data, hdr->caplen);
|
|
|
|
else
|
|
|
|
pcap_dump(wt->write_pcap_dumper, hdr, data);
|
2015-11-26 23:10:53 +01:00
|
|
|
if (wt->pcap_no_buffer)
|
|
|
|
pcap_dump_flush(wt->write_pcap_dumper);
|
2010-11-12 00:00:31 +01:00
|
|
|
}
|
2010-11-06 15:20:45 +01:00
|
|
|
if (hdr->caplen < hdr->len) {
|
2013-05-26 18:39:48 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "pcap: Dropped incomplete "
|
|
|
|
"frame (%u/%u captured)",
|
|
|
|
hdr->caplen, hdr->len);
|
|
|
|
write_pcapng_write_read(wt, dlt, hdr, data);
|
2010-11-06 15:20:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count++;
|
2010-12-02 21:56:37 +01:00
|
|
|
switch (dlt) {
|
|
|
|
case DLT_IEEE802_11_RADIO:
|
|
|
|
wlantest_process(wt, data, hdr->caplen);
|
|
|
|
break;
|
|
|
|
case DLT_PRISM_HEADER:
|
|
|
|
wlantest_process_prism(wt, data, hdr->caplen);
|
|
|
|
break;
|
2010-12-04 20:18:21 +01:00
|
|
|
case DLT_IEEE802_11:
|
|
|
|
wlantest_process_80211(wt, data, hdr->caplen);
|
2013-05-26 18:39:48 +02:00
|
|
|
break;
|
2010-12-02 21:56:37 +01:00
|
|
|
}
|
2013-05-26 18:39:48 +02:00
|
|
|
write_pcapng_write_read(wt, dlt, hdr, data);
|
2010-11-06 15:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pcap_close(pcap);
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-07 16:59:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
int read_wired_cap_file(struct wlantest *wt, const char *fname)
|
|
|
|
{
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
pcap_t *pcap;
|
|
|
|
unsigned int count = 0;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const u_char *data;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
pcap = pcap_open_offline(fname, errbuf);
|
|
|
|
if (pcap == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
|
|
|
|
fname, errbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
res = pcap_next_ex(pcap, &hdr, &data);
|
|
|
|
if (res == -2)
|
|
|
|
break; /* No more packets */
|
|
|
|
if (res == -1) {
|
|
|
|
wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
|
|
|
|
pcap_geterr(pcap));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res != 1) {
|
|
|
|
wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
|
|
|
|
"value %d", res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet was read without problems */
|
|
|
|
wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
|
|
|
|
"len=%u/%u",
|
|
|
|
(int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
|
|
|
|
hdr->caplen, hdr->len);
|
|
|
|
if (hdr->caplen < hdr->len) {
|
|
|
|
wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
|
|
|
|
"(%u/%u captured)",
|
|
|
|
hdr->caplen, hdr->len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
wlantest_process_wired(wt, data, hdr->caplen);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcap_close(pcap);
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|