libtins  4.4
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  HT_CAPABILITY,
131  QOS_CAPABILITY,
132  RSN = 48,
133  EXT_SUPPORTED_RATES = 50,
134  AP_CHANNEL_REPORT,
135  NEIGHBOR_REPORT,
136  RCPI,
137  MOBILITY_DOMAIN_MDE,
138  FAST_BSS_TRANSITION_FTE,
139  TIMEOUT_INTERVAL,
140  RIC_DATA_RDE,
141  DSE_REG_LOC,
142  SUPPORTED_OP_CLASSES,
143  EXT_CH_SWITCH_ANNOUNCEMENT,
144  HT_OPERATION,
145  SEC_CH_OFFSET,
146  BSS_AVG_ACCESS_DELAY,
147  ANTENNA,
148  RSNI,
149  MEASUREMENT_PILOT_TRANSMISSION,
150  BSS_AVAIL_ADMISSION_CAPACITY,
151  BSS_AC_ACCESS_DELAY,
152  TIME_ADVERTISEMENT,
153  RM_ENABLED_CAP,
154  MULTIPLE_BSSID,
155  BSS_2040_COEX,
156  BSS_2040_INTOLERANT_CH_REPORT,
157  OVERLAPPING_BSS_SCAN_PARAM,
158  RIC_DESCRIPTOR,
159  MGMT_MIC,
160  EVENT_REQ = 78,
161  EVENT_REPORT,
162  DIAG_REQ,
163  DIAG_REPORT,
164  LOCATION_PARAMS,
165  NONTRANSMITTED_BSSID_CAP,
166  SSID_LIST,
167  MULTIPLE_BSSID_INDEX,
168  FMS_DESCRIPTOR,
169  FMS_REQ,
170  FMS_RESP,
171  QOS_TRAFFIC_CAP,
172  BSS_MAX_IDLE_PERIOD,
173  TFS_REQ,
174  TFS_RESP,
175  WNM_SLEEP_MODE,
176  TIM_BROADCAST_REQ,
177  TIM_BROADCAST_RESP,
178  COLLOCATED_INTERFERENCE_REPORT,
179  CH_USAGE,
180  TIME_ZONE,
181  DMS_REQ,
182  DMS_RESP,
183  LINK_ID,
184  WAKEUP_SCHEDULE,
185  CH_SWITCH_TIMING,
186  PTI_CONTROL,
187  TPU_BUFFER_STATUS,
188  INTERWORKING,
189  ADVERTISEMENT_PROTOCOL,
190  EXPEDITED_BANDWIDTH_REQ,
191  QOS_MAP,
192  ROAMING_CONSORTIUM,
193  EMERG_ALERT_ID,
194  MESH_CONFIG,
195  MESH_ID,
196  MESH_LINK_METRIC_REPORT,
197  CONGESTION_NOTIFICATION,
198  MESH_PEERING_MGMT,
199  MESH_CH_SWITCH_PARAMS,
200  MESH_AWAKE_WINDOW,
201  BEACON_TIMING,
202  MCCAOP_SETUP_REQ,
203  MCCAOP_SETUP_REPLY,
204  MCCAOP_ADVERTISEMENT,
205  MCCAOP_TEARDOWN,
206  GANN,
207  RANN,
208  EXT_CAP,
209  PREQ = 130,
210  PREP,
211  PERR,
212  PXU = 137,
213  PXUC,
214  AUTH_MESH_PEER_EX,
215  MIC,
216  DEST_URI,
217  UAPSD_COEX,
218  DMG_WAKEUP_SCHEDULE,
219  EXT_SCHEDULE,
220  STA_AVAIL,
221  DMG_TSPEC,
222  NEXT_DMG_ATI,
223  DMG_CAP,
224  DMG_OP = 151,
225  DMG_BSS_PARAM_CHG,
226  DMG_BEAM_REFINEMENT,
227  CH_MEASUREMENT_FEEDBACK,
228  AWAKE_WINDOW = 157,
229  MULTIBAND,
230  ADDBA_ESXT,
231  NEXTPCP_LIST,
232  PCP_HANDOVER,
233  DMG_LINK_MARGIN,
234  SWITCHING_STREAM,
235  SESSION_TRANSITION,
236  DYNAMIC_TONE_PAIRING_REPORT,
237  CLUSTER_REPORT,
238  RELAY_CAP,
239  RELAY_TRANSFER_PARAM_SET,
240  BEAMLINK_MAINTENANCE,
241  MULTIPLE_MAC_SUBLAYERS,
242  UPID,
243  DMG_LINK_ADAPTATION_ACK,
244  MCCAOP_ADV_OVERVIEW = 174,
245  QUIET_PERIOD_REQ,
246  QUIET_PERIOD_RESP = 177,
247  QMF_POLICY = 181,
248  ECAPC_POLICY = 182,
249  CLUSTER_TIME_OFFSET,
250  INTRA_ACCESS_CAT_PRIORITY,
251  SCS_DESCRIPTOR,
252  QLOAD_REPORT,
253  HCCA_TXOP_UPDATE_COUNT,
254  HIGHER_LAYER_STREAM_ID,
255  GCR_GROUP_ADDR,
256  ANTENNA_SECTOR_ID_PATTERN,
257  VHT_CAP,
258  VHT_OP,
259  EXT_BSS_LOAD,
260  WIDE_BANDWIDTH_CH_SWITCH,
261  TRANSMIT_POWER_ENVELOPE,
262  CH_SWITCH_WRAPPER,
263  AID,
264  QUIET_CHANNEL,
265  OP_MODE_NOTIFY,
266  UPSIM,
267  REDUCED_NEIGHBOR_REPORT,
268  TVHT_OP,
269  DEV_LOC = 204,
270  WHITE_SPACE_MAP,
271  FINE_TUNING_MEASUREMENT_PARAMS,
272  VENDOR_SPECIFIC = 221
273  };
274 
280  ASSOC_REQ = 0,
281  ASSOC_RESP = 1,
282  REASSOC_REQ = 2,
283  REASSOC_RESP = 3,
284  PROBE_REQ = 4,
285  PROBE_RESP = 5,
286  BEACON = 8,
287  ATIM = 9,
288  DISASSOC = 10,
289  AUTH = 11,
290  DEAUTH = 12
291  };
292 
298  BLOCK_ACK_REQ = 8,
299  BLOCK_ACK = 9,
300  PS = 10,
301  RTS = 11,
302  CTS = 12,
303  ACK = 13,
304  CF_END = 14,
305  CF_END_ACK = 15
306  };
307 
313  DATA_DATA = 0,
314  DATA_CF_ACK = 1,
315  DATA_CF_POLL = 2,
316  DATA_CF_ACK_POLL = 3,
317  DATA_NULL = 4,
318  CF_ACK = 5,
319  CF_POLL = 6,
320  CF_ACK_POLL = 7,
321  QOS_DATA_DATA = 8,
322  QOS_DATA_CF_ACK = 9,
323  QOS_DATA_CF_POLL = 10,
324  QOS_DATA_CF_ACK_POLL = 11,
325  QOS_DATA_NULL = 12
326  };
327 
333  Dot11(const address_type& dst_hw_addr = address_type());
334 
347  Dot11(const uint8_t* buffer, uint32_t total_sz);
348 
355  return header_.control.protocol;
356  }
357 
363  small_uint<2> type() const {
364  return header_.control.type;
365  }
366 
373  return header_.control.subtype;
374  }
375 
382  return header_.control.to_ds;
383  }
384 
391  return header_.control.from_ds;
392  }
393 
400  return header_.control.more_frag;
401  }
402 
409  return header_.control.retry;
410  }
411 
418  return header_.control.power_mgmt;
419  }
420 
427  return header_.control.more_data;
428  }
429 
435  small_uint<1> wep() const {
436  return header_.control.wep;
437  }
438 
445  return header_.control.order;
446  }
447 
453  uint16_t duration_id() const {
454  return Endian::le_to_host(header_.duration_id);
455  }
456 
462  address_type addr1() const {
463  return header_.addr1;
464  }
465 
466  // Setters
467 
473  void protocol(small_uint<2> new_proto);
474 
480  void type(small_uint<2> new_type);
481 
487  void subtype(small_uint<4> new_subtype);
488 
494  void to_ds(small_uint<1> new_value);
495 
501  void from_ds(small_uint<1> new_value);
502 
508  void more_frag(small_uint<1> new_value);
509 
515  void retry(small_uint<1> new_value);
516 
522  void power_mgmt(small_uint<1> new_value);
523 
529  void more_data(small_uint<1> new_value);
530 
536  void wep(small_uint<1> new_value);
537 
543  void order(small_uint<1> new_value);
544 
550  void duration_id(uint16_t new_duration_id);
551 
557  void addr1(const address_type& new_addr1);
558 
559  /* Virtual methods */
566  uint32_t header_size() const;
567 
568  #ifndef _WIN32
572  void send(PacketSender& sender, const NetworkInterface& iface);
573  #endif // _WIN32
574 
579  void add_option(const option& opt);
580 
581  #if TINS_IS_CXX11
589  void add_option(option &&opt) {
590  internal_add_option(opt);
591  options_.push_back(std::move(opt));
592  }
593  #endif
594 
604  bool remove_option(OptionTypes type);
605 
614  const option* search_option(OptionTypes type) const;
615 
620  PDUType pdu_type() const {
621  return pdu_flag;
622  }
623 
627  Dot11* clone() const {
628  return new Dot11(*this);
629  }
630 
636  bool matches_flag(PDUType flag) const {
637  return flag == pdu_flag;
638  }
639 
645  const options_type& options() const {
646  return options_;
647  }
648 
663  static Dot11* from_bytes(const uint8_t* buffer, uint32_t total_sz);
664 protected:
665  virtual void write_ext_header(Memory::OutputMemoryStream& stream);
666  virtual void write_fixed_parameters(Memory::OutputMemoryStream& stream);
667  void parse_tagged_parameters(Memory::InputMemoryStream& stream);
668  void add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t* val);
669 protected:
673  TINS_BEGIN_PACK
674  struct dot11_header {
675  TINS_BEGIN_PACK
676  struct {
677  #if TINS_IS_LITTLE_ENDIAN
678  uint16_t protocol:2,
679  type:2,
680  subtype:4,
681  to_ds:1,
682  from_ds:1,
683  more_frag:1,
684  retry:1,
685  power_mgmt:1,
686  more_data:1,
687  wep:1,
688  order:1;
689  #elif TINS_IS_BIG_ENDIAN
690  uint16_t subtype:4,
691  type:2,
692  protocol:2,
693  order:1,
694  wep:1,
695  more_data:1,
696  power_mgmt:1,
697  retry:1,
698  more_frag:1,
699  from_ds:1,
700  to_ds:1;
701  #endif
702  } TINS_END_PACK control;
703  uint16_t duration_id;
704  uint8_t addr1[address_type::address_size];
705 
706  } TINS_END_PACK;
707 private:
708  Dot11(const dot11_header* header_ptr);
709 
710  void internal_add_option(const option& opt);
711  void write_serialization(uint8_t* buffer, uint32_t total_sz);
712  options_type::const_iterator search_option_iterator(OptionTypes type) const;
713  options_type::iterator search_option_iterator(OptionTypes type);
714 
715 
716  dot11_header header_;
717  uint32_t options_size_;
718  options_type options_;
719 };
720 
721 } // Tins
722 
723 #endif // TINS_DOT11_DOT11_H
Class representing an 802.11 frame.
Definition: dot11_base.h:54
small_uint< 1 > wep() const
Getter for the WEP field.
Definition: dot11_base.h:435
static const address_type BROADCAST
Broadcast hardware address.
Definition: dot11_base.h:79
PDUType pdu_type() const
Getter for the PDU's type.
Definition: dot11_base.h:620
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_base.h:636
ManagementSubtypes
Enum for the different subtypes of 802.11 management frames.
Definition: dot11_base.h:279
small_uint< 2 > protocol() const
Getter for the protocol version field.
Definition: dot11_base.h:354
OptionTypes
Enum for the different types of tagged options.
Definition: dot11_base.h:99
small_uint< 1 > more_data() const
Getter for the More Data field.
Definition: dot11_base.h:426
small_uint< 4 > subtype() const
Getter for the Subtype field.
Definition: dot11_base.h:372
small_uint< 1 > power_mgmt() const
Getter for the Power-Management field.
Definition: dot11_base.h:417
std::vector< option > options_type
Definition: dot11_base.h:69
small_uint< 1 > from_ds() const
Getter for the From-DS field.
Definition: dot11_base.h:390
const options_type & options() const
Getter for the option list.
Definition: dot11_base.h:645
Types
Enum for the different types of 802.11 frames.
Definition: dot11_base.h:90
DataSubtypes
Enum fro the different subtypes of 802.11 data frames.
Definition: dot11_base.h:312
HWAddress< 6 > address_type
Definition: dot11_base.h:59
small_uint< 1 > to_ds() const
Getter for the To-DS field.
Definition: dot11_base.h:381
uint16_t duration_id() const
Getter for the Duration-ID field.
Definition: dot11_base.h:453
PDUOption< uint8_t, Dot11 > option
IEEE 802.11 options struct.
Definition: dot11_base.h:64
address_type addr1() const
Getter for the first address.
Definition: dot11_base.h:462
small_uint< 1 > more_frag() const
Getter for the More-Frag field.
Definition: dot11_base.h:399
ControlSubtypes
Enum for the different subtypes of 802.11 control frames.
Definition: dot11_base.h:297
small_uint< 2 > type() const
Getter for the Type field.
Definition: dot11_base.h:363
small_uint< 1 > order() const
Getter for the Order field.
Definition: dot11_base.h:444
Dot11 * clone() const
Definition: dot11_base.h:627
void add_option(option &&opt)
Adds a new option to this Dot11 PDU.
Definition: dot11_base.h:589
small_uint< 1 > retry() const
Getter for the Retry field.
Definition: dot11_base.h:408
Abstraction of a network interface.
Definition: network_interface.h:47
Represents a PDU option field.
Definition: pdu_option.h:201
Base class for protocol data units.
Definition: pdu.h:107
endian_type
Definition: pdu.h:117
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
Sends packets through a network interface.
Definition: packet_sender.h:118
The Tins namespace.
Definition: address_range.h:38
Definition: dot11_base.h:674