# ospf — Dual-Stack OSPFv2 & OSPFv3 Link-State Adjacencies (`ospf`)

> **FrameAssembly v3.2.2** · Protocol 21 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/ospf

- **Full Protocol Name:** Open Shortest Path First — OSPFv2 & Dual-Stack OSPFv3
- **Keyword:** `ospf`
- **OSI Layer & Transport:** Layer 3 · IP Proto 89 (TTL 1)
- **Host Endpoint Rule:** Dual-Stack Morphing (v4/v6)
- **Standards:** RFC 2328, RFC 5340
- **Wireshark Display Filter:** `ospf`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> OSPFv2 / OSPFv3 (IP Proto 89 (TTL 1))`

## Overview

Mismatched hello/dead timers have caused more silent routing blackholes than fiber backhoes. `ospf` builds IPv4 OSPFv2 (Fletcher-16 checksum) and IPv6 OSPFv3 (ICMPv6-style pseudo-header checksum) automatically.

Open Shortest Path First (`ospf`) is the enterprise and service-provider link-state Interior Gateway Protocol running directly over **IP Protocol `89`** with link-local `ttl 1`. In FrameAssembly v3.2.0, `ospf` is **dual-stack morphing**: over IPv4 hosts, it emits **OSPFv2** (RFC 2328, 24-byte header, RFC 1071 checksum omitting the 64-bit authentication field); over IPv6 hosts, the exact same syntax emits **OSPFv3** (RFC 5340, Version `3`, 16-byte header, Instance ID `0`, and full IPv6 pseudo-header checksum).

All five OSPF packet types (`hello`, `dbd`, `lsr`, `lsu`, `lsack`) are natively generated, including valid **Fletcher-16 (`ISO 8473`) LSA checksums** on Link State Update and Acknowledgment records.

## Minimal Quickstart (`ospf_quickstart.frasm`)

```frameassembly
host rtr_a { ip 10.0.0.1 }
host rtr_b { ip 10.0.0.2 }

compile {
    rtr_a -> rtr_b ospf hello routerid 1.1.1.1 areaid 0
}
```

### Automatic Wire Calculations

- Selects OSPFv2 (24B header, IPv4 Proto `89`) over IPv4 hosts or OSPFv3 (16B header, IPv6 Next Header `89`, IPv6 pseudo-header checksum) over IPv6 hosts.
- Defaults IP `ttl` / Hop Limit to `1` and derives multicast destination MACs automatically when sending to `224.0.0.5` / `224.0.0.6` or `ff02::5` / `ff02::6`.
- Computes both the outer OSPF packet checksum and inner ISO 8473 Fletcher-16 LSA checksums on `lsu` and `lsack` packets.

## Practical Lab Example: Full OSPF Adjacency Synchronization Plus OSPFv3 IPv6 Backbone Hello (`ospf_adjacency_v2_v3.frasm`)

Walks two core routers through OSPFv2 `hello`, `dbd`, `lsr`, `lsu`, and `lsack` exchange on Area 0, followed by an OSPFv3 IPv6 Hello using 32-bit `routerid`.

```frameassembly
host core_1_v4 { ip 172.16.0.1 }
host core_2_v4 { ip 172.16.0.2 }
host allspf_v6 { ip ff02::5 }
host core_1_v6 { ip fe80::1c73:1 }

compile {
    // 1. OSPFv2 Area 0 adjacency bring-up and LSA exchange
    core_1_v4 -> core_2_v4 ospf hello routerid 1.1.1.1 areaid 0 hellointerval 10 deadinterval 40 priority 100 dscp 48 wait 2ms
    core_1_v4 <- core_2_v4 ospf hello routerid 2.2.2.2 areaid 0 hellointerval 10 deadinterval 40 priority 1 dscp 48 wait 2ms
    core_1_v4 -> core_2_v4 ospf dbd routerid 1.1.1.1 areaid 0 dscp 48 wait 1ms
    core_1_v4 <- core_2_v4 ospf lsr routerid 2.2.2.2 areaid 0 dscp 48 wait 1ms
    core_1_v4 -> core_2_v4 ospf lsu routerid 1.1.1.1 areaid 0 dscp 48 wait 1ms
    core_1_v4 <- core_2_v4 ospf lsack routerid 2.2.2.2 areaid 0 dscp 48 wait 4ms
    // 2. OSPFv3 over IPv6 multicast ff02::5 (routerid stays 32-bit dotted-quad!)
    core_1_v6 -> allspf_v6 ospf hello routerid 1.1.1.1 areaid 0.0.0.0 hellointerval 10 deadinterval 40
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show ospf_adjacency_v2_v3.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly ospf_adjacency_v2_v3.frasm ospf_adjacency_v2_v3.pcap
frameassembly ospf_adjacency_v2_v3.frasm ospf_adjacency_v2_v3.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse ospf_adjacency_v2_v3.pcap ospf_adjacency_v2_v3_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r ospf_adjacency_v2_v3.pcap
```

## All `ospf` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ospf [hello | dbd | lsr | lsu | lsack] [routerid <ipv4>] [areaid <u32|ipv4>] [hellointerval <0..65535>] [deadinterval <number>] [priority <0..255>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `hello \| dbd \| lsr \| lsu \| lsack` | OSPF Packet Type keyword | `hello (Type 1)` | Selects Type `1` Hello, Type `2` Database Description (`dbd`), Type `3` Link State Request (`lsr`), Type `4` Link State Update (`lsu`), or Type `5` Link State Acknowledgment (`lsack`). |
| `routerid <ipv4>` | 32-bit dotted-quad IPv4 format (e.g. 1.1.1.1) | `Source IPv4 (or 1.1.1.1 on IPv6)` | Sets the 32-bit OSPF Router ID in both OSPFv2 and OSPFv3 headers. |
| `areaid <u32\|ipv4>` | u32 integer (0) or dotted-quad (0.0.0.0) | `0 (Backbone Area 0.0.0.0)` | Sets the 32-bit OSPF Area ID. |
| `hellointerval <0..65535> \| deadinterval <number>` | Timers in seconds | `hellointerval 10, deadinterval 40` | Sets the Hello Interval (`u16` seconds) and Router Dead Interval (`u32` OSPFv2 / `u16` OSPFv3) in `hello` packets. |
| `priority <0..255>` | Whole number (0..255) | `1` | Sets the Designated Router (DR) Election Priority (`0` = ineligible to become DR/BDR). |

## Cross-Layer Options Valid with `ospf`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 1, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |

## Common Mistakes & Quick Fixes

### OSPFv3 routerid Always Remains 32-Bit Dotted-Quad (RFC 5340)

- **Avoid:** `core_1_v6 -> core_2_v6 ospf hello routerid 2001:db8::1`
- **Valid v3.2.2 Syntax:** `core_1_v6 -> core_2_v6 ospf hello routerid 1.1.1.1`

Per RFC 5340 §2.11, OSPFv3 retains 32-bit Router IDs and Area IDs formatted in dotted-quad notation (`1.1.1.1`) even when running over 128-bit IPv6 endpoints.

### Zero Underscores in Timer Modifiers

- **Avoid:** `r1 -> r2 ospf hello router_id 1.1.1.1 hello_interval 10`
- **Valid v3.2.2 Syntax:** `r1 -> r2 ospf hello routerid 1.1.1.1 hellointerval 10 deadinterval 40`

Use `routerid`, `areaid`, `hellointerval`, and `deadinterval` with zero underscores.
