Skip to content
This repository

Welcome to the ospfv2 wiki!

This is an attempt to emulate OSPF using the ruby programming language.

I have created an API to create, send, and receive OSPF packets.

The following OSPF packets are supported;

  • Hello
  • LinkStateUpdate
  • LinkStateRequest
  • LinkStateAck
  • DatabaseDescription

The following LSA are supported:

  • Router
  • Network
  • Summary
  • ASBR_Summary
  • AS_External

Using the API I created and ‘ospfv2’ simple emulator which can:

  • send and receive hello
  • establish a p2p adjacency
  • exchange database description packets
  • respond to received link state request
  • send and receive link state update
  • periodic lsa refresh
  • simulate other OSPF router in the same area by calculating their corresponding LSAs (Intra-router, ABR-router, ASBR-router)
# ospfv2 --help

  Usage: /usr/bin/ospfv2 [options]
        --base-p2p-addr [PREFIX]     base p2p links addres [1.3.0.0/30]
    -i, --address [PREFIX]           IP Address of the OSPF Interface.
        --base-router-id [ID]        base router-id [0.0.0.0]
    -r, --router-id [ID]             Router Id.   [0.0.0.1]
    -n, --neighbor-id [ID]           Neighbor Id. [0.0.0.2]
    -a, --area-id [ID]               Area Id.     [0.0.0.0]
        --hello-interval [INT]       Hello Int.   [10]
        --dead-interval [INT]        Dead Int.    [40]
    -g, --grid [colxrow]             Area Grid.   [2x2]
    -S, --number-of-summary [INT]    Number of Summary LSA.
    -E, --number-of-external [INT]   Number AsExternal
    -f, --log-fname [FILENAME]       To redirect logs to a file.

    -h                               Show this message
    -?, --help


# ospfv2 -i 192.168.1.123/24 -r 0.0.0.1 -n 2.0.0.0  -g 2x2 --hello-int 30 --number-of-external 5 --number-of-summary 5 

… will calculate the corresponding link state database and start an interactive CLI session from which you can:
  • start the neighbor state machine
  • stop the neighbor state machine
  • output your link state database to the terminal


>> start
*** ADDED 224.0.0.5 membership to Recv Socket ***
*** ADDED 224.0.0.6 membership to Recv Socket ***
*** ADDED 224.0.0.5 membership to Send Socket ***
*** ADDED 224.0.0.6 membership to Send Socket ***

16:01 state change: 1# [start]: down -> Attempt>> 
>> 
16:09 (attempt) rcv Hello:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 44
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 128, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 

16:11 (attempt) snd Hello:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 48
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 0, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 2.0.0.0

16:11 (attempt) rcv Hello:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 48
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 128, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 0.0.0.1

16:11 (attempt) snd DatabaseDescription:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 32
 MTU 1500, Options 0x0, I|M|MS: 7 [111], DD_SEQ: 0xb73

16:11 state change: 2# [two_way_received]: attempt -> ExStart
16:11 (exstart) rcv DatabaseDescription:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 32
 MTU 1500, Options 0x0, I|M|MS: 7 [111], DD_SEQ: 0x201cd33
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length


16:11 (exstart) snd DatabaseDescription:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 332
 MTU 1500, Options 0x0, I|M|MS: 0 [000], DD_SEQ: 0x201cd33
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 17    0x00  summary   30.0.2.0        0.1.0.1         0x80000001  0xdd5f   28     
 17    0x00  asbrSum   0.1.0.1         0.1.0.1         0x80000001  0x5703   28     
 18    0x22  router    0.2.0.2         0.2.0.2         0x80000001  0xa3da   96     
 18    0x22  router    0.2.0.1         0.2.0.1         0x80000001  0xf894   96     
 18    0x22  router    0.1.0.2         0.1.0.2         0x80000001  0xf5b0   96     
 17    0x00  external  50.0.1.0        0.1.0.1         0x80000001  0x9556   48     
 18    0x22  router    0.1.0.1         0.1.0.1         0x80000001  0x9ecc   120    
 17    0x00  external  50.0.2.0        0.1.0.1         0x80000001  0x8a60   48     
 18    0x22  router    0.0.0.1         0.0.0.1         0x80000001  0xaadb   72     
 17    0x00  external  50.0.3.0        0.1.0.1         0x80000001  0x7f6a   48     
 17    0x00  external  50.0.4.0        0.1.0.1         0x80000001  0x7474   48     
 18    0x00  summary   30.0.3.0        0.2.0.1         0x80000001  0xca70   28     
 17    0x00  external  50.0.5.0        0.1.0.1         0x80000001  0x697e   48     
 18    0x00  summary   30.0.4.0        0.2.0.1         0x80000001  0xbf7a   28     
 18    0x00  summary   30.0.1.0        0.1.0.1         0x80000001  0xe855   28     

16:12 state change: 3# []: exstart -> Exchange
16:12 (exchange) rcv Hello:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 48
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 128, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 0.0.0.1

16:12 ev_timer_start: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:11"]
16:12 ev_timer_cancel: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:11"]
16:12 ev_timer_cancel: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:11"]
16:12 ev_timer_start: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:11"]
16:12 ev_timer_cancel: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:12"]
16:13 (exchange) rcv DatabaseDescription:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 52
 MTU 1500, Options 0x0, I|M|MS: 1 [001], DD_SEQ: 0x201cd34
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 1581  0x22  router    2.0.0.0         2.0.0.0         0x80000020  0x2bf8   20     

16:13 (exchange) snd DatabaseDescription:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 32
 MTU 1500, Options 0x0, I|M|MS: 0 [000], DD_SEQ: 0x201cd34
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length


16:13 state change: 4# [exchange_done]: exchange -> Loading
16:13 (loading) rcv LinkStateRequest:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 204
 LS type summary_lsa Link State ID 30.0.2.0 Advertising Router 0.1.0.1
 LS type asbr_summary_lsa Link State ID 0.1.0.1 Advertising Router 0.1.0.1
 LS type router_lsa Link State ID 0.2.0.2 Advertising Router 0.2.0.2
 LS type router_lsa Link State ID 0.2.0.1 Advertising Router 0.2.0.1
 LS type router_lsa Link State ID 0.1.0.2 Advertising Router 0.1.0.2
 LS type as_external_lsa Link State ID 50.0.1.0 Advertising Router 0.1.0.1
 LS type router_lsa Link State ID 0.1.0.1 Advertising Router 0.1.0.1
 LS type as_external_lsa Link State ID 50.0.2.0 Advertising Router 0.1.0.1
 LS type router_lsa Link State ID 0.0.0.1 Advertising Router 0.0.0.1
 LS type as_external_lsa Link State ID 50.0.3.0 Advertising Router 0.1.0.1
 LS type as_external_lsa Link State ID 50.0.4.0 Advertising Router 0.1.0.1
 LS type summary_lsa Link State ID 30.0.3.0 Advertising Router 0.2.0.1
 LS type as_external_lsa Link State ID 50.0.5.0 Advertising Router 0.1.0.1
 LS type summary_lsa Link State ID 30.0.4.0 Advertising Router 0.2.0.1
 LS type summary_lsa Link State ID 30.0.1.0 Advertising Router 0.1.0.1

16:14 (loading) snd LinkStateUpdate:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 888
 # LSAs 15
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 20    0x00  summary   30.0.2.0        0.1.0.1         0x80000001  0xdd5f   28     
   Netmask: 255.255.255.0
   Metric: 0
 20    0x00  asbrSum   0.1.0.1         0.1.0.1         0x80000001  0x5703   28     
   Netmask: 0.0.0.0
   Metric: 0
 21    0x22  router    0.2.0.2         0.2.0.2         0x80000001  0xa3da   96     
   |Nt|W|V|E|B| 00000000
   PointToPoint:
      LinkId: 0.1.0.2
      LinkData: 1.3.0.10
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.2.0.1
      LinkData: 1.3.0.14
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.1.0.2
      LinkData: 1.3.0.10
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.8
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
   PointToPoint:
      LinkId: 0.2.0.1
      LinkData: 1.3.0.14
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.12
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
 21    0x22  router    0.2.0.1         0.2.0.1         0x80000001  0xf894   96     
   |Nt|W|V|E|B| 00000001
   PointToPoint:
      LinkId: 0.1.0.1
      LinkData: 1.3.0.6
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.2.0.2
      LinkData: 1.3.0.13
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.1.0.1
      LinkData: 1.3.0.6
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.4
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
   PointToPoint:
      LinkId: 0.2.0.2
      LinkData: 1.3.0.13
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.12
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
 21    0x22  router    0.1.0.2         0.1.0.2         0x80000001  0xf5b0   96     
   |Nt|W|V|E|B| 00000000
   PointToPoint:
      LinkId: 0.1.0.1
      LinkData: 1.3.0.2
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.2.0.2
      LinkData: 1.3.0.9
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.1.0.1
      LinkData: 1.3.0.2
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.0
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
   PointToPoint:
      LinkId: 0.2.0.2
      LinkData: 1.3.0.9
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.8
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
 20    0x00  external  50.0.1.0        0.1.0.1         0x80000001  0x9556   48     
   Netmask: 255.255.255.0
   E1 (ID 0) Metric: 0 Forwarding: 0.0.0.0 Tag: 0
   E1 (ID 10) Metric: 20 Forwarding: 0.0.0.0 Tag: 10
 21    0x22  router    0.1.0.1         0.1.0.1         0x80000001  0x9ecc   120    
   |Nt|W|V|E|B| 00000011
   PointToPoint:
      LinkId: 0.1.0.2
      LinkData: 1.3.0.1
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.2.0.1
      LinkData: 1.3.0.5
      RouterLinkType: point_to_point
      Metric: 1
   PointToPoint:
      LinkId: 0.1.0.2
      LinkData: 1.3.0.1
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.0
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
   PointToPoint:
      LinkId: 0.2.0.1
      LinkData: 1.3.0.5
      RouterLinkType: point_to_point
      Metric: 0
   StubNetwork:
      LinkId: 1.3.0.4
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 0
   PointToPoint:
      LinkId: 0.0.0.1
      LinkData: 1.3.0.18
      RouterLinkType: point_to_point
      Metric: 1
   StubNetwork:
      LinkId: 1.3.0.16
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 1
 20    0x00  external  50.0.2.0        0.1.0.1         0x80000001  0x8a60   48     
   Netmask: 255.255.255.0
   E1 (ID 0) Metric: 0 Forwarding: 0.0.0.0 Tag: 0
   E1 (ID 10) Metric: 20 Forwarding: 0.0.0.0 Tag: 10
 21    0x22  router    0.0.0.1         0.0.0.1         0x80000001  0xaadb   72     
   |Nt|W|V|E|B| 00000000
   PointToPoint:
      LinkId: 2.0.0.0
      LinkData: 192.168.1.123
      RouterLinkType: point_to_point
      Metric: 1
   StubNetwork:
      LinkId: 192.168.1.0
      LinkData: 255.255.255.0
      RouterLinkType: stub_network
      Metric: 1
   PointToPoint:
      LinkId: 0.1.0.1
      LinkData: 1.3.0.17
      RouterLinkType: point_to_point
      Metric: 1
   StubNetwork:
      LinkId: 1.3.0.16
      LinkData: 255.255.255.252
      RouterLinkType: stub_network
      Metric: 1
 21    0x00  external  50.0.3.0        0.1.0.1         0x80000001  0x7f6a   48     
   Netmask: 255.255.255.0
   E1 (ID 0) Metric: 0 Forwarding: 0.0.0.0 Tag: 0
   E1 (ID 10) Metric: 20 Forwarding: 0.0.0.0 Tag: 10
 21    0x00  external  50.0.4.0        0.1.0.1         0x80000001  0x7474   48     
   Netmask: 255.255.255.0
   E1 (ID 0) Metric: 0 Forwarding: 0.0.0.0 Tag: 0
   E1 (ID 10) Metric: 20 Forwarding: 0.0.0.0 Tag: 10
 21    0x00  summary   30.0.3.0        0.2.0.1         0x80000001  0xca70   28     
   Netmask: 255.255.255.0
   Metric: 0
 21    0x00  external  50.0.5.0        0.1.0.1         0x80000001  0x697e   48     
   Netmask: 255.255.255.0
   E1 (ID 0) Metric: 0 Forwarding: 0.0.0.0 Tag: 0
   E1 (ID 10) Metric: 20 Forwarding: 0.0.0.0 Tag: 10
 21    0x00  summary   30.0.4.0        0.2.0.1         0x80000001  0xbf7a   28     
   Netmask: 255.255.255.0
   Metric: 0
 21    0x00  summary   30.0.1.0        0.1.0.1         0x80000001  0xe855   28     
   Netmask: 255.255.255.0
   Metric: 0

16:16 ev_timer_cancel: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:13"]
16:16 ev_timer_start: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:13"]
16:16 ev_timer_cancel: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:13"]
16:16 ev_timer_start: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:13"]
16:16 ev_timer_start: [:"OSPFv2::Neighbor::RefreshTimer", "16:13"]
16:16 (loading) rcv LinkStateUpdate:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 88
 # LSAs 1
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 1     0x22  router    2.0.0.0         2.0.0.0         0x80000021  0xcea6   60     
   |Nt|W|V|E|B| 00000000
   PointToPoint:
      LinkId: 0.0.0.1
      LinkData: 192.168.1.200
      RouterLinkType: point_to_point
      Metric: 10
   StubNetwork:
      LinkId: 192.168.1.0
      LinkData: 255.255.255.0
      RouterLinkType: stub_network
      Metric: 10
   StubNetwork:
      LinkId: 99.99.1.1
      LinkData: 255.255.255.255
      RouterLinkType: stub_network
      Metric: 0

16:16 (loading) snd LinkStateAck:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 44
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 1     0x22  router    2.0.0.0         2.0.0.0         0x80000021  0xcea6   60     

16:16 state change: 5# [loading_done]: loading -> Full
16:17 (full) rcv LinkStateAck:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 324
 Age  Options  Type    Link-State ID   Advr Router     Sequence   Checksum  Length
 0     0x00  summary   30.0.2.0        0.1.0.1         0x80000001  0xcd77   20     
 0     0x00  asbrSum   0.1.0.1         0.1.0.1         0x80000001  0x471b   20     
 0     0x22  router    0.2.0.2         0.2.0.2         0x80000001  0x4df1   20     
 0     0x22  router    0.2.0.1         0.2.0.1         0x80000001  0x5de3   20     
 0     0x22  router    0.1.0.2         0.1.0.2         0x80000001  0x61df   20     
 0     0x00  external  50.0.1.0        0.1.0.1         0x80000001  0xb778   20     
 0     0x22  router    0.1.0.1         0.1.0.1         0x80000001  0x71d1   20     
 0     0x00  external  50.0.2.0        0.1.0.1         0x80000001  0xac82   20     
 0     0x22  router    0.0.0.1         0.0.0.1         0x80000001  0x85bf   20     
 0     0x00  external  50.0.3.0        0.1.0.1         0x80000001  0xa18c   20     
 0     0x00  external  50.0.4.0        0.1.0.1         0x80000001  0x9696   20     
 0     0x00  summary   30.0.3.0        0.2.0.1         0x80000001  0xba88   20     
 0     0x00  external  50.0.5.0        0.1.0.1         0x80000001  0x8ba0   20     
 0     0x00  summary   30.0.4.0        0.2.0.1         0x80000001  0xaf92   20     
 0     0x00  summary   30.0.1.0        0.1.0.1         0x80000001  0xd86d   20     

16:18 ev_timer_fire: [:"OSPFv2::Neighbor::RxmtIntervalTimer", "16:18"]
16:19 (full) rcv Hello:
Version 2, RouterId 2.0.0.0, AreaId 0.0.0.0, Checksum 0, len 48
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 128, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 0.0.0.1

16:21 (full) snd Hello:
Version 2, RouterId 0.0.0.1, AreaId 0.0.0.0, Checksum 0, len 48
 HellInt 10, DeadInt 40, Options Options:  0x2  [E], mask Netmask: 255.255.255.0
 Prio 0, DR 0.0.0.0, BDR 0.0.0.0
 Neighbors: 2.0.0.0

We can check our adjacent router’s link state database:

show ospf database    

    OSPF link state database, Area 0.0.0.0
 Type       ID               Adv Rtr           Seq      Age  Opt  Cksum  Len 
Router   0.0.0.1          0.0.0.1          0x80000001     3  0x22 0xaadb  72
Router   0.1.0.1          0.1.0.1          0x80000001     3  0x22 0x9ecc 120
Router   0.1.0.2          0.1.0.2          0x80000001     3  0x22 0xf5b0  96
Router   0.2.0.1          0.2.0.1          0x80000001     3  0x22 0xf894  96
Router   0.2.0.2          0.2.0.2          0x80000001     3  0x22 0xa3da  96
Router  *2.0.0.0          2.0.0.0          0x80000008     3  0x22 0x18d   60
Summary  30.0.1.0         0.1.0.1          0x80000001     3  0x0  0xe855  28
Summary  30.0.2.0         0.1.0.1          0x80000001     3  0x0  0xdd5f  28
Summary  30.0.3.0         0.2.0.1          0x80000001     3  0x0  0xca70  28
Summary  30.0.4.0         0.2.0.1          0x80000001     3  0x0  0xbf7a  28
ASBRSum  0.1.0.1          0.1.0.1          0x80000001     3  0x0  0x5703  28
    OSPF AS SCOPE link state database
 Type       ID               Adv Rtr           Seq      Age  Opt  Cksum  Len 
Extern   50.0.1.0         0.1.0.1          0x80000001     3  0x0  0x9556  48
Extern   50.0.2.0         0.1.0.1          0x80000001     3  0x0  0x8a60  48
Extern   50.0.3.0         0.1.0.1          0x80000001     3  0x0  0x7f6a  48
Extern   50.0.4.0         0.1.0.1          0x80000001     3  0x0  0x7474  48
Extern   50.0.5.0         0.1.0.1          0x80000001     3  0x0  0x697e  48

Something went wrong with that request. Please try again.