| 1 |
#include <sys/types.h> |
| 2 |
#include <sys/stat.h> |
| 3 |
#include <sys/mman.h> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <string.h> |
| 9 |
#if defined(__APPLE__) |
| 10 |
#include <machine/endian.h> |
| 11 |
#else |
| 12 |
#include <endian.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "radiotap_iter.h" |
| 16 |
|
| 17 |
static int fcshdr = 0; |
| 18 |
|
| 19 |
static const struct radiotap_align_size align_size_000000_00[] = { |
| 20 |
[0] = { .align = 1, .size = 4, }, |
| 21 |
[52] = { .align = 1, .size = 4, }, |
| 22 |
}; |
| 23 |
|
| 24 |
static const struct ieee80211_radiotap_namespace vns_array[] = { |
| 25 |
{ |
| 26 |
.oui = 0x000000, |
| 27 |
.subns = 0, |
| 28 |
.n_bits = sizeof(align_size_000000_00), |
| 29 |
.align_size = align_size_000000_00, |
| 30 |
}, |
| 31 |
}; |
| 32 |
|
| 33 |
static const struct ieee80211_radiotap_vendor_namespaces vns = { |
| 34 |
.ns = vns_array, |
| 35 |
.n_ns = sizeof(vns_array)/sizeof(vns_array[0]), |
| 36 |
}; |
| 37 |
|
| 38 |
static void print_radiotap_namespace(struct ieee80211_radiotap_iterator *iter) |
| 39 |
{ |
| 40 |
switch (iter->this_arg_index) { |
| 41 |
case IEEE80211_RADIOTAP_TSFT: |
| 42 |
printf("\tTSFT: %llu\n", le64toh(*(unsigned long long *)iter->this_arg)); |
| 43 |
break; |
| 44 |
case IEEE80211_RADIOTAP_FLAGS: |
| 45 |
printf("\tflags: %02x\n", *iter->this_arg); |
| 46 |
break; |
| 47 |
case IEEE80211_RADIOTAP_RATE: |
| 48 |
printf("\trate: %lf\n", (double)*iter->this_arg/2); |
| 49 |
break; |
| 50 |
case IEEE80211_RADIOTAP_CHANNEL: |
| 51 |
case IEEE80211_RADIOTAP_FHSS: |
| 52 |
case IEEE80211_RADIOTAP_DBM_ANTSIGNAL: |
| 53 |
case IEEE80211_RADIOTAP_DBM_ANTNOISE: |
| 54 |
case IEEE80211_RADIOTAP_LOCK_QUALITY: |
| 55 |
case IEEE80211_RADIOTAP_TX_ATTENUATION: |
| 56 |
case IEEE80211_RADIOTAP_DB_TX_ATTENUATION: |
| 57 |
case IEEE80211_RADIOTAP_DBM_TX_POWER: |
| 58 |
case IEEE80211_RADIOTAP_ANTENNA: |
| 59 |
case IEEE80211_RADIOTAP_DB_ANTSIGNAL: |
| 60 |
case IEEE80211_RADIOTAP_DB_ANTNOISE: |
| 61 |
case IEEE80211_RADIOTAP_TX_FLAGS: |
| 62 |
break; |
| 63 |
case IEEE80211_RADIOTAP_RX_FLAGS: |
| 64 |
if (fcshdr) { |
| 65 |
printf("\tFCS in header: %.8x\n", |
| 66 |
le32toh(*(uint32_t *)iter->this_arg)); |
| 67 |
break; |
| 68 |
} |
| 69 |
printf("\tRX flags: %#.4x\n", |
| 70 |
le16toh(*(uint16_t *)iter->this_arg)); |
| 71 |
break; |
| 72 |
case IEEE80211_RADIOTAP_RTS_RETRIES: |
| 73 |
case IEEE80211_RADIOTAP_DATA_RETRIES: |
| 74 |
break; |
| 75 |
default: |
| 76 |
printf("\tBOGUS DATA\n"); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static void print_test_namespace(struct ieee80211_radiotap_iterator *iter) |
| 82 |
{ |
| 83 |
switch (iter->this_arg_index) { |
| 84 |
case 0: |
| 85 |
case 52: |
| 86 |
printf("\t00:00:00-00|%d: %.2x/%.2x/%.2x/%.2x\n", |
| 87 |
iter->this_arg_index, |
| 88 |
*iter->this_arg, *(iter->this_arg + 1), |
| 89 |
*(iter->this_arg + 2), *(iter->this_arg + 3)); |
| 90 |
break; |
| 91 |
default: |
| 92 |
printf("\tBOGUS DATA - vendor ns %d\n", iter->this_arg_index); |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static const struct radiotap_override overrides[] = { |
| 98 |
{ .field = 14, .align = 4, .size = 4, } |
| 99 |
}; |
| 100 |
|
| 101 |
int main(int argc, char *argv[]) |
| 102 |
{ |
| 103 |
struct ieee80211_radiotap_iterator iter; |
| 104 |
struct stat statbuf; |
| 105 |
int fd, err, fnidx = 1, i; |
| 106 |
void *data; |
| 107 |
|
| 108 |
if (argc != 2 && argc != 3) { |
| 109 |
fprintf(stderr, "usage: parse [--fcshdr] <file>\n"); |
| 110 |
fprintf(stderr, " --fcshdr: read bit 14 as FCS\n"); |
| 111 |
return 2; |
| 112 |
} |
| 113 |
|
| 114 |
if (strcmp(argv[1], "--fcshdr") == 0) { |
| 115 |
fcshdr = 1; |
| 116 |
fnidx++; |
| 117 |
} |
| 118 |
|
| 119 |
fd = open(argv[fnidx], O_RDONLY); |
| 120 |
if (fd < 0) { |
| 121 |
fprintf(stderr, "cannot open file %s\n", argv[fnidx]); |
| 122 |
return 2; |
| 123 |
} |
| 124 |
|
| 125 |
if (fstat(fd, &statbuf)) { |
| 126 |
perror("fstat"); |
| 127 |
return 2; |
| 128 |
} |
| 129 |
|
| 130 |
data = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 131 |
|
| 132 |
err = ieee80211_radiotap_iterator_init(&iter, data, statbuf.st_size, &vns); |
| 133 |
if (err) { |
| 134 |
printf("malformed radiotap header (init returns %d)\n", err); |
| 135 |
return 3; |
| 136 |
} |
| 137 |
|
| 138 |
if (fcshdr) { |
| 139 |
iter.overrides = overrides; |
| 140 |
iter.n_overrides = sizeof(overrides)/sizeof(overrides[0]); |
| 141 |
} |
| 142 |
|
| 143 |
while (!(err = ieee80211_radiotap_iterator_next(&iter))) { |
| 144 |
if (iter.this_arg_index == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) { |
| 145 |
printf("\tvendor NS (%.2x-%.2x-%.2x:%d, %d bytes)\n", |
| 146 |
iter.this_arg[0], iter.this_arg[1], |
| 147 |
iter.this_arg[2], iter.this_arg[3], |
| 148 |
iter.this_arg_size - 6); |
| 149 |
for (i = 6; i < iter.this_arg_size; i++) { |
| 150 |
if (i % 8 == 6) |
| 151 |
printf("\t\t"); |
| 152 |
else |
| 153 |
printf(" "); |
| 154 |
printf("%.2x", iter.this_arg[i]); |
| 155 |
} |
| 156 |
printf("\n"); |
| 157 |
} else if (iter.is_radiotap_ns) |
| 158 |
print_radiotap_namespace(&iter); |
| 159 |
else if (iter.current_namespace == &vns_array[0]) |
| 160 |
print_test_namespace(&iter); |
| 161 |
} |
| 162 |
|
| 163 |
if (err != -ENOENT) { |
| 164 |
printf("malformed radiotap data\n"); |
| 165 |
return 3; |
| 166 |
} |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |