# pim — RFC 7761 Sparse-Mode Multicast Hellos, Joins & RP (`pim`)

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

- **Full Protocol Name:** Protocol Independent Multicast — Sparse Mode
- **Keyword:** `pim`
- **OSI Layer & Transport:** Layer 3 · IP Proto 103
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 7761
- **Wireshark Display Filter:** `pim`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> PIM-SM (IP Proto 103)`

## Overview

Debug Rendezvous Points (`rp`) without a 6-router multicast lab: `pim` emits IP Protocol `103` `hello`, `joinprune`, and `register` frames with proper RFC 7761 encoded unicast/group addresses.

Protocol Independent Multicast — Sparse Mode (`pim`, RFC 7761) builds shared (`*,G`) and shortest-path (`S,G`) multicast distribution trees across Layer 3 routers over **IP Protocol `103`** (`PIMv2`, header byte `0x20`).

FrameAssembly encodes PIMv2 `hello` neighbor discovery (`Type 0`, carrying Holdtime TLV 1 and DR Priority TLV 19), unicast First-Hop Router `register` (`Type 1` to the Rendezvous Point, checksumming **only the 8-byte PIMv2 header** excluding encapsulated multicast data per RFC 7761 §4.9), and `joinprune` (`Type 3` carrying Encoded-Group and Encoded-Source addresses) across both IPv4 and IPv6.

## Minimal Quickstart (`pim_quickstart.frasm`)

```frameassembly
host fhr_leaf { ip 172.16.20.1 }
host core_rp  { ip 172.16.0.1 }

compile {
    fhr_leaf -> core_rp pim hello holdtime 105 drpriority 100 wait 3ms
    fhr_leaf -> core_rp pim register group 239.10.10.1 source 10.10.1.50 rp 172.16.0.1
}
```

### Automatic Wire Calculations

- Encapsulates over IPv4 Protocol `103` or IPv6 Next Header `103` with PIM Version `2` (`0x20`).
- Computes full-message checksums (including IPv6 pseudo-header on v6) for `hello` and `joinprune`, while restricting `register` checksums to the 8-byte PIMv2 header per RFC 7761 §4.9.
- Derives multicast Ethernet MACs automatically when targeting `224.0.0.13` (`ALL-PIM-ROUTERS`) or `ff02::d`.

## Practical Lab Example: End-to-End Multicast Control Plane: IGMPv3 Join + PIM Hello, Join/Prune & Register (`pim_multicast_tree.frasm`)

Combines a receiver `igmp v3 report` with upstream `pim hello`, `(S,G)` `joinprune`, and First-Hop `pim register` signaling to the Rendezvous Point.

```frameassembly
host receiver_01 { ip 10.40.1.50 }
host lhr_router  { ip 10.40.1.1 }
host pim_mcast   { ip 224.0.0.13 }
host core_rp     { ip 172.16.255.1 }

compile {
    // 1. PIMv2 Hello on multicast 224.0.0.13 electing DR (drpriority 200)
    lhr_router -> pim_mcast pim hello holdtime 105 drpriority 200 ttl 1 dscp 48 wait 2ms
    // 2. Receiver joins SSM channel 232.10.10.5 on LAN
    receiver_01 -> lhr_router igmp v3 report group 232.10.10.5 wait 2ms
    // 3. Last-Hop Router sends PIM Join/Prune upstream toward source 10.10.5.100
    lhr_router -> core_rp pim joinprune group 232.10.10.5 source 10.10.5.100 holdtime 210 dscp 48 wait 3ms
    // 4. Unicast PIM Register encapsulated to Rendezvous Point
    lhr_router -> core_rp pim register group 239.20.20.1 source 10.40.1.50 rp 172.16.255.1 ttl 64 dscp 48
}
```

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

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

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

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

## All `pim` Keywords & Header Options

Packet line pattern: `src_host -> dst_host pim [hello | joinprune | register] [holdtime <0..65535>] [drpriority <number>] [group <ip>] [source <ip>] [rp <ip>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `hello \| joinprune \| register` | PIMv2 Message Type keyword | `hello (Type 0)` | Selects Type `0` Hello, Type `1` unicast Register (to RP), or Type `3` Join/Prune. |
| `holdtime <0..65535>` | Whole number (0..65535) (seconds) | `105 (3.5x 30s Hello)` | Encodes Holdtime Option TLV `1` in `hello` or Holdtime field in `joinprune` (`0xffff` = never expire). |
| `drpriority <number>` | Whole number | `1` | Encodes Designated Router Priority Option TLV `19` in `pim hello` messages. |
| `group <ip> \| source <ip> \| rp <ip>` | IPv4 or IPv6 address literals | `239.1.1.1 / endpoint IPs` | Sets the multicast Group, unicast Source `(S,G)`, and Rendezvous Point addresses in `joinprune` and `register` packets. |

## Cross-Layer Options Valid with `pim`

| 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). |

## Common Mistakes & Quick Fixes

### Spell joinprune and drpriority Without Underscores or Slashes

- **Avoid:** `r1 -> r2 pim join_prune dr_priority 100`
- **Valid v3.2.2 Syntax:** `r1 -> r2 pim joinprune drpriority 100`

All built-in PIM tokens are strictly lowercase alphanumeric (`joinprune`, `drpriority`, `holdtime`).

### Match Address Families Across group, source, and Endpoints

- **Avoid:** `rtr6_a -> rtr6_b pim joinprune group 239.1.1.1`
- **Valid v3.2.2 Syntax:** `rtr6_a -> rtr6_b pim joinprune group ff05::100 source 2001:db8::10`

When running `pim` over IPv6 routers, pass IPv6 multicast `group` (`ffxx::`) and IPv6 `source` literals.
