]> git.ipfire.org Git - thirdparty/chrony.git/log
thirdparty/chrony.git
3 years agontp: don't use first response in interleaved mode
Miroslav Lichvar [Thu, 30 Jun 2022 08:18:48 +0000 (10:18 +0200)] 
ntp: don't use first response in interleaved mode

With the first interleaved response coming after a basic response the
client is forced to select the four timestamps covering most of the last
polling interval, which makes measured delay very sensitive to the
frequency offset between server and client. To avoid corrupting the
minimum delay held in sourcestats (which can cause testC failures),
reject the first interleaved response in the client/server mode as
failing the test A.

This does not change anything for the symmetric mode, where both sets of
the four timestamps generally cover a significant part of the polling
interval.

3 years agotest: fix server interleaved mode in ntp_core unit test
Miroslav Lichvar [Tue, 28 Jun 2022 12:41:28 +0000 (14:41 +0200)] 
test: fix server interleaved mode in ntp_core unit test

3 years agodoc: improve and add more questions to FAQ
Miroslav Lichvar [Wed, 22 Jun 2022 15:02:05 +0000 (17:02 +0200)] 
doc: improve and add more questions to FAQ

3 years agosys_generic: damp slew oscillation due to delayed stop
Miroslav Lichvar [Tue, 14 Jun 2022 14:31:22 +0000 (16:31 +0200)] 
sys_generic: damp slew oscillation due to delayed stop

If the computer is overloaded so much that chronyd cannot stop a slew
within one second of the scheduled end and the actual duration is more
than doubled (2 seconds with the minimum duration of 1 second), the
overshoot will be larger than the intended correction. If these
conditions persist, the oscillation will grow up to the maximum offset
allowed by maxslewrate and the delay in stopping.

Monitor the excess duration as an exponentially decaying maximum value
and don't allow any slews shorter than 5 times the value to damp the
oscillation. Ignore delays longer than 100 seconds, assuming they have a
different cause (e.g. the system was suspended and resumed) and are
already handled in the scheduler by triggering cancellation of the
ongoing slew.

This should also make it safer to shorten the minimum duration if
needed.

Reported-by: Daniel Franke <dff@amazon.com>
3 years agosys_generic: rename slew constants
Miroslav Lichvar [Tue, 14 Jun 2022 14:02:06 +0000 (16:02 +0200)] 
sys_generic: rename slew constants

3 years agotest: improve 133-hwtimestamp test
Miroslav Lichvar [Thu, 9 Jun 2022 11:56:46 +0000 (13:56 +0200)] 
test: improve 133-hwtimestamp test

3 years agohwclock: improve filtering of readings
Miroslav Lichvar [Thu, 9 Jun 2022 10:21:38 +0000 (12:21 +0200)] 
hwclock: improve filtering of readings

Estimate the 1st and 2nd 10-quantile of the reading delay and accept
only readings between them unless the error of the offset predicted from
previous samples is larger than the minimum reading error. With the 25
PHC readings per ioctl it should combine about 2-3 readings.

This should improve hwclock tracking and synchronization stability when
a PHC reading delay occasionally falls below the normal expected
minimum, or all readings in the batch are delayed significantly (e.g.
due to high PCIe load).

3 years agoquantiles: add support for quantile estimation
Miroslav Lichvar [Wed, 18 May 2022 10:16:33 +0000 (12:16 +0200)] 
quantiles: add support for quantile estimation

Add estimation of quantiles using the Frugal-2U streaming algorithm
(https://arxiv.org/pdf/1407.1121v1.pdf). It does not need to save
previous samples and adapts to changes in the distribution.

Allow multiple estimates of the same quantile and select the median for
better stability.

3 years agohwclock: refactor processing of PHC readings
Miroslav Lichvar [Tue, 7 Jun 2022 13:03:14 +0000 (15:03 +0200)] 
hwclock: refactor processing of PHC readings

Move processing of PHC readings from sys_linux to hwclock, where
statistics can be collected and filtering improved.

In the PHC refclock driver accumulate the samples even if not in the
external timestamping mode to update the context which will be needed
for improved filtering.

3 years agontp: convert HW timestamp even if PHC reading fails
Miroslav Lichvar [Wed, 8 Jun 2022 13:30:05 +0000 (15:30 +0200)] 
ntp: convert HW timestamp even if PHC reading fails

Reading of PHC can fail occasionally on some hardware. If that happens,
don't abort the conversion of the timestamp that triggered the reading.

3 years agosys_linux: increase number of PHC readings
Miroslav Lichvar [Thu, 2 Jun 2022 14:06:04 +0000 (16:06 +0200)] 
sys_linux: increase number of PHC readings

Increase the number of requested readings from 10 to 25 - the maximum
accepted by the PTP_SYS_OFFSET* ioctls. This should improve stability of
HW clock tracking and PHC refclock.

3 years agodoc: improve hwtimestamp description
Miroslav Lichvar [Thu, 19 May 2022 08:09:08 +0000 (10:09 +0200)] 
doc: improve hwtimestamp description

Latest versions of ethtool print only the shorter lower-case names of
capabilities and filters. Explain that chronyd doesn't synchronize the
PHC and refer to the new vclock feature of the kernel, which should be
used by applications that need a synchronized PHC (e.g. ptp4l and
phc2sys) in order to not interfere with chronyd.

3 years agosources: add selection log
Miroslav Lichvar [Thu, 19 May 2022 06:23:05 +0000 (08:23 +0200)] 
sources: add selection log

Add an option to enable selection log, capturing some data from the
selectdata report.

3 years agosourcestats: don't load samples from future
Miroslav Lichvar [Wed, 18 May 2022 14:30:19 +0000 (16:30 +0200)] 
sourcestats: don't load samples from future

When loading a dumped file, make sure there are no sample times in
future relative to the current system time (e.g. after reboot with
missing RTC).

3 years agolocal: cancel remaining correction after external step
Miroslav Lichvar [Thu, 12 May 2022 09:53:15 +0000 (11:53 +0200)] 
local: cancel remaining correction after external step

Instead of the generic clock driver silently zeroing the remaining
offset after detecting an external step, cancel it properly with the
slew handlers in order to correct timestamps that are not reset in
handling of the unknown step (e.g. the NTP local TX).

3 years agorefclock: set minimum maxlockage in local mode
Miroslav Lichvar [Wed, 11 May 2022 09:53:07 +0000 (11:53 +0200)] 
refclock: set minimum maxlockage in local mode

Use 3 as the minimum maxlockage in the local mode to avoid disruptions
due to losing the lock when a single sample is missed, e.g. when the PPS
driver polling interval is slightly longer than the pulse interval and a
pulse is skipped.

3 years agorefclock: restart local mode after losing lock
Miroslav Lichvar [Wed, 11 May 2022 09:36:57 +0000 (11:36 +0200)] 
refclock: restart local mode after losing lock

A refclock in the local mode is locked to itself. When the maxlockage
check failed after missing some samples, it failed permanently and the
refclock was not able to accumulate any new samples.

When the check fails, drop all samples and reset the source to start
from scratch.

Reported-by: Dan Drown <dan-ntp@drown.org>
3 years agorefclock: fix invalid warning in local mode
Miroslav Lichvar [Mon, 9 May 2022 14:41:27 +0000 (16:41 +0200)] 
refclock: fix invalid warning in local mode

A refclock in the local mode is locked to itself by design.

Reported-by: Dan Drown <dan-ntp@drown.org>
3 years agosamplefilt: drop last sample in SPF_DropSamples()
Miroslav Lichvar [Wed, 11 May 2022 09:04:52 +0000 (11:04 +0200)] 
samplefilt: drop last sample in SPF_DropSamples()

When SPF_DropSamples() is called, don't keep the last sample to be
retrieved by SPF_GetLastSample(). It should be kept only after
filtering.

3 years agosiv: set key directly with gnutls
Miroslav Lichvar [Wed, 11 May 2022 06:57:22 +0000 (08:57 +0200)] 
siv: set key directly with gnutls

A new function is provided by the latest gnutls (should be in 3.7.5) to
set the key of an AEAD cipher. If available, use it to avoid destroying
and creating a new SIV instance with each key change.

This improves the server NTS-NTP performance if using gnutls for SIV.

3 years agodoc: improve description of chronyc -h option
Miroslav Lichvar [Thu, 5 May 2022 10:14:26 +0000 (12:14 +0200)] 
doc: improve description of chronyc -h option

3 years agodoc: improve maxchange description
Miroslav Lichvar [Thu, 5 May 2022 09:50:00 +0000 (11:50 +0200)] 
doc: improve maxchange description

3 years agonts: don't exit if initialization of priority cache fails
Miroslav Lichvar [Wed, 4 May 2022 12:17:34 +0000 (14:17 +0200)] 
nts: don't exit if initialization of priority cache fails

Initialization of the gnutls priority cache can fail depending on the
system crypto policy (e.g. disabled TLS1.3). Log an error mentioning
TLS, but continue to run without the server/client credentials.

3 years agoclient: rework command catenation
Miroslav Lichvar [Tue, 3 May 2022 11:25:11 +0000 (13:25 +0200)] 
client: rework command catenation

Use snprintf() instead of strcat() and don't try to parse commands
longer than 2048 characters to make it consistent with the chrony.conf
parser, avoid memory allocation, and not rely on the system ARG_MAX to
keep the length sane.

3 years agoexamples: replace grep command in NM dispatcher script
Miroslav Lichvar [Wed, 23 Mar 2022 14:17:03 +0000 (15:17 +0100)] 
examples: replace grep command in NM dispatcher script

Some grep implementations detect binary data and return success without
matching whole line. This might be an issue for the DHCPv6 NTP FQDN
check. The GNU grep in the C locale seems to check only for the NUL
character, which cannot be passed in an environment variable, but other
implementations might behave differently and there doesn't seem to be a
portable way to force matching the whole line.

Instead of the grep command, check for invalid characters by comparing
the length of the input passed through "tr -d -c".

3 years agodoc: include gnutls in libraries providing SECHASH feature
Miroslav Lichvar [Wed, 16 Mar 2022 13:46:13 +0000 (14:46 +0100)] 
doc: include gnutls in libraries providing SECHASH feature

3 years agontp: keep original source IP address
Miroslav Lichvar [Wed, 9 Mar 2022 14:34:16 +0000 (15:34 +0100)] 
ntp: keep original source IP address

When an added source is specified by IP address, save the original
string instead of formatting a new string from the parsed address, which
can be different (e.g. compressed vs expanded IPv6 address).

This fixes the chronyc sourcename command and -N option to print the IP
address exactly as it was specified in the configuration file or chronyc
add command.

3 years agontp: split out conf_id allocation
Miroslav Lichvar [Wed, 9 Mar 2022 14:30:16 +0000 (15:30 +0100)] 
ntp: split out conf_id allocation

3 years agotest: update 007-cmdmon system test for recent changes
Miroslav Lichvar [Mon, 7 Mar 2022 14:54:08 +0000 (15:54 +0100)] 
test: update 007-cmdmon system test for recent changes

The new unsynchronised source state is now reported in selectdata before
the first measurement.

Fixes: c29f8888c767 ("sources: handle unsynchronized sources in selection")
3 years agotest: extend sources unit test
Miroslav Lichvar [Mon, 28 Feb 2022 15:22:32 +0000 (16:22 +0100)] 
test: extend sources unit test

3 years agosources: improve debug messages
Miroslav Lichvar [Thu, 24 Feb 2022 13:48:24 +0000 (14:48 +0100)] 
sources: improve debug messages

Print source status as char and print the name instead of index in
combining.

3 years agorefclock: trim offset in local mode
Miroslav Lichvar [Thu, 24 Feb 2022 07:45:10 +0000 (08:45 +0100)] 
refclock: trim offset in local mode

With the local option, trim offset larger than 1 second to not lose
precision after large steps of the clock.

3 years agosamplefilt: add function to correct accumulated offsets
Miroslav Lichvar [Thu, 24 Feb 2022 07:44:10 +0000 (08:44 +0100)] 
samplefilt: add function to correct accumulated offsets

Analogously to SST_CorrectOffset(), add SPF_CorrectOffset() to correct
the offsets accumulated in the filter.

3 years agosys_linux: don't require configurable pin for external PPS
Miroslav Lichvar [Wed, 23 Feb 2022 10:31:24 +0000 (11:31 +0100)] 
sys_linux: don't require configurable pin for external PPS

Some PHCs that have a PPS input don't have configurable pins (their
function is hardcoded). Accept a negative pin index to skip the pin
configuration before requesting external timestamping.

3 years agorefclock: improve precision with large offset
Miroslav Lichvar [Wed, 23 Feb 2022 09:23:18 +0000 (10:23 +0100)] 
refclock: improve precision with large offset

If a SHM or PHC refclock has a very large offset compensated by the
offset option, or ignored with the pps or local option, there is a
persistent loss of precision in the calculation of the sample offset
using the double format.

Rework the code to delay the calculation of the accumulated offset to
include the specificed compensation and remaining correction of the
system clock, where the calculation can be split to improve the
precision. In the pps mode ignore integer seconds competely.

The precision of the SOCK refclock is now limited to 1 nanosecond due to
the extra double->timespec->double conversion.

3 years agorefclock: add local option
Miroslav Lichvar [Tue, 22 Feb 2022 10:24:00 +0000 (11:24 +0100)] 
refclock: add local option

Add "local" option to specify that the reference clock is an
unsynchronized clock which is more stable than the system clock (e.g.
TCXO, OCXO, or atomic clock) and it should be used as a local standard
to stabilize the system clock.

Handle the local refclock as a PPS refclock locked to itself which gives
the unsynchronized status to be ignored in the source selection. Wait
for the refclock to get at least minsamples samples and adjust the clock
directly to follow changes in the refclock's sourcestats frequency and
offset.

There should be at most one refclock specified with this option.

3 years agoreference: allow clock adjustments without updating reference
Miroslav Lichvar [Tue, 22 Feb 2022 10:00:27 +0000 (11:00 +0100)] 
reference: allow clock adjustments without updating reference

Add support for accumulating frequency and time offset without changing
the reference parameters and calling the local parameter change
handlers.

This will allow an unsynchronized source to operate below other sources
in order to stabilize the clock.

3 years agosources: handle unsynchronized sources in selection
Miroslav Lichvar [Mon, 14 Feb 2022 09:55:22 +0000 (10:55 +0100)] 
sources: handle unsynchronized sources in selection

Allow sources to accumulate samples with the leap status set to not
synchronized. Define a new state for them to be ignored in the
selection. This is intended for sources that are never synchronized and
will be used only for stabilization.

3 years agosourcestats: add function to get minsamples
Miroslav Lichvar [Thu, 10 Feb 2022 15:38:50 +0000 (16:38 +0100)] 
sourcestats: add function to get minsamples

3 years agosourcestats: clamp minsamples and maxsamples in initialization
Miroslav Lichvar [Thu, 10 Feb 2022 14:24:25 +0000 (15:24 +0100)] 
sourcestats: clamp minsamples and maxsamples in initialization

Don't leave the variables set to values outside their effective range.
This has no functional impact, but makes it clear what is the precedence
of the two settings.

3 years agosourcestats: use constant for required number of samples
Miroslav Lichvar [Thu, 10 Feb 2022 14:16:08 +0000 (15:16 +0100)] 
sourcestats: use constant for required number of samples

3 years agosys_linux: allow rseq in seccomp filter
Michael Hudson-Doyle [Wed, 9 Feb 2022 08:06:13 +0000 (09:06 +0100)] 
sys_linux: allow rseq in seccomp filter

Libc 2.35 will use rseq syscalls [1][2] by default and thereby
break chrony in seccomp isolation.

[1]: https://www.efficios.com/blog/2019/02/08/linux-restartable-sequences/
[2]: https://sourceware.org/pipermail/libc-alpha/2022-February/136040.html

Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
Signed-off-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
Signed-off-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
3 years agoexamples: handle more actions in NM dispatcher script
Miroslav Lichvar [Mon, 7 Feb 2022 12:27:48 +0000 (13:27 +0100)] 
examples: handle more actions in NM dispatcher script

Run the chronyc onoffline command also when the connectivity-change
and dhcp6-change actions are reported by the NetworkManager dispatcher.

The latter should not be necessary, but there currently doesn't seem to
be any action for IPv6 becoming routable after duplicate address
detection, so at least in networks using DHCPv6, IPv6 NTP servers should
not be stuck in the offline state from a previously reported action.

3 years agoexamples: support DHCPv6 NTP servers in NM dispatcher script
Miroslav Lichvar [Mon, 7 Feb 2022 12:27:25 +0000 (13:27 +0100)] 
examples: support DHCPv6 NTP servers in NM dispatcher script

Latest NetworkManager code provides NTP servers from the DHCPv6 NTP
option (RFC 5908) in the DHCP6_DHCP6_NTP_SERVERS variable to dispatcher
scripts.

Check for invalid characters (which can come from the FQDN suboption)
and include the servers in the interface-specific sources file.

3 years agoclient: fix waitsync command to reconnect to server
Miroslav Lichvar [Wed, 26 Jan 2022 15:00:36 +0000 (16:00 +0100)] 
client: fix waitsync command to reconnect to server

If chronyc waitsync was started before chronyd, it would try all
addresses (Unix socket, IPv4, IPv6) and get stuck with no address, not
getting any response later when chronyd was running.

Reset the address index in open_io() when returning with failure to
allow the next call to start with the first address again.

Reported-by: Jan Mikkelsen <janm@transactionware.com>
3 years agotest: ensure awk commands in 008-ntpera return an integer
Vincent Blut [Wed, 12 Jan 2022 17:08:34 +0000 (18:08 +0100)] 
test: ensure awk commands in 008-ntpera return an integer

Some awk interpreters (e.g. mawk) print long integers in exponential
notation skewing the test result.

3 years agodoc: update NEWS 4.2
Miroslav Lichvar [Thu, 16 Dec 2021 08:56:20 +0000 (09:56 +0100)] 
doc: update NEWS

3 years agoupdate copyright years
Miroslav Lichvar [Thu, 16 Dec 2021 08:54:11 +0000 (09:54 +0100)] 
update copyright years

3 years agontp: set local address on PTP socket on FreeBSD
Miroslav Lichvar [Thu, 16 Dec 2021 12:08:19 +0000 (13:08 +0100)] 
ntp: set local address on PTP socket on FreeBSD

Fix the FreeBSD-specific code checking for a bound IPv4 socket to
include the new PTP port. This should fix a multihomed server to respond
to NTP-over-PTP requests from the address which received the request.

Fixes: be3158c4e5b2 ("ntp: add support for NTP over PTP")
3 years agocmdmon: fix transmit_reply() to not read uninitialized data
Miroslav Lichvar [Thu, 16 Dec 2021 10:36:26 +0000 (11:36 +0100)] 
cmdmon: fix transmit_reply() to not read uninitialized data

In the FreeBSD-specific code checking for a bound IPv4 socket, make
sure it is not a Unix domain address to avoid reading uninitialized
IP-specific fields.

This fixes an error reported by valgrind.

3 years agosocket: zero sockaddr_un to initialize sa_len
Miroslav Lichvar [Thu, 16 Dec 2021 09:41:31 +0000 (10:41 +0100)] 
socket: zero sockaddr_un to initialize sa_len

Zero the whole sockaddr struct before calling bind() and connect() to
initialize the FreeBSD-specific sa_len field.

This fixes errors reported by valgrind.

3 years agoclientlog: remove unnecessary operation in timestamp conversion
Miroslav Lichvar [Tue, 14 Dec 2021 09:41:08 +0000 (10:41 +0100)] 
clientlog: remove unnecessary operation in timestamp conversion

3 years agontp: avoid unnecessary source lookups
Miroslav Lichvar [Tue, 14 Dec 2021 09:04:39 +0000 (10:04 +0100)] 
ntp: avoid unnecessary source lookups

Avoid searching the hash table of sources when a packet in the client
mode is received. It cannot be a response from our source. Analogously,
avoid source lookups for transmitted packets in the server mode. This
doesn't change anything for packets in symmetric modes, which can be
requests and responses at the same time.

This slightly improves the maximum packet rate handled as a server.

3 years agontp: fix typo in comment
Miroslav Lichvar [Mon, 13 Dec 2021 14:54:43 +0000 (15:54 +0100)] 
ntp: fix typo in comment

3 years agodoc: describe use case for leapsecmode ignore option
Miroslav Lichvar [Thu, 9 Dec 2021 16:13:09 +0000 (17:13 +0100)] 
doc: describe use case for leapsecmode ignore option

3 years agodoc: switch Solaris support to illumos
Miroslav Lichvar [Thu, 9 Dec 2021 12:27:50 +0000 (13:27 +0100)] 
doc: switch Solaris support to illumos

For a long time, the Solaris support in chrony wasn't tested on a real
Solaris system, but on illumos/OpenIndiana, which was forked from
OpenSolaris when it was discontinued in 2010.

While Solaris and illumos might have not diverged enough to make a
difference for chrony, replace Solaris in the documentation with illumos
to make it clear which system is actually supported by the chrony
project.

3 years agosys_solaris: disable kernel dosynctodr
Miroslav Lichvar [Tue, 7 Dec 2021 10:32:54 +0000 (11:32 +0100)] 
sys_solaris: disable kernel dosynctodr

The dosynctodr kernel variable needs to be set to 0 to block automatic
synchronization of the system clock to the hardware clock. chronyd used
to disable dosynctodr on Solaris versions before 2.6, but it seems it is
now needed even on current versions as the clock driver sets frequency
only without calling adjtime() or setting the ntp_adjtime() PLL offset.

This issue was reproduced and fix tested on current OpenIndiana.

Fixes: 8feb37df2b48 ("sys_solaris: use timex driver")
3 years agodoc: update NEWS 4.2-pre1
Miroslav Lichvar [Thu, 2 Dec 2021 10:19:40 +0000 (11:19 +0100)] 
doc: update NEWS

3 years agontp: limit total monotonic offset correction
Miroslav Lichvar [Thu, 2 Dec 2021 09:56:26 +0000 (10:56 +0100)] 
ntp: limit total monotonic offset correction

In addition to the 16s limit in per-response change in the monotonic
offset, don't allow the total accumulated offset injected in sourcestats
to be larger than 16 seconds.

3 years agotest: update and improve 003-sanitizers test
Miroslav Lichvar [Wed, 1 Dec 2021 09:13:14 +0000 (10:13 +0100)] 
test: update and improve 003-sanitizers test

3 years agotest: allow another inaccuracy in util unit test
Miroslav Lichvar [Wed, 1 Dec 2021 08:26:41 +0000 (09:26 +0100)] 
test: allow another inaccuracy in util unit test

A 1ns error in UTI_AdjustTimespec() was observed with an i686 build.

3 years agotest: fix 008-ntpera test for arbitrary NTP era split
Miroslav Lichvar [Wed, 1 Dec 2021 08:24:13 +0000 (09:24 +0100)] 
test: fix 008-ntpera test for arbitrary NTP era split

3 years agoreference: check for unset leap_when in is_leap_close()
Miroslav Lichvar [Wed, 1 Dec 2021 08:14:34 +0000 (09:14 +0100)] 
reference: check for unset leap_when in is_leap_close()

Check that the leap_when variable is set before testing a timestamp for
being close to a leap second. This allows the first measurement to be
accepted if starting at the Unix epoch (e.g. in a test).

3 years agontp: check for zero timestamp in initial TX timeout
Miroslav Lichvar [Wed, 1 Dec 2021 08:11:09 +0000 (09:11 +0100)] 
ntp: check for zero timestamp in initial TX timeout

Calculate the delay since the previous transmission only if the
TX timestamp is actually set. This removes an unnecessary delay when
starting at the Unix epoch in 1970 (e.g. in a test).

3 years agortc: don't drop first sample after initial trim
Miroslav Lichvar [Mon, 29 Nov 2021 11:30:09 +0000 (12:30 +0100)] 
rtc: don't drop first sample after initial trim

It seems there is no longer an issue with the first sample after the
initial trim and it can be accumulated. It might have been a workaround
for an unrelated bug which was fixed since then.

This fixes the number of samples reported in rtcdata briefly jumping to
65535 and also brings back the expectation that n_samples is never
negative.

3 years agortc: drop rtc_trim array
Miroslav Lichvar [Mon, 29 Nov 2021 11:15:25 +0000 (12:15 +0100)] 
rtc: drop rtc_trim array

It always contained zero values and had no effect on anything.

3 years agortc: remove unnecessary variable initializations
Miroslav Lichvar [Mon, 29 Nov 2021 10:55:24 +0000 (11:55 +0100)] 
rtc: remove unnecessary variable initializations

3 years agodoc: update FAQ
Miroslav Lichvar [Tue, 23 Nov 2021 15:05:16 +0000 (16:05 +0100)] 
doc: update FAQ

3 years agontp: improve check for PTP socket
Miroslav Lichvar [Wed, 24 Nov 2021 14:05:15 +0000 (15:05 +0100)] 
ntp: improve check for PTP socket

Check for INVALID_SOCK_FD in case the PTP port is enabled, but opening
one of the PTP sockets failed.

3 years agotest: update 110-chronyc test
Miroslav Lichvar [Wed, 24 Nov 2021 11:10:46 +0000 (12:10 +0100)] 
test: update 110-chronyc test

3 years agomain: add assertions for timespec signedness
Miroslav Lichvar [Tue, 23 Nov 2021 13:41:08 +0000 (14:41 +0100)] 
main: add assertions for timespec signedness

Some of the code (e.g. util and clientlog) may work with negative
values. Require that time_t and the tv_nsec types are signed. This seems
to be the case on all supported systems, but it it is not required by
POSIX.

3 years agoutil: reset GetRandom functions in helpers after fork
Miroslav Lichvar [Tue, 23 Nov 2021 12:17:26 +0000 (13:17 +0100)] 
util: reset GetRandom functions in helpers after fork

Close /dev/urandom and drop cached getrandom() data after forking helper
processes to avoid them getting the same sequence of random numbers
(e.g. two NTS-KE helpers generating cookies with identical nonces).
arc4random() is assumed to be able to detect forks and reseed
automatically.

This is not strictly necessary with the current code, which does not use
the GetRandom functions before the NTS-KE helper processes are forked,
but that could change in future.

Also, call the reset function before exit to close /dev/urandom in order
to avoid valgrind reporting the file object as "still reachable".

3 years agontp: fix exp1 EF search in process_response()
Miroslav Lichvar [Tue, 23 Nov 2021 09:35:22 +0000 (10:35 +0100)] 
ntp: fix exp1 EF search in process_response()

Don't ignore the magic field when searching for the exp1 extension
field in a received response. If there were two exp1 fields in the
packet, and only one of them had the expected magic value, it should
pick the right one.

Fixes: 2319f72b29a9 ("ntp: add client support for experimental extension field")
3 years agontp: make default NTP version with xleave to be always 4
Miroslav Lichvar [Mon, 22 Nov 2021 15:44:24 +0000 (16:44 +0100)] 
ntp: make default NTP version with xleave to be always 4

If the xleave option is enabled, ignore the key option and the hash
length. Always use version 4 as the default to get interleaved responses
from new chrony servers.

3 years agontp: limit interleaved responses to NTPv4
Miroslav Lichvar [Mon, 22 Nov 2021 15:33:46 +0000 (16:33 +0100)] 
ntp: limit interleaved responses to NTPv4

The interleaved modes are being specified for NTPv4 only. As a server,
detect interleaved requests only in NTPv4 packets.

Clients and peers can still send interleaved requests in lower-version
packets if configured with the version option.

3 years agontp: suppress monotonic timestamp if smoothing is enabled
Miroslav Lichvar [Mon, 22 Nov 2021 14:52:01 +0000 (15:52 +0100)] 
ntp: suppress monotonic timestamp if smoothing is enabled

Frequency transfer and time smoothing are conflicting features. Set the
monotonic timestamp in the experimental extension field to zero
(invalid) if time smoothing is activated.

3 years agontp: add special value to experimental root delay/disp
Miroslav Lichvar [Mon, 22 Nov 2021 10:39:29 +0000 (11:39 +0100)] 
ntp: add special value to experimental root delay/disp

The maximum value of the new 32-bit fields is slightly less than 16,
which can cause the NTP test #7 to pass for a server which has a zero
root delay but maximum root dispersion.

Interpret the maximum value as the maximum value of the original 32-bit
fields (~65536.0 seconds) for better compatibility with NTPv4.

3 years agotest: add 144-exp1 test
Miroslav Lichvar [Mon, 15 Nov 2021 09:11:03 +0000 (10:11 +0100)] 
test: add 144-exp1 test

3 years agontp: add client support for experimental extension field
Miroslav Lichvar [Mon, 15 Nov 2021 09:08:34 +0000 (10:08 +0100)] 
ntp: add client support for experimental extension field

Add "extfield F323" option to include the new extension field in
requests. If the server responds with this field, use the root
delay/dispersion and monotonic timestamp. Accumulate changes in the
offset between the monotonic and real-time receive timestamps and use
it for the correction of previous offsets in sourcestats. In the
interleaved mode, cancel out the latest change in the offset in
timestamps of the previous request and response, which were captured
before the change actually happened.

3 years agosourcestats: add function to correct accumulated offsets
Miroslav Lichvar [Wed, 10 Nov 2021 14:34:26 +0000 (15:34 +0100)] 
sourcestats: add function to correct accumulated offsets

This will be needed to follow server time corrections in order to
better estimate frequency.

3 years agontp: add server support for experimental extension field
Miroslav Lichvar [Wed, 10 Nov 2021 13:56:31 +0000 (14:56 +0100)] 
ntp: add server support for experimental extension field

Maintain a server monotonic timescale needed for the experimental
extension field. It follows the best estimate of frequency without
time corrections. Implement it as an offset relative to the NTP time,
starting at zero, using a slew handler to cancel time corrections of the
NTP clock. The 32-bit epoch ID is set to a random value on start and
every step of the system clock.

3 years agoutil: add functions for converting new root delay/dispersion
Miroslav Lichvar [Mon, 15 Nov 2021 09:25:35 +0000 (10:25 +0100)] 
util: add functions for converting new root delay/dispersion

3 years agoutil: add function to subtract NTP timestamps
Miroslav Lichvar [Wed, 10 Nov 2021 13:28:53 +0000 (14:28 +0100)] 
util: add function to subtract NTP timestamps

This will be needed to work with monotonic timestamps, which don't have
a stable epoch and cannot be converted to timespec.

3 years agontp: add pre-NTPv5 experimental extension field
Miroslav Lichvar [Wed, 10 Nov 2021 13:52:04 +0000 (14:52 +0100)] 
ntp: add pre-NTPv5 experimental extension field

Add an experimental extension field for some features that were proposed
for NTPv5. Higher-resolution root delay and dispersion (using 28-bit
fraction) are added. A monotonic receive timestamp will allow a
frequency transfer between the server and client. The client will be
able to separate the server's time corrections from frequency
corrections by tracking the offset between the real-time and monotonic
receive timestamps.

The field has a type of 0xF323 from the new experimental range proposed
by the NTP working group. Include a magic 32-bit value in the field to
avoid interoperability issues if a different implementation choses the
same type for its own experimental field. The value will be changed on
incompatible changes to avoid issues between two different chrony
versions.

3 years agontp: prepare for non-authentication extension fields
Miroslav Lichvar [Mon, 8 Nov 2021 15:35:47 +0000 (16:35 +0100)] 
ntp: prepare for non-authentication extension fields

Add a new variable to the packet info structure with flags for extension
fields included in received packets and add a new parameter to
transmit_packet() to add the fields to transmitted packets.

3 years agontp: move initial packet parsing from ntp_auth to ntp_core
Miroslav Lichvar [Mon, 8 Nov 2021 15:06:03 +0000 (16:06 +0100)] 
ntp: move initial packet parsing from ntp_auth to ntp_core

Since commit fdfcabd79bd3 ("ntp: drop support for long NTPv4 MACs"), the
parser doesn't need to check validify of MACs in NTPv4 packets to
distinguish them from extension fields. Move the parser to ntp_core to
avoid having a separate iteration looking for non-authentication
extension fields.

3 years agosocket: increase message buffer length
Miroslav Lichvar [Tue, 26 Oct 2021 13:40:09 +0000 (15:40 +0200)] 
socket: increase message buffer length

Add extra space to the socket message buffer to be able to receive
maximum-length NTP-over-PTP SW/HW-timestamped messages from the Linux
error queue (which are looped back as layer-2 frames).

3 years agontp: use previous root delay/disp in interleaved mode
Miroslav Lichvar [Tue, 26 Oct 2021 12:41:49 +0000 (14:41 +0200)] 
ntp: use previous root delay/disp in interleaved mode

When calculating the root delay and dispersion of a sample measured in
the interleaved mode, use the root delay and dispersion values from
the previous response (to which the TX timestamp corresponds). If the TX
timestamp is combined with the RX timestamp of the latest response (e.g.
in the symmetric mode), use the maximum of the previous and latest root
delay/dispersion.

3 years agotest: extend 122-xleave test
Miroslav Lichvar [Tue, 26 Oct 2021 10:22:55 +0000 (12:22 +0200)] 
test: extend 122-xleave test

3 years agoclientlog: undo clock adjustments in updated TX timestamps
Miroslav Lichvar [Mon, 25 Oct 2021 15:00:28 +0000 (17:00 +0200)] 
clientlog: undo clock adjustments in updated TX timestamps

When the server clock was updated between saving of the RX timestamp and
updating the TX timestamp, a client using interleaved mode with the four
timestamps which minimize error in measured delay (e.g. chrony) had the
server clock adjustment included in the measured delay, which could
disrupt the sample filtering and weighting.

Add a handler to track the slew epoch and remember the last offset. Undo
the adjustment in TX timestamps which have their RX timestamp in the
previous epoch to fix the delay observed by the clients.

If an unknown clock step is detected, drop all timestamps.

3 years agontp: don't save timestamps if transmit_packet() failed
Miroslav Lichvar [Mon, 25 Oct 2021 08:23:41 +0000 (10:23 +0200)] 
ntp: don't save timestamps if transmit_packet() failed

Don't save server RX and TX timestamp to clientlog if the transmission
or authentication failed (e.g. packet is handled in ntp_signd). They
will not be needed.

3 years agontp: initialize saved TX timestamp
Miroslav Lichvar [Mon, 25 Oct 2021 08:18:27 +0000 (10:18 +0200)] 
ntp: initialize saved TX timestamp

Zero the initial TX timestamp which is saved for the interleaved
mode in case there is no previous timestamp saved in clientlog and
transmit_packet() does not generate a new one (e.g. due to failure in
authentication).

Fixes: 5f4cbaab7e0e ("ntp: optimize detection of clients using interleaved mode")
3 years agocmdmon: add interleaved stats to serverstats
Miroslav Lichvar [Wed, 20 Oct 2021 14:10:21 +0000 (16:10 +0200)] 
cmdmon: add interleaved stats to serverstats

Report the number of received interleaved requests and current timestamp
count with their span.

Expand the serverstats description in chronyc man page.

3 years agotest: improve clientlog unit test
Miroslav Lichvar [Mon, 18 Oct 2021 10:25:47 +0000 (12:25 +0200)] 
test: improve clientlog unit test

Test also timestamp maps with smaller maximum sizes.

3 years agontp: don't capture TX timestamps if clientlog is disabled
Miroslav Lichvar [Mon, 18 Oct 2021 09:29:58 +0000 (11:29 +0200)] 
ntp: don't capture TX timestamps if clientlog is disabled

When responding to a request, don't waste time with TX timestamping
if the timestamp will not be saved (i.e. clientlog is disabled).

Fixes: 5f4cbaab7e0e ("ntp: optimize detection of clients using interleaved mode")
3 years agodoc: improve clientloglimit description
Miroslav Lichvar [Thu, 14 Oct 2021 10:29:47 +0000 (12:29 +0200)] 
doc: improve clientloglimit description

3 years agontp: optimize detection of clients using interleaved mode
Miroslav Lichvar [Thu, 14 Oct 2021 10:03:49 +0000 (12:03 +0200)] 
ntp: optimize detection of clients using interleaved mode

Use the lowest bit of the server RX and TX timestamp as a flag
indicating RX timestamp. This allows the server to detect potential
interleaved requests without having to save all its RX timestamps. It
significantly reduces the amount of memory needed to support clients
using the interleaved mode if most of the server's clients are using the
basic mode (e.g. a public server).

Capture the TX timestamp on the first response to the request which has
the flag set to not further delay the first interleaved response.

False positives are possible with broken clients which set the origin
timestamp to something else than zero or the server RX or TX timestamp.
This causes an unnecessary RX timestamp to be saved and TX timestamp
captured and saved.

3 years agontp: move authentication calls in transmit_packet()
Miroslav Lichvar [Thu, 14 Oct 2021 09:52:21 +0000 (11:52 +0200)] 
ntp: move authentication calls in transmit_packet()

Move the calls resetting and generating authentication data out of the
loop checking for unique TX timestamp. This allows the timestamps to be
manipulated after the check.

3 years agoclientlog: separate NTP timestamps from IP addresses
Miroslav Lichvar [Mon, 11 Oct 2021 11:27:35 +0000 (13:27 +0200)] 
clientlog: separate NTP timestamps from IP addresses

Instead of keeping one pair of RX and TX timestamp for each address, add
a separate RX->TX map using an ordered circular buffer. Save the RX
timestamps as 64-bit integers and search them with a combined linear
interpolation and binary algorithm.

This enables the server to support multiple interleaved clients sharing
the same IP address (e.g. NAT) and it will allow other improvements to
be implemented later. A drawback is that a single broken client sending
interleaved requests at a high rate (without spoofing the source
address) can now prevent clients on other addresses from getting
interleaved responses.

The total number of saved timestamps does not change. It's still
determined by the clientloglimit directive. A new option may be added
later if needed. The whole buffer is allocated at once, but only on
first use to not waste memory on client-only configurations.

3 years agoclientlog: fix debug message for maximum number of records
Miroslav Lichvar [Thu, 14 Oct 2021 10:31:52 +0000 (12:31 +0200)] 
clientlog: fix debug message for maximum number of records