class PacketGen::Header::OSPFv2

This class supports OSPFv2 (RFC 2328). A OSPFv2 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            |             AuType            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Authentication                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Authentication                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

An OSPFv2 header consists of:

Create an OSPFv2 header

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

OSPFv2 attributes

ospf.version              # => 2
ospf.type = 'LS_ACK'      # or 5
ospf.length = 154
ospf.router_id = 0xc0a80001
ospf.area_id = 1
ospf.checksum = 0xabcd
ospf.au_type = 'NO_AUTH'  # or 0
ospf.authentication = 0

OSPFv2 body

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

@author Sylvain Daubert @since 2.5.0

Constants

AU_TYPES

Authentication types

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 OSPFv2 options field. @param [Base] hdr header on which define a OSPFv2 options field @return [void] @!macro [attach] define_options

@!attribute options
  8-bit options field. Handle {#mt_opt}, {#e_opt}, {#mc_opt},
  {#n_opt}, {#l_opt}, {#dc_opt}, {#o_opt} and {#dn_opt}.
  @return [Integer]
@!attribute dn_opt
  @return [Boolean]
@!attribute o_opt
  @return [Boolean]
@!attribute dc_opt
  This bit describes the router's handling of demand circuits.
  @return [Boolean]
@!attribute l_opt
  This specifies if a LLS Data block is present.
  @return [Boolean]
@!attribute n_opt
  This bit specifies if NSSA is supported.
  @return [Boolean]
@!attribute mc_opt
  This bit describes whether IP multicast datagrams are forwarded.
  @return [Boolean]
@!attribute e_opt
  This bit describes the way AS-external-LSAs are flooded.
  @return [Boolean]
@!attribute mt_opt
  @return [Boolean]
# File lib/packetgen/header/ospfv2.rb, line 159
def self.define_options(hdr)
  hdr.define_field :options, Types::Int8
  hdr.define_bit_fields_on :options, :dn_opt, :o_opt, :dc_opt, :l_opt,
                           :n_opt, :mc_opt, :e_opt, :mt_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/ospfv2.rb, line 168
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/ospfv2.rb, line 175
def calc_checksum
  # #authentication field is not used in checksum calculation,
  # so force it to 0 before checksumming
  saved_auth = self.authentication
  self.authentication = 0

  sum = IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)

  # Restore #authentication value
  self.authentication = saved_auth

  self.checksum
end
calc_length() click to toggle source

Compute length and set length field @return [Integer]

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

Get human-readable AU type @return [String]

# File lib/packetgen/header/ospfv2.rb, line 198
def human_au_type
  self[:au_type].to_human
end
human_type() click to toggle source

Get human-readable type @return [String]

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

Fixup IP header according to RFC 2328:

  • set TOS field to 0xc0,

  • optionally set destination address,

  • set TTL to 1 if destination is a mcast address.

This method may be called as:

# first way
pkt.ospfv2.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/ospfv2.rb, line 221
def ospfize(dst: nil)
  ip = ip_header(self)
  ip.tos = 0xc0
  dst = case dst
        when :all_spf_routers
          '224.0.0.5'
        when :all_d_routers
          '224.0.0.6'
        else
          dst
        end
  ip.dst = dst unless dst.nil?
  ip.ttl = 1 if ip[:dst].mcast?
end