Class: PacketGen::Header::OSPFv3

Inherits:
Base
  • Object
show all
Defined in:
lib/packetgen/header/ospfv3.rb,
lib/packetgen/header/ospfv3/lsa.rb,
lib/packetgen/header/ospfv3/hello.rb,
lib/packetgen/header/ospfv3/ls_ack.rb,
lib/packetgen/header/ospfv3/ls_update.rb,
lib/packetgen/header/ospfv3/ls_request.rb,
lib/packetgen/header/ospfv3/lsa_header.rb,
lib/packetgen/header/ospfv3/ipv6_prefix.rb,
lib/packetgen/header/ospfv3/db_description.rb

Overview

This class supports OSPFv3 (RFC 5340). A OSPFv3 header has the following format:

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 #   |     Type      |         Packet length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Router ID                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                          Area ID                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Checksum             |  Instance ID  |      0        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

An OSPFv3 header consists of:

OSPFv3 body

OSPFv3 #body should contain OSPF payload for given #type:

Examples:

Create an OSPFv3 header

# standalone
ospf = PacketGen::Header::OSPFv3.new
# in a packet
pkt = PacketGen.gen('IPv6').add('OSPFv3')
# make IPv6 header correct for OSPF
pkt.ospfize
# or make it correct with specific destination address
pkt.ospfize(dst: :all_spf_routers)
# access to OSPF header
pkt.ospfv3.class    # => PacketGen::Header::OSPFv3

OSPFv3 attributes

ospf = PacketGen::Header::OSPFv3.new
ospf.version              # => 3
ospf.type = 'LS_ACK'      # or 5
ospf.length = 154
ospf.router_id = 0xc0a80001
ospf.area_id = 1
ospf.checksum = 0xabcd
ospf.instance_id = 0

Author:

  • Sylvain Daubert

  • LemonTree55

Since:

  • 2.5.0

Defined Under Namespace

Classes: ArrayOfIPv6Prefix, ArrayOfLSA, ArrayOfLSR, ArrayOfLink, DbDescription, Hello, IPv6Prefix, LSA, LSAHeader, LSAIntraAreaPrefix, LSALink, LSANetwork, LSARouter, LSAck, LSR, LSRequest, LSUpdate, Link

Constant Summary collapse

IP_PROTOCOL =

IP protocol number for OSPF

Since:

  • 2.5.0

OSPFv2::IP_PROTOCOL
TYPES =

OSPF packet types

Since:

  • 2.5.0

OSPFv2::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from PacketGen::Headerable

included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read, #to_s

Constructor Details

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

Instance Attribute Details

#area_idInteger

32-bit area ID

Returns:

  • (Integer)

95
# File 'lib/packetgen/header/ospfv3.rb', line 95

define_attr :area_id, BinStruct::Int32

#bodyString, Headerable

OSPFv3 body

Returns:


111
# File 'lib/packetgen/header/ospfv3.rb', line 111

define_attr :body, BinStruct::String

#checksumInteger

16-bit OSPF packet checksum

Returns:

  • (Integer)

99
# File 'lib/packetgen/header/ospfv3.rb', line 99

define_attr :checksum, BinStruct::Int16

#instance_idInteger

8-bit instance ID.

Returns:

  • (Integer)

103
# File 'lib/packetgen/header/ospfv3.rb', line 103

define_attr :instance_id, BinStruct::Int8

#lengthInteger

16-bit OSPF packet length. Header is included in length

Returns:

  • (Integer)

87
# File 'lib/packetgen/header/ospfv3.rb', line 87

define_attr :length, BinStruct::Int16

#reservedInteger

8-bit reserved field.

Returns:

  • (Integer)

107
# File 'lib/packetgen/header/ospfv3.rb', line 107

define_attr :reserved, BinStruct::Int8, default: 0

#router_idInteger

32-bit router ID

Returns:

  • (Integer)

91
# File 'lib/packetgen/header/ospfv3.rb', line 91

define_attr :router_id, BinStruct::Int32

#typeInteger

8-bit OSPF packet type. Types are defined in TYPES.

Returns:

  • (Integer)

83
# File 'lib/packetgen/header/ospfv3.rb', line 83

define_attr :type, BinStruct::Int8Enum, enum: TYPES

#versionInteger

8-bit OSPF version

Returns:

  • (Integer)

79
# File 'lib/packetgen/header/ospfv3.rb', line 79

define_attr :version, BinStruct::Int8, default: 3

Class Method Details

.define_options(hdr) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Helper class method to define an OSPFv3 options field.

Parameters:

  • hdr (Base)

    header on which define a OSPFv3 options field

Since:

  • 2.5.0


141
142
143
# File 'lib/packetgen/header/ospfv3.rb', line 141

def self.define_options(hdr)
  hdr.define_bit_attr :options, z: 18, dc_opt: 1, r_opt: 1, n_opt: 1, x_opt: 1, e_opt: 1, v6_opt: 1
end

Instance Method Details

#added_to_packet(packet) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This method is used internally by PacketGen and should not be directly called

Add #ospfize method to packet. This method calls #ospfize.

Since:

  • 2.5.0


149
150
151
152
# File 'lib/packetgen/header/ospfv3.rb', line 149

def added_to_packet(packet)
  ospf_idx = packet.headers.size
  packet.instance_eval "def ospfize(**kwargs) @headers[#{ospf_idx}].ospfize(**kwargs); end" # def ospfize(**kwargs) @headers[2].ospfize(**kwargs); end
end

#calc_checksumInteger

Compute checksum and set #checksum attribute

Returns:

  • (Integer)

Since:

  • 2.5.0


156
157
158
159
160
161
162
163
# File 'lib/packetgen/header/ospfv3.rb', line 156

def calc_checksum
  ipv6 = ip_header(self)
  sum = ipv6.pseudo_header_checksum
  sum += IP_PROTOCOL
  sum += self.sz
  sum += IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)
end

#calc_lengthInteger

Compute length and set #length attribute

Returns:

  • (Integer)

Since:

  • 2.5.0


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

def calc_length
  self[:length].value = Base.calculate_and_set_length(self)
end

#human_typeString

Get human-readable type

Returns:

  • (String)

Since:

  • 2.5.0


167
168
169
# File 'lib/packetgen/header/ospfv3.rb', line 167

def human_type
  self[:type].to_human
end

#ospfize(dst: nil) ⇒ void

This method returns an undefined value.

Fixup IPv6 header according to RFC 5340:

  • set Traffic Class field to 0xc0,

  • optionally set destination address,

  • set Hop-limit to 1 if destination is a mcast address.

This method may be called as:

# first way
pkt.ospfv3.ospfize
# second way
pkt.ospfize

Parameters:

  • dst (String, Symbol, nil) (defaults to: nil)

    destination address. May be a coloned IP address (by example 1::1234:5) or a Symbol (:all_spf_routers or :all_d_routers)

Since:

  • 2.5.0


190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/packetgen/header/ospfv3.rb', line 190

def ospfize(dst: nil)
  ipv6 = ip_header(self)
  ipv6.traffic_class = 0xc0
  dst = case dst
        when :all_spf_routers
          'ff02::5'
        when :all_d_routers
          'ff02::6'
        else
          dst
        end
  ipv6.dst = dst unless dst.nil?
  ipv6.hop = 1 if ipv6[:dst].mcast?
end