libtins  4.0
dot11_control.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_CONTROL_H) && defined(TINS_HAVE_DOT11)
33 
34 #define TINS_DOT11_DOT11_CONTROL_H
35 
36 #include <tins/dot11/dot11_base.h>
37 #include <tins/macros.h>
38 
39 namespace Tins {
43 class TINS_API Dot11Control : public Dot11 {
44 public:
48  static const PDU::PDUType pdu_flag = PDU::DOT11_CONTROL;
49 
58  Dot11Control(const address_type& dst_addr = address_type());
59 
74  Dot11Control(const uint8_t* buffer, uint32_t total_sz);
75 
80  PDUType pdu_type() const {
81  return pdu_flag;
82  }
83 
89  bool matches_flag(PDUType flag) const {
90  return flag == pdu_flag || Dot11::matches_flag(flag);
91  }
92 };
93 
98 class TINS_API Dot11ControlTA : public Dot11Control {
99 public:
103  address_type target_addr() const {
104  return taddr_;
105  }
106 
111  void target_addr(const address_type& addr);
112 protected:
122  Dot11ControlTA(const address_type& dst_addr = address_type(),
123  const address_type& target_addr = address_type());
124 
139  Dot11ControlTA(const uint8_t* buffer, uint32_t total_sz);
140 
147  uint32_t header_size() const;
148 protected:
152  uint32_t controlta_size() const {
153  return static_cast<uint32_t>(taddr_.size() + sizeof(dot11_header));
154  }
155 
156  void write_ext_header(Memory::OutputMemoryStream& stream);
157 private:
158 
159  address_type taddr_;
160 };
161 
165 class TINS_API Dot11RTS : public Dot11ControlTA {
166 public:
170  static const PDU::PDUType pdu_flag = PDU::DOT11_RTS;
171 
181  Dot11RTS(const address_type& dst_addr = address_type(),
182  const address_type& target_addr = address_type());
183 
197  Dot11RTS(const uint8_t* buffer, uint32_t total_sz);
198 
204  Dot11RTS* clone() const {
205  return new Dot11RTS(*this);
206  }
207 
212  PDUType pdu_type() const {
213  return pdu_flag;
214  }
215 
221  bool matches_flag(PDUType flag) const {
222  return flag == pdu_flag || Dot11Control::matches_flag(flag);
223  }
224 };
225 
226 class TINS_API Dot11PSPoll : public Dot11ControlTA {
227 public:
231  static const PDU::PDUType pdu_flag = PDU::DOT11_PS_POLL;
232 
242  Dot11PSPoll(const address_type& dst_addr = address_type(),
243  const address_type& target_addr = address_type());
244 
259  Dot11PSPoll(const uint8_t* buffer, uint32_t total_sz);
260 
266  Dot11PSPoll* clone() const {
267  return new Dot11PSPoll(*this);
268  }
269 
274  PDUType pdu_type() const {
275  return pdu_flag;
276  }
277 
283  bool matches_flag(PDUType flag) const {
284  return flag == pdu_flag || Dot11Control::matches_flag(flag);
285  }
286 };
287 
288 class TINS_API Dot11CFEnd : public Dot11ControlTA {
289 public:
293  static const PDU::PDUType pdu_flag = PDU::DOT11_CF_END;
294 
304  Dot11CFEnd(const address_type& dst_addr = address_type(),
305  const address_type& target_addr = address_type());
306 
321  Dot11CFEnd(const uint8_t* buffer, uint32_t total_sz);
322 
328  Dot11CFEnd* clone() const {
329  return new Dot11CFEnd(*this);
330  }
331 
336  PDUType pdu_type() const {
337  return pdu_flag;
338  }
339 
345  bool matches_flag(PDUType flag) const {
346  return flag == pdu_flag || Dot11Control::matches_flag(flag);
347  }
348 };
349 
350 class TINS_API Dot11EndCFAck : public Dot11ControlTA {
351 public:
355  static const PDU::PDUType pdu_flag = PDU::DOT11_END_CF_ACK;
356 
366  Dot11EndCFAck(const address_type& dst_addr = address_type(),
367  const address_type& target_addr = address_type());
368 
383  Dot11EndCFAck(const uint8_t* buffer, uint32_t total_sz);
384 
390  Dot11EndCFAck* clone() const {
391  return new Dot11EndCFAck(*this);
392  }
393 
398  PDUType pdu_type() const {
399  return pdu_flag;
400  }
401 
407  bool matches_flag(PDUType flag) const {
408  return flag == pdu_flag || Dot11Control::matches_flag(flag);
409  }
410 };
411 
412 class TINS_API Dot11Ack : public Dot11Control {
413 public:
417  static const PDU::PDUType pdu_flag = PDU::DOT11_ACK;
418 
427  Dot11Ack(const address_type& dst_addr = address_type());
428 
443  Dot11Ack(const uint8_t* buffer, uint32_t total_sz);
444 
450  Dot11Ack* clone() const {
451  return new Dot11Ack(*this);
452  }
453 
458  PDUType pdu_type() const {
459  return pdu_flag;
460  }
461 
467  bool matches_flag(PDUType flag) const {
468  return flag == pdu_flag || Dot11Control::matches_flag(flag);
469  }
470 };
471 
475 class TINS_API Dot11BlockAckRequest : public Dot11ControlTA {
476 public:
480  static const PDU::PDUType pdu_flag = PDU::DOT11_BLOCK_ACK_REQ;
481 
491  Dot11BlockAckRequest(const address_type& dst_addr = address_type(),
492  const address_type& target_addr = address_type());
493 
508  Dot11BlockAckRequest(const uint8_t* buffer, uint32_t total_sz);
509 
510  /* Getter */
511 
517  #if TINS_IS_LITTLE_ENDIAN
518  return bar_control_ & 0xf;
519  #else
520  return (bar_control_ >> 8) & 0xf;
521  #endif
522  }
523 
529  #if TINS_IS_LITTLE_ENDIAN
530  return (start_sequence_ >> 4) & 0xfff;
531  #else
532  return (Endian::le_to_host<uint16_t>(start_sequence_) >> 4) & 0xfff;
533  #endif
534  }
535 
541  #if TINS_IS_LITTLE_ENDIAN
542  return start_sequence_ & 0xf;
543  #else
544  return (start_sequence_ >> 8) & 0xf;
545  #endif
546  }
547 
554  uint32_t header_size() const;
555 
556  /* Setter */
557 
562  void bar_control(small_uint<4> bar);
563 
568  void start_sequence(small_uint<12> seq);
569 
574  void fragment_number(small_uint<4> frag);
575 
582  return new Dot11BlockAckRequest(*this);
583  }
584 
589  PDUType pdu_type() const {
590  return pdu_flag;
591  }
592 
598  bool matches_flag(PDUType flag) const {
599  return flag == pdu_flag || Dot11Control::matches_flag(flag);
600  }
601 protected:
602  void write_ext_header(Memory::OutputMemoryStream& stream);
603 private:
604  uint16_t bar_control_;
605  uint16_t start_sequence_;
606 };
607 
611 class TINS_API Dot11BlockAck : public Dot11ControlTA {
612 public:
616  static const PDU::PDUType pdu_flag = PDU::DOT11_BLOCK_ACK;
617 
621  static const size_t bitmap_size = 8;
622 
632  Dot11BlockAck(const address_type& dst_addr = address_type(),
633  const address_type& target_addr = address_type());
634 
649  Dot11BlockAck(const uint8_t* buffer, uint32_t total_sz);
650 
651  /* Getters */
652 
658  #if TINS_IS_LITTLE_ENDIAN
659  return bar_control_ & 0xf;
660  #else
661  return (bar_control_ >> 8) & 0xf;
662  #endif
663  }
664 
670  #if TINS_IS_LITTLE_ENDIAN
671  return (start_sequence_ >> 4) & 0xfff;
672  #else
673  return (Endian::le_to_host<uint16_t>(start_sequence_) >> 4) & 0xfff;
674  #endif
675  }
676 
682  #if TINS_IS_LITTLE_ENDIAN
683  return start_sequence_ & 0xf;
684  #else
685  return (start_sequence_ >> 8) & 0xf;
686  #endif
687  }
688 
695  uint32_t header_size() const;
696 
697  /* Setters */
698 
703  void bar_control(small_uint<4> bar);
704 
709  void start_sequence(small_uint<12> seq);
710 
715  void fragment_number(small_uint<4> frag);
716 
724  const uint8_t* bitmap() const {
725  return bitmap_;
726  }
727 
732  void bitmap(const uint8_t* bit);
733 
738  PDUType pdu_type() const {
739  return pdu_flag;
740  }
741 
747  bool matches_flag(PDUType flag) const {
748  return flag == pdu_flag || Dot11Control::matches_flag(flag);
749  }
750 
756  Dot11BlockAck* clone() const {
757  return new Dot11BlockAck(*this);
758  }
759 private:
760  void write_ext_header(Memory::OutputMemoryStream& stream);
761 
762  uint16_t bar_control_, start_sequence_;
763  uint8_t bitmap_[bitmap_size];
764 };
765 
766 } // namespace Tins
767 
768 #endif // TINS_DOT11_DOT11_CONTROL_H
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:458
address_type target_addr() const
Getter for the target address field.
Definition: dot11_control.h:103
Dot11BlockAckRequest * clone() const
Clones this PDU.
Definition: dot11_control.h:581
small_uint< 4 > bar_control() const
Getter for the bar control field.
Definition: dot11_control.h:516
Dot11RTS * clone() const
Clones this PDU.
Definition: dot11_control.h:204
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:345
Class representing an 802.11 frame.
Definition: dot11_base.h:54
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:738
Dot11Ack * clone() const
Clones this PDU.
Definition: dot11_control.h:450
Dot11BlockAck * clone() const
Clones this PDU.
Definition: dot11_control.h:756
uint32_t controlta_size() const
Getter for the control ta additional fields size.
Definition: dot11_control.h:152
Class that represents an 802.11 block ack frame.
Definition: dot11_control.h:611
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:398
const uint8_t * bitmap() const
Getter for the bitmap field.
Definition: dot11_control.h:724
small_uint< 4 > fragment_number() const
Getter for the fragment number field.
Definition: dot11_control.h:681
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:283
Definition: dot11_base.h:535
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:89
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:407
Class that represents an abstraction of the 802.11 control frames that contain a target address...
Definition: dot11_control.h:98
small_uint< 12 > start_sequence() const
Getter for the start sequence field.
Definition: dot11_control.h:528
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:467
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_base.h:497
small_uint< 4 > fragment_number() const
Getter for the fragment number field.
Definition: dot11_control.h:540
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:274
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:589
Represents an IEEE 802.11 control frame.
Definition: dot11_control.h:43
small_uint< 12 > start_sequence() const
Getter for the start sequence field.
Definition: dot11_control.h:669
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:336
Dot11PSPoll * clone() const
Clones this PDU.
Definition: dot11_control.h:266
The Tins namespace.
Definition: address_range.h:38
IEEE 802.11 RTS frame.
Definition: dot11_control.h:165
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:221
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:212
Dot11EndCFAck * clone() const
Clones this PDU.
Definition: dot11_control.h:390
small_uint< 4 > bar_control() const
Getter for the bar control field.
Definition: dot11_control.h:657
Class that represents an 802.11 Block Ack Request PDU.
Definition: dot11_control.h:475
Definition: dot11_control.h:412
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot11_control.h:80
Definition: dot11_control.h:350
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:598
bool matches_flag(PDUType flag) const
Check whether this PDU matches the specified flag.
Definition: dot11_control.h:747
Definition: dot11_control.h:226
Dot11CFEnd * clone() const
Clones this PDU.
Definition: dot11_control.h:328
Definition: dot11_control.h:288