Add signed private fleet controls for filtering and secondary radio

Enroll a private channel and publisher public key on repeaters and room servers. Local Companion fleet sends sign targeted or whole-fleet commands, with expiry, durable replay protection, and a restricted command allowlist. Preserve exact transmitted-reply barriers for secondary-radio changes, bound validation and replies, and qualify crypto, persistence, transport scope, role permissions, and compact-profile exclusions with regression tests.
This commit is contained in:
mikecarper committed 2026-10-08 16:45:19 -07:00
1 parent a14bbdc58f
commit d83fa7cf8e
27 files changed
+3028 -2

No files matched your search

+102
View File
@@ -4,6 +4,7 @@
#include "NotificationSettingsFile.h"
#include "MyMesh.h"
#include <helpers/CompanionTxRoutingCLI.h>
#include <helpers/FleetCommand.h>
#include "CompanionBluetooth.h"
#include "CompanionWireless.h"
#include "CompanionRetry.h"
@@ -9704,6 +9705,107 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
&& meshBleTraceCommand(command, reply, reply_capacity)) return true;
#endif
#if MESH_ENABLE_FLEET_CONTROL
if (!strncmp(command, "fleet", 5)
&& (command[5] == 0 || command[5] == ' ' || command[5] == '\t')) {
// Only the attached Companion client can originate multicast management.
// A remote CLI peer must not borrow this Companion's signing identity.
if (sender_timestamp != 0) {
snprintf(reply, reply_capacity, "Error: fleet send requires local Companion access");
return true;
}
const char* text = command + 5;
while (*text == ' ' || *text == '\t') ++text;
if (strncmp(text, "send", 4)
|| (text[4] != ' ' && text[4] != '\t')) {
snprintf(reply, reply_capacity,
"Error: use fleet send <channel-index> <all|64-hex-key> <command>");
return true;
}
text += 4;
while (*text == ' ' || *text == '\t') ++text;
uint32_t channel_index = 0;
bool have_index = false;
while (*text >= '0' && *text <= '9') {
have_index = true;
const uint8_t digit = *text++ - '0';
if (channel_index > (0xFFFFFFFFUL - digit) / 10UL) {
snprintf(reply, reply_capacity, "Error: invalid fleet channel index");
return true;
}
channel_index = channel_index * 10UL + digit;
}
if (!have_index || channel_index >= MAX_GROUP_CHANNELS
|| (*text != ' ' && *text != '\t')) {
snprintf(reply, reply_capacity, "Error: invalid fleet channel index");
return true;
}
while (*text == ' ' || *text == '\t') ++text;
// Reuse the wire buffer for the temporary target token. Encoding happens
// only after the target has been parsed into its separate binary buffer.
uint8_t payload[mesh::FleetCommand::MaxPayloadLength];
const size_t target_length = strcspn(text, " \t");
if (target_length > PUB_KEY_SIZE * 2U
|| text[target_length] == 0) {
snprintf(reply, reply_capacity, "Error: fleet target must be all or a full public key");
return true;
}
memcpy(payload, text, target_length);
payload[target_length] = 0;
uint8_t target[mesh::FleetCommand::TargetSize];
if (!mesh::FleetCommand::parseTarget((const char*)payload, target)) {
snprintf(reply, reply_capacity, "Error: fleet target must be all or a full public key");
return true;
}
text += target_length;
while (*text == ' ' || *text == '\t') ++text;
if (!mesh::FleetCommand::commandAllowed(text)) {
snprintf(reply, reply_capacity, "Error: command is not permitted for fleet management");
return true;
}
ChannelDetails channel;
if (!getChannel((int)channel_index, channel) || !channel.name[0]) {
snprintf(reply, reply_capacity, "Error: fleet channel is not configured");
return true;
}
bool padded_key = true;
for (size_t i = mesh::FleetCommand::KeySize;
i < sizeof(channel.channel.secret); ++i)
padded_key = padded_key && channel.channel.secret[i] == 0;
if (!padded_key || !mesh::FleetCommand::privateKeyAllowed(channel.channel.secret)) {
snprintf(reply, reply_capacity, "Error: fleet requires a private 128-bit channel key");
return true;
}
const uint32_t sequence = getRTCClock()->getCurrentTime();
static uint32_t last_fleet_sequence = 0;
if (sequence < mesh::FleetCommand::MinEpoch
|| sequence > 0xFFFFFFFFUL - mesh::FleetCommand::MaxLifetime) {
snprintf(reply, reply_capacity, "Error: set the Companion clock before fleet send");
return true;
}
if (sequence <= last_fleet_sequence) {
snprintf(reply, reply_capacity, "Error: wait for the next clock second before fleet send");
return true;
}
const size_t length = mesh::FleetCommand::encode(
self_id, channel.channel.secret, sequence,
sequence + mesh::FleetCommand::MaxLifetime, target, text,
payload, sizeof(payload));
if (!length) {
snprintf(reply, reply_capacity, "Error: fleet command is too long or invalid");
} else if (!sendGroupData(channel.channel, nullptr, OUT_PATH_UNKNOWN,
mesh::FleetCommand::DataType, payload, (int)length)) {
snprintf(reply, reply_capacity, "Error: could not queue fleet command");
} else {
last_fleet_sequence = sequence;
snprintf(reply, reply_capacity, "OK - fleet command queued; seq=%lu",
(unsigned long)sequence);
}
memset(payload, 0, sizeof(payload));
return true;
}
#endif
if (_radio_profiles.handle(command, reply, reply_capacity, sender_timestamp != 0)) return true;
if (!strncmp(command, "set tempradio ", 14)) command += 4;
if (!strcmp(command, "get tempradio")) command += 4;
+29
View File
@@ -2816,6 +2816,13 @@ void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32
}
void MyMesh::onGroupPacketRecv(mesh::Packet* packet) {
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) {
TransportKey scope;
const bool known = mesh::captureFleetReplyScope(region_map, recv_pkt_region, packet, scope);
_cli.fleetChannel()->receive(packet, *this, known ? &scope : nullptr);
}
#endif
#if !defined(PORTABLE_MQTT_OBSERVER) && MESH_ENABLE_CLOCK_SYNC
// The base Mesh calls this for every unseen, structurally valid group packet
// before the forwarding decision. Public-channel decryption below also
@@ -4156,6 +4163,9 @@ bool MyMesh::sendRepeatersFloodText(const char* text, const TransportKey* scope,
void MyMesh::onSendComplete(mesh::Packet* packet) {
mesh::Mesh::onSendComplete(packet);
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) _cli.fleetChannel()->complete(packet, _cli.radioProfiles());
#endif
if (temp_radio_reply_barrier.complete(packet) && !temp_radio_reply_barrier.waiting()) finishRadioReply(true);
if (packet == pending_battery_alert_packet) {
pending_battery_alert_packet = NULL;
@@ -4166,12 +4176,18 @@ void MyMesh::onSendComplete(mesh::Packet* packet) {
void MyMesh::onRadioProfileCopyQueued(mesh::Packet* packet, const mesh::Packet* original,
uint8_t priority) {
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) _cli.fleetChannel()->copy(packet, original);
#endif
temp_radio_reply_barrier.trackCopy(original, packet);
mesh::Mesh::onRadioProfileCopyQueued(packet, original, priority);
}
void MyMesh::onSendFail(mesh::Packet* packet) {
mesh::Mesh::onSendFail(packet);
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) _cli.fleetChannel()->fail(packet, _cli.radioProfiles());
#endif
if (temp_radio_reply_barrier.fail(packet)) {
// Failure of every acknowledgement copy cancels the unconfirmed
// handoff. A later cached-command retry may replay the reply, but it does
@@ -12585,6 +12601,19 @@ void MyMesh::loop() {
_cli.loop();
serviceRadioReplyDeadline();
processDeferredCliCommand();
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) {
_cli.fleetChannel()->service(*this, _cli.radioProfiles(), _prefs.node_name,
[this](uint32_t sequence, char* command, char* reply) {
if (region_load_active) {
strcpy(reply, "Err - region load active; retry later"); return;
}
// Signature and a strict command allowlist are checked before this
// dispatch. Fleet capability never grants unrestricted admin CLI.
handleCommand(sequence, nullptr, command, reply);
});
}
#endif
servicePostMeshLoop();
#if defined(ENABLE_OTA) && OTA_DYNAMIC_CONTEXT
mesh::ota::ota_service_temp_radio_context(isAnyTempRadioActive());
+1
View File
@@ -1092,6 +1092,7 @@ public:
bool sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
// CommonCLICallbacks
bool supportsFleetControl() const override { return true; }
mesh::Radio* getProfileRadio() override { return _radio; }
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins, uint16_t preamble = 0) override;
bool scheduleNormalRadio() override;
+38
View File
@@ -1166,9 +1166,35 @@ void MyMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
}
void MyMesh::onGroupPacketRecv(mesh::Packet* packet) {
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) {
TransportKey scope;
const bool known = mesh::captureFleetReplyScope(region_map, recv_pkt_region, packet, scope);
_cli.fleetChannel()->receive(packet, *this, known ? &scope : nullptr);
}
#endif
_clock_sync.observeGroupPacket(packet);
}
#if MESH_ENABLE_FLEET_CONTROL
void MyMesh::onSendComplete(mesh::Packet* packet) {
mesh::Mesh::onSendComplete(packet);
if (_cli.fleetChannel()) _cli.fleetChannel()->complete(packet, _cli.radioProfiles());
}
void MyMesh::onSendFail(mesh::Packet* packet) {
mesh::Mesh::onSendFail(packet);
if (_cli.fleetChannel()) _cli.fleetChannel()->fail(packet, _cli.radioProfiles());
}
void MyMesh::onRadioProfileCopyQueued(mesh::Packet* packet,
const mesh::Packet* original,
uint8_t priority) {
mesh::Mesh::onRadioProfileCopyQueued(packet, original, priority);
if (_cli.fleetChannel()) _cli.fleetChannel()->copy(packet, original);
}
#endif
#if defined(WITH_MQTT_NEIGHBORS)
#define CTL_TYPE_NODE_DISCOVER_REQ 0x80
@@ -2679,6 +2705,18 @@ void MyMesh::loop() {
servicePendingSerialOutput();
#endif
_cli.loop();
#if MESH_ENABLE_FLEET_CONTROL
if (_cli.fleetChannel()) {
_cli.fleetChannel()->service(*this, _cli.radioProfiles(), _prefs.node_name,
[this](uint32_t sequence, char* command, char* reply) {
if (region_load_active) {
strcpy(reply, "Err - region load active; retry later");
return;
}
handleCommand(sequence, command, reply);
});
}
#endif
_clock_sync.loop();
#if MESH_ENABLE_TELEMETRY_HISTORY
sampleTelemetryHistory();
+7
View File
@@ -409,6 +409,12 @@ protected:
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override;
void onGroupPacketRecv(mesh::Packet* packet) override;
#if MESH_ENABLE_FLEET_CONTROL
void onSendComplete(mesh::Packet* packet) override;
void onSendFail(mesh::Packet* packet) override;
void onRadioProfileCopyQueued(mesh::Packet* packet, const mesh::Packet* original,
uint8_t priority) override;
#endif
#if defined(WITH_MQTT_NEIGHBORS)
void onControlDataRecv(mesh::Packet* packet) override;
#endif
@@ -431,6 +437,7 @@ protected:
#endif
public:
bool supportsFleetControl() const override { return true; }
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
void begin(FILESYSTEM* fs);