# dhcp — Full DHCPv4 DORA & IPv6 DHCPv6 Lease Negotiation (`dhcp`)

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

- **Full Protocol Name:** Dynamic Host Configuration Protocol — DHCPv4 & DHCPv6
- **Keyword:** `dhcp`
- **OSI Layer & Transport:** Layer 7 · UDP 68/67 (DHCPv4) · UDP 546/547 (DHCPv6)
- **Host Endpoint Rule:** Dual-Stack Morphing (v4/v6)
- **Standards:** RFC 2131, RFC 2132, RFC 8415
- **Wireshark Display Filter:** `dhcp || dhcpv6`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 68/67 (DHCPv4) · UDP 546/547 (DHCPv6) -> DHCPv4 / DHCPv6`

## Overview

Test switch DHCP Snooping without plugging a rogue home router into the wall: `dhcp` builds BOOTP + magic cookie `0x63825363` Option TLVs on IPv4 (`68 <-> 67`) and morphs into DHCPv6 (`546 <-> 547`) over IPv6.

Dynamic Host Configuration Protocol (`dhcp`) is a **dual-stack morphing protocol** in FrameAssembly: over IPv4 endpoints, it compiles a 240-byte **RFC 2131 BOOTP header + magic cookie `63:82:53:63`** + TLV Options (`53` Message Type, `1` Subnet Mask, `3` Router, `6` DNS, `12` Hostname, `51` Lease Time, `255` End) on **UDP `68 <-> 67`**. Over IPv6 endpoints, it automatically morphs into **RFC 8415 DHCPv6** (`solicit`, `advertise`, `request`, `reply`, `release`) with 24-bit Transaction ID and Client/Server DUID options on **UDP `546 <-> 547`**.

## Minimal Quickstart (`dhcp_quickstart.frasm`)

```frameassembly
host client_pc { ip 0.0.0.0 mac 00:1b:21:44:55:66 }
host dhcp_srv  { ip 10.10.10.1 mac 00:1b:21:00:00:01 }

compile {
    client_pc -> dhcp_srv dhcp discover xid 0x3903f326 chaddr 00:1b:21:44:55:66 hostname "eng-laptop-04" wait 2ms
    client_pc <- dhcp_srv dhcp offer xid 0x3903f326 yiaddr 10.10.10.150 siaddr 10.10.10.1 chaddr 00:1b:21:44:55:66 subnetmask 255.255.255.0 router 10.10.10.1 dnsserver 10.10.10.53 leasetime 86400
}
```

### Automatic Wire Calculations

- Selects UDP `68 -> 67` for client messages (`discover`, `request`, `release`) and UDP `67 -> 68` for server messages (`offer`, `ack`) on IPv4—or UDP `546 <-> 547` on IPv6.
- Writes the 4-byte RFC 2132 DHCP magic cookie (`0x63825363`), packs every specified option TLV (`1`, `3`, `6`, `12`, `51`, `53`), and terminates with Option `255` (`0xff`).
- Defaults `chaddr` to the sending host's Ethernet MAC when omitted so BOOTP hardware fields stay consistent.

## Practical Lab Example: Complete 4-Step DHCPv4 DORA Lease Workflow + DHCPv6 SARR Exchange (`dhcp_v4_dora_and_v6_sarr.frasm`)

Captures a full 4-frame IPv4 `discover -> offer -> request -> ack` cycle on VLAN 10 followed by a 4-frame IPv6 `solicit -> advertise -> request -> reply` exchange.

```frameassembly
host phone_v4 { ip 0.0.0.0 mac 00:50:56:aa:10:01 }
host srv_v4   { ip 10.20.10.1 mac 00:50:56:aa:00:01 }
host host_v6  { ip fe80::25 mac 00:50:56:bb:10:25 }
host srv_v6   { ip fe80::1 mac 00:50:56:bb:00:01 }

compile {
    // 1. DHCPv4 DORA on VLAN 10 (UDP 68 <-> 67)
    phone_v4 -> srv_v4 dhcp discover xid 0x77889900 chaddr 00:50:56:aa:10:01 hostname "voip-desk-12" vlan 10 wait 2ms
    phone_v4 <- srv_v4 dhcp offer xid 0x77889900 yiaddr 10.20.10.112 siaddr 10.20.10.1 chaddr 00:50:56:aa:10:01 subnetmask 255.255.255.0 router 10.20.10.1 dnsserver 10.20.10.53 leasetime 43200 vlan 10 wait 1ms
    phone_v4 -> srv_v4 dhcp request xid 0x77889900 chaddr 00:50:56:aa:10:01 hostname "voip-desk-12" vlan 10 wait 1ms
    phone_v4 <- srv_v4 dhcp ack xid 0x77889900 yiaddr 10.20.10.112 siaddr 10.20.10.1 chaddr 00:50:56:aa:10:01 subnetmask 255.255.255.0 router 10.20.10.1 dnsserver 10.20.10.53 leasetime 43200 vlan 10 wait 5ms
    // 2. DHCPv6 SARR over IPv6 link-local endpoints (UDP 546 <-> 547)
    host_v6 -> srv_v6 dhcp solicit xid 0x00a1b2c3 dnsserver 2001:db8:20::53 wait 2ms
    host_v6 <- srv_v6 dhcp advertise xid 0x00a1b2c3 dnsserver 2001:db8:20::53 wait 1ms
    host_v6 -> srv_v6 dhcp request xid 0x00a1b2c3 wait 1ms
    host_v6 <- srv_v6 dhcp reply xid 0x00a1b2c3 dnsserver 2001:db8:20::53
}
```

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

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

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

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

## All `dhcp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host dhcp [discover | offer | request | ack | release | solicit | advertise | reply] [xid <number>] [ciaddr <ipv4>] [yiaddr <ipv4>] [siaddr <ipv4>] [chaddr <mac>] [subnetmask <ipv4>] [router <ipv4>] [dnsserver <ip>] [leasetime <number>] [hostname "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `discover \| offer \| request \| ack \| release \| solicit \| advertise \| reply` | DHCPv4 / DHCPv6 message type keyword | `discover (IPv4) / solicit (IPv6)` | Sets DHCPv4 Option `53` + BOOTP `op` (`1` request, `2` reply) or DHCPv6 `msg-type` (`1` Solicit, `2` Advertise, `3` Request, `7` Reply, `8` Release). |
| `xid <number>` | Whole number (24-bit in DHCPv6) | `0x12345678` | Sets BOOTP `xid` (4 bytes) or DHCPv6 `transaction-id` (lower 3 bytes). |
| `ciaddr <ipv4> \| yiaddr <ipv4> \| siaddr <ipv4>` | IPv4 dotted-quad literals | `0.0.0.0` | Sets Client IP, Your (offered) IP, and Next Server IP in the DHCPv4 BOOTP header. |
| `chaddr <mac>` | Colon, hyphen, or Cisco dot MAC | `Source host MAC` | Populates the 16-byte Client Hardware Address field in DHCPv4. |
| `subnetmask <ipv4> \| router <ipv4> \| dnsserver <ip>` | IPv4/IPv6 address literals | `Omitted` | Appends Option `1` (Subnet Mask), Option `3` (Router), and Option `6` / DHCPv6 Option `23` (DNS Recursive Name Server). |
| `leasetime <number> \| hostname "<text>"` | u32 seconds, UTF-8 string | `leasetime 86400` | Appends Option `51` (IP Address Lease Time) and Option `12` (Host Name). |

## Cross-Layer Options Valid with `dhcp`

| 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). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), UDP 68/67 (DHCPv4) · UDP 546/547 (DHCPv6)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Both Endpoints Still Require IP Literals (Use 0.0.0.0 for Unconfigured DHCPv4 Clients)

- **Avoid:** `host client_pc { mac 00:1b:21:44:55:66 }
client_pc -> dhcp_srv dhcp discover`
- **Valid v3.2.2 Syntax:** `host client_pc { ip 0.0.0.0 mac 00:1b:21:44:55:66 }
client_pc -> dhcp_srv dhcp discover`

Because DHCP runs inside IPv4/UDP or IPv6/UDP, FrameAssembly requires an `ip` address on both hosts. Use `ip 0.0.0.0` (the standard RFC 2131 pre-lease source IP) on unconfigured IPv4 clients, or a `fe80::...` link-local address on IPv6 clients.

### Use ack (Not dhcp_ack) for DHCPACK Replies

- **Avoid:** `client_pc <- dhcp_srv dhcp dhcp_ack xid 0x1234`
- **Valid v3.2.2 Syntax:** `client_pc <- dhcp_srv dhcp ack xid 0x1234`

On a `dhcp` line, `ack` sets DHCP Option 53 to `DHCPACK` (`5`) over UDP `67 -> 68` rather than a TCP flag.
