From 8efec1d6406f901f0494e641fa63bda51378f672 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 5 Jan 2017 16:27:27 +0100 Subject: [PATCH] ntp: add sanity check for HW timestamps Accept HW timestamp only if it doesn't differ from the kernel/daemon timestamp by more than one second. --- ntp_io_linux.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ntp_io_linux.c b/ntp_io_linux.c index 9dcbd4a..7ea15f1 100644 --- a/ntp_io_linux.c +++ b/ntp_io_linux.c @@ -72,6 +72,9 @@ struct Interface { /* Number of PHC readings per HW clock sample */ #define PHC_READINGS 10 +/* Maximum acceptable offset between HW and daemon/kernel timestamp */ +#define MAX_TS_DELAY 1.0 + /* Array of Interfaces */ static ARR_Instance interfaces; @@ -417,8 +420,8 @@ static void process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts, NTP_Local_Timestamp *local_ts, int rx_ntp_length, int family) { - struct timespec sample_phc_ts, sample_local_ts; - double sample_delay, rx_correction; + struct timespec sample_phc_ts, sample_local_ts, ts; + double sample_delay, rx_correction, ts_delay, err; int l2_length; if (HCL_NeedsNewSample(iface->clock, &local_ts->ts)) { @@ -444,9 +447,18 @@ process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts, UTI_AddDoubleToTimespec(hw_ts, rx_correction, hw_ts); } - if (!HCL_CookTime(iface->clock, hw_ts, &local_ts->ts, &local_ts->err)) + if (!HCL_CookTime(iface->clock, hw_ts, &ts, &err)) return; + ts_delay = UTI_DiffTimespecsToDouble(&local_ts->ts, &ts); + + if (fabs(ts_delay) > MAX_TS_DELAY) { + DEBUG_LOG(LOGF_NtpIOLinux, "Unacceptable timestamp delay %.9f", ts_delay); + return; + } + + local_ts->ts = ts; + local_ts->err = err; local_ts->source = NTP_TS_HARDWARE; }