# mdns — Port 5353 .local Multicast Service Discovery Frames (`mdns`)

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

- **Full Protocol Name:** Multicast DNS Zero-Configuration Discovery
- **Keyword:** `mdns`
- **OSI Layer & Transport:** Layer 7 · UDP 5353 -> 5353 (224.0.0.251 / ff02::fb)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 6762, RFC 6763
- **Wireshark Display Filter:** `mdns`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 5353 -> 5353 (224.0.0.251 / ff02::fb) -> mDNS (Zero-Conf)`

## Overview

Chatty printers, Apple TVs, and rogue IoT gadgets shouting `.local` records on `224.0.0.251:5353` — `mdns` locks both source and destination ports to `5353` per RFC 6762.

Multicast DNS (`mdns`, RFC 6762) enables zero-configuration `.local` hostname resolution and DNS-SD service discovery (Apple Bonjour, Linux Avahi, Chromecast, AirPrint, Matter IoT) without a central DNS server. Every frame runs on **UDP `5353 -> 5353`**.

When compiling `mdns`, FrameAssembly automatically rewrites the wire destination IP and Ethernet multicast MAC to **`224.0.0.251` (`01:00:5e:00:00:fb`)** on IPv4 or **`ff02::fb` (`33:33:00:00:00:fb`)** on IPv6, sets `txid 0` per RFC 6762 §18.1, and shares full RFC 1035 label validation with `dns`.

## Minimal Quickstart (`mdns_quickstart.frasm`)

```frameassembly
host laptop  { ip 192.168.10.42 }
host printer { ip 192.168.10.90 }

compile {
    laptop -> printer mdns query qname "_ipp._tcp.local" qtype "PTR" wait 3ms
    printer -> laptop mdns response qname "laserjet-3f.local" qtype "A" rdata "192.168.10.90" ansttl 120
}
```

### Automatic Wire Calculations

- Overrides the L3 destination IP to link-local multicast `224.0.0.251` (IPv4) or `ff02::fb` (IPv6) with matching multicast Ethernet MAC (`01:00:5e:00:00:fb` / `33:33:00:00:00:fb`).
- Fixes both UDP source and destination ports to `5353 -> 5353` and defaults `txid` to `0` per RFC 6762.
- Enforces RFC 1035 label (`<= 63`B) and total name (`<= 253`B) validation identically to `dns`.

## Practical Lab Example: Dual-Stack DNS-SD Service Browse (`PTR`) and Host Address Announcement (`AAAA`) (`mdns_ipv6_matter_discovery.frasm`)

Discovers `_matterc._udp.local` commissioning nodes and announces an IPv6 link-local service record over VLAN 30.

```frameassembly
host controller { ip fe80::10 mac 00:1c:73:aa:00:10 }
host sensor_hub { ip fe80::55 mac 00:1c:73:aa:00:55 }

compile {
    // 1. Multicast browse query to ff02::fb on UDP 5353 -> 5353
    controller -> sensor_hub mdns query qname "_matter._tcp.local" qtype "PTR" vlan 30 ttl 255 wait 2ms
    // 2. Hub announces PTR instance and AAAA address record
    sensor_hub -> controller mdns response qname "_matter._tcp.local" qtype "PTR" rdata "hub-01._matter._tcp.local" ansttl 4500 vlan 30 ttl 255 wait 1ms
    sensor_hub -> controller mdns response qname "hub-01.local" qtype "AAAA" rdata "fe80::55" ansttl 120 vlan 30 ttl 255
}
```

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

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

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

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

## All `mdns` Keywords & Header Options

Packet line pattern: `src_host -> dst_host mdns [query | response] [txid <0..65535>] [qname "<text>"] [qtype "<text>"] [rdata "<text>"] [rcode <0..15>] [ansttl <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `query \| response` | mDNS message direction keyword | `query` | Selects mDNS Question (`0x0000`) or Authoritative Multicast Response (`0x8400`). |
| `qname "<text>"` | `.local` domain or service string | `"_http._tcp.local"` | Encodes the Question/Answer domain labels (`<= 63`B per label, `<= 253`B total). |
| `qtype "<text>"` | "PTR" \| "SRV" \| "TXT" \| "A" \| "AAAA" \| "ANY" | `"PTR"` | Sets the 16-bit DNS record type. |
| `rdata "<text>" \| ansttl <number>` | Record data string, u32 seconds | `ansttl 120` | Encodes the Answer RR payload and cache lifetime on `mdns response` frames. |
| `txid <0..65535> \| rcode <0..15>` | 16-bit ID, 4-bit RCODE | `txid 0, rcode 0` | Overrides Transaction ID (legacy unicast queries) or response code. |

## Cross-Layer Options Valid with `mdns`

| 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 5353 -> 5353 (224.0.0.251 / ff02::fb)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### mDNS Is Strictly UDP-Only (transport tcp Is Rejected)

- **Avoid:** `laptop -> printer mdns query qname "_http._tcp.local" transport tcp`
- **Valid v3.2.2 Syntax:** `laptop -> printer mdns query qname "_http._tcp.local"`

RFC 6762 Multicast DNS operates exclusively over UDP `5353`; `mdns` is not on the 9-protocol `transport` whitelist.

### Use ansttl for Resource Record TTL vs ttl for IP Hop Limit

- **Avoid:** `printer -> laptop mdns response rdata "192.168.10.90" ans_ttl 120`
- **Valid v3.2.2 Syntax:** `printer -> laptop mdns response rdata "192.168.10.90" ansttl 120 ttl 255`

`ansttl 120` sets the mDNS resource record cache lifetime, while `ttl 255` sets the IPv4/IPv6 Hop Limit.
