# ICMPv4/v6, Dual-Stack Flows, Fragmentation & DiffServ QoS

> **FrameAssembly v3.2.2** · Anthology 02 (Layer 3 · 14 Recipes) · MIT License · Canonical URL: https://frameassembly-lang.dev/docs/examples/icmp-dual-stack-qos

Reusable IPv4 and IPv6 `flow(src, dst)` reachability checks, Path MTU Discovery (`df`), manual IPv4 fragment offsets (`ipid`, `mf`, `fragoffset`), IPv6 Neighbor Discovery (`ndp`), and IGMP/MLD group joins.

## Key Wire & Syntax Takeaways

- Define one `flow name(a, b)` block and invoke it over IPv4 and IPv6 host pairs so `icmp` morphs cleanly between IPv4 Proto 1 and IPv6 Next Header 58.
- `igmp` validates IPv4 endpoints exclusively, whereas `ndp` and `mld` validate IPv6 endpoints and RFC multicast MACs (`33:33:xx:xx:xx:xx`).
- Unified `ttl`, `dscp`, and `ecn` modifiers work identically across IPv4 TOS and IPv6 Traffic Class headers.

## 02.01 · Dual-Stack IPv4 & IPv6 Reachability via Parameterized flow(a, b) (`dual_stack_icmp_flow.frasm`)

Reuses a single flow(src, dst) definition across both IPv4 and IPv6 host pairs so protocol builders automatically morph from IPv4 ICMP (Proto 1) to IPv6 ICMPv6 (Next Header 58).

- **Protocols:** `icmp` (2 Ethernet frames)
- **Wire Behavior:** flowlabel 0x12345 applies to the IPv6 header and is cleanly ignored when the same flow runs over IPv4 endpoints.
- **Wireshark Filter:** `icmp || icmpv6`
- **CLI Build:** `frameassembly dual_stack_icmp_flow.frasm`

```frameassembly
host probe_v4 { ip 10.20.1.10 }
host core_v4  { ip 10.20.1.1 }
host probe_v6 { ip 2001:db8:20::10 }
host core_v6  { ip 2001:db8:20::1 }

flow reachability_check(src, dst) {
    src -> dst icmp echorequest id 0x4200 seq 1 dscp 46 ecn 2 flowlabel 0x12345 wait 2ms
    src <- dst icmp echoreply id 0x4200 seq 1 dscp 46 ecn 2 flowlabel 0x12345 wait 5ms
}

compile {
    reachability_check(probe_v4, core_v4)
    reachability_check(probe_v6, core_v6)
}
```

## 02.02 · IPv4 Multi-Fragment Reassembly Test (ipid, mf & fragoffset) (`ipv4_fragment_reassembly.frasm`)

Emits two correlated IPv4 UDP fragments sharing ipid 0x7788 to verify firewall fragment-reassembly engines and Suricata defrag thresholds.

- **Protocols:** `udp` (2 Ethernet frames)
- **Wire Behavior:** Fragment 1 sets More-Fragments (mf) with fragoffset 0; Fragment 2 clears mf and sets fragoffset 2 (16-byte offset in 8-byte units).
- **Wireshark Filter:** `ip.id == 0x7788`
- **CLI Build:** `frameassembly ipv4_fragment_reassembly.frasm`

```frameassembly
host sender_box { ip 172.16.30.10 }
host dpi_sensor { ip 172.16.30.50 }

compile {
    sender_box -> dpi_sensor udp srcport 54100 dstport 9000 ipid 0x7788 mf fragoffset 0 payload "FRAGMENT-PART-01" wait 1ms
    sender_box -> dpi_sensor udp srcport 54100 dstport 9000 ipid 0x7788 fragoffset 2 payload "FRAGMENT-PART-02"
}
```

## 02.03 · UDP Traceroute Hop Expiration (TTL=1 Time Exceeded + Port Unreachable) (`traceroute_ttl_hops.frasm`)

Reproduces a classic Unix UDP traceroute across an intermediate router hop (TTL=1 -> ICMP Time Exceeded) followed by arrival at the target host (TTL=2 -> ICMP Port Unreachable).

- **Protocols:** `udp`, `icmp` (4 Ethernet frames)
- **Wire Behavior:** ICMP Type 11 Code 0 marks transit TTL expiry; ICMP Type 3 Code 3 signals the final destination port 33435.
- **Wireshark Filter:** `udp.port >= 33434 || icmp`
- **CLI Build:** `frameassembly traceroute_ttl_hops.frasm`

```frameassembly
host workstation { ip 10.10.0.25 }
host hop1_router { ip 10.10.0.1 }
host dest_server { ip 10.90.0.40 }

compile {
    workstation -> dest_server udp srcport 51200 dstport 33434 ttl 1 payload "probe-hop-1" wait 2ms
    hop1_router -> workstation icmp timeexceeded type 11 code 0 wait 5ms
    workstation -> dest_server udp srcport 51200 dstport 33435 ttl 2 payload "probe-hop-2" wait 3ms
    dest_server -> workstation icmp destunreach type 3 code 3
}
```

## 02.04 · IPv6 Router Discovery & Neighbor Solicitation / Advertisement (NDP) (`ipv6_ndp_slaac_resolution.frasm`)

Captures IPv6 Router Solicitation, Router Advertisement, and link-layer Neighbor Solicitation / Advertisement with Hop Limit 255.

- **Protocols:** `ndp` (4 Ethernet frames)
- **Wire Behavior:** ndp automatically sets IPv6 Hop Limit to 255 per RFC 4861 so router anti-spoofing checks pass.
- **Wireshark Filter:** `icmpv6`
- **CLI Build:** `frameassembly ipv6_ndp_slaac_resolution.frasm`

```frameassembly
host host_v6   { ip fe80::21a:2bff:fe3c:4d5e mac 00:1a:2b:3c:4d:5e }
host router_v6 { ip fe80::1                   mac 00:1c:73:00:00:01 }

compile {
    host_v6 -> router_v6 ndp routersolicit slla 00:1a:2b:3c:4d:5e wait 3ms
    host_v6 <- router_v6 ndp routeradvert routerlifetime 1800 slla 00:1c:73:00:00:01 wait 5ms
    host_v6 -> router_v6 ndp neighborsolicit target fe80::1 slla 00:1a:2b:3c:4d:5e wait 1ms
    host_v6 <- router_v6 ndp neighboradvert target fe80::1 tlla 00:1c:73:00:00:01
}
```

## 02.05 · Path MTU Discovery Check (IPv4 DF Bit + ICMP Fragmentation Needed) (`fw_pmtud_df_check.frasm`)

Checks that an edge firewall permits ICMP Type 3 Code 4 (Fragmentation Needed) when a Don't-Fragment (df) HTTPS segment hits a lower-MTU IPSec or GRE tunnel.

- **Protocols:** `tcp`, `http`, `icmp` (5 Ethernet frames)
- **Wire Behavior:** Blocking ICMP Type 3 Code 4 causes classic "pings work, but HTTPS hangs" tickets. Use df on IPv4 packets to set the Don't-Fragment bit cleanly.
- **Wireshark Filter:** `ip.flags.df == 1 || (icmp.type == 3 && icmp.code == 4)`
- **CLI Build:** `frameassembly fw_pmtud_df_check.frasm`

```frameassembly
// Branch client, VPN tunnel router (1400-byte MTU), and remote web server
host branch_client { ip 10.25.1.15 }
host vpn_router    { ip 10.25.1.1 }
host remote_web    { ip 10.90.5.80 }

compile {
    // 1. Normal TCP 3-way handshake succeeds (small 60-byte packets pass any MTU)
    branch_client -> remote_web tcp syn srcport 54210 dstport 80 win 65535 mss 1460 df wait 2ms
    branch_client <- remote_web tcp syn ack win 65535 mss 1460 df wait 2ms
    branch_client -> remote_web tcp ack df wait 1ms

    // 2. Client sends HTTP request with Don't-Fragment (df) flag set
    branch_client -> remote_web http post uri "/upload" hosthdr "files.corp.internal" body "large-unfragmentable-payload-block" df wait 2ms

    // 3. Tunnel router notifies client to lower its packet size (ICMP Type 3, Code 4)
    vpn_router -> branch_client icmp destunreach type 3 code 4
}
```

## 02.06 · ICMP Ping Sweep (Live Replies vs. Host Unreachable) (`scan_icmp_ping_sweep.frasm`)

Pings three subnet targets in sequence: two active servers answer with Echo Reply, while the router reports Host Unreachable (Type 3, Code 1) for a dead address.

- **Protocols:** `icmp` (6 Ethernet frames)
- **Wire Behavior:** Use this capture in Wireshark training to compare fast sub-millisecond LAN replies against router-generated ICMP Destination Unreachable responses.
- **Wireshark Filter:** `icmp`
- **CLI Build:** `frameassembly scan_icmp_ping_sweep.frasm`

```frameassembly
// Admin workstation, branch router, and three target IPs
host admin_pc   { ip 10.12.0.50 }
host branch_rtr { ip 10.12.10.1 }
host dns_01     { ip 10.12.10.10 }
host print_01   { ip 10.12.10.20 }
host offline_pc { ip 10.12.10.99 }

compile {
    // Target 1 (.10): Online DNS server replies in 1ms
    admin_pc -> dns_01 icmp echorequest id 300 seq 1 wait 1ms
    admin_pc <- dns_01 icmp echoreply id 300 seq 1 wait 3ms

    // Target 2 (.20): Online printer replies in 2ms
    admin_pc -> print_01 icmp echorequest id 300 seq 2 wait 2ms
    admin_pc <- print_01 icmp echoreply id 300 seq 2 wait 3ms

    // Target 3 (.99): Offline host -> Router returns ICMP Host Unreachable (Type 3, Code 1)
    admin_pc -> offline_pc icmp echorequest id 300 seq 3 wait 15ms
    branch_rtr -> admin_pc icmp destunreach type 3 code 1
}
```

## 02.07 · Minimal Two-Block ICMP / ICMPv6 (`icmp`) Quickstart (`icmp_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Internet Control Message Protocol v4 & Dual-Stack ICMPv6 (IP Proto 1 (v4) / NextHdr 58 (v6), Dual-Stack Morphing (v4/v6)).

- **Protocols:** `icmp` (2 Ethernet frames)
- **Wire Behavior:** Automatically selects IPv4 Protocol `1` (`0x0800`) over IPv4 endpoints or ICMPv6 Next Header `58` (`0x86dd`) over IPv6 endpoints.
- **Wireshark Filter:** `icmp || icmpv6`
- **CLI Build:** `frameassembly icmp_quickstart.frasm`

```frameassembly
host client { ip 10.0.0.10 }
host router { ip 10.0.0.1 }

compile {
    client -> router icmp echorequest wait 2ms
    client <- router icmp echoreply
}
```

## 02.08 · Minimal Two-Block IPv6 NDP (`ndp`) Quickstart (`ndp_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for IPv6 Neighbor Discovery Protocol (ICMPv6 NextHdr 58 (Hop Limit 255), IPv6-Only).

- **Protocols:** `ndp` (2 Ethernet frames)
- **Wire Behavior:** Enforces IPv6 Hop Limit `255` (`ttl 255`) by default in accordance with RFC 4861 §6.1.2 / §7.1.1.
- **Wireshark Filter:** `icmpv6.type >= 133 && icmpv6.type <= 137`
- **CLI Build:** `frameassembly ndp_quickstart.frasm`

```frameassembly
host host_v6   { ip 2001:db8:cafe::25 mac 00:1b:21:aa:00:25 }
host router_v6 { ip 2001:db8:cafe::1  mac 00:1c:73:bb:00:01 }

compile {
    host_v6 -> router_v6 ndp neighborsolicit target 2001:db8:cafe::1 slla 00:1b:21:aa:00:25 wait 1ms
    host_v6 <- router_v6 ndp neighboradvert target 2001:db8:cafe::1 tlla 00:1c:73:bb:00:01
}
```

## 02.09 · Minimal Two-Block IGMPv1/v2/v3 (`igmp`) Quickstart (`igmp_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Internet Group Management Protocol v1, v2 & v3 (IPv4 Proto 2 (TTL 1), IPv4-Only).

- **Protocols:** `igmp` (2 Ethernet frames)
- **Wire Behavior:** Encapsulates inside IPv4 Protocol `2` with link-local `ttl 1` by default.
- **Wireshark Filter:** `igmp`
- **CLI Build:** `frameassembly igmp_quickstart.frasm`

```frameassembly
host querier  { ip 10.20.1.1 }
host receiver { ip 10.20.1.55 }

compile {
    querier -> receiver igmp v2 query group 224.0.0.1 maxresp 100 wait 5ms
    receiver -> querier igmp v2 report group 239.1.1.10
}
```

## 02.10 · Minimal Two-Block IPv6 MLD (`mld`) Quickstart (`mld_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Multicast Listener Discovery v1 & v2 (IPv6) (ICMPv6 NextHdr 58 (Hop Limit 1), IPv6-Only).

- **Protocols:** `mld` (2 Ethernet frames)
- **Wire Behavior:** Sets IPv6 Next Header to `58` (ICMPv6) and defaults Hop Limit (`ttl`) to `1` per RFC 2710.
- **Wireshark Filter:** `icmpv6.type >= 130 && icmpv6.type <= 132`
- **CLI Build:** `frameassembly mld_quickstart.frasm`

```frameassembly
host rtr_v6  { ip fe80::1 }
host node_v6 { ip fe80::25 }

compile {
    rtr_v6 -> node_v6 mld query maxrespdelay 10000 wait 3ms
    node_v6 -> rtr_v6 mld report multicastaddr ff02::1:ff00:25
}
```

## 02.11 · ICMP / ICMPv6 (`icmp`): Dual-Stack Echo Flow Plus IPv4 Fragmentation-Needed & Traceroute TTL Expiry (`icmp_dualstack_pmtud.frasm`)

Reuses a `ping_test(src, dst)` flow across IPv4 and IPv6 hosts, then sends a router Time Exceeded (`ttl 1`) and Fragmentation Needed (`destunreach code 4`) reply.

- **Protocols:** `icmp` (5 Ethernet frames)
- **Wire Behavior:** Automatically selects IPv4 Protocol `1` (`0x0800`) over IPv4 endpoints or ICMPv6 Next Header `58` (`0x86dd`) over IPv6 endpoints. Translates `echorequest` (`8` v4 / `128` v6), `echoreply` (`0` v4 / `129` v6), `destunreach` (`3` v4 / `1` v6), `timeexceeded` (`11` v4 / `3` v6), and `redirect` (`5` v4 / `137` v6).
- **Wireshark Filter:** `icmp || icmpv6`
- **CLI Build:** `frameassembly show icmp_dualstack_pmtud.frasm`

```frameassembly
host app_v4  { ip 10.10.0.25 }
host edge_v4 { ip 10.10.0.1 }
host app_v6  { ip 2001:db8:10::25 }
host edge_v6 { ip 2001:db8:10::1 }

flow ping_test(src, dst) {
    src -> dst icmp echorequest id 0x2026 seq 1 payload "healthcheck-probe" wait 1ms
    src <- dst icmp echoreply id 0x2026 seq 1 payload "healthcheck-probe" wait 2ms
}

compile {
    ping_test(app_v4, edge_v4)
    ping_test(app_v6, edge_v6)
    // Traceroute hop 1 expiry + PMTUD Fragmentation Needed (Type 3, Code 4)
    app_v4 -> edge_v4 icmp echorequest id 0x2026 seq 2 ttl 1 df wait 1ms
    app_v4 <- edge_v4 icmp timeexceeded code 0 wait 2ms
    app_v4 <- edge_v4 icmp destunreach code 4
}
```

## 02.12 · IPv6 NDP (`ndp`): IPv6 SLAAC Router Discovery Followed by Neighbor Solicitation & Advertisement (`ndp_slaac_discovery.frasm`)

Models a dual-interface IPv6 leaf node soliciting an upstream router (`routersolicit` -> `routeradvert`) before resolving next-hop MAC reachability (`neighborsolicit` -> `neighboradvert`).

- **Protocols:** `ndp`, `vlan` (4 Ethernet frames)
- **Wire Behavior:** Enforces IPv6 Hop Limit `255` (`ttl 255`) by default in accordance with RFC 4861 §6.1.2 / §7.1.1. Encodes exact ICMPv6 message bodies for `routersolicit` (`133`), `routeradvert` (`134`, `routerlifetime 1800`), `neighborsolicit` (`135`), `neighboradvert` (`136`, Solicited + Override flags set), and `redirect` (`137`).
- **Wireshark Filter:** `icmpv6.type >= 133 && icmpv6.type <= 137`
- **CLI Build:** `frameassembly show ndp_slaac_discovery.frasm`

```frameassembly
host leaf_node { ip fe80::1b21:ff:feaa:10 mac 00:1b:21:aa:00:10 }
host core_rtr  { ip fe80::1c73:ff:febb:01 mac 00:1c:73:bb:00:01 }

compile {
    // 1. Host sends Router Solicitation with Source Link-Layer Address (SLLA)
    leaf_node -> core_rtr ndp routersolicit slla 00:1b:21:aa:00:10 vlan 100 wait 2ms
    // 2. Router replies with Router Advertisement (30-minute router lifetime)
    core_rtr -> leaf_node ndp routeradvert routerlifetime 1800 slla 00:1c:73:bb:00:01 vlan 100 wait 5ms
    // 3. Neighbor Solicitation & Solicited+Override Neighbor Advertisement
    leaf_node -> core_rtr ndp neighborsolicit target fe80::1c73:ff:febb:01 slla 00:1b:21:aa:00:10 vlan 100 wait 1ms
    leaf_node <- core_rtr ndp neighboradvert target fe80::1c73:ff:febb:01 tlla 00:1c:73:bb:00:01 vlan 100
}
```

## 02.13 · IGMPv1/v2/v3 (`igmp`): VLAN-Tagged IGMPv2 Query,IGMPv3 Receiver Join, and IGMPv2 Fast Leave (`igmp_snooping_vlan.frasm`)

Demonstrates a multicast router polling VLAN 50, one receiver joining `239.255.10.1` via IGMPv3, and a second receiver joining and leaving via IGMPv2.

- **Protocols:** `igmp`, `vlan` (4 Ethernet frames)
- **Wire Behavior:** Encapsulates inside IPv4 Protocol `2` with link-local `ttl 1` by default. Maps version + operation cleanly onto wire Type codes: `0x11` (`query`, 8B in v1/v2 or 12B in v3), `0x12` (`v1 report`), `0x16` (`v2 report`), `0x17` (`v2 leave`), and `0x22` (`v3 report` with `MODE_IS_EXCLUDE` Group Record).
- **Wireshark Filter:** `igmp`
- **CLI Build:** `frameassembly show igmp_snooping_vlan.frasm`

```frameassembly
host mcast_rtr { ip 10.50.0.1 }
host all_hosts { ip 224.0.0.1 }
host decoder_1 { ip 10.50.0.21 }
host decoder_2 { ip 10.50.0.22 }

compile {
    // 1. General Query to 224.0.0.1 (auto-derives MAC 01:00:5e:00:00:01)
    mcast_rtr -> all_hosts igmp v2 query group 0.0.0.0 maxresp 100 vlan 50 wait 4ms
    // 2. Receiver 1 sends IGMPv3 Membership Report for 239.255.10.1
    decoder_1 -> mcast_rtr igmp v3 report group 239.255.10.1 vlan 50 wait 3ms
    // 3. Receiver 2 joins and then leaves 239.255.10.2 via IGMPv2
    decoder_2 -> mcast_rtr igmp v2 report group 239.255.10.2 vlan 50 wait 20ms
    decoder_2 -> mcast_rtr igmp v2 leave group 239.255.10.2 vlan 50
}
```

## 02.14 · IPv6 MLD (`mld`): Link-Local MLD General Query, Solicited-Node Join, and Listener Done (`mld_snooping_ipv6.frasm`)

Simulates an IPv6 multicast router querying all link-local nodes (`ff02::1`), a subscriber reporting membership in `ff05::100`, and cleanly departing with `mld done`.

- **Protocols:** `mld`, `vlan` (3 Ethernet frames)
- **Wire Behavior:** Sets IPv6 Next Header to `58` (ICMPv6) and defaults Hop Limit (`ttl`) to `1` per RFC 2710. Encodes ICMPv6 Type `130` (`query`), `131` (`report`), or `132` (`done`), 16-bit `Maximum Response Delay` (milliseconds), and 128-bit `Multicast Address`.
- **Wireshark Filter:** `icmpv6.type >= 130 && icmpv6.type <= 132`
- **CLI Build:** `frameassembly show mld_snooping_ipv6.frasm`

```frameassembly
host querier_v6 { ip fe80::1c73:1 }
host all_nodes  { ip ff02::1 }
host all_rtrs   { ip ff02::2 }
host listener   { ip fe80::1b21:50 }

compile {
    // 1. General MLD Query to ff02::1 (multicastaddr :: = all groups, 10000ms max delay)
    querier_v6 -> all_nodes mld query multicastaddr :: maxrespdelay 10000 vlan 200 wait 4ms
    // 2. Host joins site-local multicast stream ff05::100
    listener -> querier_v6 mld report multicastaddr ff05::100 maxrespdelay 0 vlan 200 wait 25ms
    // 3. Host sends MLD Done to all-routers multicast ff02::2 upon leaving
    listener -> all_rtrs mld done multicastaddr ff05::100 vlan 200
}
```
