libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
dot11_base.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_DOT11_DOT11_H) && defined(TINS_HAVE_DOT11)
33 #define TINS_DOT11_DOT11_H
34 
35 #include <list>
36 #include "../pdu.h"
37 #include "../pdu_option.h"
38 #include "../small_uint.h"
39 #include "../hw_address.h"
40 #include "../endianness.h"
41 #include "../cxxstd.h"
42 #include "../macros.h"
43 
44 namespace Tins {
45 namespace Memory {
46 class InputMemoryStream;
47 class OutputMemoryStream;
48 } // Memory
49 
50 class RSNInformation;
51 
55 class TINS_API Dot11 : public PDU {
56 public:
61 
66 
70  typedef std::list<option> options_type;
71 
75  static const PDU::PDUType pdu_flag = PDU::DOT11;
76 
80  static const address_type BROADCAST;
81 
85  static const endian_type endianness = LE;
86 
91  enum Types {
92  MANAGEMENT = 0,
93  CONTROL = 1,
94  DATA = 2
95  };
96 
100  enum OptionTypes {
101  SSID,
102  SUPPORTED_RATES,
103  FH_SET,
104  DS_SET,
105  CF_SET,
106  TIM,
107  IBSS_SET,
108  COUNTRY,
109  HOPPING_PATTERN_PARAMS,
110  HOPPING_PATTERN_TABLE,
111  REQUEST_INFORMATION,
112  BSS_LOAD,
113  EDCA,
114  TSPEC,
115  TCLAS,
116  SCHEDULE,
117  CHALLENGE_TEXT,
118  POWER_CONSTRAINT = 32,
119  POWER_CAPABILITY,
120  TPC_REQUEST,
121  TPC_REPORT,
122  SUPPORTED_CHANNELS,
123  CHANNEL_SWITCH,
124  MEASUREMENT_REQUEST,
125  MEASUREMENT_REPORT,
126  QUIET,
127  IBSS_DFS,
128  ERP_INFORMATION,
129  TS_DELAY,
130  TCLAS_PROCESSING,
131  QOS_CAPABILITY = 46,
132  RSN = 48,
133  EXT_SUPPORTED_RATES = 50,
134  VENDOR_SPECIFIC = 221
135  };
136 
142  ASSOC_REQ = 0,
143  ASSOC_RESP = 1,
144  REASSOC_REQ = 2,
145  REASSOC_RESP = 3,
146  PROBE_REQ = 4,
147  PROBE_RESP = 5,
148  BEACON = 8,
149  ATIM = 9,
150  DISASSOC = 10,
151  AUTH = 11,
152  DEAUTH = 12
153  };
154 
160  BLOCK_ACK_REQ = 8,
161  BLOCK_ACK = 9,
162  PS = 10,
163  RTS = 11,
164  CTS = 12,
165  ACK = 13,
166  CF_END = 14,
167  CF_END_ACK = 15
168  };
169 
175  DATA_DATA = 0,
176  DATA_CF_ACK = 1,
177  DATA_CF_POLL = 2,
178  DATA_CF_ACK_POLL = 3,
179  DATA_NULL = 4,
180  CF_ACK = 5,
181  CF_POLL = 6,
182  CF_ACK_POLL = 7,
183  QOS_DATA_DATA = 8,
184  QOS_DATA_CF_ACK = 9,
185  QOS_DATA_CF_POLL = 10,
186  QOS_DATA_CF_ACK_POLL = 11,
187  QOS_DATA_NULL = 12
188  };
189 
195  Dot11(const address_type& dst_hw_addr = address_type());
196 
209  Dot11(const uint8_t* buffer, uint32_t total_sz);
210 
217  return header_.control.protocol;
218  }
219 
225  small_uint<2> type() const {
226  return header_.control.type;
227  }
228 
235  return header_.control.subtype;
236  }
237 
244  return header_.control.to_ds;
245  }
246 
253  return header_.control.from_ds;
254  }
255 
262  return header_.control.more_frag;
263  }
264 
271  return header_.control.retry;
272  }
273 
280  return header_.control.power_mgmt;
281  }
282 
288  small_uint<1> wep() const {
289  return header_.control.wep;
290  }
291 
298  return header_.control.order;
299  }
300 
306  uint16_t duration_id() const {
307  return Endian::le_to_host(header_.duration_id);
308  }
309 
315  address_type addr1() const {
316  return header_.addr1;
317  }
318 
319  // Setters
320 
326  void protocol(small_uint<2> new_proto);
327 
333  void type(small_uint<2> new_type);
334 
340  void subtype(small_uint<4> new_subtype);
341 
347  void to_ds(small_uint<1> new_value);
348 
354  void from_ds(small_uint<1> new_value);
355 
361  void more_frag(small_uint<1> new_value);
362 
368  void retry(small_uint<1> new_value);
369 
375  void power_mgmt(small_uint<1> new_value);
376 
382  void wep(small_uint<1> new_value);
383 
389  void order(small_uint<1> new_value);
390 
396  void duration_id(uint16_t new_duration_id);
397 
403  void addr1(const address_type& new_addr1);
404 
405  /* Virtual methods */
412  uint32_t header_size() const;
413 
414  #ifndef _WIN32
415 
418  void send(PacketSender& sender, const NetworkInterface& iface);
419  #endif // _WIN32
420 
425  void add_option(const option& opt);
426 
427  #if TINS_IS_CXX11
428 
435  void add_option(option &&opt) {
436  internal_add_option(opt);
437  options_.push_back(std::move(opt));
438  }
439  #endif
440 
450  bool remove_option(OptionTypes type);
451 
460  const option* search_option(OptionTypes type) const;
461 
466  PDUType pdu_type() const {
467  return pdu_flag;
468  }
469 
473  Dot11* clone() const {
474  return new Dot11(*this);
475  }
476 
482  bool matches_flag(PDUType flag) const {
483  return flag == pdu_flag;
484  }
485 
491  const options_type& options() const {
492  return options_;
493  }
494 
509  static Dot11* from_bytes(const uint8_t* buffer, uint32_t total_sz);
510 protected:
511  virtual void write_ext_header(Memory::OutputMemoryStream& stream) { }
512  virtual void write_fixed_parameters(Memory::OutputMemoryStream& stream) { }
513  void parse_tagged_parameters(Memory::InputMemoryStream& stream);
514  void add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t* val);
515 protected:
519  TINS_BEGIN_PACK
520  struct dot11_header {
521  TINS_BEGIN_PACK
522  struct {
523  #if TINS_IS_LITTLE_ENDIAN
524  uint16_t protocol:2,
525  type:2,
526  subtype:4,
527  to_ds:1,
528  from_ds:1,
529  more_frag:1,
530  retry:1,
531  power_mgmt:1,
532  more_data:1,
533  wep:1,
534  order:1;
535  #elif TINS_IS_BIG_ENDIAN
536  uint16_t subtype:4,
537  type:2,
538  protocol:2,
539  order:1,
540  wep:1,
541  more_data:1,
542  power_mgmt:1,
543  retry:1,
544  more_frag:1,
545  from_ds:1,
546  to_ds:1;
547  #endif
548  } TINS_END_PACK control;
549  uint16_t duration_id;
550  uint8_t addr1[address_type::address_size];
551 
552  } TINS_END_PACK;
553 private:
554  Dot11(const dot11_header* header_ptr);
555 
556  void internal_add_option(const option& opt);
557  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
558  options_type::const_iterator search_option_iterator(OptionTypes type) const;
559  options_type::iterator search_option_iterator(OptionTypes type);
560 
561 
562  dot11_header header_;
563  uint32_t options_size_;
564  options_type options_;
565 };
566 
567 } // Tins
568 
569 #endif // TINS_DOT11_DOT11_H
const options_type & options() const
Getter for the option list.
Definition: dot11_base.h:491
PDUOption< uint8_t, Dot11 > option
IEEE 802.11 options struct.
Definition: dot11_base.h:65
Class representing an 802.11 frame.
Definition: dot11_base.h:55
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
address_type addr1() const
Getter for the first address.
Definition: dot11_base.h:315
small_uint< 1 > power_mgmt() const
Getter for the Power-Management field.
Definition: dot11_base.h:279
OptionTypes
Enum for the different types of tagged options.
Definition: dot11_base.h:100
small_uint< 1 > more_frag() const
Getter for the More-Frag field.
Definition: dot11_base.h:261
Definition: dot11_base.h:520
Sends packets through a network interface.
Definition: packet_sender.h:117
Represents a PDU option field.
Definition: pdu_option.h:320
small_uint< 1 > to_ds() const
Getter for the To-DS field.
Definition: dot11_base.h:243
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_base.h:482
small_uint< 1 > wep() const
Getter for the WEP field.
Definition: dot11_base.h:288
HWAddress< 6 > address_type
Definition: dot11_base.h:60
DataSubtypes
Enum fro the different subtypes of 802.11 data frames.
Definition: dot11_base.h:174
Dot11 * clone() const
Definition: dot11_base.h:473
small_uint< 2 > type() const
Getter for the Type field.
Definition: dot11_base.h:225
void add_option(option &&opt)
Adds a new option to this Dot11 PDU.
Definition: dot11_base.h:435
small_uint< 1 > order() const
Getter for the Order field.
Definition: dot11_base.h:297
Types
Enum for the different types of 802.11 frames.
Definition: dot11_base.h:91
small_uint< 1 > retry() const
Getter for the Retry field.
Definition: dot11_base.h:270
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:306
small_uint< 2 > protocol() const
Getter for the protocol version field.
Definition: dot11_base.h:216
PDUType pdu_type() const
Getter for the PDU's type.
Definition: dot11_base.h:466
ManagementSubtypes
Enum for the different subtypes of 802.11 management frames.
Definition: dot11_base.h:141
std::list< option > options_type
Definition: dot11_base.h:70
Base class for protocol data units.
Definition: pdu.h:108
static const address_type BROADCAST
Broadcast hardware address.
Definition: dot11_base.h:80
endian_type
Definition: pdu.h:118
ControlSubtypes
Enum for the different subtypes of 802.11 control frames.
Definition: dot11_base.h:159
small_uint< 4 > subtype() const
Getter for the Subtype field.
Definition: dot11_base.h:234
small_uint< 1 > from_ds() const
Getter for the From-DS field.
Definition: dot11_base.h:252