# macsec — IEEE 802.1AE Line-Rate Hop-by-Hop SecTAG & ICV Frames (`macsec`)

> **FrameAssembly v3.3.0** · Protocol 11 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/macsec

- **Full Protocol Name:** IEEE 802.1AE Media Access Control Security (MACsec)
- **Keyword:** `macsec`
- **OSI Layer & Transport:** Layer 2 · EtherType 0x88e5
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** IEEE 802.1AE, IEEE 802.1AEbw
- **Wireshark Display Filter:** `macsec`
- **Wire Encapsulation:** `Ethernet II -> MACsec (802.1AE) (EtherType 0x88e5)`

## Overview

Encrypting dark-fiber metro trunks and campus switch uplinks at Layer 2 (`0x88e5`): `macsec` packs the exact SecTAG (`tci`, `an`, `pn`, optional 8-byte `sci`) plus 16-byte AES-GCM `icv` trailer.

IEEE 802.1AE Media Access Control Security (`macsec`) provides line-rate hop-by-hop Layer 2 encryption and replay protection across dark fiber interconnects, metro WAN handoffs, and high-security campus access ports (`EtherType 0x88e5`). Every MACsec frame prepends an 8-byte or 16-byte Security Tag (`SecTAG`) carrying Tag Control Information (`tci`), Association Number (`an`), Short Length (`sl`), 32-bit Packet Number (`pn`), and optional 64-bit Secure Channel Identifier (`sci`), followed by ciphertext and a 16-byte Integrity Check Value (`icv`).

FrameAssembly builds deterministic `0x88e5` SecTAG headers and AES-GCM-style ICV trailers over MAC-only or IP switch endpoints, enabling replay-window, key-rollover (`an 0..3`), and MTU overhead verification without hardware MACsec PHYs.

## Minimal Quickstart (`macsec_quickstart.frasm`)

```frameassembly
host dc_west { mac 00:1b:21:aa:00:01 }
host dc_east { mac 00:1b:21:bb:00:02 }

compile {
    dc_west -> dc_east macsec tci 0x2c an 0 pn 1 sci 0x001b21aa00010001 icv "0102030405060708090a0b0c0d0e0f10"
}
```

### Automatic Wire Calculations

- Sets Ethernet II EtherType to `0x88e5` (IEEE 802.1AE MACsec) and bit-packs upper `tci` flags with the 2-bit Association Number (`an`).
- Automatically sets the `SC` (Secure Channel) bit and appends the 8-byte big-endian `sci` whenever `sci` is specified (emitting a 16-byte SecTAG vs. 8-byte compact SecTAG).
- Appends the 16-byte Integrity Check Value (`icv`) trailer after the protected payload bytes.

## Practical Lab Example: MKA Key Establishment Followed by MACsec SAK Rollover Across a Metro Trunk (`macsec_sak_rollover.frasm`)

Simulates an 802.1X MKA (`eapol key`) exchange between two data-center border switches followed by encrypted `macsec` frames rolling cleanly from Association Number 0 to Association Number 1.

```frameassembly
host border_a { mac 00:1c:73:a0:00:01 }
host border_b { mac 00:1c:73:b0:00:02 }

compile {
    // 1. MKA (MACsec Key Agreement) EAPOL-Key peer keepalive
    border_a -> border_b eapol key version 3 wait 2ms
    // 2. Encrypted MACsec frames on SAK Association Number 0 (with 8-byte SCI)
    border_a -> border_b macsec tci 0x2c an 0 pn 100 sci 0x001c73a000010001 payload "ciphertext-block-100" icv "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" wait 1ms
    border_a <- border_b macsec tci 0x2c an 0 pn 200 sci 0x001c73b000020001 payload "ciphertext-block-200" icv "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" wait 2ms
    // 3. Hitless SAK key rollover onto Association Number 1 (pn resets to 1)
    border_a -> border_b macsec tci 0x2c an 1 pn 1 sci 0x001c73a000010001 payload "ciphertext-new-sak" icv "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
}
```

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

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly macsec_sak_rollover.frasm macsec_sak_rollover.pcap
frameassembly macsec_sak_rollover.frasm macsec_sak_rollover.pcapng

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

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

## All `macsec` Keywords & Header Options

Packet line pattern: `src_host -> dst_host macsec [tci <0..255>] [an <0..3>] [sl <0..63>] [pn <number>] [sci <number>] [icv "<hex>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `tci <0..255>` | Tag Control Information byte (0..255) | `0x08 (E=0, C=0) or 0x2c with SCI` | Sets SecTAG TCI flags (`0x20` SC present, `0x08` E encryption, `0x04` C changed text). |
| `an <0..3>` | 2-bit Association Number (0..3) | `0` | Sets the active SAK Association Number in the low 2 bits of the TCI/AN byte. |
| `sl <0..63>` | 6-bit Short Length (0..63) | `0` | Sets Short Length in byte 3 of SecTAG (`1..47` octets for short frames; `0` otherwise). |
| `pn <number>` | 32-bit Packet Number (>= 1) | `1` | Sets the big-endian 32-bit replay-protection Packet Number in bytes 4..7 of SecTAG. |
| `sci <number>` | 64-bit hex/decimal Secure Channel ID | `Omitted unless set (6-byte MAC + 2-byte port)` | Appends the 8-byte Secure Channel Identifier (`0x001b21aa00010001`) to SecTAG. |
| `icv "<hex>"` | 16-byte hex string (32 hex digits) | `16-byte deterministic tag` | Appends the 128-bit AES-GCM Integrity Check Value trailer at the end of the frame. |

## Cross-Layer Options Valid with `macsec`

| 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). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `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>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |

## Common Mistakes & Quick Fixes

### Pass sci as a 64-Bit Hex Literal (0x...) and icv as a Quoted Hex String

- **Avoid:** `sw1 -> sw2 macsec sci "00:1b:21:aa:00:01:00:01" icv 0x0102`
- **Valid v3.3.0 Syntax:** `sw1 -> sw2 macsec sci 0x001b21aa00010001 icv "0102030405060708090a0b0c0d0e0f10"`

`sci` takes a Whole number (`0x001b21aa00010001`), while `icv` takes a quoted hex byte string (`"0102...0f10"`).

### Association Number an Is Bounded to 0..3

- **Avoid:** `sw1 -> sw2 macsec an 4 pn 1`
- **Valid v3.3.0 Syntax:** `sw1 -> sw2 macsec an 3 pn 1`

Per IEEE 802.1AE, `an` occupies bits 0..1 of the TCI/AN octet and only accepts values `0`, `1`, `2`, or `3`.
