Class: PacketGen::Header::Dot11 Abstract
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::Dot11
- Defined in:
- lib/packetgen/header/dot11.rb,
lib/packetgen/header/dot11/data.rb,
lib/packetgen/header/dot11/control.rb,
lib/packetgen/header/dot11/element.rb,
lib/packetgen/header/dot11/sub_mngt.rb,
lib/packetgen/header/dot11/management.rb
Overview
This is a base class to demultiplex different IEEE 802.11 frames when parsing.
IEEE 802.11 header A IEEE 802.11 header may consist of at least:
-
a #frame_ctrl (Types::Int16),
-
a #id/duration (Types::Int16le),
-
and a #mac1 (Eth::MacAddr).
Depending on frame type and subtype, it may also contains:
-
a #mac2 (Eth::MacAddr),
-
a #mac3 (Eth::MacAddr),
-
a #mac4 (Eth::MacAddr),
-
a #qos_ctrl (Types::Int16),
-
a #ht_ctrl (Types::Int32),
-
a #body (a Types::String or another Base class),
-
a Frame check sequence (#fcs, of type Types::Int32le)
Header accessors
As Dot11 header types are defined under Dot11 namespace, Dot11 header accessors have a specific name. By example, to access to a Beacon header, accessor is #dot11_beacon
.
Create Dot11 packets
As Dot11 is an abstract class, you have to use one of its subclasses to instanciate a IEEE802.11 header.
IEEE802.11 control frames
Control frames may be created this way:
pkt = PacketGen.gen('Dot11::Control', subtype: 13) # Ack control frame
pkt.dot11_control # => PacketGen::Header::Dot11::Control
# #dot11 is a shortcut for #dot11_control
pkt.dot11 # => PacketGen::Header::Dot11::Control
IEEE802.11 management frames
Management frames may be created this way:
pkt = PacketGen.gen('Dot11::Management')
pkt.dot11_management # => PacketGen::Header::Dot11::Management
# #dot11 is a shortcut for #dot11_management
pkt.dot11 # => PacketGen::Header::Dot11::Management
Management frames are usually specialized, AssociationRequest by example:
pkt.add('Dot11::AssoReq')
pkt.dot11_assoreq # => PacketGen::Header::Dot11::AssoReq
Management frames also may contain some elements (see IEEE 802.11 standard):
pkt.dot11_assoreq.add_elements(type: 'SSID', value: "My SSID")
pkt.dot11_assoreq.add_elements(type: 'Rates', value: supported_rates)
IEEE802.11 data frames
Data frames may be created this way:
pkt = PacketGen.gen('Dot11::Data')
pkt.dot11_data # => PacketGen::Header::Dot11::Data
# #dot11 is a shortcut for #dot11_data
pkt.dot11 # => PacketGen::Header::Dot11::Data
Parse Dot11 packets
When parsing a Dot11 packet, Dot11 subclass is created from type
value. Dot11 header should then be accessed through Packet#dot11, whatever the frame type is. But, specialized methods also exist: by example, for a control frame, Packet#dot11_control may also be used.
Send Dot11 packets
To send a Dot11 packet, a RadioTap header is needed:
pkt = PacketGen.gen('RadioTap')
pkt.add('Dot11::Management', mac1: client, mac2: bssid, mac3: bssid)
pkt.add('Dot11::Beacon')
pkt.dot11_beacon.add_element(type: 'SSID', value: 'My SSID')
pkt.dot11_beacon.add_element(type: 'Rates', value: "\x85\x0c")
pkt.calc
pkt.to_w('wlan0')
Direct Known Subclasses
Defined Under Namespace
Classes: ATIM, ArrayOfElements, AssoReq, AssoResp, Auth, Beacon, Control, Data, DeAuth, Disas, Element, Management, ProbeReq, ProbeResp, ReAssoReq, ReAssoResp, SubMngt
Constant Summary collapse
- TYPES =
Frame types
%w[Management Control Data Reserved].freeze
Class Attribute Summary collapse
-
.fcs ⇒ Boolean
(also: fcs?)
Set a flag for parsing Dot11 packets.
Instance Attribute Summary collapse
- #body ⇒ Types::String
- #fcs ⇒ Types::Int32le
- #fragment_number ⇒ Integer readonly
-
#frame_ctrl ⇒ Integer
16-bit frame control word.
-
#from_ds ⇒ Boolean
From_ds flag from #frame_ctrl.
-
#ht_ctrl ⇒ Integer
16-bit HT control word.
-
#id ⇒ Integer
(also: #duration)
16-bit ID/Duration word.
- #mac1 ⇒ Eth::MacAddr
- #mac2 ⇒ Eth::MacAddr
- #mac3 ⇒ Eth::MacAddr
- #mac4 ⇒ Eth::MacAddr
-
#md ⇒ Boolean
Md flag from #frame_ctrl.
-
#mf ⇒ Boolean
Mf flag from #frame_ctrl.
-
#order ⇒ Boolean
Order flag from #frame_ctrl.
-
#proto_version ⇒ Integer
2-bit protocol version from #frame_ctrl.
-
#pwmngt ⇒ Boolean
Pwmngt flag from #frame_ctrl.
-
#qos_ctrl ⇒ Integer
16-bit QoS control word.
-
#retry ⇒ Boolean
Retry flag from #frame_ctrl.
-
#sequence_ctrl ⇒ Integer
16-bit sequence control word.
- #sequence_number ⇒ Integer readonly
-
#subtype ⇒ Integer
4-bit frame subtype from #frame_ctrl.
-
#to_ds ⇒ Boolean
To_ds flag from #frame_ctrl.
-
#type ⇒ Integer
2-bit frame type from #frame_ctrl.
-
#wep ⇒ Boolean
Wep flag from #frame_ctrl.
Instance Method Summary collapse
-
#added_to_packet(packet) ⇒ void
Callback called when a Dot11 header is added to a packet Here, add
#dot11
method as a shortcut to existing #dot11_(control|management|data). -
#calc_checksum ⇒ Integer
Compute checksum and set
fcs
field. -
#fields ⇒ Array<Symbol>
Get all used field names.
-
#human_type ⇒ String
Get human readable type.
-
#initialize(options = {}) ⇒ Dot11
constructor
A new instance of Dot11.
- #inspect ⇒ String
- #old_fields ⇒ Object
- #old_read ⇒ Object
-
#read(str) ⇒ Dot11
Populate object from a binary string.
- #to_s ⇒ String
-
#to_w(iface) ⇒ void
send Dot11 packet on wire.
Methods inherited from Base
bind, calculate_and_set_length, #header_id, inherited, #ip_header, #ll_header
Methods included from PacketGen::Headerable
included, #method_name, #packet, #packet=, #parse?, #protocol_name
Methods inherited from Types::Fields
#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, fields, inherited, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, update_field
Constructor Details
#initialize(options = {}) ⇒ Dot11
Returns a new instance of Dot11.
263 264 265 266 |
# File 'lib/packetgen/header/dot11.rb', line 263 def initialize(={}) super @applicable_fields = old_fields end |
Class Attribute Details
.fcs ⇒ Boolean Also known as: fcs?
Set a flag for parsing Dot11 packets. If set to true
, parse FCS field, else don’t. Default is true
.
185 186 187 |
# File 'lib/packetgen/header/dot11.rb', line 185 def fcs @fcs end |
Instance Attribute Details
#body ⇒ Types::String
219 |
# File 'lib/packetgen/header/dot11.rb', line 219 define_field :body, Types::String |
#fcs ⇒ Types::Int32le
222 |
# File 'lib/packetgen/header/dot11.rb', line 222 define_field :fcs, Types::Int32le |
#fragment_number ⇒ Integer (readonly)
255 |
# File 'lib/packetgen/header/dot11.rb', line 255 define_bit_fields_on :sequence_ctrl, :sequence_number, 12, :fragment_number, 4 |
#frame_ctrl ⇒ Integer
Returns 16-bit frame control word.
192 |
# File 'lib/packetgen/header/dot11.rb', line 192 define_field :frame_ctrl, Types::Int16, default: 0 |
#from_ds ⇒ Boolean
Returns from_ds flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#ht_ctrl ⇒ Integer
Returns 16-bit HT control word.
216 |
# File 'lib/packetgen/header/dot11.rb', line 216 define_field :ht_ctrl, Types::Int32 |
#id ⇒ Integer Also known as: duration
Returns 16-bit ID/Duration word.
195 |
# File 'lib/packetgen/header/dot11.rb', line 195 define_field :id, Types::Int16le, default: 0 |
#mac1 ⇒ Eth::MacAddr
198 |
# File 'lib/packetgen/header/dot11.rb', line 198 define_field :mac1, Eth::MacAddr |
#mac2 ⇒ Eth::MacAddr
201 |
# File 'lib/packetgen/header/dot11.rb', line 201 define_field :mac2, Eth::MacAddr |
#mac3 ⇒ Eth::MacAddr
204 |
# File 'lib/packetgen/header/dot11.rb', line 204 define_field :mac3, Eth::MacAddr |
#mac4 ⇒ Eth::MacAddr
210 |
# File 'lib/packetgen/header/dot11.rb', line 210 define_field :mac4, Eth::MacAddr |
#md ⇒ Boolean
Returns md flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#mf ⇒ Boolean
Returns mf flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#order ⇒ Boolean
Returns order flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#proto_version ⇒ Integer
Returns 2-bit protocol version from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#pwmngt ⇒ Boolean
Returns pwmngt flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#qos_ctrl ⇒ Integer
Returns 16-bit QoS control word.
213 |
# File 'lib/packetgen/header/dot11.rb', line 213 define_field :qos_ctrl, Types::Int16 |
#retry ⇒ Boolean
Returns retry flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#sequence_ctrl ⇒ Integer
Returns 16-bit sequence control word.
207 |
# File 'lib/packetgen/header/dot11.rb', line 207 define_field :sequence_ctrl, Types::Int16le, default: 0 |
#sequence_number ⇒ Integer (readonly)
255 |
# File 'lib/packetgen/header/dot11.rb', line 255 define_bit_fields_on :sequence_ctrl, :sequence_number, 12, :fragment_number, 4 |
#subtype ⇒ Integer
Returns 4-bit frame subtype from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#to_ds ⇒ Boolean
Returns to_ds flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#type ⇒ Integer
Returns 2-bit frame type from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
#wep ⇒ Boolean
Returns wep flag from #frame_ctrl.
246 247 |
# File 'lib/packetgen/header/dot11.rb', line 246 define_bit_fields_on :frame_ctrl, :subtype, 4, :type, 2, :proto_version, 2, :order, :wep, :md, :pwmngt, :retry, :mf, :from_ds, :to_ds |
Instance Method Details
#added_to_packet(packet) ⇒ void
This method returns an undefined value.
Callback called when a Dot11 header is added to a packet Here, add #dot11
method as a shortcut to existing #dot11_(control|management|data).
356 357 358 359 360 |
# File 'lib/packetgen/header/dot11.rb', line 356 def added_to_packet(packet) return if packet.respond_to? :dot11 packet.instance_eval("def dot11(arg=nil); header(#{self.class}, arg); end") # def dot11(arg=nil); header(Dot11, arg); end end |
#calc_checksum ⇒ Integer
Compute checksum and set fcs
field
307 308 309 310 311 |
# File 'lib/packetgen/header/dot11.rb', line 307 def calc_checksum fcs = Zlib.crc32(to_s[0...-4]) self.fcs = fcs fcs end |
#fields ⇒ Array<Symbol>
Get all used field names
270 271 272 |
# File 'lib/packetgen/header/dot11.rb', line 270 def fields @applicable_fields end |
#human_type ⇒ String
Get human readable type
321 322 323 |
# File 'lib/packetgen/header/dot11.rb', line 321 def human_type TYPES[type] end |
#inspect ⇒ String
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/packetgen/header/dot11.rb', line 326 def inspect str = if self.instance_of? Dot11 Inspect.dashed_line("#{self.class} #{human_type}", 1) elsif self.respond_to? :human_subtype Inspect.dashed_line("#{self.class} #{human_subtype}", 1) else Inspect.dashed_line(self.class.to_s, 1) end define_applicable_fields @applicable_fields.each do |attr| next if attr == :body str << Inspect.inspect_attribute(attr, @fields[attr], 1) end str end |
#old_fields ⇒ Object
259 |
# File 'lib/packetgen/header/dot11.rb', line 259 alias old_fields fields |
#old_read ⇒ Object
275 |
# File 'lib/packetgen/header/dot11.rb', line 275 alias old_read read |
#read(str) ⇒ Dot11
Populate object from a binary string
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/packetgen/header/dot11.rb', line 281 def read(str) fcs = Dot11.fcs? if self.instance_of? Dot11 return self if str.nil? force_binary str self[:frame_ctrl].read str[0, 2] case type when 0 Dot11::Management.new.read str when 1 Dot11::Control.new.read str when 2 Dot11::Data.new.read str else private_read str, fcs end else private_read str, fcs end end |
#to_s ⇒ String
314 315 316 317 |
# File 'lib/packetgen/header/dot11.rb', line 314 def to_s define_applicable_fields @applicable_fields.map { |f| force_binary @fields[f].to_s }.join end |