class PacketGen::Header::OSPFv3

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:

Create an OSPFv3 header

# standalone
ospf = PacketGen::Header::OSPFv3.new
# in a packet
pkt = PacketGen.gen('IPv6', src: source_ip).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    # => PacketGen::Header::OSPFv3

OSPFv3 attributes

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

OSPFv3 body

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

@author Sylvain Daubert @since 2.5.0

Constants

IP_PROTOCOL

IP protocol number for OSPF

TYPES

OSPF packet types

Public Class Methods

define_options(hdr) click to toggle source

@api private Helper class method to define an OSPFv3 options field. @param [Base] hdr header on which define a OSPFv3 options field @return [void] @!macro [attach] define_ospfv3_options

@!attribute options
  24-bit options field. Handle {#v6_opt}, {#e_opt}, {#x_opt},
  {#n_opt}, {#r_opt} and {#dc_opt}.
  @return [Integer]
@!attribute dc_opt
  This bit describes the router's handling of demand circuits.
  @return [Boolean]
@!attribute r_opt
  This bit indicates whether the originator is an active router.
  @return [Boolean]
@!attribute n_opt
  This bit indicates whether or not the router is attached to an NSSA.
  @return [Boolean]
@!attribute x_opt
  This bit should be set to 0, and ignored when received.
  @return [Boolean]
@!attribute e_opt
  This bit describes the way AS-external-LSAs are flooded.
  @return [Boolean]
@!attribute v6_opt
  If this bit is clear, the router/link should be excluded from IPv6
  routing calculations.
  @return [Boolean]
# File lib/packetgen/header/ospfv3.rb, line 137
def self.define_options(hdr)
  hdr.define_field :options, Types::Int24
  hdr.define_bit_fields_on :options, :z, 18, :dc_opt, :r_opt,
                           :n_opt, :x_opt, :e_opt, :v6_opt
end

Public Instance Methods

added_to_packet(packet) click to toggle source

@api private @note This method is used internally by PacketGen and should not be

directly called
# File lib/packetgen/header/ospfv3.rb, line 146
def added_to_packet(packet)
  ospf_idx = packet.headers.size
  packet.instance_eval "def ospfize(**kwargs) @headers[#{ospf_idx}].ospfize(**kwargs); end"
end
calc_checksum() click to toggle source

Compute checksum and set checksum field @return [Integer]

# File lib/packetgen/header/ospfv3.rb, line 153
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_length() click to toggle source

Compute length and set length field @return [Integer]

# File lib/packetgen/header/ospfv3.rb, line 170
def calc_length
  self[:length].value = Base.calculate_and_set_length(self)
end
human_type() click to toggle source

Get human-readable type @return [String]

# File lib/packetgen/header/ospfv3.rb, line 164
def human_type
  self[:type].to_human
end
ospfize(dst: nil) click to toggle source

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

@param [String,Symbol,nil] dst destination address. May be a dotted IP

address (by example '224.0.0.5') or a Symbol (+:all_spf_routers+ or
+:all_d_routers+)

@return [void]

# File lib/packetgen/header/ospfv3.rb, line 187
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