Repository navigation
Protocol Linux Backend
LinuxRadio is the Linux implementation of the library's Radio trait (see Radio Interface). It is separate from the protocol code. It is compiled only on Linux, and nothing else in the library depends on it. It takes over one wireless card, creates its own monitor interfaces with iw and ip, and sends and receives raw frames on a packet socket.
Implemented in: radio::linux.
| Requirement | Why |
|---|---|
Root, or CAP_NET_ADMIN and CAP_NET_RAW
|
Creating interfaces, changing addresses and opening a raw packet socket |
iw |
Reading card capabilities, adding and deleting interfaces, setting the channel |
ip (iproute2) |
Bringing interfaces up and down, setting the MAC and MTU |
| A card with monitor mode | Receiving every frame and injecting frames |
nmcli, iwctl
|
Optional. Used only when NetworkManager or iwd is present. |
The backend has three ways to set up a card. The difference is how the card acknowledges unicast frames sent to ctrpass's MAC, which consoles need.
| Mode | Interfaces | ACKs frames to its MAC | Channels | Used for |
|---|---|---|---|---|
| Active | One monitor interface with the active flag, holding the MAC; one plain monitor to hold the channel |
Yes | Any | Exchanges, when the card supports active monitor |
| Mixed | One plain monitor for sending and listening; one unassociated station interface holding the MAC | Yes, through the station interface | Channel 1 only | Exchanges on cards without active monitor |
| Listen | One plain monitor | No | Any | Scanning, which only listens |
flowchart TD
open["LinuxRadio::open(interface, mac, force, mode)"] --> conn{"Interface connected to a network, and not forced?"}
conn -- yes --> err1["Error: use another card or force it"]
conn -- no --> caps["Read capabilities with iw phy info"]
caps --> mon{"Supports monitor?"}
mon -- no --> err2["Error"]
mon -- yes --> pick{"Mode given?"}
pick -- no --> auto{"Active monitor supported?"}
auto -- yes --> active["Active"]
auto -- no --> mixed["Mixed"]
pick -- yes --> given["Given mode, Active refused if not supported"]
Without a mode, an exchange uses Active when the card supports it and Mixed otherwise. The command line and the helper also let the user choose Active or Mixed. Scanning always uses Listen.
The backend finds the card's phy with iw dev <interface> info (the wiphy N line), then reads iw phy phyN info:
-
monitor: a
* monitorline under "Supported interface modes". -
active monitor: the text
active monitoranywhere in the output.
sequenceDiagram
participant B as LinuxRadio
participant S as System
B->>S: iw dev IF link (must say Not connected unless forced)
B->>S: iw dev IF info, iw phy phyN info
B->>S: flock /run/lock/ctrpass-phyN.lock
B->>S: iw dev ctrpassN del, ctrpassNt del, ctrpassNs del (leftovers)
B->>S: nmcli device set IF managed no, if managed
B->>S: iwctl device IF set-property Powered off, if iwd is present
B->>S: ip link set IF down
B->>S: Create interfaces for the mode
B->>S: socket(AF_PACKET, SOCK_RAW, ETH_P_ALL), bind to the monitor
B->>S: Read /sys/class/net/ADDRESSED/address and check it is the MAC asked for
IF is the user's interface and N its phy number. The monitor interface is named ctrpassN. Its helpers are ctrpassNt (tuner) and ctrpassNs (station).
- If NetworkManager manages the card, the backend sets it unmanaged with
nmcli, waits up to 5 s for the state to show "unmanaged", then waits 1 more second because the supplicant resets the address and link shortly after. - iwd ignores NetworkManager's unmanaged state, so the backend also asks iwd to power the card off with
iwctl. Ifiwctlis not there, this step does nothing.
iw phy phyN interface add ctrpassN type monitor flags active
ip link set ctrpassN down
ip link set ctrpassN address MAC
ip link set ctrpassN mtu 2000
iw phy phyN interface add ctrpassNt type monitor
ip link set ctrpassNt up
ip link set ctrpassN up
An active monitor gets no channel of its own, so a plain monitor (ctrpassNt) is brought up first to hold the channel.
iw phy phyN interface add ctrpassN type monitor
ip link set ctrpassN mtu 2000
ip link set ctrpassN up
iw phy phyN interface add ctrpassNs type managed
nmcli device set ctrpassNs managed no (when NetworkManager sees it)
ip link set ctrpassNs down
ip link set ctrpassNs address MAC
ip link set ctrpassNs up
The station interface holds the MAC, so the card acknowledges frames sent to it, while the monitor sends and listens. After 1 s the backend checks that the station is still up with the right MAC and sets it again if not, since a network manager may reset a new station.
In this mode the card stays on channel 1. set_channel succeeds for channel 1 and returns an error for any other. The backend reports the fixed channel, and the command line and helper then pass only channel 1 to the pass.
iw phy phyN interface add ctrpassN type monitor
ip link set ctrpassN mtu 2000
ip link set ctrpassN up
No MAC is set, so the card keeps its own.
The monitor's MTU is set to 2000 so a full YY segment fits with its 802.11, CCMP and radiotap headers.
set_channel(c) runs iw dev ctrpassN set channel c. In Mixed mode it refuses every channel but 1.
The library passes the store's MAC (see Store) to open. The backend sets it on the interface that carries the address (the active monitor or the station) and then reads it back from /sys/class/net/<interface>/address. If the card kept a different address, opening fails.
A MAC rotation needs a new LinuxRadio. The command line and helper drop the radio and open it again with the new MAC.
send(frame, rate):
- Checks whether address 1 is broadcast.
- Builds the 12-byte radiotap header with the rate, and the no-ACK TX flag for broadcast frames (see 802.11 Frames and CCMP).
- Writes header and frame to the packet socket with
send.
recv(timeout):
- Waits on the socket with
polluntil the timeout. - Reads one packet with
recvfrom. - Skips packets the kernel marks as outgoing, which are the backend's own injected frames.
- Parses the radiotap header. Skips packets that do not parse or have a bad FCS.
- Clears the signal reading during the first 3 s after opening, while drivers settle.
- Returns the frame without radiotap header or FCS.
Only one ctrpass may use a card. The backend takes an exclusive, non-blocking flock on /run/lock/ctrpass-phyN.lock and fails if another process holds it.
When the radio is dropped, it puts the card back:
flowchart LR
drop["LinuxRadio dropped"] --> del["iw dev ctrpassN, ctrpassNt, ctrpassNs del"]
del --> iwd["iwctl Powered on, if it was turned off"]
iwd --> up["ip link set IF up, if it was up"]
up --> nm["nmcli managed yes, if it was managed"]
nm --> lock["Release the phy lock"]
The lock is released last, after the card has been restored.
The desktop app lists wireless interfaces from /sys/class/net: every entry with a phy80211 child, skipping names that start with ctrpass. Interfaces that are not connected (no carrier) are listed first.
- Linux Helper and polkit: how the desktop app gets a privileged process that owns the radio.
- Radio Interface: the trait this backend implements.
How to set up
- Overview and requirements
- Choosing a Wi-Fi adapter
- Building and installing
- Making the key file
- Desktop app
- Command line
- Messages and emulators
- Troubleshooting
Plugins
Protocol
- Overview
- Discovery
- Link setup
- Frames and CCMP
- YY transport
- Exchange flow
- Exchange objects
- CEC messages
- CEC message boxes
- Mii records
- Mii Plaza
- Keys
ctrpass library
Linux