Class: PacketGen::Header::Dot11 Abstract

Inherits:
Base
  • Object
show all
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 class is abstract.

This is a base class to demultiplex different IEEE 802.11 frames when parsing.

A IEEE 802.11 header may consist of at least:

Depending on frame type and subtype, it may also contains:

Header accessors

As Dot11 header types are defined under Dot11 namespace, Dot11 header accessors have a specific name. For 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 (see examples below).

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 or PPI header is needed (depending on your hardware):

pkt = PacketGen.gen('RadioTap')
pkt.add('Dot11::Management', mac1: client, mac2: bssid, mac3: bssid)
pkt.add('Dot11::Beacon')
pkt.dot11_management.add_element(type: 'SSID', value: 'My SSID')
pkt.dot11_management.add_element(type: 'Rates', value: "\x85\x0c")
pkt.to_w('wlan0')

Examples:

Create IEEE802.11 control frames

pkt = PacketGen.gen('Dot11::Control', subtype: 13) # Ack control frame
pkt.dot11_control.class   # => PacketGen::Header::Dot11::Control
# #dot11 is a shortcut for #dot11_control
pkt.dot11.class           # => PacketGen::Header::Dot11::Control

Create IEEE802.11 management frames

pkt = PacketGen.gen('Dot11::Management')
pkt.dot11_management.class   # => PacketGen::Header::Dot11::Management
# #dot11 is a shortcut for #dot11_management
pkt.dot11.class              # => PacketGen::Header::Dot11::Management
# Management frames are usually specialized, AssociationRequest by example
pkt.add('Dot11::AssoReq')
pkt.dot11_assoreq .class     # => PacketGen::Header::Dot11::AssoReq
# Management frames also may contain some elements (see IEEE 802.11 standard)
pkt.dot11_assoreq.elements << { type: 'SSID', value: "My SSID" }
pkt.dot11_assoreq.elements << { type: 'Rates', value: "\x8c\x12\x98\x24\xb0" }

Create IEEE802.11 data frames

pkt = PacketGen.gen('Dot11::Data')
pkt.dot11_data.class   # => PacketGen::Header::Dot11::Data
# #dot11 is a shortcut for #dot11_data
pkt.dot11.class        # => PacketGen::Header::Dot11::Data

Author:

  • Sylvain Daubert

Since:

  • 1.4.0

Direct Known Subclasses

Control, Data, Management

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

Since:

  • 1.4.0

%w[Management Control Data Reserved].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Constructor Details

#initialize(options = {}) ⇒ Dot11

Returns a new instance of Dot11.

Parameters:

  • options (Hash) (defaults to: {})

See Also:

Since:

  • 1.4.0


257
258
259
260
# File 'lib/packetgen/header/dot11.rb', line 257

def initialize(options={})
  super
  @applicable_attributes = old_attributes
end

Class Attribute Details

.fcsBoolean Also known as: fcs?

Set a flag for parsing Dot11 packets. If set to true, parse FCS field, else don’t. Default is true.

Returns:

  • (Boolean)

Since:

  • 1.4.0


179
180
181
# File 'lib/packetgen/header/dot11.rb', line 179

def fcs
  @fcs
end

Instance Attribute Details

#bodyBinStruct::String

Dot11 body, if any

Returns:

  • (BinStruct::String)

245
# File 'lib/packetgen/header/dot11.rb', line 245

define_attr :body, BinStruct::String

#fcsBinStruct::Int32le

Checksum of the Dot11 frame.

Returns:

  • (BinStruct::Int32le)

249
# File 'lib/packetgen/header/dot11.rb', line 249

define_attr :fcs, BinStruct::Int32le

#fragment_numberInteger

4-bit attribute from #sequence_ctrl

Returns:

  • (Integer)

Since:

  • 2.1.3


232
# File 'lib/packetgen/header/dot11.rb', line 232

define_bit_attr :sequence_ctrl, sequence_number: 12, fragment_number: 4

#frame_ctrlInteger

Returns 16-bit frame control word.

Returns:

  • (Integer)

    16-bit frame control word


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#from_dsBoolean

Returns from_ds flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#ht_ctrlInteger

Returns 16-bit HT control word.

Returns:

  • (Integer)

    16-bit HT control word


241
# File 'lib/packetgen/header/dot11.rb', line 241

define_attr :ht_ctrl, BinStruct::Int32

#idInteger Also known as: duration

Returns 16-bit ID/Duration word.

Returns:

  • (Integer)

    16-bit ID/Duration word


212
# File 'lib/packetgen/header/dot11.rb', line 212

define_attr :id, BinStruct::Int16le, default: 0

#mac1Eth::MacAddr

Returns:


215
# File 'lib/packetgen/header/dot11.rb', line 215

define_attr :mac1, Eth::MacAddr

#mac2Eth::MacAddr

Returns:


218
# File 'lib/packetgen/header/dot11.rb', line 218

define_attr :mac2, Eth::MacAddr

#mac3Eth::MacAddr

Returns:


221
# File 'lib/packetgen/header/dot11.rb', line 221

define_attr :mac3, Eth::MacAddr

#mac4Eth::MacAddr

Returns:


235
# File 'lib/packetgen/header/dot11.rb', line 235

define_attr :mac4, Eth::MacAddr

#mdBoolean

Returns md flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#mfBoolean

Returns mf flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#orderBoolean

Returns order flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#proto_versionInteger

Returns 2-bit protocol version from #frame_ctrl.

Returns:

  • (Integer)

    2-bit protocol version from #frame_ctrl


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#pwmngtBoolean

Returns pwmngt flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#qos_ctrlInteger

Returns 16-bit QoS control word.

Returns:

  • (Integer)

    16-bit QoS control word


238
# File 'lib/packetgen/header/dot11.rb', line 238

define_attr :qos_ctrl, BinStruct::Int16

#retryBoolean

Returns retry flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#sequence_ctrlInteger

Returns 16-bit sequence control word.

Returns:

  • (Integer)

    16-bit sequence control word


232
# File 'lib/packetgen/header/dot11.rb', line 232

define_bit_attr :sequence_ctrl, sequence_number: 12, fragment_number: 4

#sequence_numberInteger

12-bit field from #sequence_ctrl

Returns:

  • (Integer)

Since:

  • 2.1.3


232
# File 'lib/packetgen/header/dot11.rb', line 232

define_bit_attr :sequence_ctrl, sequence_number: 12, fragment_number: 4

#subtypeInteger

Returns 4-bit frame subtype from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#to_dsBoolean

Returns to_ds flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#typeInteger

Returns 2-bit frame type from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

#wepBoolean

Returns wep flag from #frame_ctrl.

Returns:


208
209
# File 'lib/packetgen/header/dot11.rb', line 208

define_bit_attr :frame_ctrl, subtype: 4, type: 2, proto_version: 2, order: 1,
wep: 1, md: 1, pwmngt: 1, retry: 1, mf: 1, from_ds: 1, to_ds: 1

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).

Parameters:

Since:

  • 1.4.0


351
352
353
354
355
# File 'lib/packetgen/header/dot11.rb', line 351

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

#attributesArray<Symbol>

Get all used attribute names

Returns:

  • (Array<Symbol>)

Since:

  • 1.4.0


264
265
266
# File 'lib/packetgen/header/dot11.rb', line 264

def attributes
  @applicable_attributes
end

#calc_checksumInteger

Compute checksum and set fcs field

Returns:

  • (Integer)

Since:

  • 1.4.0


301
302
303
304
305
# File 'lib/packetgen/header/dot11.rb', line 301

def calc_checksum
  fcs = Zlib.crc32(to_s[0...-4])
  self.fcs = fcs
  fcs
end

#human_typeString

Get human readable type

Returns:

  • (String)

Since:

  • 1.4.0


316
317
318
# File 'lib/packetgen/header/dot11.rb', line 316

def human_type
  TYPES[type]
end

#inspectString

Returns:

  • (String)

Since:

  • 1.4.0


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/packetgen/header/dot11.rb', line 321

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_attributes
  @applicable_attributes.each do |attr|
    next if attr == :body

    str << Inspect.inspect_attribute(attr, @attributes[attr], 1)
  end
  str
end

#old_attributesObject

Since:

  • 1.4.0


253
# File 'lib/packetgen/header/dot11.rb', line 253

alias old_attributes attributes

#old_readObject

Since:

  • 1.4.0


269
# File 'lib/packetgen/header/dot11.rb', line 269

alias old_read read

#read(str) ⇒ Dot11

Populate object from a binary string

Parameters:

  • str (String)

Returns:

  • (Dot11)

    may return a subclass object if a more specific class may be determined

Since:

  • 1.4.0


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/packetgen/header/dot11.rb', line 275

def read(str)
  fcs = Dot11.fcs?

  if self.instance_of?(Dot11)
    return self if str.nil?

    str = str.b unless str.encoding == Encoding::BINARY
    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_sString

Generate binary string

Returns:

  • (String)

Since:

  • 1.4.0


309
310
311
312
# File 'lib/packetgen/header/dot11.rb', line 309

def to_s
  define_applicable_attributes
  @applicable_attributes.map { |f| @attributes[f].to_s.b }.join
end

#to_w(iface) ⇒ void

This method returns an undefined value.

send Dot11 packet on wire.

Parameters:

  • iface (String)

    interface name

Since:

  • 1.4.0


342
343
344
# File 'lib/packetgen/header/dot11.rb', line 342

def to_w(iface)
  Inject.inject(iface: iface, data: self)
end