libtins  4.0
dot11_base.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_DOT11_DOT11_H) && defined(TINS_HAVE_DOT11)
33 #define TINS_DOT11_DOT11_H
34 
35 #include <tins/pdu.h>
36 #include <tins/pdu_option.h>
37 #include <tins/small_uint.h>
38 #include <tins/hw_address.h>
39 #include <tins/endianness.h>
40 #include <tins/cxxstd.h>
41 #include <tins/macros.h>
42 
43 namespace Tins {
44 namespace Memory {
45 class InputMemoryStream;
46 class OutputMemoryStream;
47 } // Memory
48 
49 class RSNInformation;
50 
54 class TINS_API Dot11 : public PDU {
55 public:
60 
65 
69  typedef std::vector<option> options_type;
70 
74  static const PDU::PDUType pdu_flag = PDU::DOT11;
75 
79  static const address_type BROADCAST;
80 
84  static const endian_type endianness = LE;
85 
90  enum Types {
91  MANAGEMENT = 0,
92  CONTROL = 1,
93  DATA = 2
94  };
95 
99  enum OptionTypes {
100  SSID,
101  SUPPORTED_RATES,
102  FH_SET,
103  DS_SET,
104  CF_SET,
105  TIM,
106  IBSS_SET,
107  COUNTRY,
108  HOPPING_PATTERN_PARAMS,
109  HOPPING_PATTERN_TABLE,
110  REQUEST_INFORMATION,
111  BSS_LOAD,
112  EDCA,
113  TSPEC,
114  TCLAS,
115  SCHEDULE,
116  CHALLENGE_TEXT,
117  POWER_CONSTRAINT = 32,
118  POWER_CAPABILITY,
119  TPC_REQUEST,
120  TPC_REPORT,
121  SUPPORTED_CHANNELS,
122  CHANNEL_SWITCH,
123  MEASUREMENT_REQUEST,
124  MEASUREMENT_REPORT,
125  QUIET,
126  IBSS_DFS,
127  ERP_INFORMATION,
128  TS_DELAY,
129  TCLAS_PROCESSING,
130  QOS_CAPABILITY = 46,
131  RSN = 48,
132  EXT_SUPPORTED_RATES = 50,
133  VENDOR_SPECIFIC = 221
134  };
135 
141  ASSOC_REQ = 0,
142  ASSOC_RESP = 1,
143  REASSOC_REQ = 2,
144  REASSOC_RESP = 3,
145  PROBE_REQ = 4,
146  PROBE_RESP = 5,
147  BEACON = 8,
148  ATIM = 9,
149  DISASSOC = 10,
150  AUTH = 11,
151  DEAUTH = 12
152  };
153 
159  BLOCK_ACK_REQ = 8,
160  BLOCK_ACK = 9,
161  PS = 10,
162  RTS = 11,
163  CTS = 12,
164  ACK = 13,
165  CF_END = 14,
166  CF_END_ACK = 15
167  };
168 
174  DATA_DATA = 0,
175  DATA_CF_ACK = 1,
176  DATA_CF_POLL = 2,
177  DATA_CF_ACK_POLL = 3,
178  DATA_NULL = 4,
179  CF_ACK = 5,
180  CF_POLL = 6,
181  CF_ACK_POLL = 7,
182  QOS_DATA_DATA = 8,
183  QOS_DATA_CF_ACK = 9,
184  QOS_DATA_CF_POLL = 10,
185  QOS_DATA_CF_ACK_POLL = 11,
186  QOS_DATA_NULL = 12
187  };
188 
194  Dot11(const address_type& dst_hw_addr = address_type());
195 
208  Dot11(const uint8_t* buffer, uint32_t total_sz);
209 
216  return header_.control.protocol;
217  }
218 
224  small_uint<2> type() const {
225  return header_.control.type;
226  }
227 
234  return header_.control.subtype;
235  }
236 
243  return header_.control.to_ds;
244  }
245 
252  return header_.control.from_ds;
253  }
254 
261  return header_.control.more_frag;
262  }
263 
270  return header_.control.retry;
271  }
272 
279  return header_.control.power_mgmt;
280  }
281 
288  return header_.control.more_data;
289  }
290 
296  small_uint<1> wep() const {
297  return header_.control.wep;
298  }
299 
306  return header_.control.order;
307  }
308 
314  uint16_t duration_id() const {
315  return Endian::le_to_host(header_.duration_id);
316  }
317 
323  address_type addr1() const {
324  return header_.addr1;
325  }
326 
327  // Setters
328 
334  void protocol(small_uint<2> new_proto);
335 
341  void type(small_uint<2> new_type);
342 
348  void subtype(small_uint<4> new_subtype);
349 
355  void to_ds(small_uint<1> new_value);
356 
362  void from_ds(small_uint<1> new_value);
363 
369  void more_frag(small_uint<1> new_value);
370 
376  void retry(small_uint<1> new_value);
377 
383  void power_mgmt(small_uint<1> new_value);
384 
390  void more_data(small_uint<1> new_value);
391 
397  void wep(small_uint<1> new_value);
398 
404  void order(small_uint<1> new_value);
405 
411  void duration_id(uint16_t new_duration_id);
412 
418  void addr1(const address_type& new_addr1);
419 
420  /* Virtual methods */
427  uint32_t header_size() const;
428 
429  #ifndef _WIN32
430 
433  void send(PacketSender& sender, const NetworkInterface& iface);
434  #endif // _WIN32
435 
440  void add_option(const option& opt);
441 
442  #if TINS_IS_CXX11
443 
450  void add_option(option &&opt) {
451  internal_add_option(opt);
452  options_.push_back(std::move(opt));
453  }
454  #endif
455 
465  bool remove_option(OptionTypes type);
466 
475  const option* search_option(OptionTypes type) const;
476 
481  PDUType pdu_type() const {
482  return pdu_flag;
483  }
484 
488  Dot11* clone() const {
489  return new Dot11(*this);
490  }
491 
497  bool matches_flag(PDUType flag) const {
498  return flag == pdu_flag;
499  }
500 
506  const options_type& options() const {
507  return options_;
508  }
509 
524  static Dot11* from_bytes(const uint8_t* buffer, uint32_t total_sz);
525 protected:
526  virtual void write_ext_header(Memory::OutputMemoryStream& stream);
527  virtual void write_fixed_parameters(Memory::OutputMemoryStream& stream);
528  void parse_tagged_parameters(Memory::InputMemoryStream& stream);
529  void add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t* val);
530 protected:
534  TINS_BEGIN_PACK
535  struct dot11_header {
536  TINS_BEGIN_PACK
537  struct {
538  #if TINS_IS_LITTLE_ENDIAN
539  uint16_t protocol:2,
540  type:2,
541  subtype:4,
542  to_ds:1,
543  from_ds:1,
544  more_frag:1,
545  retry:1,
546  power_mgmt:1,
547  more_data:1,
548  wep:1,
549  order:1;
550  #elif TINS_IS_BIG_ENDIAN
551  uint16_t subtype:4,
552  type:2,
553  protocol:2,
554  order:1,
555  wep:1,
556  more_data:1,
557  power_mgmt:1,
558  retry:1,
559  more_frag:1,
560  from_ds:1,
561  to_ds:1;
562  #endif
563  } TINS_END_PACK control;
564  uint16_t duration_id;
565  uint8_t addr1[address_type::address_size];
566 
567  } TINS_END_PACK;
568 private:
569  Dot11(const dot11_header* header_ptr);
570 
571  void internal_add_option(const option& opt);
572  void write_serialization(uint8_t* buffer, uint32_t total_sz);
573  options_type::const_iterator search_option_iterator(OptionTypes type) const;
574  options_type::iterator search_option_iterator(OptionTypes type);
575 
576 
577  dot11_header header_;
578  uint32_t options_size_;
579  options_type options_;
580 };
581 
582 } // Tins
583 
584 #endif // TINS_DOT11_DOT11_H
const options_type & options() const
Getter for the option list.
Definition: dot11_base.h:506
small_uint< 1 > more_data() const
Getter for the More Data field.
Definition: dot11_base.h:287
PDUOption< uint8_t, Dot11 > option
IEEE 802.11 options struct.
Definition: dot11_base.h:64
Class representing an 802.11 frame.
Definition: dot11_base.h:54
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
address_type addr1() const
Getter for the first address.
Definition: dot11_base.h:323
small_uint< 1 > power_mgmt() const
Getter for the Power-Management field.
Definition: dot11_base.h:278
OptionTypes
Enum for the different types of tagged options.
Definition: dot11_base.h:99
small_uint< 1 > more_frag() const
Getter for the More-Frag field.
Definition: dot11_base.h:260
Definition: dot11_base.h:535
Sends packets through a network interface.
Definition: packet_sender.h:116
Represents a PDU option field.
Definition: pdu_option.h:201
small_uint< 1 > to_ds() const
Getter for the To-DS field.
Definition: dot11_base.h:242
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_base.h:497
small_uint< 1 > wep() const
Getter for the WEP field.
Definition: dot11_base.h:296
HWAddress< 6 > address_type
Definition: dot11_base.h:59
DataSubtypes
Enum fro the different subtypes of 802.11 data frames.
Definition: dot11_base.h:173
Dot11 * clone() const
Definition: dot11_base.h:488
static const address_type BROADCAST
Broadcast hardware address.
Definition: dot11_base.h:79
The Tins namespace.
Definition: address_range.h:38
small_uint< 2 > type() const
Getter for the Type field.
Definition: dot11_base.h:224
void add_option(option &&opt)
Adds a new option to this Dot11 PDU.
Definition: dot11_base.h:450
small_uint< 1 > order() const
Getter for the Order field.
Definition: dot11_base.h:305
Types
Enum for the different types of 802.11 frames.
Definition: dot11_base.h:90
std::vector< option > options_type
Definition: dot11_base.h:69
small_uint< 1 > retry() const
Getter for the Retry field.
Definition: dot11_base.h:269
Abstraction of a network interface.
Definition: network_interface.h:47
uint16_t duration_id() const
Getter for the Duration-ID field.
Definition: dot11_base.h:314
small_uint< 2 > protocol() const
Getter for the protocol version field.
Definition: dot11_base.h:215
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_base.h:481
ManagementSubtypes
Enum for the different subtypes of 802.11 management frames.
Definition: dot11_base.h:140
Base class for protocol data units.
Definition: pdu.h:107
ControlSubtypes
Enum for the different subtypes of 802.11 control frames.
Definition: dot11_base.h:158
small_uint< 4 > subtype() const
Getter for the Subtype field.
Definition: dot11_base.h:233
small_uint< 1 > from_ds() const
Getter for the From-DS field.
Definition: dot11_base.h:251