1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | /* |
---|
27 | * libpacketdump decoder for Radiotap |
---|
28 | */ |
---|
29 | #include <sys/types.h> |
---|
30 | #include <netinet/in.h> |
---|
31 | #include <stdio.h> |
---|
32 | #include <inttypes.h> |
---|
33 | #include <dlfcn.h> |
---|
34 | #include "libtrace.h" |
---|
35 | #include "libpacketdump.h" |
---|
36 | #include "lt_bswap.h" |
---|
37 | |
---|
38 | #define ALIGN_NATURAL_64(_p,_s,_c) \ |
---|
39 | while ( (_p - _s) % sizeof(uint64_t)) {_p++; _c++;} |
---|
40 | #define ALIGN_NATURAL_32(_p,_s,_c) \ |
---|
41 | while ( (_p - _s) % sizeof(uint32_t)) {_p++; _c++;} |
---|
42 | #define ALIGN_NATURAL_16(_p,_s,_c) \ |
---|
43 | while ( (_p - _s) % sizeof(uint16_t)) {_p++; _c++;} |
---|
44 | |
---|
45 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
46 | { |
---|
47 | uint32_t *ptr; |
---|
48 | uint8_t *p; /* Our current field "cursor" */ |
---|
49 | uint8_t *s; /* Start of data fields, for alignment */ |
---|
50 | struct libtrace_radiotap_t *rtap; |
---|
51 | uint16_t rtap_len; |
---|
52 | uint32_t rtap_pres; |
---|
53 | uint16_t rtap_real_len; /* to make sure length in header matches fields present */ |
---|
54 | rtap = (libtrace_radiotap_t *)packet; |
---|
55 | |
---|
56 | printf(" Radiotap:"); |
---|
57 | |
---|
58 | if (len < 8) { |
---|
59 | printf(" [|Truncated (%u bytes)]\n", len); |
---|
60 | return; |
---|
61 | } |
---|
62 | |
---|
63 | rtap_real_len = sizeof(struct libtrace_radiotap_t); |
---|
64 | rtap_len = bswap_le_to_host16(rtap->it_len); |
---|
65 | rtap_pres = bswap_le_to_host32(rtap->it_present); |
---|
66 | |
---|
67 | printf(" version: %u, length: %u, fields: %#08x\n", rtap->it_version, |
---|
68 | rtap_len, rtap_pres); |
---|
69 | |
---|
70 | /* Check for extended bitmasks */ |
---|
71 | ptr = (uint32_t *) (char *)(&(rtap->it_present)); |
---|
72 | |
---|
73 | if ( (rtap_pres) & (1 << TRACE_RADIOTAP_EXT) ) |
---|
74 | printf(" extended fields:"); |
---|
75 | |
---|
76 | while( (bswap_le_to_host32(*ptr)) & (1 << TRACE_RADIOTAP_EXT) ) { |
---|
77 | rtap_real_len += sizeof (uint32_t); |
---|
78 | ptr++; |
---|
79 | printf(" %#08x", bswap_le_to_host32(*ptr)); |
---|
80 | } |
---|
81 | |
---|
82 | if ( (rtap_pres) & (1 << TRACE_RADIOTAP_EXT) ) |
---|
83 | printf("\n"); |
---|
84 | |
---|
85 | /* make p point to the first data field */ |
---|
86 | s = (uint8_t *) rtap; |
---|
87 | p = (uint8_t *) ++ptr; |
---|
88 | |
---|
89 | if (rtap_pres & (1 << TRACE_RADIOTAP_TSFT)) { |
---|
90 | ALIGN_NATURAL_64(p,s,rtap_real_len); |
---|
91 | printf(" Radiotap: TSFT = %" PRIu64 " microseconds\n", bswap_le_to_host64(*((uint64_t *)p))); |
---|
92 | p += sizeof (uint64_t); |
---|
93 | rtap_real_len += sizeof (uint64_t); |
---|
94 | } |
---|
95 | |
---|
96 | if (rtap_pres & (1 << TRACE_RADIOTAP_FLAGS)) { |
---|
97 | printf(" Radiotap: Flags = 0x%02x\n", *p); |
---|
98 | p += sizeof (uint8_t); |
---|
99 | rtap_real_len += sizeof(uint8_t); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | if (rtap_pres & (1 << TRACE_RADIOTAP_RATE)) { |
---|
104 | printf(" Radiotap: Rate = %u kbps\n", (*p) * 500); |
---|
105 | p += sizeof (uint8_t); |
---|
106 | rtap_real_len += sizeof(uint8_t); |
---|
107 | } |
---|
108 | |
---|
109 | if (rtap_pres & (1 << TRACE_RADIOTAP_CHANNEL)) { |
---|
110 | ALIGN_NATURAL_16(p,s,rtap_real_len); |
---|
111 | printf(" Radiotap: Freq = %u MHz, ChanFlags: 0x%04x\n", bswap_le_to_host16(*((uint16_t *)p)), |
---|
112 | *(((uint16_t *)p) + 1)); |
---|
113 | p += sizeof (uint32_t); |
---|
114 | rtap_real_len += sizeof(uint32_t); |
---|
115 | } |
---|
116 | |
---|
117 | if (rtap_pres & (1 << TRACE_RADIOTAP_FHSS)) { |
---|
118 | ALIGN_NATURAL_16(p,s, rtap_real_len); |
---|
119 | printf(" Radiotap: FHSS HopSet = %u , HopPattern: %u\n", *p, *(p+1)); |
---|
120 | p += sizeof (uint16_t); |
---|
121 | rtap_real_len += sizeof(uint16_t); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_ANTSIGNAL)) { |
---|
126 | printf(" Radiotap: Signal = %i dBm\n", (int8_t) *p) ; |
---|
127 | p += sizeof (uint8_t); |
---|
128 | rtap_real_len += sizeof(uint8_t); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_ANTNOISE)) { |
---|
133 | printf(" Radiotap: Noise = %i dBm\n", (int8_t) *p); |
---|
134 | p += sizeof (uint8_t); |
---|
135 | rtap_real_len += sizeof(uint8_t); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | if (rtap_pres & (1 << TRACE_RADIOTAP_LOCK_QUALITY)) { |
---|
140 | ALIGN_NATURAL_16(p,s, rtap_real_len); |
---|
141 | printf(" Radiotap: Barker Code Lock Quality = %u\n", bswap_le_to_host16(*((uint16_t *)p))); |
---|
142 | p += sizeof (uint16_t); |
---|
143 | rtap_real_len += sizeof(uint16_t); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | if (rtap_pres & (1 << TRACE_RADIOTAP_TX_ATTENUATION)) { |
---|
148 | ALIGN_NATURAL_16(p,s, rtap_real_len); |
---|
149 | printf(" Radiotap: TX Attenuation = %u\n", bswap_le_to_host16(*((uint16_t *)p))); |
---|
150 | p += sizeof (uint16_t); |
---|
151 | rtap_real_len += sizeof(uint16_t); |
---|
152 | } |
---|
153 | |
---|
154 | if (rtap_pres & (1 << TRACE_RADIOTAP_DB_TX_ATTENUATION)) { |
---|
155 | ALIGN_NATURAL_16(p,s,rtap_real_len); |
---|
156 | printf(" Radiotap: TX Attenuation = %u dB\n", bswap_le_to_host16(*((uint16_t *)p))); |
---|
157 | p += sizeof (uint16_t); |
---|
158 | rtap_real_len += sizeof(uint16_t); |
---|
159 | } |
---|
160 | |
---|
161 | if (rtap_pres & (1 << TRACE_RADIOTAP_DBM_TX_POWER)) { |
---|
162 | printf(" Radiotap: TX Power = %i dBm\n", *p); |
---|
163 | p += sizeof (uint8_t); |
---|
164 | rtap_real_len += sizeof(uint8_t); |
---|
165 | } |
---|
166 | |
---|
167 | if (rtap_pres & (1 << TRACE_RADIOTAP_ANTENNA)) { |
---|
168 | printf(" Radiotap: Antenna = %u\n", *p); |
---|
169 | p += sizeof (uint8_t); |
---|
170 | rtap_real_len += sizeof(uint8_t); |
---|
171 | } |
---|
172 | |
---|
173 | if (rtap_pres & (1 << TRACE_RADIOTAP_DB_ANTSIGNAL)) { |
---|
174 | printf(" Radiotap: Signal = %u dB\n", *p); |
---|
175 | p += sizeof (uint8_t); |
---|
176 | rtap_real_len += sizeof(uint8_t); |
---|
177 | } |
---|
178 | |
---|
179 | if (rtap_pres & (1 << TRACE_RADIOTAP_DB_ANTNOISE)) { |
---|
180 | printf(" Radiotap: Noise = %u dB\n", *p); |
---|
181 | p += sizeof (uint8_t); |
---|
182 | rtap_real_len += sizeof(uint8_t); |
---|
183 | } |
---|
184 | |
---|
185 | if (rtap_pres & (1 << TRACE_RADIOTAP_RX_FLAGS)) { |
---|
186 | ALIGN_NATURAL_16(p,s,rtap_real_len); |
---|
187 | printf(" Radiotap: RX Flags = 0x%04x\n", *((uint16_t *)p)); |
---|
188 | p += sizeof (uint16_t); |
---|
189 | rtap_real_len += sizeof(uint16_t); |
---|
190 | } |
---|
191 | |
---|
192 | if (rtap_pres & (1 << TRACE_RADIOTAP_TX_FLAGS)) { |
---|
193 | ALIGN_NATURAL_16(p,s,rtap_real_len); |
---|
194 | printf(" Radiotap: TX Flags = 0x%04x\n", *((uint16_t *)p)); |
---|
195 | p += sizeof (uint16_t); |
---|
196 | rtap_real_len += sizeof(uint16_t); |
---|
197 | } |
---|
198 | |
---|
199 | if (rtap_pres & (1 << TRACE_RADIOTAP_RTS_RETRIES)) { |
---|
200 | printf(" Radiotap: RTS Retries = %u\n", *p); |
---|
201 | p += sizeof (uint8_t); |
---|
202 | rtap_real_len += sizeof(uint8_t); |
---|
203 | } |
---|
204 | |
---|
205 | if (rtap_pres & (1 << TRACE_RADIOTAP_DATA_RETRIES)) { |
---|
206 | printf(" Radiotap: Data Retries = %u\n", *p); |
---|
207 | p += sizeof (uint8_t); |
---|
208 | rtap_real_len += sizeof(uint8_t); |
---|
209 | } |
---|
210 | |
---|
211 | if (rtap_real_len != rtap_len) |
---|
212 | printf(" Radiotap: WARNING: Header contains un-decoded fields.\n"); |
---|
213 | |
---|
214 | if (len > rtap_len) |
---|
215 | decode_next(packet + rtap_len, len - rtap_len, "link", TRACE_TYPE_80211); |
---|
216 | |
---|
217 | return; |
---|
218 | |
---|
219 | } |
---|