libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
radiotap.h
1 /*
2  * Copyright (c) 2016, 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 "config.h"
31 
32 #if !defined(TINS_RADIOTAP_H) && defined(TINS_HAVE_DOT11)
33 #define TINS_RADIOTAP_H
34 
35 #include "macros.h"
36 #include "pdu.h"
37 #include "endianness.h"
38 
39 namespace Tins {
40 class PacketSender;
41 
50 class TINS_API RadioTap : public PDU {
51 public:
55  static const PDU::PDUType pdu_flag = PDU::RADIOTAP;
56 
63  enum ChannelType {
64  TURBO = 0x10,
65  CCK = 0x20,
66  OFDM = 0x40,
67  TWO_GZ = 0x80,
68  FIVE_GZ = 0x100,
69  PASSIVE = 0x200,
70  DYN_CCK_OFDM = 0x400,
71  GFSK = 0x800
72  };
73 
79  enum PresentFlags {
80  TSTF = 1 << 0,
81  FLAGS = 1 << 1,
82  RATE = 1 << 2,
83  CHANNEL = 1 << 3,
84  FHSS = 1 << 4,
85  DBM_SIGNAL = 1 << 5,
86  DBM_NOISE = 1 << 6,
87  LOCK_QUALITY = 1 << 7,
88  TX_ATTENUATION = 1 << 8,
89  DB_TX_ATTENUATION = 1 << 9,
90  DBM_TX_ATTENUATION = 1 << 10,
91  ANTENNA = 1 << 11,
92  DB_SIGNAL = 1 << 12,
93  DB_NOISE = 1 << 13,
94  RX_FLAGS = 1 << 14,
95  TX_FLAGS = 1 << 15,
96  DATA_RETRIES = 1 << 17,
97  CHANNEL_PLUS = 1 << 18,
98  MCS = 1 << 19
99  };
100 
104  enum FrameFlags {
105  CFP = 1,
106  PREAMBLE = 2,
107  WEP = 4,
108  FRAGMENTATION = 8,
109  FCS = 16,
110  PADDING = 32,
111  FAILED_FCS = 64,
112  SHORT_GI = 128
113  };
114 
118  TINS_BEGIN_PACK
119  struct mcs_type {
120  uint8_t known;
121  uint8_t flags;
122  uint8_t mcs;
123  } TINS_END_PACK;
124 
128  RadioTap();
129 
140  RadioTap(const uint8_t* buffer, uint32_t total_sz);
141 
142  /* Setters */
143 
144  #ifndef _WIN32
145 
148  void send(PacketSender& sender, const NetworkInterface& iface);
149  #endif
150 
155  void version(uint8_t new_version);
156 
161  void padding(uint8_t new_padding);
162 
167  void length(uint16_t new_length);
168 
173  void tsft(uint64_t new_tsft);
174 
179  void flags(FrameFlags new_flags);
180 
185  void rate(uint8_t new_rate);
186 
192  void channel(uint16_t new_freq, uint16_t new_type);
193 
198  void dbm_signal(int8_t new_dbm_signal);
199 
204  void dbm_noise(int8_t new_dbm_noise);
205 
210  void signal_quality(uint8_t new_signal_quality);
211 
216  void antenna(uint8_t new_antenna);
217 
222  void db_signal(uint8_t new_db_signal);
223 
228  void rx_flags(uint16_t new_rx_flag);
229 
234  void tx_flags(uint16_t new_tx_flag);
235 
240  void data_retries(uint8_t new_data_retries);
241 
246  void mcs(const mcs_type& new_mcs);
247 
248  /* Getters */
249 
254  uint8_t version() const;
255 
260  uint8_t padding() const;
261 
266  uint16_t length() const;
267 
272  uint64_t tsft() const;
273 
278  FrameFlags flags() const;
279 
284  uint8_t rate() const;
285 
290  uint16_t channel_freq() const;
291 
296  uint16_t channel_type() const;
297 
302  int8_t dbm_signal() const;
303 
308  int8_t dbm_noise() const;
309 
314  uint16_t signal_quality() const;
315 
320  uint8_t antenna() const;
321 
326  uint8_t db_signal() const;
327 
332  uint32_t channel_plus() const;
333 
338  uint8_t data_retries() const;
339 
344  uint16_t rx_flags() const;
345 
350  uint16_t tx_flags() const;
351 
356  mcs_type mcs() const;
357 
368  //return (PresentFlags)*(uint32_t*)(&radio_.it_len + 1);
369  return (PresentFlags)Endian::le_to_host(radio_.flags_32);
370  }
371 
378  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
379 
386  uint32_t header_size() const;
387 
392  uint32_t trailer_size() const;
393 
397  RadioTap* clone() const {
398  return new RadioTap(*this);
399  }
400 
405  PDUType pdu_type() const {
406  return pdu_flag;
407  }
408 private:
409  TINS_BEGIN_PACK
410  #if TINS_IS_LITTLE_ENDIAN
411  struct flags_type {
412  uint32_t
413  tsft:1,
414  flags:1,
415  rate:1,
416  channel:1,
417  fhss:1,
418  dbm_signal:1,
419  dbm_noise:1,
420  lock_quality:1,
421 
422  tx_attenuation:1,
423  db_tx_attenuation:1,
424  dbm_tx_power:1,
425  antenna:1,
426  db_signal:1,
427  db_noise:1,
428  rx_flags:1,
429  tx_flags:1,
430 
431  reserved1:1,
432  data_retries:1,
433  channel_plus:1,
434  mcs:1,
435  reserved2:4,
436 
437  reserved3:7,
438  ext:1;
439  } TINS_END_PACK;
440  #else
441  struct flags_type {
442  uint32_t
443  lock_quality:1,
444  dbm_noise:1,
445  dbm_signal:1,
446  fhss:1,
447  channel:1,
448  rate:1,
449  flags:1,
450  tsft:1,
451 
452  tx_flags:1,
453  rx_flags:1,
454  db_noise:1,
455  db_signal:1,
456  antenna:1,
457  dbm_tx_power:1,
458  db_tx_attenuation:1,
459  tx_attenuation:1,
460 
461  reserved2:4,
462  mcs:1,
463  channel_plus:1,
464  data_retries:1,
465  reserved1:1,
466 
467  ext:1,
468  reserved3:7;
469  } TINS_END_PACK;
470  #endif
471 
472  TINS_BEGIN_PACK
473  struct radiotap_hdr {
474  #if TINS_IS_LITTLE_ENDIAN
475  uint8_t it_version;
476  uint8_t it_pad;
477  #else
478  uint8_t it_pad;
479  uint8_t it_version;
480  #endif // TINS_IS_LITTLE_ENDIAN
481  uint16_t it_len;
482  union {
483  flags_type flags;
484  uint32_t flags_32;
485  };
486  } TINS_END_PACK;
487 
488  void init();
489  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
490  uint32_t find_extra_flag_fields_size(const uint8_t* buffer, uint32_t total_sz);
491 
492  template <size_t n>
493  void align_buffer(const uint8_t* buffer_start, const uint8_t*& buffer, uint32_t& size) {
494  uint32_t offset = ((buffer - buffer_start) % n);
495  if (offset) {
496  offset = n - offset;
497  if (offset > size) {
498  throw malformed_packet();
499  }
500  buffer += offset;
501  size -= offset;
502  }
503  }
504 
505 
506  radiotap_hdr radio_;
507  // present fields...
508  uint64_t tsft_;
509  uint16_t channel_type_;
510  uint16_t channel_freq_;
511  uint16_t rx_flags_;
512  uint16_t signal_quality_;
513  uint16_t tx_flags_;
514  mcs_type mcs_;
515  uint8_t antenna_;
516  uint8_t flags_;
517  uint8_t rate_;
518  uint8_t channel_;
519  uint8_t max_power_;
520  uint8_t db_signal_;
521  uint8_t data_retries_;
522  int8_t dbm_signal_;
523  int8_t dbm_noise_;
524 };
525 }
526 
527 #endif // TINS_RADIOTAP_H
The type used to represent the MCS flags field.
Definition: radiotap.h:119
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
PresentFlags
Flags used in the present field.
Definition: radiotap.h:79
Sends packets through a network interface.
Definition: packet_sender.h:117
PDUType pdu_type() const
Getter for the PDU's type.
Definition: radiotap.h:405
FrameFlags
Flags used in the RadioTap::flags() method.
Definition: radiotap.h:104
ChannelType
Enumeration of the different channel type flags.
Definition: radiotap.h:63
Class that represents the IEEE 802.11 radio tap header.
Definition: radiotap.h:50
Abstraction of a network interface.
Definition: network_interface.h:47
RadioTap * clone() const
Definition: radiotap.h:397
PresentFlags present() const
Getter for the present bit fields.
Definition: radiotap.h:367
Base class for protocol data units.
Definition: pdu.h:108