Skip to content

Protocol Library Radio

finthetechguy edited this page Nov 23, 2026 · 1 revision

Radio Interface

The library reaches the air only through the Radio trait. Everything above it (scanning, link setup, the exchange) is written against the trait and runs the same on any backend. This page describes the trait and what a backend must do. The Linux implementation is described on Linux Backend.

Implemented in: radio, radio::pcap, wifi::radiotap.

The trait

pub trait Radio {
    /// Address the radio acknowledges frames for.
    fn mac(&self) -> Mac;
    fn set_channel(&mut self, channel: u8) -> io::Result<()>;
    /// Injects a frame given without FCS at `rate`, in units of 500 kbit/s.
    fn send(&mut self, frame: &[u8], rate: u8) -> io::Result<()>;
    /// Waits up to `timeout` for the next frame.
    fn recv(&mut self, timeout: Duration) -> io::Result<Option<Received>>;
}

pub struct Received {
    pub info: RxInfo,   // frequency, signal, bad_fcs
    pub frame: Vec<u8>, // 802.11 frame, no radiotap header, no FCS
}

&mut R also implements Radio, so a caller can lend a radio to a pass without giving it up.

What a backend must provide

Requirement Why
Receive every frame on the channel, including frames to other addresses Discovery uses broadcast probes and offers to the local MAC.
Inject arbitrary management and data frames ctrpass builds every frame itself, including the MAC header and sequence number.
Send at a chosen rate Probes, offers and link-ups go at 1 Mbit/s (rate 2); accepts and data at 11 Mbit/s (rate 22).
Acknowledge unicast frames sent to mac() at the 802.11 layer Unicast frames from the console expect a link-layer ACK. ctrpass does not build ACK frames itself, so the hardware must send them.
Not wait for ACKs on broadcast frames Nobody acknowledges them.
Tune to channels 1, 6 and 11 Consoles use these. A backend may support fewer; see below.
Report frequency and signal of each frame The link locks onto the channel a console was heard on, and the scanner reports signal.
Drop frames with a bad FCS, strip the FCS The library expects clean frames.
Not return frames the backend itself sent The link also drops frames whose address 2 is its own MAC.

Constants the library offers to backends:

Constant Value Use
RATE_1M 2 1 Mbit/s in 500 kbit/s units
RATE_11M 22 11 Mbit/s
SIGNAL_SETTLE 3 s Signal readings should be left out for this long after the card starts, while drivers settle their noise floor
INTERFACE_PREFIX ctrpass Start of the names of interfaces a backend creates

Fixed-channel backends

A backend that can only use one channel returns an error from set_channel for any other. The caller should then pass only that channel to the pass. The library's channel hopping stays within the configured list, so a one-channel list never hops.

How the library uses it

sequenceDiagram
    participant P as run_pass or scan
    participant R as Radio
    P->>R: mac()
    P->>R: set_channel(first channel)
    loop
        P->>R: send(frame, rate) for each queued frame
        P->>R: set_channel(c) when the link changes channel
        P->>R: recv(5 ms)
        R-->>P: Received frame or None
    end
Loading

The scanner calls set_channel for each channel in turn and calls recv with the time left in the dwell.

Radiotap helpers

Most raw-frame interfaces use radiotap headers. The library provides:

  • radiotap::parse(packet) returns RxInfo and the bare frame. It strips the FCS when the flags say it is there and sets bad_fcs.
  • radiotap::build(frame, rate, no_ack) returns a 12-byte radiotap header with the rate and TX flags, followed by the frame.

Field details are on 802.11 Frames and CCMP.

Capture files

radio::pcap reads classic pcap files (microsecond or nanosecond, either byte order). It yields each packet with its timestamp. Link types 105 (bare 802.11) and 127 (radiotap) are the ones that matter for StreetPass.

A capture can be decoded offline with the same code the link uses. The command-line decode command does this:

  1. Each packet is stripped of radiotap and classified as a discovery frame.
  2. The token of each MAC is remembered from its probes and offers.
  3. For a protected data frame, address 3 is taken as the host. The other address is the client. With both tokens known and a key file, the link key is derived and the frame decrypted.
  4. The YY segment in the plaintext is printed.

A pcap reader does not implement Radio; it is a separate input for offline analysis.

Clone this wiki locally