Packets/80211Bundle: fixed endian-bug in Radiotap offset calculation
[senf.git] / senf / Packets / 80211Bundle / RadiotapPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27 //   Christian Niephaus <cni@berlios.de>
28
29
30 // Definition of RadiotapPacket non-inline non-template functions
31
32 #include "RadiotapPacket.hh"
33 //#include "RadiotapPacket.ih"
34
35 // Custom includes
36 #include "WLANPacket.hh"
37 #include <boost/io/ios_state.hpp>
38 #include <memory.h>
39
40 extern "C" {
41 #   include "radiotap/radiotap_iter.h"
42 }
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 // Offset table management
49
50 prefix_ senf::RadiotapPacketParser::OffsetTable &
51 senf::RadiotapPacketParser::offsetTable(boost::uint32_t presentFlags)
52 {
53     typedef std::map<boost::uint32_t, OffsetTable> OffsetMap;
54     static OffsetMap offsetMap;
55
56     OffsetMap::iterator i (offsetMap.find(presentFlags));
57     if (i == offsetMap.end())
58         i = offsetMap.insert(std::make_pair(presentFlags, OffsetTable())).first;
59     return i->second;
60 }
61
62 prefix_ void senf::RadiotapPacketParser::parseOffsetTable(boost::uint8_t * data, int maxLength,
63                                                           OffsetTable & table)
64 {
65     struct ieee80211_radiotap_iterator iter;
66     ieee80211_radiotap_iterator_init(&iter,
67                                      (struct ieee80211_radiotap_header *)data,
68                                      maxLength,
69                                      0);
70     unsigned size (8u);
71     while (ieee80211_radiotap_iterator_next(&iter) == 0) {
72         if (iter.is_radiotap_ns &&
73             iter.this_arg_index <= int(RadiotapPacketParser::MAX_INDEX))
74             table[iter.this_arg_index] = iter.this_arg - data;
75         // We need to set size here in the loop since the iter fields are only valid
76         // when at least one present bit is set ...
77         size = iter.this_arg - data + iter.this_arg_size;
78     }
79     table[MAX_INDEX+1] = size;
80 }
81
82 prefix_ void senf::RadiotapPacketParser::buildOffsetTable(boost::uint32_t presentFlags,
83                                                           OffsetTable & table)
84 {
85     SENF_ASSERT(!(presentFlags & ( (1<<IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE) |
86                                    (1<<IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
87                                    (1<<IEEE80211_RADIOTAP_EXT) )),
88                 "Extended or vendor fields not supported");
89
90     struct ieee80211_radiotap_header header;
91     memset(&header, 0, sizeof(header));
92     // header.it_version = 0;
93
94     // Iterating this packet will generate invalid addresses but we don't care since neither
95     // radiotap.c nor we will ever dereference those pointers, we just calculate the offsets.
96     // This works, as long as we don't support extension headers ...
97     header.it_len = 0xFFFF;
98     // Note that all data in the header is little endian!
99     header.it_present = htole32(presentFlags);
100
101     parseOffsetTable((boost::uint8_t*)&header, header.it_len, table);
102 }
103
104 //-/////////////////////////////////////////////////////////////////////////////////////////////////
105 // senf::RadiotapPacketParser
106
107 unsigned const senf::RadiotapPacketParser_Header::FIELD_SIZE[] = {
108     8, 1, 1, 4, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1 };
109
110 prefix_ senf::UInt32Parser senf::RadiotapPacketParser::init_fcs()
111 {
112     if (!has_fcs()) {
113         protect(), data().insert(data().end(), 4u, 0u);
114         init_flags().fcsAtEnd_() = true;
115     }
116     return fcs();
117 }
118
119 prefix_ void senf::RadiotapPacketParser::disable_fcs()
120 {
121     if (has_fcs()) {
122         validate(RadiotapPacketParser_Header::fixed_bytes+4);
123         data().erase(data().end()-4, data().end());
124         flags().fcsAtEnd_() = false;
125     }
126 }
127
128 prefix_ senf::RadiotapPacketParser::OffsetTable const &
129 senf::RadiotapPacketParser::getTable(boost::uint32_t presentFlags)
130     const
131 {
132     OffsetTable & table(offsetTable(presentFlags));
133     if (! table[MAX_INDEX+1])
134         buildOffsetTable(presentFlags, table);
135     return table;
136 }
137
138 prefix_ void senf::RadiotapPacketParser::insertRemoveBytes(unsigned from , unsigned to, int bytes)
139 {
140     data_iterator b (i() + from);
141     data_iterator e (i() + to);
142     if (bytes >= 0) {
143         // Insert some bytes cleaning the old bytes to 0 first
144         std::fill(b, e,  0u);
145         if (bytes > 0)
146             // need to protect the parser since data().insert() invalidates iterators
147             protect(), data().insert(e, bytes, 0u);
148     }
149     else { // bytes < 0
150         // Remove some bytes ...
151         // remember: bytes is negative ...
152         if (b < e + bytes)
153             std::fill(b, e + bytes, 0u);
154         data().erase(e + bytes, e);
155     }
156 }
157
158 prefix_ void senf::RadiotapPacketParser::updatePresentFlags(boost::uint32_t flags)
159 {
160     if (flags == presentFlags())
161         return;
162     validate(bytes());
163
164     OffsetTable const & oldTable (currentTable());
165     OffsetTable const & newTable (getTable(flags));
166     unsigned b (RadiotapPacketParser_Header::fixed_bytes);
167     int cumulativeNewBytes (0);
168
169     for (unsigned index (0); index <= MAX_INDEX; ++index) {
170         // Skip any unchanged fields
171         for (; index <= MAX_INDEX+1
172                  && ((oldTable[index] == 0 && newTable[index] == 0)
173                      || (oldTable[index]+cumulativeNewBytes == newTable[index])); ++index)
174             if (newTable[index] != 0)
175                 b = newTable[index] + FIELD_SIZE[index];
176         if (index > MAX_INDEX+1)
177             break;
178         // Now skip over all changed fields
179         // (The condition index <= MAX_INDEX is not needed here since the last
180         // table entry MAX_INDEX+1 is always != 0 in both tables)
181         for (; ! (oldTable[index]!=0 && newTable[index]!=0); ++index) ;
182         // index now either points to
183         // a) an entry set in both tables
184         // b) at the end of the table which contains the total length
185         // (remember: the table has a size of MAX_INDEX+2 entries !!)
186         // in both cases, the difference between the new and old size
187         // is found from the difference between the old and the new table
188         // entry
189         int newBytes (newTable[index] - oldTable[index] - cumulativeNewBytes);
190         insertRemoveBytes(b, oldTable[index] + cumulativeNewBytes, newBytes);
191         cumulativeNewBytes += newBytes;
192         b = newTable[index] + FIELD_SIZE[index];
193     }
194     length() += cumulativeNewBytes;
195     presentFlags() = flags;
196     currentTable_ = &newTable;
197 }
198
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
200 // senf::RadiotapPacketType
201
202 prefix_ void senf::RadiotapPacketType::dump(packet p, std::ostream & os)
203 {
204     boost::io::ios_all_saver ias(os);
205     os << "Radiotap:\n"
206        << senf::fieldName("version") << unsigned(p->version()) << '\n'
207        << senf::fieldName("length")  << unsigned(p->length()) << '\n';
208
209 #   define FIELD(name, sign, desc)                                      \
210         if (p->name ## Present())                                       \
211             os << senf::fieldName(desc) << sign(p->name()) << '\n';
212
213 #   define ENTER(name)                                                  \
214         if (p->name ## Present()) {                                     \
215             packet::Parser::name ## _t subparser (p->name());
216
217 #   define SUBFIELD(name, sign, desc)                                   \
218         os << senf::fieldName(desc) << sign(subparser.name()) << '\n';
219
220 #   define LEAVE()                                                      \
221         }
222
223 #   define START_FLAGS(desc)                                            \
224         os << senf::fieldName(desc);
225
226 #   define FLAG(name, desc)                                             \
227         if (subparser.name()) os << desc " "
228
229 #   define END_FLAGS()                                                  \
230         os << '\n';
231
232     FIELD           ( tsft,              boost::uint64_t, "MAC timestamp"        );
233     ENTER           ( flags                                                      );
234       START_FLAGS   (                                     "flags"                );
235         FLAG        (     shortGI,                            "ShortGI"          );
236         FLAG        (     badFCS,                             "BadFCS"           );
237         FLAG        (     fcsAtEnd,                           "FCSatEnd"         );
238         FLAG        (     fragmentation,                      "Frag"             );
239         FLAG        (     wep,                                "WEP"              );
240         FLAG        (     shortPreamble,                      "ShortPreamble"    );
241         FLAG        (     cfp,                                "CFP"              );
242       END_FLAGS     (                                                            );
243     LEAVE           (                                                            );
244     FIELD           ( rate,              unsigned,        "rate"                 );
245     ENTER           ( channelOptions                                             );
246       SUBFIELD      (     freq,          unsigned,        "channel frequency"    );
247       START_FLAGS   (                                     "channel flags"        );
248         FLAG        (     flag2ghz,                           "2GHz"             );
249         FLAG        (     ofdm,                               "OFDM"             );
250         FLAG        (     cck,                                "CCK"              );
251         FLAG        (     turbo,                              "Turbo"            );
252         FLAG        (     quarterRateChannel,                 "Rate/4"           );
253         FLAG        (     halfRateChannel,                    "Rate/2"           );
254         FLAG        (     gsm,                                "GSM"              );
255         FLAG        (     staticTurbo,                        "StaticTurbo"      );
256         FLAG        (     gfsk,                               "GFSK"             );
257         FLAG        (     cckOfdm,                            "CCK+OFDM"         );
258         FLAG        (     passive,                            "Passive"          );
259         FLAG        (     flag5ghz,                           "5GHz"             );
260       END_FLAGS     (                                                            );
261     LEAVE           (                                                            );
262     FIELD           ( fhss,              unsigned,        "FHSS"                 );
263     FIELD           ( dbmAntennaSignal,  signed,          "antenna signal (dBm)" );
264     FIELD           ( dbmAntennaNoise,   signed,          "antenna noise (dBm)"  );
265     FIELD           ( lockQuality,       unsigned,        "lock quality"         );
266     FIELD           ( txAttenuation,     unsigned,        "tx attenuation"       );
267     FIELD           ( dbTxAttenuation,   unsigned,        "tx attenuation (dB)"  );
268     FIELD           ( dbmTxAttenuation,  signed,          "tx attenuation (dBm)" );
269     FIELD           ( antenna,           unsigned,        "antenna"              );
270     FIELD           ( dbAntennaSignal,   unsigned,        "antenna signal (dB)"  );
271     FIELD           ( dbAntennaNoise,    unsigned,        "antenna noise (dB)"   );
272     ENTER           ( rxFlags                                                    );
273       START_FLAGS   (                                     "rx flags"             );
274         FLAG        (     badPlcp,                            "BadPLCP"          );
275       END_FLAGS     (                                                            );
276     LEAVE           (                                                            );
277     ENTER           ( txFlags                                                    );
278       START_FLAGS   (                                     "tx flags"             );
279         FLAG        (     fail,                               "Fail"             );
280         FLAG        (     txRts,                              "RTS"              );
281         FLAG        (     txCts,                              "CTS"              );
282       END_FLAGS     (                                                            );
283     LEAVE           (                                                            );
284     FIELD           ( rtsRetries,        unsigned,        "rts retries"          );
285     FIELD           ( dataRetries,       unsigned,        "data retries"         );
286
287     if (p->flagsPresent() && p->flags().fcsAtEnd())
288         os << senf::fieldName("fcs") << unsigned(p->fcs()) << '\n';
289
290 #   undef END_FLAGS
291 #   undef FLAG
292 #   undef START_FLAGS
293 #   undef LEAVE
294 #   undef SUBFIELD
295 #   undef ENTER
296 #   undef FIELD
297 }
298
299 prefix_ void senf::RadiotapPacketType::init(packet p)
300 {
301     // ?? Why the heck do we need the +0? Otherwise we get an
302     // 'undefined reference to 'RadiotapPacketParser_Header::fixed_bytes'
303     p->length() << RadiotapPacketParser_Header::fixed_bytes+0;
304 }
305
306 prefix_ senf::PacketInterpreterBase::factory_t senf::RadiotapPacketType::nextPacketType(packet p)
307 {
308     static factory_t frameTypeFactory[] = { WLANPacket_MgtFrame::factory(),
309                                             WLANPacket_CtrlFrame::factory(),
310                                             WLANPacket_DataFrame::factory(),
311                                             no_factory() };
312     return frameTypeFactory[p->frameType()];
313 }
314
315 prefix_ senf::RadiotapPacketType::optional_range
316 senf::RadiotapPacketType::nextPacketRange(packet const & p)
317 {
318     parser rtParser (p.parser());
319     size_type h (senf::bytes(rtParser));
320     size_type t (rtParser.flagsPresent() && rtParser.flags().fcsAtEnd() ? 4 : 0);
321     return p.size() <= h+t
322         ? no_range()
323         : optional_range( range(p.data().begin() + h, p.data().end() - t) );
324 }
325
326 //-/////////////////////////////////////////////////////////////////////////////////////////////////
327 #undef prefix_
328
329 \f
330 // Local Variables:
331 // mode: c++
332 // fill-column: 100
333 // c-file-style: "senf"
334 // indent-tabs-mode: nil
335 // ispell-local-dictionary: "american"
336 // compile-command: "scons -u test"
337 // comment-column: 40
338 // End: