# ah — RFC 4302 IPsec Authentication Header (IP Proto 51) (`ah`)

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

- **Full Protocol Name:** IPsec Authentication Header
- **Keyword:** `ah`
- **OSI Layer & Transport:** Layer 3 · IP Proto 51
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 4302
- **Wireshark Display Filter:** `ah`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> IPsec AH (IP Proto 51)`

## Overview

Rare in the wild because NAT breaks it on sight, yet still lurking in compliance checklists and OSPFv3 auth: `ah` builds IP Protocol `51` headers with aligned 32-bit `Payload Len` math.

IPsec Authentication Header (`ah`, RFC 4302) provides connectionless integrity, data-origin authentication, and anti-replay protection across the outer IP header and upper-layer payload **without encrypting the payload** (**IP Protocol `51`** / IPv6 Extension Header `51`).

RFC 4302 has a subtle wire length formula: `Payload Len` is encoded in **32-bit words minus `2`**, while the total AH header must be padded to a multiple of 32 bits on IPv4 or 64 bits on IPv6. FrameAssembly computes `Payload Len` and aligns the `icv` field automatically across both IPv4 and IPv6.

## Minimal Quickstart (`ah_quickstart.frasm`)

```frameassembly
host rtr_core1 { ip 10.0.51.1 }
host rtr_core2 { ip 10.0.51.2 }

compile {
    rtr_core1 -> rtr_core2 ah spi 0x00002001 seq 1
}
```

### Automatic Wire Calculations

- Sets outer IPv4 Protocol or IPv6 Next Header to `51` (Authentication Header).
- Computes RFC 4302 `Payload Len = (total_ah_bytes / 4) - 2` automatically and zero-pads `icv` to 4-byte (IPv4) or 8-byte (IPv6) alignment.
- Populates 16-bit Reserved (`0x0000`), 32-bit `spi` (default `0x00000200`), 32-bit `seq` (default `1`), and 96-bit default `icv`.

## Practical Lab Example: Authenticated Dual-Stack Router Peering Using IPsec AH (Protocol 51) (`ah_authenticated_peering.frasm`)

Emits bidirectional IPv4 and IPv6 RFC 4302 Authentication Header packets demonstrating 32-bit vs 64-bit ICV alignment.

```frameassembly
host core_a_v4 { ip 172.16.51.1 }
host core_b_v4 { ip 172.16.51.2 }
host core_a_v6 { ip 2001:db8:51::1 }
host core_b_v6 { ip 2001:db8:51::2 }

compile {
    // IPv4 AH (24-byte header: Payload Len = 4, 12-byte 96-bit ICV)
    core_a_v4 -> core_b_v4 ah spi 0x00002001 seq 1 nextheader 59 icv "00112233445566778899aabb" payload "cleartext-signed-v4" wait 2ms
    core_a_v4 <- core_b_v4 ah spi 0x00002002 seq 1 nextheader 59 icv "aabbccddeeff001122334455" payload "cleartext-ack-v4" wait 3ms
    // IPv6 AH (aligned to 8-octet boundary per RFC 4302 §2.2)
    core_a_v6 -> core_b_v6 ah spi 0x00062001 seq 1 nextheader 59 icv "0102030405060708090a0b0c" payload "cleartext-signed-v6"
}
```

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

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

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

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

## All `ah` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ah [spi <number>] [seq <number>] [icv "<hex>"] [nextheader <0..255>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `spi <number>` | Whole number (decimal or 0x hex) | `0x00000200` | Sets the 4-byte Security Parameters Index. |
| `seq <number>` | Whole number | `1` | Sets the 4-byte RFC 4302 Sequence Number. |
| `icv "<hex>"` | Double-quoted even-length hex string | `12-byte (96-bit) ICV` | Sets the Integrity Check Value bytes (padded automatically to 32-bit alignment on IPv4 or 64-bit alignment on IPv6). |
| `nextheader <0..255>` | 8-bit IP protocol number | `6 (TCP) or 59 (No Next Header)` | Sets the first byte of the AH header identifying the payload immediately following the ICV. |

## Cross-Layer Options Valid with `ah`

| 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 64, 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

### ah Does Not Take an iv (Initialization Vector) Modifier

- **Avoid:** `a -> b ah spi 0x2001 iv "0102030405060708"`
- **Valid v3.2.2 Syntax:** `a -> b ah spi 0x2001 seq 1 icv "00112233445566778899aabb"`

Authentication Header (`ah`) signs packets in cleartext without cipher encryption, so there is no Initialization Vector on the wire (`iv` applies to `esp`/`ipsec`).

### Choose Valid Inner nextheader Codes for Wireshark Dissection

- **Valid v3.2.2 Syntax:** `a -> b ah spi 0x2001 nextheader 59 payload "raw-test"`

If you attach an arbitrary ASCII `payload`, set `nextheader 59` (IPv6 No Next Header / raw data) so Wireshark does not try to dissect the ASCII text as a truncated TCP (`6`) header.
