libtins  4.0
radiotap.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <tins/config.h>
31 
32 #if !defined(TINS_RADIOTAP_H) && defined(TINS_HAVE_DOT11)
33 #define TINS_RADIOTAP_H
34 
35 #include <tins/macros.h>
36 #include <tins/pdu.h>
37 #include <tins/endianness.h>
38 #include <tins/pdu_option.h>
39 
40 namespace Tins {
41 class PacketSender;
42 
51 class TINS_API RadioTap : public PDU {
52 public:
56  static const PDU::PDUType pdu_flag = PDU::RADIOTAP;
57 
61  static const endian_type endianness = LE;
62 
69  enum ChannelType {
70  TURBO = 0x10,
71  CCK = 0x20,
72  OFDM = 0x40,
73  TWO_GZ = 0x80,
74  FIVE_GZ = 0x100,
75  PASSIVE = 0x200,
76  DYN_CCK_OFDM = 0x400,
77  GFSK = 0x800
78  };
79 
85  enum PresentFlags {
86  TSFT = 1 << 0,
87  TSTF = 1 << 0,
88  FLAGS = 1 << 1,
89  RATE = 1 << 2,
90  CHANNEL = 1 << 3,
91  FHSS = 1 << 4,
92  DBM_SIGNAL = 1 << 5,
93  DBM_NOISE = 1 << 6,
94  LOCK_QUALITY = 1 << 7,
95  TX_ATTENUATION = 1 << 8,
96  DB_TX_ATTENUATION = 1 << 9,
97  DBM_TX_ATTENUATION = 1 << 10,
98  ANTENNA = 1 << 11,
99  DB_SIGNAL = 1 << 12,
100  DB_NOISE = 1 << 13,
101  RX_FLAGS = 1 << 14,
102  TX_FLAGS = 1 << 15,
103  DATA_RETRIES = 1 << 17,
104  XCHANNEL = 1 << 18,
105  CHANNEL_PLUS = 1 << 18,
106  MCS = 1 << 19
107  };
108 
112  enum FrameFlags {
113  CFP = 1,
114  PREAMBLE = 2,
115  WEP = 4,
116  FRAGMENTATION = 8,
117  FCS = 16,
118  PADDING = 32,
119  FAILED_FCS = 64,
120  SHORT_GI = 128
121  };
122 
126  TINS_BEGIN_PACK
127  struct mcs_type {
128  uint8_t known;
129  uint8_t flags;
130  uint8_t mcs;
131  } TINS_END_PACK;
132 
136  TINS_BEGIN_PACK
137  struct xchannel_type {
138  uint32_t flags;
139  uint16_t frequency;
140  uint8_t channel;
141  uint8_t max_power;
142  } TINS_END_PACK;
143 
148 
152  typedef std::vector<uint8_t> options_payload_type;
153 
157  RadioTap();
158 
169  RadioTap(const uint8_t* buffer, uint32_t total_sz);
170 
171  /* Setters */
172 
173  #ifndef _WIN32
174 
177  void send(PacketSender& sender, const NetworkInterface& iface);
178  #endif
179 
184  void version(uint8_t new_version);
185 
190  void padding(uint8_t new_padding);
191 
196  void length(uint16_t new_length);
197 
202  void tsft(uint64_t new_tsft);
203 
208  void flags(FrameFlags new_flags);
209 
214  void rate(uint8_t new_rate);
215 
221  void channel(uint16_t new_freq, uint16_t new_type);
222 
227  void dbm_signal(int8_t new_dbm_signal);
228 
233  void dbm_noise(int8_t new_dbm_noise);
234 
239  void signal_quality(uint8_t new_signal_quality);
240 
245  void antenna(uint8_t new_antenna);
246 
251  void db_signal(uint8_t new_db_signal);
252 
257  void rx_flags(uint16_t new_rx_flag);
258 
263  void tx_flags(uint16_t new_tx_flag);
264 
269  void xchannel(xchannel_type new_xchannel);
270 
275  void data_retries(uint8_t new_data_retries);
276 
281  void mcs(const mcs_type& new_mcs);
282 
283  /* Getters */
284 
289  uint8_t version() const;
290 
295  uint8_t padding() const;
296 
301  uint16_t length() const;
302 
307  uint64_t tsft() const;
308 
313  FrameFlags flags() const;
314 
319  uint8_t rate() const;
320 
325  uint16_t channel_freq() const;
326 
331  uint16_t channel_type() const;
332 
337  int8_t dbm_signal() const;
338 
343  int8_t dbm_noise() const;
344 
349  uint16_t signal_quality() const;
350 
355  uint8_t antenna() const;
356 
361  uint8_t db_signal() const;
362 
367  xchannel_type xchannel() const;
368 
373  uint8_t data_retries() const;
374 
379  uint16_t rx_flags() const;
380 
385  uint16_t tx_flags() const;
386 
391  mcs_type mcs() const;
392 
402  PresentFlags present() const;
403 
410  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
411 
418  uint32_t header_size() const;
419 
424  uint32_t trailer_size() const;
425 
431  void add_option(const option& opt);
432 
440  const options_payload_type& options_payload() const;
441 
445  RadioTap* clone() const {
446  return new RadioTap(*this);
447  }
448 
453  PDUType pdu_type() const {
454  return pdu_flag;
455  }
456 private:
457  TINS_BEGIN_PACK
458  struct radiotap_header {
459  #if TINS_IS_LITTLE_ENDIAN
460  uint8_t it_version;
461  uint8_t it_pad;
462  #else
463  uint8_t it_pad;
464  uint8_t it_version;
465  #endif // TINS_IS_LITTLE_ENDIAN
466  uint16_t it_len;
467  } TINS_END_PACK;
468 
469  void write_serialization(uint8_t* buffer, uint32_t total_sz);
470  option do_find_option(PresentFlags type) const;
471 
472  radiotap_header header_;
473  options_payload_type options_payload_;
474 };
475 }
476 
477 #endif // TINS_RADIOTAP_H
The type used to represent the MCS flags field.
Definition: radiotap.h:127
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
PresentFlags
Flags used in the present field.
Definition: radiotap.h:85
Sends packets through a network interface.
Definition: packet_sender.h:116
Represents a PDU option field.
Definition: pdu_option.h:201
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: radiotap.h:453
FrameFlags
Flags used in the RadioTap::flags() method.
Definition: radiotap.h:112
ChannelType
Enumeration of the different channel type flags.
Definition: radiotap.h:69
Class that represents the IEEE 802.11 radio tap header.
Definition: radiotap.h:51
The Tins namespace.
Definition: address_range.h:38
The type used to represent the XChannel field.
Definition: radiotap.h:137
Abstraction of a network interface.
Definition: network_interface.h:47
RadioTap * clone() const
Definition: radiotap.h:445
PDUOption< RadioTap::PresentFlags, RadioTap > option
Definition: radiotap.h:147
std::vector< uint8_t > options_payload_type
Definition: radiotap.h:152
Base class for protocol data units.
Definition: pdu.h:107