Class: PacketGen::Header::IPv6

Inherits:
Base show all
Defined in:
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6/addr.rb,
lib/packetgen/header/ipv6/extension.rb,
lib/packetgen/header/ipv6/hop_by_hop.rb

Overview

IPv6 (RFC 8200)

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| Traffic Class |           Flow Label                  |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Payload Length        |  Next Header  |   Hop Limit   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                                                               +
|                                                               |
+                         Source Address                        +
|                                                               |
+                                                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                                                               +
|                                                               |
+                      Destination Address                      +
|                                                               |
+                                                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

A IPv6 header consists of:

Create a IPv6 header

# standalone
ipv6 = PacketGen::Header::IPv6.new
# in a packet
pkt = PacketGen.gen('IPv6')
# access to IPv6 header
pkt.ipv6   # => PacketGen::Header::IPv6

IPv6 attributes

ipv6.u32 = 0x60280001
# the same as
ipv6.version = 6
ipv6.traffic_class = 2
ipv6.flow_label = 0x80001

ipv6.length = 0x43
ipv6.hop = 0x40
ipv6.next = 6
ipv6.src = '::1'
ipv6.src                # => "::1"
ipv6[:src]              # => PacketGen::Header::IPv6::Addr
ipv6.dst = '2001:1234:5678:abcd::123'
ipv6.body.read 'this is a body'

Add IPv6 extensions

In IPv6, optional extensions are encoded in separate headers that may be placed between the IPv6 header and the upper-layer header.

In PacketGen, a IPv6 extension is processedf as a classical header:

pkt = PacketGen.gen('IPv6')
# Add a HopByHop extension
pkt.add('IPv6::HopByHop')
pkt.ipv6_hopbyhop.options << { type: 'router_alert', value: [0].pack('n') }
# Add another header
pkt.add('UDP')

Author:

  • Sylvain Daubert

Defined Under Namespace

Classes: Addr, ArrayOfAddr, Extension, HopByHop, Option, Options, Pad1

Constant Summary collapse

ETHERTYPE =

IPv6 Ether type

0x86dd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #protocol_name, #read

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, fields, #fields, inherited, #initialize, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

This class inherits a constructor from PacketGen::Header::Base

Instance Attribute Details

#bodyTypes::String, Header::Base


120
# File 'lib/packetgen/header/ipv6.rb', line 120

define_field :body, Types::String

#dstAddr

IPv6 destination address

Returns:


117
# File 'lib/packetgen/header/ipv6.rb', line 117

define_field :dst, Addr, default: '::1'

#flow_labelInteger

Returns 20-bit flow_label attribute.

Returns:

  • (Integer)

    20-bit flow_label attribute


128
# File 'lib/packetgen/header/ipv6.rb', line 128

define_bit_fields_on :u32, :version, 4, :traffic_class, 8, :flow_label, 20

#hopInteger

8-bit IPv6 hop limit

Returns:

  • (Integer)

109
# File 'lib/packetgen/header/ipv6.rb', line 109

define_field :hop, Types::Int8, default: 64

#lengthInteger

16-bit word of IPv6 payload length

Returns:

  • (Integer)

101
# File 'lib/packetgen/header/ipv6.rb', line 101

define_field :length, Types::Int16

#nextInteger

8-bit IPv6 next payload value

Returns:

  • (Integer)

105
# File 'lib/packetgen/header/ipv6.rb', line 105

define_field :next, Types::Int8

#srcAddr

IPv6 source address

Returns:


113
# File 'lib/packetgen/header/ipv6.rb', line 113

define_field :src, Addr, default: '::1'

#traffic_classInteger

Returns 8-bit traffic_class attribute.

Returns:

  • (Integer)

    8-bit traffic_class attribute


128
# File 'lib/packetgen/header/ipv6.rb', line 128

define_bit_fields_on :u32, :version, 4, :traffic_class, 8, :flow_label, 20

#u32Integer

First 32-bit word of IPv6 header

Returns:

  • (Integer)

97
# File 'lib/packetgen/header/ipv6.rb', line 97

define_field :u32, Types::Int32, default: 0x6000_0000

#versionInteger

Returns 4-bit version attribute.

Returns:

  • (Integer)

    4-bit version attribute


128
# File 'lib/packetgen/header/ipv6.rb', line 128

define_bit_fields_on :u32, :version, 4, :traffic_class, 8, :flow_label, 20

Class Method Details

.bind(header_klass, args = {}) ⇒ Object

Bind a upper header to IPv6 and its defined extension headers.

See Also:


205
206
207
208
209
210
# File 'lib/packetgen/header/ipv6.rb', line 205

def bind(header_klass, args={})
  IPv6.old_bind header_klass, args
  [IPv6::HopByHop].each do |klass|
    klass.bind header_klass, args
  end
end

.old_bindObject


201
# File 'lib/packetgen/header/ipv6.rb', line 201

alias old_bind bind

Instance Method Details

#calc_lengthInteger

Compute length and set len field

Returns:

  • (Integer)

132
133
134
# File 'lib/packetgen/header/ipv6.rb', line 132

def calc_length
  Base.calculate_and_set_length self, header_in_size: false
end

#inspectString

Returns:

  • (String)

157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/packetgen/header/ipv6.rb', line 157

def inspect
  super do |attr|
    next unless attr == :u32

    str = Inspect.inspect_attribute(attr, self[attr])
    shift = Inspect.shift_level
    str << shift + Inspect::FMT_ATTR % ['', 'version', version]
    tclass = Inspect.int_dec_hex(traffic_class, 2)
    str << shift + Inspect::FMT_ATTR % ['', 'tclass', tclass]
    fl_value = Inspect.int_dec_hex(flow_label, 5)
    str << shift + Inspect::FMT_ATTR % ['', 'flow_label', fl_value]
  end
end

#parse?Boolean

Check version field

Returns:

  • (Boolean)

See Also:

  • PacketGen::Header::IPv6.[Base[Base#parse?]

173
174
175
# File 'lib/packetgen/header/ipv6.rb', line 173

def parse?
  version == 6
end

#pseudo_header_checksumInteger

Get IPv6 part of pseudo header checksum.

Returns:

  • (Integer)

138
139
140
141
142
143
# File 'lib/packetgen/header/ipv6.rb', line 138

def pseudo_header_checksum
  sum = 0
  self[:src].to_a.each { |word| sum += word.to_i }
  self[:dst].to_a.each { |word| sum += word.to_i }
  sum
end

#reply!self

Invert source and destination addresses

Returns:

  • (self)

Since:

  • 2.7.0


180
181
182
183
# File 'lib/packetgen/header/ipv6.rb', line 180

def reply!
  self[:src], self[:dst] = self[:dst], self[:src]
  self
end

#to_w(_iface = nil) ⇒ void

This method returns an undefined value.

Send IPv6 packet on wire. All fields may be set (even #version).

Parameters:

  • _iface (String) (defaults to: nil)

    interface name (not used)

Since:

  • 3.0.0 no more limitations on flow_label, length and src fields.


149
150
151
152
153
154
# File 'lib/packetgen/header/ipv6.rb', line 149

def to_w(_iface=nil)
  sock = Socket.new(Socket::AF_INET6, Socket::SOCK_RAW, Socket::IPPROTO_RAW)
  sockaddrin = Socket.sockaddr_in(0, dst)
  sock.send to_s, 0, sockaddrin
  sock.close
end