Skip to content

feat(lan): serve the secure channel over TCP on the LAN, with UDP discovery - #135

Merged
Tichael merged 6 commits into
mainfrom
feat/lan-transport
Dec 6, 2026
Merged

Tichael merged 6 commits into
mainfrom
feat/lan-transport

Conversation

@Tichael

@Tichael Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Phase 2 of OpenCarControls/contracts#44 (LAN direct), firmware core and PC board. Closes #120. Implements OpenCarControls/contracts#52 (contracts#47); the submodule is pinned to that PR's branch in its own commit, to re-pin to main once it merges.

core-interface

  • New lan.rs. The board stays dumb: lan_accept gives it a connection slot, lan_receive_frame takes a frame (without its length prefix) and returns the reply, lan_next_frame / lan_try_next_frame give the next sealed frame, and lan_should_close / lan_close handle closing.
  • What a LAN session gets, newest session first:
    • command responses, in a queue of 4 per connection that is never waited on;
    • a system state with the session's client's command availability and lan_address, sent when the session starts, when either changes, and every 5 s;
    • the newest full vehicle state, like BLE.
  • Slots: LAN_MAX_CLIENTS (2). When both are taken, a connection without a session loses its slot. When both have sessions, the new connection is refused. A connection is closed if it has no handshake within 10 s, or once its sessions have ended.
  • Discovery: lan_discovery_reply (the probe and reply from the contract, rate-limited) and lan_mdns_txt (id, v).
  • Secure channel: ChannelLink now has a LinkKind (Lan or Relay). LAN responses go to their connection's queue, and relay ones keep SECURE_TX_CHANNEL. Handshake inits are capped at MAX_HANDSHAKE_INITS_PER_SECOND (8) before any X25519 work. New helpers newest_session and has_open_session, plus client_command_availability_at.
  • SystemState.lan_address also goes out over BLE with the CAN access state, and again whenever it changes.

PC board: lan_task(port, name, mdns) polls a non-blocking TcpListener, its connections and the UDP discovery socket from one Embassy task, so a slow client can't freeze the executor (#89). It reports its IPv4 address (checked every 30 s). mDNS goes through mdns-sd (0.22, its own thread). I did mDNS on PC, behind [network.lan] mdns.

xtask: [network.lan] (enabled, port defaulting to the contract's, mdns), documented in config.toml.example (off by default). The PC main.rs gets {LAN_SPAWN}. ESP builds print a warning and ignore it until #121.

End-to-end client: tools/lan_client.py (Python noiseprotocol, run with python -I).

  • setup pairs, approves, validates (cluster code from the log) and enrolls through the BLE mock.
  • discover sends the UDP probe.
  • run --lock handshakes and prints the handshake time, the state and the command's response.

Docs: new docs/lan.md, plus docs/architecture.md and the README.

How it was tested

  • Not compiled here: my sandbox has no protoc, so nothing that builds core-interface ran. CI on this PR is the first compile, and I'll fix what it finds.
  • Ran here:
    • cargo fmt --all -- --check and cargo clippy --package xtask -- -D warnings.
    • xtask generating the PC main.rs with LAN on (the lan_task spawn is right).
    • The mdns-sd 0.22 calls, in a scratch crate.
    • tools/lan_client.py's Noise and protobuf helpers, checked against a Python responder (message sizes 96 and 48, as in the contract).
  • New host tests in core-interface/tests/lan.rs:
    • slots and replacement, and the handshake timeout;
    • a new session gets the system state and then the full state;
    • a phone that isn't validated sees every command unavailable;
    • an address change reaches open sessions and BLE;
    • a command over LAN gets the session's phone ID and authorization, and its response comes back on its connection;
    • the probe and reply, including the rate limit, and the mDNS TXT records;
    • the handshake rate limit.
  • Test plan for you (PC firmware, virtual car, [network.lan] enabled = true):
    1. tools/lan_client.py setup, then discover, then run --lock. Check that candump vcan0 shows the 0x310 command frame.
    2. Restart the firmware with the client still holding its session: run again does a fresh handshake.
    3. A third connection while two have sessions is refused (log LAN: refused).
    4. mDNS: avahi-browse -r _opencar._tcp shows id and v only.

Checklist

  • PR title uses conventional-commit style
  • cargo fmt --all -- --check, cargo xtask clippy and cargo xtask test pass: fmt passes; clippy and tests are left to CI (no protoc here)
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml, credentials, build artifacts or .bin files committed (.lan-client.json is gitignored)
  • If the wire format changed: the contracts PR is linked and the submodule bump is its own commit

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA

LAN commands go through the same handle_secure_message path as phase 1: they get the same checks as the phone over BLE, and system commands are dropped. The source_device_id comes from the session's client, never from the app. Nothing new reaches CAN TX.


Generated by Claude Code

…covery

core-interface gets lan.rs: connection slots, frames in and out per
connection (command responses, a system state with the client's command
availability and the LAN address, the full vehicle state), the UDP
discovery reply and the mDNS TXT records. Sessions now know whether their
link is LAN or relay, and handshake inits are rate-limited before any
X25519 work. SystemState.lan_address goes out over BLE too.

The PC board serves it with [network.lan] (non-blocking sockets polled
from one task, mDNS through mdns-sd), and tools/lan_client.py plays the
app's part for an end-to-end test.

Closes #120

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
Enrolling a client key needs a validated phone, so the LAN tests
validate both phones, enroll them, then end the validations by
approving another car, like the secure channel tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael
Tichael marked this pull request as ready for review December 6, 2026 01:06
@Tichael

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware test: WiCAN (ESP32-C3) + virtual-car

Tested together with contracts#52, firmware#136 (built on top of this PR; the WiCAN ran #136) and mobile-app#138, plus this PR's own PC test plan.

Requested changes

  1. Wi-Fi (LAN direct) is the remote tier, like MQTT. Being on the same network as the car is no proof of being at the car, so only Bluetooth gets the proximity (advanced) tier. Over the LAN the device sends the basic state only, and accepts basic vehicle commands only (advanced and system commands are refused), exactly like the relay. This PR follows contracts#52 as written, which will change. In core-interface, a LAN session should get the basic state only (no advanced_state_bytes, as for MQTT in routing.rs). Its client's command availability should show advanced commands unavailable, and handle_secure_message should refuse advanced commands that come over the LAN. Test 9 below shows advanced=6B going to the LAN client today.
  2. Nit: board_pc announces the mDNS instance as OpenCar, while the ESP responder (firmware#136) uses OpenCar-cead. The app doesn't trust the instance name, but two PC devices on one LAN would clash; consider the same name on both boards.

Results

Setup (2026-12-06)

  • WiCAN (ESP32-C3) on firmware#136 da1e97e (includes firmware#135 3b7bc4a), platform virtual-car, [network.lan] enabled = true, mdns = true, MQTT on (plain). Not erased: the bond, the validated phone, the enrolled client key and the device identity from the phase 1 test were kept.
  • App: mobile-app#138 4652eeb, release APK installed over the previous one (pairing kept), Pixel 9 Pro Fold (Android), on the same WiFi as the WiCAN.
  • Contracts: contracts#52 ac2fcad (pinned by all three PRs).
  • The phone runs Tailscale with a subnet route for the home LAN. Every phone test was run twice: through the VPN (the app reached the board via a subnet router), and direct (the app excluded from Tailscale, connecting from the phone's own WiFi address). The container's own probes went through the laptop's Tailscale route.
  • PC firmware test (firmware#135's plan): PC board, virtual car, debug build, LAN on, in the dev container, with tools/lan_client.py.

9. [PR #135] The PR's PC test plan: pass

Two clients enrolled through the BLE mock, A from 127.0.0.1 and B from 172.17.0.2 (the mock tells phones apart by IP).

  1. discover (broadcast and subnet broadcast) finds 8509306d… at 172.17.0.2:4734. run --lock does the handshake in 46 ms, receives the system state (availability and lan_address) then the full state, and the lock gets command response …: ok. candump vcan0 couldn't be checked: the dev container can't create vcan0.
  2. With A holding a session, the firmware was stopped and started again: same identity, and run did a fresh handshake (45 ms).
  3. With A and B both holding sessions, a third connection was closed at once, logged as LAN: refused 127.0.0.1:49880: every connection has a session. Both sessions kept receiving updates (12 each in 25 s).
  4. mDNS (no avahi in the container; checked with raw queries): mdns-sd answers legacy unicast and multicast, with PTR OpenCar._opencar._tcp.local, SRV 4734, TXT id, v=1 only, and A.

On the WiCAN (with firmware#136, which carries this PR's core):

1. [Analysis] Boot, and Bluetooth with the new app: pass

The boot log shows LAN: TCP port 4734, up to 2 clients, UDP discovery on 4735, mDNS on, then LAN: address 10.20.0.212:4734 about 5 s later (after WiFi). The bond and identity were kept. The app connects over Bluetooth as before.

2. [PR] Bluetooth off → Wi-Fi, commands, connection log, settings: pass, but see the tier change below

Through the VPN Direct
BLE drops (Bluetooth off) 01:16:25.353 01:29:39.572
LAN connected (slot 0) +0.30 s +0.07 s
handshake answered in 191 ms 190 ms

Lock and unlock work over Wi-Fi. The connection log shows Wi-Fi connected, and Settings hides the Bluetooth-only sections and shows the Wi-Fi address row. The availability over LAN follows the dev lock (dimmed while locked), and the dev lock can only be unlocked over Bluetooth, as expected for a system command.

3. [PR] Discovery, from the container: pass

  • UDP probe with the device's key ID, and with all zero: a 17-byte OCDR reply, version 1, key ID ceade7dc…, port 4734. Another key ID, a 5- or 16-byte probe, or version 2: no reply.
  • Rate limit: a burst of 20 probes gets 4 replies.
  • mDNS, a legacy-unicast PTR query to the board: answered from 5353 to the sender, ID and question echoed, TTL 10, no cache-flush bit, with PTR OpenCar-cead._opencar._tcp.local, SRV port 4734 → opencar-ceade7dcc2033c4cbc38.local, TXT id=ceade7dcc2033c4cbc38, v=1 only, and A 10.20.0.212. Another service: no answer.

4. [Analysis] Strangers on the LAN can't push the phone off: pass (twice)

With the phone's session in slot 0, three raw TCP connections were opened one second apart and never handshaked. Each took slot 1 from the previous one, and the last was closed 10.0 s after connecting. The phone's session never dropped. The free heap was 66380 B with the phone alone and 65892 B with the phone and one stranger.

5. [PR] Board reset while on Wi-Fi: pass, but slow on the direct path

Through the VPN Direct
reset 01:21:15.8 01:31:59.8
LAN: address (WiFi up) +6.3 s +6.4 s
phone reconnects, fresh handshake +13.4 s (187 ms) +42.9 s (187 ms)

Through the VPN, the subnet router reset the dead connection quickly. On the direct path nothing tells the app the board is gone, so it waits for its idle keepalive (see mobile-app#138).

6. [Analysis] Bluetooth back on → LAN released: pass (twice)

Direct: BLE connected at 01:36:45.9, the app subscribed at 01:36:47.46, and the LAN connection was closed by the peer at 01:36:47.50. The free heap after each LAN close over 6 cycles was 66208–66768 B, with no steady fall.

7. [PR] Manual Wi-Fi address: pass as built, but the requested behaviour changes (see mobile-app#138)

With 10.20.0.212:4734, the app connected at once. Changed to 10.20.0.99:4734, the app dropped the link and reconnected 2.65 s later, after the wrong address failed and discovery found the board.

10. [Analysis] Screen lock and unlock on Wi-Fi: pass

Screen off at 01:33:53, and Android cut the app's network about 4 s after it stopped (board: disconnected (read error) at 01:33:58). App resumed at 01:35:56.4, reconnected at 01:36:05.4: 9 s showing "Offline", waiting out the retry backoff.

Not tested: an address change from DHCP or a WiFi reconnection, the heap with MQTT over TLS, two real phones on the LAN at once (the second session was a PC client), iOS, LTE builds (LAN is left out by design), E-GMP in the car, and the 0x310 frame on candump (no vcan0 in the container).

…racts#52)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
- A secure channel session gets the basic vehicle state only, and its
  client's advanced commands are reported unavailable and refused with
  REJECTED_UNAVAILABLE: reaching the device over a network is no proof of
  being at the car.
- The system state repeats every LAN_SYSTEM_STATE_REPEAT_MS (from the
  contract), which the app's receive timeout relies on.
- lan_mdns_instance_name: the BLE name and the key ID's first four
  characters, so the PC board announces the same name as the ESP.
- lan_is_open, for the boards' logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

Both done in a7bb7fe:

  1. Basic state only. A LAN session gets basic_only(msg) (no advanced_state_bytes), its client availability marks every advanced command unavailable, and handle_secure_message answers an advanced command with REJECTED_UNAVAILABLE ("advanced commands need a Bluetooth connection"). The same applies to any secure-channel session, so the relay gets it for free. Tests: advanced_commands_are_refused_through_a_session, an_advanced_command_over_the_lan_is_refused, and the basic-state test. The system state repeat now uses the contract's system_state_repeat_ms.
  2. mDNS name. New lan_mdns_instance_name() in core-interface (<name>-<first 4 of key ID>); the PC board announces with it, and the ESP responder uses it too (feat(lan): serve LAN direct on ESP WiFi builds #136), so both boards show e.g. OpenCar-cead.

CI is green.


Generated by Claude Code

@Tichael

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware retest: WiCAN (ESP32-C3) + virtual-car

Retest after a7bb7fe, together with contracts#52 b101725, firmware#136 c5fa22f (which carries this PR's core; the WiCAN ran it) and mobile-app#138 555a5d6.

All the requested changes check out. Nothing is left to change from the hardware side.

Setup (2026-12-06, same bench as the first test)

  • WiCAN (ESP32-C3) on firmware#136 c5fa22f (includes firmware#135 a7bb7fe), virtual-car, [network.lan] enabled = true, mdns = true. Not erased (bond, validation, enrolled key and identity kept).
  • App: mobile-app#138 555a5d6, release APK installed over the previous one; Pixel 9 Pro Fold, the app excluded from Tailscale, so it connects directly over WiFi (10.20.0.193).
  • Contracts: contracts#52 b101725 (pinned by the firmware and app PRs).
  • PC checks: PC board, virtual car, debug build, LAN on, in the dev container, with tools/lan_client.py and a small driver for the advanced command.

R2. Wi-Fi is the remote tier, on the device (PC): pass

Over the LAN session, decoded:

  • vehicle state: basic=6B advanced=0B (BLE through the mock still gets the 6-byte advanced state);
  • availability: basic {door_lock: AVAILABLE}, advanced {toggle_custom_state1: UNAVAILABLE};
  • AdvancedCommand.toggle_custom_state1 → status_code 4 (REJECTED_UNAVAILABLE), "command rejected: advanced commands need a Bluetooth connection";
  • the basic door lock → ok.

Regression: with two sessions open, a third connection is still refused (LAN: refused 127.0.0.1:50564: every connection has a session).

R1. Wi-Fi is the remote tier, in the app: pass

Bluetooth off at 04:38:29.19 → LAN connected at 29.39, handshake 190 ms. The pill says Remote, and the dashboard is the remote one (car picture, Doors, Driving, Odometer; no Gear, Speed or Custom 1). Doors lock and unlock work. The connection log says it's Wi-Fi, and Settings still hides the Bluetooth-only sections. Bluetooth back on: BLE connected at 04:56:05.30, and the LAN connection was closed by the app at 06.82.

R7. mDNS instance name: pass

ESP: OpenCar-cead._opencar._tcp.local (legacy-unicast answer, TXT id, v=1 only, A 10.20.0.212). PC: announced over mDNS as 'OpenCar-8509'. The probe reply is unchanged (17 bytes, port 4734).

R8. Log reason for a replaced connection: pass

Three raw TCP connections from the container while the phone held slot 0: both replacements logged as slot taken back, the last connection closed 10.0 s after connecting (no session), and the phone's session untouched.

Not retested (code unchanged): the probe byte format and its rate limit, the 10 s handshake timeout. Still not tested: an address change from DHCP or a WiFi reconnection, the heap with MQTT over TLS, two real phones, iOS, LTE builds, E-GMP in the car, candump (no vcan0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael
Tichael merged commit 5ec1497 into main Dec 6, 2026
9 checks passed
@Tichael
Tichael deleted the feat/lan-transport branch December 6, 2026 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LAN transport in core-interface and on the PC board

2 participants