# ntp — 48-Byte RFC 5905 Stratum-1 Time Sync on UDP 123 (`ntp`)

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

- **Full Protocol Name:** Network Time Protocol v1–v4
- **Keyword:** `ntp`
- **OSI Layer & Transport:** Layer 7 · UDP 123
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 5905
- **Wireshark Display Filter:** `ntp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 123 -> NTPv4`

## Overview

When Kerberos tickets start failing because two servers drifted five minutes apart, everyone stares at UDP `123`. `ntp` writes clean 48-byte RFC 5905 packets right down to signed `precision` and ASCII `refid`.

Network Time Protocol (`ntp`, RFC 5905) synchronizes system clocks across routers, servers, financial engines, and Kerberos/TLS infrastructure over **UDP `dstport 123`**. Every standard NTPv4 packet is a fixed **48-byte binary structure** packing Leap Indicator (`li`), Version (`vn`), Mode (`mode`), `stratum`, `poll`, signed log2 `precision`, Root Delay/Dispersion, 4-byte `refid` (`"GPS"`, `"PPS"`, `"ATOM"`, or kiss-o'-death `"RATE"`), and 64-bit NTP epoch timestamps.

## Minimal Quickstart (`ntp_quickstart.frasm`)

```frameassembly
host app_srv  { ip 10.10.20.40 }
host time_appliance { ip 10.10.20.123 }

compile {
    app_srv -> time_appliance ntp vn 4 mode 3 poll 6 wait 2ms
    app_srv <- time_appliance ntp li 0 vn 4 mode 4 stratum 1 poll 6 precision -20 refid "GPS" txtimestamp 0xe9b4c20000000000
}
```

### Automatic Wire Calculations

- Packs the first byte `(li << 6) | (vn << 3) | mode` cleanly (`0x23` for `li 0 vn 4 mode 3` client; `0x24` for `mode 4` server) inside a 48-byte RFC 5905 header.
- Zero-pads 4-character ASCII `refid` strings (`"GPS\0"`, `"PPS\0"`, `"RATE"`) for Stratum 0/1 servers.
- Reverses ephemeral `srcport <-> 123` automatically across `->` and `<-` statements.

## Practical Lab Example: Stratum 1 Synchronization + Kiss-o'-Death Rate-Limit & Mode 6 Audit Probe (`ntp_sync_and_security_audit.frasm`)

Captures normal NTPv4 client/server clock sync, a `stratum 0 refid "RATE"` Kiss-o'-Death response, and a blocked `mode 6` control query for IDS testing.

```frameassembly
host branch_rtr { ip 172.16.30.1 }
host ntp_core   { ip 172.16.30.123 }

compile {
    // 1. Standard NTPv4 Client (mode 3) -> Stratum 1 GPS Server (mode 4)
    branch_rtr -> ntp_core ntp vn 4 mode 3 poll 6 precision -18 dscp 48 wait 2ms
    branch_rtr <- ntp_core ntp li 0 vn 4 mode 4 stratum 1 poll 6 precision -24 refid "GPS" txtimestamp 0xe9b4d01080000000 dscp 48 wait 5ms
    // 2. RFC 5905 Kiss-o'-Death (stratum 0, li 3, refid "RATE")
    branch_rtr <- ntp_core ntp li 3 vn 4 mode 4 stratum 0 refid "RATE" wait 5ms
    // 3. Mode 6 NTP Control query probe (tested against perimeter IDS/firewall)
    branch_rtr -> ntp_core ntp vn 2 mode 6
}
```

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

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

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

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

## All `ntp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ntp [li <0..3>] [vn <1..4>] [mode <0..7>] [stratum <0..16>] [poll <0..255>] [precision <-128..127>] [refid "<text>"] [txtimestamp <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `li <0..3>` | 2-bit Leap Indicator | `0 (no warning)` | Sets bits 7..6 of byte 0 (`0`=no leap, `1`=61s minute, `2`=59s minute, `3`=unsynchronized/alarm). |
| `vn <1..4>` | 3-bit NTP Version Number | `4 (NTPv4)` | Sets bits 5..3 of byte 0 (`1`..`4`). |
| `mode <0..7>` | 3-bit Association Mode | `3 (Client)` | Sets bits 2..0 of byte 0 (`1`=Symmetric Active, `2`=Passive, `3`=Client, `4`=Server, `5`=Broadcast, `6`=Control, `7`=Private). |
| `stratum <0..16>` | Whole number (0..255) | `0 (client) / 1 (when refid set)` | Sets Clock Stratum (`0`=unspecified/KoD, `1`=primary GPS/PPS, `2..15`=secondary, `16`=unsynchronized). |
| `poll <0..255> \| precision <-128..127>` | poll: u8 log2 seconds, precision: signed i8 (-128..127) | `poll 6 (64s), precision -20 (~1us)` | Sets 8-bit Poll Interval exponent and signed 8-bit two's-complement Clock Precision. |
| `refid "<text>"` | Up to 4 ASCII chars ("GPS", "PPS", "ATOM", "RATE") | `"LOCL" / zeroed` | Writes the 4-byte Reference Identifier in bytes 12..15. |
| `txtimestamp <number>` | 64-bit NTP timestamp (32b sec since 1900 + 32b frac) | `Non-zero deterministic epoch` | Writes the 8-byte Transmit Timestamp in bytes 40..47. |

## Cross-Layer Options Valid with `ntp`

| 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 123` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Signed precision Accepts Negative Integers Directly (precision -20)

- **Avoid:** `app_srv <- time_appliance ntp mode 4 precision "-20"`
- **Valid v3.2.2 Syntax:** `app_srv <- time_appliance ntp mode 4 precision -20`

`precision` expects an unquoted signed 8-bit integer (`-20` represents $2^{-20} \approx 0.95\,\mu\text{s}$).

### Set mode 4 Explicitly on Server Replies

- **Avoid:** `app_srv <- time_appliance ntp stratum 1 refid "GPS"`
- **Valid v3.2.2 Syntax:** `app_srv <- time_appliance ntp mode 4 stratum 1 refid "GPS"`

`ntp` defaults to `mode 3` (Client); always specify `mode 4` on `<-` server responses so Wireshark labels the packet `NTP Version 4, server`.
