# ldp — Port 646 MPLS Label Distribution (UDP Hello & TCP Session) (`ldp`)

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

- **Full Protocol Name:** Label Distribution Protocol
- **Keyword:** `ldp`
- **OSI Layer & Transport:** Layer 3 · TCP 646 (Session) / UDP 646 (transport udp Hello)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 5036, RFC 7552
- **Wireshark Display Filter:** `ldp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> MPLS LDP (Port 646) (TCP 646 (Session) / UDP 646 (transport udp Hello))`

## Overview

Discover neighbors with `ldp hello ... transport udp` on port `646`, then flip straight onto stateful TCP `646` to trade `init`, `keepalive`, and `labelmapping` FEC bindings.

Label Distribution Protocol (`ldp`, RFC 5036 / RFC 7552) distributes MPLS forwarding labels for IGP prefixes across service-provider and enterprise MPLS cores without manual tunnel engineering. LDP operates across **both UDP and TCP port `646`**: routers multicast link `hello` discovery messages over **UDP `646`** (`transport udp`), then establish a stateful **TCP `646`** session (the default transport) to exchange `init`, `keepalive`, `labelmapping`, `labelrequest`, `labelwithdraw`, `labelrelease`, and `notification` PDUs.

FrameAssembly encodes the 10-byte RFC 5036 LDP PDU header (`Version = 1`, `PDU Length`, 4-byte `LSR ID` + 2-byte `Label Space ID`) and nested TLV messages (`Common Hello Parameters`, `Common Session Parameters`, `FEC Prefix TLV`, `Generic Label TLV`, and `Status TLV`).

## Minimal Quickstart (`ldp_quickstart.frasm`)

```frameassembly
host lsr_pe1 { ip 10.0.0.1 }
host lsr_p1  { ip 10.0.0.2 }

compile {
    lsr_pe1 -> lsr_p1 ldp hello lsrid 1.1.1.1 holdtime 15 transport udp wait 2ms
    lsr_pe1 -> lsr_p1 ldp labelmapping lsrid 1.1.1.1 msgid 10 prefix "192.0.2.0/24" label 24001
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP port `646` for session messages and switches seamlessly to UDP port `646` when `transport udp` is added for link/targeted `hello` discovery.
- Computes exact nested RFC 5036 `PDU Length` (excluding Version + Length) and `Message Length` across `FEC`, `Generic Label`, `Common Hello`, `Common Session`, and `Status` TLVs.
- Parses IPv4 or IPv6 CIDR strings in `prefix "<cidr>"` (`"10.20.0.0/16"` or `"2001:db8:10::/48"`) into compact Address Family + Prefix-Length bit-aligned octets inside the FEC Element.

## Practical Lab Example: End-to-End MPLS LDP Session: UDP 646 Discovery, TCP-MD5 Session & Label Mapping (`ldp_session_and_labels.frasm`)

Captures bidirectional UDP `646` Hellos, a 3-way TCP `646` handshake, `init` + `keepalive` negotiation, and FEC `labelmapping` exchanges between two core LSRs.

```frameassembly
host lsr_west { ip 172.16.1.1 mac 00:1c:73:11:00:01 }
host lsr_east { ip 172.16.1.2 mac 00:1c:73:22:00:02 }

compile {
    // 1. Link Hello discovery over UDP port 646
    lsr_west -> lsr_east ldp hello lsrid 10.255.0.1 holdtime 15 transport udp wait 1ms
    lsr_west <- lsr_east ldp hello lsrid 10.255.0.2 holdtime 15 transport udp wait 2ms
    // 2. Active LSR (higher IP 172.16.1.2 -> 172.16.1.1) opens TCP 646 session
    lsr_east -> lsr_west tcp syn srcport 51646 dstport 646 win 65535 mss 1460 wait 1ms
    lsr_east <- lsr_west tcp syn ack win 65535 mss 1460 wait 1ms
    lsr_east -> lsr_west tcp ack wait 1ms
    // 3. LDP Initialization, KeepAlive, and Loopback FEC Label Mapping (Implicit Null 3 & 24010)
    lsr_east -> lsr_west ldp init lsrid 10.255.0.2 msgid 1 holdtime 180 wait 1ms
    lsr_east <- lsr_west ldp init lsrid 10.255.0.1 msgid 1 holdtime 180 wait 1ms
    lsr_east -> lsr_west ldp keepalive lsrid 10.255.0.2 msgid 2 wait 1ms
    lsr_east <- lsr_west ldp keepalive lsrid 10.255.0.1 msgid 2 wait 2ms
    lsr_east -> lsr_west ldp labelmapping lsrid 10.255.0.2 msgid 3 prefix "10.255.0.2/32" label 3 wait 1ms
    lsr_east <- lsr_west ldp labelmapping lsrid 10.255.0.1 msgid 3 prefix "10.100.20.0/24" label 24010
}
```

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

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

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

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

## All `ldp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ldp [hello | init | keepalive | notification | labelmapping | labelrequest | labelwithdraw | labelrelease] [lsrid <ipv4>] [labelspace <0..65535>] [msgid <number>] [holdtime <0..65535>] [label <number>] [prefix "<cidr>"] [status <number>] [transport <tcp|udp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `hello \| init \| keepalive \| notification \| labelmapping \| labelrequest \| labelwithdraw \| labelrelease` | LDP message type keyword | `hello (0x0100) or labelmapping` | Selects RFC 5036 Message Type (`0x0001`=Notification, `0x0100`=Hello, `0x0200`=Initialization, `0x0201`=KeepAlive, `0x0400`=Label Mapping, `0x0401`=Request, `0x0402`=Withdraw, `0x0403`=Release). |
| `lsrid <ipv4> [labelspace <0..65535>]` | 32-bit dotted-quad LSR Router ID & 16-bit Label Space | `Source IPv4 (or 1.1.1.1 on IPv6), labelspace 0` | Writes the 6-byte LDP Identifier (`4B LSR ID : 2B Label Space`) in the 10-byte LDP PDU header. |
| `msgid <number>` | 32-bit LDP Message ID | `1` | Sets the 32-bit correlation Message ID inside the LDP message header. |
| `holdtime <0..65535>` | 16-bit Hold Time in seconds | `15 on hello, 180 on init` | Sets Hello Hold Time (`Common Hello Parameters`) or KeepAlive Time (`Common Session Parameters`). |
| `prefix "<cidr>" \| label <0..1048575>` | Quoted CIDR string & 20-bit MPLS label | `"10.0.0.0/24", label 16` | Encodes the `FEC TLV` (`0x0100` Prefix element) and `Generic Label TLV` (`0x0200`) on label distribution messages. |
| `status <number>` | 32-bit RFC 5036 Status Code | `0` | Sets the Status Code inside the `Status TLV` (`0x0300`) on `notification` messages. |
| `transport <tcp\|udp>` | Layer 4 transport selector | `tcp (port 646)` | Use `transport udp` for UDP `646` Discovery Hellos; omit or use `transport tcp` for TCP `646` session PDUs. |

## Cross-Layer Options Valid with `ldp`

| 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. |
| `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>] [routeralert] [badipchecksum]` | IPv4 ID, DF/MF flags, offset 0..8191, Router Alert & checksum fault | `ipid 1, flags 0, fragoffset 0, valid RFC 1071 checksum` | Sets IPv4 Identification/fragmentation, injects RFC 2113 Router Alert (`20 04 00 00`), or corrupts the 16-bit IPv4 header checksum (`badipchecksum`). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (valid on IPv6 endpoint pairs only). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 646 (Session) / UDP 646 (transport udp Hello)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport <tcp\|udp>` | `tcp` or `udp` (LDP discovery UDP 646 vs session TCP 646) | `transport tcp` | Switches Layer 4 transport between stateful TCP (`psh ack`) and UDP. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window, flags, SACK, TCP-MD5 & TFO options | `psh ack, win 65535, automatic TCP seq/acknum` | When using TCP (`transport tcp` or default TCP): overrides automatic TCP sequence tracking, flags, or `mss`/`wscale`/`sackperm`/`sackblock`/`tsval`/`tsecr`/`tcpmd5`/`tfo`. |

## Common Mistakes & Quick Fixes

### Add transport udp on LDP Link Hellos for Authentic Wire Captures

- **Avoid:** `Assuming ldp hello automatically switches away from TCP 646`
- **Valid v3.3.0 Syntax:** `lsr1 -> lsr2 ldp hello lsrid 1.1.1.1 holdtime 15 transport udp`

`ldp` defaults to `transport tcp` (`646`) because 7 of its 8 message types run over TCP; add `transport udp` on `ldp hello` so Wireshark dissects UDP `646` Link Discovery.

### lsrid Is Always a 32-Bit Dotted-Quad ID (Even Over IPv6 LDP)

- **Avoid:** `lsr1 -> lsr2 ldp init lsrid 2001:db8::1`
- **Valid v3.3.0 Syntax:** `lsr1 -> lsr2 ldp init lsrid 10.255.0.1`

Per RFC 5036 and RFC 7552 (IPv6 LDP), the LDP Identifier always uses a 4-byte Router ID (`10.255.0.1`) rather than a 128-bit IPv6 address.
