]> git.ipfire.org Git - thirdparty/chrony.git/log
thirdparty/chrony.git
16 months agokeys+nts: warn if loading world-readable/writable key
Miroslav Lichvar [Thu, 19 Jan 2023 15:09:40 +0000 (16:09 +0100)] 
keys+nts: warn if loading world-readable/writable key

Log a warning message if the file specified by the keyfile or
ntsserverkey directive is world-readable or writable, which is likely
an insecure misconfiguration. There is no check of directories
containing the file.

16 months agorefclock: fix preprocessor conditional
Miroslav Lichvar [Thu, 19 Jan 2023 10:34:55 +0000 (11:34 +0100)] 
refclock: fix preprocessor conditional

Split the new SOCK conditional using __GLIBC_PREREQ macro (which has
arguments) to fix compilation when it is not defined.

Fix also debug message using sizeof(time_t) in case it's enabled on
64-bit systems.

Reported-by: Bryan Christianson <bryan@whatroute.net>
Fixes: badaa83c319a ("refclock: convert mismatched timeval in SOCK messages")
16 months agorefclock: convert mismatched timeval in SOCK messages
Miroslav Lichvar [Wed, 18 Jan 2023 15:14:10 +0000 (16:14 +0100)] 
refclock: convert mismatched timeval in SOCK messages

On 32-bit glibc-based (>=2.34) systems, allow the SOCK client to send
messages with timevals using the other time_t size than chrony. If the
length of the received message corresponds to the other size, convert
the timeval and move the rest of the message before its processing.

This is needed for compatibility with the current development version of
gpsd, which forces 64-bit time_t on these systems, while chrony needs to
be compiled with the same time_t as gnutls.

16 months agodoc: deprecate SHM refclocks in favor of SOCK
Miroslav Lichvar [Thu, 12 Jan 2023 14:23:21 +0000 (15:23 +0100)] 
doc: deprecate SHM refclocks in favor of SOCK

The NTP SHM refclock protocol has the following properties:

- the memory segments have a predictable key (first segment 0x4e545030)
- it's expected to work in any order of starting chronyd and the program
  providing samples to chronyd, i.e. both the consumer and producer need
  to be able to create the segment
- the producer and consumer generally don't know under which user is
  the other side running (e.g. gpsd can create the segment as root and
  also as nobody after it drops root privileges)
- there is no authentication of data provided via SHM
- there is no way to restart the protocol

This makes it difficult for chronyd to ensure it is receiving
measurements from the process that the admin expects it to and not some
other process that managed to create the segment before it was started.
It's up to the admin to configure the system so that chronyd or the
producer is started before untrusted applications or users can create
the segment, or at least verify at some point later that the segment was
created with the expected owner and permissions.

There doesn't seem to be a backward-compatible fix of the protocol. Even
if one side could detect the segment had a wrong owner or permissions,
it wouldn't be able to tell the other side to reattach after recreating
the segment with the expected owner and permissions, if it still had the
permissions to do that.

The protocol would need to specify which side is responsible for
creating the segment and the start order would need to strictly follow
that.

As gpsd (likely the most common refclock source for chronyd) now
supports in the latest version SOCK even for message-based timing,
update the man page and FAQ to deprecate SHM in favor of SOCK.

16 months agoexamples: add chronyd-restricted.service
Miroslav Lichvar [Tue, 10 Jan 2023 14:02:49 +0000 (15:02 +0100)] 
examples: add chronyd-restricted.service

This is a more restricted version of the chronyd service intended for
minimal NTP/NTS client configurations. The daemon is started without
root privileges and is allowed to write only to its own runtime, state,
and log directories. It cannot bind to privileged ports in order to
operate as an NTP server, or provide monitoring access over IPv4/IPv6.
It cannot use reference clocks, HW timestamping, RTC tracking, and other
features.

17 months agocmdmon+client: add selectopts command
Miroslav Lichvar [Wed, 14 Dec 2022 15:04:43 +0000 (16:04 +0100)] 
cmdmon+client: add selectopts command

This command uses the new source function to modify configured selection
options of an NTP source or reference clock.

17 months agocmdmon+client: split out conversion of selection options
Miroslav Lichvar [Wed, 14 Dec 2022 14:28:52 +0000 (15:28 +0100)] 
cmdmon+client: split out conversion of selection options

This will be shared with new command modifying the selection options.

17 months agosources: add function to modify selection options
Miroslav Lichvar [Wed, 14 Dec 2022 14:15:41 +0000 (15:15 +0100)] 
sources: add function to modify selection options

Add a function to add new selection options or remove existing options
specified in the configuration for both NTP sources and reference
clocks.

Provide a pair of IP address and reference ID to identify the source
depending on the type. Find the source directly in the array of sources
instead of going through the NSR hashtable for NTP sources to not
complicate it unnecessarily.

17 months agosources: add assertion for instance index
Miroslav Lichvar [Wed, 14 Dec 2022 13:57:42 +0000 (14:57 +0100)] 
sources: add assertion for instance index

17 months agocmdparse: add functions for parsing refclock refid and select options
Miroslav Lichvar [Tue, 6 Dec 2022 15:33:03 +0000 (16:33 +0100)] 
cmdparse: add functions for parsing refclock refid and select options

This will be used in new chronyc command working on refclocks.

18 months agontp: update comment about minimum request spacing
Miroslav Lichvar [Mon, 5 Dec 2022 15:44:38 +0000 (16:44 +0100)] 
ntp: update comment about minimum request spacing

18 months agolog more changes made by chronyc commands
Miroslav Lichvar [Thu, 1 Dec 2022 13:43:45 +0000 (14:43 +0100)] 
log more changes made by chronyc commands

Log important changes from chronyc for auditing purposes.

Add log messages for:
- loaded symmetric keys and server NTS keys (logged also on start)
- modified maxupdateskew and makestep
- enabled/disabled local reference mode (logged also on start)
- reset time smoothing (logged also on clock steps)
- reset sources

18 months agontp: set DSCP for IPv6
Mike Ryan [Wed, 16 Nov 2022 14:13:09 +0000 (09:13 -0500)] 
ntp: set DSCP for IPv6

Chrony's dscp setting currently applies to IPv4 only. This patch sets
the necessary option for IPv6 as well.

18 months agontp+cmdmon: log allow/deny commands
Miroslav Lichvar [Wed, 16 Nov 2022 14:59:49 +0000 (15:59 +0100)] 
ntp+cmdmon: log allow/deny commands

Log added NTP and command access restrictions, using INFO severity if
from a chronyc command, DEBUG otherwise (i.e. from the config).

18 months agoutil: add function for printing access subnets
Miroslav Lichvar [Wed, 16 Nov 2022 14:57:46 +0000 (15:57 +0100)] 
util: add function for printing access subnets

18 months agontp: log added and removed sources
Miroslav Lichvar [Tue, 15 Nov 2022 15:38:50 +0000 (16:38 +0100)] 
ntp: log added and removed sources

Log a message when a single NTP source or pool of sources is added or
removed. Use the INFO severity if it's a result of a chronyc command or
(re)load of sourcefiles (which are assumed to change over time), and
DEBUG for other contexts, e.g. sources loaded from the config, sources
removed when pruning pools after reaching maxsources, and other parts of
normal operation.

18 months agologging: support context-specific severity
Miroslav Lichvar [Tue, 15 Nov 2022 14:05:36 +0000 (15:05 +0100)] 
logging: support context-specific severity

Allow messages to have severity set to INFO or DEBUG depending on the
context in which they are made to allow logging important changes made
from chronyc or sourcefile, but not spam the system log if those changes
are normally expected (e.g. specified in the config).

19 months agogetdate: fix various warnings which will be errors with clang-16
Holger Hoffstätte [Wed, 9 Nov 2022 08:17:14 +0000 (09:17 +0100)] 
getdate: fix various warnings which will be errors with clang-16

These were found by Gentoo's QA while rebuilding the world with
clang-16: https://bugs.gentoo.org/880519

Signed-off-by: Holger Hoffstätte <holger@applied-asynchrony.com>
19 months agonts: warn if server started without ntsdumpdir
Miroslav Lichvar [Mon, 24 Oct 2022 14:14:35 +0000 (16:14 +0200)] 
nts: warn if server started without ntsdumpdir

If an NTS server is configured without ntsdumpdir, keys will not be
saved and reloaded after restart, which will cause existing cookies
to be invalidated and can cause a short-term denial of service if
the server has so many clients that it cannot handle them all
making an NTS-KE session within one polling interval.

Log a warning message if a server key+certificate is specified without
ntsdumpdir.

19 months agonts: fix number of extension fields after failed encryption
Miroslav Lichvar [Wed, 19 Oct 2022 12:57:16 +0000 (14:57 +0200)] 
nts: fix number of extension fields after failed encryption

If the authenticator SIV encryption fails (e.g. due to wrong nonce
length), decrement the number of extension fields to keep the packet
info consistent.

19 months agonts: change ntskeys format to support different algorithms
Miroslav Lichvar [Thu, 13 Oct 2022 13:35:53 +0000 (15:35 +0200)] 
nts: change ntskeys format to support different algorithms

Specify the AEAD ID for each key saved in the ntskeys file instead of
one ID for all keys. Keep support for loading files in the old format.

This will allow servers to save their keys after upgrading to a new
version with AES-128-GCM-SIV support before the loaded AES-SIV-CMAC-256
keys are rotated out.

If an unsupported key is found, don't load any keys. Also, change the
severity of the error message from debug to error.

19 months agonts: add support for encrypting cookies with AES-128-GCM-SIV
Miroslav Lichvar [Wed, 12 Oct 2022 14:46:56 +0000 (16:46 +0200)] 
nts: add support for encrypting cookies with AES-128-GCM-SIV

If AES-128-GCM-SIV is available on the server, use it for encryption of
cookies. This makes them shorter by 4 bytes due to shorter nonce and it
might also improve the server performance.

After server upgrade and restart with ntsdumpdir, the switch will happen
on the second rotation of the server key. Clients should accept shorter
cookies without restarting NTS-KE. The first response will have extra
padding in the authenticator field to make the length symmetric.

19 months agonts: make server key access more readable
Miroslav Lichvar [Wed, 12 Oct 2022 14:00:45 +0000 (16:00 +0200)] 
nts: make server key access more readable

Get a pointer to the server key instead of repeated indexing.

19 months agonts: add server support for authentication with AES-128-GCM-SIV
Miroslav Lichvar [Tue, 11 Oct 2022 12:36:14 +0000 (14:36 +0200)] 
nts: add server support for authentication with AES-128-GCM-SIV

Keep a server SIV instance for each available algorithm.

Select AES-128-GCM-SIV if requested by NTS-KE client as the first
supported algorithm.

Instead of encoding the AEAD ID in the cookie, select the algorithm
according to the length of decrypted keys. (This can work as a long as
all supported algorithms use keys with different lengths.)

19 months agonts: add client support for authentication with AES-128-GCM-SIV
Miroslav Lichvar [Mon, 10 Oct 2022 14:35:20 +0000 (16:35 +0200)] 
nts: add client support for authentication with AES-128-GCM-SIV

If AES-128-GCM-SIV is available on the client, add it to the requested
algorithms in NTS-KE as the first (preferred) entry.

If supported on the server, it will make the cookies shorter, which
will get the length of NTP messages containing only one cookie below
200 octets. This should make NTS more reliable in networks where longer
NTP packets are filtered as a mitigation against amplification attacks
exploiting the ntpd mode 6/7 protocol.

19 months agonts: add support for NTP authenticator field using AES-GCM-SIV
Miroslav Lichvar [Mon, 10 Oct 2022 13:09:01 +0000 (15:09 +0200)] 
nts: add support for NTP authenticator field using AES-GCM-SIV

Add support for SIV algorithms which have maximum nonce length shorter
than 16 bytes.

19 months agonts: make sure encrypted S2C and C2S keys have equal length
Miroslav Lichvar [Tue, 11 Oct 2022 10:32:04 +0000 (12:32 +0200)] 
nts: make sure encrypted S2C and C2S keys have equal length

Don't allow a cookie to contain keys with different lengths to not break
the assumption made in decoding, if there will ever be a case where this
could be requested.

19 months agonts: don't connect to server if missing AES-SIV-CMAC-256
Miroslav Lichvar [Tue, 11 Oct 2022 08:35:19 +0000 (10:35 +0200)] 
nts: don't connect to server if missing AES-SIV-CMAC-256

Avoid wasting server resources if the client doesn't support
AES-SIV-CMAC-256 (the only algorithm required on servers).

19 months agonts: use signed lengths in NNA_DecryptAuthEF()
Miroslav Lichvar [Mon, 10 Oct 2022 10:43:40 +0000 (12:43 +0200)] 
nts: use signed lengths in NNA_DecryptAuthEF()

Make the types consistent with the rest of the file.

19 months agosiv: add functions to return min and max nonce length
Miroslav Lichvar [Mon, 10 Oct 2022 10:25:47 +0000 (12:25 +0200)] 
siv: add functions to return min and max nonce length

While AES-SIV-CMAC allows nonces of any length, AES-GCM-SIV requires
exactly 12 bytes, which is less than the unpadded minimum length of 16
used in the NTS authenticator field. These functions will be needed to
support both ciphers in the NTS code.

19 months agosiv: add support for AES-128-GCM-SIV in Nettle
Miroslav Lichvar [Mon, 3 Oct 2022 15:28:39 +0000 (17:28 +0200)] 
siv: add support for AES-128-GCM-SIV in Nettle

This is a newer nonce misuse-resistant cipher specified in RFC 8452,
which is now supported in the development code of the Nettle library.

The advantages over AES-SIV-CMAC-256 are shorter keys and better
performance.

19 months agodoc: improve ntsrotate description
Miroslav Lichvar [Tue, 18 Oct 2022 08:22:23 +0000 (10:22 +0200)] 
doc: improve ntsrotate description

19 months agodoc: fix wrong name of authselectmode directive
Miroslav Lichvar [Thu, 13 Oct 2022 10:29:29 +0000 (12:29 +0200)] 
doc: fix wrong name of authselectmode directive

20 months agotest: add float-cast-overflow to 003-sanitizers test
Miroslav Lichvar [Tue, 20 Sep 2022 08:56:28 +0000 (10:56 +0200)] 
test: add float-cast-overflow to 003-sanitizers test

21 months agoupdate copyright years 4.3
Miroslav Lichvar [Mon, 29 Aug 2022 13:04:33 +0000 (15:04 +0200)] 
update copyright years

21 months agodoc: improve description of server directive
Miroslav Lichvar [Mon, 29 Aug 2022 10:08:45 +0000 (12:08 +0200)] 
doc: improve description of server directive

21 months agodoc: improve description of system time in tracking report
Miroslav Lichvar [Mon, 29 Aug 2022 10:07:10 +0000 (12:07 +0200)] 
doc: improve description of system time in tracking report

21 months agocmdmon: add good responses to ntpdata report
Miroslav Lichvar [Thu, 18 Aug 2022 09:59:40 +0000 (11:59 +0200)] 
cmdmon: add good responses to ntpdata report

21 months agontp: initialize remote address in ntpdata report
Miroslav Lichvar [Wed, 17 Aug 2022 13:46:11 +0000 (15:46 +0200)] 
ntp: initialize remote address in ntpdata report

Don't wait for the first response with setting the address.

21 months agodoc: update NEWS 4.3-pre1
Miroslav Lichvar [Thu, 11 Aug 2022 07:36:40 +0000 (09:36 +0200)] 
doc: update NEWS

22 months agodoc: mention maxdelayquant in FAQ
Miroslav Lichvar [Wed, 10 Aug 2022 13:32:54 +0000 (15:32 +0200)] 
doc: mention maxdelayquant in FAQ

22 months agotest: extend 106-refclock test
Miroslav Lichvar [Tue, 9 Aug 2022 14:53:12 +0000 (16:53 +0200)] 
test: extend 106-refclock test

22 months agoconfigure: disable arc4random on Linux
Miroslav Lichvar [Wed, 3 Aug 2022 11:17:42 +0000 (13:17 +0200)] 
configure: disable arc4random on Linux

In glibc 2.36 was added the arc4random family of functions. However,
unlike on other supported systems, it is not a user-space PRNG
implementation. It just wraps the getrandom() system call with no
buffering, which causes a performance loss on NTP servers due to
the function being called twice for each response to add randomness
to the RX and TX timestamp below the clock precision.

Don't check for arc4random on Linux to keep using the buffered
getrandom().

22 months agoconfigure: avoid -Wnonnull warnings
Miroslav Lichvar [Tue, 2 Aug 2022 13:09:38 +0000 (15:09 +0200)] 
configure: avoid -Wnonnull warnings

Replace NULL in test code of functions which have (at least in glibc) or
could have arguments marked as nonnull to avoid the -Wnonnull warnings,
which breaks the detection with the -Werror option.

22 months agodoc: suggest self-signed certificates for NTS in FAQ
Miroslav Lichvar [Tue, 2 Aug 2022 14:51:48 +0000 (16:51 +0200)] 
doc: suggest self-signed certificates for NTS in FAQ

22 months agotest: catch definite leaks with valgrind
Miroslav Lichvar [Tue, 2 Aug 2022 12:45:18 +0000 (14:45 +0200)] 
test: catch definite leaks with valgrind

22 months agotest: fix ntp_core unit test to disable source selection
Miroslav Lichvar [Mon, 1 Aug 2022 14:20:13 +0000 (16:20 +0200)] 
test: fix ntp_core unit test to disable source selection

If the randomly generated timestamps are close to the current time, the
source can be selected for synchronization, which causes a crash when
logging the source name due to uninitialized ntp_sources.

Specify the source with the noselect option to prevent selection.

22 months agotest: fix sources unit test to call SRC_ReportSource() correctly
Miroslav Lichvar [Mon, 1 Aug 2022 11:04:00 +0000 (13:04 +0200)] 
test: fix sources unit test to call SRC_ReportSource() correctly

Call the function with current time instead of latest sample of the
first source to avoid undefined conversion of negative double to long
int.

Fixes: 07600cbd714f ("test: extend sources unit test")
22 months agontp: add maxdelayquant option
Miroslav Lichvar [Thu, 21 Jul 2022 13:16:47 +0000 (15:16 +0200)] 
ntp: add maxdelayquant option

Add a new test for maximum delay using a long-term estimate of a
p-quantile of the peer delay. If enabled, it replaces the
maxdelaydevratio test. It's main advantage is that it is not sensitive
to outliers corrupting the minimum delay.

As it can take a large number of samples for the estimate to reach the
expected value and adapt to a new value after a network change, the
option is recommended only for local networks with very short polling
intervals.

22 months agodoc: improve description of maxdelay* options
Miroslav Lichvar [Tue, 19 Jul 2022 12:51:50 +0000 (14:51 +0200)] 
doc: improve description of maxdelay* options

22 months agoquantiles: add function to get minimum k
Miroslav Lichvar [Tue, 19 Jul 2022 12:33:40 +0000 (14:33 +0200)] 
quantiles: add function to get minimum k

22 months agotest: extend 101-poll and 127-filter tests
Miroslav Lichvar [Mon, 18 Jul 2022 11:21:22 +0000 (13:21 +0200)] 
test: extend 101-poll and 127-filter tests

22 months agontp: change minimum allowed poll to -7
Miroslav Lichvar [Mon, 18 Jul 2022 11:11:27 +0000 (13:11 +0200)] 
ntp: change minimum allowed poll to -7

Change the minimum poll allowed in configuration from -6 to -7. This
matches some PTP profiles using 128 sync messages per second.

22 months agontp: rework filter option to count missing samples
Miroslav Lichvar [Tue, 19 Jul 2022 14:28:32 +0000 (16:28 +0200)] 
ntp: rework filter option to count missing samples

Instead of waiting for the sample filter to accumulate the specified
number of samples and then deciding if the result is acceptable, count
missing samples and get the result after the specified number of polls.

This should work better when samples are dropped at a high rate. The
source and clock update interval will be stable as long as at least
one sample can be collected.

22 months agosamplefilt: add debug message for selected samples
Miroslav Lichvar [Wed, 20 Jul 2022 10:23:04 +0000 (12:23 +0200)] 
samplefilt: add debug message for selected samples

22 months agosamplefilt: add function to get maximum number of samples
Miroslav Lichvar [Tue, 19 Jul 2022 14:02:38 +0000 (16:02 +0200)] 
samplefilt: add function to get maximum number of samples

22 months agontp: enable sub-second poll sooner with filter option
Miroslav Lichvar [Mon, 18 Jul 2022 10:50:05 +0000 (12:50 +0200)] 
ntp: enable sub-second poll sooner with filter option

When the minimum round-trip time is checked to enable a sub-second
polling interval, consider also the last sample in the filter to avoid
waiting for the first sample to be accumulated in sourcestats.

22 months agontp: fix initial poll to follow non-LAN minimum
Miroslav Lichvar [Mon, 18 Jul 2022 10:43:13 +0000 (12:43 +0200)] 
ntp: fix initial poll to follow non-LAN minimum

If a sub-second polling interval is configured, initialize the local
poll to 0 to avoid a shorter interval between the first and second
request in case no response to the first request is received (in time).

22 months agoclient: check for stdout errors
Miroslav Lichvar [Thu, 14 Jul 2022 12:51:24 +0000 (14:51 +0200)] 
client: check for stdout errors

Return with an error code from chronyc if the command is expected to
print some data and fflush() or ferror() indicates an error. This should
make it easier for scripts to detect missing data when redirected to a
file.

23 months agorefclock: remove unused struct MedianFilter
Yury Vostrikov [Mon, 4 Jul 2022 17:37:52 +0000 (19:37 +0200)] 
refclock: remove unused struct MedianFilter

Filtering was moved to a separate source file in commit
c498c21fad35 ("refclock: split off median filter). It looks like
MedianFilter struct somehow survived the split. Remove it to reduce
confusion.

23 months agomain: add log message for timeout reached with -t option
Miroslav Lichvar [Thu, 30 Jun 2022 09:52:40 +0000 (11:52 +0200)] 
main: add log message for timeout reached with -t option

This should make it more clear why chronyd exits if -q/-Q does not
finish before the timeout is reached.

23 months agodoc: improve description of test A in measurements log
Miroslav Lichvar [Thu, 30 Jun 2022 08:19:40 +0000 (10:19 +0200)] 
doc: improve description of test A in measurements log

23 months 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.

23 months 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

23 months 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

23 months 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>
23 months agosys_generic: rename slew constants
Miroslav Lichvar [Tue, 14 Jun 2022 14:02:06 +0000 (16:02 +0200)] 
sys_generic: rename slew constants

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

2 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).

2 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.

2 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.

2 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.

2 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.

2 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.

2 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.

2 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).

2 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).

2 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.

2 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>
2 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>
2 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.

2 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.

2 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

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

2 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.

2 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.

2 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".

2 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

2 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.

2 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

2 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")
2 years agotest: extend sources unit test
Miroslav Lichvar [Mon, 28 Feb 2022 15:22:32 +0000 (16:22 +0100)] 
test: extend sources unit test

2 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.

2 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.

2 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.

2 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.

2 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.

2 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.

2 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.

2 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.