GPS: optional anti-spoofing guard for position and time (GPS_GUARD)
A spoofed GPS signal can move a fixed node anywhere and set its clock to any time; the clock only moves forward, so a time pushed into the future breaks timestamps and replay protection until fixed by hand. With GPS_GUARD, MicroNMEALocationProvider asks a GpsGuard before a fix is stored as the last fix (satellites, HDOP, optional region box, fence around the configured position, jump speed, N stable fixes) and before GPS time is written to the clock (trusted fix current, not before the build date, small steps once the clock is valid, drift-sized after the first sync). Without a trusted fix no live position is reported. CLI in CommonGpsPrefs: 'gps guard', 'gps guard accept', 'gps fence', 'gps ref'; CommonCLI keeps the reference on set lat/lon and gps setloc, resets the time anchor on manual clock sets and adds 'time force'. Ported from #3736 onto the GpsDriver/LocationProvider refactor.
This commit is contained in:
1 parent
017005d1cf
commit
6b3da8a2b1
10 files changed
+466
-2
No files matched your search
@@ -69,6 +69,8 @@ This document provides an overview of CLI commands that can be sent to MeshCore
|
||||
**Parameters:**
|
||||
- `epoch_seconds`: Unix epoch time
|
||||
|
||||
**Note:** The clock only moves forward. To set it back (e.g. after the clock was pushed into the future by a wrong GPS time) use `time force <epoch_seconds>`.
|
||||
|
||||
---
|
||||
|
||||
### Send a flood advert
|
||||
@@ -1077,6 +1079,21 @@ region save
|
||||
|
||||
---
|
||||
|
||||
#### GPS anti-spoofing guard (When built with `GPS_GUARD`)
|
||||
With `GPS_GUARD`, a fix becomes the node's last fix (position, telemetry, adverts) only after it passes plausibility checks: at least `GPS_GUARD_MIN_SATS` satellites, HDOP ≤ `GPS_GUARD_MAX_HDOP10`/10, inside the optional build-time region (`GPS_GUARD_LAT_MIN/MAX`, `GPS_GUARD_LON_MIN/MAX`), within the fence around the node's configured `lat`/`lon` (if set), no jump faster than `GPS_GUARD_MAX_SPEED_KMH`, and `GPS_GUARD_STABLE_FIXES` consistent fixes in a row. An unverified position is never reported.
|
||||
|
||||
GPS time is written to the clock only while such a fix is current, never earlier than the firmware build date, and, once the clock is valid, only within `GPS_GUARD_TIME_MAX_STEP` seconds for the first sync and within clock drift (`GPS_GUARD_TIME_DRIFT_BASE` s + `GPS_GUARD_TIME_DRIFT_PPM`) afterwards. Setting the clock manually (`time`, `time force`, `clock sync`) resets that anchor.
|
||||
|
||||
Covers serial NMEA receivers (`MicroNMEALocationProvider`).
|
||||
|
||||
**Usage:**
|
||||
- `gps guard`: status, e.g. `> ok 7sat time ok +1s`, `> rej region time rej nofix`, or `... ALERT gps-clk +3600s 75m` when a trusted fix has disagreed with the clock by the same offset for `GPS_GUARD_ALERT_MIN` minutes. While the alert is active, the offset in hours is also reported in telemetry on the next free channel.
|
||||
- `gps guard accept`: while an alert is active, confirm that the GPS time is right and apply it to the clock.
|
||||
- `gps fence`, `gps fence <metres>`: fence radius around the configured `lat`/`lon` (`0` = off). Default `GPS_GUARD_FENCE_M` (1000), not saved.
|
||||
- `gps ref`: the reference position; follows `set lat`, `set lon` and `gps setloc`.
|
||||
|
||||
---
|
||||
|
||||
#### Set this node's location based on the GPS coordinates
|
||||
**Usage:**
|
||||
- `gps setloc`
|
||||
|
||||
@@ -211,6 +211,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
if (sender_timestamp > curr) {
|
||||
getRTCClock()->setCurrentTime(sender_timestamp + 1);
|
||||
CommonGpsPrefs::onClockSetManually(*_sensors);
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
DateTime dt = DateTime(now);
|
||||
sprintf(reply, "OK - clock set: %02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
|
||||
@@ -225,11 +226,22 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
DateTime dt = DateTime(now);
|
||||
sprintf(reply, "%02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
|
||||
} else if (memcmp(command, "time force ", 11) == 0) { // set time, allowing it to go backwards
|
||||
uint32_t secs = _atoi(&command[11]);
|
||||
if (secs > 1700000000UL) {
|
||||
getRTCClock()->setCurrentTime(secs);
|
||||
CommonGpsPrefs::onClockSetManually(*_sensors);
|
||||
DateTime dt = DateTime(getRTCClock()->getCurrentTime());
|
||||
sprintf(reply, "OK - clock forced: %02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
|
||||
} else {
|
||||
strcpy(reply, "ERR: invalid epoch");
|
||||
}
|
||||
} else if (memcmp(command, "time ", 5) == 0) { // set time (to epoch seconds)
|
||||
uint32_t secs = _atoi(&command[5]);
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
if (secs > curr) {
|
||||
getRTCClock()->setCurrentTime(secs);
|
||||
CommonGpsPrefs::onClockSetManually(*_sensors);
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
DateTime dt = DateTime(now);
|
||||
sprintf(reply, "OK - clock set: %02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
|
||||
@@ -357,6 +369,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
} else if (memcmp(command, "gps setloc", 10) == 0) {
|
||||
_prefs->node_lat = _sensors->node_lat;
|
||||
_prefs->node_lon = _sensors->node_lon;
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(command, "gps advert", 10) == 0) {
|
||||
@@ -495,10 +508,12 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, _prefs->disable_fwd ? "OK - repeat is now OFF" : "OK - repeat is now ON");
|
||||
} else if (memcmp(config, "lat ", 4) == 0) {
|
||||
_prefs->node_lat = atof(&config[4]);
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "lon ", 4) == 0) {
|
||||
_prefs->node_lon = atof(&config[4]);
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "flood.max.unscoped ", 19) == 0) {
|
||||
|
||||
@@ -169,6 +169,11 @@ private:
|
||||
void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); }
|
||||
uint32_t getGpsPeriod() const override { return _parent->gps_period; }
|
||||
void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); }
|
||||
bool getGuardReference(double& lat, double& lon) const override {
|
||||
lat = _parent->node_lat;
|
||||
lon = _parent->node_lon;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
GPSPrefs gps;
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "CommonGpsPrefs.h"
|
||||
#include <helpers/sensors/LocationProvider.h>
|
||||
#ifdef GPS_GUARD
|
||||
#include <helpers/sensors/GpsGuard.h>
|
||||
#endif
|
||||
|
||||
bool CommonGpsPrefs::setGpsEnabled(SensorManager& sensors, bool enabled) {
|
||||
LocationProvider* location = sensors.getLocationProvider();
|
||||
@@ -17,9 +20,28 @@ bool CommonGpsPrefs::setGpsPeriod(SensorManager& sensors, uint32_t seconds) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void CommonGpsPrefs::applyGuardReference(SensorManager& sensors) const {
|
||||
#ifdef GPS_GUARD
|
||||
LocationProvider* location = sensors.getLocationProvider();
|
||||
GpsGuard* guard = location != nullptr ? location->getGuard() : nullptr;
|
||||
double lat, lon;
|
||||
if (guard != nullptr && getGuardReference(lat, lon)) guard->setReference(lat, lon);
|
||||
#endif
|
||||
}
|
||||
|
||||
// The clock was set by hand: the guard drops its GPS time anchor and any alert.
|
||||
void CommonGpsPrefs::onClockSetManually(SensorManager& sensors) {
|
||||
#ifdef GPS_GUARD
|
||||
LocationProvider* location = sensors.getLocationProvider();
|
||||
GpsGuard* guard = location != nullptr ? location->getGuard() : nullptr;
|
||||
if (guard != nullptr) guard->clockSetManually();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CommonGpsPrefs::apply(SensorManager& sensors) const {
|
||||
LocationProvider* location = sensors.getLocationProvider();
|
||||
if (location == nullptr) return false;
|
||||
applyGuardReference(sensors);
|
||||
location->setGpsPeriod(getGpsPeriod());
|
||||
location->setGpsOn(isGpsEnabled());
|
||||
return true;
|
||||
@@ -198,6 +220,47 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#ifdef GPS_GUARD
|
||||
if (memcmp(command, "gps guard", 9) == 0 || memcmp(command, "gps fence", 9) == 0 ||
|
||||
strcmp(command, "gps ref") == 0) {
|
||||
LocationProvider* location = sensors.getLocationProvider();
|
||||
GpsGuard* guard = location != nullptr ? location->getGuard() : nullptr;
|
||||
if (guard == nullptr) {
|
||||
strcpy(reply, "Error, gps guard not supported");
|
||||
} else if (strcmp(command, "gps guard") == 0) {
|
||||
reply[0] = '>'; reply[1] = ' ';
|
||||
guard->formatStatus(reply + 2, 158);
|
||||
} else if (strcmp(command, "gps guard accept") == 0) {
|
||||
// operator confirms that the alerted GPS time is right: apply it to the clock
|
||||
long offset;
|
||||
if (!guard->isTimeAlert()) {
|
||||
strcpy(reply, "Error, no time alert");
|
||||
} else if (!location->getClockOffset(offset) ||
|
||||
!guard->acceptAlertedTime(offset, (uint32_t)location->getTimestamp()) ||
|
||||
!location->applyTimeNow()) {
|
||||
strcpy(reply, "Error, no current trusted fix with the alerted time");
|
||||
} else {
|
||||
strcpy(reply, "OK - clock set from GPS");
|
||||
}
|
||||
} else if (strcmp(command, "gps fence") == 0) {
|
||||
sprintf(reply, "> %lu", (unsigned long)guard->getFence());
|
||||
} else if (memcmp(command, "gps fence ", 10) == 0) {
|
||||
char* end;
|
||||
unsigned long metres = strtoul(command + 10, &end, 10);
|
||||
if (command[10] == ' | ||||