mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 17:45:07 -05:00
Compare commits
354 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a6161dc0f | ||
|
|
a4eb5be8ea | ||
|
|
3050e29b1d | ||
|
|
fb1af6e55b | ||
|
|
47a13ae88c | ||
|
|
a8496658a0 | ||
|
|
6ea1082a72 | ||
|
|
4f674539fd | ||
|
|
68d3fb4af8 | ||
|
|
3c24f2c2ed | ||
|
|
0189dac7d8 | ||
|
|
4a11399c2e | ||
|
|
cf98551ea1 | ||
|
|
5508b01bd8 | ||
|
|
907accec87 | ||
|
|
a511029cc2 | ||
|
|
0845df7684 | ||
|
|
2f961ab36a | ||
|
|
a0cf7f7f12 | ||
|
|
a5f1a113f0 | ||
|
|
5160f14fdc | ||
|
|
b0a2ad2535 | ||
|
|
ecdde75f8f | ||
|
|
2d80be9541 | ||
|
|
ab776ed9d8 | ||
|
|
ccebec3eb6 | ||
|
|
3ea3e0efd7 | ||
|
|
c3e4e3e47a | ||
|
|
e949e1d991 | ||
|
|
c8649ccb7e | ||
|
|
39ff7ceeca | ||
|
|
06945d927b | ||
|
|
caf82b1a45 | ||
|
|
f99b2f633b | ||
|
|
6270a3eb7c | ||
|
|
1daa40a2f7 | ||
|
|
a1406eded3 | ||
|
|
1eb8994c00 | ||
|
|
221e5fb501 | ||
|
|
ecfbde9872 | ||
|
|
dec07aa844 | ||
|
|
5b3d4dfe76 | ||
|
|
dc0f0cd134 | ||
|
|
bd37efa52e | ||
|
|
c71185a0e5 | ||
|
|
f149b7b758 | ||
|
|
883b0dde94 | ||
|
|
9cba9c8585 | ||
|
|
88e711ad9a | ||
|
|
badaa83c31 | ||
|
|
bbeec7361c | ||
|
|
6fba5a4a7f | ||
|
|
26889a8cb7 | ||
|
|
cd278d1826 | ||
|
|
3877734814 | ||
|
|
19f2ab9e09 | ||
|
|
3260dc82fe | ||
|
|
1a98c5ffa9 | ||
|
|
8247b8525f | ||
|
|
8901293be8 | ||
|
|
e789b0817f | ||
|
|
d0fd04c0a2 | ||
|
|
7122321249 | ||
|
|
b328c8c348 | ||
|
|
7b97668319 | ||
|
|
6f5df7e4a4 | ||
|
|
5a39074e01 | ||
|
|
c8e57f4350 | ||
|
|
b1230efac3 | ||
|
|
4e1ce88981 | ||
|
|
790a336eb2 | ||
|
|
cc706b50b9 | ||
|
|
73042494bd | ||
|
|
ec89739d50 | ||
|
|
4baf999cc3 | ||
|
|
9afd19c29b | ||
|
|
5dd173c050 | ||
|
|
5caf0ad187 | ||
|
|
17d2291a84 | ||
|
|
a6179261a7 | ||
|
|
098e0c43fc | ||
|
|
7b197953e8 | ||
|
|
9dcace0fc4 | ||
|
|
a07ac38331 | ||
|
|
166e43b13e | ||
|
|
b84d6759f9 | ||
|
|
f323c814af | ||
|
|
19b47dcbc9 | ||
|
|
5edeadcbd9 | ||
|
|
d91ae2094f | ||
|
|
30a5845098 | ||
|
|
0f367efac5 | ||
|
|
24c011d4a6 | ||
|
|
0c2cdd2fb1 | ||
|
|
cd1a666e1b | ||
|
|
070b4f69d0 | ||
|
|
851c823b42 | ||
|
|
df80274644 | ||
|
|
bb2d68ddf9 | ||
|
|
685d8f725b | ||
|
|
4234732b08 | ||
|
|
a16094adfb | ||
|
|
a4349b13df | ||
|
|
3556dadea1 | ||
|
|
220e6d1907 | ||
|
|
a738037705 | ||
|
|
7daf34675a | ||
|
|
de598c2310 | ||
|
|
91cc4dbb12 | ||
|
|
0ae6f2485b | ||
|
|
52ec694d2b | ||
|
|
e2e07af8a4 | ||
|
|
2ed88c31c7 | ||
|
|
af8e4a5115 | ||
|
|
f503a9a490 | ||
|
|
9c64fbb9c4 | ||
|
|
b428f901c7 | ||
|
|
09b7f77f9a | ||
|
|
c23c0b8484 | ||
|
|
d530055917 | ||
|
|
f41d09e19f | ||
|
|
46030d9d3e | ||
|
|
02ccd3a3c7 | ||
|
|
9cc609c4b0 | ||
|
|
a0a496dcb4 | ||
|
|
8d08486edf | ||
|
|
a3b376cf0a | ||
|
|
e66f1df89d | ||
|
|
35220aac9d | ||
|
|
5b04f3ca90 | ||
|
|
beb1c36136 | ||
|
|
da3495c472 | ||
|
|
356771c0c3 | ||
|
|
fca8966ada | ||
|
|
25f80a1a9d | ||
|
|
1219f99935 | ||
|
|
33a1fe7a9c | ||
|
|
eed0a0de56 | ||
|
|
07600cbd71 | ||
|
|
f2e341b5ed | ||
|
|
55717c1ccd | ||
|
|
d5e645eb38 | ||
|
|
3196630fb9 | ||
|
|
663dde1ad7 | ||
|
|
62757cda49 | ||
|
|
af6ae9186b | ||
|
|
4c29f8888c | ||
|
|
d06ae4a60e | ||
|
|
f9af2f9733 | ||
|
|
43ae0131cd | ||
|
|
8bb8f15a7d | ||
|
|
e55f174bd3 | ||
|
|
5bd13c8d59 | ||
|
|
759580aa6f | ||
|
|
b61cbed689 | ||
|
|
2ac2247756 | ||
|
|
55f48b14b7 | ||
|
|
3dfac33858 | ||
|
|
d5f2401421 | ||
|
|
fb0570cc73 | ||
|
|
43936ba0d1 | ||
|
|
f2ba20f293 | ||
|
|
fcd384523b | ||
|
|
48bce351bf | ||
|
|
25f93875d9 | ||
|
|
ebc610fcb3 | ||
|
|
264957a443 | ||
|
|
af611b5842 | ||
|
|
1c1ca1d12f | ||
|
|
c506b9aac8 | ||
|
|
2eefa61f10 | ||
|
|
89a5e21e4d | ||
|
|
6a79771898 | ||
|
|
53353529cf | ||
|
|
22bfdf204f | ||
|
|
fc28e9ae56 | ||
|
|
17e6258694 | ||
|
|
d7a444593f | ||
|
|
701b9415a5 | ||
|
|
d5894c0738 | ||
|
|
a0a9560258 | ||
|
|
09067e06d3 | ||
|
|
dbbdd5af06 | ||
|
|
7f984cf7fa | ||
|
|
8df49b799f | ||
|
|
e7c2f71cea | ||
|
|
219085b8f6 | ||
|
|
2319f72b29 | ||
|
|
72f7d09f58 | ||
|
|
0bf39c0ab9 | ||
|
|
2e126ed2b5 | ||
|
|
a652ce7d0e | ||
|
|
a97ca73704 | ||
|
|
125d7a5c32 | ||
|
|
36356ef033 | ||
|
|
a2d1569455 | ||
|
|
952c3b2528 | ||
|
|
d92d24ad7f | ||
|
|
bc33e1cda1 | ||
|
|
189bf9c536 | ||
|
|
c5dde9b66a | ||
|
|
1fb60f8db8 | ||
|
|
2f05287e15 | ||
|
|
61226cda8c | ||
|
|
26b51d841e | ||
|
|
5f4cbaab7e | ||
|
|
7a80647fb4 | ||
|
|
14b8df3702 | ||
|
|
5cb469b204 | ||
|
|
29d7d3176d | ||
|
|
76a905d652 | ||
|
|
83f96efdfd | ||
|
|
127826a399 | ||
|
|
7ee5f4888e | ||
|
|
9ed1d1afc2 | ||
|
|
d0d9a3fa43 | ||
|
|
9600993c28 | ||
|
|
5e6f8458ff | ||
|
|
f5fe5452f6 | ||
|
|
3ac6a0c26c | ||
|
|
c2872d1e12 | ||
|
|
e47e7e3661 | ||
|
|
d8f14ec59b | ||
|
|
274a51bc38 | ||
|
|
92700e194c | ||
|
|
87df268723 | ||
|
|
17a9caf5c8 | ||
|
|
36441fabde | ||
|
|
f363998517 | ||
|
|
6fc30baba8 | ||
|
|
70a0f18d52 | ||
|
|
0ad5f5ea89 | ||
|
|
d676f39b84 | ||
|
|
31690261f5 | ||
|
|
93326488a3 | ||
|
|
d5ca98eaaa | ||
|
|
be3158c4e5 | ||
|
|
2f1d5d9255 | ||
|
|
b2c2132e4b | ||
|
|
aab6d1b153 | ||
|
|
bbbd80bf03 | ||
|
|
f27d719a4e | ||
|
|
789817cd91 | ||
|
|
885e7774fd | ||
|
|
883b7eed8a | ||
|
|
4049ed8766 | ||
|
|
f9f6803b8a | ||
|
|
385f7ebfd9 | ||
|
|
f9cbc4803d | ||
|
|
97973b1833 | ||
|
|
9cdfc15e31 | ||
|
|
fc99317291 | ||
|
|
bb9ba3e4bd | ||
|
|
649f54a1e6 | ||
|
|
4070d7ffa6 | ||
|
|
0493abb68a | ||
|
|
8c1e16711d | ||
|
|
1d03908646 | ||
|
|
49d718c025 | ||
|
|
c536b2561b | ||
|
|
b9f5ce83b0 | ||
|
|
8baab00ae0 | ||
|
|
d01cb5af46 | ||
|
|
7925ed39b8 | ||
|
|
9d869d8709 | ||
|
|
4f94e22b4b | ||
|
|
d9b720471d | ||
|
|
039b388c82 | ||
|
|
3f6528da77 | ||
|
|
4f43c060a3 | ||
|
|
3e55fe6919 | ||
|
|
754097944b | ||
|
|
dd6a25edf2 | ||
|
|
e697833976 | ||
|
|
40d80624f6 | ||
|
|
9a716cc284 | ||
|
|
13a78ecd2f | ||
|
|
a9f0c681cb | ||
|
|
862aa285a2 | ||
|
|
84d2811800 | ||
|
|
635a9d3f5a | ||
|
|
81f7f6ddf0 | ||
|
|
aa22c515ce | ||
|
|
2ca2c85365 | ||
|
|
966e6fd939 | ||
|
|
4f0dd72cf0 | ||
|
|
69aa2eff99 | ||
|
|
3e1ec36ca5 | ||
|
|
224ab8ddb1 | ||
|
|
307c2ec70f | ||
|
|
5381fb4ee9 | ||
|
|
3812ec2aa2 | ||
|
|
4e7690ebec | ||
|
|
cf3d976a68 | ||
|
|
26fc28c056 | ||
|
|
d2117ab697 | ||
|
|
52b29f673f | ||
|
|
e86b60a9d7 | ||
|
|
53501b743f | ||
|
|
c61ddb70da | ||
|
|
9339766bfe | ||
|
|
f60410016a | ||
|
|
7a02371698 | ||
|
|
579d8c9907 | ||
|
|
10c760a80c | ||
|
|
2d39a12f51 | ||
|
|
517b1ae29a | ||
|
|
b7347d931b | ||
|
|
4f878ba144 | ||
|
|
8acdb5d1e2 | ||
|
|
62f2d5736d | ||
|
|
dc22df93f5 | ||
|
|
d898bd246b | ||
|
|
ebf0ff2c0d | ||
|
|
cc77b0e9fd | ||
|
|
a8bc25e543 | ||
|
|
6615bb1b78 | ||
|
|
f650b8c515 | ||
|
|
ae2e0318d1 | ||
|
|
26ce610155 | ||
|
|
316d47e3b4 | ||
|
|
90557cf1ba | ||
|
|
80e627c86b | ||
|
|
0e4995e10b | ||
|
|
a598983f9b | ||
|
|
27641876c5 | ||
|
|
4d139eeca6 | ||
|
|
3f2806c19c | ||
|
|
e297df78e4 | ||
|
|
c1d56ede3f | ||
|
|
2e52aca3bf | ||
|
|
b0fc5832f4 | ||
|
|
cf6af112e1 | ||
|
|
fa3052e776 | ||
|
|
f8610d69f0 | ||
|
|
1a8dcce84f | ||
|
|
f74eb67567 | ||
|
|
144fcdde34 | ||
|
|
3cef7f975c | ||
|
|
a2372b0c3a | ||
|
|
362d7c517d | ||
|
|
62389b7e50 | ||
|
|
eb9e6701fd | ||
|
|
b585954b21 | ||
|
|
82ddc6a883 | ||
|
|
624b76e86e | ||
|
|
4dd0aece02 | ||
|
|
e85fb0c25e | ||
|
|
fc8783a933 | ||
|
|
e7897eb9cc | ||
|
|
59e8b79034 | ||
|
|
fb7475bf59 | ||
|
|
cd98516cae | ||
|
|
e399d8dd1f |
@@ -35,7 +35,7 @@ LDFLAGS = @LDFLAGS@
|
||||
|
||||
EXTRA_OBJS = @EXTRA_OBJS@
|
||||
|
||||
OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o \
|
||||
OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o quantiles.o \
|
||||
reference.o regress.o rtc.o samplefilt.o sched.o socket.o sources.o sourcestats.o \
|
||||
stubs.o smooth.o sys.o sys_null.o tempcomp.o util.o $(EXTRA_OBJS)
|
||||
|
||||
|
||||
91
NEWS
91
NEWS
@@ -1,3 +1,94 @@
|
||||
New in version 4.4
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add support for AES-GCM-SIV with Nettle >= 3.9 to shorten NTS
|
||||
cookies to avoid some length-specific blocking of NTP on Internet
|
||||
* Add support for multiple refclocks using extpps option on one PHC
|
||||
* Add maxpoll option to hwtimestamp directive to improve PHC tracking
|
||||
with low packet rates
|
||||
* Add hwtstimeout directive to configure timeout for late timestamps
|
||||
* Handle late hardware transmit timestamps of NTP requests on all sockets
|
||||
* Handle mismatched 32/64-bit time_t in SOCK refclock samples
|
||||
* Log important changes made by command requests (chronyc)
|
||||
* Set DSCP for IPv6 packets
|
||||
* Shorten NTS-KE retry interval when network is down
|
||||
* Warn if loading keys from file with unexpected permissions
|
||||
* Warn if source selection fails or falseticker is detected
|
||||
* Add selectopts command to modify source-specific selection options
|
||||
* Add timestamp sources to serverstats report and make its fields 64-bit
|
||||
* Add -e option to chronyc to indicate end of response
|
||||
|
||||
New in version 4.3
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add local option to refclock directive to stabilise system clock
|
||||
with more stable free-running clock (e.g. TCXO, OCXO)
|
||||
* Add maxdelayquant option to server/pool/peer directive to replace
|
||||
maxdelaydevratio filter with long-term quantile-based filtering
|
||||
* Add selection option to log directive
|
||||
* Allow external PPS in PHC refclock without configurable pin
|
||||
* Don't accept first interleaved response to minimise error in delay
|
||||
* Don't use arc4random on Linux to avoid server performance loss
|
||||
* Improve filter option to better handle missing NTP samples
|
||||
* Improve stability with hardware timestamping and PHC refclock
|
||||
* Update seccomp filter
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Fix waitsync command to reconnect when not getting response
|
||||
|
||||
New in version 4.2
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add support for NTPv4 extension field improving synchronisation
|
||||
stability and resolution of root delay and dispersion (experimental)
|
||||
* Add support for NTP over PTP (experimental)
|
||||
* Add support for AES-CMAC and hash functions in GnuTLS
|
||||
* Improve server interleaved mode to be more reliable and support
|
||||
multiple clients behind NAT
|
||||
* Update seccomp filter
|
||||
* Add statistics about interleaved mode to serverstats report
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Fix RTC support with 64-bit time_t on 32-bit Linux
|
||||
* Fix seccomp filter to work correctly with bind*device directives
|
||||
* Suppress kernel adjustments of system clock (dosynctodr) on illumos
|
||||
|
||||
Other changes
|
||||
-------------
|
||||
* Switch Solaris support to illumos
|
||||
|
||||
New in version 4.1
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add support for NTS servers specified by IP address (matching
|
||||
Subject Alternative Name in server certificate)
|
||||
* Add source-specific configuration of trusted certificates
|
||||
* Allow multiple files and directories with trusted certificates
|
||||
* Allow multiple pairs of server keys and certificates
|
||||
* Add copy option to server/pool directive
|
||||
* Increase PPS lock limit to 40% of pulse interval
|
||||
* Perform source selection immediately after loading dump files
|
||||
* Reload dump files for addresses negotiated by NTS-KE server
|
||||
* Update seccomp filter and add less restrictive level
|
||||
* Restart ongoing name resolution on online command
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Fix responding to IPv4 command requests on FreeBSD
|
||||
* Fix dump files to not include uncorrected offset
|
||||
* Fix initstepslew to accept time from own NTP clients
|
||||
* Reset NTP address and port when no longer negotiated by NTS-KE server
|
||||
|
||||
New in version 4.0
|
||||
==================
|
||||
|
||||
|
||||
13
README
13
README
@@ -28,7 +28,7 @@ What will chrony run on?
|
||||
========================
|
||||
|
||||
The software is known to work on Linux, FreeBSD, NetBSD, macOS and
|
||||
Solaris. Closely related systems may work too. Any other system will
|
||||
illumos. Closely related systems may work too. Any other system will
|
||||
likely require a porting exercise.
|
||||
|
||||
How do I set it up?
|
||||
@@ -91,7 +91,7 @@ Acknowledgements
|
||||
In writing the chronyd program, extensive use has been made of the NTPv3 (RFC
|
||||
1305) and NTPv4 (RFC 5905) specification. The source code of the xntpd/ntpd
|
||||
implementation written by Dennis Fergusson, Lars Mathiesen, David Mills, and
|
||||
others, has been used to check the details of the protocol.
|
||||
others has been used to check the details of the protocol.
|
||||
|
||||
The following people have provided patches and other major contributions
|
||||
to chrony:
|
||||
@@ -108,6 +108,8 @@ Erik Bryer <ebryer@spots.ab.ca>
|
||||
Jonathan Cameron <jic23@cam.ac.uk>
|
||||
Bryan Christianson <bryan@whatroute.net>
|
||||
Juliusz Chroboczek <jch@pps.jussieu.fr>
|
||||
Dan Drown <dan-ntp@drown.org>
|
||||
Kamil Dudka <kdudka@redhat.com>
|
||||
Christian Ehrhardt <christian.ehrhardt@canonical.com>
|
||||
Paul Elliott <pelliott@io.com>
|
||||
Robert Fairley <rfairley@redhat.com>
|
||||
@@ -120,10 +122,12 @@ Juergen Hannken-Illjes <hannken@eis.cs.tu-bs.de>
|
||||
John Hasler <john@dhh.gt.org>
|
||||
Tjalling Hattink <t.hattink@fugro.nl>
|
||||
Liam Hatton <me@liamhatton.com>
|
||||
Holger Hoffstätte <holger@applied-asynchrony.com>
|
||||
Jachym Holecek <jakym@volny.cz>
|
||||
Håkan Johansson <f96hajo@chalmers.se>
|
||||
Jim Knoble <jmknoble@pobox.com>
|
||||
Antti Jrvinen <costello@iki.fi>
|
||||
Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
|
||||
Eric Lammerts <eric@lammerts.org>
|
||||
Stefan Lucke <stefan@lucke.in-berlin.de>
|
||||
Victor Lum <viclum@vanu.com>
|
||||
@@ -134,9 +138,13 @@ Victor Moroz <vim@prv.adlum.ru>
|
||||
Kalle Olavi Niemitalo <tosi@stekt.oulu.fi>
|
||||
Frank Otto <sandwichmacher@web.de>
|
||||
Denny Page <dennypage@me.com>
|
||||
Rupesh Patel <rupatel@redhat.com>
|
||||
Chris Perl <cperl@janestreet.com>
|
||||
Gautier PHILIPPON <gautier.philippon@ensimag.grenoble-inp.fr>
|
||||
Andreas Piesk <apiesk@virbus.de>
|
||||
Mike Ryan <msr@hsilop.net>
|
||||
Baruch Siach <baruch@tkos.co.il>
|
||||
Foster Snowhill <forst@forstwoof.ru>
|
||||
Andreas Steinmetz <ast@domdv.de>
|
||||
NAKAMURA Takumi <takumi@ps.sakura.ne.jp>
|
||||
Timo Teras <timo.teras@iki.fi>
|
||||
@@ -148,6 +156,7 @@ Bernhard M. Wiedemann <bwiedemann@suse.de>
|
||||
Joachim Wiedorn <ad_debian@joonet.de>
|
||||
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
|
||||
Ulrich Windl <ulrich.windl@rz.uni-regensburg.de>
|
||||
Michael Witten <mfwitten@gmail.com>
|
||||
Doug Woodward <dougw@whistler.com>
|
||||
Thomas Zajic <zlatko@zlatko.fdns.net>
|
||||
|
||||
|
||||
15
array.c
15
array.c
@@ -116,6 +116,21 @@ ARR_AppendElement(ARR_Instance array, void *element)
|
||||
memcpy(e, element, array->elem_size);
|
||||
}
|
||||
|
||||
void
|
||||
ARR_RemoveElement(ARR_Instance array, unsigned int index)
|
||||
{
|
||||
void *e, *l;
|
||||
|
||||
e = ARR_GetElement(array, index);
|
||||
l = ARR_GetElement(array, array->used - 1);
|
||||
|
||||
if (e < l)
|
||||
memmove(e, (char *)e + array->elem_size, (char *)l - (char *)e);
|
||||
array->used--;
|
||||
|
||||
realloc_array(array, array->used);
|
||||
}
|
||||
|
||||
void
|
||||
ARR_SetSize(ARR_Instance array, unsigned int size)
|
||||
{
|
||||
|
||||
3
array.h
3
array.h
@@ -47,6 +47,9 @@ extern void *ARR_GetElements(ARR_Instance array);
|
||||
/* Add a new element to the end of the array */
|
||||
extern void ARR_AppendElement(ARR_Instance array, void *element);
|
||||
|
||||
/* Remove element with given index */
|
||||
extern void ARR_RemoveElement(ARR_Instance array, unsigned int index);
|
||||
|
||||
/* Set the size of the array */
|
||||
extern void ARR_SetSize(ARR_Instance array, unsigned int size);
|
||||
|
||||
|
||||
67
candm.h
67
candm.h
@@ -108,7 +108,9 @@
|
||||
#define REQ_CLIENT_ACCESSES_BY_INDEX3 68
|
||||
#define REQ_SELECT_DATA 69
|
||||
#define REQ_RELOAD_SOURCES 70
|
||||
#define N_REQUEST_TYPES 71
|
||||
#define REQ_DOFFSET2 71
|
||||
#define REQ_MODIFY_SELECTOPTS 72
|
||||
#define N_REQUEST_TYPES 73
|
||||
|
||||
/* Structure used to exchange timespecs independent of time_t size */
|
||||
typedef struct {
|
||||
@@ -120,6 +122,12 @@ typedef struct {
|
||||
/* This is used in tv_sec_high for 32-bit timestamps */
|
||||
#define TV_NOHIGHSEC 0x7fffffff
|
||||
|
||||
/* Structure for 64-bit integers (not requiring 64-bit alignment) */
|
||||
typedef struct {
|
||||
uint32_t high;
|
||||
uint32_t low;
|
||||
} Integer64;
|
||||
|
||||
/* 32-bit floating-point format consisting of 7-bit signed exponent
|
||||
and 25-bit signed coefficient without hidden bit.
|
||||
The result is calculated as: 2^(exp - 25) * coef */
|
||||
@@ -268,6 +276,8 @@ typedef struct {
|
||||
#define REQ_ADDSRC_INTERLEAVED 0x80
|
||||
#define REQ_ADDSRC_BURST 0x100
|
||||
#define REQ_ADDSRC_NTS 0x200
|
||||
#define REQ_ADDSRC_COPY 0x400
|
||||
#define REQ_ADDSRC_EF_EXP1 0x800
|
||||
|
||||
typedef struct {
|
||||
uint32_t type;
|
||||
@@ -292,7 +302,9 @@ typedef struct {
|
||||
Float offset;
|
||||
uint32_t flags;
|
||||
int32_t filter_length;
|
||||
uint32_t reserved[3];
|
||||
uint32_t cert_set;
|
||||
Float max_delay_quant;
|
||||
uint32_t reserved[1];
|
||||
int32_t EOR;
|
||||
} REQ_NTP_Source;
|
||||
|
||||
@@ -307,8 +319,7 @@ typedef struct {
|
||||
} REQ_Dfreq;
|
||||
|
||||
typedef struct {
|
||||
int32_t sec;
|
||||
int32_t usec;
|
||||
Float doffset;
|
||||
int32_t EOR;
|
||||
} REQ_Doffset;
|
||||
|
||||
@@ -367,6 +378,15 @@ typedef struct {
|
||||
int32_t EOR;
|
||||
} REQ_SelectData;
|
||||
|
||||
/* Mask and options reuse the REQ_ADDSRC flags */
|
||||
typedef struct {
|
||||
IPAddr address;
|
||||
uint32_t ref_id;
|
||||
uint32_t mask;
|
||||
uint32_t options;
|
||||
int32_t EOR;
|
||||
} REQ_Modify_SelectOpts;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define PKT_TYPE_CMD_REQUEST 1
|
||||
@@ -403,7 +423,7 @@ typedef struct {
|
||||
domain socket.
|
||||
|
||||
Version 6 (no authentication) : changed format of client accesses by index
|
||||
(using new request/reply types) and manual timestamp, added new fields and
|
||||
(two times), delta offset, and manual timestamp, added new fields and
|
||||
flags to NTP source request and report, made length of manual list constant,
|
||||
added new commands: authdata, ntpdata, onoffline, refresh, reset,
|
||||
selectdata, serverstats, shutdown, sourcename
|
||||
@@ -473,6 +493,7 @@ typedef struct {
|
||||
REQ_NTPSourceName ntp_source_name;
|
||||
REQ_AuthData auth_data;
|
||||
REQ_SelectData select_data;
|
||||
REQ_Modify_SelectOpts modify_select_opts;
|
||||
} data; /* Command specific parameters */
|
||||
|
||||
/* Padding used to prevent traffic amplification. It only defines the
|
||||
@@ -514,7 +535,9 @@ typedef struct {
|
||||
#define RPY_CLIENT_ACCESSES_BY_INDEX3 21
|
||||
#define RPY_SERVER_STATS2 22
|
||||
#define RPY_SELECT_DATA 23
|
||||
#define N_REPLY_TYPES 24
|
||||
#define RPY_SERVER_STATS3 24
|
||||
#define RPY_SERVER_STATS4 25
|
||||
#define N_REPLY_TYPES 26
|
||||
|
||||
/* Status codes */
|
||||
#define STT_SUCCESS 0
|
||||
@@ -527,8 +550,7 @@ typedef struct {
|
||||
#define STT_BADSUBNET 7
|
||||
#define STT_ACCESSALLOWED 8
|
||||
#define STT_ACCESSDENIED 9
|
||||
/* Deprecated */
|
||||
#define STT_NOHOSTACCESS 10
|
||||
#define STT_NOHOSTACCESS 10 /* Deprecated */
|
||||
#define STT_SOURCEALREADYKNOWN 11
|
||||
#define STT_TOOMANYSOURCES 12
|
||||
#define STT_NORTC 13
|
||||
@@ -650,14 +672,24 @@ typedef struct {
|
||||
} RPY_ClientAccessesByIndex;
|
||||
|
||||
typedef struct {
|
||||
uint32_t ntp_hits;
|
||||
uint32_t nke_hits;
|
||||
uint32_t cmd_hits;
|
||||
uint32_t ntp_drops;
|
||||
uint32_t nke_drops;
|
||||
uint32_t cmd_drops;
|
||||
uint32_t log_drops;
|
||||
uint32_t ntp_auth_hits;
|
||||
Integer64 ntp_hits;
|
||||
Integer64 nke_hits;
|
||||
Integer64 cmd_hits;
|
||||
Integer64 ntp_drops;
|
||||
Integer64 nke_drops;
|
||||
Integer64 cmd_drops;
|
||||
Integer64 log_drops;
|
||||
Integer64 ntp_auth_hits;
|
||||
Integer64 ntp_interleaved_hits;
|
||||
Integer64 ntp_timestamps;
|
||||
Integer64 ntp_span_seconds;
|
||||
Integer64 ntp_daemon_rx_timestamps;
|
||||
Integer64 ntp_daemon_tx_timestamps;
|
||||
Integer64 ntp_kernel_rx_timestamps;
|
||||
Integer64 ntp_kernel_tx_timestamps;
|
||||
Integer64 ntp_hw_rx_timestamps;
|
||||
Integer64 ntp_hw_tx_timestamps;
|
||||
Integer64 reserved[4];
|
||||
int32_t EOR;
|
||||
} RPY_ServerStats;
|
||||
|
||||
@@ -727,7 +759,8 @@ typedef struct {
|
||||
uint32_t total_tx_count;
|
||||
uint32_t total_rx_count;
|
||||
uint32_t total_valid_count;
|
||||
uint32_t reserved[4];
|
||||
uint32_t total_good_count;
|
||||
uint32_t reserved[3];
|
||||
int32_t EOR;
|
||||
} RPY_NTPData;
|
||||
|
||||
|
||||
548
client.c
548
client.c
@@ -4,7 +4,7 @@
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Lonnie Abelbeck 2016, 2018
|
||||
* Copyright (C) Miroslav Lichvar 2009-2020
|
||||
* Copyright (C) Miroslav Lichvar 2009-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -61,7 +61,7 @@ static ARR_Instance server_addresses;
|
||||
|
||||
static int sock_fd = -1;
|
||||
|
||||
static int quit = 0;
|
||||
static volatile int quit = 0;
|
||||
|
||||
static int on_terminal = 0;
|
||||
|
||||
@@ -71,6 +71,8 @@ static int source_names = 0;
|
||||
|
||||
static int csv_mode = 0;
|
||||
|
||||
static int end_dot = 0;
|
||||
|
||||
/* ================================================== */
|
||||
/* Log a message. This is a minimalistic replacement of the logging.c
|
||||
implementation to avoid linking with it and other modules. */
|
||||
@@ -283,6 +285,9 @@ open_io(void)
|
||||
close_io();
|
||||
}
|
||||
|
||||
/* Start from the first address if called again */
|
||||
address_index = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -779,182 +784,25 @@ process_cmd_manual(CMD_Request *msg, const char *line)
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
parse_allow_deny(CMD_Request *msg, char *line)
|
||||
process_cmd_allowdeny(CMD_Request *msg, char *line, int cmd, int allcmd)
|
||||
{
|
||||
unsigned long a, b, c, d;
|
||||
int n, specified_subnet_bits;
|
||||
int all, subnet_bits;
|
||||
IPAddr ip;
|
||||
char *p;
|
||||
|
||||
p = line;
|
||||
if (!*p) {
|
||||
/* blank line - applies to all addresses */
|
||||
ip.family = IPADDR_UNSPEC;
|
||||
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
|
||||
msg->data.allow_deny.subnet_bits = htonl(0);
|
||||
} else {
|
||||
char *slashpos;
|
||||
slashpos = strchr(p, '/');
|
||||
if (slashpos) *slashpos = 0;
|
||||
|
||||
n = 0;
|
||||
if (!UTI_StringToIP(p, &ip) &&
|
||||
(n = sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d)) <= 0) {
|
||||
|
||||
/* Try to parse as the name of a machine */
|
||||
if (slashpos || DNS_Name2IPAddress(p, &ip, 1) != DNS_Success) {
|
||||
LOG(LOGS_ERR, "Could not read address");
|
||||
return 0;
|
||||
} else {
|
||||
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
|
||||
if (ip.family == IPADDR_INET6)
|
||||
msg->data.allow_deny.subnet_bits = htonl(128);
|
||||
else
|
||||
msg->data.allow_deny.subnet_bits = htonl(32);
|
||||
}
|
||||
} else {
|
||||
|
||||
if (n == 0) {
|
||||
if (ip.family == IPADDR_INET6)
|
||||
msg->data.allow_deny.subnet_bits = htonl(128);
|
||||
else
|
||||
msg->data.allow_deny.subnet_bits = htonl(32);
|
||||
} else {
|
||||
ip.family = IPADDR_INET4;
|
||||
|
||||
a &= 0xff;
|
||||
b &= 0xff;
|
||||
c &= 0xff;
|
||||
d &= 0xff;
|
||||
|
||||
switch (n) {
|
||||
case 1:
|
||||
ip.addr.in4 = htonl((a<<24));
|
||||
msg->data.allow_deny.subnet_bits = htonl(8);
|
||||
break;
|
||||
case 2:
|
||||
ip.addr.in4 = htonl((a<<24) | (b<<16));
|
||||
msg->data.allow_deny.subnet_bits = htonl(16);
|
||||
break;
|
||||
case 3:
|
||||
ip.addr.in4 = htonl((a<<24) | (b<<16) | (c<<8));
|
||||
msg->data.allow_deny.subnet_bits = htonl(24);
|
||||
break;
|
||||
case 4:
|
||||
ip.addr.in4 = htonl((a<<24) | (b<<16) | (c<<8) | d);
|
||||
msg->data.allow_deny.subnet_bits = htonl(32);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
|
||||
|
||||
if (slashpos) {
|
||||
n = sscanf(slashpos+1, "%d", &specified_subnet_bits);
|
||||
if (n == 1) {
|
||||
msg->data.allow_deny.subnet_bits = htonl(specified_subnet_bits);
|
||||
} else {
|
||||
LOG(LOGS_WARN, "Warning: badly formatted subnet size, using %d",
|
||||
(int)ntohl(msg->data.allow_deny.subnet_bits));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!CPS_ParseAllowDeny(line, &all, &ip, &subnet_bits)) {
|
||||
LOG(LOGS_ERR, "Could not read address");
|
||||
return 0;
|
||||
}
|
||||
|
||||
msg->command = htons(all ? allcmd : cmd);
|
||||
UTI_IPHostToNetwork(&ip, &msg->data.allow_deny.ip);
|
||||
msg->data.allow_deny.subnet_bits = htonl(subnet_bits);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_allow(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_ALLOW);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_allowall(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_ALLOWALL);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_deny(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_DENY);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_denyall(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_DENYALL);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_cmdallow(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_CMDALLOW);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_cmdallowall(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_CMDALLOWALL);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_cmddeny(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_CMDDENY);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_cmddenyall(CMD_Request *msg, char *line)
|
||||
{
|
||||
int status;
|
||||
msg->command = htons(REQ_CMDDENYALL);
|
||||
status = parse_allow_deny(msg, line);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_accheck(CMD_Request *msg, char *line)
|
||||
{
|
||||
@@ -987,35 +835,49 @@ process_cmd_cmdaccheck(CMD_Request *msg, char *line)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
static int
|
||||
process_cmd_dfreq(CMD_Request *msg, char *line)
|
||||
{
|
||||
double dfreq;
|
||||
|
||||
msg->command = htons(REQ_DFREQ);
|
||||
if (sscanf(line, "%lf", &dfreq) == 1) {
|
||||
msg->data.dfreq.dfreq = UTI_FloatHostToNetwork(dfreq);
|
||||
} else {
|
||||
msg->data.dfreq.dfreq = UTI_FloatHostToNetwork(0.0);
|
||||
|
||||
if (sscanf(line, "%lf", &dfreq) != 1) {
|
||||
LOG(LOGS_ERR, "Invalid value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
msg->data.dfreq.dfreq = UTI_FloatHostToNetwork(dfreq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
static int
|
||||
process_cmd_doffset(CMD_Request *msg, char *line)
|
||||
{
|
||||
struct timeval tv;
|
||||
double doffset;
|
||||
|
||||
msg->command = htons(REQ_DOFFSET);
|
||||
if (sscanf(line, "%lf", &doffset) == 1) {
|
||||
UTI_DoubleToTimeval(doffset, &tv);
|
||||
msg->data.doffset.sec = htonl(tv.tv_sec);
|
||||
msg->data.doffset.usec = htonl(tv.tv_usec);
|
||||
} else {
|
||||
msg->data.doffset.sec = htonl(0);
|
||||
msg->data.doffset.usec = htonl(0);
|
||||
msg->command = htons(REQ_DOFFSET2);
|
||||
|
||||
if (sscanf(line, "%lf", &doffset) != 1) {
|
||||
LOG(LOGS_ERR, "Invalid value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
msg->data.doffset.doffset = UTI_FloatHostToNetwork(doffset);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
convert_addsrc_sel_options(int options)
|
||||
{
|
||||
return (options & SRC_SELECT_PREFER ? REQ_ADDSRC_PREFER : 0) |
|
||||
(options & SRC_SELECT_NOSELECT ? REQ_ADDSRC_NOSELECT : 0) |
|
||||
(options & SRC_SELECT_TRUST ? REQ_ADDSRC_TRUST : 0) |
|
||||
(options & SRC_SELECT_REQUIRE ? REQ_ADDSRC_REQUIRE : 0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1095,11 +957,13 @@ process_cmd_add_source(CMD_Request *msg, char *line)
|
||||
(data.params.interleaved ? REQ_ADDSRC_INTERLEAVED : 0) |
|
||||
(data.params.burst ? REQ_ADDSRC_BURST : 0) |
|
||||
(data.params.nts ? REQ_ADDSRC_NTS : 0) |
|
||||
(data.params.sel_options & SRC_SELECT_PREFER ? REQ_ADDSRC_PREFER : 0) |
|
||||
(data.params.sel_options & SRC_SELECT_NOSELECT ? REQ_ADDSRC_NOSELECT : 0) |
|
||||
(data.params.sel_options & SRC_SELECT_TRUST ? REQ_ADDSRC_TRUST : 0) |
|
||||
(data.params.sel_options & SRC_SELECT_REQUIRE ? REQ_ADDSRC_REQUIRE : 0));
|
||||
(data.params.copy ? REQ_ADDSRC_COPY : 0) |
|
||||
(data.params.ext_fields & NTP_EF_FLAG_EXP1 ? REQ_ADDSRC_EF_EXP1 : 0) |
|
||||
convert_addsrc_sel_options(data.params.sel_options));
|
||||
msg->data.ntp_source.filter_length = htonl(data.params.filter_length);
|
||||
msg->data.ntp_source.cert_set = htonl(data.params.cert_set);
|
||||
msg->data.ntp_source.max_delay_quant =
|
||||
UTI_FloatHostToNetwork(data.params.max_delay_quant);
|
||||
memset(msg->data.ntp_source.reserved, 0, sizeof (msg->data.ntp_source.reserved));
|
||||
|
||||
result = 1;
|
||||
@@ -1159,6 +1023,7 @@ give_help(void)
|
||||
"sources [-a] [-v]\0Display information about current sources\0"
|
||||
"sourcestats [-a] [-v]\0Display statistics about collected measurements\0"
|
||||
"selectdata [-a] [-v]\0Display information about source selection\0"
|
||||
"selectopts <address|refid> <+|-options>\0Modify selection options\0"
|
||||
"reselect\0Force reselecting synchronisation source\0"
|
||||
"reselectdist <dist>\0Modify reselection distance\0"
|
||||
"\0\0"
|
||||
@@ -1273,8 +1138,8 @@ command_name_generator(const char *text, int state)
|
||||
"manual", "maxdelay", "maxdelaydevratio", "maxdelayratio", "maxpoll",
|
||||
"maxupdateskew", "minpoll", "minstratum", "ntpdata", "offline", "online", "onoffline",
|
||||
"polltarget", "quit", "refresh", "rekey", "reload", "reselect", "reselectdist", "reset",
|
||||
"retries", "rtcdata", "selectdata", "serverstats", "settime", "shutdown", "smoothing",
|
||||
"smoothtime", "sourcename", "sources", "sourcestats",
|
||||
"retries", "rtcdata", "selectdata", "selectopts", "serverstats", "settime",
|
||||
"shutdown", "smoothing", "smoothtime", "sourcename", "sources", "sourcestats",
|
||||
"timeout", "tracking", "trimrtc", "waitsync", "writertc",
|
||||
NULL
|
||||
};
|
||||
@@ -1608,24 +1473,24 @@ request_reply(CMD_Request *request, CMD_Reply *reply, int requested_reply, int v
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
print_seconds(unsigned long s)
|
||||
print_seconds(uint32_t s)
|
||||
{
|
||||
unsigned long d;
|
||||
uint32_t d;
|
||||
|
||||
if (s == (uint32_t)-1) {
|
||||
printf(" -");
|
||||
} else if (s < 1200) {
|
||||
printf("%4lu", s);
|
||||
printf("%4"PRIu32, s);
|
||||
} else if (s < 36000) {
|
||||
printf("%3lum", s / 60);
|
||||
printf("%3"PRIu32"m", s / 60);
|
||||
} else if (s < 345600) {
|
||||
printf("%3luh", s / 3600);
|
||||
printf("%3"PRIu32"h", s / 3600);
|
||||
} else {
|
||||
d = s / 86400;
|
||||
if (d > 999) {
|
||||
printf("%3luy", d / 365);
|
||||
printf("%3"PRIu32"y", d / 365);
|
||||
} else {
|
||||
printf("%3lud", d);
|
||||
printf("%3"PRIu32"d", d);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1756,8 +1621,9 @@ print_report(const char *format, ...)
|
||||
va_list ap;
|
||||
int i, field, sign, width, prec, spec;
|
||||
const char *string;
|
||||
unsigned long long_uinteger;
|
||||
unsigned int uinteger;
|
||||
uint64_t uinteger64;
|
||||
uint32_t uinteger32;
|
||||
int integer;
|
||||
struct timespec *ts;
|
||||
struct tm *tm;
|
||||
@@ -1855,9 +1721,9 @@ print_report(const char *format, ...)
|
||||
spec == 'O' ? "seconds" : "ppm",
|
||||
(dbl > 0.0) ^ (spec != 'O') ? "slow" : "fast");
|
||||
break;
|
||||
case 'I': /* interval with unit */
|
||||
long_uinteger = va_arg(ap, unsigned long);
|
||||
print_seconds(long_uinteger);
|
||||
case 'I': /* uint32_t interval with unit */
|
||||
uinteger32 = va_arg(ap, uint32_t);
|
||||
print_seconds(uinteger32);
|
||||
break;
|
||||
case 'L': /* leap status */
|
||||
integer = va_arg(ap, int);
|
||||
@@ -1924,8 +1790,8 @@ print_report(const char *format, ...)
|
||||
print_freq_ppm(dbl);
|
||||
break;
|
||||
case 'R': /* reference ID in hexdecimal */
|
||||
long_uinteger = va_arg(ap, unsigned long);
|
||||
printf("%08lX", long_uinteger);
|
||||
uinteger32 = va_arg(ap, uint32_t);
|
||||
printf("%08"PRIX32, uinteger32);
|
||||
break;
|
||||
case 'S': /* offset with unit */
|
||||
dbl = va_arg(ap, double);
|
||||
@@ -1942,14 +1808,18 @@ print_report(const char *format, ...)
|
||||
strftime(buf, sizeof (buf), "%a %b %d %T %Y", tm);
|
||||
printf("%s", buf);
|
||||
break;
|
||||
case 'U': /* unsigned long in decimal */
|
||||
long_uinteger = va_arg(ap, unsigned long);
|
||||
printf("%*lu", width, long_uinteger);
|
||||
case 'U': /* uint32_t in decimal */
|
||||
uinteger32 = va_arg(ap, uint32_t);
|
||||
printf("%*"PRIu32, width, uinteger32);
|
||||
break;
|
||||
case 'V': /* timespec as seconds since epoch */
|
||||
ts = va_arg(ap, struct timespec *);
|
||||
printf("%s", UTI_TimespecToString(ts));
|
||||
break;
|
||||
case 'Q': /* uint64_t in decimal */
|
||||
uinteger64 = va_arg(ap, uint64_t);
|
||||
printf("%*"PRIu64, width, uinteger64);
|
||||
break;
|
||||
case 'b': /* unsigned int in binary */
|
||||
uinteger = va_arg(ap, unsigned int);
|
||||
for (i = prec - 1; i >= 0; i--)
|
||||
@@ -2086,7 +1956,7 @@ process_cmd_sourcename(char *line)
|
||||
IPAddr ip_addr;
|
||||
char name[256];
|
||||
|
||||
if (!UTI_StringToIP(line, &ip_addr)) {
|
||||
if (!parse_source_address(line, &ip_addr)) {
|
||||
LOG(LOGS_ERR, "Could not read address");
|
||||
return 0;
|
||||
}
|
||||
@@ -2197,7 +2067,7 @@ process_cmd_sources(char *line)
|
||||
ntohs(reply.data.source_data.stratum),
|
||||
(int16_t)ntohs(reply.data.source_data.poll),
|
||||
ntohs(reply.data.source_data.reachability),
|
||||
(unsigned long)ntohl(reply.data.source_data.since_sample),
|
||||
ntohl(reply.data.source_data.since_sample),
|
||||
UTI_FloatNetworkToHost(reply.data.source_data.latest_meas),
|
||||
UTI_FloatNetworkToHost(reply.data.source_data.orig_latest_meas),
|
||||
UTI_FloatNetworkToHost(reply.data.source_data.latest_meas_err),
|
||||
@@ -2258,9 +2128,9 @@ process_cmd_sourcestats(char *line)
|
||||
|
||||
print_report("%-25s %3U %3U %I %+P %P %+S %S\n",
|
||||
name,
|
||||
(unsigned long)ntohl(reply.data.sourcestats.n_samples),
|
||||
(unsigned long)ntohl(reply.data.sourcestats.n_runs),
|
||||
(unsigned long)ntohl(reply.data.sourcestats.span_seconds),
|
||||
ntohl(reply.data.sourcestats.n_samples),
|
||||
ntohl(reply.data.sourcestats.n_runs),
|
||||
ntohl(reply.data.sourcestats.span_seconds),
|
||||
UTI_FloatNetworkToHost(reply.data.sourcestats.resid_freq_ppm),
|
||||
UTI_FloatNetworkToHost(reply.data.sourcestats.skew_ppm),
|
||||
UTI_FloatNetworkToHost(reply.data.sourcestats.est_offset),
|
||||
@@ -2308,7 +2178,7 @@ process_cmd_tracking(char *line)
|
||||
"Root dispersion : %.9f seconds\n"
|
||||
"Update interval : %.1f seconds\n"
|
||||
"Leap status : %L\n",
|
||||
(unsigned long)ref_id, name,
|
||||
ref_id, name,
|
||||
ntohs(reply.data.tracking.stratum),
|
||||
&ref_time,
|
||||
UTI_FloatNetworkToHost(reply.data.tracking.current_correction),
|
||||
@@ -2396,10 +2266,10 @@ process_cmd_authdata(char *line)
|
||||
|
||||
print_report("%-27s %4s %5U %4d %4d %I %4d %4d %4d %4d\n",
|
||||
name, mode_str,
|
||||
(unsigned long)ntohl(reply.data.auth_data.key_id),
|
||||
ntohl(reply.data.auth_data.key_id),
|
||||
ntohs(reply.data.auth_data.key_type),
|
||||
ntohs(reply.data.auth_data.key_length),
|
||||
(unsigned long)ntohl(reply.data.auth_data.last_ke_ago),
|
||||
ntohl(reply.data.auth_data.last_ke_ago),
|
||||
ntohs(reply.data.auth_data.ke_attempts),
|
||||
ntohs(reply.data.auth_data.nak),
|
||||
ntohs(reply.data.auth_data.cookies),
|
||||
@@ -2492,18 +2362,18 @@ process_cmd_ntpdata(char *line)
|
||||
"RX timestamping : %N\n"
|
||||
"Total TX : %U\n"
|
||||
"Total RX : %U\n"
|
||||
"Total valid RX : %U\n",
|
||||
UTI_IPToString(&remote_addr), (unsigned long)UTI_IPToRefid(&remote_addr),
|
||||
"Total valid RX : %U\n"
|
||||
"Total good RX : %U\n",
|
||||
UTI_IPToString(&remote_addr), UTI_IPToRefid(&remote_addr),
|
||||
ntohs(reply.data.ntp_data.remote_port),
|
||||
UTI_IPToString(&local_addr), (unsigned long)UTI_IPToRefid(&local_addr),
|
||||
UTI_IPToString(&local_addr), UTI_IPToRefid(&local_addr),
|
||||
reply.data.ntp_data.leap, reply.data.ntp_data.version,
|
||||
reply.data.ntp_data.mode, reply.data.ntp_data.stratum,
|
||||
reply.data.ntp_data.poll, UTI_Log2ToDouble(reply.data.ntp_data.poll),
|
||||
reply.data.ntp_data.precision, UTI_Log2ToDouble(reply.data.ntp_data.precision),
|
||||
UTI_FloatNetworkToHost(reply.data.ntp_data.root_delay),
|
||||
UTI_FloatNetworkToHost(reply.data.ntp_data.root_dispersion),
|
||||
(unsigned long)ntohl(reply.data.ntp_data.ref_id),
|
||||
reply.data.ntp_data.stratum <= 1 ?
|
||||
ntohl(reply.data.ntp_data.ref_id), reply.data.ntp_data.stratum <= 1 ?
|
||||
UTI_RefidToString(ntohl(reply.data.ntp_data.ref_id)) : "",
|
||||
&ref_time,
|
||||
UTI_FloatNetworkToHost(reply.data.ntp_data.offset),
|
||||
@@ -2517,9 +2387,10 @@ process_cmd_ntpdata(char *line)
|
||||
ntohs(reply.data.ntp_data.flags) & RPY_NTP_FLAG_INTERLEAVED,
|
||||
ntohs(reply.data.ntp_data.flags) & RPY_NTP_FLAG_AUTHENTICATED,
|
||||
reply.data.ntp_data.tx_tss_char, reply.data.ntp_data.rx_tss_char,
|
||||
(unsigned long)ntohl(reply.data.ntp_data.total_tx_count),
|
||||
(unsigned long)ntohl(reply.data.ntp_data.total_rx_count),
|
||||
(unsigned long)ntohl(reply.data.ntp_data.total_valid_count),
|
||||
ntohl(reply.data.ntp_data.total_tx_count),
|
||||
ntohl(reply.data.ntp_data.total_rx_count),
|
||||
ntohl(reply.data.ntp_data.total_valid_count),
|
||||
ntohl(reply.data.ntp_data.total_good_count),
|
||||
REPORT_END);
|
||||
}
|
||||
|
||||
@@ -2547,12 +2418,12 @@ process_cmd_selectdata(char *line)
|
||||
n_sources = ntohl(reply.data.n_sources.n_sources);
|
||||
|
||||
if (verbose) {
|
||||
printf( " .-- State: N - noselect, M - missing samples, d/D - large distance,\n");
|
||||
printf( " / ~ - jittery, w/W - waits for others, T - not trusted,\n");
|
||||
printf( "| x - falseticker, P - not preferred, U - waits for update,\n");
|
||||
printf( "| S - stale, O - orphan, + - combined, * - best.\n");
|
||||
printf( "| Effective options ------. (N - noselect, P - prefer\n");
|
||||
printf( "| Configured options -. \\ T - trust, R - require)\n");
|
||||
printf( " . State: N - noselect, s - unsynchronised, M - missing samples,\n");
|
||||
printf( " / d/D - large distance, ~ - jittery, w/W - waits for others,\n");
|
||||
printf( "| S - stale, O - orphan, T - not trusted, P - not preferred,\n");
|
||||
printf( "| U - waits for update,, x - falseticker, + - combined, * - best.\n");
|
||||
printf( "| Effective options ---------. (N - noselect, P - prefer\n");
|
||||
printf( "| Configured options ----. \\ T - trust, R - require)\n");
|
||||
printf( "| Auth. enabled (Y/N) -. \\ \\ Offset interval --.\n");
|
||||
printf( "| | | | |\n");
|
||||
}
|
||||
@@ -2591,7 +2462,7 @@ process_cmd_selectdata(char *line)
|
||||
eff_options & RPY_SD_OPTION_TRUST ? 'T' : '-',
|
||||
eff_options & RPY_SD_OPTION_REQUIRE ? 'R' : '-',
|
||||
'-',
|
||||
(unsigned long)ntohl(reply.data.select_data.last_sample_ago),
|
||||
ntohl(reply.data.select_data.last_sample_ago),
|
||||
UTI_FloatNetworkToHost(reply.data.select_data.score),
|
||||
UTI_FloatNetworkToHost(reply.data.select_data.lo_limit),
|
||||
UTI_FloatNetworkToHost(reply.data.select_data.hi_limit),
|
||||
@@ -2611,25 +2482,43 @@ process_cmd_serverstats(char *line)
|
||||
CMD_Reply reply;
|
||||
|
||||
request.command = htons(REQ_SERVER_STATS);
|
||||
if (!request_reply(&request, &reply, RPY_SERVER_STATS2, 0))
|
||||
if (!request_reply(&request, &reply, RPY_SERVER_STATS4, 0))
|
||||
return 0;
|
||||
|
||||
print_report("NTP packets received : %U\n"
|
||||
"NTP packets dropped : %U\n"
|
||||
"Command packets received : %U\n"
|
||||
"Command packets dropped : %U\n"
|
||||
"Client log records dropped : %U\n"
|
||||
"NTS-KE connections accepted: %U\n"
|
||||
"NTS-KE connections dropped : %U\n"
|
||||
"Authenticated NTP packets : %U\n",
|
||||
(unsigned long)ntohl(reply.data.server_stats.ntp_hits),
|
||||
(unsigned long)ntohl(reply.data.server_stats.ntp_drops),
|
||||
(unsigned long)ntohl(reply.data.server_stats.cmd_hits),
|
||||
(unsigned long)ntohl(reply.data.server_stats.cmd_drops),
|
||||
(unsigned long)ntohl(reply.data.server_stats.log_drops),
|
||||
(unsigned long)ntohl(reply.data.server_stats.nke_hits),
|
||||
(unsigned long)ntohl(reply.data.server_stats.nke_drops),
|
||||
(unsigned long)ntohl(reply.data.server_stats.ntp_auth_hits),
|
||||
print_report("NTP packets received : %Q\n"
|
||||
"NTP packets dropped : %Q\n"
|
||||
"Command packets received : %Q\n"
|
||||
"Command packets dropped : %Q\n"
|
||||
"Client log records dropped : %Q\n"
|
||||
"NTS-KE connections accepted: %Q\n"
|
||||
"NTS-KE connections dropped : %Q\n"
|
||||
"Authenticated NTP packets : %Q\n"
|
||||
"Interleaved NTP packets : %Q\n"
|
||||
"NTP timestamps held : %Q\n"
|
||||
"NTP timestamp span : %Q\n"
|
||||
"NTP daemon RX timestamps : %Q\n"
|
||||
"NTP daemon TX timestamps : %Q\n"
|
||||
"NTP kernel RX timestamps : %Q\n"
|
||||
"NTP kernel TX timestamps : %Q\n"
|
||||
"NTP hardware RX timestamps : %Q\n"
|
||||
"NTP hardware TX timestamps : %Q\n",
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_hits),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_drops),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.cmd_hits),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.cmd_drops),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.log_drops),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.nke_hits),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.nke_drops),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_auth_hits),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_interleaved_hits),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_span_seconds),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_daemon_rx_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_daemon_tx_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_kernel_rx_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_kernel_tx_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_hw_rx_timestamps),
|
||||
UTI_Integer64NetworkToHost(reply.data.server_stats.ntp_hw_tx_timestamps),
|
||||
REPORT_END);
|
||||
|
||||
return 1;
|
||||
@@ -2711,7 +2600,7 @@ process_cmd_rtcreport(char *line)
|
||||
&ref_time,
|
||||
ntohs(reply.data.rtc.n_samples),
|
||||
ntohs(reply.data.rtc.n_runs),
|
||||
(unsigned long)ntohl(reply.data.rtc.span_seconds),
|
||||
ntohl(reply.data.rtc.span_seconds),
|
||||
UTI_FloatNetworkToHost(reply.data.rtc.rtc_seconds_fast),
|
||||
UTI_FloatNetworkToHost(reply.data.rtc.rtc_gain_rate_ppm),
|
||||
REPORT_END);
|
||||
@@ -2786,16 +2675,15 @@ process_cmd_clients(char *line)
|
||||
|
||||
print_report("%-25s %6U %5U %C %C %I %6U %5U %C %I\n",
|
||||
name,
|
||||
(unsigned long)ntohl(client->ntp_hits),
|
||||
(unsigned long)ntohl(client->ntp_drops),
|
||||
ntohl(client->ntp_hits),
|
||||
ntohl(client->ntp_drops),
|
||||
client->ntp_interval,
|
||||
client->ntp_timeout_interval,
|
||||
(unsigned long)ntohl(client->last_ntp_hit_ago),
|
||||
(unsigned long)ntohl(nke ? client->nke_hits : client->cmd_hits),
|
||||
(unsigned long)ntohl(nke ? client->nke_drops : client->cmd_drops),
|
||||
ntohl(client->last_ntp_hit_ago),
|
||||
ntohl(nke ? client->nke_hits : client->cmd_hits),
|
||||
ntohl(nke ? client->nke_drops : client->cmd_drops),
|
||||
nke ? client->nke_interval : client->cmd_interval,
|
||||
(unsigned long)ntohl(nke ? client->last_nke_hit_ago :
|
||||
client->last_cmd_hit_ago),
|
||||
ntohl(nke ? client->last_nke_hit_ago : client->last_cmd_hit_ago),
|
||||
REPORT_END);
|
||||
}
|
||||
|
||||
@@ -2826,7 +2714,7 @@ process_cmd_manual_list(const char *line)
|
||||
return 0;
|
||||
|
||||
n_samples = ntohl(reply.data.manual_list.n_samples);
|
||||
print_info_field("210 n_samples = %lu\n", (unsigned long)n_samples);
|
||||
print_info_field("210 n_samples = %"PRIu32"\n", n_samples);
|
||||
|
||||
print_header("# Date Time(UTC) Slewed Original Residual");
|
||||
|
||||
@@ -2946,11 +2834,11 @@ process_cmd_activity(const char *line)
|
||||
"%U sources doing burst (return to online)\n"
|
||||
"%U sources doing burst (return to offline)\n"
|
||||
"%U sources with unknown address\n",
|
||||
(unsigned long)ntohl(reply.data.activity.online),
|
||||
(unsigned long)ntohl(reply.data.activity.offline),
|
||||
(unsigned long)ntohl(reply.data.activity.burst_online),
|
||||
(unsigned long)ntohl(reply.data.activity.burst_offline),
|
||||
(unsigned long)ntohl(reply.data.activity.unresolved),
|
||||
ntohl(reply.data.activity.online),
|
||||
ntohl(reply.data.activity.offline),
|
||||
ntohl(reply.data.activity.burst_online),
|
||||
ntohl(reply.data.activity.burst_offline),
|
||||
ntohl(reply.data.activity.unresolved),
|
||||
REPORT_END);
|
||||
|
||||
return 1;
|
||||
@@ -3029,6 +2917,55 @@ process_cmd_reset(CMD_Request *msg, char *line)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_selectopts(CMD_Request *msg, char *line)
|
||||
{
|
||||
int mask, options, option;
|
||||
uint32_t ref_id;
|
||||
IPAddr ip_addr;
|
||||
char *src, *opt;
|
||||
|
||||
src = line;
|
||||
line = CPS_SplitWord(line);
|
||||
ref_id = 0;
|
||||
|
||||
/* Don't allow hostnames to avoid conflicts with reference IDs */
|
||||
if (!UTI_StringToIdIP(src, &ip_addr) && !UTI_StringToIP(src, &ip_addr)) {
|
||||
ip_addr.family = IPADDR_UNSPEC;
|
||||
if (CPS_ParseRefid(src, &ref_id) == 0) {
|
||||
LOG(LOGS_ERR, "Invalid syntax for selectopts command");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
mask = options = 0;
|
||||
|
||||
while (*line != '\0') {
|
||||
opt = line;
|
||||
line = CPS_SplitWord(line);
|
||||
|
||||
if ((opt[0] != '+' && opt[0] != '-') || (option = CPS_GetSelectOption(opt + 1)) == 0) {
|
||||
LOG(LOGS_ERR, "Invalid syntax for selectopts command");
|
||||
return 0;
|
||||
}
|
||||
|
||||
mask |= option;
|
||||
if (opt[0] == '+')
|
||||
options |= option;
|
||||
}
|
||||
|
||||
UTI_IPHostToNetwork(&ip_addr, &msg->data.modify_select_opts.address);
|
||||
msg->data.modify_select_opts.ref_id = htonl(ref_id);
|
||||
msg->data.modify_select_opts.mask = htonl(mask);
|
||||
msg->data.modify_select_opts.options = htonl(convert_addsrc_sel_options(options));
|
||||
|
||||
msg->command = htons(REQ_MODIFY_SELECTOPTS);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_cmd_waitsync(char *line)
|
||||
{
|
||||
@@ -3064,7 +3001,7 @@ process_cmd_waitsync(char *line)
|
||||
skew_ppm = UTI_FloatNetworkToHost(reply.data.tracking.skew_ppm);
|
||||
|
||||
print_report("try: %d, refid: %R, correction: %.9f, skew: %.3f\n",
|
||||
i, (unsigned long)ref_id, correction, skew_ppm, REPORT_END);
|
||||
i, ref_id, correction, skew_ppm, REPORT_END);
|
||||
|
||||
if ((ip_addr.family != IPADDR_UNSPEC ||
|
||||
(ref_id != 0 && ref_id != 0x7f7f0101L /* LOCAL refid */)) &&
|
||||
@@ -3227,11 +3164,7 @@ process_line(char *line)
|
||||
} else if (!strcmp(command, "add")) {
|
||||
do_normal_submit = process_cmd_add_source(&tx_message, line);
|
||||
} else if (!strcmp(command, "allow")) {
|
||||
if (!strncmp(line, "all", 3)) {
|
||||
do_normal_submit = process_cmd_allowall(&tx_message, CPS_SplitWord(line));
|
||||
} else {
|
||||
do_normal_submit = process_cmd_allow(&tx_message, line);
|
||||
}
|
||||
do_normal_submit = process_cmd_allowdeny(&tx_message, line, REQ_ALLOW, REQ_ALLOWALL);
|
||||
} else if (!strcmp(command, "authdata")) {
|
||||
do_normal_submit = 0;
|
||||
ret = process_cmd_authdata(line);
|
||||
@@ -3243,35 +3176,22 @@ process_line(char *line)
|
||||
} else if (!strcmp(command, "cmdaccheck")) {
|
||||
do_normal_submit = process_cmd_cmdaccheck(&tx_message, line);
|
||||
} else if (!strcmp(command, "cmdallow")) {
|
||||
if (!strncmp(line, "all", 3)) {
|
||||
do_normal_submit = process_cmd_cmdallowall(&tx_message, CPS_SplitWord(line));
|
||||
} else {
|
||||
do_normal_submit = process_cmd_cmdallow(&tx_message, line);
|
||||
}
|
||||
do_normal_submit = process_cmd_allowdeny(&tx_message, line, REQ_CMDALLOW, REQ_CMDALLOWALL);
|
||||
} else if (!strcmp(command, "cmddeny")) {
|
||||
if (!strncmp(line, "all", 3)) {
|
||||
line = CPS_SplitWord(line);
|
||||
do_normal_submit = process_cmd_cmddenyall(&tx_message, line);
|
||||
} else {
|
||||
do_normal_submit = process_cmd_cmddeny(&tx_message, line);
|
||||
}
|
||||
do_normal_submit = process_cmd_allowdeny(&tx_message, line, REQ_CMDDENY, REQ_CMDDENYALL);
|
||||
} else if (!strcmp(command, "cyclelogs")) {
|
||||
process_cmd_cyclelogs(&tx_message, line);
|
||||
} else if (!strcmp(command, "delete")) {
|
||||
do_normal_submit = process_cmd_delete(&tx_message, line);
|
||||
} else if (!strcmp(command, "deny")) {
|
||||
if (!strncmp(line, "all", 3)) {
|
||||
do_normal_submit = process_cmd_denyall(&tx_message, CPS_SplitWord(line));
|
||||
} else {
|
||||
do_normal_submit = process_cmd_deny(&tx_message, line);
|
||||
}
|
||||
do_normal_submit = process_cmd_allowdeny(&tx_message, line, REQ_DENY, REQ_DENYALL);
|
||||
} else if (!strcmp(command, "dfreq")) {
|
||||
process_cmd_dfreq(&tx_message, line);
|
||||
do_normal_submit = process_cmd_dfreq(&tx_message, line);
|
||||
} else if (!strcmp(command, "dns")) {
|
||||
ret = process_cmd_dns(line);
|
||||
do_normal_submit = 0;
|
||||
} else if (!strcmp(command, "doffset")) {
|
||||
process_cmd_doffset(&tx_message, line);
|
||||
do_normal_submit = process_cmd_doffset(&tx_message, line);
|
||||
} else if (!strcmp(command, "dump")) {
|
||||
process_cmd_dump(&tx_message, line);
|
||||
} else if (!strcmp(command, "exit")) {
|
||||
@@ -3348,6 +3268,8 @@ process_line(char *line)
|
||||
} else if (!strcmp(command, "selectdata")) {
|
||||
do_normal_submit = 0;
|
||||
ret = process_cmd_selectdata(line);
|
||||
} else if (!strcmp(command, "selectopts")) {
|
||||
do_normal_submit = process_cmd_selectopts(&tx_message, line);
|
||||
} else if (!strcmp(command, "serverstats")) {
|
||||
do_normal_submit = 0;
|
||||
ret = process_cmd_serverstats(line);
|
||||
@@ -3397,44 +3319,50 @@ process_line(char *line)
|
||||
if (do_normal_submit) {
|
||||
ret = request_reply(&tx_message, &rx_message, RPY_NULL, 1);
|
||||
}
|
||||
|
||||
if (end_dot) {
|
||||
printf(".\n");
|
||||
}
|
||||
|
||||
fflush(stderr);
|
||||
fflush(stdout);
|
||||
|
||||
if (fflush(stdout) != 0 || ferror(stdout) != 0) {
|
||||
LOG(LOGS_ERR, "Could not write to stdout");
|
||||
|
||||
/* Return error for commands that print data */
|
||||
if (!do_normal_submit)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define MAX_LINE_LENGTH 2048
|
||||
|
||||
static int
|
||||
process_args(int argc, char **argv, int multi)
|
||||
{
|
||||
int total_length, i, ret = 0;
|
||||
char *line;
|
||||
char line[MAX_LINE_LENGTH];
|
||||
int i, l, ret = 0;
|
||||
|
||||
total_length = 0;
|
||||
for(i=0; i<argc; i++) {
|
||||
total_length += strlen(argv[i]) + 1;
|
||||
}
|
||||
|
||||
line = (char *) Malloc((2 + total_length) * sizeof(char));
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
line[0] = '\0';
|
||||
if (multi) {
|
||||
strcat(line, argv[i]);
|
||||
} else {
|
||||
for (; i < argc; i++) {
|
||||
strcat(line, argv[i]);
|
||||
if (i + 1 < argc)
|
||||
strcat(line, " ");
|
||||
}
|
||||
for (i = l = 0; i < argc; i++) {
|
||||
l += snprintf(line + l, sizeof (line) - l, "%s ", argv[i]);
|
||||
if (l >= sizeof (line)) {
|
||||
LOG(LOGS_ERR, "Command too long");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!multi && i + 1 < argc)
|
||||
continue;
|
||||
|
||||
ret = process_line(line);
|
||||
if (!ret || quit)
|
||||
break;
|
||||
}
|
||||
|
||||
Free(line);
|
||||
l = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -3453,7 +3381,7 @@ static void
|
||||
display_gpl(void)
|
||||
{
|
||||
printf("chrony version %s\n"
|
||||
"Copyright (C) 1997-2003, 2007, 2009-2020 Richard P. Curnow and others\n"
|
||||
"Copyright (C) 1997-2003, 2007, 2009-2022 Richard P. Curnow and others\n"
|
||||
"chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and\n"
|
||||
"you are welcome to redistribute it under certain conditions. See the\n"
|
||||
"GNU General Public License version 2 for details.\n\n",
|
||||
@@ -3472,6 +3400,7 @@ print_help(const char *progname)
|
||||
" -n\t\tDon't resolve hostnames\n"
|
||||
" -N\t\tPrint original source names\n"
|
||||
" -c\t\tEnable CSV format\n"
|
||||
" -e\t\tEnd responses with dot\n"
|
||||
#if DEBUG > 0
|
||||
" -d\t\tEnable debug messages\n"
|
||||
#endif
|
||||
@@ -3516,7 +3445,7 @@ main(int argc, char **argv)
|
||||
optind = 1;
|
||||
|
||||
/* Parse short command-line options */
|
||||
while ((opt = getopt(argc, argv, "+46acdf:h:mnNp:v")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "+46acdef:h:mnNp:v")) != -1) {
|
||||
switch (opt) {
|
||||
case '4':
|
||||
case '6':
|
||||
@@ -3534,6 +3463,9 @@ main(int argc, char **argv)
|
||||
log_min_severity = LOGS_DEBUG;
|
||||
#endif
|
||||
break;
|
||||
case 'e':
|
||||
end_dot = 1;
|
||||
break;
|
||||
case 'h':
|
||||
hostnames = optarg;
|
||||
break;
|
||||
|
||||
443
clientlog.c
443
clientlog.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2009, 2015-2017
|
||||
* Copyright (C) Miroslav Lichvar 2009, 2015-2017, 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "array.h"
|
||||
#include "clientlog.h"
|
||||
#include "conf.h"
|
||||
#include "local.h"
|
||||
#include "memory.h"
|
||||
#include "ntp.h"
|
||||
#include "reports.h"
|
||||
@@ -55,8 +56,6 @@ typedef struct {
|
||||
int8_t rate[MAX_SERVICES];
|
||||
int8_t ntp_timeout_rate;
|
||||
uint8_t drop_flags;
|
||||
NTP_int64 ntp_rx_ts;
|
||||
NTP_int64 ntp_tx_ts;
|
||||
} Record;
|
||||
|
||||
/* Hash table of records, there is a fixed number of records per slot */
|
||||
@@ -124,17 +123,58 @@ static int limit_interval[MAX_SERVICES];
|
||||
/* Flag indicating whether facility is turned on or not */
|
||||
static int active;
|
||||
|
||||
/* RX and TX timestamp saved for clients using interleaved mode */
|
||||
typedef struct {
|
||||
uint64_t rx_ts;
|
||||
uint8_t flags;
|
||||
uint8_t tx_ts_source;
|
||||
uint16_t slew_epoch;
|
||||
int32_t tx_ts_offset;
|
||||
} NtpTimestamps;
|
||||
|
||||
/* Flags for NTP timestamps */
|
||||
#define NTPTS_DISABLED 1
|
||||
#define NTPTS_VALID_TX 2
|
||||
|
||||
/* RX->TX map using a circular buffer with ordered timestamps */
|
||||
typedef struct {
|
||||
ARR_Instance timestamps;
|
||||
uint32_t first;
|
||||
uint32_t size;
|
||||
uint32_t max_size;
|
||||
uint32_t cached_index;
|
||||
uint64_t cached_rx_ts;
|
||||
uint16_t slew_epoch;
|
||||
double slew_offset;
|
||||
} NtpTimestampMap;
|
||||
|
||||
static NtpTimestampMap ntp_ts_map;
|
||||
|
||||
/* Maximum interval of NTP timestamps in future after a backward step */
|
||||
#define NTPTS_FUTURE_LIMIT (1LL << 32) /* 1 second */
|
||||
|
||||
/* Maximum number of timestamps moved in the array to insert a new timestamp */
|
||||
#define NTPTS_INSERT_LIMIT 64
|
||||
|
||||
/* Maximum expected value of the timestamp source */
|
||||
#define MAX_NTP_TS NTP_TS_HARDWARE
|
||||
|
||||
/* Global statistics */
|
||||
static uint32_t total_hits[MAX_SERVICES];
|
||||
static uint32_t total_drops[MAX_SERVICES];
|
||||
static uint32_t total_ntp_auth_hits;
|
||||
static uint32_t total_record_drops;
|
||||
static uint64_t total_hits[MAX_SERVICES];
|
||||
static uint64_t total_drops[MAX_SERVICES];
|
||||
static uint64_t total_ntp_auth_hits;
|
||||
static uint64_t total_ntp_interleaved_hits;
|
||||
static uint64_t total_record_drops;
|
||||
static uint64_t total_ntp_rx_timestamps[MAX_NTP_TS + 1];
|
||||
static uint64_t total_ntp_tx_timestamps[MAX_NTP_TS + 1];
|
||||
|
||||
#define NSEC_PER_SEC 1000000000U
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int expand_hashtable(void);
|
||||
static void handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
double doffset, LCL_ChangeType change_type, void *anything);
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -229,8 +269,6 @@ get_record(IPAddr *ip)
|
||||
record->rate[i] = INVALID_RATE;
|
||||
record->ntp_timeout_rate = INVALID_RATE;
|
||||
record->drop_flags = 0;
|
||||
UTI_ZeroNtp64(&record->ntp_rx_ts);
|
||||
UTI_ZeroNtp64(&record->ntp_tx_ts);
|
||||
|
||||
return record;
|
||||
}
|
||||
@@ -316,7 +354,7 @@ set_bucket_params(int interval, int burst, uint16_t *max_tokens,
|
||||
void
|
||||
CLG_Initialise(void)
|
||||
{
|
||||
int i, interval, burst, lrate;
|
||||
int i, interval, burst, lrate, slots2;
|
||||
|
||||
for (i = 0; i < MAX_SERVICES; i++) {
|
||||
max_tokens[i] = 0;
|
||||
@@ -359,9 +397,13 @@ CLG_Initialise(void)
|
||||
/* Calculate the maximum number of slots that can be allocated in the
|
||||
configured memory limit. Take into account expanding of the hash
|
||||
table where two copies exist at the same time. */
|
||||
max_slots = CNF_GetClientLogLimit() / (sizeof (Record) * SLOT_SIZE * 3 / 2);
|
||||
max_slots = CNF_GetClientLogLimit() /
|
||||
((sizeof (Record) + sizeof (NtpTimestamps)) * SLOT_SIZE * 3 / 2);
|
||||
max_slots = CLAMP(MIN_SLOTS, max_slots, MAX_SLOTS);
|
||||
DEBUG_LOG("Max records %u", 1U << ((int)round(log(max_slots) / log(2)) + SLOT_BITS));
|
||||
for (slots2 = 0; 1U << (slots2 + 1) <= max_slots; slots2++)
|
||||
;
|
||||
|
||||
DEBUG_LOG("Max records %u", 1U << (slots2 + SLOT_BITS));
|
||||
|
||||
slots = 0;
|
||||
records = NULL;
|
||||
@@ -370,6 +412,17 @@ CLG_Initialise(void)
|
||||
|
||||
UTI_GetRandomBytes(&ts_offset, sizeof (ts_offset));
|
||||
ts_offset %= NSEC_PER_SEC / (1U << TS_FRAC);
|
||||
|
||||
ntp_ts_map.timestamps = NULL;
|
||||
ntp_ts_map.first = 0;
|
||||
ntp_ts_map.size = 0;
|
||||
ntp_ts_map.max_size = 1U << (slots2 + SLOT_BITS);
|
||||
ntp_ts_map.cached_index = 0;
|
||||
ntp_ts_map.cached_rx_ts = 0ULL;
|
||||
ntp_ts_map.slew_epoch = 0;
|
||||
ntp_ts_map.slew_offset = 0.0;
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_slew, NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -381,6 +434,10 @@ CLG_Finalise(void)
|
||||
return;
|
||||
|
||||
ARR_DestroyInstance(records);
|
||||
if (ntp_ts_map.timestamps)
|
||||
ARR_DestroyInstance(ntp_ts_map.timestamps);
|
||||
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -588,21 +645,14 @@ CLG_LimitServiceRate(CLG_Service service, int index)
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CLG_LogAuthNtpRequest(void)
|
||||
CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src, NTP_Timestamp_Source tx_ts_src)
|
||||
{
|
||||
total_ntp_auth_hits++;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts)
|
||||
{
|
||||
Record *record;
|
||||
|
||||
record = ARR_GetElement(records, index);
|
||||
|
||||
*rx_ts = &record->ntp_rx_ts;
|
||||
*tx_ts = &record->ntp_tx_ts;
|
||||
if (auth)
|
||||
total_ntp_auth_hits++;
|
||||
if (rx_ts_src >= 0 && rx_ts_src <= MAX_NTP_TS)
|
||||
total_ntp_rx_timestamps[rx_ts_src]++;
|
||||
if (tx_ts_src >= 0 && tx_ts_src <= MAX_NTP_TS)
|
||||
total_ntp_tx_timestamps[tx_ts_src]++;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -615,6 +665,336 @@ CLG_GetNtpMinPoll(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static NtpTimestamps *
|
||||
get_ntp_tss(uint32_t index)
|
||||
{
|
||||
return ARR_GetElement(ntp_ts_map.timestamps,
|
||||
(ntp_ts_map.first + index) & (ntp_ts_map.max_size - 1));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
find_ntp_rx_ts(uint64_t rx_ts, uint32_t *index)
|
||||
{
|
||||
uint64_t rx_x, rx_lo, rx_hi, step;
|
||||
uint32_t i, x, lo, hi;
|
||||
|
||||
if (ntp_ts_map.cached_rx_ts == rx_ts && rx_ts != 0ULL) {
|
||||
*index = ntp_ts_map.cached_index;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ntp_ts_map.size == 0) {
|
||||
*index = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
lo = 0;
|
||||
hi = ntp_ts_map.size - 1;
|
||||
rx_lo = get_ntp_tss(lo)->rx_ts;
|
||||
rx_hi = get_ntp_tss(hi)->rx_ts;
|
||||
|
||||
/* Check for ts < lo before ts > hi to trim timestamps from "future" later
|
||||
if both conditions are true to not break the order of the endpoints.
|
||||
Compare timestamps by their difference to allow adjacent NTP eras. */
|
||||
if ((int64_t)(rx_ts - rx_lo) < 0) {
|
||||
*index = 0;
|
||||
return 0;
|
||||
} else if ((int64_t)(rx_ts - rx_hi) > 0) {
|
||||
*index = ntp_ts_map.size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Perform a combined linear interpolation and binary search */
|
||||
|
||||
for (i = 0; ; i++) {
|
||||
if (rx_ts == rx_hi) {
|
||||
*index = ntp_ts_map.cached_index = hi;
|
||||
ntp_ts_map.cached_rx_ts = rx_ts;
|
||||
return 1;
|
||||
} else if (rx_ts == rx_lo) {
|
||||
*index = ntp_ts_map.cached_index = lo;
|
||||
ntp_ts_map.cached_rx_ts = rx_ts;
|
||||
return 1;
|
||||
} else if (lo + 1 == hi) {
|
||||
*index = hi;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (hi - lo > 3 && i % 2 == 0) {
|
||||
step = (rx_hi - rx_lo) / (hi - lo);
|
||||
if (step == 0)
|
||||
step = 1;
|
||||
x = lo + (rx_ts - rx_lo) / step;
|
||||
} else {
|
||||
x = lo + (hi - lo) / 2;
|
||||
}
|
||||
|
||||
if (x <= lo)
|
||||
x = lo + 1;
|
||||
else if (x >= hi)
|
||||
x = hi - 1;
|
||||
|
||||
rx_x = get_ntp_tss(x)->rx_ts;
|
||||
|
||||
if ((int64_t)(rx_x - rx_ts) <= 0) {
|
||||
lo = x;
|
||||
rx_lo = rx_x;
|
||||
} else {
|
||||
hi = x;
|
||||
rx_hi = rx_x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static uint64_t
|
||||
ntp64_to_int64(NTP_int64 *ts)
|
||||
{
|
||||
return (uint64_t)ntohl(ts->hi) << 32 | ntohl(ts->lo);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
int64_to_ntp64(uint64_t ts, NTP_int64 *ntp_ts)
|
||||
{
|
||||
ntp_ts->hi = htonl(ts >> 32);
|
||||
ntp_ts->lo = htonl(ts);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static uint32_t
|
||||
push_ntp_tss(uint32_t index)
|
||||
{
|
||||
if (ntp_ts_map.size < ntp_ts_map.max_size) {
|
||||
ntp_ts_map.size++;
|
||||
} else {
|
||||
ntp_ts_map.first = (ntp_ts_map.first + 1) % (ntp_ts_map.max_size);
|
||||
if (index > 0)
|
||||
index--;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
set_ntp_tx(NtpTimestamps *tss, NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source tx_src)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
if (!tx_ts) {
|
||||
tss->flags &= ~NTPTS_VALID_TX;
|
||||
return;
|
||||
}
|
||||
|
||||
UTI_Ntp64ToTimespec(rx_ts, &ts);
|
||||
UTI_DiffTimespecs(&ts, tx_ts, &ts);
|
||||
|
||||
if (ts.tv_sec < -2 || ts.tv_sec > 1) {
|
||||
tss->flags &= ~NTPTS_VALID_TX;
|
||||
return;
|
||||
}
|
||||
|
||||
tss->tx_ts_offset = (int32_t)ts.tv_nsec + (int32_t)ts.tv_sec * (int32_t)NSEC_PER_SEC;
|
||||
tss->flags |= NTPTS_VALID_TX;
|
||||
tss->tx_ts_source = tx_src;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
get_ntp_tx(NtpTimestamps *tss, struct timespec *tx_ts, NTP_Timestamp_Source *tx_src)
|
||||
{
|
||||
int32_t offset = tss->tx_ts_offset;
|
||||
NTP_int64 ntp_ts;
|
||||
|
||||
if (tss->flags & NTPTS_VALID_TX) {
|
||||
int64_to_ntp64(tss->rx_ts, &ntp_ts);
|
||||
UTI_Ntp64ToTimespec(&ntp_ts, tx_ts);
|
||||
if (offset >= (int32_t)NSEC_PER_SEC) {
|
||||
offset -= NSEC_PER_SEC;
|
||||
tx_ts->tv_sec++;
|
||||
}
|
||||
tx_ts->tv_nsec += offset;
|
||||
UTI_NormaliseTimespec(tx_ts);
|
||||
} else {
|
||||
UTI_ZeroTimespec(tx_ts);
|
||||
}
|
||||
|
||||
*tx_src = tss->tx_ts_source;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CLG_SaveNtpTimestamps(NTP_int64 *rx_ts, struct timespec *tx_ts, NTP_Timestamp_Source tx_src)
|
||||
{
|
||||
NtpTimestamps *tss;
|
||||
uint32_t i, index;
|
||||
uint64_t rx;
|
||||
|
||||
if (!active)
|
||||
return;
|
||||
|
||||
/* Allocate the array on first use */
|
||||
if (!ntp_ts_map.timestamps) {
|
||||
ntp_ts_map.timestamps = ARR_CreateInstance(sizeof (NtpTimestamps));
|
||||
ARR_SetSize(ntp_ts_map.timestamps, ntp_ts_map.max_size);
|
||||
}
|
||||
|
||||
rx = ntp64_to_int64(rx_ts);
|
||||
|
||||
if (rx == 0ULL)
|
||||
return;
|
||||
|
||||
/* Disable the RX timestamp if it already exists to avoid responding
|
||||
with a wrong TX timestamp */
|
||||
if (find_ntp_rx_ts(rx, &index)) {
|
||||
get_ntp_tss(index)->flags |= NTPTS_DISABLED;
|
||||
return;
|
||||
}
|
||||
|
||||
assert(index <= ntp_ts_map.size);
|
||||
|
||||
if (index == ntp_ts_map.size) {
|
||||
/* Increase the size or drop the oldest timestamp to make room for
|
||||
the new timestamp */
|
||||
index = push_ntp_tss(index);
|
||||
} else {
|
||||
/* Trim timestamps in distant future after backward step */
|
||||
while (index < ntp_ts_map.size &&
|
||||
get_ntp_tss(ntp_ts_map.size - 1)->rx_ts - rx > NTPTS_FUTURE_LIMIT)
|
||||
ntp_ts_map.size--;
|
||||
|
||||
/* Insert the timestamp if it is close to the latest timestamp.
|
||||
Otherwise, replace the closest older or the oldest timestamp. */
|
||||
if (index + NTPTS_INSERT_LIMIT >= ntp_ts_map.size) {
|
||||
index = push_ntp_tss(index);
|
||||
for (i = ntp_ts_map.size - 1; i > index; i--)
|
||||
*get_ntp_tss(i) = *get_ntp_tss(i - 1);
|
||||
} else {
|
||||
if (index > 0)
|
||||
index--;
|
||||
}
|
||||
}
|
||||
|
||||
ntp_ts_map.cached_index = index;
|
||||
ntp_ts_map.cached_rx_ts = rx;
|
||||
|
||||
tss = get_ntp_tss(index);
|
||||
tss->rx_ts = rx;
|
||||
tss->flags = 0;
|
||||
tss->slew_epoch = ntp_ts_map.slew_epoch;
|
||||
set_ntp_tx(tss, rx_ts, tx_ts, tx_src);
|
||||
|
||||
DEBUG_LOG("Saved RX+TX index=%"PRIu32" first=%"PRIu32" size=%"PRIu32,
|
||||
index, ntp_ts_map.first, ntp_ts_map.size);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
double doffset, LCL_ChangeType change_type, void *anything)
|
||||
{
|
||||
/* Drop all timestamps on unknown step */
|
||||
if (change_type == LCL_ChangeUnknownStep) {
|
||||
ntp_ts_map.size = 0;
|
||||
ntp_ts_map.cached_rx_ts = 0ULL;
|
||||
}
|
||||
|
||||
ntp_ts_map.slew_epoch++;
|
||||
ntp_ts_map.slew_offset = doffset;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CLG_UndoNtpTxTimestampSlew(NTP_int64 *rx_ts, struct timespec *tx_ts)
|
||||
{
|
||||
uint32_t index;
|
||||
|
||||
if (!ntp_ts_map.timestamps)
|
||||
return;
|
||||
|
||||
if (!find_ntp_rx_ts(ntp64_to_int64(rx_ts), &index))
|
||||
return;
|
||||
|
||||
/* If the RX timestamp was captured before the last correction of the clock,
|
||||
remove the adjustment from the TX timestamp */
|
||||
if ((uint16_t)(get_ntp_tss(index)->slew_epoch + 1U) == ntp_ts_map.slew_epoch)
|
||||
UTI_AddDoubleToTimespec(tx_ts, ntp_ts_map.slew_offset, tx_ts);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CLG_UpdateNtpTxTimestamp(NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source tx_src)
|
||||
{
|
||||
uint32_t index;
|
||||
|
||||
if (!ntp_ts_map.timestamps)
|
||||
return;
|
||||
|
||||
if (!find_ntp_rx_ts(ntp64_to_int64(rx_ts), &index))
|
||||
return;
|
||||
|
||||
set_ntp_tx(get_ntp_tss(index), rx_ts, tx_ts, tx_src);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_GetNtpTxTimestamp(NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source *tx_src)
|
||||
{
|
||||
NtpTimestamps *tss;
|
||||
uint32_t index;
|
||||
|
||||
if (!ntp_ts_map.timestamps)
|
||||
return 0;
|
||||
|
||||
if (!find_ntp_rx_ts(ntp64_to_int64(rx_ts), &index))
|
||||
return 0;
|
||||
|
||||
tss = get_ntp_tss(index);
|
||||
|
||||
if (tss->flags & NTPTS_DISABLED)
|
||||
return 0;
|
||||
|
||||
get_ntp_tx(tss, tx_ts, tx_src);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CLG_DisableNtpTimestamps(NTP_int64 *rx_ts)
|
||||
{
|
||||
uint32_t index;
|
||||
|
||||
if (!ntp_ts_map.timestamps)
|
||||
return;
|
||||
|
||||
if (find_ntp_rx_ts(ntp64_to_int64(rx_ts), &index))
|
||||
get_ntp_tss(index)->flags |= NTPTS_DISABLED;
|
||||
|
||||
/* This assumes the function is called only to prevent multiple
|
||||
interleaved responses to the same timestamp */
|
||||
total_ntp_interleaved_hits++;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_GetNumberOfIndices(void)
|
||||
{
|
||||
@@ -717,4 +1097,15 @@ CLG_GetServerStatsReport(RPT_ServerStatsReport *report)
|
||||
report->cmd_drops = total_drops[CLG_CMDMON];
|
||||
report->log_drops = total_record_drops;
|
||||
report->ntp_auth_hits = total_ntp_auth_hits;
|
||||
report->ntp_interleaved_hits = total_ntp_interleaved_hits;
|
||||
report->ntp_timestamps = ntp_ts_map.size;
|
||||
report->ntp_span_seconds = ntp_ts_map.size > 1 ?
|
||||
(get_ntp_tss(ntp_ts_map.size - 1)->rx_ts -
|
||||
get_ntp_tss(0)->rx_ts) >> 32 : 0;
|
||||
report->ntp_daemon_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_DAEMON];
|
||||
report->ntp_daemon_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_DAEMON];
|
||||
report->ntp_kernel_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_KERNEL];
|
||||
report->ntp_kernel_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_KERNEL];
|
||||
report->ntp_hw_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_HARDWARE];
|
||||
report->ntp_hw_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_HARDWARE];
|
||||
}
|
||||
|
||||
14
clientlog.h
14
clientlog.h
@@ -42,10 +42,20 @@ extern void CLG_Finalise(void);
|
||||
extern int CLG_GetClientIndex(IPAddr *client);
|
||||
extern int CLG_LogServiceAccess(CLG_Service service, IPAddr *client, struct timespec *now);
|
||||
extern int CLG_LimitServiceRate(CLG_Service service, int index);
|
||||
extern void CLG_LogAuthNtpRequest(void);
|
||||
extern void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts);
|
||||
extern void CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src,
|
||||
NTP_Timestamp_Source tx_ts_src);
|
||||
extern int CLG_GetNtpMinPoll(void);
|
||||
|
||||
/* Functions to save and retrieve timestamps for server interleaved mode */
|
||||
extern void CLG_SaveNtpTimestamps(NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source tx_src);
|
||||
extern void CLG_UndoNtpTxTimestampSlew(NTP_int64 *rx_ts, struct timespec *tx_ts);
|
||||
extern void CLG_UpdateNtpTxTimestamp(NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source tx_src);
|
||||
extern int CLG_GetNtpTxTimestamp(NTP_int64 *rx_ts, struct timespec *tx_ts,
|
||||
NTP_Timestamp_Source *tx_src);
|
||||
extern void CLG_DisableNtpTimestamps(NTP_int64 *rx_ts);
|
||||
|
||||
/* And some reporting functions, for use by chronyc. */
|
||||
|
||||
extern int CLG_GetNumberOfIndices(void);
|
||||
|
||||
189
cmac_gnutls.c
Normal file
189
cmac_gnutls.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
CMAC using the GnuTLS library
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <gnutls/crypto.h>
|
||||
|
||||
#include "cmac.h"
|
||||
#include "hash.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
|
||||
struct CMC_Instance_Record {
|
||||
gnutls_mac_algorithm_t algorithm;
|
||||
gnutls_hmac_hd_t mac;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int instance_counter = 0;
|
||||
static int gnutls_initialised = 0;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
init_gnutls(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
if (gnutls_initialised)
|
||||
return;
|
||||
|
||||
r = gnutls_global_init();
|
||||
if (r < 0)
|
||||
LOG_FATAL("Could not initialise %s : %s", "gnutls", gnutls_strerror(r));
|
||||
|
||||
DEBUG_LOG("Initialised");
|
||||
gnutls_initialised = 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
deinit_gnutls(void)
|
||||
{
|
||||
assert(gnutls_initialised);
|
||||
gnutls_global_deinit();
|
||||
gnutls_initialised = 0;
|
||||
DEBUG_LOG("Deinitialised");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static gnutls_mac_algorithm_t
|
||||
get_mac_algorithm(CMC_Algorithm algorithm)
|
||||
{
|
||||
switch (algorithm) {
|
||||
case CMC_AES128:
|
||||
return GNUTLS_MAC_AES_CMAC_128;
|
||||
case CMC_AES256:
|
||||
return GNUTLS_MAC_AES_CMAC_256;
|
||||
default:
|
||||
return GNUTLS_MAC_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CMC_GetKeyLength(CMC_Algorithm algorithm)
|
||||
{
|
||||
gnutls_mac_algorithm_t malgo = get_mac_algorithm(algorithm);
|
||||
int len;
|
||||
|
||||
if (malgo == GNUTLS_MAC_UNKNOWN)
|
||||
return 0;
|
||||
|
||||
len = gnutls_hmac_get_key_size(malgo);
|
||||
|
||||
if (len < 0)
|
||||
return 0;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
CMC_Instance
|
||||
CMC_CreateInstance(CMC_Algorithm algorithm, const unsigned char *key, int length)
|
||||
{
|
||||
gnutls_hmac_hd_t handle;
|
||||
CMC_Instance inst;
|
||||
|
||||
int r;
|
||||
|
||||
if (instance_counter == 0)
|
||||
init_gnutls();
|
||||
|
||||
if (length <= 0 || length != CMC_GetKeyLength(algorithm))
|
||||
goto error;
|
||||
|
||||
r = gnutls_hmac_init(&handle, get_mac_algorithm(algorithm), key, length);
|
||||
if (r < 0) {
|
||||
DEBUG_LOG("Could not initialise %s : %s", "mac", gnutls_strerror(r));
|
||||
goto error;
|
||||
}
|
||||
|
||||
inst = MallocNew(struct CMC_Instance_Record);
|
||||
inst->algorithm = get_mac_algorithm(algorithm);
|
||||
inst->mac = handle;
|
||||
|
||||
instance_counter++;
|
||||
|
||||
return inst;
|
||||
|
||||
error:
|
||||
if (instance_counter == 0)
|
||||
deinit_gnutls();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CMC_Hash(CMC_Instance inst, const void *in, int in_len, unsigned char *out, int out_len)
|
||||
{
|
||||
unsigned char buf[MAX_HASH_LENGTH];
|
||||
int hash_len;
|
||||
|
||||
if (in_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
hash_len = gnutls_hmac_get_len(inst->algorithm);
|
||||
|
||||
if (out_len > hash_len)
|
||||
out_len = hash_len;
|
||||
|
||||
if (hash_len > sizeof (buf))
|
||||
return 0;
|
||||
|
||||
if (gnutls_hmac(inst->mac, in, in_len) < 0) {
|
||||
/* Reset the state */
|
||||
gnutls_hmac_output(inst->mac, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
gnutls_hmac_output(inst->mac, buf);
|
||||
memcpy(out, buf, out_len);
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CMC_DestroyInstance(CMC_Instance inst)
|
||||
{
|
||||
gnutls_hmac_deinit(inst->mac, NULL);
|
||||
Free(inst);
|
||||
|
||||
instance_counter--;
|
||||
if (instance_counter == 0)
|
||||
deinit_gnutls();
|
||||
}
|
||||
132
cmdmon.c
132
cmdmon.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2009-2016, 2018-2020
|
||||
* Copyright (C) Miroslav Lichvar 2009-2016, 2018-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -62,6 +62,9 @@ static int sock_fdu;
|
||||
static int sock_fd4;
|
||||
static int sock_fd6;
|
||||
|
||||
/* Flag indicating the IPv4 socket is bound to an address */
|
||||
static int bound_sock_fd4;
|
||||
|
||||
/* Flag indicating whether this module has been initialised or not */
|
||||
static int initialised = 0;
|
||||
|
||||
@@ -140,6 +143,8 @@ static const char permissions[] = {
|
||||
PERMIT_AUTH, /* CLIENT_ACCESSES_BY_INDEX3 */
|
||||
PERMIT_AUTH, /* SELECT_DATA */
|
||||
PERMIT_AUTH, /* RELOAD_SOURCES */
|
||||
PERMIT_AUTH, /* DOFFSET2 */
|
||||
PERMIT_AUTH, /* MODIFY_SELECTOPTS */
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -179,6 +184,9 @@ open_socket(int family)
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
if (family == IPADDR_INET4)
|
||||
bound_sock_fd4 = local_addr.ip_addr.addr.in4 != INADDR_ANY;
|
||||
|
||||
break;
|
||||
case IPADDR_UNSPEC:
|
||||
local_path = CNF_GetBindCommandPath();
|
||||
@@ -244,6 +252,8 @@ CAM_Initialise(void)
|
||||
|
||||
initialised = 1;
|
||||
|
||||
bound_sock_fd4 = 0;
|
||||
|
||||
sock_fdu = INVALID_SOCK_FD;
|
||||
sock_fd4 = open_socket(IPADDR_INET4);
|
||||
sock_fd6 = open_socket(IPADDR_INET6);
|
||||
@@ -310,6 +320,13 @@ transmit_reply(int sock_fd, int request_length, SCK_Message *message)
|
||||
!SCK_IsLinkLocalIPAddress(&message->remote_addr.ip.ip_addr))
|
||||
message->if_index = INVALID_IF_INDEX;
|
||||
|
||||
#if !defined(HAVE_IN_PKTINFO) && defined(IP_SENDSRCADDR)
|
||||
/* On FreeBSD a local IPv4 address cannot be specified on bound socket */
|
||||
if (message->addr_type == SCK_ADDR_IP && message->local_addr.ip.family == IPADDR_INET4 &&
|
||||
(sock_fd != sock_fd4 || bound_sock_fd4))
|
||||
message->local_addr.ip.family = IPADDR_UNSPEC;
|
||||
#endif
|
||||
|
||||
if (!SCK_SendMessage(sock_fd, message, 0))
|
||||
return;
|
||||
}
|
||||
@@ -687,6 +704,17 @@ handle_cmdaccheck(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
convert_addsrc_select_options(int flags)
|
||||
{
|
||||
return (flags & REQ_ADDSRC_PREFER ? SRC_SELECT_PREFER : 0) |
|
||||
(flags & REQ_ADDSRC_NOSELECT ? SRC_SELECT_NOSELECT : 0) |
|
||||
(flags & REQ_ADDSRC_TRUST ? SRC_SELECT_TRUST : 0) |
|
||||
(flags & REQ_ADDSRC_REQUIRE ? SRC_SELECT_REQUIRE : 0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
{
|
||||
@@ -735,11 +763,14 @@ handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
params.filter_length = ntohl(rx_message->data.ntp_source.filter_length);
|
||||
params.authkey = ntohl(rx_message->data.ntp_source.authkey);
|
||||
params.nts_port = ntohl(rx_message->data.ntp_source.nts_port);
|
||||
params.cert_set = ntohl(rx_message->data.ntp_source.cert_set);
|
||||
params.max_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay);
|
||||
params.max_delay_ratio =
|
||||
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_ratio);
|
||||
params.max_delay_dev_ratio =
|
||||
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_dev_ratio);
|
||||
params.max_delay_quant =
|
||||
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_quant);
|
||||
params.min_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.min_delay);
|
||||
params.asymmetry = UTI_FloatNetworkToHost(rx_message->data.ntp_source.asymmetry);
|
||||
params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset);
|
||||
@@ -751,11 +782,10 @@ handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0;
|
||||
params.burst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_BURST ? 1 : 0;
|
||||
params.nts = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NTS ? 1 : 0;
|
||||
params.sel_options =
|
||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_PREFER ? SRC_SELECT_PREFER : 0) |
|
||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NOSELECT ? SRC_SELECT_NOSELECT : 0) |
|
||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_TRUST ? SRC_SELECT_TRUST : 0) |
|
||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_REQUIRE ? SRC_SELECT_REQUIRE : 0);
|
||||
params.copy = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_COPY ? 1 : 0;
|
||||
params.ext_fields =
|
||||
ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_EF_EXP1 ? NTP_EF_FLAG_EXP1 : 0;
|
||||
params.sel_options = convert_addsrc_select_options(ntohl(rx_message->data.ntp_source.flags));
|
||||
|
||||
status = NSR_AddSourceByName(name, port, pool, type, ¶ms, NULL);
|
||||
switch (status) {
|
||||
@@ -841,13 +871,14 @@ handle_dfreq(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
static void
|
||||
handle_doffset(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
{
|
||||
long sec, usec;
|
||||
double doffset;
|
||||
sec = (int32_t)ntohl(rx_message->data.doffset.sec);
|
||||
usec = (int32_t)ntohl(rx_message->data.doffset.usec);
|
||||
doffset = (double) sec + 1.0e-6 * (double) usec;
|
||||
LOG(LOGS_INFO, "Accumulated delta offset of %.6f seconds", doffset);
|
||||
LCL_AccumulateOffset(doffset, 0.0);
|
||||
|
||||
doffset = UTI_FloatNetworkToHost(rx_message->data.doffset.doffset);
|
||||
if (!LCL_AccumulateOffset(doffset, 0.0)) {
|
||||
tx_message->status = htons(STT_FAILED);
|
||||
} else {
|
||||
LOG(LOGS_INFO, "Accumulated delta offset of %.6f seconds", doffset);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1146,15 +1177,36 @@ handle_server_stats(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
RPT_ServerStatsReport report;
|
||||
|
||||
CLG_GetServerStatsReport(&report);
|
||||
tx_message->reply = htons(RPY_SERVER_STATS2);
|
||||
tx_message->data.server_stats.ntp_hits = htonl(report.ntp_hits);
|
||||
tx_message->data.server_stats.nke_hits = htonl(report.nke_hits);
|
||||
tx_message->data.server_stats.cmd_hits = htonl(report.cmd_hits);
|
||||
tx_message->data.server_stats.ntp_drops = htonl(report.ntp_drops);
|
||||
tx_message->data.server_stats.nke_drops = htonl(report.nke_drops);
|
||||
tx_message->data.server_stats.cmd_drops = htonl(report.cmd_drops);
|
||||
tx_message->data.server_stats.log_drops = htonl(report.log_drops);
|
||||
tx_message->data.server_stats.ntp_auth_hits = htonl(report.ntp_auth_hits);
|
||||
tx_message->reply = htons(RPY_SERVER_STATS4);
|
||||
tx_message->data.server_stats.ntp_hits = UTI_Integer64HostToNetwork(report.ntp_hits);
|
||||
tx_message->data.server_stats.nke_hits = UTI_Integer64HostToNetwork(report.nke_hits);
|
||||
tx_message->data.server_stats.cmd_hits = UTI_Integer64HostToNetwork(report.cmd_hits);
|
||||
tx_message->data.server_stats.ntp_drops = UTI_Integer64HostToNetwork(report.ntp_drops);
|
||||
tx_message->data.server_stats.nke_drops = UTI_Integer64HostToNetwork(report.nke_drops);
|
||||
tx_message->data.server_stats.cmd_drops = UTI_Integer64HostToNetwork(report.cmd_drops);
|
||||
tx_message->data.server_stats.log_drops = UTI_Integer64HostToNetwork(report.log_drops);
|
||||
tx_message->data.server_stats.ntp_auth_hits =
|
||||
UTI_Integer64HostToNetwork(report.ntp_auth_hits);
|
||||
tx_message->data.server_stats.ntp_interleaved_hits =
|
||||
UTI_Integer64HostToNetwork(report.ntp_interleaved_hits);
|
||||
tx_message->data.server_stats.ntp_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_timestamps);
|
||||
tx_message->data.server_stats.ntp_span_seconds =
|
||||
UTI_Integer64HostToNetwork(report.ntp_span_seconds);
|
||||
tx_message->data.server_stats.ntp_daemon_rx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_daemon_rx_timestamps);
|
||||
tx_message->data.server_stats.ntp_daemon_tx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_daemon_tx_timestamps);
|
||||
tx_message->data.server_stats.ntp_kernel_rx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_kernel_rx_timestamps);
|
||||
tx_message->data.server_stats.ntp_kernel_tx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_kernel_tx_timestamps);
|
||||
tx_message->data.server_stats.ntp_hw_rx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_hw_rx_timestamps);
|
||||
tx_message->data.server_stats.ntp_hw_tx_timestamps =
|
||||
UTI_Integer64HostToNetwork(report.ntp_hw_tx_timestamps);
|
||||
memset(tx_message->data.server_stats.reserved, 0xff,
|
||||
sizeof (tx_message->data.server_stats.reserved));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1198,6 +1250,7 @@ handle_ntp_data(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
tx_message->data.ntp_data.total_tx_count = htonl(report.total_tx_count);
|
||||
tx_message->data.ntp_data.total_rx_count = htonl(report.total_rx_count);
|
||||
tx_message->data.ntp_data.total_valid_count = htonl(report.total_valid_count);
|
||||
tx_message->data.ntp_data.total_good_count = htonl(report.total_good_count);
|
||||
memset(tx_message->data.ntp_data.reserved, 0xff, sizeof (tx_message->data.ntp_data.reserved));
|
||||
}
|
||||
|
||||
@@ -1301,7 +1354,7 @@ handle_auth_data(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
/* ================================================== */
|
||||
|
||||
static uint16_t
|
||||
convert_select_options(int options)
|
||||
convert_sd_sel_options(int options)
|
||||
{
|
||||
return (options & SRC_SELECT_PREFER ? RPY_SD_OPTION_PREFER : 0) |
|
||||
(options & SRC_SELECT_NOSELECT ? RPY_SD_OPTION_NOSELECT : 0) |
|
||||
@@ -1328,14 +1381,32 @@ handle_select_data(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
tx_message->data.select_data.state_char = report.state_char;
|
||||
tx_message->data.select_data.authentication = report.authentication;
|
||||
tx_message->data.select_data.leap = report.leap;
|
||||
tx_message->data.select_data.conf_options = htons(convert_select_options(report.conf_options));
|
||||
tx_message->data.select_data.eff_options = htons(convert_select_options(report.eff_options));
|
||||
tx_message->data.select_data.conf_options = htons(convert_sd_sel_options(report.conf_options));
|
||||
tx_message->data.select_data.eff_options = htons(convert_sd_sel_options(report.eff_options));
|
||||
tx_message->data.select_data.last_sample_ago = htonl(report.last_sample_ago);
|
||||
tx_message->data.select_data.score = UTI_FloatHostToNetwork(report.score);
|
||||
tx_message->data.select_data.hi_limit = UTI_FloatHostToNetwork(report.hi_limit);
|
||||
tx_message->data.select_data.lo_limit = UTI_FloatHostToNetwork(report.lo_limit);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_modify_selectopts(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||
{
|
||||
int mask, options;
|
||||
uint32_t ref_id;
|
||||
IPAddr ip_addr;
|
||||
|
||||
UTI_IPNetworkToHost(&rx_message->data.modify_select_opts.address, &ip_addr);
|
||||
ref_id = ntohl(rx_message->data.modify_select_opts.ref_id);
|
||||
mask = ntohl(rx_message->data.modify_select_opts.mask);
|
||||
options = convert_addsrc_select_options(ntohl(rx_message->data.modify_select_opts.options));
|
||||
|
||||
if (!SRC_ModifySelectOptions(&ip_addr, ref_id, options, mask))
|
||||
tx_message->status = htons(STT_NOSUCHSOURCE);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Read a packet and process it */
|
||||
|
||||
@@ -1488,6 +1559,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
||||
}
|
||||
|
||||
if (allowed) {
|
||||
LOG_SetContext(LOGC_Command);
|
||||
|
||||
switch(rx_command) {
|
||||
case REQ_NULL:
|
||||
/* Do nothing */
|
||||
@@ -1622,7 +1695,7 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
||||
handle_dfreq(&rx_message, &tx_message);
|
||||
break;
|
||||
|
||||
case REQ_DOFFSET:
|
||||
case REQ_DOFFSET2:
|
||||
handle_doffset(&rx_message, &tx_message);
|
||||
break;
|
||||
|
||||
@@ -1730,11 +1803,17 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
||||
handle_reload_sources(&rx_message, &tx_message);
|
||||
break;
|
||||
|
||||
case REQ_MODIFY_SELECTOPTS:
|
||||
handle_modify_selectopts(&rx_message, &tx_message);
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_LOG("Unhandled command %d", rx_command);
|
||||
tx_message.status = htons(STT_FAILED);
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_UnsetContext(LOGC_Command);
|
||||
} else {
|
||||
tx_message.status = htons(STT_UNAUTH);
|
||||
}
|
||||
@@ -1768,6 +1847,9 @@ CAM_AddAccessRestriction(IPAddr *ip_addr, int subnet_bits, int allow, int all)
|
||||
if (status == ADF_BADSUBNET) {
|
||||
return 0;
|
||||
} else if (status == ADF_SUCCESS) {
|
||||
LOG(LOG_GetContextSeverity(LOGC_Command), "%s%s %s access from %s",
|
||||
allow ? "Allowed" : "Denied", all ? " all" : "", "command",
|
||||
UTI_IPSubnetToString(ip_addr, subnet_bits));
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
|
||||
149
cmdparse.c
149
cmdparse.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2013-2014, 2016
|
||||
* Copyright (C) Miroslav Lichvar 2013-2014, 2016, 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -43,7 +43,8 @@ int
|
||||
CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
{
|
||||
char *hostname, *cmd;
|
||||
int n;
|
||||
uint32_t ef_type;
|
||||
int n, sel_option;
|
||||
|
||||
src->port = SRC_DEFAULT_PORT;
|
||||
src->params.minpoll = SRC_DEFAULT_MINPOLL;
|
||||
@@ -64,10 +65,14 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
src->params.sel_options = 0;
|
||||
src->params.nts = 0;
|
||||
src->params.nts_port = SRC_DEFAULT_NTSPORT;
|
||||
src->params.copy = 0;
|
||||
src->params.ext_fields = 0;
|
||||
src->params.authkey = INACTIVE_AUTHKEY;
|
||||
src->params.cert_set = SRC_DEFAULT_CERTSET;
|
||||
src->params.max_delay = SRC_DEFAULT_MAXDELAY;
|
||||
src->params.max_delay_ratio = SRC_DEFAULT_MAXDELAYRATIO;
|
||||
src->params.max_delay_dev_ratio = SRC_DEFAULT_MAXDELAYDEVRATIO;
|
||||
src->params.max_delay_quant = 0.0;
|
||||
src->params.min_delay = 0.0;
|
||||
src->params.asymmetry = SRC_DEFAULT_ASYMMETRY;
|
||||
src->params.offset = 0.0;
|
||||
@@ -90,18 +95,15 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
src->params.auto_offline = 1;
|
||||
} else if (!strcasecmp(cmd, "burst")) {
|
||||
src->params.burst = 1;
|
||||
} else if (!strcasecmp(cmd, "copy")) {
|
||||
src->params.copy = 1;
|
||||
} else if (!strcasecmp(cmd, "iburst")) {
|
||||
src->params.iburst = 1;
|
||||
} else if (!strcasecmp(cmd, "offline")) {
|
||||
src->params.connectivity = SRC_OFFLINE;
|
||||
} else if (!strcasecmp(cmd, "noselect")) {
|
||||
src->params.sel_options |= SRC_SELECT_NOSELECT;
|
||||
} else if (!strcasecmp(cmd, "prefer")) {
|
||||
src->params.sel_options |= SRC_SELECT_PREFER;
|
||||
} else if (!strcasecmp(cmd, "require")) {
|
||||
src->params.sel_options |= SRC_SELECT_REQUIRE;
|
||||
} else if (!strcasecmp(cmd, "trust")) {
|
||||
src->params.sel_options |= SRC_SELECT_TRUST;
|
||||
} else if (!strcasecmp(cmd, "certset")) {
|
||||
if (sscanf(line, "%"SCNu32"%n", &src->params.cert_set, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "key")) {
|
||||
if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
|
||||
src->params.authkey == INACTIVE_AUTHKEY)
|
||||
@@ -109,6 +111,16 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
} else if (!strcasecmp(cmd, "asymmetry")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.asymmetry, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "extfield")) {
|
||||
if (sscanf(line, "%"SCNx32"%n", &ef_type, &n) != 1)
|
||||
return 0;
|
||||
switch (ef_type) {
|
||||
case NTP_EF_EXP1:
|
||||
src->params.ext_fields |= NTP_EF_FLAG_EXP1;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
} else if (!strcasecmp(cmd, "filter")) {
|
||||
if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1)
|
||||
return 0;
|
||||
@@ -121,6 +133,9 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
} else if (!strcasecmp(cmd, "maxdelaydevratio")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay_dev_ratio, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxdelayquant")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay_quant, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxpoll")) {
|
||||
if (sscanf(line, "%d%n", &src->params.maxpoll, &n) != 1)
|
||||
return 0;
|
||||
@@ -164,6 +179,8 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "xleave")) {
|
||||
src->params.interleaved = 1;
|
||||
} else if ((sel_option = CPS_GetSelectOption(cmd)) != 0) {
|
||||
src->params.sel_options |= sel_option;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
@@ -174,6 +191,102 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CPS_GetSelectOption(char *option)
|
||||
{
|
||||
if (!strcasecmp(option, "noselect")) {
|
||||
return SRC_SELECT_NOSELECT;
|
||||
} else if (!strcasecmp(option, "prefer")) {
|
||||
return SRC_SELECT_PREFER;
|
||||
} else if (!strcasecmp(option, "require")) {
|
||||
return SRC_SELECT_REQUIRE;
|
||||
} else if (!strcasecmp(option, "trust")) {
|
||||
return SRC_SELECT_TRUST;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CPS_ParseAllowDeny(char *line, int *all, IPAddr *ip, int *subnet_bits)
|
||||
{
|
||||
char *p, *net, *slash;
|
||||
uint32_t a, b, c;
|
||||
int bits, len, n;
|
||||
|
||||
p = CPS_SplitWord(line);
|
||||
|
||||
if (strcmp(line, "all") == 0) {
|
||||
*all = 1;
|
||||
net = p;
|
||||
p = CPS_SplitWord(p);
|
||||
} else {
|
||||
*all = 0;
|
||||
net = line;
|
||||
}
|
||||
|
||||
/* Make sure there are no other arguments */
|
||||
if (*p)
|
||||
return 0;
|
||||
|
||||
/* No specified address or network means all IPv4 and IPv6 addresses */
|
||||
if (!*net) {
|
||||
ip->family = IPADDR_UNSPEC;
|
||||
*subnet_bits = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
slash = strchr(net, '/');
|
||||
if (slash) {
|
||||
if (sscanf(slash + 1, "%d%n", &bits, &len) != 1 || slash[len + 1] || bits < 0)
|
||||
return 0;
|
||||
*slash = '\0';
|
||||
} else {
|
||||
bits = -1;
|
||||
}
|
||||
|
||||
if (UTI_StringToIP(net, ip)) {
|
||||
if (bits >= 0)
|
||||
*subnet_bits = bits;
|
||||
else
|
||||
*subnet_bits = ip->family == IPADDR_INET6 ? 128 : 32;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check for a shortened IPv4 network notation using only 1, 2, or 3 decimal
|
||||
numbers. This is different than the numbers-and-dots notation accepted
|
||||
by inet_aton()! */
|
||||
|
||||
a = b = c = 0;
|
||||
n = sscanf(net, "%"PRIu32"%n.%"PRIu32"%n.%"PRIu32"%n", &a, &len, &b, &len, &c, &len);
|
||||
|
||||
if (n > 0 && !net[len]) {
|
||||
if (a > 255 || b > 255 || c > 255)
|
||||
return 0;
|
||||
|
||||
ip->family = IPADDR_INET4;
|
||||
ip->addr.in4 = (a << 24) | (b << 16) | (c << 8);
|
||||
|
||||
if (bits >= 0)
|
||||
*subnet_bits = bits;
|
||||
else
|
||||
*subnet_bits = n * 8;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The last possibility is a hostname */
|
||||
if (bits < 0 && DNS_Name2IPAddress(net, ip, 1) == DNS_Success) {
|
||||
*subnet_bits = ip->family == IPADDR_INET6 ? 128 : 32;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance)
|
||||
{
|
||||
@@ -294,3 +407,19 @@ CPS_ParseKey(char *line, uint32_t *id, const char **type, char **key)
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CPS_ParseRefid(char *line, uint32_t *ref_id)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = *ref_id = 0; line[i] && !isspace((unsigned char)line[i]); i++) {
|
||||
if (i >= 4)
|
||||
return 0;
|
||||
*ref_id |= (uint32_t)line[i] << (24 - i * 8);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,12 @@ typedef struct {
|
||||
/* Parse a command to add an NTP server or peer */
|
||||
extern int CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src);
|
||||
|
||||
/* Get an NTP/refclock select option */
|
||||
extern int CPS_GetSelectOption(char *option);
|
||||
|
||||
/* Parse a command to allow/deny access */
|
||||
extern int CPS_ParseAllowDeny(char *line, int *all, IPAddr *ip, int *subnet_bits);
|
||||
|
||||
/* Parse a command to enable local reference */
|
||||
extern int CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance);
|
||||
|
||||
@@ -51,4 +57,7 @@ extern char *CPS_SplitWord(char *line);
|
||||
/* Parse a key from keyfile */
|
||||
extern int CPS_ParseKey(char *line, uint32_t *id, const char **type, char **key);
|
||||
|
||||
/* Parse a refclock reference ID (returns number of characters) */
|
||||
extern int CPS_ParseRefid(char *line, uint32_t *ref_id);
|
||||
|
||||
#endif /* GOT_CMDPARSE_H */
|
||||
|
||||
312
conf.c
312
conf.c
@@ -76,6 +76,8 @@ static void parse_log(char *);
|
||||
static void parse_mailonchange(char *);
|
||||
static void parse_makestep(char *);
|
||||
static void parse_maxchange(char *);
|
||||
static void parse_ntsserver(char *, ARR_Instance files);
|
||||
static void parse_ntstrustedcerts(char *);
|
||||
static void parse_ratelimit(char *line, int *enabled, int *interval,
|
||||
int *burst, int *leak);
|
||||
static void parse_refclock(char *);
|
||||
@@ -113,6 +115,7 @@ static int cmd_port = DEFAULT_CANDM_PORT;
|
||||
|
||||
static int raw_measurements = 0;
|
||||
static int do_log_measurements = 0;
|
||||
static int do_log_selection = 0;
|
||||
static int do_log_statistics = 0;
|
||||
static int do_log_tracking = 0;
|
||||
static int do_log_rtc = 0;
|
||||
@@ -252,14 +255,15 @@ static char *user;
|
||||
/* NTS server and client configuration */
|
||||
static char *nts_dump_dir = NULL;
|
||||
static char *nts_ntp_server = NULL;
|
||||
static char *nts_server_cert_file = NULL;
|
||||
static char *nts_server_key_file = NULL;
|
||||
static ARR_Instance nts_server_cert_files; /* array of (char *) */
|
||||
static ARR_Instance nts_server_key_files; /* array of (char *) */
|
||||
static int nts_server_port = NKE_PORT;
|
||||
static int nts_server_processes = 1;
|
||||
static int nts_server_connections = 100;
|
||||
static int nts_refresh = 2419200; /* 4 weeks */
|
||||
static int nts_rotate = 604800; /* 1 week */
|
||||
static char *nts_trusted_cert_file = NULL;
|
||||
static ARR_Instance nts_trusted_certs_paths; /* array of (char *) */
|
||||
static ARR_Instance nts_trusted_certs_ids; /* array of uint32_t */
|
||||
|
||||
/* Number of clock updates needed to enable certificate time checks */
|
||||
static int no_cert_time_check = 0;
|
||||
@@ -270,6 +274,12 @@ static int no_system_cert = 0;
|
||||
/* Array of CNF_HwTsInterface */
|
||||
static ARR_Instance hwts_interfaces;
|
||||
|
||||
/* Timeout for resuming reading from sockets waiting for HW TX timestamp */
|
||||
static double hwts_timeout = 0.001;
|
||||
|
||||
/* PTP event port (disabled by default) */
|
||||
static int ptp_port = 0;
|
||||
|
||||
typedef struct {
|
||||
NTP_Source_Type type;
|
||||
int pool;
|
||||
@@ -388,6 +398,11 @@ CNF_Initialise(int r, int client_only)
|
||||
ntp_restrictions = ARR_CreateInstance(sizeof (AllowDeny));
|
||||
cmd_restrictions = ARR_CreateInstance(sizeof (AllowDeny));
|
||||
|
||||
nts_server_cert_files = ARR_CreateInstance(sizeof (char *));
|
||||
nts_server_key_files = ARR_CreateInstance(sizeof (char *));
|
||||
nts_trusted_certs_paths = ARR_CreateInstance(sizeof (char *));
|
||||
nts_trusted_certs_ids = ARR_CreateInstance(sizeof (uint32_t));
|
||||
|
||||
rtc_device = Strdup(DEFAULT_RTC_DEVICE);
|
||||
hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
|
||||
user = Strdup(DEFAULT_USER);
|
||||
@@ -426,6 +441,12 @@ CNF_Finalise(void)
|
||||
Free(((RefclockParameters *)ARR_GetElement(refclock_sources, i))->driver_name);
|
||||
Free(((RefclockParameters *)ARR_GetElement(refclock_sources, i))->driver_parameter);
|
||||
}
|
||||
for (i = 0; i < ARR_GetSize(nts_server_cert_files); i++)
|
||||
Free(*(char **)ARR_GetElement(nts_server_cert_files, i));
|
||||
for (i = 0; i < ARR_GetSize(nts_server_key_files); i++)
|
||||
Free(*(char **)ARR_GetElement(nts_server_key_files, i));
|
||||
for (i = 0; i < ARR_GetSize(nts_trusted_certs_paths); i++)
|
||||
Free(*(char **)ARR_GetElement(nts_trusted_certs_paths, i));
|
||||
|
||||
ARR_DestroyInstance(init_sources);
|
||||
ARR_DestroyInstance(ntp_sources);
|
||||
@@ -437,6 +458,11 @@ CNF_Finalise(void)
|
||||
ARR_DestroyInstance(ntp_restrictions);
|
||||
ARR_DestroyInstance(cmd_restrictions);
|
||||
|
||||
ARR_DestroyInstance(nts_server_cert_files);
|
||||
ARR_DestroyInstance(nts_server_key_files);
|
||||
ARR_DestroyInstance(nts_trusted_certs_paths);
|
||||
ARR_DestroyInstance(nts_trusted_certs_ids);
|
||||
|
||||
Free(drift_file);
|
||||
Free(dumpdir);
|
||||
Free(hwclock_file);
|
||||
@@ -457,9 +483,6 @@ CNF_Finalise(void)
|
||||
Free(tempcomp_point_file);
|
||||
Free(nts_dump_dir);
|
||||
Free(nts_ntp_server);
|
||||
Free(nts_server_cert_file);
|
||||
Free(nts_server_key_file);
|
||||
Free(nts_trusted_cert_file);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -582,6 +605,8 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
||||
parse_string(p, &hwclock_file);
|
||||
} else if (!strcasecmp(command, "hwtimestamp")) {
|
||||
parse_hwtimestamp(p);
|
||||
} else if (!strcasecmp(command, "hwtstimeout")) {
|
||||
parse_double(p, &hwts_timeout);
|
||||
} else if (!strcasecmp(command, "include")) {
|
||||
parse_include(p);
|
||||
} else if (!strcasecmp(command, "initstepslew")) {
|
||||
@@ -643,8 +668,6 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
||||
} else if (!strcasecmp(command, "ntsratelimit")) {
|
||||
parse_ratelimit(p, &nts_ratelimit_enabled, &nts_ratelimit_interval,
|
||||
&nts_ratelimit_burst, &nts_ratelimit_leak);
|
||||
} else if (!strcasecmp(command, "ntstrustedcerts")) {
|
||||
parse_string(p, &nts_trusted_cert_file);
|
||||
} else if (!strcasecmp(command, "ntscachedir") ||
|
||||
!strcasecmp(command, "ntsdumpdir")) {
|
||||
parse_string(p, &nts_dump_dir);
|
||||
@@ -659,9 +682,11 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
||||
} else if (!strcasecmp(command, "ntsrotate")) {
|
||||
parse_int(p, &nts_rotate);
|
||||
} else if (!strcasecmp(command, "ntsservercert")) {
|
||||
parse_string(p, &nts_server_cert_file);
|
||||
parse_ntsserver(p, nts_server_cert_files);
|
||||
} else if (!strcasecmp(command, "ntsserverkey")) {
|
||||
parse_string(p, &nts_server_key_file);
|
||||
parse_ntsserver(p, nts_server_key_files);
|
||||
} else if (!strcasecmp(command, "ntstrustedcerts")) {
|
||||
parse_ntstrustedcerts(p);
|
||||
} else if (!strcasecmp(command, "peer")) {
|
||||
parse_source(p, command, 1);
|
||||
} else if (!strcasecmp(command, "pidfile")) {
|
||||
@@ -670,6 +695,8 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
||||
parse_source(p, command, 1);
|
||||
} else if (!strcasecmp(command, "port")) {
|
||||
parse_int(p, &ntp_port);
|
||||
} else if (!strcasecmp(command, "ptpport")) {
|
||||
parse_int(p, &ptp_port);
|
||||
} else if (!strcasecmp(command, "ratelimit")) {
|
||||
parse_ratelimit(p, &ntp_ratelimit_enabled, &ntp_ratelimit_interval,
|
||||
&ntp_ratelimit_burst, &ntp_ratelimit_leak);
|
||||
@@ -840,16 +867,16 @@ static void
|
||||
parse_refclock(char *line)
|
||||
{
|
||||
int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options;
|
||||
int max_lock_age, pps_forced, stratum, tai;
|
||||
int local, max_lock_age, pps_forced, sel_option, stratum, tai;
|
||||
uint32_t ref_id, lock_ref_id;
|
||||
double offset, delay, precision, max_dispersion, pulse_width;
|
||||
char *p, *cmd, *name, *param;
|
||||
unsigned char ref[5];
|
||||
RefclockParameters *refclock;
|
||||
|
||||
poll = 4;
|
||||
dpoll = 0;
|
||||
filter_length = 64;
|
||||
local = 0;
|
||||
pps_forced = 0;
|
||||
pps_rate = 0;
|
||||
min_samples = SRC_DEFAULT_MINSAMPLES;
|
||||
@@ -889,13 +916,11 @@ parse_refclock(char *line)
|
||||
line = CPS_SplitWord(line);
|
||||
|
||||
if (!strcasecmp(cmd, "refid")) {
|
||||
if (sscanf(line, "%4s%n", (char *)ref, &n) != 1)
|
||||
if ((n = CPS_ParseRefid(line, &ref_id)) == 0)
|
||||
break;
|
||||
ref_id = (uint32_t)ref[0] << 24 | ref[1] << 16 | ref[2] << 8 | ref[3];
|
||||
} else if (!strcasecmp(cmd, "lock")) {
|
||||
if (sscanf(line, "%4s%n", (char *)ref, &n) != 1)
|
||||
if ((n = CPS_ParseRefid(line, &lock_ref_id)) == 0)
|
||||
break;
|
||||
lock_ref_id = (uint32_t)ref[0] << 24 | ref[1] << 16 | ref[2] << 8 | ref[3];
|
||||
} else if (!strcasecmp(cmd, "poll")) {
|
||||
if (sscanf(line, "%d%n", &poll, &n) != 1) {
|
||||
break;
|
||||
@@ -908,6 +933,9 @@ parse_refclock(char *line)
|
||||
if (sscanf(line, "%d%n", &filter_length, &n) != 1) {
|
||||
break;
|
||||
}
|
||||
} else if (!strcasecmp(cmd, "local")) {
|
||||
n = 0;
|
||||
local = 1;
|
||||
} else if (!strcasecmp(cmd, "rate")) {
|
||||
if (sscanf(line, "%d%n", &pps_rate, &n) != 1)
|
||||
break;
|
||||
@@ -945,18 +973,9 @@ parse_refclock(char *line)
|
||||
} else if (!strcasecmp(cmd, "width")) {
|
||||
if (sscanf(line, "%lf%n", &pulse_width, &n) != 1)
|
||||
break;
|
||||
} else if (!strcasecmp(cmd, "noselect")) {
|
||||
} else if ((sel_option = CPS_GetSelectOption(cmd)) != 0) {
|
||||
n = 0;
|
||||
sel_options |= SRC_SELECT_NOSELECT;
|
||||
} else if (!strcasecmp(cmd, "prefer")) {
|
||||
n = 0;
|
||||
sel_options |= SRC_SELECT_PREFER;
|
||||
} else if (!strcasecmp(cmd, "trust")) {
|
||||
n = 0;
|
||||
sel_options |= SRC_SELECT_TRUST;
|
||||
} else if (!strcasecmp(cmd, "require")) {
|
||||
n = 0;
|
||||
sel_options |= SRC_SELECT_REQUIRE;
|
||||
sel_options |= sel_option;
|
||||
} else {
|
||||
other_parse_error("Invalid refclock option");
|
||||
return;
|
||||
@@ -974,6 +993,7 @@ parse_refclock(char *line)
|
||||
refclock->driver_poll = dpoll;
|
||||
refclock->poll = poll;
|
||||
refclock->filter_length = filter_length;
|
||||
refclock->local = local;
|
||||
refclock->pps_forced = pps_forced;
|
||||
refclock->pps_rate = pps_rate;
|
||||
refclock->min_samples = min_samples;
|
||||
@@ -1006,6 +1026,8 @@ parse_log(char *line)
|
||||
raw_measurements = 1;
|
||||
} else if (!strcmp(log_name, "measurements")) {
|
||||
do_log_measurements = 1;
|
||||
} else if (!strcmp(log_name, "selection")) {
|
||||
do_log_selection = 1;
|
||||
} else if (!strcmp(log_name, "statistics")) {
|
||||
do_log_statistics = 1;
|
||||
} else if (!strcmp(log_name, "tracking")) {
|
||||
@@ -1158,103 +1180,56 @@ parse_mailonchange(char *line)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
parse_ntsserver(char *line, ARR_Instance files)
|
||||
{
|
||||
char *file = NULL;
|
||||
|
||||
parse_string(line, &file);
|
||||
ARR_AppendElement(files, &file);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
parse_ntstrustedcerts(char *line)
|
||||
{
|
||||
uint32_t id;
|
||||
char *path;
|
||||
|
||||
if (get_number_of_args(line) == 2) {
|
||||
path = CPS_SplitWord(line);
|
||||
if (sscanf(line, "%"SCNu32, &id) != 1)
|
||||
command_parse_error();
|
||||
} else {
|
||||
check_number_of_args(line, 1);
|
||||
path = line;
|
||||
id = 0;
|
||||
}
|
||||
|
||||
path = Strdup(path);
|
||||
|
||||
ARR_AppendElement(nts_trusted_certs_paths, &path);
|
||||
ARR_AppendElement(nts_trusted_certs_ids, &id);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
parse_allow_deny(char *line, ARR_Instance restrictions, int allow)
|
||||
{
|
||||
char *p;
|
||||
unsigned long a, b, c, d, n;
|
||||
int all = 0;
|
||||
AllowDeny *new_node = NULL;
|
||||
IPAddr ip_addr;
|
||||
int all, subnet_bits;
|
||||
AllowDeny *node;
|
||||
IPAddr ip;
|
||||
|
||||
p = line;
|
||||
if (!CPS_ParseAllowDeny(line, &all, &ip, &subnet_bits))
|
||||
command_parse_error();
|
||||
|
||||
if (!strncmp(p, "all", 3)) {
|
||||
all = 1;
|
||||
p = CPS_SplitWord(line);
|
||||
}
|
||||
|
||||
if (!*p) {
|
||||
/* Empty line applies to all addresses */
|
||||
new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
|
||||
new_node->allow = allow;
|
||||
new_node->all = all;
|
||||
new_node->ip.family = IPADDR_UNSPEC;
|
||||
new_node->subnet_bits = 0;
|
||||
} else {
|
||||
char *slashpos;
|
||||
slashpos = strchr(p, '/');
|
||||
if (slashpos) *slashpos = 0;
|
||||
|
||||
check_number_of_args(p, 1);
|
||||
n = 0;
|
||||
if (UTI_StringToIP(p, &ip_addr) ||
|
||||
(n = sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d)) >= 1) {
|
||||
new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
|
||||
new_node->allow = allow;
|
||||
new_node->all = all;
|
||||
|
||||
if (n == 0) {
|
||||
new_node->ip = ip_addr;
|
||||
if (ip_addr.family == IPADDR_INET6)
|
||||
new_node->subnet_bits = 128;
|
||||
else
|
||||
new_node->subnet_bits = 32;
|
||||
} else {
|
||||
new_node->ip.family = IPADDR_INET4;
|
||||
|
||||
a &= 0xff;
|
||||
b &= 0xff;
|
||||
c &= 0xff;
|
||||
d &= 0xff;
|
||||
|
||||
switch (n) {
|
||||
case 1:
|
||||
new_node->ip.addr.in4 = (a<<24);
|
||||
new_node->subnet_bits = 8;
|
||||
break;
|
||||
case 2:
|
||||
new_node->ip.addr.in4 = (a<<24) | (b<<16);
|
||||
new_node->subnet_bits = 16;
|
||||
break;
|
||||
case 3:
|
||||
new_node->ip.addr.in4 = (a<<24) | (b<<16) | (c<<8);
|
||||
new_node->subnet_bits = 24;
|
||||
break;
|
||||
case 4:
|
||||
new_node->ip.addr.in4 = (a<<24) | (b<<16) | (c<<8) | d;
|
||||
new_node->subnet_bits = 32;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
if (slashpos) {
|
||||
int specified_subnet_bits, n;
|
||||
n = sscanf(slashpos+1, "%d", &specified_subnet_bits);
|
||||
if (n == 1) {
|
||||
new_node->subnet_bits = specified_subnet_bits;
|
||||
} else {
|
||||
command_parse_error();
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!slashpos && DNS_Name2IPAddress(p, &ip_addr, 1) == DNS_Success) {
|
||||
new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
|
||||
new_node->allow = allow;
|
||||
new_node->all = all;
|
||||
new_node->ip = ip_addr;
|
||||
if (ip_addr.family == IPADDR_INET6)
|
||||
new_node->subnet_bits = 128;
|
||||
else
|
||||
new_node->subnet_bits = 32;
|
||||
} else {
|
||||
command_parse_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
node = ARR_GetNewElement(restrictions);
|
||||
node->allow = allow;
|
||||
node->all = all;
|
||||
node->ip = ip;
|
||||
node->subnet_bits = subnet_bits;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1455,8 +1430,8 @@ static void
|
||||
parse_hwtimestamp(char *line)
|
||||
{
|
||||
CNF_HwTsInterface *iface;
|
||||
int n, maxpoll_set = 0;
|
||||
char *p, filter[5];
|
||||
int n;
|
||||
|
||||
if (!*line) {
|
||||
command_parse_error();
|
||||
@@ -1486,6 +1461,10 @@ parse_hwtimestamp(char *line)
|
||||
} else if (!strcasecmp(p, "minpoll")) {
|
||||
if (sscanf(line, "%d%n", &iface->minpoll, &n) != 1)
|
||||
break;
|
||||
} else if (!strcasecmp(p, "maxpoll")) {
|
||||
if (sscanf(line, "%d%n", &iface->maxpoll, &n) != 1)
|
||||
break;
|
||||
maxpoll_set = 1;
|
||||
} else if (!strcasecmp(p, "minsamples")) {
|
||||
if (sscanf(line, "%d%n", &iface->min_samples, &n) != 1)
|
||||
break;
|
||||
@@ -1505,6 +1484,8 @@ parse_hwtimestamp(char *line)
|
||||
iface->rxfilter = CNF_HWTS_RXFILTER_NONE;
|
||||
else if (!strcasecmp(filter, "ntp"))
|
||||
iface->rxfilter = CNF_HWTS_RXFILTER_NTP;
|
||||
else if (!strcasecmp(filter, "ptp"))
|
||||
iface->rxfilter = CNF_HWTS_RXFILTER_PTP;
|
||||
else if (!strcasecmp(filter, "all"))
|
||||
iface->rxfilter = CNF_HWTS_RXFILTER_ALL;
|
||||
else
|
||||
@@ -1519,6 +1500,9 @@ parse_hwtimestamp(char *line)
|
||||
|
||||
if (*p)
|
||||
command_parse_error();
|
||||
|
||||
if (!maxpoll_set)
|
||||
iface->maxpoll = iface->minpoll + 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1643,8 +1627,9 @@ load_source_file(const char *filename)
|
||||
return;
|
||||
|
||||
while (fgets(line, sizeof (line), f)) {
|
||||
if (strlen(line) >= MAX_LINE_LENGTH)
|
||||
continue;
|
||||
/* Require lines to be terminated */
|
||||
if (line[0] == '\0' || line[strlen(line) - 1] != '\n')
|
||||
break;
|
||||
|
||||
CPS_NormalizeLine(line);
|
||||
if (line[0] == '\0')
|
||||
@@ -1719,6 +1704,8 @@ reload_source_dirs(void)
|
||||
new_ids = ARR_GetElements(ntp_source_ids);
|
||||
unresolved = 0;
|
||||
|
||||
LOG_SetContext(LOGC_SourceFile);
|
||||
|
||||
qsort(new_sources, new_size, sizeof (new_sources[0]), compare_sources);
|
||||
|
||||
for (i = j = 0; i < prev_size || j < new_size; ) {
|
||||
@@ -1741,6 +1728,8 @@ reload_source_dirs(void)
|
||||
if (s == NSR_UnresolvedName) {
|
||||
unresolved++;
|
||||
} else if (s != NSR_Success) {
|
||||
LOG(LOGS_ERR, "Could not add source %s", source->params.name);
|
||||
|
||||
/* Mark the source as not present */
|
||||
source->params.name[0] = '\0';
|
||||
}
|
||||
@@ -1752,6 +1741,8 @@ reload_source_dirs(void)
|
||||
}
|
||||
}
|
||||
|
||||
LOG_UnsetContext(LOGC_SourceFile);
|
||||
|
||||
for (i = 0; i < prev_size; i++)
|
||||
Free(prev_sources[i].params.name);
|
||||
Free(prev_sources);
|
||||
@@ -1795,6 +1786,19 @@ CNF_CreateDirs(uid_t uid, gid_t gid)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CNF_CheckReadOnlyAccess(void)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (keys_file)
|
||||
UTI_CheckReadOnlyAccess(keys_file);
|
||||
for (i = 0; i < ARR_GetSize(nts_server_key_files); i++)
|
||||
UTI_CheckReadOnlyAccess(*(char **)ARR_GetElement(nts_server_key_files, i));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CNF_AddInitSources(void)
|
||||
{
|
||||
@@ -1812,7 +1816,8 @@ CNF_AddInitSources(void)
|
||||
ntp_addr.port = cps_source.port;
|
||||
cps_source.params.iburst = 1;
|
||||
|
||||
NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params, NULL);
|
||||
if (NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params, NULL) != NSR_Success)
|
||||
LOG(LOGS_ERR, "Could not add source %s", UTI_IPToString(&ntp_addr.ip_addr));
|
||||
}
|
||||
|
||||
ARR_SetSize(init_sources, 0);
|
||||
@@ -1825,11 +1830,16 @@ CNF_AddSources(void)
|
||||
{
|
||||
NTP_Source *source;
|
||||
unsigned int i;
|
||||
NSR_Status s;
|
||||
|
||||
for (i = 0; i < ARR_GetSize(ntp_sources); i++) {
|
||||
source = (NTP_Source *)ARR_GetElement(ntp_sources, i);
|
||||
NSR_AddSourceByName(source->params.name, source->params.port,
|
||||
source->pool, source->type, &source->params.params, NULL);
|
||||
|
||||
s = NSR_AddSourceByName(source->params.name, source->params.port, source->pool,
|
||||
source->type, &source->params.params, NULL);
|
||||
if (s != NSR_Success && s != NSR_UnresolvedName)
|
||||
LOG(LOGS_ERR, "Could not add source %s", source->params.name);
|
||||
|
||||
Free(source->params.name);
|
||||
}
|
||||
|
||||
@@ -1939,6 +1949,14 @@ CNF_GetLogMeasurements(int *raw)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CNF_GetLogSelection(void)
|
||||
{
|
||||
return do_log_selection;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CNF_GetLogStatistics(void)
|
||||
{
|
||||
@@ -2499,6 +2517,22 @@ CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
double
|
||||
CNF_GetHwTsTimeout(void)
|
||||
{
|
||||
return hwts_timeout;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CNF_GetPtpPort(void)
|
||||
{
|
||||
return ptp_port;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
char *
|
||||
CNF_GetNtsDumpDir(void)
|
||||
{
|
||||
@@ -2515,18 +2549,16 @@ CNF_GetNtsNtpServer(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
char *
|
||||
CNF_GetNtsServerCertFile(void)
|
||||
int
|
||||
CNF_GetNtsServerCertAndKeyFiles(const char ***certs, const char ***keys)
|
||||
{
|
||||
return nts_server_cert_file;
|
||||
}
|
||||
*certs = ARR_GetElements(nts_server_cert_files);
|
||||
*keys = ARR_GetElements(nts_server_key_files);
|
||||
|
||||
/* ================================================== */
|
||||
if (ARR_GetSize(nts_server_cert_files) != ARR_GetSize(nts_server_key_files))
|
||||
LOG_FATAL("Uneven number of NTS certs and keys");
|
||||
|
||||
char *
|
||||
CNF_GetNtsServerKeyFile(void)
|
||||
{
|
||||
return nts_server_key_file;
|
||||
return ARR_GetSize(nts_server_cert_files);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -2571,10 +2603,16 @@ CNF_GetNtsRotate(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
char *
|
||||
CNF_GetNtsTrustedCertFile(void)
|
||||
int
|
||||
CNF_GetNtsTrustedCertsPaths(const char ***paths, uint32_t **ids)
|
||||
{
|
||||
return nts_trusted_cert_file;
|
||||
*paths = ARR_GetElements(nts_trusted_certs_paths);
|
||||
*ids = ARR_GetElements(nts_trusted_certs_ids);
|
||||
|
||||
if (ARR_GetSize(nts_trusted_certs_paths) != ARR_GetSize(nts_trusted_certs_ids))
|
||||
assert(0);
|
||||
|
||||
return ARR_GetSize(nts_trusted_certs_paths);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
13
conf.h
13
conf.h
@@ -44,6 +44,8 @@ extern void CNF_ParseLine(const char *filename, int number, char *line);
|
||||
|
||||
extern void CNF_CreateDirs(uid_t uid, gid_t gid);
|
||||
|
||||
extern void CNF_CheckReadOnlyAccess(void);
|
||||
|
||||
extern void CNF_AddInitSources(void);
|
||||
extern void CNF_AddSources(void);
|
||||
extern void CNF_AddBroadcasts(void);
|
||||
@@ -58,6 +60,7 @@ extern char *CNF_GetLogDir(void);
|
||||
extern char *CNF_GetDumpDir(void);
|
||||
extern int CNF_GetLogBanner(void);
|
||||
extern int CNF_GetLogMeasurements(int *raw);
|
||||
extern int CNF_GetLogSelection(void);
|
||||
extern int CNF_GetLogStatistics(void);
|
||||
extern int CNF_GetLogTracking(void);
|
||||
extern int CNF_GetLogRtc(void);
|
||||
@@ -134,12 +137,14 @@ typedef enum {
|
||||
CNF_HWTS_RXFILTER_ANY,
|
||||
CNF_HWTS_RXFILTER_NONE,
|
||||
CNF_HWTS_RXFILTER_NTP,
|
||||
CNF_HWTS_RXFILTER_PTP,
|
||||
CNF_HWTS_RXFILTER_ALL,
|
||||
} CNF_HwTs_RxFilter;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
int minpoll;
|
||||
int maxpoll;
|
||||
int min_samples;
|
||||
int max_samples;
|
||||
int nocrossts;
|
||||
@@ -150,17 +155,19 @@ typedef struct {
|
||||
} CNF_HwTsInterface;
|
||||
|
||||
extern int CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface);
|
||||
extern double CNF_GetHwTsTimeout(void);
|
||||
|
||||
extern int CNF_GetPtpPort(void);
|
||||
|
||||
extern char *CNF_GetNtsDumpDir(void);
|
||||
extern char *CNF_GetNtsNtpServer(void);
|
||||
extern char *CNF_GetNtsServerCertFile(void);
|
||||
extern char *CNF_GetNtsServerKeyFile(void);
|
||||
extern int CNF_GetNtsServerCertAndKeyFiles(const char ***certs, const char ***keys);
|
||||
extern int CNF_GetNtsServerPort(void);
|
||||
extern int CNF_GetNtsServerProcesses(void);
|
||||
extern int CNF_GetNtsServerConnections(void);
|
||||
extern int CNF_GetNtsRefresh(void);
|
||||
extern int CNF_GetNtsRotate(void);
|
||||
extern char *CNF_GetNtsTrustedCertFile(void);
|
||||
extern int CNF_GetNtsTrustedCertsPaths(const char ***paths, uint32_t **ids);
|
||||
extern int CNF_GetNoSystemCert(void);
|
||||
extern int CNF_GetNoCertTimeCheck(void);
|
||||
|
||||
|
||||
120
configure
vendored
120
configure
vendored
@@ -5,7 +5,7 @@
|
||||
#
|
||||
# Copyright (C) Richard P. Curnow 1997-2003
|
||||
# Copyright (C) Bryan Christianson 2016
|
||||
# Copyright (C) Miroslav Lichvar 2009, 2012-2020
|
||||
# Copyright (C) Miroslav Lichvar 2009, 2012-2021
|
||||
# Copyright (C) Stefan R. Filipek 2019
|
||||
#
|
||||
# =======================================================================
|
||||
@@ -33,13 +33,13 @@ test_code () {
|
||||
echo "int main(int argc, char **argv) {"
|
||||
echo "$code"
|
||||
echo "return 0; }"
|
||||
) > docheck.c
|
||||
) > conftest.c
|
||||
|
||||
echo "docheck.c:" >> config.log
|
||||
cat docheck.c >> config.log
|
||||
echo $MYCC $MYCFLAGS $MYCPPFLAGS $cflags -o docheck docheck.c $ldflags \
|
||||
echo "conftest.c:" >> config.log
|
||||
cat conftest.c >> config.log
|
||||
echo $MYCC $MYCFLAGS $MYCPPFLAGS $cflags -o conftest conftest.c $ldflags \
|
||||
$MYLDFLAGS >> config.log
|
||||
$MYCC $MYCFLAGS $MYCPPFLAGS $cflags -o docheck docheck.c $ldflags \
|
||||
$MYCC $MYCFLAGS $MYCPPFLAGS $cflags -o conftest conftest.c $ldflags \
|
||||
$MYLDFLAGS >> config.log 2>&1
|
||||
|
||||
if [ $? -eq 0 ]
|
||||
@@ -50,7 +50,7 @@ test_code () {
|
||||
echo "No"
|
||||
result=1
|
||||
fi
|
||||
rm -f docheck.c docheck
|
||||
rm -f conftest.c conftest
|
||||
echo >> config.log
|
||||
return $result
|
||||
}
|
||||
@@ -245,6 +245,7 @@ try_lockmem=0
|
||||
feat_asyncdns=1
|
||||
feat_forcednsretry=1
|
||||
try_clock_gettime=1
|
||||
try_arc4random=1
|
||||
try_recvmmsg=1
|
||||
feat_timestamping=1
|
||||
try_timestamping=0
|
||||
@@ -421,6 +422,7 @@ case $OPERATINGSYSTEM in
|
||||
try_setsched=1
|
||||
try_lockmem=1
|
||||
try_phc=1
|
||||
try_arc4random=0
|
||||
add_def LINUX
|
||||
echo "Configuring for " $SYSTEM
|
||||
;;
|
||||
@@ -467,7 +469,7 @@ case $OPERATINGSYSTEM in
|
||||
;;
|
||||
SunOS)
|
||||
EXTRA_OBJECTS="sys_generic.o sys_solaris.o sys_timex.o sys_posix.o"
|
||||
LIBS="$LIBS -lsocket -lnsl -lresolv"
|
||||
LIBS="$LIBS -lsocket -lnsl -lkvm -lelf -lresolv"
|
||||
try_setsched=1
|
||||
try_lockmem=1
|
||||
add_def SOLARIS
|
||||
@@ -479,7 +481,7 @@ case $OPERATINGSYSTEM in
|
||||
add_def FEAT_PRIVDROP
|
||||
priv_ops="ADJUSTTIMEX SETTIME BINDSOCKET"
|
||||
fi
|
||||
echo "Configuring for Solaris (" $SYSTEM "SunOS version" $VERSION ")"
|
||||
echo "Configuring for illumos (" $SYSTEM "SunOS version" $VERSION ")"
|
||||
;;
|
||||
* )
|
||||
echo "error: $SYSTEM is not supported (yet?)"
|
||||
@@ -655,14 +657,28 @@ then
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! test_code 'O_NOFOLLOW flag' 'sys/types.h sys/stat.h fcntl.h' '' "$LIBS" \
|
||||
'return open("/dev/null", O_NOFOLLOW);'
|
||||
then
|
||||
if test_code 'O_NOFOLLOW flag with _GNU_SOURCE' 'sys/types.h sys/stat.h fcntl.h' \
|
||||
'-D_GNU_SOURCE' "$LIBS" \
|
||||
'return open("/dev/null", O_NOFOLLOW);'
|
||||
then
|
||||
add_def _GNU_SOURCE
|
||||
else
|
||||
echo "error: open() does not support O_NOFOLLOW flag"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $try_clock_gettime = "1" ]; then
|
||||
if test_code 'clock_gettime()' 'time.h' '' '' \
|
||||
'clock_gettime(CLOCK_REALTIME, NULL);'
|
||||
'clock_gettime(CLOCK_REALTIME, (void *)1);'
|
||||
then
|
||||
add_def HAVE_CLOCK_GETTIME
|
||||
else
|
||||
if test_code 'clock_gettime() in -lrt' 'time.h' '' '-lrt' \
|
||||
'clock_gettime(CLOCK_REALTIME, NULL);'
|
||||
'clock_gettime(CLOCK_REALTIME, (void *)1);'
|
||||
then
|
||||
add_def HAVE_CLOCK_GETTIME
|
||||
EXTRA_LIBS="$EXTRA_LIBS -lrt"
|
||||
@@ -670,10 +686,11 @@ if [ $try_clock_gettime = "1" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if test_code 'getaddrinfo()' 'sys/types.h sys/socket.h netdb.h' '' "$LIBS" \
|
||||
if ! test_code 'getaddrinfo()' 'sys/types.h sys/socket.h netdb.h' '' "$LIBS" \
|
||||
'return getaddrinfo(0, 0, 0, 0);'
|
||||
then
|
||||
add_def HAVE_GETADDRINFO
|
||||
echo "error: getaddrinfo() not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $feat_asyncdns = "1" ] && \
|
||||
@@ -687,11 +704,14 @@ then
|
||||
use_pthread=1
|
||||
fi
|
||||
|
||||
if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
|
||||
if [ $try_arc4random = "1" ] && \
|
||||
test_code 'arc4random_buf()' 'stdlib.h' '' '' \
|
||||
'arc4random_buf((void *)1, 1);'
|
||||
then
|
||||
add_def HAVE_ARC4RANDOM
|
||||
else
|
||||
if test_code 'getrandom()' 'stdlib.h sys/random.h' '' '' \
|
||||
'return getrandom(NULL, 256, 0);'; then
|
||||
'return getrandom((void *)1, 1, 0);'; then
|
||||
add_def HAVE_GETRANDOM
|
||||
fi
|
||||
fi
|
||||
@@ -885,7 +905,7 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_nettle = "1" ];
|
||||
add_def FEAT_SECHASH
|
||||
|
||||
if test_code 'CMAC in nettle' 'nettle/cmac.h' "$test_cflags" "$test_link" \
|
||||
'cmac128_update(NULL, NULL, NULL, 0, NULL);'
|
||||
'cmac128_update((void *)1, (void *)2, (void *)3, 1, (void *)4);'
|
||||
then
|
||||
add_def HAVE_CMAC
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS cmac_nettle.o"
|
||||
@@ -910,7 +930,7 @@ fi
|
||||
|
||||
if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]; then
|
||||
if test_code 'tomcrypt' 'tomcrypt.h' '-I/usr/include/tomcrypt' '-ltomcrypt' \
|
||||
'hash_memory_multi(find_hash("md5"), NULL, NULL, NULL, 0, NULL, 0);'
|
||||
'hash_memory_multi(find_hash("md5"), (void *)1, (void *)2, (void *)3, 1, (void *)4, 1);'
|
||||
then
|
||||
HASH_OBJ="hash_tomcrypt.o"
|
||||
HASH_LINK="-ltomcrypt"
|
||||
@@ -919,36 +939,76 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_gnutls = "1" ]; then
|
||||
test_cflags="`pkg_config --cflags gnutls`"
|
||||
test_link="`pkg_config --libs gnutls`"
|
||||
if test_code 'gnutls' 'gnutls/crypto.h' \
|
||||
"$test_cflags" "$test_link" '
|
||||
return gnutls_hash((void *)1, (void *)2, 1);'
|
||||
then
|
||||
HASH_OBJ="hash_gnutls.o"
|
||||
HASH_LINK="$test_link"
|
||||
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||
add_def FEAT_SECHASH
|
||||
|
||||
if test_code 'CMAC in gnutls' 'gnutls/crypto.h' "$test_cflags" "$test_link" \
|
||||
'return gnutls_hmac_init((void *)1, GNUTLS_MAC_AES_CMAC_128, (void *)2, 0);'
|
||||
then
|
||||
add_def HAVE_CMAC
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS cmac_gnutls.o"
|
||||
EXTRA_CLI_OBJECTS="$EXTRA_CLI_OBJECTS cmac_gnutls.o"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS $HASH_OBJ"
|
||||
EXTRA_CLI_OBJECTS="$EXTRA_CLI_OBJECTS $HASH_OBJ"
|
||||
LIBS="$LIBS $HASH_LINK"
|
||||
|
||||
if [ $feat_ntp = "1" ] && [ $feat_nts = "1" ] && [ $try_gnutls = "1" ]; then
|
||||
test_cflags="`pkg_config --cflags gnutls`"
|
||||
test_link="`pkg_config --libs gnutls`"
|
||||
if test_code 'gnutls' 'gnutls/gnutls.h' \
|
||||
"$test_cflags" "$test_link" '
|
||||
return gnutls_init(NULL, 0) + GNUTLS_TLS1_3 +
|
||||
gnutls_priority_init2(NULL, "", NULL, GNUTLS_PRIORITY_INIT_DEF_APPEND) +
|
||||
gnutls_prf_rfc5705(NULL, 0, "", 0, "", 16, NULL);'
|
||||
if [ "$HASH_OBJ" = "hash_gnutls.o" ]; then
|
||||
test_cflags=""
|
||||
test_link=""
|
||||
else
|
||||
test_cflags="`pkg_config --cflags gnutls`"
|
||||
test_link="`pkg_config --libs gnutls`"
|
||||
fi
|
||||
if test_code 'TLS1.3 in gnutls' 'gnutls/gnutls.h' \
|
||||
"$test_cflags" "$test_link $LIBS" '
|
||||
return gnutls_init((void *)1, 0) + GNUTLS_TLS1_3 +
|
||||
gnutls_priority_init2((void *)1, "", NULL, GNUTLS_PRIORITY_INIT_DEF_APPEND) +
|
||||
gnutls_prf_rfc5705((void *)1, 0, "", 0, "", 16, (void *)2);'
|
||||
then
|
||||
if test_code 'SIV in nettle' \
|
||||
if test_code 'AES-SIV-CMAC in nettle' \
|
||||
'nettle/siv-cmac.h' "" "$LIBS" \
|
||||
'siv_cmac_aes128_set_key(NULL, NULL);'
|
||||
'siv_cmac_aes128_set_key((void *)1, (void *)2);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_nettle.o"
|
||||
add_def HAVE_SIV
|
||||
add_def HAVE_NETTLE_SIV_CMAC
|
||||
if test_code 'AES-GCM-SIV in nettle' \
|
||||
'nettle/siv-gcm.h' "" "$LIBS" \
|
||||
'siv_gcm_aes128_encrypt_message((void *)1, 0, NULL, 0, (void *)2, 16, (void *)3,
|
||||
(void *)4);'
|
||||
then
|
||||
add_def HAVE_NETTLE_SIV_GCM
|
||||
fi
|
||||
else
|
||||
if test_code 'SIV in gnutls' 'gnutls/gnutls.h' \
|
||||
"$test_cflags" "$test_link" '
|
||||
return gnutls_aead_cipher_init(NULL, GNUTLS_CIPHER_AES_128_SIV, NULL);'
|
||||
if test_code 'AES-SIV-CMAC in gnutls' 'gnutls/crypto.h' \
|
||||
"$test_cflags" "$test_link $LIBS" '
|
||||
return gnutls_aead_cipher_init((void *)1, GNUTLS_CIPHER_AES_128_SIV, (void *)2);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_gnutls.o"
|
||||
add_def HAVE_SIV
|
||||
if test_code 'gnutls_aead_cipher_set_key()' 'gnutls/crypto.h' \
|
||||
"$test_cflags" "$test_link $LIBS" '
|
||||
return gnutls_aead_cipher_set_key((void *)1, (void *)2);'
|
||||
then
|
||||
add_def HAVE_GNUTLS_AEAD_CIPHER_SET_KEY
|
||||
fi
|
||||
else
|
||||
if test_code 'AES128 in nettle' 'nettle/aes.h' '' "$LIBS" \
|
||||
'aes128_set_encrypt_key(NULL, NULL);'
|
||||
'aes128_set_encrypt_key((void *)1, (void *)2);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_nettle.o"
|
||||
add_def HAVE_SIV
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
170
doc/chronyc.adoc
170
doc/chronyc.adoc
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// Copyright (C) Richard P. Curnow 1997-2003
|
||||
// Copyright (C) Stephen Wadeley 2016
|
||||
// Copyright (C) Miroslav Lichvar 2009-2017, 2019-2020
|
||||
// Copyright (C) Miroslav Lichvar 2009-2017, 2019-2022
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -78,11 +78,10 @@ This option disables resolving of IP addresses to hostnames, e.g. to avoid slow
|
||||
DNS lookups. Long addresses will not be truncated to fit into the column.
|
||||
|
||||
*-N*::
|
||||
This option enables printing of the original names of NTP sources that were
|
||||
specified in the configuration file, or *chronyc* commands, and are internally
|
||||
used by *chronyd*. Without the *-n* and *-N* option, the names of NTP sources
|
||||
are obtained from reverse DNS lookups and can be different from the original
|
||||
names.
|
||||
This option enables printing of original hostnames or IP addresses of NTP
|
||||
sources that were specified in the configuration file, or *chronyc* commands.
|
||||
Without the *-n* and *-N* option, the printed hostnames are obtained from
|
||||
reverse DNS lookups and can be different from the specified hostnames.
|
||||
|
||||
*-c*::
|
||||
This option enables printing of reports in a comma-separated values (CSV)
|
||||
@@ -90,6 +89,10 @@ format. Reverse DNS lookups will be disabled, time will be printed as number of
|
||||
seconds since the epoch, and values in seconds will not be converted to other
|
||||
units.
|
||||
|
||||
*-e*::
|
||||
With this option each *chronyc* response will end with a line containing a
|
||||
single dot.
|
||||
|
||||
*-d*::
|
||||
This option enables printing of debugging messages if *chronyc* was compiled
|
||||
with debugging support.
|
||||
@@ -100,12 +103,15 @@ With this option multiple commands can be specified. Each argument will be
|
||||
interpreted as a whole command.
|
||||
|
||||
*-h* _host_::
|
||||
This option allows the user to specify which host (or comma-separated list of
|
||||
addresses) running the *chronyd* program is to be contacted. This allows for
|
||||
remote monitoring, without having to connect over SSH to the other host first.
|
||||
This option specifies the host to be contacted by *chronyc*. It can be
|
||||
specified with a hostname, IP address, or path to the local Unix domain socket.
|
||||
Multiple values can be specified as a comma-separated list to provide a
|
||||
fallback.
|
||||
+
|
||||
The default is to contact *chronyd* running on the same host where
|
||||
*chronyc* is being run.
|
||||
The default value is _@CHRONYRUNDIR@/chronyd.sock,127.0.0.1,::1_, i.e. the host
|
||||
where *chronyc* is being run. First, it tries to connect to the Unix domain
|
||||
socket and if that fails (e.g. due to running under a non-root user), it
|
||||
will try to connect to 127.0.0.1 and then ::1.
|
||||
|
||||
*-p* _port_::
|
||||
This option allows the user to specify the UDP port number which the target
|
||||
@@ -178,15 +184,19 @@ stratum-2 and is synchronised from a stratum-1).
|
||||
This is the time (UTC) at which the last measurement from the reference
|
||||
source was processed.
|
||||
*System time*:::
|
||||
In normal operation, *chronyd* by default never steps the system clock, because
|
||||
any jump in the time can have adverse consequences for certain application
|
||||
programs. Instead, any error in the system clock is corrected by slightly
|
||||
speeding up or slowing down the system clock until the error has been removed,
|
||||
and then returning to the system clock's normal speed. A consequence of this is
|
||||
that there will be a period when the system clock (as read by other programs)
|
||||
will be different from *chronyd*'s estimate of the current true time (which it
|
||||
reports to NTP clients when it is operating as a server). The value reported
|
||||
on this line is the difference due to this effect.
|
||||
This is the current offset between the NTP clock and system clock. The NTP
|
||||
clock is a software (virtual) clock maintained by *chronyd*, which is
|
||||
synchronised to the configured time sources and provides time to NTP clients.
|
||||
The system clock is synchronised to the NTP clock. To avoid steps in the
|
||||
system time, which might have adverse consequences for certain applications,
|
||||
the system clock is normally corrected only by speeding up or slowing down (up
|
||||
to the rate configured by the <<chrony.conf.adoc#maxslewrate,*maxslewrate*>>
|
||||
directive). If the offset is too large, this correction will take a very long
|
||||
time. A step can be forced by the <<makestep,*makestep*>> command, or the
|
||||
<<chrony.conf.adoc#makestep,*makestep*>> directive in the configuration file.
|
||||
+
|
||||
Note that all other offsets reported by *chronyc* and most offsets in the log
|
||||
files are relative to the NTP clock, not the system clock.
|
||||
*Last offset*:::
|
||||
This is the estimated local offset on the last clock update. A positive value
|
||||
indicates the local time (as previously estimated true time) was ahead of the
|
||||
@@ -449,6 +459,7 @@ states are reported.
|
||||
The following states indicate the source is not considered selectable for
|
||||
synchronisation:
|
||||
* _N_ - has the *noselect* option.
|
||||
* _s_ - is not synchronised.
|
||||
* _M_ - does not have enough measurements.
|
||||
* _d_ - has a root distance larger than the maximum distance (configured by the
|
||||
<<chrony.conf.adoc#maxdistance,*maxdistance*>> directive).
|
||||
@@ -493,8 +504,8 @@ This column displays the configured selection options of the source.
|
||||
This column displays the current effective selection options of the source,
|
||||
which can be different from the configured options due to the authentication
|
||||
selection mode (configured by the
|
||||
<<chrony.conf.adoc#authselmode,*authselmode*>> directive). The symbols are the
|
||||
same as in the *COpts* column.
|
||||
<<chrony.conf.adoc#authselectmode,*authselectmode*>> directive). The symbols
|
||||
are the same as in the *COpts* column.
|
||||
*Last*:::
|
||||
This column displays how long ago was the last measurement of the source made
|
||||
when the selection was performed.
|
||||
@@ -515,6 +526,23 @@ This column displays the current leap status of the source.
|
||||
* _-_ indicates that a leap second will be deleted at the end of the month.
|
||||
* _?_ indicates the unknown status (i.e. no valid measurement was made).
|
||||
|
||||
[[selectopts]]*selectopts* _address|refid_ [_+|-option_]...::
|
||||
The *selectopts* command modifies the configured selection options of an NTP
|
||||
source specified by IP address (or the _ID#XXXXXXXXXX_ identifier used for
|
||||
unknown addresses), or a reference clock specified by reference ID as a string.
|
||||
+
|
||||
The selection options can be added with the *+* symbol or removed with the *-*
|
||||
symbol. The *selectdata* command can be used to verify the configuration. The
|
||||
modified options will be applied in the next source selection, e.g. when a new
|
||||
measurement is made, or the *reselect* command is executed.
|
||||
+
|
||||
An example of using this command is shown below.
|
||||
+
|
||||
----
|
||||
selectopts 1.2.3.4 -noselect +prefer
|
||||
selectopts GPS +trust
|
||||
----
|
||||
|
||||
[[reselect]]*reselect*::
|
||||
To avoid excessive switching between sources, *chronyd* can stay synchronised
|
||||
to a source even when it is not currently the best one among the available
|
||||
@@ -603,6 +631,7 @@ be reported:
|
||||
* 13: AES128
|
||||
* 14: AES256
|
||||
* 15: AEAD-AES-SIV-CMAC-256
|
||||
* 30: AEAD-AES-128-GCM-SIV
|
||||
*KLen*:::
|
||||
This column shows the length of the key in bits.
|
||||
*Last*:::
|
||||
@@ -659,6 +688,7 @@ RX timestamping : Kernel
|
||||
Total TX : 24
|
||||
Total RX : 24
|
||||
Total valid RX : 24
|
||||
Total good RX : 22
|
||||
----
|
||||
+
|
||||
The fields are explained as follows:
|
||||
@@ -696,7 +726,8 @@ packets sent to the source is more variable than the delay of packets sent
|
||||
from the source back.
|
||||
*NTP tests*:::
|
||||
Results of RFC 5905 tests 1 through 3, 5 through 7, and tests for maximum
|
||||
delay, delay ratio, delay dev ratio, and synchronisation loop.
|
||||
delay, delay ratio, delay dev ratio (or delay quantile), and synchronisation
|
||||
loop.
|
||||
*Interleaved*:::
|
||||
This shows if the response was in the interleaved mode.
|
||||
*Authenticated*:::
|
||||
@@ -711,7 +742,10 @@ The number of packets sent to the source.
|
||||
*Total RX*:::
|
||||
The number of all packets received from the source.
|
||||
*Total valid RX*:::
|
||||
The number of valid packets received from the source.
|
||||
The number of packets which passed the first two groups of NTP tests.
|
||||
*Total good RX*:::
|
||||
The number of packets which passed all three groups of NTP tests, i.e. the NTP
|
||||
measurement was accepted.
|
||||
|
||||
[[add_peer]]*add peer* _name_ [_option_]...::
|
||||
The *add peer* command allows a new NTP peer to be added whilst
|
||||
@@ -1108,13 +1142,9 @@ The columns are as follows:
|
||||
received/accepted.
|
||||
|
||||
[[serverstats]]*serverstats*::
|
||||
The *serverstats* command displays how many valid NTP and command requests, and
|
||||
NTS-KE connections, *chronyd* operating as a server received from clients, and
|
||||
how many of them were dropped due to rate limiting. It also displays how many
|
||||
client log records were dropped due to the memory limit configured by the
|
||||
<<chrony.conf.adoc#clientloglimit,*clientloglimit*>> directive and how many of
|
||||
the NTP requests (from those which were not dropped) were authenticated. An
|
||||
example of the output is shown below.
|
||||
The *serverstats* command displays NTP and command server statistics.
|
||||
+
|
||||
An example of the output is shown below.
|
||||
+
|
||||
----
|
||||
NTP packets received : 1598
|
||||
@@ -1125,7 +1155,67 @@ Client log records dropped : 0
|
||||
NTS-KE connections accepted: 3
|
||||
NTS-KE connections dropped : 0
|
||||
Authenticated NTP packets : 189
|
||||
Interleaved NTP packets : 43
|
||||
NTP timestamps held : 44
|
||||
NTP timestamp span : 120
|
||||
NTP daemon RX timestamps : 0
|
||||
NTP daemon TX timestamps : 1537
|
||||
NTP kernel RX timestamps : 1590
|
||||
NTP kernel TX timestamps : 43
|
||||
NTP hardware RX timestamps : 0
|
||||
NTP hardware TX timestamps : 0
|
||||
----
|
||||
+
|
||||
The fields have the following meaning:
|
||||
+
|
||||
*NTP packets received*:::
|
||||
The number of valid NTP requests received by the server.
|
||||
*NTP packets dropped*:::
|
||||
The number of NTP requests dropped by the server due to rate limiting
|
||||
(configured by the <<chrony.conf.adoc#ratelimit,*ratelimit*>> directive).
|
||||
*Command packets received*:::
|
||||
The number of command requests received by the server.
|
||||
*Command packets dropped*:::
|
||||
The number of command requests dropped by the server due to rate limiting
|
||||
(configured by the <<chrony.conf.adoc#cmdratelimit,*cmdratelimit*>> directive).
|
||||
*Client log records dropped*:::
|
||||
The number of client log records dropped by the server to limit the memory use
|
||||
(configured by the <<chrony.conf.adoc#clientloglimit,*clientloglimit*>>
|
||||
directive).
|
||||
*NTS-KE connections accepted*:::
|
||||
The number of NTS-KE connections accepted by the server.
|
||||
*NTS-KE connections dropped*:::
|
||||
The number of NTS-KE connections dropped by the server due to rate limiting
|
||||
(configured by the <<chrony.conf.adoc#ntsratelimit,*ntsratelimit*>> directive).
|
||||
*Authenticated NTP packets*:::
|
||||
The number of received NTP requests that were authenticated (with a symmetric
|
||||
key or NTS).
|
||||
*Interleaved NTP packets*:::
|
||||
The number of received NTP requests that were detected to be in the interleaved
|
||||
mode.
|
||||
*NTP timestamps held*:::
|
||||
The number of pairs of receive and transmit timestamps that the server is
|
||||
currently holding in memory for clients using the interleaved mode.
|
||||
*NTP timestamp span*:::
|
||||
The interval (in seconds) covered by the currently held NTP timestamps.
|
||||
*NTP daemon RX timestamps*:::
|
||||
The number of NTP responses which included a receive timestamp captured by the
|
||||
daemon.
|
||||
*NTP daemon TX timestamps*:::
|
||||
The number of NTP responses which included a transmit timestamp captured by the
|
||||
daemon.
|
||||
*NTP kernel RX timestamps*:::
|
||||
The number of NTP responses which included a receive timestamp captured by the
|
||||
kernel.
|
||||
*NTP kernel TX timestamps*:::
|
||||
The number of NTP responses (in the interleaved mode) which included a transmit
|
||||
timestamp captured by the kernel.
|
||||
*NTP hardware RX timestamps*:::
|
||||
The number of NTP responses which included a receive timestamp captured by the
|
||||
NIC.
|
||||
*NTP hardware TX timestamps*:::
|
||||
The number of NTP responses (in the interleaved mode) which included a transmit
|
||||
timestamp captured by the NIC.
|
||||
|
||||
[[allow]]*allow* [*all*] [_subnet_]::
|
||||
The effect of the allow command is identical to the
|
||||
@@ -1134,11 +1224,8 @@ The effect of the allow command is identical to the
|
||||
The syntax is illustrated in the following examples:
|
||||
+
|
||||
----
|
||||
allow foo.example.net
|
||||
allow all 1.2
|
||||
allow 3.4.5
|
||||
allow 6.7.8/22
|
||||
allow 6.7.8.9/22
|
||||
allow 1.2.3.4
|
||||
allow all 3.4.5.0/24
|
||||
allow 2001:db8:789a::/48
|
||||
allow 0/0
|
||||
allow ::/0
|
||||
@@ -1153,11 +1240,8 @@ The effect of the allow command is identical to the
|
||||
The syntax is illustrated in the following examples:
|
||||
+
|
||||
----
|
||||
deny foo.example.net
|
||||
deny all 1.2
|
||||
deny 3.4.5
|
||||
deny 6.7.8/22
|
||||
deny 6.7.8.9/22
|
||||
deny 1.2.3.4
|
||||
deny all 3.4.5.0/24
|
||||
deny 2001:db8:789a::/48
|
||||
deny 0/0
|
||||
deny ::/0
|
||||
@@ -1170,8 +1254,8 @@ deny all
|
||||
*local* *off*::
|
||||
The *local* command allows *chronyd* to be told that it is to appear as a
|
||||
reference source, even if it is not itself properly synchronised to an external
|
||||
source. (This can be used on isolated networks, to allow one computer to be a
|
||||
master time server with the other computers slaving to it.)
|
||||
source. This can be used on isolated networks, to allow a computer to be the
|
||||
primary time server for other computers.
|
||||
+
|
||||
The first form enables the local reference mode on the host. The syntax is
|
||||
identical to the <<chrony.conf.adoc#local,*local*>> directive in the
|
||||
|
||||
@@ -88,8 +88,10 @@ will not detach from the terminal.
|
||||
|
||||
*-Q*::
|
||||
This option is similar to the *-q* option, except it only prints the offset
|
||||
without making any corrections of the clock and it allows *chronyd* to be
|
||||
started without root privileges.
|
||||
without making any corrections of the clock and disables server ports to allow
|
||||
*chronyd* to be started without root privileges, assuming the configuration
|
||||
does not have any directives which would require them (e.g. *refclock*,
|
||||
*hwtimestamp*, *rtcfile*, etc).
|
||||
|
||||
*-r*::
|
||||
This option will try to reload and then delete files containing sample
|
||||
@@ -100,7 +102,7 @@ directive in the configuration file. This option is useful if you want to stop
|
||||
and restart *chronyd* briefly for any reason, e.g. to install a new version.
|
||||
However, it should be used only on systems where the kernel can maintain clock
|
||||
compensation whilst not under *chronyd*'s control (i.e. Linux, FreeBSD, NetBSD,
|
||||
Solaris, and macOS 10.13 or later).
|
||||
illumos, and macOS 10.13 or later).
|
||||
|
||||
*-R*::
|
||||
When this option is used, the <<chrony.conf.adoc#initstepslew,*initstepslew*>>
|
||||
@@ -141,7 +143,7 @@ after start in order to drop root privileges. It overrides the
|
||||
_@DEFAULT_USER@_.
|
||||
+
|
||||
On Linux, *chronyd* needs to be compiled with support for the *libcap* library.
|
||||
On macOS, FreeBSD, NetBSD and Solaris *chronyd* forks into two processes.
|
||||
On macOS, FreeBSD, NetBSD, and illumos *chronyd* forks into two processes.
|
||||
The child process retains root privileges, but can only perform a very limited
|
||||
range of privileged system calls on behalf of the parent.
|
||||
|
||||
@@ -156,29 +158,40 @@ not recommended when the configuration is not known, or at least limited to
|
||||
specific directives.
|
||||
|
||||
*-F* _level_::
|
||||
This option configures a system call filter when *chronyd* is compiled with
|
||||
support for the Linux secure computing (seccomp) facility. In level 1 the
|
||||
process is killed when a forbidden system call is made, in level -1 the SIGSYS
|
||||
signal is thrown instead and in level 0 the filter is disabled. The default
|
||||
value is 0.
|
||||
This option configures system call filters loaded by *chronyd* processes if it
|
||||
was compiled with support for the Linux secure computing (seccomp) facility.
|
||||
Three levels are defined: 0, 1, 2. The filters are disabled at level 0. At
|
||||
levels 1 and 2, *chronyd* will be killed if it makes a system call which is
|
||||
blocked by the filters. The level can be specified as a negative number to
|
||||
trigger the SIGSYS signal instead of SIGKILL, which can be useful for
|
||||
debugging. The default value is 0.
|
||||
+
|
||||
It is recommended to enable the filter only when it is known to work on the
|
||||
version of the system where *chrony* is installed as the filter needs to allow
|
||||
also system calls made from libraries that *chronyd* is using (e.g. libc) and
|
||||
different versions or implementations of the libraries might make different
|
||||
system calls. If the filter is missing some system call, *chronyd* could be
|
||||
killed even in normal operation.
|
||||
At level 1, the filters allow only selected system calls that are normally
|
||||
expected to be made by *chronyd*. Other system calls are blocked. This level is
|
||||
recommended only if it is known to work on the version of the system where
|
||||
*chrony* is installed. The filters need to allow also system calls made by
|
||||
libraries that *chronyd* is using (e.g. libc), but different versions or
|
||||
implementations of the libraries might make different system calls. If the
|
||||
filters are missing a system call, *chronyd* could be killed even in normal
|
||||
operation.
|
||||
+
|
||||
At level 2, the filters block only a small number of specific system calls
|
||||
(e.g. fork and exec). This approach should avoid false positives, but the
|
||||
protection of the system against a compromised *chronyd* process is much more
|
||||
limited.
|
||||
+
|
||||
The filters cannot be enabled with the *mailonchange* directive.
|
||||
|
||||
*-P* _priority_::
|
||||
On Linux, this option will select the SCHED_FIFO real-time scheduler at the
|
||||
specified priority (which must be between 0 and 100). On macOS, this option
|
||||
must have either a value of 0 to disable the thread time
|
||||
constraint policy or 1 for the policy to be enabled. Other systems do not
|
||||
On Linux, FreeBSD, NetBSD, and illumos this option will select the SCHED_FIFO
|
||||
real-time scheduler at the specified priority (which must be between 0 and
|
||||
100). On macOS, this option must have either a value of 0 to disable the thread
|
||||
time constraint policy or 1 for the policy to be enabled. Other systems do not
|
||||
support this option. The default value is 0.
|
||||
|
||||
*-m*::
|
||||
This option will lock *chronyd* into RAM so that it will never be paged out.
|
||||
This mode is only supported on Linux.
|
||||
This mode is only supported on Linux, FreeBSD, NetBSD, and illumos.
|
||||
|
||||
*-x*::
|
||||
This option disables the control of the system clock. *chronyd* will not try to
|
||||
|
||||
474
doc/faq.adoc
474
doc/faq.adoc
@@ -1,7 +1,7 @@
|
||||
// This file is part of chrony
|
||||
//
|
||||
// Copyright (C) Richard P. Curnow 1997-2003
|
||||
// Copyright (C) Miroslav Lichvar 2014-2016, 2020
|
||||
// Copyright (C) Miroslav Lichvar 2014-2016, 2020-2022
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -49,9 +49,11 @@ website.
|
||||
|
||||
First, the client needs to know which NTP servers it should ask for the current
|
||||
time. They are specified by the `server` or `pool` directive. The `pool`
|
||||
directive can be used for names that resolve to multiple addresses. For good
|
||||
reliability the client should have at least three servers. The `iburst` option
|
||||
speeds up the initial synchronisation.
|
||||
directive is used with names that resolve to multiple addresses of different
|
||||
servers. For reliable operation, the client should have at least three servers.
|
||||
|
||||
The `iburst` option enables a burst of requests to speed up the initial
|
||||
synchronisation.
|
||||
|
||||
To stabilise the initial synchronisation on the next start, the estimated drift
|
||||
of the system clock is saved to a file specified by the `driftfile` directive.
|
||||
@@ -67,7 +69,7 @@ next boot from the RTC, the `rtcsync` directive enables a mode in which the
|
||||
system time is periodically copied to the RTC. It is supported on Linux and
|
||||
macOS.
|
||||
|
||||
If you want to use public NTP servers from the
|
||||
If you wanted to use public NTP servers from the
|
||||
https://www.pool.ntp.org/[pool.ntp.org] project, the minimal _chrony.conf_ file
|
||||
could be:
|
||||
|
||||
@@ -80,9 +82,16 @@ rtcsync
|
||||
|
||||
=== How do I make an NTP server?
|
||||
|
||||
You need to add an `allow` directive to the _chrony.conf_ file in order to open
|
||||
the NTP port and allow `chronyd` to reply to client requests. `allow` with no
|
||||
specified subnet allows access from all IPv4 and IPv6 addresses.
|
||||
By default, `chronyd` does not operate as an NTP server. You need to add an
|
||||
`allow` directive to the _chrony.conf_ file in order for `chronyd` to open the
|
||||
server NTP port and respond to client requests.
|
||||
|
||||
----
|
||||
allow 192.168.1.0/24
|
||||
----
|
||||
|
||||
An `allow` directive with no specified subnet allows access from all IPv4 and
|
||||
IPv6 addresses.
|
||||
|
||||
=== Should all computers on a LAN be clients of an external server?
|
||||
|
||||
@@ -128,21 +137,136 @@ versions or implementations of the libraries might make different system calls.
|
||||
If the filter is missing some system call, `chronyd` could be killed even in
|
||||
normal operation.
|
||||
|
||||
=== How can I make the system clock more secure?
|
||||
|
||||
An NTP client synchronising the system clock to an NTP server is susceptible to
|
||||
various attacks, which can break applications and network protocols relying on
|
||||
accuracy of the clock (e.g. DNSSEC, Kerberos, TLS, WireGuard).
|
||||
|
||||
Generally, a man-in-the-middle (MITM) attacker between the client and server
|
||||
can
|
||||
|
||||
* make fake responses, or modify real responses from the server, to create an
|
||||
arbitrarily large time and frequency offset, make the server appear more
|
||||
accurate, insert a leap second, etc.
|
||||
* delay the requests and/or responses to create a limited time offset and
|
||||
temporarily also a limited frequency offset
|
||||
* drop the requests or responses to prevent updates of the clock with new
|
||||
measurements
|
||||
* redirect the requests to a different server
|
||||
|
||||
The attacks can be combined for a greater effect. The attacker can delay
|
||||
packets to create a significant frequency offset first and then drop all
|
||||
subsequent packets to let the clock quickly drift away from the true time.
|
||||
The attacker might also be able to control the server's clock.
|
||||
|
||||
Some attacks cannot be prevented. Monitoring is needed for detection, e.g. the
|
||||
reachability register in the `sources` report shows missing packets. The extent
|
||||
to which the attacker can control the client's clock depends on its
|
||||
configuration.
|
||||
|
||||
Enable authentication to prevent `chronyd` from accepting modified, fake, or
|
||||
redirected packets. It can be enabled with a symmetric key specified by the
|
||||
`key` option, or Network Time Security (NTS) by the `nts` option (supported
|
||||
since `chrony` version 4.0). The server needs to support the selected
|
||||
authentication mechanism. Symmetric keys have to be configured on both client
|
||||
and server, and each client must have its own key (one per server).
|
||||
|
||||
The maximum offset that the attacker can insert in an NTP measurement by
|
||||
delaying packets can be limited by the `maxdelay` option. The default value is
|
||||
3 seconds. The measured delay is reported as the peer delay in the `ntpdata`
|
||||
report and `measurements` log. Set the `maxdelay` option to a value larger than
|
||||
the maximum value that is normally observed. Note that the delay can increase
|
||||
significantly even when not under an attack, e.g. when the network is congested
|
||||
or the routing has changed.
|
||||
|
||||
The maximum accepted change in time offset between clock updates can be limited
|
||||
by the `maxchange` directive. Larger changes in the offset will be ignored or
|
||||
cause `chronyd` to exit. Note that the attacker can get around this limit by
|
||||
splitting the offset into multiple smaller offsets and/or creating a large
|
||||
frequency offset. When this directive is used, `chronyd` will have to be
|
||||
restarted after a successful attack. It will not be able to recover on its own.
|
||||
It must not be restarted automatically (e.g. by the service manager).
|
||||
|
||||
The impact of a large accepted time offset can be reduced by disabling clock
|
||||
steps, i.e. by not using the `makestep` and `initstepslew` directives. The
|
||||
offset will be slowly corrected by speeding up or slowing down the clock at a
|
||||
rate which can be limited by the `maxslewrate` directive. Disabling clock steps
|
||||
completely is practical only if the clock cannot gain a larger error on its
|
||||
own, e.g. when the computer is shut down or suspended, and the `maxslewrate`
|
||||
limit is large enough to correct an expected error in an acceptable time. The
|
||||
`rtcfile` directive with the `-s` option can be used to compensate for the RTC
|
||||
drift.
|
||||
|
||||
A more practical approach is to enable `makestep` for a limited number of clock
|
||||
updates (the 2nd argument of the directive) and limit the offset change in all
|
||||
updates by the `maxchange` directive. The attacker will be able to make only a
|
||||
limited step and only if the attack starts in a short window after booting the
|
||||
computer, or when `chronyd` is restarted without the `-R` option.
|
||||
|
||||
The frequency offset can be limited by the `maxdrift` directive. The measured
|
||||
frequency offset is reported in the drift file, `tracking` report, and
|
||||
`tracking` log. Set `maxdrift` to a value larger than the maximum absolute
|
||||
value that is normally observed. Note that the frequency of the clock can
|
||||
change due to aging of the crystal, differences in calibration of the clock
|
||||
source between reboots, migrated virtual machine, etc. A typical computer clock
|
||||
has a drift smaller than 100 parts per million (ppm), but much larger drifts
|
||||
are possible (e.g. in some virtual machines).
|
||||
|
||||
Use only trusted servers, which you expect to be well configured and managed,
|
||||
using authentication for their own servers, etc. Use multiple servers, ideally
|
||||
in different locations. The attacker will have to deal with a majority of the
|
||||
servers in order to pass the source selection and update the clock with a large
|
||||
offset. Use the `minsources` directive to increase the required number of
|
||||
selectable sources to make the selection more robust.
|
||||
|
||||
Do not specify servers as peers. The symmetric mode is less secure than the
|
||||
client/server mode. If not authenticated, it is vulnerable to off-path
|
||||
denial-of-service attacks, and even when it is authenticated, it is still
|
||||
susceptible to replay attacks.
|
||||
|
||||
Mixing of authenticated and unauthenticated servers should generally be
|
||||
avoided. If mixing is necessary (e.g. for a more accurate and stable
|
||||
synchronisation to a closer server which does not support authentication), the
|
||||
authenticated servers should be configured as trusted and required to not allow
|
||||
the unauthenticated servers to override the authenticated servers in the source
|
||||
selection. Since `chrony` version 4.0, the selection options are enabled in
|
||||
such a case automatically. This behaviour can be disabled or modified by the
|
||||
`authselectmode` directive.
|
||||
|
||||
An example of a client configuration limiting the impact of the attacks could
|
||||
be
|
||||
|
||||
----
|
||||
server foo.example.net iburst nts maxdelay 0.1
|
||||
server bar.example.net iburst nts maxdelay 0.2
|
||||
server baz.example.net iburst nts maxdelay 0.05
|
||||
server qux.example.net iburst nts maxdelay 0.1
|
||||
server quux.example.net iburst nts maxdelay 0.1
|
||||
minsources 3
|
||||
maxchange 100 0 0
|
||||
makestep 0.001 1
|
||||
maxdrift 100
|
||||
maxslewrate 100
|
||||
driftfile /var/lib/chrony/drift
|
||||
ntsdumpdir /var/lib/chrony
|
||||
rtcsync
|
||||
----
|
||||
|
||||
=== How can I improve the accuracy of the system clock with NTP sources?
|
||||
|
||||
Select NTP servers that are well synchronised, stable and close to your
|
||||
network. It is better to use more than one server, three or four is usually
|
||||
network. It is better to use more than one server. Three or four is usually
|
||||
recommended as the minimum, so `chronyd` can detect servers that serve false
|
||||
time and combine measurements from multiple sources.
|
||||
|
||||
If you have a network card with hardware timestamping supported on Linux, it
|
||||
can be enabled by the `hwtimestamp` directive in the _chrony.conf_ file. It
|
||||
should make local receive and transmit timestamps of NTP packets much more
|
||||
accurate.
|
||||
can be enabled by the `hwtimestamp` directive. It should make local receive and
|
||||
transmit timestamps of NTP packets much more stable and accurate.
|
||||
|
||||
There are also useful options which can be set in the `server` directive, they
|
||||
are `minpoll`, `maxpoll`, `polltarget`, `maxdelay`, `maxdelayratio`,
|
||||
`maxdelaydevratio`, and `xleave`.
|
||||
The `server` directive has some useful options: `minpoll`, `maxpoll`,
|
||||
`polltarget`, `maxdelay`, `maxdelayratio`, `maxdelaydevratio`, `xleave`,
|
||||
`filter`.
|
||||
|
||||
The first three options set the minimum and maximum allowed polling interval,
|
||||
and how should be the actual interval adjusted in the specified range. Their
|
||||
@@ -177,7 +301,7 @@ LAN could be
|
||||
server ntp.local minpoll 2 maxpoll 4 polltarget 30
|
||||
----
|
||||
|
||||
The maxdelay options are useful to ignore measurements with an unusally large
|
||||
The maxdelay options are useful to ignore measurements with an unusually large
|
||||
delay (e.g. due to congestion in the network) and improve the stability of the
|
||||
synchronisation. The `maxdelaydevratio` option could be added to the example
|
||||
with local NTP server
|
||||
@@ -187,8 +311,8 @@ server ntp.local minpoll 2 maxpoll 4 polltarget 30 maxdelaydevratio 2
|
||||
----
|
||||
|
||||
If your server supports the interleaved mode (e.g. it is running `chronyd`),
|
||||
the `xleave` option should be added to the `server` directive in order to allow
|
||||
the server to send the client more accurate transmit timestamps (kernel or
|
||||
the `xleave` option should be added to the `server` directive to enable the
|
||||
server to provide the client with more accurate transmit timestamps (kernel or
|
||||
preferably hardware). For example:
|
||||
|
||||
----
|
||||
@@ -221,6 +345,24 @@ server ntp.local minpoll -6 maxpoll -6 filter 15 xleave
|
||||
hwtimestamp eth0 minpoll -6
|
||||
----
|
||||
|
||||
Since `chrony` version 4.3, the minimum `minpoll` is -7 and a filter using a
|
||||
long-term estimate of a delay quantile can be enabled by the `maxdelayquant`
|
||||
option to replace the default `maxdelaydevratio` filter, which is sensitive to
|
||||
outliers corrupting the minimum delay. For example:
|
||||
|
||||
----
|
||||
server ntp.local minpoll -7 maxpoll -7 filter 31 maxdelayquant 0.3 xleave
|
||||
----
|
||||
|
||||
As an experimental feature added in version 4.2, `chronyd` supports an NTPv4
|
||||
extension field containing an additional timestamp to enable frequency transfer
|
||||
and significantly improve stability of synchronisation. It can be enabled by
|
||||
the `extfield F323` option. For example:
|
||||
|
||||
----
|
||||
server ntp.local minpoll 0 maxpoll 0 xleave extfield F323
|
||||
----
|
||||
|
||||
=== Does `chronyd` have an ntpdate mode?
|
||||
|
||||
Yes. With the `-q` option `chronyd` will set the system clock once and exit.
|
||||
@@ -237,7 +379,7 @@ well synchronised and responding to all requests. If not synchronised or
|
||||
responding, it would take about 10 seconds for `chronyd` to give up and exit
|
||||
with a non-zero status. A faster configuration is possible. A single server can
|
||||
be used instead of four servers, the number of measurements can be reduced with
|
||||
the `maxsamples` option to one (supported in `chrony` version 4.0), and a
|
||||
the `maxsamples` option to one (supported since `chrony` version 4.0), and a
|
||||
timeout can be specified with the `-t` option. The following command would take
|
||||
only up to about one second.
|
||||
|
||||
@@ -284,18 +426,49 @@ and be unsuitable for monitoring.
|
||||
|
||||
=== Can NTP server be separated from NTP client?
|
||||
|
||||
Yes, it is possible to run multiple instances of `chronyd` on the same
|
||||
computer. One can be configured as an NTP client, and another as a server. They
|
||||
need to use different pidfiles, NTP ports, command ports, and Unix domain
|
||||
command sockets. The server instance should be started with the `-x` option to
|
||||
avoid touching the clock. It can be configured to serve the system time with
|
||||
the `local` directive, or synchronise its NTP clock to the client instance
|
||||
running on localhost using a non-standard NTP port.
|
||||
Yes, it is possible to run multiple instances of `chronyd` on a computer at the
|
||||
same time. One can operate primarily as an NTP client to synchronise the system
|
||||
clock and another as a server for other computers. If they use the same
|
||||
filesystem, they need to be configured with different pidfiles, Unix domain
|
||||
command sockets, and any other file or directory specified in the configuration
|
||||
file. If they run in the same network namespace, they need to use different NTP
|
||||
and command ports, or bind the ports to different addresses or interfaces.
|
||||
|
||||
On Linux, starting with `chrony` version 4.0, it is also possible to run
|
||||
multiple server instances sharing a port to utilise multiple cores of the CPU.
|
||||
Note that the client/server interleaved mode requires that all packets from an
|
||||
address are handled by the same server instance.
|
||||
The server instance should be started with the `-x` option to prevent it from
|
||||
adjusting the system clock and interfering with the client instance. It can be
|
||||
configured as a client to synchronise its NTP clock to other servers, or the
|
||||
client instance running on the same computer. In the latter case, the `copy`
|
||||
option (added in `chrony` version 4.1) can be used to assume the reference ID
|
||||
and stratum of the client instance, which enables detection of synchronisation
|
||||
loops with its own clients.
|
||||
|
||||
On Linux, starting with `chrony` version 4.0, it is possible to run multiple
|
||||
server instances sharing a port to better utilise multiple cores of the CPU.
|
||||
Note that for rate limiting and client/server interleaved mode to work well
|
||||
it is necessary that all packets received from the same address are handled by
|
||||
the same server instance.
|
||||
|
||||
An example configuration of the client instance could be
|
||||
|
||||
----
|
||||
pool pool.ntp.org iburst
|
||||
allow 127.0.0.1
|
||||
port 11123
|
||||
driftfile /var/lib/chrony/drift
|
||||
makestep 1 3
|
||||
rtcsync
|
||||
----
|
||||
|
||||
and configuration of the first server instance could be
|
||||
|
||||
----
|
||||
server 127.0.0.1 port 11123 minpoll 0 maxpoll 0 copy
|
||||
allow
|
||||
cmdport 11323
|
||||
bindcmdaddress /var/run/chrony/chronyd-server1.sock
|
||||
pidfile /var/run/chronyd-server1.pid
|
||||
driftfile /var/lib/chrony/drift-server1
|
||||
----
|
||||
|
||||
=== Should be a leap smear enabled on NTP server?
|
||||
|
||||
@@ -308,12 +481,79 @@ e.g. when the clients cannot be configured to handle the leap seconds as
|
||||
needed, or their number is so large that configuring them all would be
|
||||
impractical. The clients should use only one leap-smearing server, or multiple
|
||||
identically configured leap-smearing servers. Note that some clients can get
|
||||
leap seconds from external sources (e.g. with the `leapsectz` directive in
|
||||
leap seconds from other sources (e.g. with the `leapsectz` directive in
|
||||
`chrony`) and they will not work correctly with a leap smearing server.
|
||||
|
||||
=== How should `chronyd` be configuration with `gpsd`?
|
||||
|
||||
A GPS or other GNSS receiver can be used as a reference clock with `gpsd`. It
|
||||
can work as one or two separate time sources for each connected receiver. The
|
||||
first time source is based on timestamping of messages sent by the receiver.
|
||||
Typically, it is accurate to milliseconds. The other source is much more
|
||||
accurate. It is timestamping a pulse-per-second (PPS) signal, usually connected
|
||||
to a serial port (e.g. DCD pin) or GPIO pin.
|
||||
|
||||
If the PPS signal is connected to the serial port which is receiving messages
|
||||
from the GPS/GNSS receiver, `gpsd` should detect and use it automatically. If
|
||||
it is connected to a GPIO pin, or another serial port, the PPS device needs to
|
||||
be specified on the command line as an additional data source. On Linux, the
|
||||
`ldattach` utility can be used to create a PPS device for a serial device.
|
||||
|
||||
The PPS-based time source provided by `gpsd` is available as a `SHM 1`
|
||||
refclock, or other odd number if `gpsd` is configured with multiple receivers,
|
||||
and also as `SOCK /var/run/chrony.DEV.sock` where `DEV` is the name of the
|
||||
serial device (e.g. ttyS0).
|
||||
|
||||
The message-based time source is available as a `SHM 0` refclock (or other even
|
||||
number) and since `gpsd` version 3.25 also as
|
||||
`SOCK /var/run/chrony.clk.DEV.sock` where `DEV` is the name of the serial
|
||||
device.
|
||||
|
||||
The SOCK refclocks should be preferred over SHM for better security
|
||||
(the shared memory segment needs to be created by `chronyd` or `gpsd` with an
|
||||
expected owner and permissions before an untrusted application or user has a
|
||||
chance to create its own in order to feed `chronyd` with false measurements).
|
||||
`gpsd` needs to be started after `chronyd` in order to connect to the socket.
|
||||
|
||||
With `chronyd` and `gpsd` both supporting PPS, there are two different
|
||||
recommended configurations:
|
||||
|
||||
----
|
||||
# First option
|
||||
refclock SOCK /var/run/chrony.ttyS0.sock refid GPS
|
||||
|
||||
# Second option
|
||||
refclock PPS /dev/pps0 lock NMEA refid GPS
|
||||
refclock SOCK /var/run/chrony.clk.ttyS0.sock offset 0.5 delay 0.1 refid NMEA noselect
|
||||
----
|
||||
|
||||
They both have some advantages:
|
||||
|
||||
* `SOCK` can be more accurate than `PPS` if `gpsd` corrects for the
|
||||
sawtooth error provided by the receiver in serial data
|
||||
* `PPS` can be used with higher PPS rates (specified by the `rate` option),
|
||||
but it requires a second refclock or another time source to pair pulses
|
||||
with seconds, and the `SOCK` offset needs to be specified
|
||||
<<using-pps-refclock,correctly>> to compensate for the message delay, while
|
||||
`gpsd` can apply HW-specific information
|
||||
|
||||
If the PPS signal is not available, or cannot be used for some reason, the only
|
||||
option is the message-based timing
|
||||
|
||||
----
|
||||
refclock SOCK /var/run/chrony.clk.ttyS0.sock offset 0.5 delay 0.1 refid GPS
|
||||
----
|
||||
|
||||
or the SHM equivalent if using `gpsd` version before 3.25
|
||||
|
||||
----
|
||||
refclock SHM 0 offset 0.5 delay 0.1 refid GPS
|
||||
----
|
||||
|
||||
=== Does `chrony` support PTP?
|
||||
|
||||
No, the Precision Time Protocol (PTP) is not supported and there are no plans
|
||||
No, the Precision Time Protocol (PTP) is not supported as a protocol for
|
||||
synchronisation of clocks and there are no plans
|
||||
to support it. It is a complex protocol, which shares some issues with the
|
||||
NTP broadcast mode. One of the main differences between NTP and PTP is that PTP
|
||||
was designed to be easily supported in hardware (e.g. network switches and
|
||||
@@ -328,6 +568,32 @@ packets (enabled by the `hwtimestamp` directive) if the NIC can timestamp other
|
||||
packets than PTP, which is usually the case at least for transmitted packets.
|
||||
The `ethtool -T` command can be used to verify the timestamping support.
|
||||
|
||||
As an experimental feature added in version 4.2, `chrony` can use PTP as a
|
||||
transport for NTP messages (NTP over PTP) to enable hardware timestamping on
|
||||
hardware which can timestamp PTP packets only. It can be enabled by the
|
||||
`ptpport` directive.
|
||||
|
||||
=== Why are client log records dropped before reaching `clientloglimit`?
|
||||
|
||||
The number of dropped client log records reported by the `serverstats` command
|
||||
can be increasing before the number of clients reported by the `clients` command
|
||||
reaches the maximum value corresponding to the memory limit set by the
|
||||
`clientloglimit` directive.
|
||||
|
||||
This is due to the design of the data structure keeping the client records. It
|
||||
is a hash table which can store only up to 16 colliding addresses per slot. If
|
||||
a slot has more collisions and the table already has the maximum size, the
|
||||
oldest record will be dropped and replaced by the new client.
|
||||
|
||||
Note that the size of the table is always a power of two and it can only grow.
|
||||
The limit set by the `clientloglimit` directive takes into account that two
|
||||
copies of the table exist when it is being resized. This means the actual
|
||||
memory usage reported by `top` and other utilities can be significantly smaller
|
||||
than the limit even when the maximum number of records is used.
|
||||
|
||||
The absolute maximum number of client records kept at the same time is
|
||||
16777216.
|
||||
|
||||
=== What happened to the `commandkey` and `generatecommandkey` directives?
|
||||
|
||||
They were removed in version 2.2. Authentication is no longer supported in the
|
||||
@@ -347,15 +613,15 @@ following questions.
|
||||
Check the `Reach` value printed by the ``chronyc``'s `sources` command. If it
|
||||
is zero, it means `chronyd` did not get any valid responses from the NTP server
|
||||
you are trying to use. If there is a firewall between you and the server, the
|
||||
packets might be blocked. Try using a tool like `wireshark` or `tcpdump` to see
|
||||
if you are getting any responses from the server.
|
||||
requests sent to the UDP port 123 of the server or responses sent back from
|
||||
the port might be blocked. Try using a tool like `wireshark` or `tcpdump` to
|
||||
see if you are getting any responses from the server.
|
||||
|
||||
When `chronyd` is receiving responses from the servers, the output of the
|
||||
`sources` command issued few minutes after `chronyd` start might look like
|
||||
this:
|
||||
|
||||
----
|
||||
210 Number of sources = 3
|
||||
MS Name/IP address Stratum Poll Reach LastRx Last sample
|
||||
===============================================================================
|
||||
^* foo.example.net 2 6 377 34 +484us[ -157us] +/- 30ms
|
||||
@@ -379,6 +645,19 @@ example:
|
||||
0 sources with unknown address
|
||||
----
|
||||
|
||||
=== Is name resolution working correctly?
|
||||
|
||||
NTP servers specified by their hostname (instead of an IP address) have to have
|
||||
their names resolved before `chronyd` can send any requests to them. If the
|
||||
`activity` command prints a non-zero number of sources with unknown address,
|
||||
there is an issue with the resolution. Typically, a DNS server is specified in
|
||||
_/etc/resolv.conf_. Make sure it is working correctly.
|
||||
|
||||
Since `chrony` version 4.0, you can run `chronyc -N sources -a` command to
|
||||
print all sources, even those that do not have a known address yet, with their
|
||||
names as they were specified in the configuration. This can be useful to verify
|
||||
that the names specified in the configuration are used as expected.
|
||||
|
||||
=== Is `chronyd` allowed to step the system clock?
|
||||
|
||||
By default, `chronyd` adjusts the clock gradually by slowing it down or
|
||||
@@ -405,6 +684,62 @@ to
|
||||
makestep 1 -1
|
||||
----
|
||||
|
||||
=== Using NTS?
|
||||
|
||||
The Network Time Security (NTS) mechanism uses Transport Layer Security (TLS)
|
||||
to establish the keys needed for authentication of NTP packets.
|
||||
|
||||
Run the `authdata` command to check whether the key establishment was
|
||||
successful:
|
||||
|
||||
----
|
||||
# chronyc -N authdata
|
||||
Name/IP address Mode KeyID Type KLen Last Atmp NAK Cook CLen
|
||||
=========================================================================
|
||||
foo.example.net NTS 1 15 256 33m 0 0 8 100
|
||||
bar.example.net NTS 1 15 256 33m 0 0 8 100
|
||||
baz.example.net NTS 1 15 256 33m 0 0 8 100
|
||||
----
|
||||
|
||||
The KeyID, Type, and KLen columns should have non-zero values. If they are
|
||||
zero, check the system log for error messages from `chronyd`. One possible
|
||||
cause of failure is a firewall blocking the client's connection to the server's
|
||||
TCP port 4460.
|
||||
|
||||
Another possible cause of failure is a certificate that is failing to verify
|
||||
because the client's clock is wrong. This is a chicken-and-egg problem with NTS.
|
||||
You might need to manually correct the date, or temporarily disable NTS, in
|
||||
order to get NTS working. If your computer has an RTC and it is backed up by a
|
||||
good battery, this operation should be needed only once, assuming the RTC will
|
||||
be set periodically with the `rtcsync` directive, or compensated with the
|
||||
`rtcfile` directive and the `-s` option.
|
||||
|
||||
If the computer does not have an RTC or battery, you can use the `-s` option
|
||||
without `rtcfile` directive to restore time of the last shutdown or reboot from
|
||||
the drift file. The clock will start behind the true time, but if the computer
|
||||
was not shut down for too long and the server's certificate was not renewed too
|
||||
close to its expiration, it should be sufficient for the time checks to
|
||||
succeed.
|
||||
|
||||
If you run your own server, you can use a self-signed certificate covering
|
||||
all dates where the client can start (e.g. years 1970-2100). The certificate
|
||||
needs to be installed on the client and specified with the `ntstrustedcerts`
|
||||
directive. The server can have multiple names and certificates. To avoid
|
||||
trusting a certificate for too long, a new certificate can be added to the
|
||||
server periodically (e.g. once per year) and the client can have the server
|
||||
name and trusted certificate updated automatically (e.g. using a package
|
||||
repository, or a cron script downloading the files directly from the server
|
||||
over HTTPS). A client that was shut down for years will still be able to
|
||||
synchronise its clock and perform the update as long as the server keeps
|
||||
the old certificate.
|
||||
|
||||
As a last resort, you can disable the time checks by the `nocerttimecheck`
|
||||
directive. This has some important security implications. To reduce the
|
||||
security risk, you can use the `nosystemcert` and `ntstrustedcerts` directives
|
||||
to disable the system's default trusted certificate authorities and trust only
|
||||
a minimal set of selected authorities needed to validate the certificates of
|
||||
used NTP servers.
|
||||
|
||||
=== Using a Windows NTP server?
|
||||
|
||||
A common issue with Windows NTP servers is that they report a very large root
|
||||
@@ -466,12 +801,14 @@ frequently, you can effectively disable the test by setting the
|
||||
`maxdelaydevratio` option to a very large value (e.g. 1000000), or speed up the
|
||||
recovery by increasing the clock error rate with the `maxclockerror` directive.
|
||||
|
||||
[[using-pps-refclock]]
|
||||
=== Using a PPS reference clock?
|
||||
|
||||
A pulse-per-second (PPS) reference clock requires a non-PPS time source to
|
||||
determine which second of UTC corresponds to each pulse. If it is another
|
||||
reference clock specified with the `lock` option in the `refclock` directive,
|
||||
the offset between the two reference clocks must be smaller than 0.2 seconds in
|
||||
the offset between the two reference clocks must be smaller than 0.4 seconds
|
||||
(0.2 seconds with `chrony` versions before 4.1) in
|
||||
order for the PPS reference clock to work. With NMEA reference clocks it is
|
||||
common to have a larger offset. It needs to be corrected with the `offset`
|
||||
option.
|
||||
@@ -490,7 +827,7 @@ foo.example.net 7 3 200 -2.991 16.141 -107us 492us
|
||||
|
||||
the offset of the NMEA source would need to be increased by about 0.504
|
||||
seconds. It does not have to be very accurate. As long as the offset of the
|
||||
NMEA reference clock stays below 0.2 seconds, the PPS reference clock should be
|
||||
NMEA reference clock stays below the limit, the PPS reference clock should be
|
||||
able to determine the seconds corresponding to the pulses and allow the samples
|
||||
to be used for synchronisation.
|
||||
|
||||
@@ -546,6 +883,10 @@ in parentheses) on the `Reference ID` line.
|
||||
Only by the source code. See _cmdmon.c_ (`chronyd` side) and _client.c_
|
||||
(`chronyc` side).
|
||||
|
||||
Note that this protocol is not compatible with the mode 6 or mode 7 protocol
|
||||
supported by `ntpd`, i.e. the `ntpq` or `ntpdc` utility cannot be used to
|
||||
monitor `chronyd`, and `chronyc` cannot be used to monitor `ntpd`.
|
||||
|
||||
== Real-time clock issues
|
||||
|
||||
=== What is the real-time clock (RTC)?
|
||||
@@ -556,7 +897,7 @@ more than few seconds per day.
|
||||
|
||||
There are two approaches how `chronyd` can work with it. One is to use the
|
||||
`rtcsync` directive, which tells `chronyd` to enable a kernel mode which sets
|
||||
the RTC from the system clock every 11 minutes. `chronyd` itself won't touch
|
||||
the RTC from the system clock every 11 minutes. `chronyd` itself will not touch
|
||||
the RTC. If the computer is not turned off for a long time, the RTC should
|
||||
still be close to the true time when the system clock will be initialised from
|
||||
it on the next boot.
|
||||
@@ -571,12 +912,12 @@ it is not strictly necessary if its only purpose is to set the system clock when
|
||||
|
||||
=== Does `hwclock` have to be disabled?
|
||||
|
||||
The `hwclock` program is often set-up by default in the boot and shutdown
|
||||
scripts with many Linux installations. With the kernel RTC synchronisation
|
||||
The `hwclock` program is run by default in the boot and/or shutdown
|
||||
scripts in some Linux installations. With the kernel RTC synchronisation
|
||||
(`rtcsync` directive), the RTC will be set also every 11 minutes as long as the
|
||||
system clock is synchronised. If you want to use ``chronyd``'s RTC monitoring
|
||||
(`rtcfile` directive), it is important to disable `hwclock` in the shutdown
|
||||
procedure. If you do not that, it will over-write the RTC with a new value, unknown
|
||||
procedure. If you do not do that, it will overwrite the RTC with a new value, unknown
|
||||
to `chronyd`. At the next reboot, `chronyd` started with the `-s` option will
|
||||
compensate this (wrong) time with its estimate of how far the RTC has drifted
|
||||
whilst the power was off, giving a meaningless initial system time.
|
||||
@@ -597,6 +938,19 @@ things
|
||||
|
||||
Some other program running on the system might be using the device.
|
||||
|
||||
=== When I start `chronyd`, the log says `Could not enable RTC interrupt : Invalid argument` (or it may say `disable`)
|
||||
|
||||
Your real-time clock hardware might not support the required ioctl requests:
|
||||
|
||||
* `RTC_UIE_ON`
|
||||
* `RTC_UIE_OFF`
|
||||
|
||||
A possible solution could be to build the Linux kernel with support for software
|
||||
emulation instead; try enabling the following configuration option when building
|
||||
the Linux kernel:
|
||||
|
||||
* `CONFIG_RTC_INTF_DEV_UIE_EMUL`
|
||||
|
||||
=== What if my computer does not have an RTC or backup battery?
|
||||
|
||||
In this case you can still use the `-s` option to set the system clock to the
|
||||
@@ -653,11 +1007,39 @@ When two computers are synchronised to each other using the client/server or
|
||||
symmetric NTP mode, there is an expectation that NTP measurements between the
|
||||
two computers made on both ends show an average offset close to zero.
|
||||
|
||||
With `chronyd` that can be expected only when the interleaved mode (`xleave`
|
||||
option) is enabled. Otherwise, `chronyd` will use different transmit timestamps
|
||||
(e.g. daemon timestamp vs kernel timestamp) for serving time and
|
||||
synchronisation of its own clock, which creates an asymmetry in the
|
||||
timestamping and causes the other end to measure a significant offset.
|
||||
With `chronyd` that can be expected only when the interleaved mode is enabled
|
||||
by the `xleave` option. Otherwise, `chronyd` will use different transmit
|
||||
timestamps (e.g. daemon timestamp vs kernel timestamp) for serving time and
|
||||
synchronisation of its own clock, which will cause the other computer to
|
||||
measure a significant offset.
|
||||
|
||||
== Operation
|
||||
|
||||
=== What clocks does `chronyd` use?
|
||||
|
||||
There are several different clocks used by `chronyd`:
|
||||
|
||||
* *System clock:* software clock maintained by the kernel. It is the main clock
|
||||
used by applications running on the computer. It is synchronised by `chronyd`
|
||||
to its NTP clock, unless started with the *-x* option.
|
||||
* *NTP clock:* software clock (virtual) based on the system clock and internal
|
||||
to `chronyd`. It keeps the best estimate of the true time according to the
|
||||
configured time sources, which is served to NTP clients unless time smoothing
|
||||
is enabled by the *smoothtime* directive. The *System time* value in the
|
||||
`tracking` report is the current offset between the system and NTP clock.
|
||||
* *Real-time clock (RTC):* hardware clock keeping time even when the
|
||||
computer is turned off. It is used by the kernel to initialise the system
|
||||
clock on boot and also by `chronyd` to compensate for its measured drift if
|
||||
configured with the `rtcfile` directive and started with the `-s` option.
|
||||
The clock can be kept accurate only by stepping enabled by the `rtcsync` or
|
||||
`rtcautotrim` directive.
|
||||
* *Reference clock:* hardware clock used as a time source. It is specified by
|
||||
the `refclock` directive.
|
||||
* *NIC clock (also known as PTP hardware clock):* hardware clock timestamping
|
||||
packets received and transmitted by a network device specified by the
|
||||
*hwtimestamp* directive. The clock is expected to be running free. It is not
|
||||
synchronised by `chronyd`. Its offset is tracked relative to the NTP clock in
|
||||
order to convert the hardware timestamps.
|
||||
|
||||
== Operating systems
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ The following libraries with their development files, and programs, are needed
|
||||
to enable optional features:
|
||||
|
||||
* pkg-config: detection of development libraries
|
||||
* Nettle, NSS, or LibTomCrypt: secure hash functions (`SECHASH`)
|
||||
* Nettle, GnuTLS, NSS, or LibTomCrypt: secure hash functions (`SECHASH`)
|
||||
* libcap: dropping root privileges on Linux (`DROPROOT`)
|
||||
* libseccomp: system call filter on Linux (`SCFILTER`)
|
||||
* GnuTLS and Nettle: Network Time Security (`NTS`)
|
||||
|
||||
@@ -16,5 +16,32 @@ TimeoutStartSec=180
|
||||
RemainAfterExit=yes
|
||||
StandardOutput=null
|
||||
|
||||
CapabilityBoundingSet=
|
||||
DevicePolicy=closed
|
||||
DynamicUser=yes
|
||||
IPAddressAllow=localhost
|
||||
IPAddressDeny=any
|
||||
LockPersonality=yes
|
||||
MemoryDenyWriteExecute=yes
|
||||
PrivateDevices=yes
|
||||
PrivateUsers=yes
|
||||
ProcSubset=pid
|
||||
ProtectClock=yes
|
||||
ProtectControlGroups=yes
|
||||
ProtectHome=yes
|
||||
ProtectHostname=yes
|
||||
ProtectKernelLogs=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectKernelTunables=yes
|
||||
ProtectProc=invisible
|
||||
ProtectSystem=strict
|
||||
RestrictAddressFamilies=AF_INET AF_INET6
|
||||
RestrictNamespaces=yes
|
||||
RestrictRealtime=yes
|
||||
SystemCallArchitectures=native
|
||||
SystemCallFilter=@system-service
|
||||
SystemCallFilter=~@privileged @resources
|
||||
UMask=0777
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
@@ -11,3 +11,5 @@
|
||||
#1 MD5 AVeryLongAndRandomPassword
|
||||
#2 MD5 HEX:12114855C7931009B4049EF3EFC48A139C3F989F
|
||||
#3 SHA1 HEX:B2159C05D6A219673A3B7E896B6DE07F6A440995
|
||||
#4 AES128 HEX:2DA837C4B6573748CA692B8C828E4891
|
||||
#5 AES256 HEX:2666B8099BFF2D5BA20876121788ED24D2BE59111B8FFB562F0F56AE6EC7246E
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#!/bin/sh
|
||||
# This is a NetworkManager dispatcher script for chronyd to update
|
||||
# its NTP sources passed from DHCP options. Note that this script is
|
||||
# specific to NetworkManager-dispatcher due to use of the
|
||||
# DHCP4_NTP_SERVERS environment variable.
|
||||
# its NTP sources with servers from DHCP options passed by NetworkManager
|
||||
# in the DHCP4_NTP_SERVERS and DHCP6_DHCP6_NTP_SERVERS environment variables.
|
||||
|
||||
export LC_ALL=C
|
||||
|
||||
@@ -10,17 +9,23 @@ interface=$1
|
||||
action=$2
|
||||
|
||||
chronyc=/usr/bin/chronyc
|
||||
default_server_options=iburst
|
||||
server_options=iburst
|
||||
server_dir=/var/run/chrony-dhcp
|
||||
|
||||
dhcp_server_file=$server_dir/$interface.sources
|
||||
# DHCP4_NTP_SERVERS is passed from DHCP options by NetworkManager.
|
||||
nm_dhcp_servers=$DHCP4_NTP_SERVERS
|
||||
dhcp_ntp_servers="$DHCP4_NTP_SERVERS $DHCP6_DHCP6_NTP_SERVERS"
|
||||
|
||||
add_servers_from_dhcp() {
|
||||
rm -f "$dhcp_server_file"
|
||||
for server in $nm_dhcp_servers; do
|
||||
echo "server $server $default_server_options" >> "$dhcp_server_file"
|
||||
for server in $dhcp_ntp_servers; do
|
||||
# Check for invalid characters (from the DHCPv6 NTP FQDN suboption)
|
||||
len1=$(printf '%s' "$server" | wc -c)
|
||||
len2=$(printf '%s' "$server" | tr -d -c 'A-Za-z0-9:.-' | wc -c)
|
||||
if [ "$len1" -ne "$len2" ] || [ "$len2" -lt 1 ] || [ "$len2" -gt 255 ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
printf 'server %s %s\n' "$server" "$server_options" >> "$dhcp_server_file"
|
||||
done
|
||||
$chronyc reload sources > /dev/null 2>&1 || :
|
||||
}
|
||||
@@ -34,10 +39,11 @@ clear_servers_from_dhcp() {
|
||||
|
||||
mkdir -p $server_dir
|
||||
|
||||
if [ "$action" = "up" ] || [ "$action" = "dhcp4-change" ]; then
|
||||
add_servers_from_dhcp
|
||||
elif [ "$action" = "down" ]; then
|
||||
clear_servers_from_dhcp
|
||||
fi
|
||||
case "$action" in
|
||||
up|dhcp4-change|dhcp6-change)
|
||||
add_servers_from_dhcp;;
|
||||
down)
|
||||
clear_servers_from_dhcp;;
|
||||
esac
|
||||
|
||||
exit 0
|
||||
|
||||
@@ -7,8 +7,20 @@ export LC_ALL=C
|
||||
|
||||
chronyc=/usr/bin/chronyc
|
||||
|
||||
# For NetworkManager consider only up/down events
|
||||
[ $# -ge 2 ] && [ "$2" != "up" ] && [ "$2" != "down" ] && exit 0
|
||||
# For NetworkManager consider only selected events
|
||||
if [ $# -ge 2 ]; then
|
||||
case "$2" in
|
||||
up|down|connectivity-change)
|
||||
;;
|
||||
dhcp4-change|dhcp6-change)
|
||||
# Actions "up" and "connectivity-change" in some cases do not
|
||||
# guarantee that the interface has a route (e.g. a bond).
|
||||
# dhcp(x)-change handles at least cases that use DHCP.
|
||||
;;
|
||||
*)
|
||||
exit 0;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
|
||||
|
||||
|
||||
59
examples/chronyd-restricted.service
Normal file
59
examples/chronyd-restricted.service
Normal file
@@ -0,0 +1,59 @@
|
||||
# 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.
|
||||
[Unit]
|
||||
Description=NTP client (restricted)
|
||||
Documentation=man:chronyd(8) man:chrony.conf(5)
|
||||
After=chronyd.service ntpdate.service sntp.service ntpd.service
|
||||
Conflicts=chronyd.service ntpd.service systemd-timesyncd.service
|
||||
ConditionCapability=CAP_SYS_TIME
|
||||
|
||||
[Service]
|
||||
Type=forking
|
||||
PIDFile=/run/chrony/chronyd.pid
|
||||
EnvironmentFile=-/etc/sysconfig/chronyd
|
||||
ExecStart=/usr/sbin/chronyd -U $OPTIONS
|
||||
|
||||
User=chrony
|
||||
LogsDirectory=chrony
|
||||
LogsDirectoryMode=0750
|
||||
RuntimeDirectory=chrony
|
||||
RuntimeDirectoryMode=0750
|
||||
RuntimeDirectoryPreserve=restart
|
||||
StateDirectory=chrony
|
||||
StateDirectoryMode=0750
|
||||
|
||||
AmbientCapabilities=CAP_SYS_TIME
|
||||
CapabilityBoundingSet=CAP_SYS_TIME
|
||||
DevicePolicy=closed
|
||||
LockPersonality=yes
|
||||
MemoryDenyWriteExecute=yes
|
||||
NoNewPrivileges=yes
|
||||
PrivateDevices=yes
|
||||
PrivateTmp=yes
|
||||
# This breaks adjtimex()
|
||||
#PrivateUsers=yes
|
||||
ProcSubset=pid
|
||||
ProtectControlGroups=yes
|
||||
ProtectHome=yes
|
||||
ProtectHostname=yes
|
||||
ProtectKernelLogs=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectKernelTunables=yes
|
||||
ProtectProc=invisible
|
||||
ProtectSystem=strict
|
||||
RemoveIPC=yes
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
RestrictNamespaces=yes
|
||||
RestrictRealtime=yes
|
||||
RestrictSUIDSGID=yes
|
||||
SystemCallArchitectures=native
|
||||
SystemCallFilter=~@cpu-emulation @debug @module @mount @obsolete @raw-io
|
||||
SystemCallFilter=~@reboot @resources @swap
|
||||
UMask=0077
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -10,9 +10,40 @@ Type=forking
|
||||
PIDFile=/run/chrony/chronyd.pid
|
||||
EnvironmentFile=-/etc/sysconfig/chronyd
|
||||
ExecStart=/usr/sbin/chronyd $OPTIONS
|
||||
|
||||
CapabilityBoundingSet=~CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_AUDIT_WRITE
|
||||
CapabilityBoundingSet=~CAP_BLOCK_SUSPEND CAP_KILL CAP_LEASE CAP_LINUX_IMMUTABLE
|
||||
CapabilityBoundingSet=~CAP_MAC_ADMIN CAP_MAC_OVERRIDE CAP_MKNOD CAP_SYS_ADMIN
|
||||
CapabilityBoundingSet=~CAP_SYS_BOOT CAP_SYS_CHROOT CAP_SYS_MODULE CAP_SYS_PACCT
|
||||
CapabilityBoundingSet=~CAP_SYS_PTRACE CAP_SYS_RAWIO CAP_SYS_TTY_CONFIG CAP_WAKE_ALARM
|
||||
DeviceAllow=char-pps rw
|
||||
DeviceAllow=char-ptp rw
|
||||
DeviceAllow=char-rtc rw
|
||||
DevicePolicy=closed
|
||||
LockPersonality=yes
|
||||
MemoryDenyWriteExecute=yes
|
||||
NoNewPrivileges=yes
|
||||
PrivateTmp=yes
|
||||
ProcSubset=pid
|
||||
ProtectControlGroups=yes
|
||||
ProtectHome=yes
|
||||
ProtectSystem=full
|
||||
ProtectHostname=yes
|
||||
ProtectKernelLogs=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectKernelTunables=yes
|
||||
ProtectProc=invisible
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/run /var/lib/chrony -/var/log
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
RestrictNamespaces=yes
|
||||
RestrictSUIDSGID=yes
|
||||
SystemCallArchitectures=native
|
||||
SystemCallFilter=~@cpu-emulation @debug @module @mount @obsolete @raw-io @reboot @swap
|
||||
|
||||
# Adjust restrictions for /usr/sbin/sendmail (mailonchange directive)
|
||||
NoNewPrivileges=no
|
||||
ReadWritePaths=-/var/spool
|
||||
RestrictAddressFamilies=AF_NETLINK
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
19
getdate.y
19
getdate.y
@@ -448,9 +448,9 @@ o_merid : /* NULL */
|
||||
the same signature as the function definition does. */
|
||||
#include "getdate.h"
|
||||
|
||||
extern struct tm *gmtime ();
|
||||
extern struct tm *localtime ();
|
||||
extern time_t mktime ();
|
||||
extern struct tm *gmtime (const time_t *timep);
|
||||
extern struct tm *localtime (const time_t *timep);
|
||||
extern time_t mktime (struct tm *tm);
|
||||
|
||||
/* Month and day table. */
|
||||
static TABLE const MonthDayTable[] = {
|
||||
@@ -641,16 +641,13 @@ static TABLE const MilitaryTable[] = {
|
||||
|
||||
/* ARGSUSED */
|
||||
static int
|
||||
yyerror (s)
|
||||
char *s ATTRIBUTE_UNUSED;
|
||||
yyerror (char *s ATTRIBUTE_UNUSED)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
ToHour (Hours, Meridian)
|
||||
int Hours;
|
||||
MERIDIAN Meridian;
|
||||
ToHour (int Hours, MERIDIAN Meridian)
|
||||
{
|
||||
switch (Meridian)
|
||||
{
|
||||
@@ -677,8 +674,7 @@ ToHour (Hours, Meridian)
|
||||
}
|
||||
|
||||
static int
|
||||
ToYear (Year)
|
||||
int Year;
|
||||
ToYear (int Year)
|
||||
{
|
||||
if (Year < 0)
|
||||
Year = -Year;
|
||||
@@ -694,8 +690,7 @@ ToYear (Year)
|
||||
}
|
||||
|
||||
static int
|
||||
LookupWord (buff)
|
||||
char *buff;
|
||||
LookupWord (char *buff)
|
||||
{
|
||||
register char *p;
|
||||
register char *q;
|
||||
|
||||
1
hash.h
1
hash.h
@@ -44,6 +44,7 @@ typedef enum {
|
||||
HSH_SHA3_512 = 9,
|
||||
HSH_TIGER = 10,
|
||||
HSH_WHIRLPOOL = 11,
|
||||
HSH_MD5_NONCRYPTO = 10000, /* For NTPv4 reference ID */
|
||||
} HSH_Algorithm;
|
||||
|
||||
extern int HSH_GetHashId(HSH_Algorithm algorithm);
|
||||
|
||||
145
hash_gnutls.c
Normal file
145
hash_gnutls.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Crypto hashing using the GnuTLS library
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <gnutls/crypto.h>
|
||||
|
||||
#include "hash.h"
|
||||
#include "logging.h"
|
||||
|
||||
struct hash {
|
||||
const HSH_Algorithm algorithm;
|
||||
const gnutls_digest_algorithm_t type;
|
||||
gnutls_hash_hd_t handle;
|
||||
};
|
||||
|
||||
static struct hash hashes[] = {
|
||||
{ HSH_MD5_NONCRYPTO, GNUTLS_DIG_MD5, NULL },
|
||||
{ HSH_MD5, GNUTLS_DIG_MD5, NULL },
|
||||
{ HSH_SHA1, GNUTLS_DIG_SHA1, NULL },
|
||||
{ HSH_SHA256, GNUTLS_DIG_SHA256, NULL },
|
||||
{ HSH_SHA384, GNUTLS_DIG_SHA384, NULL },
|
||||
{ HSH_SHA512, GNUTLS_DIG_SHA512, NULL },
|
||||
{ HSH_SHA3_224, GNUTLS_DIG_SHA3_224, NULL },
|
||||
{ HSH_SHA3_256, GNUTLS_DIG_SHA3_256, NULL },
|
||||
{ HSH_SHA3_384, GNUTLS_DIG_SHA3_384, NULL },
|
||||
{ HSH_SHA3_512, GNUTLS_DIG_SHA3_512, NULL },
|
||||
{ 0, 0, NULL }
|
||||
};
|
||||
|
||||
static int gnutls_initialised = 0;
|
||||
|
||||
int
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int id, r;
|
||||
|
||||
if (!gnutls_initialised) {
|
||||
r = gnutls_global_init();
|
||||
if (r < 0)
|
||||
LOG_FATAL("Could not initialise %s : %s", "gnutls", gnutls_strerror(r));
|
||||
gnutls_initialised = 1;
|
||||
}
|
||||
|
||||
for (id = 0; hashes[id].algorithm != 0; id++) {
|
||||
if (hashes[id].algorithm == algorithm)
|
||||
break;
|
||||
}
|
||||
|
||||
if (hashes[id].algorithm == 0)
|
||||
return -1;
|
||||
|
||||
if (hashes[id].handle)
|
||||
return id;
|
||||
|
||||
if (algorithm == HSH_MD5_NONCRYPTO)
|
||||
GNUTLS_FIPS140_SET_LAX_MODE();
|
||||
|
||||
r = gnutls_hash_init(&hashes[id].handle, hashes[id].type);
|
||||
|
||||
if (algorithm == HSH_MD5_NONCRYPTO)
|
||||
GNUTLS_FIPS140_SET_STRICT_MODE();
|
||||
|
||||
if (r < 0) {
|
||||
DEBUG_LOG("Could not initialise %s : %s", "hash", gnutls_strerror(r));
|
||||
hashes[id].handle = NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
int
|
||||
HSH_Hash(int id, const void *in1, int in1_len, const void *in2, int in2_len,
|
||||
unsigned char *out, int out_len)
|
||||
{
|
||||
unsigned char buf[MAX_HASH_LENGTH];
|
||||
gnutls_hash_hd_t handle;
|
||||
int hash_len;
|
||||
|
||||
if (in1_len < 0 || in2_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
handle = hashes[id].handle;
|
||||
hash_len = gnutls_hash_get_len(hashes[id].type);
|
||||
|
||||
if (out_len > hash_len)
|
||||
out_len = hash_len;
|
||||
|
||||
if (hash_len > sizeof (buf))
|
||||
return 0;
|
||||
|
||||
if (gnutls_hash(handle, in1, in1_len) < 0 ||
|
||||
(in2 && gnutls_hash(handle, in2, in2_len) < 0)) {
|
||||
/* Reset the state */
|
||||
gnutls_hash_output(handle, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
gnutls_hash_output(handle, buf);
|
||||
memcpy(out, buf, out_len);
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
void
|
||||
HSH_Finalise(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!gnutls_initialised)
|
||||
return;
|
||||
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].handle)
|
||||
gnutls_hash_deinit(hashes[i].handle, NULL);
|
||||
}
|
||||
|
||||
gnutls_global_deinit();
|
||||
}
|
||||
@@ -39,7 +39,7 @@ int
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
/* only MD5 is supported */
|
||||
if (algorithm != HSH_MD5)
|
||||
if (algorithm != HSH_MD5 && algorithm != HSH_MD5_NONCRYPTO)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -59,6 +59,9 @@ HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int id, nid;
|
||||
|
||||
if (algorithm == HSH_MD5_NONCRYPTO)
|
||||
algorithm = HSH_MD5;
|
||||
|
||||
for (id = 0; hashes[id].algorithm != 0; id++) {
|
||||
if (hashes[id].algorithm == algorithm)
|
||||
break;
|
||||
|
||||
@@ -56,6 +56,9 @@ HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (algorithm == HSH_MD5_NONCRYPTO)
|
||||
algorithm = HSH_MD5;
|
||||
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].algorithm == algorithm)
|
||||
break;
|
||||
|
||||
@@ -71,6 +71,9 @@ HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int i, h;
|
||||
|
||||
if (algorithm == HSH_MD5_NONCRYPTO)
|
||||
algorithm = HSH_MD5;
|
||||
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].algorithm == algorithm)
|
||||
break;
|
||||
|
||||
111
hwclock.c
111
hwclock.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2016-2018
|
||||
* Copyright (C) Miroslav Lichvar 2016-2018, 2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "quantiles.h"
|
||||
#include "regress.h"
|
||||
#include "util.h"
|
||||
|
||||
@@ -43,6 +44,13 @@
|
||||
/* Maximum acceptable frequency offset of the clock */
|
||||
#define MAX_FREQ_OFFSET (2.0 / 3.0)
|
||||
|
||||
/* Quantiles for filtering readings by delay */
|
||||
#define DELAY_QUANT_MIN_K 1
|
||||
#define DELAY_QUANT_MAX_K 2
|
||||
#define DELAY_QUANT_Q 10
|
||||
#define DELAY_QUANT_REPEAT 7
|
||||
#define DELAY_QUANT_MIN_STEP 1.0e-9
|
||||
|
||||
struct HCL_Instance_Record {
|
||||
/* HW and local reference timestamp */
|
||||
struct timespec hw_ref;
|
||||
@@ -64,12 +72,18 @@ struct HCL_Instance_Record {
|
||||
/* Minimum interval between samples */
|
||||
double min_separation;
|
||||
|
||||
/* Expected precision of readings */
|
||||
double precision;
|
||||
|
||||
/* Flag indicating the offset and frequency values are valid */
|
||||
int valid_coefs;
|
||||
|
||||
/* Estimated offset and frequency of HW clock relative to local clock */
|
||||
double offset;
|
||||
double frequency;
|
||||
|
||||
/* Estimated quantiles of reading delay */
|
||||
QNT_Instance delay_quants;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -92,7 +106,7 @@ handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
/* ================================================== */
|
||||
|
||||
HCL_Instance
|
||||
HCL_CreateInstance(int min_samples, int max_samples, double min_separation)
|
||||
HCL_CreateInstance(int min_samples, int max_samples, double min_separation, double precision)
|
||||
{
|
||||
HCL_Instance clock;
|
||||
|
||||
@@ -110,6 +124,10 @@ HCL_CreateInstance(int min_samples, int max_samples, double min_separation)
|
||||
clock->n_samples = 0;
|
||||
clock->valid_coefs = 0;
|
||||
clock->min_separation = min_separation;
|
||||
clock->precision = precision;
|
||||
clock->delay_quants = QNT_CreateInstance(DELAY_QUANT_MIN_K, DELAY_QUANT_MAX_K,
|
||||
DELAY_QUANT_Q, DELAY_QUANT_REPEAT,
|
||||
DELAY_QUANT_MIN_STEP);
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_slew, clock);
|
||||
|
||||
@@ -121,6 +139,7 @@ HCL_CreateInstance(int min_samples, int max_samples, double min_separation)
|
||||
void HCL_DestroyInstance(HCL_Instance clock)
|
||||
{
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, clock);
|
||||
QNT_DestroyInstance(clock->delay_quants);
|
||||
Free(clock->y_data);
|
||||
Free(clock->x_data);
|
||||
Free(clock);
|
||||
@@ -140,6 +159,94 @@ HCL_NeedsNewSample(HCL_Instance clock, struct timespec *now)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
HCL_ProcessReadings(HCL_Instance clock, int n_readings, struct timespec tss[][3],
|
||||
struct timespec *hw_ts, struct timespec *local_ts, double *err)
|
||||
{
|
||||
double delay, raw_delay, min_delay, low_delay, high_delay, e, pred_err;
|
||||
double delay_sum, hw_sum, local_sum, local_prec, freq;
|
||||
int i, min_reading, combined;
|
||||
struct timespec ts1, ts2;
|
||||
|
||||
if (n_readings < 1)
|
||||
return 0;
|
||||
|
||||
/* Work out the current correction multiplier needed to get cooked delays */
|
||||
LCL_CookTime(&tss[0][0], &ts1, NULL);
|
||||
LCL_CookTime(&tss[n_readings - 1][2], &ts2, NULL);
|
||||
if (UTI_CompareTimespecs(&tss[0][0], &tss[n_readings - 1][2]) < 0)
|
||||
freq = UTI_DiffTimespecsToDouble(&ts1, &ts2) /
|
||||
UTI_DiffTimespecsToDouble(&tss[0][0], &tss[n_readings - 1][2]);
|
||||
else
|
||||
freq = 1.0;
|
||||
|
||||
for (i = 0; i < n_readings; i++) {
|
||||
delay = freq * UTI_DiffTimespecsToDouble(&tss[i][2], &tss[i][0]);
|
||||
|
||||
if (delay < 0.0) {
|
||||
/* Step in the middle of a reading? */
|
||||
DEBUG_LOG("Bad reading delay=%e", delay);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (i == 0 || min_delay > delay) {
|
||||
min_delay = delay;
|
||||
min_reading = i;
|
||||
}
|
||||
|
||||
QNT_Accumulate(clock->delay_quants, delay);
|
||||
}
|
||||
|
||||
local_prec = LCL_GetSysPrecisionAsQuantum();
|
||||
|
||||
low_delay = QNT_GetQuantile(clock->delay_quants, DELAY_QUANT_MIN_K);
|
||||
high_delay = QNT_GetQuantile(clock->delay_quants, DELAY_QUANT_MAX_K);
|
||||
low_delay = MIN(low_delay, high_delay);
|
||||
high_delay = MAX(high_delay, low_delay + local_prec);
|
||||
|
||||
/* Combine readings with delay in the expected interval */
|
||||
for (i = combined = 0, delay_sum = hw_sum = local_sum = 0.0; i < n_readings; i++) {
|
||||
raw_delay = UTI_DiffTimespecsToDouble(&tss[i][2], &tss[i][0]);
|
||||
delay = freq * raw_delay;
|
||||
|
||||
if (delay < low_delay || delay > high_delay)
|
||||
continue;
|
||||
|
||||
delay_sum += delay;
|
||||
hw_sum += UTI_DiffTimespecsToDouble(&tss[i][1], &tss[0][1]);
|
||||
local_sum += UTI_DiffTimespecsToDouble(&tss[i][0], &tss[0][0]) + raw_delay / 2.0;
|
||||
combined++;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Combined %d readings lo=%e hi=%e", combined, low_delay, high_delay);
|
||||
|
||||
if (combined > 0) {
|
||||
UTI_AddDoubleToTimespec(&tss[0][1], hw_sum / combined, hw_ts);
|
||||
UTI_AddDoubleToTimespec(&tss[0][0], local_sum / combined, local_ts);
|
||||
*err = MAX(delay_sum / combined / 2.0, clock->precision);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Accept the reading with minimum delay if its interval does not contain
|
||||
the current offset predicted from previous samples */
|
||||
|
||||
*hw_ts = tss[min_reading][1];
|
||||
UTI_AddDoubleToTimespec(&tss[min_reading][0], min_delay / freq / 2.0, local_ts);
|
||||
*err = MAX(min_delay / 2.0, clock->precision);
|
||||
|
||||
pred_err = 0.0;
|
||||
LCL_CookTime(local_ts, &ts1, NULL);
|
||||
if (!HCL_CookTime(clock, hw_ts, &ts2, &e) ||
|
||||
((pred_err = UTI_DiffTimespecsToDouble(&ts1, &ts2)) > *err)) {
|
||||
DEBUG_LOG("Accepted reading err=%e prerr=%e", *err, pred_err);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
struct timespec *local_ts, double err)
|
||||
|
||||
@@ -30,7 +30,7 @@ typedef struct HCL_Instance_Record *HCL_Instance;
|
||||
|
||||
/* Create a new HW clock instance */
|
||||
extern HCL_Instance HCL_CreateInstance(int min_samples, int max_samples,
|
||||
double min_separation);
|
||||
double min_separation, double precision);
|
||||
|
||||
/* Destroy a HW clock instance */
|
||||
extern void HCL_DestroyInstance(HCL_Instance clock);
|
||||
@@ -38,6 +38,11 @@ extern void HCL_DestroyInstance(HCL_Instance clock);
|
||||
/* Check if a new sample should be accumulated at this time */
|
||||
extern int HCL_NeedsNewSample(HCL_Instance clock, struct timespec *now);
|
||||
|
||||
/* Process new readings of the HW clock in form of (sys, hw, sys) triplets and
|
||||
produce a sample which can be accumulated */
|
||||
extern int HCL_ProcessReadings(HCL_Instance clock, int n_readings, struct timespec tss[][3],
|
||||
struct timespec *hw_ts, struct timespec *local_ts, double *err);
|
||||
|
||||
/* Accumulate a new sample */
|
||||
extern void HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
struct timespec *local_ts, double err);
|
||||
|
||||
5
keys.c
5
keys.c
@@ -182,6 +182,9 @@ KEY_Reload(void)
|
||||
if (!key_file)
|
||||
return;
|
||||
|
||||
if (!UTI_CheckFilePermissions(key_file, 0771))
|
||||
;
|
||||
|
||||
in = UTI_OpenFile(NULL, key_file, NULL, 'r', 0);
|
||||
if (!in) {
|
||||
LOG(LOGS_WARN, "Could not open keyfile %s", key_file);
|
||||
@@ -255,6 +258,8 @@ KEY_Reload(void)
|
||||
more careful! */
|
||||
qsort(ARR_GetElements(keys), ARR_GetSize(keys), sizeof (Key), compare_keys_by_id);
|
||||
|
||||
LOG(LOGS_INFO, "Loaded %u symmetric keys", ARR_GetSize(keys));
|
||||
|
||||
/* Check for duplicates */
|
||||
for (i = 1; i < ARR_GetSize(keys); i++) {
|
||||
if (get_key(i - 1)->id == get_key(i)->id)
|
||||
|
||||
45
local.c
45
local.c
@@ -505,7 +505,7 @@ LCL_AccumulateDeltaFrequency(double dfreq)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
int
|
||||
LCL_AccumulateOffset(double offset, double corr_rate)
|
||||
{
|
||||
struct timespec raw, cooked;
|
||||
@@ -517,12 +517,14 @@ LCL_AccumulateOffset(double offset, double corr_rate)
|
||||
LCL_CookTime(&raw, &cooked, NULL);
|
||||
|
||||
if (!check_offset(&cooked, offset))
|
||||
return;
|
||||
return 0;
|
||||
|
||||
(*drv_accrue_offset)(offset, corr_rate);
|
||||
|
||||
/* Dispatch to all handlers */
|
||||
invoke_parameter_change_handlers(&raw, &cooked, 0.0, offset, LCL_ChangeAdjust);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -561,6 +563,8 @@ void
|
||||
LCL_NotifyExternalTimeStep(struct timespec *raw, struct timespec *cooked,
|
||||
double offset, double dispersion)
|
||||
{
|
||||
LCL_CancelOffsetCorrection();
|
||||
|
||||
/* Dispatch to all handlers */
|
||||
invoke_parameter_change_handlers(raw, cooked, 0.0, offset, LCL_ChangeUnknownStep);
|
||||
|
||||
@@ -586,7 +590,7 @@ LCL_NotifyLeap(int leap)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
int
|
||||
LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate)
|
||||
{
|
||||
struct timespec raw, cooked;
|
||||
@@ -598,7 +602,7 @@ LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate)
|
||||
LCL_CookTime(&raw, &cooked, NULL);
|
||||
|
||||
if (!check_offset(&cooked, doffset))
|
||||
return;
|
||||
return 0;
|
||||
|
||||
old_freq_ppm = current_freq_ppm;
|
||||
|
||||
@@ -620,6 +624,26 @@ LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate)
|
||||
|
||||
/* Dispatch to all handlers */
|
||||
invoke_parameter_change_handlers(&raw, &cooked, dfreq, doffset, LCL_ChangeAdjust);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
LCL_AccumulateFrequencyAndOffsetNoHandlers(double dfreq, double doffset, double corr_rate)
|
||||
{
|
||||
ChangeListEntry *first_handler;
|
||||
int r;
|
||||
|
||||
first_handler = change_list.next;
|
||||
change_list.next = &change_list;
|
||||
|
||||
r = LCL_AccumulateFrequencyAndOffset(dfreq, doffset, corr_rate);
|
||||
|
||||
change_list.next = first_handler;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -687,6 +711,19 @@ LCL_MakeStep(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LCL_CancelOffsetCorrection(void)
|
||||
{
|
||||
struct timespec raw;
|
||||
double correction;
|
||||
|
||||
LCL_ReadRawTime(&raw);
|
||||
LCL_GetOffsetCorrection(&raw, &correction, NULL);
|
||||
LCL_AccumulateOffset(correction, 0.0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
LCL_CanSystemLeap(void)
|
||||
{
|
||||
|
||||
12
local.h
12
local.h
@@ -149,7 +149,7 @@ extern void LCL_AccumulateDeltaFrequency(double dfreq);
|
||||
forwards (i.e. it is currently slow of true time). Provided is also
|
||||
a suggested correction rate (correction time * offset). */
|
||||
|
||||
extern void LCL_AccumulateOffset(double offset, double corr_rate);
|
||||
extern int LCL_AccumulateOffset(double offset, double corr_rate);
|
||||
|
||||
/* Routine to apply an immediate offset by doing a sudden step if
|
||||
possible. (Intended for use after an initial estimate of offset has
|
||||
@@ -171,7 +171,12 @@ extern void LCL_NotifyLeap(int leap);
|
||||
|
||||
/* Perform the combination of modifying the frequency and applying
|
||||
a slew, in one easy step */
|
||||
extern void LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate);
|
||||
extern int LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate);
|
||||
|
||||
/* Same as the routine above, except it does not call the registered
|
||||
parameter change handlers */
|
||||
extern int LCL_AccumulateFrequencyAndOffsetNoHandlers(double dfreq, double doffset,
|
||||
double corr_rate);
|
||||
|
||||
/* Routine to read the system precision as a log to base 2 value. */
|
||||
extern int LCL_GetSysPrecisionAsLog(void);
|
||||
@@ -197,6 +202,9 @@ extern void LCL_Finalise(void);
|
||||
to a timezone problem. */
|
||||
extern int LCL_MakeStep(void);
|
||||
|
||||
/* Routine to cancel the outstanding system clock correction */
|
||||
extern void LCL_CancelOffsetCorrection(void);
|
||||
|
||||
/* Check if the system driver supports leap seconds, i.e. LCL_SetSystemLeap
|
||||
does something */
|
||||
extern int LCL_CanSystemLeap(void);
|
||||
|
||||
29
logging.c
29
logging.c
@@ -39,6 +39,9 @@
|
||||
/* This is used by DEBUG_LOG macro */
|
||||
LOG_Severity log_min_severity = LOGS_INFO;
|
||||
|
||||
/* Current logging contexts */
|
||||
static LOG_Context log_contexts;
|
||||
|
||||
/* ================================================== */
|
||||
/* Flag indicating we have initialised */
|
||||
static int initialised = 0;
|
||||
@@ -72,6 +75,8 @@ void
|
||||
LOG_Initialise(void)
|
||||
{
|
||||
debug_prefix = Strdup("");
|
||||
log_contexts = 0;
|
||||
|
||||
initialised = 1;
|
||||
LOG_OpenFileLog(NULL);
|
||||
}
|
||||
@@ -237,6 +242,30 @@ LOG_GetMinSeverity(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_SetContext(LOG_Context context)
|
||||
{
|
||||
log_contexts |= context;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_UnsetContext(LOG_Context context)
|
||||
{
|
||||
log_contexts &= ~context;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
LOG_Severity
|
||||
LOG_GetContextSeverity(LOG_Context contexts)
|
||||
{
|
||||
return log_contexts & contexts ? LOGS_INFO : LOGS_DEBUG;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_SetDebugPrefix(const char *prefix)
|
||||
{
|
||||
|
||||
14
logging.h
14
logging.h
@@ -100,6 +100,20 @@ extern void LOG_SetMinSeverity(LOG_Severity severity);
|
||||
/* Get the minimum severity */
|
||||
extern LOG_Severity LOG_GetMinSeverity(void);
|
||||
|
||||
/* Flags for info messages that should be logged only in specific contexts */
|
||||
typedef enum {
|
||||
LOGC_Command = 1,
|
||||
LOGC_SourceFile = 2,
|
||||
} LOG_Context;
|
||||
|
||||
/* Modify current contexts */
|
||||
extern void LOG_SetContext(LOG_Context context);
|
||||
extern void LOG_UnsetContext(LOG_Context context);
|
||||
|
||||
/* Get severity depending on the current active contexts: INFO if they contain
|
||||
at least one of the specified contexts, DEBUG otherwise */
|
||||
extern LOG_Severity LOG_GetContextSeverity(LOG_Context contexts);
|
||||
|
||||
/* Set a prefix for debug messages */
|
||||
extern void LOG_SetDebugPrefix(const char *prefix);
|
||||
|
||||
|
||||
24
main.c
24
main.c
@@ -76,11 +76,18 @@ static REF_Mode ref_mode = REF_ModeNormal;
|
||||
static void
|
||||
do_platform_checks(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
/* Require at least 32-bit integers, two's complement representation and
|
||||
the usual implementation of conversion of unsigned integers */
|
||||
assert(sizeof (int) >= 4);
|
||||
assert(-1 == ~0);
|
||||
assert((int32_t)4294967295U == (int32_t)-1);
|
||||
|
||||
/* Require time_t and tv_nsec in timespec to be signed */
|
||||
ts.tv_sec = -1;
|
||||
ts.tv_nsec = -1;
|
||||
assert(ts.tv_sec < 0 && ts.tv_nsec < 0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -104,6 +111,7 @@ MAI_CleanupAndExit(void)
|
||||
{
|
||||
if (!initialised) exit(exit_status);
|
||||
|
||||
LCL_CancelOffsetCorrection();
|
||||
SRC_DumpSources();
|
||||
|
||||
/* Don't update clock when removing sources */
|
||||
@@ -140,6 +148,8 @@ MAI_CleanupAndExit(void)
|
||||
HSH_Finalise();
|
||||
LOG_Finalise();
|
||||
|
||||
UTI_ResetGetRandomFunctions();
|
||||
|
||||
exit(exit_status);
|
||||
}
|
||||
|
||||
@@ -156,6 +166,8 @@ signal_cleanup(int x)
|
||||
static void
|
||||
quit_timeout(void *arg)
|
||||
{
|
||||
LOG(LOGS_INFO, "Timeout reached");
|
||||
|
||||
/* Return with non-zero status if the clock is not synchronised */
|
||||
exit_status = REF_GetOurStratum() >= NTP_MAX_STRATUM;
|
||||
SCH_QuitProgram();
|
||||
@@ -183,7 +195,7 @@ ntp_source_resolving_end(void)
|
||||
NSR_AutoStartSources();
|
||||
|
||||
/* Special modes can end only when sources update their reachability.
|
||||
Give up immediatelly if there are no active sources. */
|
||||
Give up immediately if there are no active sources. */
|
||||
if (ref_mode != REF_ModeNormal && !SRC_ActiveSources()) {
|
||||
REF_SetUnsynchronised();
|
||||
}
|
||||
@@ -491,6 +503,7 @@ int main
|
||||
user_check = 0;
|
||||
nofork = 1;
|
||||
system_log = 0;
|
||||
log_severity = LOGS_WARN;
|
||||
break;
|
||||
case 'P':
|
||||
sched_priority = parse_int_arg(optarg);
|
||||
@@ -624,9 +637,16 @@ int main
|
||||
}
|
||||
|
||||
/* Drop root privileges if the specified user has a non-zero UID */
|
||||
if (!geteuid() && (pw->pw_uid || pw->pw_gid))
|
||||
if (!geteuid() && (pw->pw_uid || pw->pw_gid)) {
|
||||
SYS_DropRoot(pw->pw_uid, pw->pw_gid, SYS_MAIN_PROCESS);
|
||||
|
||||
/* Warn if missing read access or having write access to keys */
|
||||
CNF_CheckReadOnlyAccess();
|
||||
}
|
||||
|
||||
if (!geteuid())
|
||||
LOG(LOGS_WARN, "Running with root privileges");
|
||||
|
||||
REF_Initialise();
|
||||
SST_Initialise();
|
||||
NSR_Initialise();
|
||||
|
||||
15
md5.c
15
md5.c
@@ -117,8 +117,7 @@ inline UINT4 ROTATE_LEFT(UINT4 x, int n)
|
||||
/* The routine MD5Init initializes the message-digest context
|
||||
mdContext. All fields are set to zero.
|
||||
*/
|
||||
void MD5Init (mdContext)
|
||||
MD5_CTX *mdContext;
|
||||
void MD5Init (MD5_CTX *mdContext)
|
||||
{
|
||||
mdContext->i[0] = mdContext->i[1] = (UINT4)0;
|
||||
|
||||
@@ -134,10 +133,7 @@ MD5_CTX *mdContext;
|
||||
account for the presence of each of the characters inBuf[0..inLen-1]
|
||||
in the message whose digest is being computed.
|
||||
*/
|
||||
void MD5Update (mdContext, inBuf, inLen)
|
||||
MD5_CTX *mdContext;
|
||||
unsigned const char *inBuf;
|
||||
unsigned int inLen;
|
||||
void MD5Update (MD5_CTX *mdContext, unsigned const char *inBuf, unsigned int inLen)
|
||||
{
|
||||
UINT4 in[16];
|
||||
int mdi;
|
||||
@@ -173,8 +169,7 @@ unsigned int inLen;
|
||||
ends with the desired message digest in mdContext->digest[0...15].
|
||||
*/
|
||||
|
||||
void MD5Final (mdContext)
|
||||
MD5_CTX *mdContext;
|
||||
void MD5Final (MD5_CTX *mdContext)
|
||||
{
|
||||
UINT4 in[16];
|
||||
int mdi;
|
||||
@@ -214,9 +209,7 @@ MD5_CTX *mdContext;
|
||||
|
||||
/* Basic MD5 step. Transforms buf based on in.
|
||||
*/
|
||||
static void Transform (buf, in)
|
||||
UINT4 *buf;
|
||||
UINT4 *in;
|
||||
static void Transform (UINT4 *buf, UINT4 *in)
|
||||
{
|
||||
UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
|
||||
|
||||
|
||||
83
nameserv.c
83
nameserv.c
@@ -50,12 +50,24 @@ DNS_SetAddressFamily(int family)
|
||||
DNS_Status
|
||||
DNS_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
||||
{
|
||||
#ifdef HAVE_GETADDRINFO
|
||||
struct addrinfo hints, *res, *ai;
|
||||
int i, result;
|
||||
IPAddr ip;
|
||||
|
||||
max_addrs = MIN(max_addrs, DNS_MAX_ADDRESSES);
|
||||
|
||||
for (i = 0; i < max_addrs; i++)
|
||||
ip_addrs[i].family = IPADDR_UNSPEC;
|
||||
|
||||
/* Avoid calling getaddrinfo() if the name is an IP address */
|
||||
if (UTI_StringToIP(name, &ip)) {
|
||||
if (address_family != IPADDR_UNSPEC && ip.family != address_family)
|
||||
return DNS_Failure;
|
||||
if (max_addrs >= 1)
|
||||
ip_addrs[0] = ip;
|
||||
return DNS_Success;
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof (hints));
|
||||
|
||||
switch (address_family) {
|
||||
@@ -107,48 +119,9 @@ DNS_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
||||
}
|
||||
}
|
||||
|
||||
for (; i < max_addrs; i++)
|
||||
ip_addrs[i].family = IPADDR_UNSPEC;
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
return !max_addrs || ip_addrs[0].family != IPADDR_UNSPEC ? DNS_Success : DNS_Failure;
|
||||
#else
|
||||
struct hostent *host;
|
||||
int i;
|
||||
|
||||
if (address_family != IPADDR_UNSPEC && address_family != IPADDR_INET4)
|
||||
return DNS_Failure;
|
||||
|
||||
max_addrs = MIN(max_addrs, DNS_MAX_ADDRESSES);
|
||||
|
||||
host = gethostbyname(name);
|
||||
|
||||
if (host == NULL) {
|
||||
if (h_errno == TRY_AGAIN)
|
||||
return DNS_TryAgain;
|
||||
} else {
|
||||
if (host->h_addrtype != AF_INET || !host->h_addr_list[0])
|
||||
return DNS_Failure;
|
||||
|
||||
for (i = 0; host->h_addr_list[i] && i < max_addrs; i++) {
|
||||
ip_addrs[i].family = IPADDR_INET4;
|
||||
ip_addrs[i].addr.in4 = ntohl(*(uint32_t *)host->h_addr_list[i]);
|
||||
}
|
||||
|
||||
for (; i < max_addrs; i++)
|
||||
ip_addrs[i].family = IPADDR_UNSPEC;
|
||||
|
||||
return DNS_Success;
|
||||
}
|
||||
|
||||
#ifdef FORCE_DNSRETRY
|
||||
return DNS_TryAgain;
|
||||
#else
|
||||
return DNS_Failure;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -157,9 +130,11 @@ int
|
||||
DNS_IPAddress2Name(IPAddr *ip_addr, char *name, int len)
|
||||
{
|
||||
char *result = NULL;
|
||||
|
||||
#ifdef FEAT_IPV6
|
||||
struct sockaddr_in6 in6;
|
||||
struct sockaddr_in6 saddr;
|
||||
#else
|
||||
struct sockaddr_in saddr;
|
||||
#endif
|
||||
IPSockAddr ip_saddr;
|
||||
socklen_t slen;
|
||||
char hbuf[NI_MAXHOST];
|
||||
@@ -167,29 +142,9 @@ DNS_IPAddress2Name(IPAddr *ip_addr, char *name, int len)
|
||||
ip_saddr.ip_addr = *ip_addr;
|
||||
ip_saddr.port = 0;
|
||||
|
||||
slen = SCK_IPSockAddrToSockaddr(&ip_saddr, (struct sockaddr *)&in6, sizeof (in6));
|
||||
if (!getnameinfo((struct sockaddr *)&in6, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
||||
slen = SCK_IPSockAddrToSockaddr(&ip_saddr, (struct sockaddr *)&saddr, sizeof (saddr));
|
||||
if (!getnameinfo((struct sockaddr *)&saddr, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
||||
result = hbuf;
|
||||
#else
|
||||
struct hostent *host;
|
||||
uint32_t addr;
|
||||
|
||||
switch (ip_addr->family) {
|
||||
case IPADDR_INET4:
|
||||
addr = htonl(ip_addr->addr.in4);
|
||||
host = gethostbyaddr((const char *) &addr, sizeof (ip_addr), AF_INET);
|
||||
break;
|
||||
#ifdef FEAT_IPV6
|
||||
case IPADDR_INET6:
|
||||
host = gethostbyaddr((const void *) ip_addr->addr.in6, sizeof (ip_addr->addr.in6), AF_INET6);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
host = NULL;
|
||||
}
|
||||
if (host)
|
||||
result = host->h_name;
|
||||
#endif
|
||||
|
||||
if (result == NULL)
|
||||
result = UTI_IPToString(ip_addr);
|
||||
|
||||
33
ntp.h
33
ntp.h
@@ -113,6 +113,30 @@ typedef struct {
|
||||
#define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
|
||||
#define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */
|
||||
|
||||
/* Non-authentication extension fields and corresponding internal flags */
|
||||
|
||||
#define NTP_EF_EXP1 0xF323
|
||||
|
||||
#define NTP_EF_FLAG_EXP1 0x1
|
||||
|
||||
/* Pre-NTPv5 experimental extension field */
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
NTP_int32 root_delay;
|
||||
NTP_int32 root_dispersion;
|
||||
NTP_int64 mono_receive_ts;
|
||||
uint32_t mono_epoch;
|
||||
} NTP_ExtFieldExp1;
|
||||
|
||||
#define NTP_EF_EXP1_MAGIC 0xF5BEDD9AU
|
||||
|
||||
/* Authentication extension fields */
|
||||
|
||||
#define NTP_EF_NTS_UNIQUE_IDENTIFIER 0x0104
|
||||
#define NTP_EF_NTS_COOKIE 0x0204
|
||||
#define NTP_EF_NTS_COOKIE_PLACEHOLDER 0x0304
|
||||
#define NTP_EF_NTS_AUTH_AND_EEF 0x0404
|
||||
|
||||
/* Enumeration for authentication modes of NTP packets */
|
||||
typedef enum {
|
||||
NTP_AUTH_NONE = 0, /* No authentication */
|
||||
@@ -130,6 +154,7 @@ typedef struct {
|
||||
NTP_Mode mode;
|
||||
|
||||
int ext_fields;
|
||||
int ext_field_flags;
|
||||
|
||||
struct {
|
||||
NTP_AuthMode mode;
|
||||
@@ -152,7 +177,13 @@ typedef struct {
|
||||
double peer_dispersion;
|
||||
double root_delay;
|
||||
double root_dispersion;
|
||||
int stratum;
|
||||
} NTP_Sample;
|
||||
|
||||
/* Possible sources of timestamps */
|
||||
typedef enum {
|
||||
NTP_TS_DAEMON = 0,
|
||||
NTP_TS_KERNEL,
|
||||
NTP_TS_HARDWARE
|
||||
} NTP_Timestamp_Source;
|
||||
|
||||
#endif /* GOT_NTP_H */
|
||||
|
||||
114
ntp_auth.c
114
ntp_auth.c
@@ -32,7 +32,6 @@
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "ntp_auth.h"
|
||||
#include "ntp_ext.h"
|
||||
#include "ntp_signd.h"
|
||||
#include "nts_ntp.h"
|
||||
#include "nts_ntp_client.h"
|
||||
@@ -105,19 +104,6 @@ check_symmetric_auth(NTP_Packet *packet, NTP_PacketInfo *info)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
is_zero_data(unsigned char *data, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
if (data[i] != 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static NAU_Instance
|
||||
create_instance(NTP_AuthMode mode)
|
||||
{
|
||||
@@ -161,11 +147,12 @@ NAU_CreateSymmetricInstance(uint32_t key_id)
|
||||
/* ================================================== */
|
||||
|
||||
NAU_Instance
|
||||
NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
|
||||
NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name, uint32_t cert_set,
|
||||
uint16_t ntp_port)
|
||||
{
|
||||
NAU_Instance instance = create_instance(NTP_AUTH_NTS);
|
||||
|
||||
instance->nts = NNC_CreateInstance(nts_address, name, ntp_address);
|
||||
instance->nts = NNC_CreateInstance(nts_address, name, cert_set, ntp_port);
|
||||
|
||||
return instance;
|
||||
}
|
||||
@@ -246,101 +233,6 @@ NAU_GenerateRequestAuth(NAU_Instance instance, NTP_Packet *request, NTP_PacketIn
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_ParsePacket(NTP_Packet *packet, NTP_PacketInfo *info)
|
||||
{
|
||||
int parsed, remainder, ef_length, ef_type;
|
||||
unsigned char *data;
|
||||
|
||||
data = (void *)packet;
|
||||
parsed = NTP_HEADER_LENGTH;
|
||||
remainder = info->length - parsed;
|
||||
|
||||
info->ext_fields = 0;
|
||||
|
||||
/* Check if this is a plain NTP packet with no extension fields or MAC */
|
||||
if (remainder <= 0)
|
||||
return 1;
|
||||
|
||||
assert(remainder % 4 == 0);
|
||||
|
||||
/* In NTPv3 and older packets don't have extension fields. Anything after
|
||||
the header is assumed to be a MAC. */
|
||||
if (info->version <= 3) {
|
||||
info->auth.mode = NTP_AUTH_SYMMETRIC;
|
||||
info->auth.mac.start = parsed;
|
||||
info->auth.mac.length = remainder;
|
||||
info->auth.mac.key_id = ntohl(*(uint32_t *)(data + parsed));
|
||||
|
||||
/* Check if it is an MS-SNTP authenticator field or extended authenticator
|
||||
field with zeroes as digest */
|
||||
if (info->version == 3 && info->auth.mac.key_id != 0) {
|
||||
if (remainder == 20 && is_zero_data(data + parsed + 4, remainder - 4))
|
||||
info->auth.mode = NTP_AUTH_MSSNTP;
|
||||
else if (remainder == 72 && is_zero_data(data + parsed + 8, remainder - 8))
|
||||
info->auth.mode = NTP_AUTH_MSSNTP_EXT;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check for a crypto NAK */
|
||||
if (remainder == 4 && ntohl(*(uint32_t *)(data + parsed)) == 0) {
|
||||
info->auth.mode = NTP_AUTH_SYMMETRIC;
|
||||
info->auth.mac.start = parsed;
|
||||
info->auth.mac.length = remainder;
|
||||
info->auth.mac.key_id = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Parse the rest of the NTPv4 packet */
|
||||
|
||||
while (remainder > 0) {
|
||||
/* Check if the remaining data is a MAC */
|
||||
if (remainder >= NTP_MIN_MAC_LENGTH && remainder <= NTP_MAX_V4_MAC_LENGTH)
|
||||
break;
|
||||
|
||||
/* Check if this is a valid NTPv4 extension field and skip it */
|
||||
if (!NEF_ParseField(packet, info->length, parsed, &ef_length, &ef_type, NULL, NULL)) {
|
||||
DEBUG_LOG("Invalid format");
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(ef_length > 0 && ef_length % 4 == 0);
|
||||
|
||||
switch (ef_type) {
|
||||
case NTP_EF_NTS_UNIQUE_IDENTIFIER:
|
||||
case NTP_EF_NTS_COOKIE:
|
||||
case NTP_EF_NTS_COOKIE_PLACEHOLDER:
|
||||
case NTP_EF_NTS_AUTH_AND_EEF:
|
||||
info->auth.mode = NTP_AUTH_NTS;
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG("Unknown extension field type=%x", (unsigned int)ef_type);
|
||||
}
|
||||
|
||||
info->ext_fields++;
|
||||
parsed += ef_length;
|
||||
remainder = info->length - parsed;
|
||||
}
|
||||
|
||||
if (remainder == 0) {
|
||||
/* No MAC */
|
||||
return 1;
|
||||
} else if (remainder >= NTP_MIN_MAC_LENGTH) {
|
||||
info->auth.mode = NTP_AUTH_SYMMETRIC;
|
||||
info->auth.mac.start = parsed;
|
||||
info->auth.mac.length = remainder;
|
||||
info->auth.mac.key_id = ntohl(*(uint32_t *)(data + parsed));
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Invalid format");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_CheckRequestAuth(NTP_Packet *request, NTP_PacketInfo *info, uint32_t *kod)
|
||||
{
|
||||
|
||||
@@ -37,7 +37,7 @@ typedef struct NAU_Instance_Record *NAU_Instance;
|
||||
extern NAU_Instance NAU_CreateNoneInstance(void);
|
||||
extern NAU_Instance NAU_CreateSymmetricInstance(uint32_t key_id);
|
||||
extern NAU_Instance NAU_CreateNtsInstance(IPSockAddr *nts_address, const char *name,
|
||||
const IPSockAddr *ntp_address);
|
||||
uint32_t cert_set, uint16_t ntp_port);
|
||||
|
||||
/* Destroy an instance */
|
||||
extern void NAU_DestroyInstance(NAU_Instance instance);
|
||||
@@ -55,9 +55,6 @@ extern int NAU_PrepareRequestAuth(NAU_Instance instance);
|
||||
extern int NAU_GenerateRequestAuth(NAU_Instance instance, NTP_Packet *request,
|
||||
NTP_PacketInfo *info);
|
||||
|
||||
/* Parse a request or response to detect the authentication mode */
|
||||
extern int NAU_ParsePacket(NTP_Packet *packet, NTP_PacketInfo *info);
|
||||
|
||||
/* Verify that a request is authentic. If it is not authentic and a non-zero
|
||||
kod code is returned, a KoD response should be sent back. */
|
||||
extern int NAU_CheckRequestAuth(NTP_Packet *request, NTP_PacketInfo *info, uint32_t *kod);
|
||||
|
||||
801
ntp_core.c
801
ntp_core.c
File diff suppressed because it is too large
Load Diff
@@ -38,12 +38,6 @@ typedef enum {
|
||||
NTP_SERVER, NTP_PEER
|
||||
} NTP_Source_Type;
|
||||
|
||||
typedef enum {
|
||||
NTP_TS_DAEMON = 0,
|
||||
NTP_TS_KERNEL,
|
||||
NTP_TS_HARDWARE
|
||||
} NTP_Timestamp_Source;
|
||||
|
||||
typedef struct {
|
||||
struct timespec ts;
|
||||
double err;
|
||||
|
||||
180
ntp_io.c
180
ntp_io.c
@@ -4,7 +4,7 @@
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Timo Teras 2009
|
||||
* Copyright (C) Miroslav Lichvar 2009, 2013-2016, 2018-2020
|
||||
* Copyright (C) Miroslav Lichvar 2009, 2013-2016, 2018-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -30,9 +30,11 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "memory.h"
|
||||
#include "ntp_io.h"
|
||||
#include "ntp_core.h"
|
||||
#include "ntp_sources.h"
|
||||
#include "ptp.h"
|
||||
#include "sched.h"
|
||||
#include "socket.h"
|
||||
#include "local.h"
|
||||
@@ -70,6 +72,16 @@ static int permanent_server_sockets;
|
||||
/* Flag indicating the server IPv4 socket is bound to an address */
|
||||
static int bound_server_sock_fd4;
|
||||
|
||||
/* PTP event port, or 0 if disabled */
|
||||
static int ptp_port;
|
||||
|
||||
/* Shared server/client sockets for NTP-over-PTP */
|
||||
static int ptp_sock_fd4;
|
||||
static int ptp_sock_fd6;
|
||||
|
||||
/* Buffer for transmitted NTP-over-PTP messages */
|
||||
static PTP_NtpMessage *ptp_message;
|
||||
|
||||
/* Flag indicating that we have been initialised */
|
||||
static int initialised=0;
|
||||
|
||||
@@ -114,8 +126,14 @@ open_socket(int family, int local_port, int client_only, IPSockAddr *remote_addr
|
||||
dscp = CNF_GetNtpDscp();
|
||||
if (dscp > 0 && dscp < 64) {
|
||||
#ifdef IP_TOS
|
||||
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_TOS, dscp << 2))
|
||||
;
|
||||
if (family == IPADDR_INET4)
|
||||
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_TOS, dscp << 2))
|
||||
;
|
||||
#endif
|
||||
#if defined(FEAT_IPV6) && defined(IPV6_TCLASS)
|
||||
if (family == IPADDR_INET6)
|
||||
if (!SCK_SetIntOption(sock_fd, IPPROTO_IPV6, IPV6_TCLASS, dscp << 2))
|
||||
;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -151,9 +169,6 @@ close_socket(int sock_fd)
|
||||
if (sock_fd == INVALID_SOCK_FD)
|
||||
return;
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
NIO_Linux_NotifySocketClosing(sock_fd);
|
||||
#endif
|
||||
SCH_RemoveFileHandler(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
}
|
||||
@@ -221,6 +236,17 @@ NIO_Initialise(void)
|
||||
client_sock_fd4 == INVALID_SOCK_FD && client_sock_fd6 == INVALID_SOCK_FD)) {
|
||||
LOG_FATAL("Could not open NTP sockets");
|
||||
}
|
||||
|
||||
ptp_port = CNF_GetPtpPort();
|
||||
ptp_sock_fd4 = INVALID_SOCK_FD;
|
||||
ptp_sock_fd6 = INVALID_SOCK_FD;
|
||||
ptp_message = NULL;
|
||||
|
||||
if (ptp_port > 0) {
|
||||
ptp_sock_fd4 = open_socket(IPADDR_INET4, ptp_port, 0, NULL);
|
||||
ptp_sock_fd6 = open_socket(IPADDR_INET6, ptp_port, 0, NULL);
|
||||
ptp_message = MallocNew(PTP_NtpMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -238,6 +264,11 @@ NIO_Finalise(void)
|
||||
close_socket(server_sock_fd6);
|
||||
server_sock_fd6 = client_sock_fd6 = INVALID_SOCK_FD;
|
||||
|
||||
close_socket(ptp_sock_fd4);
|
||||
close_socket(ptp_sock_fd6);
|
||||
ptp_sock_fd4 = ptp_sock_fd6 = INVALID_SOCK_FD;
|
||||
Free(ptp_message);
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
NIO_Linux_Finalise();
|
||||
#endif
|
||||
@@ -250,17 +281,21 @@ NIO_Finalise(void)
|
||||
int
|
||||
NIO_OpenClientSocket(NTP_Remote_Address *remote_addr)
|
||||
{
|
||||
if (separate_client_sockets) {
|
||||
return open_separate_client_socket(remote_addr);
|
||||
} else {
|
||||
switch (remote_addr->ip_addr.family) {
|
||||
case IPADDR_INET4:
|
||||
return client_sock_fd4;
|
||||
case IPADDR_INET6:
|
||||
return client_sock_fd6;
|
||||
default:
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
switch (remote_addr->ip_addr.family) {
|
||||
case IPADDR_INET4:
|
||||
if (ptp_port > 0 && remote_addr->port == ptp_port)
|
||||
return ptp_sock_fd4;
|
||||
if (separate_client_sockets)
|
||||
return open_separate_client_socket(remote_addr);
|
||||
return client_sock_fd4;
|
||||
case IPADDR_INET6:
|
||||
if (ptp_port > 0 && remote_addr->port == ptp_port)
|
||||
return ptp_sock_fd6;
|
||||
if (separate_client_sockets)
|
||||
return open_separate_client_socket(remote_addr);
|
||||
return client_sock_fd6;
|
||||
default:
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,6 +306,8 @@ NIO_OpenServerSocket(NTP_Remote_Address *remote_addr)
|
||||
{
|
||||
switch (remote_addr->ip_addr.family) {
|
||||
case IPADDR_INET4:
|
||||
if (ptp_port > 0 && remote_addr->port == ptp_port)
|
||||
return ptp_sock_fd4;
|
||||
if (permanent_server_sockets)
|
||||
return server_sock_fd4;
|
||||
if (server_sock_fd4 == INVALID_SOCK_FD)
|
||||
@@ -279,6 +316,8 @@ NIO_OpenServerSocket(NTP_Remote_Address *remote_addr)
|
||||
server_sock_ref4++;
|
||||
return server_sock_fd4;
|
||||
case IPADDR_INET6:
|
||||
if (ptp_port > 0 && remote_addr->port == ptp_port)
|
||||
return ptp_sock_fd6;
|
||||
if (permanent_server_sockets)
|
||||
return server_sock_fd6;
|
||||
if (server_sock_fd6 == INVALID_SOCK_FD)
|
||||
@@ -293,9 +332,21 @@ NIO_OpenServerSocket(NTP_Remote_Address *remote_addr)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
is_ptp_socket(int sock_fd)
|
||||
{
|
||||
return ptp_port > 0 && sock_fd != INVALID_SOCK_FD &&
|
||||
(sock_fd == ptp_sock_fd4 || sock_fd == ptp_sock_fd6);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NIO_CloseClientSocket(int sock_fd)
|
||||
{
|
||||
if (is_ptp_socket(sock_fd))
|
||||
return;
|
||||
|
||||
if (separate_client_sockets)
|
||||
close_socket(sock_fd);
|
||||
}
|
||||
@@ -305,7 +356,7 @@ NIO_CloseClientSocket(int sock_fd)
|
||||
void
|
||||
NIO_CloseServerSocket(int sock_fd)
|
||||
{
|
||||
if (permanent_server_sockets || sock_fd == INVALID_SOCK_FD)
|
||||
if (permanent_server_sockets || sock_fd == INVALID_SOCK_FD || is_ptp_socket(sock_fd))
|
||||
return;
|
||||
|
||||
if (sock_fd == server_sock_fd4) {
|
||||
@@ -329,7 +380,7 @@ int
|
||||
NIO_IsServerSocket(int sock_fd)
|
||||
{
|
||||
return sock_fd != INVALID_SOCK_FD &&
|
||||
(sock_fd == server_sock_fd4 || sock_fd == server_sock_fd6);
|
||||
(sock_fd == server_sock_fd4 || sock_fd == server_sock_fd6 || is_ptp_socket(sock_fd));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -337,7 +388,8 @@ NIO_IsServerSocket(int sock_fd)
|
||||
int
|
||||
NIO_IsServerSocketOpen(void)
|
||||
{
|
||||
return server_sock_fd4 != INVALID_SOCK_FD || server_sock_fd6 != INVALID_SOCK_FD;
|
||||
return server_sock_fd4 != INVALID_SOCK_FD || server_sock_fd6 != INVALID_SOCK_FD ||
|
||||
ptp_sock_fd4 != INVALID_SOCK_FD || ptp_sock_fd6 != INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -392,6 +444,9 @@ process_message(SCK_Message *message, int sock_fd, int event)
|
||||
DEBUG_LOG("Updated RX timestamp delay=%.9f tss=%u",
|
||||
UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts), local_ts.source);
|
||||
|
||||
if (!NIO_UnwrapMessage(message, sock_fd))
|
||||
return;
|
||||
|
||||
/* Just ignore the packet if it's not of a recognized length */
|
||||
if (message->length < NTP_HEADER_LENGTH || message->length > sizeof (NTP_Packet)) {
|
||||
DEBUG_LOG("Unexpected length");
|
||||
@@ -409,11 +464,6 @@ read_from_socket(int sock_fd, int event, void *anything)
|
||||
SCK_Message *messages;
|
||||
int i, received, flags = 0;
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
if (NIO_Linux_ProcessEvent(sock_fd, event))
|
||||
return;
|
||||
#endif
|
||||
|
||||
if (event == SCH_FILE_EXCEPTION) {
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
flags |= SCK_FLAG_MSG_ERRQUEUE;
|
||||
@@ -430,6 +480,81 @@ read_from_socket(int sock_fd, int event, void *anything)
|
||||
process_message(&messages[i], sock_fd, event);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_UnwrapMessage(SCK_Message *message, int sock_fd)
|
||||
{
|
||||
PTP_NtpMessage *msg;
|
||||
|
||||
if (!is_ptp_socket(sock_fd))
|
||||
return 1;
|
||||
|
||||
if (message->length <= PTP_NTP_PREFIX_LENGTH) {
|
||||
DEBUG_LOG("Unexpected length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
msg = message->data;
|
||||
|
||||
if (msg->header.type != PTP_TYPE_DELAY_REQ || msg->header.version != PTP_VERSION ||
|
||||
ntohs(msg->header.length) != message->length ||
|
||||
msg->header.domain != PTP_DOMAIN_NTP ||
|
||||
ntohs(msg->header.flags) != PTP_FLAG_UNICAST ||
|
||||
ntohs(msg->tlv_header.type) != PTP_TLV_NTP ||
|
||||
ntohs(msg->tlv_header.length) != message->length - PTP_NTP_PREFIX_LENGTH) {
|
||||
DEBUG_LOG("Unexpected PTP message");
|
||||
return 0;
|
||||
}
|
||||
|
||||
message->data = (char *)message->data + PTP_NTP_PREFIX_LENGTH;
|
||||
message->length -= PTP_NTP_PREFIX_LENGTH;
|
||||
|
||||
DEBUG_LOG("Unwrapped PTP->NTP len=%d", message->length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
wrap_message(SCK_Message *message, int sock_fd)
|
||||
{
|
||||
static uint16_t sequence_id = 0;
|
||||
|
||||
assert(PTP_NTP_PREFIX_LENGTH == 48);
|
||||
|
||||
if (!is_ptp_socket(sock_fd))
|
||||
return 1;
|
||||
|
||||
if (!ptp_message)
|
||||
return 0;
|
||||
|
||||
if (message->length < NTP_HEADER_LENGTH ||
|
||||
message->length + PTP_NTP_PREFIX_LENGTH > sizeof (*ptp_message)) {
|
||||
DEBUG_LOG("Unexpected length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(ptp_message, 0, PTP_NTP_PREFIX_LENGTH);
|
||||
ptp_message->header.type = PTP_TYPE_DELAY_REQ;
|
||||
ptp_message->header.version = PTP_VERSION;
|
||||
ptp_message->header.length = htons(PTP_NTP_PREFIX_LENGTH + message->length);
|
||||
ptp_message->header.domain = PTP_DOMAIN_NTP;
|
||||
ptp_message->header.flags = htons(PTP_FLAG_UNICAST);
|
||||
ptp_message->header.sequence_id = htons(sequence_id++);
|
||||
ptp_message->tlv_header.type = htons(PTP_TLV_NTP);
|
||||
ptp_message->tlv_header.length = htons(message->length);
|
||||
memcpy((char *)ptp_message + PTP_NTP_PREFIX_LENGTH, message->data, message->length);
|
||||
|
||||
message->data = ptp_message;
|
||||
message->length += PTP_NTP_PREFIX_LENGTH;
|
||||
|
||||
DEBUG_LOG("Wrapped NTP->PTP len=%d", message->length - PTP_NTP_PREFIX_LENGTH);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Send a packet to remote address from local address */
|
||||
|
||||
@@ -451,6 +576,9 @@ NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||
message.data = packet;
|
||||
message.length = length;
|
||||
|
||||
if (!wrap_message(&message, local_addr->sock_fd))
|
||||
return 0;
|
||||
|
||||
/* Specify remote address if the socket is not connected */
|
||||
if (NIO_IsServerSocket(local_addr->sock_fd) || !separate_client_sockets) {
|
||||
message.remote_addr.ip.ip_addr = remote_addr->ip_addr;
|
||||
@@ -467,7 +595,7 @@ NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||
#if !defined(HAVE_IN_PKTINFO) && defined(IP_SENDSRCADDR)
|
||||
/* On FreeBSD a local IPv4 address cannot be specified on bound socket */
|
||||
if (message.local_addr.ip.family == IPADDR_INET4 &&
|
||||
(local_addr->sock_fd != server_sock_fd4 || bound_server_sock_fd4))
|
||||
(bound_server_sock_fd4 || !NIO_IsServerSocket(local_addr->sock_fd)))
|
||||
message.local_addr.ip.family = IPADDR_UNSPEC;
|
||||
#endif
|
||||
|
||||
|
||||
4
ntp_io.h
4
ntp_io.h
@@ -31,6 +31,7 @@
|
||||
|
||||
#include "ntp.h"
|
||||
#include "addressing.h"
|
||||
#include "socket.h"
|
||||
|
||||
/* Function to initialise the module. */
|
||||
extern void NIO_Initialise(void);
|
||||
@@ -59,6 +60,9 @@ extern int NIO_IsServerSocketOpen(void);
|
||||
/* Function to check if client packets can be sent to a server */
|
||||
extern int NIO_IsServerConnectable(NTP_Remote_Address *remote_addr);
|
||||
|
||||
/* Function to unwrap an NTP message from non-native transport (e.g. PTP) */
|
||||
extern int NIO_UnwrapMessage(SCK_Message *message, int sock_fd);
|
||||
|
||||
/* Function to transmit a packet */
|
||||
extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||
NTP_Local_Address *local_addr, int length, int process_tx);
|
||||
|
||||
242
ntp_io_linux.c
242
ntp_io_linux.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2016-2019
|
||||
* Copyright (C) Miroslav Lichvar 2016-2019, 2021-2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "hwclock.h"
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "ntp_core.h"
|
||||
#include "ntp_io.h"
|
||||
#include "ntp_io_linux.h"
|
||||
@@ -59,21 +60,22 @@ struct Interface {
|
||||
/* Start of UDP data at layer 2 for IPv4 and IPv6 */
|
||||
int l2_udp4_ntp_start;
|
||||
int l2_udp6_ntp_start;
|
||||
/* Precision of PHC readings */
|
||||
double precision;
|
||||
/* Compensation of errors in TX and RX timestamping */
|
||||
double tx_comp;
|
||||
double rx_comp;
|
||||
HCL_Instance clock;
|
||||
int maxpoll;
|
||||
SCH_TimeoutID poll_timeout_id;
|
||||
};
|
||||
|
||||
/* Number of PHC readings per HW clock sample */
|
||||
#define PHC_READINGS 10
|
||||
#define PHC_READINGS 25
|
||||
|
||||
/* Minimum interval between PHC readings */
|
||||
/* Minimum and maximum interval between PHC readings */
|
||||
#define MIN_PHC_POLL -6
|
||||
#define MAX_PHC_POLL 20
|
||||
|
||||
/* Maximum acceptable offset between HW and daemon/kernel timestamp */
|
||||
/* Maximum acceptable offset between SW/HW and daemon timestamp */
|
||||
#define MAX_TS_DELAY 1.0
|
||||
|
||||
/* Array of Interfaces */
|
||||
@@ -86,19 +88,6 @@ static int ts_tx_flags;
|
||||
/* Flag indicating the socket options can't be changed in control messages */
|
||||
static int permanent_ts_options;
|
||||
|
||||
/* When sending client requests to a close and fast server, it is possible that
|
||||
a response will be received before the HW transmit timestamp of the request
|
||||
itself. To avoid processing of the response without the HW timestamp, we
|
||||
monitor events returned by select() and suspend reading of packets from the
|
||||
receive queue for up to 200 microseconds. As the requests are normally
|
||||
separated by at least 200 milliseconds, it is sufficient to monitor and
|
||||
suspend one socket at a time. */
|
||||
static int monitored_socket;
|
||||
static int suspended_socket;
|
||||
static SCH_TimeoutID resume_timeout_id;
|
||||
|
||||
#define RESUME_TIMEOUT 200.0e-6
|
||||
|
||||
/* Unbound socket keeping the kernel RX timestamping permanently enabled
|
||||
in order to avoid a race condition between receiving a server response
|
||||
and the kernel actually starting to timestamp received packets after
|
||||
@@ -109,13 +98,17 @@ static int dummy_rxts_socket;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void poll_phc(struct Interface *iface, struct timespec *now);
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
add_interface(CNF_HwTsInterface *conf_iface)
|
||||
{
|
||||
int sock_fd, if_index, minpoll, phc_fd, req_hwts_flags, rx_filter;
|
||||
struct ethtool_ts_info ts_info;
|
||||
struct hwtstamp_config ts_config;
|
||||
struct ifreq req;
|
||||
int sock_fd, if_index, phc_fd, req_hwts_flags, rx_filter;
|
||||
unsigned int i;
|
||||
struct Interface *iface;
|
||||
|
||||
@@ -189,6 +182,14 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||
break;
|
||||
#endif
|
||||
case CNF_HWTS_RXFILTER_PTP:
|
||||
if (ts_info.rx_filters & (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT))
|
||||
rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
|
||||
else if (ts_info.rx_filters & (1 << HWTSTAMP_FILTER_PTP_V2_EVENT))
|
||||
rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
|
||||
else
|
||||
rx_filter = HWTSTAMP_FILTER_NONE;
|
||||
break;
|
||||
default:
|
||||
rx_filter = HWTSTAMP_FILTER_ALL;
|
||||
break;
|
||||
@@ -236,12 +237,18 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
iface->l2_udp4_ntp_start = 42;
|
||||
iface->l2_udp6_ntp_start = 62;
|
||||
|
||||
iface->precision = conf_iface->precision;
|
||||
iface->tx_comp = conf_iface->tx_comp;
|
||||
iface->rx_comp = conf_iface->rx_comp;
|
||||
|
||||
minpoll = CLAMP(MIN_PHC_POLL, conf_iface->minpoll, MAX_PHC_POLL);
|
||||
iface->clock = HCL_CreateInstance(conf_iface->min_samples, conf_iface->max_samples,
|
||||
UTI_Log2ToDouble(MAX(conf_iface->minpoll, MIN_PHC_POLL)));
|
||||
UTI_Log2ToDouble(minpoll), conf_iface->precision);
|
||||
|
||||
iface->maxpoll = CLAMP(minpoll, conf_iface->maxpoll, MAX_PHC_POLL);
|
||||
|
||||
/* Do not schedule the first poll timeout here! The argument (interface) can
|
||||
move until all interfaces are added. Wait for the first HW timestamp. */
|
||||
iface->poll_timeout_id = 0;
|
||||
|
||||
LOG(LOGS_INFO, "Enabled HW timestamping %son %s",
|
||||
ts_config.rx_filter == HWTSTAMP_FILTER_NONE ? "(TX only) " : "", iface->name);
|
||||
@@ -406,8 +413,6 @@ NIO_Linux_Initialise(void)
|
||||
/* Kernels before 4.7 ignore timestamping flags set in control messages */
|
||||
permanent_ts_options = !SYS_Linux_CheckKernelVersion(4, 7);
|
||||
|
||||
monitored_socket = INVALID_SOCK_FD;
|
||||
suspended_socket = INVALID_SOCK_FD;
|
||||
dummy_rxts_socket = INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
@@ -424,6 +429,7 @@ NIO_Linux_Finalise(void)
|
||||
|
||||
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
||||
iface = ARR_GetElement(interfaces, i);
|
||||
SCH_RemoveTimeout(iface->poll_timeout_id);
|
||||
HCL_DestroyInstance(iface->clock);
|
||||
close(iface->phc_fd);
|
||||
}
|
||||
@@ -466,73 +472,6 @@ NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
resume_socket(int sock_fd)
|
||||
{
|
||||
if (monitored_socket == sock_fd)
|
||||
monitored_socket = INVALID_SOCK_FD;
|
||||
|
||||
if (sock_fd == INVALID_SOCK_FD || sock_fd != suspended_socket)
|
||||
return;
|
||||
|
||||
suspended_socket = INVALID_SOCK_FD;
|
||||
|
||||
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_INPUT, 1);
|
||||
|
||||
DEBUG_LOG("Resumed RX processing %s timeout fd=%d",
|
||||
resume_timeout_id ? "before" : "on", sock_fd);
|
||||
|
||||
if (resume_timeout_id) {
|
||||
SCH_RemoveTimeout(resume_timeout_id);
|
||||
resume_timeout_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
resume_timeout(void *arg)
|
||||
{
|
||||
resume_timeout_id = 0;
|
||||
resume_socket(suspended_socket);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
suspend_socket(int sock_fd)
|
||||
{
|
||||
resume_socket(suspended_socket);
|
||||
|
||||
suspended_socket = sock_fd;
|
||||
|
||||
SCH_SetFileHandlerEvent(suspended_socket, SCH_FILE_INPUT, 0);
|
||||
resume_timeout_id = SCH_AddTimeoutByDelay(RESUME_TIMEOUT, resume_timeout, NULL);
|
||||
|
||||
DEBUG_LOG("Suspended RX processing fd=%d", sock_fd);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_Linux_ProcessEvent(int sock_fd, int event)
|
||||
{
|
||||
if (sock_fd != monitored_socket)
|
||||
return 0;
|
||||
|
||||
if (event == SCH_FILE_INPUT) {
|
||||
suspend_socket(monitored_socket);
|
||||
monitored_socket = INVALID_SOCK_FD;
|
||||
|
||||
/* Don't process the message yet */
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static struct Interface *
|
||||
get_interface(int if_index)
|
||||
{
|
||||
@@ -552,26 +491,70 @@ get_interface(int if_index)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
poll_timeout(void *arg)
|
||||
{
|
||||
struct Interface *iface = arg;
|
||||
struct timespec now;
|
||||
|
||||
iface->poll_timeout_id = 0;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||
poll_phc(iface, &now);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
poll_phc(struct Interface *iface, struct timespec *now)
|
||||
{
|
||||
struct timespec sample_phc_ts, sample_sys_ts, sample_local_ts;
|
||||
struct timespec phc_readings[PHC_READINGS][3];
|
||||
double phc_err, local_err, interval;
|
||||
int n_readings;
|
||||
|
||||
if (!HCL_NeedsNewSample(iface->clock, now))
|
||||
return;
|
||||
|
||||
DEBUG_LOG("Polling PHC on %s%s",
|
||||
iface->name, iface->poll_timeout_id != 0 ? " before timeout" : "");
|
||||
|
||||
n_readings = SYS_Linux_GetPHCReadings(iface->phc_fd, iface->phc_nocrossts,
|
||||
&iface->phc_mode, PHC_READINGS, phc_readings);
|
||||
|
||||
/* Add timeout for the next poll in case no HW timestamp will be captured
|
||||
between the minpoll and maxpoll. Separate reading of different PHCs to
|
||||
avoid long intervals between handling I/O events. */
|
||||
SCH_RemoveTimeout(iface->poll_timeout_id);
|
||||
interval = UTI_Log2ToDouble(iface->maxpoll);
|
||||
iface->poll_timeout_id = SCH_AddTimeoutInClass(interval, interval /
|
||||
ARR_GetSize(interfaces) / 4, 0.1,
|
||||
SCH_PhcPollClass, poll_timeout, iface);
|
||||
|
||||
if (n_readings <= 0)
|
||||
return;
|
||||
|
||||
if (!HCL_ProcessReadings(iface->clock, n_readings, phc_readings,
|
||||
&sample_phc_ts, &sample_sys_ts, &phc_err))
|
||||
return;
|
||||
|
||||
LCL_CookTime(&sample_sys_ts, &sample_local_ts, &local_err);
|
||||
HCL_AccumulateSample(iface->clock, &sample_phc_ts, &sample_local_ts, phc_err + local_err);
|
||||
|
||||
update_interface_speed(iface);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
|
||||
NTP_Local_Timestamp *local_ts, int rx_ntp_length, int family,
|
||||
int l2_length)
|
||||
{
|
||||
struct timespec sample_phc_ts, sample_sys_ts, sample_local_ts, ts;
|
||||
double rx_correction, ts_delay, phc_err, local_err;
|
||||
double rx_correction, ts_delay, local_err;
|
||||
struct timespec ts;
|
||||
|
||||
if (HCL_NeedsNewSample(iface->clock, &local_ts->ts)) {
|
||||
if (!SYS_Linux_GetPHCSample(iface->phc_fd, iface->phc_nocrossts, iface->precision,
|
||||
&iface->phc_mode, &sample_phc_ts, &sample_sys_ts,
|
||||
&phc_err))
|
||||
return;
|
||||
|
||||
LCL_CookTime(&sample_sys_ts, &sample_local_ts, &local_err);
|
||||
HCL_AccumulateSample(iface->clock, &sample_phc_ts, &sample_local_ts,
|
||||
phc_err + local_err);
|
||||
|
||||
update_interface_speed(iface);
|
||||
}
|
||||
poll_phc(iface, &local_ts->ts);
|
||||
|
||||
/* We need to transpose RX timestamps as hardware timestamps are normally
|
||||
preamble timestamps and RX timestamps in NTP are supposed to be trailer
|
||||
@@ -611,6 +594,28 @@ process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
|
||||
local_ts->source = NTP_TS_HARDWARE;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
process_sw_timestamp(struct timespec *sw_ts, NTP_Local_Timestamp *local_ts)
|
||||
{
|
||||
double ts_delay, local_err;
|
||||
struct timespec ts;
|
||||
|
||||
LCL_CookTime(sw_ts, &ts, &local_err);
|
||||
|
||||
ts_delay = UTI_DiffTimespecsToDouble(&local_ts->ts, &ts);
|
||||
|
||||
if (fabs(ts_delay) > MAX_TS_DELAY) {
|
||||
DEBUG_LOG("Unacceptable timestamp delay %.9f", ts_delay);
|
||||
return;
|
||||
}
|
||||
|
||||
local_ts->ts = ts;
|
||||
local_ts->err = local_err;
|
||||
local_ts->source = NTP_TS_KERNEL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Extract UDP data from a layer 2 message. Supported is Ethernet
|
||||
with optional VLAN tags. */
|
||||
@@ -727,17 +732,11 @@ NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr,
|
||||
} else {
|
||||
DEBUG_LOG("HW clock not found for interface %d", ts_if_index);
|
||||
}
|
||||
|
||||
/* If a HW transmit timestamp was received, resume processing
|
||||
of non-error messages on this socket */
|
||||
if (is_tx)
|
||||
resume_socket(local_addr->sock_fd);
|
||||
}
|
||||
|
||||
if (local_ts->source == NTP_TS_DAEMON && !UTI_IsZeroTimespec(&message->timestamp.kernel) &&
|
||||
(!is_tx || UTI_IsZeroTimespec(&message->timestamp.hw))) {
|
||||
LCL_CookTime(&message->timestamp.kernel, &local_ts->ts, &local_ts->err);
|
||||
local_ts->source = NTP_TS_KERNEL;
|
||||
process_sw_timestamp(&message->timestamp.kernel, local_ts);
|
||||
}
|
||||
|
||||
/* If the kernel is slow with enabling RX timestamping, open a dummy
|
||||
@@ -776,7 +775,10 @@ NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr,
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (message->length < NTP_HEADER_LENGTH)
|
||||
if (!NIO_UnwrapMessage(message, local_addr->sock_fd))
|
||||
return 1;
|
||||
|
||||
if (message->length < NTP_HEADER_LENGTH || message->length > sizeof (NTP_Packet))
|
||||
return 1;
|
||||
|
||||
NSR_ProcessTx(&message->remote_addr.ip, local_addr, local_ts, message->data, message->length);
|
||||
@@ -792,23 +794,9 @@ NIO_Linux_RequestTxTimestamp(SCK_Message *message, int sock_fd)
|
||||
if (!ts_flags)
|
||||
return;
|
||||
|
||||
/* If a HW transmit timestamp is requested on a client socket, monitor
|
||||
events on the socket in order to avoid processing of a fast response
|
||||
without the HW timestamp of the request */
|
||||
if (ts_tx_flags & SOF_TIMESTAMPING_TX_HARDWARE && !NIO_IsServerSocket(sock_fd))
|
||||
monitored_socket = sock_fd;
|
||||
|
||||
/* Check if TX timestamping is disabled on this socket */
|
||||
if (permanent_ts_options || !NIO_IsServerSocket(sock_fd))
|
||||
return;
|
||||
|
||||
message->timestamp.tx_flags = ts_tx_flags;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NIO_Linux_NotifySocketClosing(int sock_fd)
|
||||
{
|
||||
resume_socket(sock_fd);
|
||||
}
|
||||
|
||||
@@ -35,13 +35,9 @@ extern void NIO_Linux_Finalise(void);
|
||||
|
||||
extern int NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events);
|
||||
|
||||
extern int NIO_Linux_ProcessEvent(int sock_fd, int event);
|
||||
|
||||
extern int NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr,
|
||||
NTP_Local_Timestamp *local_ts, int event);
|
||||
|
||||
extern void NIO_Linux_RequestTxTimestamp(SCK_Message *message, int sock_fd);
|
||||
|
||||
extern void NIO_Linux_NotifySocketClosing(int sock_fd);
|
||||
|
||||
#endif
|
||||
|
||||
225
ntp_sources.c
225
ntp_sources.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2011-2012, 2014, 2016, 2020
|
||||
* Copyright (C) Miroslav Lichvar 2011-2012, 2014, 2016, 2020-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -45,6 +45,9 @@
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
/* Maximum number of sources */
|
||||
#define MAX_SOURCES 65536
|
||||
|
||||
/* Record type private to this file, used to store information about
|
||||
particular sources */
|
||||
typedef struct {
|
||||
@@ -53,7 +56,8 @@ typedef struct {
|
||||
(an IPADDR_ID address means the address
|
||||
is not resolved yet) */
|
||||
NCR_Instance data; /* Data for the protocol engine for this source */
|
||||
char *name; /* Name of the source, may be NULL */
|
||||
char *name; /* Name of the source as it was specified
|
||||
(may be an IP address) */
|
||||
int pool_id; /* ID of the pool from which was this source
|
||||
added or INVALID_POOL */
|
||||
int tentative; /* Flag indicating there was no valid response
|
||||
@@ -72,6 +76,9 @@ static int n_sources;
|
||||
/* Flag indicating new sources will be started automatically when added */
|
||||
static int auto_start_sources = 0;
|
||||
|
||||
/* Flag indicating a record is currently being modified */
|
||||
static int record_lock;
|
||||
|
||||
/* Last assigned address ID */
|
||||
static uint32_t last_address_id = 0;
|
||||
|
||||
@@ -100,6 +107,7 @@ struct UnresolvedSource {
|
||||
|
||||
static struct UnresolvedSource *unresolved_sources = NULL;
|
||||
static int resolving_interval = 0;
|
||||
static int resolving_restart = 0;
|
||||
static SCH_TimeoutID resolving_id;
|
||||
static struct UnresolvedSource *resolving_source = NULL;
|
||||
static NSR_SourceResolvingEndHandler resolving_end_handler = NULL;
|
||||
@@ -122,11 +130,21 @@ struct SourcePool {
|
||||
/* Array of SourcePool (indexed by their ID) */
|
||||
static ARR_Instance pools;
|
||||
|
||||
/* Requested update of a source's address */
|
||||
struct AddressUpdate {
|
||||
NTP_Remote_Address old_address;
|
||||
NTP_Remote_Address new_address;
|
||||
};
|
||||
|
||||
/* Update saved when record_lock is true */
|
||||
static struct AddressUpdate saved_address_update;
|
||||
|
||||
/* ================================================== */
|
||||
/* Forward prototypes */
|
||||
|
||||
static void resolve_sources(void);
|
||||
static void rehash_records(void);
|
||||
static void handle_saved_address_update(void);
|
||||
static void clean_source_record(SourceRecord *record);
|
||||
static void remove_pool_sources(int pool_id, int tentative, int unresolved);
|
||||
static void remove_unresolved_source(struct UnresolvedSource *us);
|
||||
@@ -181,14 +199,7 @@ NSR_Initialise(void)
|
||||
void
|
||||
NSR_Finalise(void)
|
||||
{
|
||||
SourceRecord *record;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||
record = get_record(i);
|
||||
if (record->remote_addr)
|
||||
clean_source_record(record);
|
||||
}
|
||||
NSR_RemoveAllSources();
|
||||
|
||||
LCL_RemoveParameterChangeHandler(slew_sources, NULL);
|
||||
|
||||
@@ -275,6 +286,8 @@ rehash_records(void)
|
||||
unsigned int i, old_size, new_size;
|
||||
int slot;
|
||||
|
||||
assert(!record_lock);
|
||||
|
||||
old_size = ARR_GetSize(records);
|
||||
|
||||
temp_records = MallocArray(SourceRecord, old_size);
|
||||
@@ -304,6 +317,31 @@ rehash_records(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
log_source(SourceRecord *record, int addition, int once_per_pool)
|
||||
{
|
||||
int pool, log_addr;
|
||||
char *ip_str;
|
||||
|
||||
if (once_per_pool && record->pool_id != INVALID_POOL) {
|
||||
if (get_pool(record->pool_id)->sources > 1)
|
||||
return;
|
||||
pool = 1;
|
||||
log_addr = 0;
|
||||
} else {
|
||||
ip_str = UTI_IPToString(&record->remote_addr->ip_addr);
|
||||
pool = 0;
|
||||
log_addr = strcmp(record->name, ip_str) != 0;
|
||||
}
|
||||
|
||||
LOG(LOG_GetContextSeverity(LOGC_Command | LOGC_SourceFile), "%s %s %s%s%s%s",
|
||||
addition ? "Added" : "Removed", pool ? "pool" : "source",
|
||||
log_addr ? ip_str : record->name,
|
||||
log_addr ? " (" : "", log_addr ? record->name : "", log_addr ? ")" : "");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
/* Procedure to add a new source */
|
||||
static NSR_Status
|
||||
add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type,
|
||||
@@ -317,6 +355,11 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type,
|
||||
/* Find empty bin & check that we don't have the address already */
|
||||
if (find_slot2(remote_addr, &slot) != 0) {
|
||||
return NSR_AlreadyInUse;
|
||||
} else if (!name && !UTI_IsIPReal(&remote_addr->ip_addr)) {
|
||||
/* Name is required for non-real addresses */
|
||||
return NSR_InvalidName;
|
||||
} else if (n_sources >= MAX_SOURCES) {
|
||||
return NSR_TooManySources;
|
||||
} else {
|
||||
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
|
||||
remote_addr->ip_addr.family != IPADDR_INET6 &&
|
||||
@@ -331,14 +374,19 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type,
|
||||
assert(0);
|
||||
}
|
||||
|
||||
assert(!record_lock);
|
||||
record_lock = 1;
|
||||
|
||||
record = get_record(slot);
|
||||
record->data = NCR_CreateInstance(remote_addr, type, params, name);
|
||||
record->name = Strdup(name ? name : UTI_IPToString(&remote_addr->ip_addr));
|
||||
record->data = NCR_CreateInstance(remote_addr, type, params, record->name);
|
||||
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
||||
record->name = name ? Strdup(name) : NULL;
|
||||
record->pool_id = pool_id;
|
||||
record->tentative = 1;
|
||||
record->conf_id = conf_id;
|
||||
|
||||
record_lock = 0;
|
||||
|
||||
if (record->pool_id != INVALID_POOL) {
|
||||
get_pool(record->pool_id)->sources++;
|
||||
if (!UTI_IsIPReal(&remote_addr->ip_addr))
|
||||
@@ -348,6 +396,11 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type,
|
||||
if (auto_start_sources && UTI_IsIPReal(&remote_addr->ip_addr))
|
||||
NCR_StartInstance(record->data);
|
||||
|
||||
log_source(record, 1, 1);
|
||||
|
||||
/* The new instance is allowed to change its address immediately */
|
||||
handle_saved_address_update();
|
||||
|
||||
return NSR_Success;
|
||||
}
|
||||
}
|
||||
@@ -365,7 +418,7 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
||||
char *name;
|
||||
|
||||
found = find_slot2(old_addr, &slot1);
|
||||
if (found == 0)
|
||||
if (found != 2)
|
||||
return NSR_NoSuchSource;
|
||||
|
||||
/* Make sure there is no other source using the new address (with the same
|
||||
@@ -374,9 +427,16 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
||||
if (found == 2 || (found != 0 && slot1 != slot2))
|
||||
return NSR_AlreadyInUse;
|
||||
|
||||
assert(!record_lock);
|
||||
record_lock = 1;
|
||||
|
||||
record = get_record(slot1);
|
||||
NCR_ChangeRemoteAddress(record->data, new_addr, !replacement);
|
||||
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
||||
|
||||
if (record->remote_addr != NCR_GetRemoteAddress(record->data) ||
|
||||
UTI_CompareIPs(&record->remote_addr->ip_addr, &new_addr->ip_addr, NULL) != 0)
|
||||
assert(0);
|
||||
|
||||
if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) {
|
||||
if (auto_start_sources)
|
||||
NCR_StartInstance(record->data);
|
||||
@@ -391,6 +451,8 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
||||
get_pool(record->pool_id)->confirmed_sources--;
|
||||
}
|
||||
|
||||
record_lock = 0;
|
||||
|
||||
name = record->name;
|
||||
severity = UTI_IsIPReal(&old_addr->ip_addr) ? LOGS_INFO : LOGS_DEBUG;
|
||||
|
||||
@@ -400,10 +462,10 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
||||
|
||||
LOG(severity, "Source %s %s %s (%s)", UTI_IPToString(&old_addr->ip_addr),
|
||||
replacement ? "replaced with" : "changed to",
|
||||
UTI_IPToString(&new_addr->ip_addr), name ? name : "");
|
||||
UTI_IPToString(&new_addr->ip_addr), name);
|
||||
} else {
|
||||
LOG(severity, "Source %s (%s) changed port to %d",
|
||||
UTI_IPToString(&new_addr->ip_addr), name ? name : "", new_addr->port);
|
||||
UTI_IPToString(&new_addr->ip_addr), name, new_addr->port);
|
||||
}
|
||||
|
||||
return NSR_Success;
|
||||
@@ -411,6 +473,24 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_saved_address_update(void)
|
||||
{
|
||||
if (!UTI_IsIPReal(&saved_address_update.old_address.ip_addr))
|
||||
return;
|
||||
|
||||
if (change_source_address(&saved_address_update.old_address,
|
||||
&saved_address_update.new_address, 0) != NSR_Success)
|
||||
/* This is expected to happen only if the old address is wrong */
|
||||
LOG(LOGS_ERR, "Could not change %s to %s",
|
||||
UTI_IPSockAddrToString(&saved_address_update.old_address),
|
||||
UTI_IPSockAddrToString(&saved_address_update.new_address));
|
||||
|
||||
saved_address_update.old_address.ip_addr.family = IPADDR_UNSPEC;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
replace_source_connectable(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
|
||||
{
|
||||
@@ -422,6 +502,8 @@ replace_source_connectable(NTP_Remote_Address *old_addr, NTP_Remote_Address *new
|
||||
if (change_source_address(old_addr, new_addr, 1) == NSR_AlreadyInUse)
|
||||
return 0;
|
||||
|
||||
handle_saved_address_update();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -524,6 +606,13 @@ name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *any
|
||||
if (status == DNS_Failure || UTI_IsIPReal(&us->address.ip_addr) || is_resolved(us))
|
||||
remove_unresolved_source(us);
|
||||
|
||||
/* If a restart was requested and this was the last source in the list,
|
||||
start with the first source again (if there still is one) */
|
||||
if (!next && resolving_restart) {
|
||||
next = unresolved_sources;
|
||||
resolving_restart = 0;
|
||||
}
|
||||
|
||||
resolving_source = next;
|
||||
|
||||
if (next) {
|
||||
@@ -635,21 +724,25 @@ static int get_unused_pool_id(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static uint32_t
|
||||
get_next_conf_id(uint32_t *conf_id)
|
||||
{
|
||||
last_conf_id++;
|
||||
|
||||
if (conf_id)
|
||||
*conf_id = last_conf_id;
|
||||
|
||||
return last_conf_id;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NSR_Status
|
||||
NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
|
||||
SourceParameters *params, uint32_t *conf_id)
|
||||
{
|
||||
NSR_Status s;
|
||||
|
||||
s = add_source(remote_addr, NULL, type, params, INVALID_POOL, last_conf_id + 1);
|
||||
if (s != NSR_Success)
|
||||
return s;
|
||||
|
||||
last_conf_id++;
|
||||
if (conf_id)
|
||||
*conf_id = last_conf_id;
|
||||
|
||||
return s;
|
||||
return add_source(remote_addr, NULL, type, params, INVALID_POOL,
|
||||
get_next_conf_id(conf_id));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -662,12 +755,13 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
|
||||
struct SourcePool *sp;
|
||||
NTP_Remote_Address remote_addr;
|
||||
int i, new_sources, pool_id;
|
||||
uint32_t cid;
|
||||
|
||||
/* If the name is an IP address, don't bother with full resolving now
|
||||
or later when trying to replace the source */
|
||||
/* If the name is an IP address, add the source with the address directly */
|
||||
if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
|
||||
remote_addr.port = port;
|
||||
return NSR_AddSource(&remote_addr, type, params, conf_id);
|
||||
return add_source(&remote_addr, name, type, params, INVALID_POOL,
|
||||
get_next_conf_id(conf_id));
|
||||
}
|
||||
|
||||
/* Make sure the name is at least printable and has no spaces */
|
||||
@@ -708,14 +802,12 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
|
||||
|
||||
append_unresolved_source(us);
|
||||
|
||||
last_conf_id++;
|
||||
if (conf_id)
|
||||
*conf_id = last_conf_id;
|
||||
cid = get_next_conf_id(conf_id);
|
||||
|
||||
for (i = 0; i < new_sources; i++) {
|
||||
if (i > 0)
|
||||
remote_addr.ip_addr.addr.id = ++last_address_id;
|
||||
if (add_source(&remote_addr, name, type, params, us->pool_id, last_conf_id) != NSR_Success)
|
||||
if (add_source(&remote_addr, name, type, params, us->pool_id, cid) != NSR_Success)
|
||||
return NSR_TooManySources;
|
||||
}
|
||||
|
||||
@@ -737,7 +829,7 @@ NSR_ResolveSources(void)
|
||||
{
|
||||
/* Try to resolve unresolved sources now */
|
||||
if (unresolved_sources) {
|
||||
/* Make sure no resolving is currently running */
|
||||
/* Allow only one resolving to be running at a time */
|
||||
if (!resolving_source) {
|
||||
if (resolving_id != 0) {
|
||||
SCH_RemoveTimeout(resolving_id);
|
||||
@@ -745,6 +837,9 @@ NSR_ResolveSources(void)
|
||||
resolving_interval--;
|
||||
}
|
||||
resolve_sources();
|
||||
} else {
|
||||
/* Try again as soon as the current resolving ends */
|
||||
resolving_restart = 1;
|
||||
}
|
||||
} else {
|
||||
/* No unresolved sources, we are done */
|
||||
@@ -796,8 +891,7 @@ clean_source_record(SourceRecord *record)
|
||||
|
||||
record->remote_addr = NULL;
|
||||
NCR_DestroyInstance(record->data);
|
||||
if (record->name)
|
||||
Free(record->name);
|
||||
Free(record->name);
|
||||
|
||||
n_sources--;
|
||||
}
|
||||
@@ -817,6 +911,7 @@ NSR_RemoveSource(IPAddr *address)
|
||||
if (find_slot(address, &slot) == 0)
|
||||
return NSR_NoSuchSource;
|
||||
|
||||
log_source(get_record(slot), 0, 0);
|
||||
clean_source_record(get_record(slot));
|
||||
|
||||
/* Rehash the table to make sure there are no broken probe sequences.
|
||||
@@ -839,6 +934,7 @@ NSR_RemoveSourcesById(uint32_t conf_id)
|
||||
record = get_record(i);
|
||||
if (!record->remote_addr || record->conf_id != conf_id)
|
||||
continue;
|
||||
log_source(record, 0, 1);
|
||||
clean_source_record(record);
|
||||
}
|
||||
|
||||
@@ -870,7 +966,8 @@ resolve_source_replacement(SourceRecord *record)
|
||||
{
|
||||
struct UnresolvedSource *us;
|
||||
|
||||
DEBUG_LOG("trying to replace %s", UTI_IPToString(&record->remote_addr->ip_addr));
|
||||
DEBUG_LOG("trying to replace %s (%s)",
|
||||
UTI_IPToString(&record->remote_addr->ip_addr), record->name);
|
||||
|
||||
us = MallocNew(struct UnresolvedSource);
|
||||
us->name = Strdup(record->name);
|
||||
@@ -894,6 +991,7 @@ NSR_HandleBadSource(IPAddr *address)
|
||||
static struct timespec last_replacement;
|
||||
struct timespec now;
|
||||
SourceRecord *record;
|
||||
IPAddr ip_addr;
|
||||
double diff;
|
||||
int slot;
|
||||
|
||||
@@ -902,8 +1000,10 @@ NSR_HandleBadSource(IPAddr *address)
|
||||
|
||||
record = get_record(slot);
|
||||
|
||||
/* Only sources with a name can be replaced */
|
||||
if (!record->name)
|
||||
/* Don't try to replace a source specified by an IP address unless the
|
||||
address changed since the source was added (e.g. by NTS-KE) */
|
||||
if (UTI_StringToIP(record->name, &ip_addr) &&
|
||||
UTI_CompareIPs(&record->remote_addr->ip_addr, &ip_addr, NULL) == 0)
|
||||
return;
|
||||
|
||||
/* Don't resolve names too frequently */
|
||||
@@ -928,7 +1028,7 @@ NSR_RefreshAddresses(void)
|
||||
|
||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||
record = get_record(i);
|
||||
if (!record->remote_addr || !record->name)
|
||||
if (!record->remote_addr)
|
||||
continue;
|
||||
|
||||
resolve_source_replacement(record);
|
||||
@@ -940,10 +1040,28 @@ NSR_RefreshAddresses(void)
|
||||
NSR_Status
|
||||
NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
|
||||
{
|
||||
if (new_addr->ip_addr.family == IPADDR_UNSPEC)
|
||||
int slot;
|
||||
|
||||
if (!UTI_IsIPReal(&old_addr->ip_addr) || !UTI_IsIPReal(&new_addr->ip_addr))
|
||||
return NSR_InvalidAF;
|
||||
|
||||
return change_source_address(old_addr, new_addr, 0);
|
||||
if (UTI_CompareIPs(&old_addr->ip_addr, &new_addr->ip_addr, NULL) != 0 &&
|
||||
find_slot(&new_addr->ip_addr, &slot))
|
||||
return NSR_AlreadyInUse;
|
||||
|
||||
/* If a record is being modified (e.g. by change_source_address(), or the
|
||||
source is just being created), postpone the change to avoid corruption */
|
||||
|
||||
if (!record_lock)
|
||||
return change_source_address(old_addr, new_addr, 0);
|
||||
|
||||
if (UTI_IsIPReal(&saved_address_update.old_address.ip_addr))
|
||||
return NSR_TooManySources;
|
||||
|
||||
saved_address_update.old_address = *old_addr;
|
||||
saved_address_update.new_address = *new_addr;
|
||||
|
||||
return NSR_Success;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -992,17 +1110,12 @@ NSR_GetLocalRefid(IPAddr *address)
|
||||
char *
|
||||
NSR_GetName(IPAddr *address)
|
||||
{
|
||||
SourceRecord *record;
|
||||
int slot;
|
||||
|
||||
if (!find_slot(address, &slot))
|
||||
return 0;
|
||||
return NULL;
|
||||
|
||||
record = get_record(slot);
|
||||
if (record->name)
|
||||
return record->name;
|
||||
|
||||
return UTI_IPToString(&record->remote_addr->ip_addr);
|
||||
return get_record(slot)->name;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1019,8 +1132,10 @@ NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
|
||||
assert(initialised);
|
||||
|
||||
/* Must match IP address AND port number */
|
||||
if (find_slot2(remote_addr, &slot) == 2) {
|
||||
/* Avoid unnecessary lookup if the packet cannot be a response from our
|
||||
source. Otherwise, it must match both IP address and port number. */
|
||||
if (NTP_LVM_TO_MODE(message->lvm) != MODE_CLIENT &&
|
||||
find_slot2(remote_addr, &slot) == 2) {
|
||||
record = get_record(slot);
|
||||
|
||||
if (!NCR_ProcessRxKnown(record->data, local_addr, rx_ts, message, length))
|
||||
@@ -1056,8 +1171,10 @@ NSR_ProcessTx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
SourceRecord *record;
|
||||
int slot;
|
||||
|
||||
/* Must match IP address AND port number */
|
||||
if (find_slot2(remote_addr, &slot) == 2) {
|
||||
/* Avoid unnecessary lookup if the packet cannot be a request to our
|
||||
source. Otherwise, it must match both IP address and port number. */
|
||||
if (NTP_LVM_TO_MODE(message->lvm) != MODE_SERVER &&
|
||||
find_slot2(remote_addr, &slot) == 2) {
|
||||
record = get_record(slot);
|
||||
NCR_ProcessTxKnown(record->data, local_addr, tx_ts, message, length);
|
||||
} else {
|
||||
|
||||
@@ -91,15 +91,16 @@ extern void NSR_HandleBadSource(IPAddr *address);
|
||||
/* Procedure to resolve all names again */
|
||||
extern void NSR_RefreshAddresses(void);
|
||||
|
||||
/* Procedure to update the address of a source */
|
||||
/* Procedure to update the address of a source. The update may be
|
||||
postponed. */
|
||||
extern NSR_Status NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr,
|
||||
NTP_Remote_Address *new_addr);
|
||||
|
||||
/* Procedure to get local reference ID corresponding to a source */
|
||||
extern uint32_t NSR_GetLocalRefid(IPAddr *address);
|
||||
|
||||
/* Procedure to get the name of a source. If the source doesn't have a name,
|
||||
it returns a temporary string containing formatted address. */
|
||||
/* Procedure to get the name of a source as it was specified (it may be
|
||||
an IP address) */
|
||||
extern char *NSR_GetName(IPAddr *address);
|
||||
|
||||
/* This routine is called by ntp_io when a new packet arrives off the network */
|
||||
|
||||
105
nts_ke_client.c
105
nts_ke_client.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
* Copyright (C) Miroslav Lichvar 2020-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -44,6 +44,7 @@
|
||||
struct NKC_Instance_Record {
|
||||
char *name;
|
||||
IPSockAddr address;
|
||||
NKSN_Credentials credentials;
|
||||
NKSN_Instance session;
|
||||
int destroying;
|
||||
int got_response;
|
||||
@@ -52,14 +53,14 @@ struct NKC_Instance_Record {
|
||||
NKE_Context context;
|
||||
NKE_Cookie cookies[NKE_MAX_COOKIES];
|
||||
int num_cookies;
|
||||
char server_name[NKE_MAX_RECORD_BODY_LENGTH + 1];
|
||||
char server_name[NKE_MAX_RECORD_BODY_LENGTH + 2];
|
||||
IPSockAddr ntp_address;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void *client_credentials = NULL;
|
||||
static int client_credentials_refs = 0;
|
||||
static NKSN_Credentials default_credentials = NULL;
|
||||
static int default_credentials_refs = 0;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -101,16 +102,22 @@ static int
|
||||
prepare_request(NKC_Instance inst)
|
||||
{
|
||||
NKSN_Instance session = inst->session;
|
||||
uint16_t datum;
|
||||
uint16_t data[2];
|
||||
int length;
|
||||
|
||||
NKSN_BeginMessage(session);
|
||||
|
||||
datum = htons(NKE_NEXT_PROTOCOL_NTPV4);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, &datum, sizeof (datum)))
|
||||
data[0] = htons(NKE_NEXT_PROTOCOL_NTPV4);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, data, sizeof (data[0])))
|
||||
return 0;
|
||||
|
||||
datum = htons(AEAD_AES_SIV_CMAC_256);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_AEAD_ALGORITHM, &datum, sizeof (datum)))
|
||||
length = 0;
|
||||
if (SIV_GetKeyLength(AEAD_AES_128_GCM_SIV) > 0)
|
||||
data[length++] = htons(AEAD_AES_128_GCM_SIV);
|
||||
if (SIV_GetKeyLength(AEAD_AES_SIV_CMAC_256) > 0)
|
||||
data[length++] = htons(AEAD_AES_SIV_CMAC_256);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_AEAD_ALGORITHM, data,
|
||||
length * sizeof (data[0])))
|
||||
return 0;
|
||||
|
||||
if (!NKSN_EndMessage(session))
|
||||
@@ -126,9 +133,10 @@ process_response(NKC_Instance inst)
|
||||
{
|
||||
int next_protocol = -1, aead_algorithm = -1, error = 0;
|
||||
int i, critical, type, length;
|
||||
uint16_t data[NKE_MAX_COOKIE_LENGTH / sizeof (uint16_t)];
|
||||
uint16_t data[NKE_MAX_RECORD_BODY_LENGTH / sizeof (uint16_t)];
|
||||
|
||||
assert(NKE_MAX_COOKIE_LENGTH % sizeof (uint16_t) == 0);
|
||||
assert(NKE_MAX_COOKIE_LENGTH <= NKE_MAX_RECORD_BODY_LENGTH);
|
||||
assert(sizeof (data) % sizeof (uint16_t) == 0);
|
||||
assert(sizeof (uint16_t) == 2);
|
||||
|
||||
inst->num_cookies = 0;
|
||||
@@ -140,6 +148,13 @@ process_response(NKC_Instance inst)
|
||||
if (!NKSN_GetRecord(inst->session, &critical, &type, &length, &data, sizeof (data)))
|
||||
break;
|
||||
|
||||
if (length > sizeof (data)) {
|
||||
DEBUG_LOG("Record too long type=%d length=%d critical=%d", type, length, critical);
|
||||
if (critical)
|
||||
error = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case NKE_RECORD_NEXT_PROTOCOL:
|
||||
if (!critical || length != 2 || ntohs(data[0]) != NKE_NEXT_PROTOCOL_NTPV4) {
|
||||
@@ -150,12 +165,14 @@ process_response(NKC_Instance inst)
|
||||
next_protocol = NKE_NEXT_PROTOCOL_NTPV4;
|
||||
break;
|
||||
case NKE_RECORD_AEAD_ALGORITHM:
|
||||
if (length != 2 || ntohs(data[0]) != AEAD_AES_SIV_CMAC_256) {
|
||||
if (length != 2 || (ntohs(data[0]) != AEAD_AES_SIV_CMAC_256 &&
|
||||
ntohs(data[0]) != AEAD_AES_128_GCM_SIV) ||
|
||||
SIV_GetKeyLength(ntohs(data[0])) <= 0) {
|
||||
DEBUG_LOG("Unexpected NTS-KE AEAD algorithm");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
aead_algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
aead_algorithm = ntohs(data[0]);
|
||||
inst->context.algorithm = aead_algorithm;
|
||||
break;
|
||||
case NKE_RECORD_ERROR:
|
||||
@@ -227,7 +244,7 @@ process_response(NKC_Instance inst)
|
||||
|
||||
if (error || inst->num_cookies == 0 ||
|
||||
next_protocol != NKE_NEXT_PROTOCOL_NTPV4 ||
|
||||
aead_algorithm != AEAD_AES_SIV_CMAC_256)
|
||||
aead_algorithm < 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -253,6 +270,17 @@ handle_message(void *arg)
|
||||
if (inst->resolving_name)
|
||||
return 0;
|
||||
if (!UTI_StringToIP(inst->server_name, &inst->ntp_address.ip_addr)) {
|
||||
int length = strlen(inst->server_name);
|
||||
|
||||
/* Add a trailing dot if not present to force the name to be
|
||||
resolved as a fully qualified domain name */
|
||||
if (length < 1 || length + 1 >= sizeof (inst->server_name))
|
||||
return 0;
|
||||
if (inst->server_name[length - 1] != '.') {
|
||||
inst->server_name[length] = '.';
|
||||
inst->server_name[length + 1] = '\0';
|
||||
}
|
||||
|
||||
DNS_Name2IPAddressAsync(inst->server_name, name_resolve_handler, inst);
|
||||
inst->resolving_name = 1;
|
||||
}
|
||||
@@ -266,9 +294,12 @@ handle_message(void *arg)
|
||||
/* ================================================== */
|
||||
|
||||
NKC_Instance
|
||||
NKC_CreateInstance(IPSockAddr *address, const char *name)
|
||||
NKC_CreateInstance(IPSockAddr *address, const char *name, uint32_t cert_set)
|
||||
{
|
||||
const char **trusted_certs;
|
||||
uint32_t *certs_ids;
|
||||
NKC_Instance inst;
|
||||
int n_certs;
|
||||
|
||||
inst = MallocNew(struct NKC_Instance_Record);
|
||||
|
||||
@@ -279,10 +310,21 @@ NKC_CreateInstance(IPSockAddr *address, const char *name)
|
||||
inst->destroying = 0;
|
||||
inst->got_response = 0;
|
||||
|
||||
/* Share the credentials with other client instances */
|
||||
if (!client_credentials)
|
||||
client_credentials = NKSN_CreateCertCredentials(NULL, NULL, CNF_GetNtsTrustedCertFile());
|
||||
client_credentials_refs++;
|
||||
n_certs = CNF_GetNtsTrustedCertsPaths(&trusted_certs, &certs_ids);
|
||||
|
||||
/* Share the credentials among clients using the default set of trusted
|
||||
certificates, which likely contains most certificates */
|
||||
if (cert_set == 0) {
|
||||
if (!default_credentials)
|
||||
default_credentials = NKSN_CreateClientCertCredentials(trusted_certs, certs_ids,
|
||||
n_certs, cert_set);
|
||||
inst->credentials = default_credentials;
|
||||
if (default_credentials)
|
||||
default_credentials_refs++;
|
||||
} else {
|
||||
inst->credentials = NKSN_CreateClientCertCredentials(trusted_certs, certs_ids,
|
||||
n_certs, cert_set);
|
||||
}
|
||||
|
||||
return inst;
|
||||
}
|
||||
@@ -296,10 +338,16 @@ NKC_DestroyInstance(NKC_Instance inst)
|
||||
|
||||
Free(inst->name);
|
||||
|
||||
client_credentials_refs--;
|
||||
if (client_credentials_refs <= 0 && client_credentials) {
|
||||
NKSN_DestroyCertCredentials(client_credentials);
|
||||
client_credentials = NULL;
|
||||
if (inst->credentials) {
|
||||
if (inst->credentials == default_credentials) {
|
||||
default_credentials_refs--;
|
||||
if (default_credentials_refs <= 0) {
|
||||
NKSN_DestroyCertCredentials(default_credentials);
|
||||
default_credentials = NULL;
|
||||
}
|
||||
} else {
|
||||
NKSN_DestroyCertCredentials(inst->credentials);
|
||||
}
|
||||
}
|
||||
|
||||
/* If the asynchronous resolver is running, let the handler free
|
||||
@@ -325,11 +373,18 @@ NKC_Start(NKC_Instance inst)
|
||||
|
||||
inst->got_response = 0;
|
||||
|
||||
if (!client_credentials) {
|
||||
if (!inst->credentials) {
|
||||
DEBUG_LOG("Missing client credentials");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Don't try to connect if missing the algorithm which all servers
|
||||
are required to support */
|
||||
if (SIV_GetKeyLength(AEAD_AES_SIV_CMAC_256) <= 0) {
|
||||
LOG(LOGS_ERR, "Missing AES-SIV-CMAC-256");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Follow the bindacqaddress and bindacqdevice settings */
|
||||
CNF_GetBindAcquisitionAddress(inst->address.ip_addr.family, &local_addr.ip_addr);
|
||||
local_addr.port = 0;
|
||||
@@ -347,7 +402,7 @@ NKC_Start(NKC_Instance inst)
|
||||
}
|
||||
|
||||
/* Start an NTS-KE session */
|
||||
if (!NKSN_StartSession(inst->session, sock_fd, label, client_credentials, CLIENT_TIMEOUT)) {
|
||||
if (!NKSN_StartSession(inst->session, sock_fd, label, inst->credentials, CLIENT_TIMEOUT)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
typedef struct NKC_Instance_Record *NKC_Instance;
|
||||
|
||||
/* Create a client NTS-KE instance */
|
||||
extern NKC_Instance NKC_CreateInstance(IPSockAddr *address, const char *name);
|
||||
extern NKC_Instance NKC_CreateInstance(IPSockAddr *address, const char *name, uint32_t cert_set);
|
||||
|
||||
/* Destroy an instance */
|
||||
extern void NKC_DestroyInstance(NKC_Instance inst);
|
||||
|
||||
234
nts_ke_server.c
234
nts_ke_server.c
@@ -47,31 +47,33 @@
|
||||
|
||||
#define SERVER_TIMEOUT 2.0
|
||||
|
||||
#define SERVER_COOKIE_SIV AEAD_AES_SIV_CMAC_256
|
||||
#define SERVER_COOKIE_NONCE_LENGTH 16
|
||||
#define MAX_COOKIE_NONCE_LENGTH 16
|
||||
|
||||
#define KEY_ID_INDEX_BITS 2
|
||||
#define MAX_SERVER_KEYS (1U << KEY_ID_INDEX_BITS)
|
||||
#define FUTURE_KEYS 1
|
||||
|
||||
#define DUMP_FILENAME "ntskeys"
|
||||
#define DUMP_IDENTIFIER "NKS0\n"
|
||||
#define DUMP_IDENTIFIER "NKS1\n"
|
||||
#define OLD_DUMP_IDENTIFIER "NKS0\n"
|
||||
|
||||
#define INVALID_SOCK_FD (-7)
|
||||
|
||||
typedef struct {
|
||||
uint32_t key_id;
|
||||
unsigned char nonce[SERVER_COOKIE_NONCE_LENGTH];
|
||||
} ServerCookieHeader;
|
||||
|
||||
typedef struct {
|
||||
uint32_t id;
|
||||
unsigned char key[SIV_MAX_KEY_LENGTH];
|
||||
SIV_Algorithm siv_algorithm;
|
||||
SIV_Instance siv;
|
||||
int nonce_length;
|
||||
} ServerKey;
|
||||
|
||||
typedef struct {
|
||||
uint32_t key_id;
|
||||
uint32_t siv_algorithm;
|
||||
unsigned char key[SIV_MAX_KEY_LENGTH];
|
||||
IPAddr client_addr;
|
||||
uint16_t client_port;
|
||||
@@ -95,7 +97,7 @@ static int initialised = 0;
|
||||
|
||||
/* Array of NKSN instances */
|
||||
static ARR_Instance sessions;
|
||||
static void *server_credentials;
|
||||
static NKSN_Credentials server_credentials;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -148,12 +150,30 @@ handle_client(int sock_fd, IPSockAddr *addr)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_key_siv(ServerKey *key, SIV_Algorithm algorithm)
|
||||
{
|
||||
if (!key->siv || key->siv_algorithm != algorithm) {
|
||||
if (key->siv)
|
||||
SIV_DestroyInstance(key->siv);
|
||||
key->siv_algorithm = algorithm;
|
||||
key->siv = SIV_CreateInstance(algorithm);
|
||||
key->nonce_length = MIN(SIV_GetMaxNonceLength(key->siv), MAX_COOKIE_NONCE_LENGTH);
|
||||
}
|
||||
|
||||
if (!key->siv || !SIV_SetKey(key->siv, key->key, SIV_GetKeyLength(key->siv_algorithm)))
|
||||
LOG_FATAL("Could not set SIV key");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_helper_request(int fd, int event, void *arg)
|
||||
{
|
||||
SCK_Message *message;
|
||||
HelperRequest *req;
|
||||
IPSockAddr client_addr;
|
||||
ServerKey *key;
|
||||
int sock_fd;
|
||||
|
||||
/* Receive the helper request with the NTS-KE session socket.
|
||||
@@ -181,16 +201,14 @@ handle_helper_request(int fd, int event, void *arg)
|
||||
req = message->data;
|
||||
|
||||
/* Extract the current server key and client address from the request */
|
||||
server_keys[current_server_key].id = ntohl(req->key_id);
|
||||
assert(sizeof (server_keys[current_server_key].key) == sizeof (req->key));
|
||||
memcpy(server_keys[current_server_key].key, req->key,
|
||||
sizeof (server_keys[current_server_key].key));
|
||||
key = &server_keys[current_server_key];
|
||||
key->id = ntohl(req->key_id);
|
||||
assert(sizeof (key->key) == sizeof (req->key));
|
||||
memcpy(key->key, req->key, sizeof (key->key));
|
||||
UTI_IPNetworkToHost(&req->client_addr, &client_addr.ip_addr);
|
||||
client_addr.port = ntohs(req->client_port);
|
||||
|
||||
if (!SIV_SetKey(server_keys[current_server_key].siv, server_keys[current_server_key].key,
|
||||
SIV_GetKeyLength(SERVER_COOKIE_SIV)))
|
||||
LOG_FATAL("Could not set SIV key");
|
||||
update_key_siv(key, ntohl(req->siv_algorithm));
|
||||
|
||||
if (!handle_client(sock_fd, &client_addr)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
@@ -240,6 +258,7 @@ accept_connection(int listening_fd, int event, void *arg)
|
||||
|
||||
/* Include the current server key and client address in the request */
|
||||
req.key_id = htonl(server_keys[current_server_key].id);
|
||||
req.siv_algorithm = htonl(server_keys[current_server_key].siv_algorithm);
|
||||
assert(sizeof (req.key) == sizeof (server_keys[current_server_key].key));
|
||||
memcpy(req.key, server_keys[current_server_key].key, sizeof (req.key));
|
||||
UTI_IPHostToNetwork(&addr.ip_addr, &req.client_addr);
|
||||
@@ -427,8 +446,9 @@ process_request(NKSN_Instance session)
|
||||
|
||||
for (i = 0; i < MIN(length, sizeof (data)) / 2; i++) {
|
||||
aead_algorithm_values++;
|
||||
if (ntohs(data[i]) == AEAD_AES_SIV_CMAC_256)
|
||||
aead_algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
/* Use the first supported algorithm */
|
||||
if (aead_algorithm < 0 && SIV_GetKeyLength(ntohs(data[i])) > 0)
|
||||
aead_algorithm = ntohs(data[i]);;
|
||||
}
|
||||
break;
|
||||
case NKE_RECORD_ERROR:
|
||||
@@ -470,28 +490,37 @@ handle_message(void *arg)
|
||||
static void
|
||||
generate_key(int index)
|
||||
{
|
||||
SIV_Algorithm algorithm;
|
||||
ServerKey *key;
|
||||
int key_length;
|
||||
|
||||
if (index < 0 || index >= MAX_SERVER_KEYS)
|
||||
assert(0);
|
||||
|
||||
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||
if (key_length > sizeof (server_keys[index].key))
|
||||
/* Prefer AES-128-GCM-SIV if available. Note that if older keys loaded
|
||||
from ntsdumpdir use a different algorithm, responding to NTP requests
|
||||
with cookies encrypted with those keys will not work if the new algorithm
|
||||
produces longer cookies (i.e. response would be longer than request).
|
||||
Switching from AES-SIV-CMAC-256 to AES-128-GCM-SIV is ok. */
|
||||
algorithm = SIV_GetKeyLength(AEAD_AES_128_GCM_SIV) > 0 ?
|
||||
AEAD_AES_128_GCM_SIV : AEAD_AES_SIV_CMAC_256;
|
||||
|
||||
key = &server_keys[index];
|
||||
|
||||
key_length = SIV_GetKeyLength(algorithm);
|
||||
if (key_length > sizeof (key->key))
|
||||
assert(0);
|
||||
|
||||
UTI_GetRandomBytesUrandom(server_keys[index].key, key_length);
|
||||
|
||||
if (!server_keys[index].siv ||
|
||||
!SIV_SetKey(server_keys[index].siv, server_keys[index].key, key_length))
|
||||
LOG_FATAL("Could not set SIV key");
|
||||
|
||||
UTI_GetRandomBytes(&server_keys[index].id, sizeof (server_keys[index].id));
|
||||
UTI_GetRandomBytesUrandom(key->key, key_length);
|
||||
UTI_GetRandomBytes(&key->id, sizeof (key->id));
|
||||
|
||||
/* Encode the index in the lowest bits of the ID */
|
||||
server_keys[index].id &= -1U << KEY_ID_INDEX_BITS;
|
||||
server_keys[index].id |= index;
|
||||
key->id &= -1U << KEY_ID_INDEX_BITS;
|
||||
key->id |= index;
|
||||
|
||||
DEBUG_LOG("Generated server key %"PRIX32, server_keys[index].id);
|
||||
update_key_siv(key, algorithm);
|
||||
|
||||
DEBUG_LOG("Generated key %08"PRIX32" (%d)", key->id, (int)key->siv_algorithm);
|
||||
|
||||
last_server_key_ts = SCH_GetLastEventMonoTime();
|
||||
}
|
||||
@@ -519,18 +548,19 @@ save_keys(void)
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||
last_key_age = SCH_GetLastEventMonoTime() - last_server_key_ts;
|
||||
|
||||
if (fprintf(f, "%s%d %.1f\n", DUMP_IDENTIFIER, SERVER_COOKIE_SIV, last_key_age) < 0)
|
||||
if (fprintf(f, "%s%.1f\n", DUMP_IDENTIFIER, last_key_age) < 0)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||
index = (current_server_key + i + 1 + FUTURE_KEYS) % MAX_SERVER_KEYS;
|
||||
key_length = SIV_GetKeyLength(server_keys[index].siv_algorithm);
|
||||
|
||||
if (key_length > sizeof (server_keys[index].key) ||
|
||||
!UTI_BytesToHex(server_keys[index].key, key_length, buf, sizeof (buf)) ||
|
||||
fprintf(f, "%08"PRIX32" %s\n", server_keys[index].id, buf) < 0)
|
||||
fprintf(f, "%08"PRIX32" %s %d\n", server_keys[index].id, buf,
|
||||
(int)server_keys[index].siv_algorithm) < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
@@ -545,7 +575,7 @@ save_keys(void)
|
||||
return;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s server keys", "save");
|
||||
LOG(LOGS_ERR, "Could not %s %s", "save", "server NTS keys");
|
||||
fclose(f);
|
||||
|
||||
if (!UTI_RemoveFile(dump_dir, DUMP_FILENAME, NULL))
|
||||
@@ -554,17 +584,16 @@ error:
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define MAX_WORDS 2
|
||||
#define MAX_WORDS 3
|
||||
|
||||
static int
|
||||
load_keys(void)
|
||||
{
|
||||
int i, index, key_length, algorithm = 0, old_ver;
|
||||
char *dump_dir, line[1024], *words[MAX_WORDS];
|
||||
unsigned char key[SIV_MAX_KEY_LENGTH];
|
||||
int i, index, key_length, algorithm;
|
||||
ServerKey new_keys[MAX_SERVER_KEYS];
|
||||
double key_age;
|
||||
FILE *f;
|
||||
uint32_t id;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
if (!dump_dir)
|
||||
@@ -574,43 +603,57 @@ load_keys(void)
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 2 ||
|
||||
sscanf(words[0], "%d", &algorithm) != 1 || algorithm != SERVER_COOKIE_SIV ||
|
||||
sscanf(words[1], "%lf", &key_age) != 1)
|
||||
if (!fgets(line, sizeof (line), f) ||
|
||||
(strcmp(line, DUMP_IDENTIFIER) != 0 && strcmp(line, OLD_DUMP_IDENTIFIER) != 0))
|
||||
goto error;
|
||||
|
||||
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||
last_server_key_ts = SCH_GetLastEventMonoTime() - MAX(key_age, 0.0);
|
||||
old_ver = strcmp(line, DUMP_IDENTIFIER) != 0;
|
||||
|
||||
if (!fgets(line, sizeof (line), f) ||
|
||||
UTI_SplitString(line, words, MAX_WORDS) != (old_ver ? 2 : 1) ||
|
||||
(old_ver && sscanf(words[0], "%d", &algorithm) != 1) ||
|
||||
sscanf(words[old_ver ? 1 : 0], "%lf", &key_age) != 1)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < MAX_SERVER_KEYS && fgets(line, sizeof (line), f); i++) {
|
||||
if (UTI_SplitString(line, words, MAX_WORDS) != 2 ||
|
||||
sscanf(words[0], "%"PRIX32, &id) != 1)
|
||||
if (UTI_SplitString(line, words, MAX_WORDS) != (old_ver ? 2 : 3) ||
|
||||
sscanf(words[0], "%"PRIX32, &new_keys[i].id) != 1 ||
|
||||
(!old_ver && sscanf(words[2], "%d", &algorithm) != 1))
|
||||
goto error;
|
||||
|
||||
if (UTI_HexToBytes(words[1], key, sizeof (key)) != key_length)
|
||||
new_keys[i].siv_algorithm = algorithm;
|
||||
key_length = SIV_GetKeyLength(algorithm);
|
||||
|
||||
if ((i > 0 && (new_keys[i].id - new_keys[i - 1].id) % MAX_SERVER_KEYS != 1) ||
|
||||
key_length <= 0 ||
|
||||
UTI_HexToBytes(words[1], new_keys[i].key, sizeof (new_keys[i].key)) != key_length)
|
||||
goto error;
|
||||
|
||||
index = id % MAX_SERVER_KEYS;
|
||||
|
||||
server_keys[index].id = id;
|
||||
assert(sizeof (server_keys[index].key) == sizeof (key));
|
||||
memcpy(server_keys[index].key, key, key_length);
|
||||
|
||||
if (!SIV_SetKey(server_keys[index].siv, server_keys[index].key, key_length))
|
||||
LOG_FATAL("Could not set SIV key");
|
||||
|
||||
DEBUG_LOG("Loaded key %"PRIX32, id);
|
||||
|
||||
current_server_key = (index + MAX_SERVER_KEYS - FUTURE_KEYS) % MAX_SERVER_KEYS;
|
||||
}
|
||||
|
||||
if (i < MAX_SERVER_KEYS)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||
index = new_keys[i].id % MAX_SERVER_KEYS;
|
||||
server_keys[index].id = new_keys[i].id;
|
||||
memcpy(server_keys[index].key, new_keys[i].key, sizeof (server_keys[index].key));
|
||||
|
||||
update_key_siv(&server_keys[index], new_keys[i].siv_algorithm);
|
||||
|
||||
DEBUG_LOG("Loaded key %08"PRIX32" (%d)",
|
||||
server_keys[index].id, (int)server_keys[index].siv_algorithm);
|
||||
}
|
||||
|
||||
current_server_key = (index + MAX_SERVER_KEYS - FUTURE_KEYS) % MAX_SERVER_KEYS;
|
||||
last_server_key_ts = SCH_GetLastEventMonoTime() - MAX(key_age, 0.0);
|
||||
|
||||
fclose(f);
|
||||
|
||||
LOG(LOGS_ERR, "Loaded %s", "server NTS keys");
|
||||
return 1;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s server keys", "load");
|
||||
LOG(LOGS_ERR, "Could not %s %s", "load", "server NTS keys");
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
@@ -669,6 +712,8 @@ run_helper(uid_t uid, gid_t gid, int scfilter_level)
|
||||
CNF_Finalise();
|
||||
LOG_Finalise();
|
||||
|
||||
UTI_ResetGetRandomFunctions();
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -678,13 +723,14 @@ void
|
||||
NKS_PreInitialise(uid_t uid, gid_t gid, int scfilter_level)
|
||||
{
|
||||
int i, processes, sock_fd1, sock_fd2;
|
||||
const char **certs, **keys;
|
||||
char prefix[16];
|
||||
pid_t pid;
|
||||
|
||||
helper_sock_fd = INVALID_SOCK_FD;
|
||||
is_helper = 0;
|
||||
|
||||
if (!CNF_GetNtsServerCertFile() || !CNF_GetNtsServerKeyFile())
|
||||
if (CNF_GetNtsServerCertAndKeyFiles(&certs, &keys) <= 0)
|
||||
return;
|
||||
|
||||
processes = CNF_GetNtsServerProcesses();
|
||||
@@ -709,6 +755,8 @@ NKS_PreInitialise(uid_t uid, gid_t gid, int scfilter_level)
|
||||
|
||||
is_helper = 1;
|
||||
|
||||
UTI_ResetGetRandomFunctions();
|
||||
|
||||
snprintf(prefix, sizeof (prefix), "nks#%d:", i + 1);
|
||||
LOG_SetDebugPrefix(prefix);
|
||||
LOG_CloseParentFd();
|
||||
@@ -728,21 +776,19 @@ NKS_PreInitialise(uid_t uid, gid_t gid, int scfilter_level)
|
||||
void
|
||||
NKS_Initialise(void)
|
||||
{
|
||||
char *cert, *key;
|
||||
const char **certs, **keys;
|
||||
int i, n_certs_keys;
|
||||
double key_delay;
|
||||
int i;
|
||||
|
||||
server_sock_fd4 = INVALID_SOCK_FD;
|
||||
server_sock_fd6 = INVALID_SOCK_FD;
|
||||
|
||||
cert = CNF_GetNtsServerCertFile();
|
||||
key = CNF_GetNtsServerKeyFile();
|
||||
|
||||
if (!cert || !key)
|
||||
n_certs_keys = CNF_GetNtsServerCertAndKeyFiles(&certs, &keys);
|
||||
if (n_certs_keys <= 0)
|
||||
return;
|
||||
|
||||
if (helper_sock_fd == INVALID_SOCK_FD) {
|
||||
server_credentials = NKSN_CreateCertCredentials(cert, key, NULL);
|
||||
server_credentials = NKSN_CreateServerCertCredentials(certs, keys, n_certs_keys);
|
||||
if (!server_credentials)
|
||||
return;
|
||||
} else {
|
||||
@@ -756,7 +802,7 @@ NKS_Initialise(void)
|
||||
/* Generate random keys, even if they will be replaced by reloaded keys,
|
||||
or unused (in the helper) */
|
||||
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||
server_keys[i].siv = SIV_CreateInstance(SERVER_COOKIE_SIV);
|
||||
server_keys[i].siv = NULL;
|
||||
generate_key(i);
|
||||
}
|
||||
|
||||
@@ -776,6 +822,11 @@ NKS_Initialise(void)
|
||||
key_delay = key_rotation_interval - (SCH_GetLastEventMonoTime() - last_server_key_ts);
|
||||
SCH_AddTimeoutByDelay(MAX(key_delay, 0.0), key_timeout, NULL);
|
||||
}
|
||||
|
||||
/* Warn if keys are not saved, which can cause a flood of requests
|
||||
after server restart */
|
||||
if (!CNF_GetNtsDumpDir())
|
||||
LOG(LOGS_WARN, "No ntsdumpdir to save server keys");
|
||||
}
|
||||
|
||||
initialised = 1;
|
||||
@@ -849,7 +900,7 @@ NKS_ReloadKeys(void)
|
||||
int
|
||||
NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
{
|
||||
unsigned char plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
unsigned char *nonce, plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
int plaintext_length, tag_length;
|
||||
ServerCookieHeader *header;
|
||||
ServerKey *key;
|
||||
@@ -859,14 +910,12 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The algorithm is hardcoded for now */
|
||||
if (context->algorithm != AEAD_AES_SIV_CMAC_256) {
|
||||
DEBUG_LOG("Unexpected SIV algorithm");
|
||||
return 0;
|
||||
}
|
||||
/* The AEAD ID is not encoded in the cookie. It is implied from the key
|
||||
length (as long as only algorithms with different key lengths are
|
||||
supported). */
|
||||
|
||||
if (context->c2s.length < 0 || context->c2s.length > NKE_MAX_KEY_LENGTH ||
|
||||
context->s2c.length < 0 || context->s2c.length > NKE_MAX_KEY_LENGTH) {
|
||||
context->s2c.length != context->c2s.length) {
|
||||
DEBUG_LOG("Invalid key length");
|
||||
return 0;
|
||||
}
|
||||
@@ -876,7 +925,11 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
header = (ServerCookieHeader *)cookie->cookie;
|
||||
|
||||
header->key_id = htonl(key->id);
|
||||
UTI_GetRandomBytes(header->nonce, sizeof (header->nonce));
|
||||
|
||||
nonce = cookie->cookie + sizeof (*header);
|
||||
if (key->nonce_length > sizeof (cookie->cookie) - sizeof (*header))
|
||||
assert(0);
|
||||
UTI_GetRandomBytes(nonce, key->nonce_length);
|
||||
|
||||
plaintext_length = context->c2s.length + context->s2c.length;
|
||||
assert(plaintext_length <= sizeof (plaintext));
|
||||
@@ -884,11 +937,11 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
memcpy(plaintext + context->c2s.length, context->s2c.key, context->s2c.length);
|
||||
|
||||
tag_length = SIV_GetTagLength(key->siv);
|
||||
cookie->length = sizeof (*header) + plaintext_length + tag_length;
|
||||
cookie->length = sizeof (*header) + key->nonce_length + plaintext_length + tag_length;
|
||||
assert(cookie->length <= sizeof (cookie->cookie));
|
||||
ciphertext = cookie->cookie + sizeof (*header);
|
||||
ciphertext = cookie->cookie + sizeof (*header) + key->nonce_length;
|
||||
|
||||
if (!SIV_Encrypt(key->siv, header->nonce, sizeof (header->nonce),
|
||||
if (!SIV_Encrypt(key->siv, nonce, key->nonce_length,
|
||||
"", 0,
|
||||
plaintext, plaintext_length,
|
||||
ciphertext, plaintext_length + tag_length)) {
|
||||
@@ -904,7 +957,7 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
int
|
||||
NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context)
|
||||
{
|
||||
unsigned char plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
unsigned char *nonce, plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
int ciphertext_length, plaintext_length, tag_length;
|
||||
ServerCookieHeader *header;
|
||||
ServerKey *key;
|
||||
@@ -921,8 +974,6 @@ NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context)
|
||||
}
|
||||
|
||||
header = (ServerCookieHeader *)cookie->cookie;
|
||||
ciphertext = cookie->cookie + sizeof (*header);
|
||||
ciphertext_length = cookie->length - sizeof (*header);
|
||||
|
||||
key_id = ntohl(header->key_id);
|
||||
key = &server_keys[key_id % MAX_SERVER_KEYS];
|
||||
@@ -932,18 +983,23 @@ NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context)
|
||||
}
|
||||
|
||||
tag_length = SIV_GetTagLength(key->siv);
|
||||
if (tag_length >= ciphertext_length) {
|
||||
|
||||
if (cookie->length <= (int)sizeof (*header) + key->nonce_length + tag_length) {
|
||||
DEBUG_LOG("Invalid cookie length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
nonce = cookie->cookie + sizeof (*header);
|
||||
ciphertext = cookie->cookie + sizeof (*header) + key->nonce_length;
|
||||
ciphertext_length = cookie->length - sizeof (*header) - key->nonce_length;
|
||||
plaintext_length = ciphertext_length - tag_length;
|
||||
|
||||
if (plaintext_length > sizeof (plaintext) || plaintext_length % 2 != 0) {
|
||||
DEBUG_LOG("Invalid cookie length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!SIV_Decrypt(key->siv, header->nonce, sizeof (header->nonce),
|
||||
if (!SIV_Decrypt(key->siv, nonce, key->nonce_length,
|
||||
"", 0,
|
||||
ciphertext, ciphertext_length,
|
||||
plaintext, plaintext_length)) {
|
||||
@@ -951,7 +1007,19 @@ NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context)
|
||||
return 0;
|
||||
}
|
||||
|
||||
context->algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
/* Select a supported algorithm corresponding to the key length, avoiding
|
||||
potentially slow SIV_GetKeyLength() */
|
||||
switch (plaintext_length / 2) {
|
||||
case 16:
|
||||
context->algorithm = AEAD_AES_128_GCM_SIV;
|
||||
break;
|
||||
case 32:
|
||||
context->algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG("Unknown key length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
context->c2s.length = plaintext_length / 2;
|
||||
context->s2c.length = plaintext_length / 2;
|
||||
|
||||
112
nts_ke_session.c
112
nts_ke_session.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
* Copyright (C) Miroslav Lichvar 2020-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -225,9 +225,13 @@ create_tls_session(int server_mode, int sock_fd, const char *server_name,
|
||||
}
|
||||
|
||||
if (!server_mode) {
|
||||
r = gnutls_server_name_set(session, GNUTLS_NAME_DNS, server_name, strlen(server_name));
|
||||
if (r < 0)
|
||||
goto error;
|
||||
assert(server_name);
|
||||
|
||||
if (!UTI_IsStringIP(server_name)) {
|
||||
r = gnutls_server_name_set(session, GNUTLS_NAME_DNS, server_name, strlen(server_name));
|
||||
if (r < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
flags = 0;
|
||||
|
||||
@@ -590,13 +594,13 @@ handle_step(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
|
||||
static int gnutls_initialised = 0;
|
||||
|
||||
static void
|
||||
static int
|
||||
init_gnutls(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
if (gnutls_initialised)
|
||||
return;
|
||||
return 1;
|
||||
|
||||
r = gnutls_global_init();
|
||||
if (r < 0)
|
||||
@@ -607,8 +611,12 @@ init_gnutls(void)
|
||||
r = gnutls_priority_init2(&priority_cache,
|
||||
"-VERS-SSL3.0:-VERS-TLS1.0:-VERS-TLS1.1:-VERS-TLS1.2:-VERS-DTLS-ALL",
|
||||
NULL, GNUTLS_PRIORITY_INIT_DEF_APPEND);
|
||||
if (r < 0)
|
||||
LOG_FATAL("Could not initialise %s : %s", "priority cache", gnutls_strerror(r));
|
||||
if (r < 0) {
|
||||
LOG(LOGS_ERR, "Could not initialise %s : %s",
|
||||
"priority cache for TLS", gnutls_strerror(r));
|
||||
gnutls_global_deinit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Use our clock instead of the system clock in certificate verification */
|
||||
gnutls_global_set_time_function(get_time);
|
||||
@@ -617,6 +625,8 @@ init_gnutls(void)
|
||||
DEBUG_LOG("Initialised");
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_step, NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -637,41 +647,67 @@ deinit_gnutls(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void *
|
||||
NKSN_CreateCertCredentials(char *cert, char *key, char *trusted_certs)
|
||||
static NKSN_Credentials
|
||||
create_credentials(const char **certs, const char **keys, int n_certs_keys,
|
||||
const char **trusted_certs, uint32_t *trusted_certs_ids,
|
||||
int n_trusted_certs, uint32_t trusted_cert_set)
|
||||
{
|
||||
gnutls_certificate_credentials_t credentials = NULL;
|
||||
int r;
|
||||
int i, r;
|
||||
|
||||
init_gnutls();
|
||||
if (!init_gnutls())
|
||||
return NULL;
|
||||
|
||||
r = gnutls_certificate_allocate_credentials(&credentials);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
if (cert && key) {
|
||||
r = gnutls_certificate_set_x509_key_file(credentials, cert, key,
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
if (certs && keys) {
|
||||
if (trusted_certs || trusted_certs_ids)
|
||||
assert(0);
|
||||
|
||||
for (i = 0; i < n_certs_keys; i++) {
|
||||
if (!UTI_CheckFilePermissions(keys[i], 0771))
|
||||
;
|
||||
r = gnutls_certificate_set_x509_key_file(credentials, certs[i], keys[i],
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
if (!CNF_GetNoSystemCert()) {
|
||||
if (certs || keys || n_certs_keys > 0)
|
||||
assert(0);
|
||||
|
||||
if (trusted_cert_set == 0 && !CNF_GetNoSystemCert()) {
|
||||
r = gnutls_certificate_set_x509_system_trust(credentials);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (trusted_certs) {
|
||||
r = gnutls_certificate_set_x509_trust_file(credentials, trusted_certs,
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
if (trusted_certs && trusted_certs_ids) {
|
||||
for (i = 0; i < n_trusted_certs; i++) {
|
||||
struct stat buf;
|
||||
|
||||
if (trusted_certs_ids[i] != trusted_cert_set)
|
||||
continue;
|
||||
|
||||
if (stat(trusted_certs[i], &buf) == 0 && S_ISDIR(buf.st_mode))
|
||||
r = gnutls_certificate_set_x509_trust_dir(credentials, trusted_certs[i],
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
else
|
||||
r = gnutls_certificate_set_x509_trust_file(credentials, trusted_certs[i],
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
DEBUG_LOG("Added %d trusted certs from %s", r, trusted_certs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
credentials_counter++;
|
||||
|
||||
return credentials;
|
||||
return (NKSN_Credentials)credentials;
|
||||
|
||||
error:
|
||||
LOG(LOGS_ERR, "Could not set credentials : %s", gnutls_strerror(r));
|
||||
@@ -683,10 +719,27 @@ error:
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKSN_DestroyCertCredentials(void *credentials)
|
||||
NKSN_Credentials
|
||||
NKSN_CreateServerCertCredentials(const char **certs, const char **keys, int n_certs_keys)
|
||||
{
|
||||
gnutls_certificate_free_credentials(credentials);
|
||||
return create_credentials(certs, keys, n_certs_keys, NULL, NULL, 0, 0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NKSN_Credentials
|
||||
NKSN_CreateClientCertCredentials(const char **certs, uint32_t *ids,
|
||||
int n_certs_ids, uint32_t trusted_cert_set)
|
||||
{
|
||||
return create_credentials(NULL, NULL, 0, certs, ids, n_certs_ids, trusted_cert_set);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKSN_DestroyCertCredentials(NKSN_Credentials credentials)
|
||||
{
|
||||
gnutls_certificate_free_credentials((gnutls_certificate_credentials_t)credentials);
|
||||
credentials_counter--;
|
||||
deinit_gnutls();
|
||||
}
|
||||
@@ -734,12 +787,13 @@ NKSN_DestroyInstance(NKSN_Instance inst)
|
||||
|
||||
int
|
||||
NKSN_StartSession(NKSN_Instance inst, int sock_fd, const char *label,
|
||||
void *credentials, double timeout)
|
||||
NKSN_Credentials credentials, double timeout)
|
||||
{
|
||||
assert(inst->state == KE_STOPPED);
|
||||
|
||||
inst->tls_session = create_tls_session(inst->server, sock_fd, inst->server_name,
|
||||
credentials, priority_cache);
|
||||
(gnutls_certificate_credentials_t)credentials,
|
||||
priority_cache);
|
||||
if (!inst->tls_session)
|
||||
return 0;
|
||||
|
||||
|
||||
@@ -30,19 +30,25 @@
|
||||
#include "nts_ke.h"
|
||||
#include "siv.h"
|
||||
|
||||
typedef struct NKSN_Credentials_Record *NKSN_Credentials;
|
||||
|
||||
typedef struct NKSN_Instance_Record *NKSN_Instance;
|
||||
|
||||
/* Handler for received NTS-KE messages. A zero return code stops
|
||||
the session. */
|
||||
typedef int (*NKSN_MessageHandler)(void *arg);
|
||||
|
||||
/* Get client or server credentials using certificates of trusted CAs,
|
||||
or a server certificate and key. The credentials may be shared between
|
||||
/* Get server or client credentials using a server certificate and key,
|
||||
or certificates of trusted CAs. The credentials may be shared between
|
||||
different clients or servers. */
|
||||
extern void *NKSN_CreateCertCredentials(char *cert, char *key, char *trusted_certs);
|
||||
extern NKSN_Credentials NKSN_CreateServerCertCredentials(const char **certs, const char **keys,
|
||||
int n_certs_keys);
|
||||
extern NKSN_Credentials NKSN_CreateClientCertCredentials(const char **certs, uint32_t *ids,
|
||||
int n_certs_ids,
|
||||
uint32_t trusted_cert_set);
|
||||
|
||||
/* Destroy the credentials */
|
||||
extern void NKSN_DestroyCertCredentials(void *credentials);
|
||||
extern void NKSN_DestroyCertCredentials(NKSN_Credentials credentials);
|
||||
|
||||
/* Create an instance */
|
||||
extern NKSN_Instance NKSN_CreateInstance(int server_mode, const char *server_name,
|
||||
@@ -53,7 +59,7 @@ extern void NKSN_DestroyInstance(NKSN_Instance inst);
|
||||
|
||||
/* Start a new NTS-KE session */
|
||||
extern int NKSN_StartSession(NKSN_Instance inst, int sock_fd, const char *label,
|
||||
void *credentials, double timeout);
|
||||
NKSN_Credentials credentials, double timeout);
|
||||
|
||||
/* Begin an NTS-KE message. A request should be made right after starting
|
||||
the session and response should be made in the message handler. */
|
||||
|
||||
@@ -27,11 +27,6 @@
|
||||
#ifndef GOT_NTS_NTP_H
|
||||
#define GOT_NTS_NTP_H
|
||||
|
||||
#define NTP_EF_NTS_UNIQUE_IDENTIFIER 0x0104
|
||||
#define NTP_EF_NTS_COOKIE 0x0204
|
||||
#define NTP_EF_NTS_COOKIE_PLACEHOLDER 0x0304
|
||||
#define NTP_EF_NTS_AUTH_AND_EEF 0x0404
|
||||
|
||||
#define NTP_KOD_NTS_NAK 0x4e54534e
|
||||
|
||||
#define NTS_MIN_UNIQ_ID_LENGTH 32
|
||||
|
||||
@@ -61,23 +61,25 @@ get_padded_length(int length)
|
||||
|
||||
int
|
||||
NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const unsigned char *nonce, int max_nonce_length,
|
||||
const unsigned char *plaintext, int plaintext_length,
|
||||
int min_ef_length)
|
||||
{
|
||||
int auth_length, ciphertext_length, assoc_length;
|
||||
int auth_length, ciphertext_length, assoc_length, nonce_length, max_siv_nonce_length;
|
||||
int nonce_padding, ciphertext_padding, additional_padding;
|
||||
unsigned char *ciphertext, *body;
|
||||
struct AuthHeader *header;
|
||||
|
||||
assert(sizeof (*header) == 4);
|
||||
|
||||
if (nonce_length <= 0 || plaintext_length < 0) {
|
||||
if (max_nonce_length <= 0 || plaintext_length < 0) {
|
||||
DEBUG_LOG("Invalid nonce/plaintext length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
assoc_length = info->length;
|
||||
max_siv_nonce_length = SIV_GetMaxNonceLength(siv);
|
||||
nonce_length = MIN(max_nonce_length, max_siv_nonce_length);
|
||||
ciphertext_length = SIV_GetTagLength(siv) + plaintext_length;
|
||||
nonce_padding = get_padding_length(nonce_length);
|
||||
ciphertext_padding = get_padding_length(ciphertext_length);
|
||||
@@ -86,8 +88,8 @@ NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
auth_length = sizeof (*header) + nonce_length + nonce_padding +
|
||||
ciphertext_length + ciphertext_padding;
|
||||
additional_padding = MAX(min_ef_length - auth_length - 4, 0);
|
||||
additional_padding = MAX(NTS_MIN_UNPADDED_NONCE_LENGTH - nonce_length - nonce_padding,
|
||||
additional_padding);
|
||||
additional_padding = MAX(MIN(NTS_MIN_UNPADDED_NONCE_LENGTH, max_siv_nonce_length) -
|
||||
nonce_length - nonce_padding, additional_padding);
|
||||
auth_length += additional_padding;
|
||||
|
||||
if (!NEF_AddBlankField(packet, info, NTP_EF_NTS_AUTH_AND_EEF, auth_length,
|
||||
@@ -113,6 +115,7 @@ NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
plaintext, plaintext_length, ciphertext, ciphertext_length)) {
|
||||
DEBUG_LOG("SIV encrypt failed");
|
||||
info->length = assoc_length;
|
||||
info->ext_fields--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -127,7 +130,7 @@ int
|
||||
NNA_DecryptAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv, int ef_start,
|
||||
unsigned char *plaintext, int buffer_length, int *plaintext_length)
|
||||
{
|
||||
unsigned int siv_tag_length, nonce_length, ciphertext_length;
|
||||
int siv_tag_length, max_siv_nonce_length, nonce_length, ciphertext_length;
|
||||
unsigned char *nonce, *ciphertext;
|
||||
int ef_type, ef_body_length;
|
||||
void *ef_body;
|
||||
@@ -155,6 +158,7 @@ NNA_DecryptAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv, in
|
||||
nonce = (unsigned char *)(header + 1);
|
||||
ciphertext = nonce + get_padded_length(nonce_length);
|
||||
|
||||
max_siv_nonce_length = SIV_GetMaxNonceLength(siv);
|
||||
siv_tag_length = SIV_GetTagLength(siv);
|
||||
|
||||
if (nonce_length < 1 ||
|
||||
@@ -164,8 +168,8 @@ NNA_DecryptAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv, in
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ef_body_length < sizeof (*header) +
|
||||
NTS_MIN_UNPADDED_NONCE_LENGTH + get_padded_length(ciphertext_length)) {
|
||||
if (sizeof (*header) + MIN(NTS_MIN_UNPADDED_NONCE_LENGTH, max_siv_nonce_length) +
|
||||
get_padded_length(ciphertext_length) > ef_body_length) {
|
||||
DEBUG_LOG("Missing padding");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "siv.h"
|
||||
|
||||
extern int NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const unsigned char *nonce, int max_nonce_length,
|
||||
const unsigned char *plaintext, int plaintext_length,
|
||||
int min_ef_length);
|
||||
|
||||
|
||||
@@ -46,18 +46,27 @@
|
||||
/* Maximum length of all cookies to avoid IP fragmentation */
|
||||
#define MAX_TOTAL_COOKIE_LENGTH (8 * 108)
|
||||
|
||||
/* Retry interval for NTS-KE start (which doesn't generate network traffic) */
|
||||
#define RETRY_INTERVAL_KE_START 2.0
|
||||
|
||||
/* Magic string of files containing keys and cookies */
|
||||
#define DUMP_IDENTIFIER "NNC0\n"
|
||||
|
||||
struct NNC_Instance_Record {
|
||||
const IPSockAddr *ntp_address;
|
||||
/* Address of NTS-KE server */
|
||||
IPSockAddr nts_address;
|
||||
/* Hostname or IP address for certificate verification */
|
||||
char *name;
|
||||
/* ID of trusted certificates */
|
||||
uint32_t cert_set;
|
||||
/* Configured NTP port */
|
||||
uint16_t default_ntp_port;
|
||||
/* Address of NTP server (can be negotiated in NTS-KE) */
|
||||
IPSockAddr ntp_address;
|
||||
|
||||
NKC_Instance nke;
|
||||
SIV_Instance siv;
|
||||
|
||||
int load_attempt;
|
||||
int nke_attempts;
|
||||
double next_nke_attempt;
|
||||
double last_nke_success;
|
||||
@@ -91,7 +100,6 @@ reset_instance(NNC_Instance inst)
|
||||
SIV_DestroyInstance(inst->siv);
|
||||
inst->siv = NULL;
|
||||
|
||||
inst->load_attempt = 0;
|
||||
inst->nke_attempts = 0;
|
||||
inst->next_nke_attempt = 0.0;
|
||||
inst->last_nke_success = 0.0;
|
||||
@@ -111,20 +119,26 @@ reset_instance(NNC_Instance inst)
|
||||
/* ================================================== */
|
||||
|
||||
NNC_Instance
|
||||
NNC_CreateInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
|
||||
NNC_CreateInstance(IPSockAddr *nts_address, const char *name, uint32_t cert_set, uint16_t ntp_port)
|
||||
{
|
||||
NNC_Instance inst;
|
||||
|
||||
inst = MallocNew(struct NNC_Instance_Record);
|
||||
|
||||
inst->ntp_address = ntp_address;
|
||||
inst->nts_address = *nts_address;
|
||||
inst->name = name ? Strdup(name) : NULL;
|
||||
inst->name = Strdup(name);
|
||||
inst->cert_set = cert_set;
|
||||
inst->default_ntp_port = ntp_port;
|
||||
inst->ntp_address.ip_addr = nts_address->ip_addr;
|
||||
inst->ntp_address.port = ntp_port;
|
||||
inst->siv = NULL;
|
||||
inst->nke = NULL;
|
||||
|
||||
reset_instance(inst);
|
||||
|
||||
/* Try to reload saved keys and cookies */
|
||||
load_cookies(inst);
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
@@ -165,13 +179,13 @@ set_ntp_address(NNC_Instance inst, NTP_Remote_Address *negotiated_address)
|
||||
{
|
||||
NTP_Remote_Address old_address, new_address;
|
||||
|
||||
old_address = *inst->ntp_address;
|
||||
old_address = inst->ntp_address;
|
||||
new_address = *negotiated_address;
|
||||
|
||||
if (new_address.ip_addr.family == IPADDR_UNSPEC)
|
||||
new_address.ip_addr = old_address.ip_addr;
|
||||
new_address.ip_addr = inst->nts_address.ip_addr;
|
||||
if (new_address.port == 0)
|
||||
new_address.port = old_address.port;
|
||||
new_address.port = inst->default_ntp_port;
|
||||
|
||||
if (UTI_CompareIPs(&old_address.ip_addr, &new_address.ip_addr, NULL) == 0 &&
|
||||
old_address.port == new_address.port)
|
||||
@@ -184,16 +198,23 @@ set_ntp_address(NNC_Instance inst, NTP_Remote_Address *negotiated_address)
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->ntp_address = new_address;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_next_nke_attempt(NNC_Instance inst, double now)
|
||||
update_next_nke_attempt(NNC_Instance inst, int failed_start, double now)
|
||||
{
|
||||
int factor, interval;
|
||||
|
||||
if (failed_start) {
|
||||
inst->next_nke_attempt = now + RETRY_INTERVAL_KE_START;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!inst->nke)
|
||||
return;
|
||||
|
||||
@@ -208,8 +229,8 @@ static int
|
||||
get_cookies(NNC_Instance inst)
|
||||
{
|
||||
NTP_Remote_Address ntp_address;
|
||||
int got_data, failed_start = 0;
|
||||
double now;
|
||||
int got_data;
|
||||
|
||||
assert(inst->num_cookies == 0);
|
||||
|
||||
@@ -223,22 +244,15 @@ get_cookies(NNC_Instance inst)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!inst->name) {
|
||||
LOG(LOGS_ERR, "Missing name of %s for NTS-KE",
|
||||
UTI_IPToString(&inst->nts_address.ip_addr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->nke = NKC_CreateInstance(&inst->nts_address, inst->name);
|
||||
inst->nke = NKC_CreateInstance(&inst->nts_address, inst->name, inst->cert_set);
|
||||
|
||||
inst->nke_attempts++;
|
||||
update_next_nke_attempt(inst, now);
|
||||
|
||||
if (!NKC_Start(inst->nke))
|
||||
return 0;
|
||||
failed_start = 1;
|
||||
}
|
||||
|
||||
update_next_nke_attempt(inst, now);
|
||||
update_next_nke_attempt(inst, failed_start, now);
|
||||
|
||||
/* Wait until the session stops */
|
||||
if (NKC_IsActive(inst->nke))
|
||||
@@ -288,12 +302,6 @@ NNC_PrepareForAuth(NNC_Instance inst)
|
||||
UTI_GetRandomBytes(inst->uniq_id, sizeof (inst->uniq_id));
|
||||
UTI_GetRandomBytes(inst->nonce, sizeof (inst->nonce));
|
||||
|
||||
/* Try to reload saved keys and cookies (once for the NTS-KE address) */
|
||||
if (!inst->load_attempt) {
|
||||
load_cookies(inst);
|
||||
inst->load_attempt = 1;
|
||||
}
|
||||
|
||||
/* Get new cookies if there are not any, or they are no longer usable */
|
||||
if (!check_cookies(inst)) {
|
||||
if (!get_cookies(inst))
|
||||
@@ -456,7 +464,7 @@ NNC_CheckResponseAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||
for (parsed = NTP_HEADER_LENGTH; parsed < info->length; parsed += ef_length) {
|
||||
if (!NEF_ParseField(packet, info->length, parsed,
|
||||
&ef_length, &ef_type, &ef_body, &ef_body_length))
|
||||
/* This is not expected as the packet already passed NAU_ParsePacket() */
|
||||
/* This is not expected as the packet already passed parsing */
|
||||
return 0;
|
||||
|
||||
switch (ef_type) {
|
||||
@@ -524,10 +532,13 @@ NNC_ChangeAddress(NNC_Instance inst, IPAddr *address)
|
||||
save_cookies(inst);
|
||||
|
||||
inst->nts_address.ip_addr = *address;
|
||||
inst->ntp_address.ip_addr = *address;
|
||||
|
||||
reset_instance(inst);
|
||||
|
||||
DEBUG_LOG("NTS reset");
|
||||
|
||||
load_cookies(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -541,7 +552,7 @@ save_cookies(NNC_Instance inst)
|
||||
FILE *f;
|
||||
int i;
|
||||
|
||||
if (inst->num_cookies < 1 || !inst->name || !UTI_IsIPReal(&inst->nts_address.ip_addr))
|
||||
if (inst->num_cookies < 1 || !UTI_IsIPReal(&inst->nts_address.ip_addr))
|
||||
return;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
@@ -560,7 +571,7 @@ save_cookies(NNC_Instance inst)
|
||||
|
||||
if (fprintf(f, "%s%s\n%.1f\n%s %d\n%u %d ",
|
||||
DUMP_IDENTIFIER, inst->name, context_time,
|
||||
UTI_IPToString(&inst->ntp_address->ip_addr), inst->ntp_address->port,
|
||||
UTI_IPToString(&inst->ntp_address.ip_addr), inst->ntp_address.port,
|
||||
inst->context_id, (int)inst->context.algorithm) < 0 ||
|
||||
!UTI_BytesToHex(inst->context.s2c.key, inst->context.s2c.length, buf, sizeof (buf)) ||
|
||||
fprintf(f, "%s ", buf) < 0 ||
|
||||
@@ -623,7 +634,7 @@ load_cookies(NNC_Instance inst)
|
||||
|
||||
if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
|
||||
!inst->name || strcmp(words[0], inst->name) != 0 ||
|
||||
strcmp(words[0], inst->name) != 0 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
|
||||
sscanf(words[0], "%lf", &context_time) != 1 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 2 ||
|
||||
@@ -665,6 +676,8 @@ load_cookies(NNC_Instance inst)
|
||||
inst->last_nke_success = context_time + SCH_GetLastEventMonoTime();
|
||||
inst->context_id = context_id;
|
||||
|
||||
fclose(f);
|
||||
|
||||
DEBUG_LOG("Loaded %d cookies for %s", i, filename);
|
||||
return;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
typedef struct NNC_Instance_Record *NNC_Instance;
|
||||
|
||||
extern NNC_Instance NNC_CreateInstance(IPSockAddr *nts_address, const char *name,
|
||||
const IPSockAddr *ntp_address);
|
||||
uint32_t cert_set, uint16_t ntp_port);
|
||||
extern void NNC_DestroyInstance(NNC_Instance inst);
|
||||
extern int NNC_PrepareForAuth(NNC_Instance inst);
|
||||
extern int NNC_GenerateRequestAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||
|
||||
@@ -41,13 +41,15 @@
|
||||
#include "siv.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SERVER_SIV AEAD_AES_SIV_CMAC_256
|
||||
#define MAX_SERVER_SIVS 2
|
||||
|
||||
struct NtsServer {
|
||||
SIV_Instance siv;
|
||||
SIV_Instance sivs[MAX_SERVER_SIVS];
|
||||
SIV_Algorithm siv_algorithms[MAX_SERVER_SIVS];
|
||||
unsigned char nonce[NTS_MIN_UNPADDED_NONCE_LENGTH];
|
||||
NKE_Cookie cookies[NTS_MAX_COOKIES];
|
||||
int num_cookies;
|
||||
int siv_index;
|
||||
NTP_int64 req_tx;
|
||||
};
|
||||
|
||||
@@ -59,16 +61,27 @@ struct NtsServer *server;
|
||||
void
|
||||
NNS_Initialise(void)
|
||||
{
|
||||
const char **certs, **keys;
|
||||
int i;
|
||||
|
||||
/* Create an NTS-NTP server instance only if NTS-KE server is enabled */
|
||||
if (!CNF_GetNtsServerCertFile() || !CNF_GetNtsServerKeyFile()) {
|
||||
if (CNF_GetNtsServerCertAndKeyFiles(&certs, &keys) <= 0) {
|
||||
server = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
server = Malloc(sizeof (struct NtsServer));
|
||||
server->siv = SIV_CreateInstance(SERVER_SIV);
|
||||
if (!server->siv)
|
||||
LOG_FATAL("Could not initialise SIV cipher");
|
||||
|
||||
server->siv_algorithms[0] = AEAD_AES_SIV_CMAC_256;
|
||||
server->siv_algorithms[1] = AEAD_AES_128_GCM_SIV;
|
||||
assert(MAX_SERVER_SIVS == 2);
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
server->sivs[i] = SIV_CreateInstance(server->siv_algorithms[i]);
|
||||
|
||||
/* AES-SIV-CMAC-256 is required on servers */
|
||||
if (!server->sivs[0])
|
||||
LOG_FATAL("Missing AES-SIV-CMAC-256");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -76,10 +89,15 @@ NNS_Initialise(void)
|
||||
void
|
||||
NNS_Finalise(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!server)
|
||||
return;
|
||||
|
||||
SIV_DestroyInstance(server->siv);
|
||||
for (i = 0; i < MAX_SERVER_SIVS; i++) {
|
||||
if (server->sivs[i])
|
||||
SIV_DestroyInstance(server->sivs[i]);
|
||||
}
|
||||
Free(server);
|
||||
server = NULL;
|
||||
}
|
||||
@@ -94,6 +112,7 @@ NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod)
|
||||
unsigned char plaintext[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
NKE_Context context;
|
||||
NKE_Cookie cookie;
|
||||
SIV_Instance siv;
|
||||
void *ef_body;
|
||||
|
||||
*kod = 0;
|
||||
@@ -102,6 +121,7 @@ NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod)
|
||||
return 0;
|
||||
|
||||
server->num_cookies = 0;
|
||||
server->siv_index = -1;
|
||||
server->req_tx = packet->transmit_ts;
|
||||
|
||||
if (info->ext_fields == 0 || info->mode != MODE_CLIENT)
|
||||
@@ -161,17 +181,22 @@ NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (context.algorithm != SERVER_SIV) {
|
||||
/* Find the SIV instance needed for authentication */
|
||||
for (i = 0; i < MAX_SERVER_SIVS && context.algorithm != server->siv_algorithms[i]; i++)
|
||||
;
|
||||
if (i == MAX_SERVER_SIVS || !server->sivs[i]) {
|
||||
DEBUG_LOG("Unexpected SIV");
|
||||
return 0;
|
||||
}
|
||||
server->siv_index = i;
|
||||
siv = server->sivs[i];
|
||||
|
||||
if (!SIV_SetKey(server->siv, context.c2s.key, context.c2s.length)) {
|
||||
if (!SIV_SetKey(siv, context.c2s.key, context.c2s.length)) {
|
||||
DEBUG_LOG("Could not set C2S key");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!NNA_DecryptAuthEF(packet, info, server->siv, auth_start,
|
||||
if (!NNA_DecryptAuthEF(packet, info, siv, auth_start,
|
||||
plaintext, sizeof (plaintext), &plaintext_length)) {
|
||||
*kod = NTP_KOD_NTS_NAK;
|
||||
return 0;
|
||||
@@ -197,7 +222,7 @@ NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod)
|
||||
}
|
||||
}
|
||||
|
||||
if (!SIV_SetKey(server->siv, context.s2c.key, context.s2c.length)) {
|
||||
if (!SIV_SetKey(siv, context.s2c.key, context.s2c.length)) {
|
||||
DEBUG_LOG("Could not set S2C key");
|
||||
return 0;
|
||||
}
|
||||
@@ -240,7 +265,7 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
||||
for (parsed = NTP_HEADER_LENGTH; parsed < req_info->length; parsed += ef_length) {
|
||||
if (!NEF_ParseField(request, req_info->length, parsed,
|
||||
&ef_length, &ef_type, &ef_body, &ef_body_length))
|
||||
/* This is not expected as the packet already passed NAU_ParsePacket() */
|
||||
/* This is not expected as the packet already passed parsing */
|
||||
return 0;
|
||||
|
||||
switch (ef_type) {
|
||||
@@ -269,9 +294,12 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
||||
|
||||
server->num_cookies = 0;
|
||||
|
||||
if (server->siv_index < 0)
|
||||
return 0;
|
||||
|
||||
/* Generate an authenticator field which will make the length
|
||||
of the response equal to the length of the request */
|
||||
if (!NNA_GenerateAuthEF(response, res_info, server->siv,
|
||||
if (!NNA_GenerateAuthEF(response, res_info, server->sivs[server->siv_index],
|
||||
server->nonce, sizeof (server->nonce),
|
||||
plaintext, plaintext_length,
|
||||
req_info->length - res_info->length))
|
||||
|
||||
@@ -87,7 +87,7 @@ static const struct request_length request_lengths[] = {
|
||||
REQ_LENGTH_ENTRY(del_source, null), /* DEL_SOURCE */
|
||||
REQ_LENGTH_ENTRY(null, null), /* WRITERTC */
|
||||
REQ_LENGTH_ENTRY(dfreq, null), /* DFREQ */
|
||||
REQ_LENGTH_ENTRY(doffset, null), /* DOFFSET */
|
||||
{ 0, 0 }, /* DOFFSET - not supported */
|
||||
REQ_LENGTH_ENTRY(null, tracking), /* TRACKING */
|
||||
REQ_LENGTH_ENTRY(sourcestats, sourcestats), /* SOURCESTATS */
|
||||
REQ_LENGTH_ENTRY(null, rtc), /* RTCREPORT */
|
||||
@@ -128,6 +128,8 @@ static const struct request_length request_lengths[] = {
|
||||
client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX3 */
|
||||
REQ_LENGTH_ENTRY(select_data, select_data), /* SELECT_DATA */
|
||||
REQ_LENGTH_ENTRY(null, null), /* RELOAD_SOURCES */
|
||||
REQ_LENGTH_ENTRY(doffset, null), /* DOFFSET2 */
|
||||
REQ_LENGTH_ENTRY(modify_select_opts, null), /* MODIFY_SELECTOPTS */
|
||||
};
|
||||
|
||||
static const uint16_t reply_lengths[] = {
|
||||
@@ -153,8 +155,10 @@ static const uint16_t reply_lengths[] = {
|
||||
RPY_LENGTH_ENTRY(ntp_source_name), /* NTP_SOURCE_NAME */
|
||||
RPY_LENGTH_ENTRY(auth_data), /* AUTH_DATA */
|
||||
RPY_LENGTH_ENTRY(client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX3 */
|
||||
RPY_LENGTH_ENTRY(server_stats), /* SERVER_STATS2 */
|
||||
0, /* SERVER_STATS2 - not supported */
|
||||
RPY_LENGTH_ENTRY(select_data), /* SELECT_DATA */
|
||||
0, /* SERVER_STATS3 - not supported */
|
||||
RPY_LENGTH_ENTRY(server_stats), /* SERVER_STATS4 */
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -255,7 +255,7 @@ do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
||||
|
||||
SCK_SockaddrToIPSockAddr(sa, sa_len, &ip_saddr);
|
||||
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||
ip_saddr.port != CNF_GetAcquisitionPort()) {
|
||||
ip_saddr.port != CNF_GetAcquisitionPort() && ip_saddr.port != CNF_GetPtpPort()) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
res_fatal(res, "Invalid port %d", ip_saddr.port);
|
||||
return;
|
||||
@@ -547,7 +547,7 @@ PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
||||
|
||||
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
|
||||
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||
ip_saddr.port != CNF_GetAcquisitionPort())
|
||||
ip_saddr.port != CNF_GetAcquisitionPort() && ip_saddr.port != CNF_GetPtpPort())
|
||||
assert(0);
|
||||
|
||||
if (!have_helper())
|
||||
@@ -662,6 +662,8 @@ PRV_StartHelper(void)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
UTI_ResetGetRandomFunctions();
|
||||
|
||||
/* ignore signals, the process will exit on OP_QUIT request */
|
||||
UTI_SetQuitSignalsHandler(SIG_IGN, 1);
|
||||
|
||||
|
||||
69
ptp.h
Normal file
69
ptp.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
This is the header file for the Precision Time Protocol (PTP).
|
||||
|
||||
*/
|
||||
#ifndef GOT_PTP_H
|
||||
#define GOT_PTP_H
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
#define PTP_VERSION 2
|
||||
#define PTP_TYPE_DELAY_REQ 1
|
||||
#define PTP_DOMAIN_NTP 123
|
||||
#define PTP_FLAG_UNICAST (1 << (2 + 8))
|
||||
#define PTP_TLV_NTP 0x2023
|
||||
|
||||
typedef struct {
|
||||
uint8_t type;
|
||||
uint8_t version;
|
||||
uint16_t length;
|
||||
uint8_t domain;
|
||||
uint8_t min_sdoid;
|
||||
uint16_t flags;
|
||||
uint8_t correction[8];
|
||||
uint8_t msg_specific[4];
|
||||
uint8_t port_id[10];
|
||||
uint16_t sequence_id;
|
||||
uint8_t control;
|
||||
int8_t interval;
|
||||
} PTP_Header;
|
||||
|
||||
typedef struct {
|
||||
uint16_t type;
|
||||
uint16_t length;
|
||||
} PTP_TlvHeader;
|
||||
|
||||
typedef struct {
|
||||
PTP_Header header;
|
||||
uint8_t origin_ts[10];
|
||||
PTP_TlvHeader tlv_header;
|
||||
NTP_Packet ntp_msg;
|
||||
} PTP_NtpMessage;
|
||||
|
||||
#define PTP_NTP_PREFIX_LENGTH (int)offsetof(PTP_NtpMessage, ntp_msg)
|
||||
|
||||
#endif
|
||||
209
quantiles.c
Normal file
209
quantiles.c
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Estimation of quantiles using the Frugal-2U streaming algorithm
|
||||
(https://arxiv.org/pdf/1407.1121v1.pdf)
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "quantiles.h"
|
||||
#include "regress.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Maximum number of repeated estimates for stabilisation */
|
||||
#define MAX_REPEAT 64
|
||||
|
||||
struct Quantile {
|
||||
double est;
|
||||
double step;
|
||||
int sign;
|
||||
};
|
||||
|
||||
struct QNT_Instance_Record {
|
||||
struct Quantile *quants;
|
||||
int n_quants;
|
||||
int repeat;
|
||||
int q;
|
||||
int min_k;
|
||||
double min_step;
|
||||
int n_set;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
QNT_Instance
|
||||
QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step)
|
||||
{
|
||||
QNT_Instance inst;
|
||||
long seed;
|
||||
|
||||
if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q ||
|
||||
repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0)
|
||||
assert(0);
|
||||
|
||||
inst = MallocNew(struct QNT_Instance_Record);
|
||||
inst->n_quants = (max_k - min_k + 1) * repeat;
|
||||
inst->quants = MallocArray(struct Quantile, inst->n_quants);
|
||||
inst->repeat = repeat;
|
||||
inst->q = q;
|
||||
inst->min_k = min_k;
|
||||
inst->min_step = min_step;
|
||||
|
||||
QNT_Reset(inst);
|
||||
|
||||
/* Seed the random number generator, which will not be isolated from
|
||||
other instances and other random() users */
|
||||
UTI_GetRandomBytes(&seed, sizeof (seed));
|
||||
srandom(seed);
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
QNT_DestroyInstance(QNT_Instance inst)
|
||||
{
|
||||
Free(inst->quants);
|
||||
Free(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
QNT_Reset(QNT_Instance inst)
|
||||
{
|
||||
int i;
|
||||
|
||||
inst->n_set = 0;
|
||||
|
||||
for (i = 0; i < inst->n_quants; i++) {
|
||||
inst->quants[i].est = 0.0;
|
||||
inst->quants[i].step = inst->min_step;
|
||||
inst->quants[i].sign = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
insert_initial_value(QNT_Instance inst, double value)
|
||||
{
|
||||
int i, j, r = inst->repeat;
|
||||
|
||||
if (inst->n_set * r >= inst->n_quants)
|
||||
assert(0);
|
||||
|
||||
/* Keep the initial estimates repeated and ordered */
|
||||
for (i = inst->n_set; i > 0 && inst->quants[(i - 1) * r].est > value; i--) {
|
||||
for (j = 0; j < r; j++)
|
||||
inst->quants[i * r + j].est = inst->quants[(i - 1) * r].est;
|
||||
}
|
||||
|
||||
for (j = 0; j < r; j++)
|
||||
inst->quants[i * r + j].est = value;
|
||||
inst->n_set++;
|
||||
|
||||
/* Duplicate the largest value in unset quantiles */
|
||||
for (i = inst->n_set * r; i < inst->n_quants; i++)
|
||||
inst->quants[i].est = inst->quants[i - 1].est;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_estimate(struct Quantile *quantile, double value, double p, double rand,
|
||||
double min_step)
|
||||
{
|
||||
if (value > quantile->est && rand > (1.0 - p)) {
|
||||
quantile->step += quantile->sign > 0 ? min_step : -min_step;
|
||||
quantile->est += quantile->step > 0.0 ? fabs(quantile->step) : min_step;
|
||||
if (quantile->est > value) {
|
||||
quantile->step += value - quantile->est;
|
||||
quantile->est = value;
|
||||
}
|
||||
if (quantile->sign < 0 && quantile->step > min_step)
|
||||
quantile->step = min_step;
|
||||
quantile->sign = 1;
|
||||
} else if (value < quantile->est && rand > p) {
|
||||
quantile->step += quantile->sign < 0 ? min_step : -min_step;
|
||||
quantile->est -= quantile->step > 0.0 ? fabs(quantile->step) : min_step;
|
||||
if (quantile->est < value) {
|
||||
quantile->step += quantile->est - value;
|
||||
quantile->est = value;
|
||||
}
|
||||
if (quantile->sign > 0 && quantile->step > min_step)
|
||||
quantile->step = min_step;
|
||||
quantile->sign = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
QNT_Accumulate(QNT_Instance inst, double value)
|
||||
{
|
||||
double p, rand;
|
||||
int i;
|
||||
|
||||
/* Initialise the estimates with first received values */
|
||||
if (inst->n_set * inst->repeat < inst->n_quants) {
|
||||
insert_initial_value(inst, value);
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < inst->n_quants; i++) {
|
||||
p = (double)(i / inst->repeat + inst->min_k) / inst->q;
|
||||
rand = (double)random() / ((1U << 31) - 1);
|
||||
|
||||
update_estimate(&inst->quants[i], value, p, rand, inst->min_step);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
QNT_GetMinK(QNT_Instance inst)
|
||||
{
|
||||
return inst->min_k;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
double
|
||||
QNT_GetQuantile(QNT_Instance inst, int k)
|
||||
{
|
||||
double estimates[MAX_REPEAT];
|
||||
int i;
|
||||
|
||||
if (k < inst->min_k || k - inst->min_k >= inst->n_quants)
|
||||
assert(0);
|
||||
|
||||
for (i = 0; i < inst->repeat; i++)
|
||||
estimates[i] = inst->quants[(k - inst->min_k) * inst->repeat + i].est;
|
||||
|
||||
return RGR_FindMedian(estimates, inst->repeat);
|
||||
}
|
||||
41
quantiles.h
Normal file
41
quantiles.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Header file for estimation of quantiles.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef GOT_QUANTILES_H
|
||||
#define GOT_QUANTILES_H
|
||||
|
||||
typedef struct QNT_Instance_Record *QNT_Instance;
|
||||
|
||||
extern QNT_Instance QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step);
|
||||
extern void QNT_DestroyInstance(QNT_Instance inst);
|
||||
|
||||
extern void QNT_Reset(QNT_Instance inst);
|
||||
extern void QNT_Accumulate(QNT_Instance inst, double value);
|
||||
extern int QNT_GetMinK(QNT_Instance inst);
|
||||
extern double QNT_GetQuantile(QNT_Instance inst, int k);
|
||||
|
||||
#endif
|
||||
189
refclock.c
189
refclock.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014, 2016-2019
|
||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014, 2016-2019, 2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -40,6 +40,9 @@
|
||||
#include "samplefilt.h"
|
||||
#include "sched.h"
|
||||
|
||||
/* Maximum offset of locked reference as a fraction of the PPS interval */
|
||||
#define PPS_LOCK_LIMIT 0.4
|
||||
|
||||
/* list of refclock drivers */
|
||||
extern RefclockDriver RCL_SHM_driver;
|
||||
extern RefclockDriver RCL_SOCK_driver;
|
||||
@@ -52,21 +55,6 @@ struct FilterSample {
|
||||
struct timespec sample_time;
|
||||
};
|
||||
|
||||
struct MedianFilter {
|
||||
int length;
|
||||
int index;
|
||||
int used;
|
||||
int last;
|
||||
int avg_var_n;
|
||||
double avg_var;
|
||||
double max_var;
|
||||
struct FilterSample *samples;
|
||||
int *selected;
|
||||
double *x_data;
|
||||
double *y_data;
|
||||
double *w_data;
|
||||
};
|
||||
|
||||
struct RCL_Instance_Record {
|
||||
RefclockDriver *driver;
|
||||
void *data;
|
||||
@@ -76,6 +64,7 @@ struct RCL_Instance_Record {
|
||||
int driver_polled;
|
||||
int poll;
|
||||
int leap_status;
|
||||
int local;
|
||||
int pps_forced;
|
||||
int pps_rate;
|
||||
int pps_active;
|
||||
@@ -187,6 +176,7 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
inst->poll = params->poll;
|
||||
inst->driver_polled = 0;
|
||||
inst->leap_status = LEAP_Normal;
|
||||
inst->local = params->local;
|
||||
inst->pps_forced = params->pps_forced;
|
||||
inst->pps_rate = params->pps_rate;
|
||||
inst->pps_active = 0;
|
||||
@@ -228,6 +218,13 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
inst->ref_id = (uint32_t)ref[0] << 24 | ref[1] << 16 | ref[2] << 8 | ref[3];
|
||||
}
|
||||
|
||||
if (inst->local) {
|
||||
inst->pps_forced = 1;
|
||||
inst->lock_ref = inst->ref_id;
|
||||
inst->leap_status = LEAP_Unsynchronised;
|
||||
inst->max_lock_age = MAX(inst->max_lock_age, 3);
|
||||
}
|
||||
|
||||
if (inst->driver->poll) {
|
||||
int max_samples;
|
||||
|
||||
@@ -267,7 +264,7 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
void
|
||||
RCL_StartRefclocks(void)
|
||||
{
|
||||
unsigned int i, j, n;
|
||||
unsigned int i, j, n, lock_index;
|
||||
|
||||
n = ARR_GetSize(refclocks);
|
||||
|
||||
@@ -277,13 +274,31 @@ RCL_StartRefclocks(void)
|
||||
SRC_SetActive(inst->source);
|
||||
inst->timeout_id = SCH_AddTimeoutByDelay(0.0, poll_timeout, (void *)inst);
|
||||
|
||||
if (inst->lock_ref) {
|
||||
/* Replace lock refid with index to refclocks */
|
||||
for (j = 0; j < n && get_refclock(j)->ref_id != inst->lock_ref; j++)
|
||||
;
|
||||
inst->lock_ref = j < n ? j : -1;
|
||||
} else
|
||||
inst->lock_ref = -1;
|
||||
/* Replace lock refid with the refclock's index, or -1 if not valid */
|
||||
|
||||
lock_index = -1;
|
||||
|
||||
if (inst->lock_ref != 0) {
|
||||
for (j = 0; j < n; j++) {
|
||||
RCL_Instance inst2 = get_refclock(j);
|
||||
|
||||
if (inst->lock_ref != inst2->ref_id)
|
||||
continue;
|
||||
|
||||
if (inst->driver->poll && inst2->driver->poll &&
|
||||
(double)inst->max_lock_age / inst->pps_rate < UTI_Log2ToDouble(inst2->driver_poll))
|
||||
LOG(LOGS_WARN, "%s maxlockage too small for %s",
|
||||
UTI_RefidToString(inst->ref_id), UTI_RefidToString(inst2->ref_id));
|
||||
|
||||
lock_index = j;
|
||||
break;
|
||||
}
|
||||
|
||||
if (lock_index == -1 || (lock_index == i && !inst->local))
|
||||
LOG(LOGS_WARN, "Invalid lock refid %s", UTI_RefidToString(inst->lock_ref));
|
||||
}
|
||||
|
||||
inst->lock_ref = lock_index;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -415,30 +430,28 @@ accumulate_sample(RCL_Instance instance, struct timespec *sample_time, double of
|
||||
sample.peer_dispersion = dispersion;
|
||||
sample.root_dispersion = dispersion;
|
||||
|
||||
/* Handle special case when PPS is used with the local reference */
|
||||
if (instance->pps_active && instance->lock_ref == -1)
|
||||
sample.stratum = pps_stratum(instance, &sample.time);
|
||||
else
|
||||
sample.stratum = instance->stratum;
|
||||
|
||||
return SPF_AccumulateSample(instance->filter, &sample);
|
||||
}
|
||||
|
||||
int
|
||||
RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap)
|
||||
RCL_AddSample(RCL_Instance instance, struct timespec *sample_time,
|
||||
struct timespec *ref_time, int leap)
|
||||
{
|
||||
double correction, dispersion;
|
||||
double correction, dispersion, raw_offset, offset;
|
||||
struct timespec cooked_time;
|
||||
|
||||
if (instance->pps_forced)
|
||||
return RCL_AddPulse(instance, sample_time, -offset);
|
||||
return RCL_AddPulse(instance, sample_time,
|
||||
1.0e-9 * (sample_time->tv_nsec - ref_time->tv_nsec));
|
||||
|
||||
raw_offset = UTI_DiffTimespecsToDouble(ref_time, sample_time);
|
||||
|
||||
LCL_GetOffsetCorrection(sample_time, &correction, &dispersion);
|
||||
UTI_AddDoubleToTimespec(sample_time, correction, &cooked_time);
|
||||
dispersion += instance->precision;
|
||||
|
||||
/* Make sure the timestamp and offset provided by the driver are sane */
|
||||
if (!UTI_IsTimeOffsetSane(sample_time, offset) ||
|
||||
if (!UTI_IsTimeOffsetSane(sample_time, raw_offset) ||
|
||||
!valid_sample_time(instance, &cooked_time))
|
||||
return 0;
|
||||
|
||||
@@ -453,18 +466,24 @@ RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Calculate offset = raw_offset - correction + instance->offset
|
||||
in parts to avoid loss of precision if there are large differences */
|
||||
offset = ref_time->tv_sec - sample_time->tv_sec -
|
||||
(time_t)correction + (time_t)instance->offset;
|
||||
offset += 1.0e-9 * (ref_time->tv_nsec - sample_time->tv_nsec) -
|
||||
(correction - (time_t)correction) + (instance->offset - (time_t)instance->offset);
|
||||
|
||||
if (instance->tai && !convert_tai_offset(sample_time, &offset)) {
|
||||
DEBUG_LOG("refclock sample ignored unknown TAI offset");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!accumulate_sample(instance, &cooked_time,
|
||||
offset - correction + instance->offset, dispersion))
|
||||
if (!accumulate_sample(instance, &cooked_time, offset, dispersion))
|
||||
return 0;
|
||||
|
||||
instance->pps_active = 0;
|
||||
|
||||
log_sample(instance, &cooked_time, 0, 0, offset, offset - correction + instance->offset, dispersion);
|
||||
log_sample(instance, &cooked_time, 0, 0, raw_offset, offset, dispersion);
|
||||
|
||||
/* for logging purposes */
|
||||
if (!instance->driver->poll)
|
||||
@@ -543,29 +562,40 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
lock_refclock = get_refclock(instance->lock_ref);
|
||||
|
||||
if (!SPF_GetLastSample(lock_refclock->filter, &ref_sample)) {
|
||||
DEBUG_LOG("refclock pulse ignored no ref sample");
|
||||
return 0;
|
||||
if (instance->local) {
|
||||
/* Make the first sample in order to lock to itself */
|
||||
ref_sample.time = *cooked_time;
|
||||
ref_sample.offset = offset;
|
||||
ref_sample.peer_delay = ref_sample.peer_dispersion = 0;
|
||||
ref_sample.root_delay = ref_sample.root_dispersion = 0;
|
||||
} else {
|
||||
DEBUG_LOG("refclock pulse ignored no ref sample");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ref_sample.root_dispersion += SPF_GetAvgSampleDispersion(lock_refclock->filter);
|
||||
|
||||
sample_diff = UTI_DiffTimespecsToDouble(cooked_time, &ref_sample.time);
|
||||
if (fabs(sample_diff) >= (double)instance->max_lock_age / rate) {
|
||||
DEBUG_LOG("refclock pulse ignored samplediff=%.9f",
|
||||
sample_diff);
|
||||
DEBUG_LOG("refclock pulse ignored samplediff=%.9f", sample_diff);
|
||||
|
||||
/* Restart the local mode */
|
||||
if (instance->local) {
|
||||
LOG(LOGS_WARN, "Local refclock lost lock");
|
||||
SPF_DropSamples(instance->filter);
|
||||
SRC_ResetInstance(instance->source);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Align the offset to the reference sample */
|
||||
if ((ref_sample.offset - offset) >= 0.0)
|
||||
shift = (long)((ref_sample.offset - offset) * rate + 0.5) / (double)rate;
|
||||
else
|
||||
shift = (long)((ref_sample.offset - offset) * rate - 0.5) / (double)rate;
|
||||
shift = round((ref_sample.offset - offset) * rate) / rate;
|
||||
|
||||
offset += shift;
|
||||
|
||||
if (fabs(ref_sample.offset - offset) +
|
||||
ref_sample.root_dispersion + dispersion >= 0.2 / rate) {
|
||||
ref_sample.root_dispersion + dispersion > PPS_LOCK_LIMIT / rate) {
|
||||
DEBUG_LOG("refclock pulse ignored offdiff=%.9f refdisp=%.9f disp=%.9f",
|
||||
ref_sample.offset - offset, ref_sample.root_dispersion, dispersion);
|
||||
return 0;
|
||||
@@ -681,11 +711,57 @@ pps_stratum(RCL_Instance instance, struct timespec *ts)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
get_local_stats(RCL_Instance inst, struct timespec *ref, double *freq, double *offset)
|
||||
{
|
||||
double offset_sd, freq_sd, skew, root_delay, root_disp;
|
||||
SST_Stats stats = SRC_GetSourcestats(inst->source);
|
||||
|
||||
if (SST_Samples(stats) < SST_GetMinSamples(stats)) {
|
||||
UTI_ZeroTimespec(ref);
|
||||
return;
|
||||
}
|
||||
|
||||
SST_GetTrackingData(stats, ref, offset, &offset_sd, freq, &freq_sd,
|
||||
&skew, &root_delay, &root_disp);
|
||||
}
|
||||
|
||||
static void
|
||||
follow_local(RCL_Instance inst, struct timespec *prev_ref_time, double prev_freq,
|
||||
double prev_offset)
|
||||
{
|
||||
SST_Stats stats = SRC_GetSourcestats(inst->source);
|
||||
double freq, dfreq, offset, doffset, elapsed;
|
||||
struct timespec now, ref_time;
|
||||
|
||||
get_local_stats(inst, &ref_time, &freq, &offset);
|
||||
|
||||
if (UTI_IsZeroTimespec(prev_ref_time) || UTI_IsZeroTimespec(&ref_time))
|
||||
return;
|
||||
|
||||
dfreq = (freq - prev_freq) / (1.0 - prev_freq);
|
||||
elapsed = UTI_DiffTimespecsToDouble(&ref_time, prev_ref_time);
|
||||
doffset = offset - elapsed * prev_freq - prev_offset;
|
||||
|
||||
if (!REF_AdjustReference(doffset, dfreq))
|
||||
return;
|
||||
|
||||
LCL_ReadCookedTime(&now, NULL);
|
||||
SST_SlewSamples(stats, &now, dfreq, doffset);
|
||||
SPF_SlewSamples(inst->filter, &now, dfreq, doffset);
|
||||
|
||||
/* Keep the offset close to zero to not lose precision */
|
||||
if (fabs(offset) >= 1.0) {
|
||||
SST_CorrectOffset(stats, -round(offset));
|
||||
SPF_CorrectOffset(inst->filter, -round(offset));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
poll_timeout(void *arg)
|
||||
{
|
||||
NTP_Sample sample;
|
||||
int poll;
|
||||
int poll, stratum;
|
||||
|
||||
RCL_Instance inst = (RCL_Instance)arg;
|
||||
|
||||
@@ -701,11 +777,28 @@ poll_timeout(void *arg)
|
||||
inst->driver_polled = 0;
|
||||
|
||||
if (SPF_GetFilteredSample(inst->filter, &sample)) {
|
||||
double local_freq, local_offset;
|
||||
struct timespec local_ref_time;
|
||||
|
||||
/* Handle special case when PPS is used with the local reference */
|
||||
if (inst->pps_active && inst->lock_ref == -1)
|
||||
stratum = pps_stratum(inst, &sample.time);
|
||||
else
|
||||
stratum = inst->stratum;
|
||||
|
||||
if (inst->local) {
|
||||
get_local_stats(inst, &local_ref_time, &local_freq, &local_offset);
|
||||
inst->leap_status = LEAP_Unsynchronised;
|
||||
}
|
||||
|
||||
SRC_UpdateReachability(inst->source, 1);
|
||||
SRC_SetLeapStatus(inst->source, inst->leap_status);
|
||||
SRC_UpdateStatus(inst->source, stratum, inst->leap_status);
|
||||
SRC_AccumulateSample(inst->source, &sample);
|
||||
SRC_SelectSource(inst->source);
|
||||
|
||||
if (inst->local)
|
||||
follow_local(inst, &local_ref_time, local_freq, local_offset);
|
||||
|
||||
log_sample(inst, &sample.time, 1, 0, 0.0, sample.offset, sample.peer_dispersion);
|
||||
} else {
|
||||
SRC_UpdateReachability(inst->source, 0);
|
||||
|
||||
@@ -37,6 +37,7 @@ typedef struct {
|
||||
int driver_poll;
|
||||
int poll;
|
||||
int filter_length;
|
||||
int local;
|
||||
int pps_forced;
|
||||
int pps_rate;
|
||||
int min_samples;
|
||||
@@ -74,7 +75,8 @@ extern void *RCL_GetDriverData(RCL_Instance instance);
|
||||
extern char *RCL_GetDriverParameter(RCL_Instance instance);
|
||||
extern void RCL_CheckDriverOptions(RCL_Instance instance, const char **options);
|
||||
extern char *RCL_GetDriverOption(RCL_Instance instance, char *name);
|
||||
extern int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap);
|
||||
extern int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time,
|
||||
struct timespec *ref_time, int leap);
|
||||
extern int RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second);
|
||||
extern int RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
double second, double dispersion, double raw_correction);
|
||||
|
||||
117
refclock_phc.c
117
refclock_phc.c
@@ -33,6 +33,9 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
#include "array.h"
|
||||
#include "refclock.h"
|
||||
#include "hwclock.h"
|
||||
#include "local.h"
|
||||
@@ -44,14 +47,19 @@
|
||||
|
||||
struct phc_instance {
|
||||
int fd;
|
||||
int dev_index;
|
||||
int mode;
|
||||
int nocrossts;
|
||||
int extpps;
|
||||
int pin;
|
||||
int channel;
|
||||
struct timespec last_extts;
|
||||
HCL_Instance clock;
|
||||
};
|
||||
|
||||
/* Array of RCL_Instance with enabled extpps */
|
||||
static ARR_Instance extts_phcs = NULL;
|
||||
|
||||
static void read_ext_pulse(int sockfd, int event, void *anything);
|
||||
|
||||
static int phc_initialise(RCL_Instance instance)
|
||||
@@ -59,6 +67,7 @@ static int phc_initialise(RCL_Instance instance)
|
||||
const char *options[] = {"nocrossts", "extpps", "pin", "channel", "clear", NULL};
|
||||
struct phc_instance *phc;
|
||||
int phc_fd, rising_edge;
|
||||
struct stat st;
|
||||
char *path, *s;
|
||||
|
||||
RCL_CheckDriverOptions(instance, options);
|
||||
@@ -66,16 +75,20 @@ static int phc_initialise(RCL_Instance instance)
|
||||
path = RCL_GetDriverParameter(instance);
|
||||
|
||||
phc_fd = SYS_Linux_OpenPHC(path, 0);
|
||||
if (phc_fd < 0) {
|
||||
if (phc_fd < 0)
|
||||
LOG_FATAL("Could not open PHC");
|
||||
return 0;
|
||||
}
|
||||
|
||||
phc = MallocNew(struct phc_instance);
|
||||
phc->fd = phc_fd;
|
||||
if (fstat(phc_fd, &st) < 0 || !S_ISCHR(st.st_mode))
|
||||
LOG_FATAL("Could not get PHC index");
|
||||
phc->dev_index = minor(st.st_rdev);
|
||||
phc->mode = 0;
|
||||
phc->nocrossts = RCL_GetDriverOption(instance, "nocrossts") ? 1 : 0;
|
||||
phc->extpps = RCL_GetDriverOption(instance, "extpps") ? 1 : 0;
|
||||
UTI_ZeroTimespec(&phc->last_extts);
|
||||
phc->clock = HCL_CreateInstance(0, 16, UTI_Log2ToDouble(RCL_GetDriverPoll(instance)),
|
||||
RCL_GetPrecision(instance));
|
||||
|
||||
if (phc->extpps) {
|
||||
s = RCL_GetDriverOption(instance, "pin");
|
||||
@@ -83,16 +96,18 @@ static int phc_initialise(RCL_Instance instance)
|
||||
s = RCL_GetDriverOption(instance, "channel");
|
||||
phc->channel = s ? atoi(s) : 0;
|
||||
rising_edge = RCL_GetDriverOption(instance, "clear") ? 0 : 1;
|
||||
phc->clock = HCL_CreateInstance(0, 16, UTI_Log2ToDouble(RCL_GetDriverPoll(instance)));
|
||||
|
||||
if (!SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel,
|
||||
rising_edge, !rising_edge, 1))
|
||||
LOG_FATAL("Could not enable external PHC timestamping");
|
||||
|
||||
SCH_AddFileHandler(phc->fd, SCH_FILE_INPUT, read_ext_pulse, instance);
|
||||
|
||||
if (!extts_phcs)
|
||||
extts_phcs = ARR_CreateInstance(sizeof (RCL_Instance));
|
||||
ARR_AppendElement(extts_phcs, &instance);
|
||||
} else {
|
||||
phc->pin = phc->channel = 0;
|
||||
phc->clock = NULL;
|
||||
}
|
||||
|
||||
RCL_SetDriverData(instance, phc);
|
||||
@@ -102,68 +117,106 @@ static int phc_initialise(RCL_Instance instance)
|
||||
static void phc_finalise(RCL_Instance instance)
|
||||
{
|
||||
struct phc_instance *phc;
|
||||
unsigned int i;
|
||||
|
||||
phc = (struct phc_instance *)RCL_GetDriverData(instance);
|
||||
|
||||
if (phc->extpps) {
|
||||
SCH_RemoveFileHandler(phc->fd);
|
||||
SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel, 0, 0, 0);
|
||||
HCL_DestroyInstance(phc->clock);
|
||||
|
||||
for (i = 0; i < ARR_GetSize(extts_phcs); i++) {
|
||||
if ((*(RCL_Instance *)ARR_GetElement(extts_phcs, i)) == instance)
|
||||
ARR_RemoveElement(extts_phcs, i--);
|
||||
}
|
||||
if (ARR_GetSize(extts_phcs) == 0) {
|
||||
ARR_DestroyInstance(extts_phcs);
|
||||
extts_phcs = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HCL_DestroyInstance(phc->clock);
|
||||
close(phc->fd);
|
||||
Free(phc);
|
||||
}
|
||||
|
||||
static void process_ext_pulse(RCL_Instance instance, struct timespec *phc_ts)
|
||||
{
|
||||
struct phc_instance *phc;
|
||||
struct timespec local_ts;
|
||||
double local_err;
|
||||
|
||||
phc = RCL_GetDriverData(instance);
|
||||
|
||||
if (UTI_CompareTimespecs(&phc->last_extts, phc_ts) == 0) {
|
||||
DEBUG_LOG("Ignoring duplicated PHC timestamp");
|
||||
return;
|
||||
}
|
||||
phc->last_extts = *phc_ts;
|
||||
|
||||
if (!HCL_CookTime(phc->clock, phc_ts, &local_ts, &local_err))
|
||||
return;
|
||||
|
||||
RCL_AddCookedPulse(instance, &local_ts, 1.0e-9 * local_ts.tv_nsec, local_err,
|
||||
UTI_DiffTimespecsToDouble(phc_ts, &local_ts));
|
||||
}
|
||||
|
||||
static void read_ext_pulse(int fd, int event, void *anything)
|
||||
{
|
||||
RCL_Instance instance;
|
||||
struct phc_instance *phc;
|
||||
struct timespec phc_ts, local_ts;
|
||||
double local_err;
|
||||
struct phc_instance *phc1, *phc2;
|
||||
struct timespec phc_ts;
|
||||
unsigned int i;
|
||||
int channel;
|
||||
|
||||
if (!SYS_Linux_ReadPHCExtTimestamp(fd, &phc_ts, &channel))
|
||||
return;
|
||||
|
||||
instance = anything;
|
||||
phc = RCL_GetDriverData(instance);
|
||||
phc1 = RCL_GetDriverData(instance);
|
||||
|
||||
if (!SYS_Linux_ReadPHCExtTimestamp(phc->fd, &phc_ts, &channel))
|
||||
return;
|
||||
/* The Linux kernel (as of 6.2) has one shared queue of timestamps for all
|
||||
descriptors of the same PHC. Search for all refclocks that expect
|
||||
the timestamp. */
|
||||
|
||||
if (channel != phc->channel) {
|
||||
DEBUG_LOG("Unexpected extts channel %d\n", channel);
|
||||
return;
|
||||
for (i = 0; i < ARR_GetSize(extts_phcs); i++) {
|
||||
instance = *(RCL_Instance *)ARR_GetElement(extts_phcs, i);
|
||||
phc2 = RCL_GetDriverData(instance);
|
||||
if (!phc2->extpps || phc2->dev_index != phc1->dev_index || phc2->channel != channel)
|
||||
continue;
|
||||
process_ext_pulse(instance, &phc_ts);
|
||||
}
|
||||
|
||||
if (!HCL_CookTime(phc->clock, &phc_ts, &local_ts, &local_err))
|
||||
return;
|
||||
|
||||
RCL_AddCookedPulse(instance, &local_ts, 1.0e-9 * local_ts.tv_nsec, local_err,
|
||||
UTI_DiffTimespecsToDouble(&phc_ts, &local_ts));
|
||||
}
|
||||
|
||||
#define PHC_READINGS 25
|
||||
|
||||
static int phc_poll(RCL_Instance instance)
|
||||
{
|
||||
struct timespec phc_ts, sys_ts, local_ts, readings[PHC_READINGS][3];
|
||||
struct phc_instance *phc;
|
||||
struct timespec phc_ts, sys_ts, local_ts;
|
||||
double offset, phc_err, local_err;
|
||||
double phc_err, local_err;
|
||||
int n_readings;
|
||||
|
||||
phc = (struct phc_instance *)RCL_GetDriverData(instance);
|
||||
|
||||
if (!SYS_Linux_GetPHCSample(phc->fd, phc->nocrossts, RCL_GetPrecision(instance),
|
||||
&phc->mode, &phc_ts, &sys_ts, &phc_err))
|
||||
n_readings = SYS_Linux_GetPHCReadings(phc->fd, phc->nocrossts, &phc->mode,
|
||||
PHC_READINGS, readings);
|
||||
if (n_readings < 1)
|
||||
return 0;
|
||||
|
||||
if (phc->extpps) {
|
||||
LCL_CookTime(&sys_ts, &local_ts, &local_err);
|
||||
HCL_AccumulateSample(phc->clock, &phc_ts, &local_ts, phc_err + local_err);
|
||||
if (!HCL_ProcessReadings(phc->clock, n_readings, readings, &phc_ts, &sys_ts, &phc_err))
|
||||
return 0;
|
||||
}
|
||||
|
||||
offset = UTI_DiffTimespecsToDouble(&phc_ts, &sys_ts);
|
||||
LCL_CookTime(&sys_ts, &local_ts, &local_err);
|
||||
HCL_AccumulateSample(phc->clock, &phc_ts, &local_ts, phc_err + local_err);
|
||||
|
||||
DEBUG_LOG("PHC offset: %+.9f err: %.9f", offset, phc_err);
|
||||
if (phc->extpps)
|
||||
return 0;
|
||||
|
||||
return RCL_AddSample(instance, &sys_ts, offset, LEAP_Normal);
|
||||
DEBUG_LOG("PHC offset: %+.9f err: %.9f",
|
||||
UTI_DiffTimespecsToDouble(&phc_ts, &sys_ts), phc_err);
|
||||
|
||||
return RCL_AddSample(instance, &sys_ts, &phc_ts, LEAP_Normal);
|
||||
}
|
||||
|
||||
RefclockDriver RCL_PHC_driver = {
|
||||
|
||||
@@ -61,49 +61,36 @@ static int pps_initialise(RCL_Instance instance) {
|
||||
edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;
|
||||
|
||||
fd = open(path, O_RDWR);
|
||||
if (fd < 0) {
|
||||
if (fd < 0)
|
||||
LOG_FATAL("Could not open %s : %s", path, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
UTI_FdSetCloexec(fd);
|
||||
|
||||
if (time_pps_create(fd, &handle) < 0) {
|
||||
if (time_pps_create(fd, &handle) < 0)
|
||||
LOG_FATAL("time_pps_create() failed on %s : %s", path, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (time_pps_getcap(handle, &mode) < 0) {
|
||||
if (time_pps_getcap(handle, &mode) < 0)
|
||||
LOG_FATAL("time_pps_getcap() failed on %s : %s", path, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (time_pps_getparams(handle, ¶ms) < 0) {
|
||||
if (time_pps_getparams(handle, ¶ms) < 0)
|
||||
LOG_FATAL("time_pps_getparams() failed on %s : %s", path, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!edge_clear) {
|
||||
if (!(mode & PPS_CAPTUREASSERT)) {
|
||||
if (!(mode & PPS_CAPTUREASSERT))
|
||||
LOG_FATAL("CAPTUREASSERT not supported on %s", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
params.mode |= PPS_CAPTUREASSERT;
|
||||
params.mode &= ~PPS_CAPTURECLEAR;
|
||||
} else {
|
||||
if (!(mode & PPS_CAPTURECLEAR)) {
|
||||
if (!(mode & PPS_CAPTURECLEAR))
|
||||
LOG_FATAL("CAPTURECLEAR not supported on %s", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
params.mode |= PPS_CAPTURECLEAR;
|
||||
params.mode &= ~PPS_CAPTUREASSERT;
|
||||
}
|
||||
|
||||
if (time_pps_setparams(handle, ¶ms) < 0) {
|
||||
if (time_pps_setparams(handle, ¶ms) < 0)
|
||||
LOG_FATAL("time_pps_setparams() failed on %s : %s", path, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
pps = MallocNew(struct pps_instance);
|
||||
pps->handle = handle;
|
||||
|
||||
@@ -95,7 +95,6 @@ static int shm_poll(RCL_Instance instance)
|
||||
{
|
||||
struct timespec receive_ts, clock_ts;
|
||||
struct shmTime t, *shm;
|
||||
double offset;
|
||||
|
||||
shm = (struct shmTime *)RCL_GetDriverData(instance);
|
||||
|
||||
@@ -124,9 +123,8 @@ static int shm_poll(RCL_Instance instance)
|
||||
|
||||
UTI_NormaliseTimespec(&clock_ts);
|
||||
UTI_NormaliseTimespec(&receive_ts);
|
||||
offset = UTI_DiffTimespecsToDouble(&clock_ts, &receive_ts);
|
||||
|
||||
return RCL_AddSample(instance, &receive_ts, offset, t.leap);
|
||||
return RCL_AddSample(instance, &receive_ts, &clock_ts, t.leap);
|
||||
}
|
||||
|
||||
RefclockDriver RCL_SHM_driver = {
|
||||
|
||||
@@ -58,23 +58,63 @@ struct sock_sample {
|
||||
int magic;
|
||||
};
|
||||
|
||||
/* On 32-bit glibc-based systems enable conversion between timevals using
|
||||
32-bit and 64-bit time_t to support SOCK clients compiled with different
|
||||
time_t size than chrony */
|
||||
#ifdef __GLIBC_PREREQ
|
||||
#if __GLIBC_PREREQ(2, 34) && __TIMESIZE == 32
|
||||
#define CONVERT_TIMEVAL 1
|
||||
#if defined(_TIME_BITS) && _TIME_BITS == 64
|
||||
typedef int32_t alt_time_t;
|
||||
typedef int32_t alt_suseconds_t;
|
||||
#else
|
||||
typedef int64_t alt_time_t;
|
||||
typedef int64_t alt_suseconds_t;
|
||||
#endif
|
||||
struct alt_timeval {
|
||||
alt_time_t tv_sec;
|
||||
alt_suseconds_t tv_usec;
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static void read_sample(int sockfd, int event, void *anything)
|
||||
{
|
||||
char buf[sizeof (struct sock_sample) + 16];
|
||||
struct timespec sys_ts, ref_ts;
|
||||
struct sock_sample sample;
|
||||
struct timespec ts;
|
||||
RCL_Instance instance;
|
||||
int s;
|
||||
|
||||
instance = (RCL_Instance)anything;
|
||||
|
||||
s = recv(sockfd, &sample, sizeof (sample), 0);
|
||||
s = recv(sockfd, buf, sizeof (buf), 0);
|
||||
|
||||
if (s < 0) {
|
||||
DEBUG_LOG("Could not read SOCK sample : %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
if (s != sizeof (sample)) {
|
||||
if (s == sizeof (sample)) {
|
||||
memcpy(&sample, buf, sizeof (sample));
|
||||
#ifdef CONVERT_TIMEVAL
|
||||
} else if (s == sizeof (sample) - sizeof (struct timeval) + sizeof (struct alt_timeval)) {
|
||||
struct alt_timeval atv;
|
||||
memcpy(&atv, buf, sizeof (atv));
|
||||
#ifndef HAVE_LONG_TIME_T
|
||||
if (atv.tv_sec > INT32_MAX || atv.tv_sec < INT32_MIN ||
|
||||
atv.tv_usec > INT32_MAX || atv.tv_usec < INT32_MIN) {
|
||||
DEBUG_LOG("Could not convert 64-bit timeval");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
sample.tv.tv_sec = atv.tv_sec;
|
||||
sample.tv.tv_usec = atv.tv_usec;
|
||||
DEBUG_LOG("Converted %d-bit timeval", 8 * (int)sizeof (alt_time_t));
|
||||
memcpy((char *)&sample + sizeof (struct timeval), buf + sizeof (struct alt_timeval),
|
||||
sizeof (sample) - sizeof (struct timeval));
|
||||
#endif
|
||||
} else {
|
||||
DEBUG_LOG("Unexpected length of SOCK sample : %d != %ld",
|
||||
s, (long)sizeof (sample));
|
||||
return;
|
||||
@@ -86,13 +126,18 @@ static void read_sample(int sockfd, int event, void *anything)
|
||||
return;
|
||||
}
|
||||
|
||||
UTI_TimevalToTimespec(&sample.tv, &ts);
|
||||
UTI_NormaliseTimespec(&ts);
|
||||
UTI_TimevalToTimespec(&sample.tv, &sys_ts);
|
||||
UTI_NormaliseTimespec(&sys_ts);
|
||||
|
||||
if (!UTI_IsTimeOffsetSane(&sys_ts, sample.offset))
|
||||
return;
|
||||
|
||||
UTI_AddDoubleToTimespec(&sys_ts, sample.offset, &ref_ts);
|
||||
|
||||
if (sample.pulse) {
|
||||
RCL_AddPulse(instance, &ts, sample.offset);
|
||||
RCL_AddPulse(instance, &sys_ts, sample.offset);
|
||||
} else {
|
||||
RCL_AddSample(instance, &ts, sample.offset, sample.leap);
|
||||
RCL_AddSample(instance, &sys_ts, &ref_ts, sample.leap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
76
reference.c
76
reference.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2009-2018, 2020
|
||||
* Copyright (C) Miroslav Lichvar 2009-2018, 2020, 2022
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -150,6 +150,9 @@ static SCH_TimeoutID fb_drift_timeout_id;
|
||||
static double last_ref_update;
|
||||
static double last_ref_update_interval;
|
||||
|
||||
static double last_ref_adjustment;
|
||||
static int ref_adjustments;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static NTP_Leap get_tz_leap(time_t when, int *tai_offset);
|
||||
@@ -286,6 +289,8 @@ REF_Initialise(void)
|
||||
UTI_ZeroTimespec(&our_ref_time);
|
||||
last_ref_update = 0.0;
|
||||
last_ref_update_interval = 0.0;
|
||||
last_ref_adjustment = 0.0;
|
||||
ref_adjustments = 0;
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_slew, NULL);
|
||||
|
||||
@@ -960,6 +965,27 @@ fuzz_ref_time(struct timespec *ts)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static double
|
||||
get_correction_rate(double offset_sd, double update_interval)
|
||||
{
|
||||
/* We want to correct the offset quickly, but we also want to keep the
|
||||
frequency error caused by the correction itself low.
|
||||
|
||||
Define correction rate as the area of the region bounded by the graph of
|
||||
offset corrected in time. Set the rate so that the time needed to correct
|
||||
an offset equal to the current sourcestats stddev will be equal to the
|
||||
update interval multiplied by the correction time ratio (assuming linear
|
||||
adjustment). The offset and the time needed to make the correction are
|
||||
inversely proportional.
|
||||
|
||||
This is only a suggestion and it's up to the system driver how the
|
||||
adjustment will be executed. */
|
||||
|
||||
return correction_time_ratio * 0.5 * offset_sd * update_interval;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
uint32_t ref_id, IPAddr *ref_ip, struct timespec *ref_time,
|
||||
@@ -969,7 +995,7 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
{
|
||||
double uncorrected_offset, accumulate_offset, step_offset;
|
||||
double residual_frequency, local_abs_frequency;
|
||||
double elapsed, mono_now, update_interval, correction_rate, orig_root_distance;
|
||||
double elapsed, mono_now, update_interval, orig_root_distance;
|
||||
struct timespec now, raw_now;
|
||||
int manual;
|
||||
|
||||
@@ -1024,21 +1050,6 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
last_ref_update_interval = update_interval;
|
||||
last_offset = offset;
|
||||
|
||||
/* We want to correct the offset quickly, but we also want to keep the
|
||||
frequency error caused by the correction itself low.
|
||||
|
||||
Define correction rate as the area of the region bounded by the graph of
|
||||
offset corrected in time. Set the rate so that the time needed to correct
|
||||
an offset equal to the current sourcestats stddev will be equal to the
|
||||
update interval multiplied by the correction time ratio (assuming linear
|
||||
adjustment). The offset and the time needed to make the correction are
|
||||
inversely proportional.
|
||||
|
||||
This is only a suggestion and it's up to the system driver how the
|
||||
adjustment will be executed. */
|
||||
|
||||
correction_rate = correction_time_ratio * 0.5 * offset_sd * update_interval;
|
||||
|
||||
/* Check if the clock should be stepped */
|
||||
if (is_step_limit_reached(offset, uncorrected_offset)) {
|
||||
/* Cancel the uncorrected offset and correct the total offset by step */
|
||||
@@ -1050,7 +1061,8 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
}
|
||||
|
||||
/* Adjust the clock */
|
||||
LCL_AccumulateFrequencyAndOffset(frequency, accumulate_offset, correction_rate);
|
||||
LCL_AccumulateFrequencyAndOffset(frequency, accumulate_offset,
|
||||
get_correction_rate(offset_sd, update_interval));
|
||||
|
||||
maybe_log_offset(offset, raw_now.tv_sec);
|
||||
|
||||
@@ -1095,6 +1107,27 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
avg2_moving = 1;
|
||||
avg2_offset = SQUARE(offset);
|
||||
}
|
||||
|
||||
ref_adjustments = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
REF_AdjustReference(double offset, double frequency)
|
||||
{
|
||||
double adj_corr_rate, ref_corr_rate, mono_now;
|
||||
|
||||
mono_now = SCH_GetLastEventMonoTime();
|
||||
ref_adjustments++;
|
||||
|
||||
adj_corr_rate = get_correction_rate(fabs(offset), mono_now - last_ref_adjustment);
|
||||
ref_corr_rate = get_correction_rate(our_offset_sd, last_ref_update_interval) /
|
||||
ref_adjustments;
|
||||
last_ref_adjustment = mono_now;
|
||||
|
||||
return LCL_AccumulateFrequencyAndOffsetNoHandlers(frequency, offset,
|
||||
MAX(adj_corr_rate, ref_corr_rate));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1296,6 +1329,7 @@ void
|
||||
REF_ModifyMaxupdateskew(double new_max_update_skew)
|
||||
{
|
||||
max_update_skew = new_max_update_skew * 1.0e-6;
|
||||
LOG(LOGS_INFO, "New maxupdateskew %f ppm", new_max_update_skew);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1305,6 +1339,7 @@ REF_ModifyMakestep(int limit, double threshold)
|
||||
{
|
||||
make_step_limit = limit;
|
||||
make_step_threshold = threshold;
|
||||
LOG(LOGS_INFO, "New makestep %f %d", threshold, limit);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1316,6 +1351,7 @@ REF_EnableLocal(int stratum, double distance, int orphan)
|
||||
local_stratum = CLAMP(1, stratum, NTP_MAX_STRATUM - 1);
|
||||
local_distance = distance;
|
||||
local_orphan = !!orphan;
|
||||
LOG(LOGS_INFO, "%s local reference mode", "Enabled");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1324,6 +1360,7 @@ void
|
||||
REF_DisableLocal(void)
|
||||
{
|
||||
enable_local_stratum = 0;
|
||||
LOG(LOGS_INFO, "%s local reference mode", "Disabled");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1333,7 +1370,8 @@ REF_DisableLocal(void)
|
||||
static int
|
||||
is_leap_close(time_t t)
|
||||
{
|
||||
return t >= leap_when - LEAP_SECOND_CLOSE && t < leap_when + LEAP_SECOND_CLOSE;
|
||||
return leap_when != 0 &&
|
||||
t >= leap_when - LEAP_SECOND_CLOSE && t < leap_when + LEAP_SECOND_CLOSE;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -162,6 +162,10 @@ extern void REF_SetManualReference
|
||||
extern void
|
||||
REF_SetUnsynchronised(void);
|
||||
|
||||
/* Make a small correction of the clock without updating the reference
|
||||
parameters and calling the clock change handlers */
|
||||
extern int REF_AdjustReference(double offset, double frequency);
|
||||
|
||||
/* Announce a leap second before the full reference update */
|
||||
extern void REF_UpdateLeapStatus(NTP_Leap leap);
|
||||
|
||||
|
||||
26
reports.h
26
reports.h
@@ -109,14 +109,23 @@ typedef struct {
|
||||
} RPT_ClientAccessByIndex_Report;
|
||||
|
||||
typedef struct {
|
||||
uint32_t ntp_hits;
|
||||
uint32_t nke_hits;
|
||||
uint32_t cmd_hits;
|
||||
uint32_t ntp_drops;
|
||||
uint32_t nke_drops;
|
||||
uint32_t cmd_drops;
|
||||
uint32_t log_drops;
|
||||
uint32_t ntp_auth_hits;
|
||||
uint64_t ntp_hits;
|
||||
uint64_t nke_hits;
|
||||
uint64_t cmd_hits;
|
||||
uint64_t ntp_drops;
|
||||
uint64_t nke_drops;
|
||||
uint64_t cmd_drops;
|
||||
uint64_t log_drops;
|
||||
uint64_t ntp_auth_hits;
|
||||
uint64_t ntp_interleaved_hits;
|
||||
uint64_t ntp_timestamps;
|
||||
uint64_t ntp_span_seconds;
|
||||
uint64_t ntp_daemon_rx_timestamps;
|
||||
uint64_t ntp_daemon_tx_timestamps;
|
||||
uint64_t ntp_kernel_rx_timestamps;
|
||||
uint64_t ntp_kernel_tx_timestamps;
|
||||
uint64_t ntp_hw_rx_timestamps;
|
||||
uint64_t ntp_hw_tx_timestamps;
|
||||
} RPT_ServerStatsReport;
|
||||
|
||||
typedef struct {
|
||||
@@ -171,6 +180,7 @@ typedef struct {
|
||||
uint32_t total_tx_count;
|
||||
uint32_t total_rx_count;
|
||||
uint32_t total_valid_count;
|
||||
uint32_t total_good_count;
|
||||
} RPT_NTPReport;
|
||||
|
||||
typedef struct {
|
||||
|
||||
2
rtc.c
2
rtc.c
@@ -148,6 +148,8 @@ RTC_Initialise(int initial_set)
|
||||
if (driver.init) {
|
||||
if ((driver.init)()) {
|
||||
driver_initialised = 1;
|
||||
} else {
|
||||
LOG(LOGS_ERR, "RTC driver could not be initialised");
|
||||
}
|
||||
} else {
|
||||
LOG(LOGS_ERR, "RTC not supported on this operating system");
|
||||
|
||||
39
rtc_linux.c
39
rtc_linux.c
@@ -64,7 +64,7 @@ static OperatingMode operating_mode = OM_NORMAL;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int fd = -1;
|
||||
static int fd;
|
||||
|
||||
#define LOWEST_MEASUREMENT_PERIOD 15
|
||||
#define HIGHEST_MEASUREMENT_PERIOD 480
|
||||
@@ -82,16 +82,12 @@ static int skip_interrupts;
|
||||
#define MAX_SAMPLES 64
|
||||
|
||||
/* Real time clock samples. We store the seconds count as originally
|
||||
measured, together with a 'trim' that compensates these values for
|
||||
any steps made to the RTC to bring it back into line
|
||||
occasionally. The trim is in seconds. */
|
||||
measured. */
|
||||
static time_t *rtc_sec = NULL;
|
||||
static double *rtc_trim = NULL;
|
||||
|
||||
/* Reference time, against which delta times on the RTC scale are measured */
|
||||
static time_t rtc_ref;
|
||||
|
||||
|
||||
/* System clock samples associated with the above samples. */
|
||||
static struct timespec *system_times = NULL;
|
||||
|
||||
@@ -145,7 +141,7 @@ static double file_ref_offset, file_rate_ppm;
|
||||
/* ================================================== */
|
||||
|
||||
/* Flag to remember whether to assume the RTC is running on UTC */
|
||||
static int rtc_on_utc = 1;
|
||||
static int rtc_on_utc;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -168,7 +164,6 @@ discard_samples(int new_first)
|
||||
n_to_save = n_samples - new_first;
|
||||
|
||||
memmove(rtc_sec, rtc_sec + new_first, n_to_save * sizeof(time_t));
|
||||
memmove(rtc_trim, rtc_trim + new_first, n_to_save * sizeof(double));
|
||||
memmove(system_times, system_times + new_first, n_to_save * sizeof(struct timespec));
|
||||
|
||||
n_samples = n_to_save;
|
||||
@@ -188,21 +183,16 @@ accumulate_sample(time_t rtc, struct timespec *sys)
|
||||
}
|
||||
|
||||
/* Discard all samples if the RTC was stepped back (not our trim) */
|
||||
if (n_samples > 0 && rtc_sec[n_samples - 1] - rtc >= rtc_trim[n_samples - 1]) {
|
||||
if (n_samples > 0 && rtc_sec[n_samples - 1] >= rtc) {
|
||||
DEBUG_LOG("RTC samples discarded");
|
||||
n_samples = 0;
|
||||
}
|
||||
|
||||
/* Always use most recent sample as reference */
|
||||
/* use sample only if n_sample is not negative*/
|
||||
if(n_samples >=0)
|
||||
{
|
||||
rtc_ref = rtc;
|
||||
rtc_sec[n_samples] = rtc;
|
||||
rtc_trim[n_samples] = 0.0;
|
||||
system_times[n_samples] = *sys;
|
||||
++n_samples_since_regression;
|
||||
}
|
||||
++n_samples;
|
||||
}
|
||||
|
||||
@@ -227,7 +217,7 @@ run_regression(int new_sample,
|
||||
if (n_samples > 0) {
|
||||
|
||||
for (i=0; i<n_samples; i++) {
|
||||
rtc_rel[i] = rtc_trim[i] + (double)(rtc_sec[i] - rtc_ref);
|
||||
rtc_rel[i] = (double)(rtc_sec[i] - rtc_ref);
|
||||
offsets[i] = ((double) (rtc_ref - system_times[i].tv_sec) -
|
||||
(1.0e-9 * system_times[i].tv_nsec) +
|
||||
rtc_rel[i]);
|
||||
@@ -434,6 +424,7 @@ setup_config(void)
|
||||
static void
|
||||
read_coefs_from_file(void)
|
||||
{
|
||||
double ref_time;
|
||||
FILE *in;
|
||||
|
||||
if (!tried_to_load_coefs) {
|
||||
@@ -444,11 +435,12 @@ read_coefs_from_file(void)
|
||||
|
||||
if (coefs_file_name &&
|
||||
(in = UTI_OpenFile(NULL, coefs_file_name, NULL, 'r', 0))) {
|
||||
if (fscanf(in, "%d%ld%lf%lf",
|
||||
if (fscanf(in, "%d%lf%lf%lf",
|
||||
&valid_coefs_from_file,
|
||||
&file_ref_time,
|
||||
&ref_time,
|
||||
&file_ref_offset,
|
||||
&file_rate_ppm) == 4) {
|
||||
file_ref_time = ref_time;
|
||||
} else {
|
||||
LOG(LOGS_WARN, "Could not read coefficients from %s", coefs_file_name);
|
||||
}
|
||||
@@ -472,7 +464,7 @@ write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
||||
return RTC_ST_BADFILE;
|
||||
|
||||
/* Gain rate is written out in ppm */
|
||||
fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate);
|
||||
fprintf(out, "%1d %.0f %.6f %.3f\n", valid, (double)ref_time, offset, 1.0e6 * rate);
|
||||
fclose(out);
|
||||
|
||||
/* Rename the temporary file to the correct location */
|
||||
@@ -525,7 +517,6 @@ RTC_Linux_Initialise(void)
|
||||
UTI_FdSetCloexec(fd);
|
||||
|
||||
rtc_sec = MallocArray(time_t, MAX_SAMPLES);
|
||||
rtc_trim = MallocArray(double, MAX_SAMPLES);
|
||||
system_times = MallocArray(struct timespec, MAX_SAMPLES);
|
||||
|
||||
/* Setup details depending on configuration options */
|
||||
@@ -578,7 +569,6 @@ RTC_Linux_Finalise(void)
|
||||
LCL_RemoveParameterChangeHandler(slew_samples, NULL);
|
||||
|
||||
Free(rtc_sec);
|
||||
Free(rtc_trim);
|
||||
Free(system_times);
|
||||
}
|
||||
|
||||
@@ -639,11 +629,7 @@ handle_initial_trim(void)
|
||||
run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate);
|
||||
|
||||
n_samples_since_regression = 0;
|
||||
|
||||
/* Set sample number to -1 so the next sample is not used, as it will not yet be corrected for System Trim*/
|
||||
|
||||
n_samples = -1;
|
||||
|
||||
n_samples = 0;
|
||||
|
||||
read_coefs_from_file();
|
||||
|
||||
@@ -1028,8 +1014,7 @@ RTC_Linux_GetReport(RPT_RTC_Report *report)
|
||||
report->n_samples = n_samples;
|
||||
report->n_runs = n_runs;
|
||||
if (n_samples > 1) {
|
||||
report->span_seconds = ((rtc_sec[n_samples-1] - rtc_sec[0]) +
|
||||
(long)(rtc_trim[n_samples-1] - rtc_trim[0]));
|
||||
report->span_seconds = rtc_sec[n_samples - 1] - rtc_sec[0];
|
||||
} else {
|
||||
report->span_seconds = 0;
|
||||
}
|
||||
|
||||
73
samplefilt.c
73
samplefilt.c
@@ -162,6 +162,14 @@ SPF_GetNumberOfSamples(SPF_Instance filter)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SPF_GetMaxSamples(SPF_Instance filter)
|
||||
{
|
||||
return filter->max_samples;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
double
|
||||
SPF_GetAvgSampleDispersion(SPF_Instance filter)
|
||||
{
|
||||
@@ -170,11 +178,21 @@ SPF_GetAvgSampleDispersion(SPF_Instance filter)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_DropSamples(SPF_Instance filter)
|
||||
static void
|
||||
drop_samples(SPF_Instance filter, int keep_last)
|
||||
{
|
||||
filter->index = -1;
|
||||
filter->used = 0;
|
||||
if (!keep_last)
|
||||
filter->last = -1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_DropSamples(SPF_Instance filter)
|
||||
{
|
||||
drop_samples(filter, 0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -386,7 +404,6 @@ combine_selected_samples(SPF_Instance filter, int n, NTP_Sample *result)
|
||||
result->root_dispersion = MAX(disp, mean_root_dispersion);
|
||||
result->peer_delay = mean_peer_delay;
|
||||
result->root_delay = mean_root_delay;
|
||||
result->stratum = last_sample->stratum;
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -400,17 +417,40 @@ SPF_GetFilteredSample(SPF_Instance filter, NTP_Sample *sample)
|
||||
|
||||
n = select_samples(filter);
|
||||
|
||||
DEBUG_LOG("selected %d from %d samples", n, filter->used);
|
||||
|
||||
if (n < 1)
|
||||
return 0;
|
||||
|
||||
if (!combine_selected_samples(filter, n, sample))
|
||||
return 0;
|
||||
|
||||
SPF_DropSamples(filter);
|
||||
drop_samples(filter, 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_first_last(SPF_Instance filter, int *first, int *last)
|
||||
{
|
||||
if (filter->last < 0)
|
||||
return 0;
|
||||
|
||||
/* Always slew the last sample as it may be returned even if no new
|
||||
samples were accumulated */
|
||||
if (filter->used > 0) {
|
||||
*first = 0;
|
||||
*last = filter->used - 1;
|
||||
} else {
|
||||
*first = *last = filter->last;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
@@ -419,18 +459,9 @@ SPF_SlewSamples(SPF_Instance filter, struct timespec *when, double dfreq, double
|
||||
int i, first, last;
|
||||
double delta_time;
|
||||
|
||||
if (filter->last < 0)
|
||||
if (!get_first_last(filter, &first, &last))
|
||||
return;
|
||||
|
||||
/* Always slew the last sample as it may be returned even if no new
|
||||
samples were accumulated */
|
||||
if (filter->used > 0) {
|
||||
first = 0;
|
||||
last = filter->used - 1;
|
||||
} else {
|
||||
first = last = filter->last;
|
||||
}
|
||||
|
||||
for (i = first; i <= last; i++) {
|
||||
UTI_AdjustTimespec(&filter->samples[i].time, when, &filter->samples[i].time,
|
||||
&delta_time, dfreq, doffset);
|
||||
@@ -440,6 +471,20 @@ SPF_SlewSamples(SPF_Instance filter, struct timespec *when, double dfreq, double
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_CorrectOffset(SPF_Instance filter, double doffset)
|
||||
{
|
||||
int i, first, last;
|
||||
|
||||
if (!get_first_last(filter, &first, &last))
|
||||
return;
|
||||
|
||||
for (i = first; i <= last; i++)
|
||||
filter->samples[i].offset -= doffset;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_AddDispersion(SPF_Instance filter, double dispersion)
|
||||
{
|
||||
|
||||
@@ -39,11 +39,13 @@ extern void SPF_DestroyInstance(SPF_Instance filter);
|
||||
extern int SPF_AccumulateSample(SPF_Instance filter, NTP_Sample *sample);
|
||||
extern int SPF_GetLastSample(SPF_Instance filter, NTP_Sample *sample);
|
||||
extern int SPF_GetNumberOfSamples(SPF_Instance filter);
|
||||
extern int SPF_GetMaxSamples(SPF_Instance filter);
|
||||
extern double SPF_GetAvgSampleDispersion(SPF_Instance filter);
|
||||
extern void SPF_DropSamples(SPF_Instance filter);
|
||||
extern int SPF_GetFilteredSample(SPF_Instance filter, NTP_Sample *sample);
|
||||
extern void SPF_SlewSamples(SPF_Instance filter, struct timespec *when,
|
||||
double dfreq, double doffset);
|
||||
extern void SPF_CorrectOffset(SPF_Instance filter, double doffset);
|
||||
extern void SPF_AddDispersion(SPF_Instance filter, double dispersion);
|
||||
|
||||
#endif
|
||||
|
||||
47
sched.c
47
sched.c
@@ -104,14 +104,18 @@ static unsigned long n_timer_queue_entries;
|
||||
static SCH_TimeoutID next_tqe_id;
|
||||
|
||||
/* Pointer to head of free list */
|
||||
static TimerQueueEntry *tqe_free_list = NULL;
|
||||
static TimerQueueEntry *tqe_free_list;
|
||||
|
||||
/* Array of all allocated tqe blocks to be freed in finalisation */
|
||||
static ARR_Instance tqe_blocks;
|
||||
|
||||
/* Timestamp when was last timeout dispatched for each class */
|
||||
static struct timespec last_class_dispatch[SCH_NumberOfClasses];
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int need_to_exit;
|
||||
/* Flag terminating the main loop, which can be set from a signal handler */
|
||||
static volatile int need_to_exit;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -132,6 +136,8 @@ SCH_Initialise(void)
|
||||
|
||||
n_timer_queue_entries = 0;
|
||||
next_tqe_id = 0;
|
||||
tqe_free_list = NULL;
|
||||
tqe_blocks = ARR_CreateInstance(sizeof (TimerQueueEntry *));
|
||||
|
||||
timer_queue.next = &timer_queue;
|
||||
timer_queue.prev = &timer_queue;
|
||||
@@ -153,8 +159,14 @@ SCH_Initialise(void)
|
||||
|
||||
void
|
||||
SCH_Finalise(void) {
|
||||
unsigned int i;
|
||||
|
||||
ARR_DestroyInstance(file_handlers);
|
||||
|
||||
for (i = 0; i < ARR_GetSize(tqe_blocks); i++)
|
||||
Free(*(TimerQueueEntry **)ARR_GetElement(tqe_blocks, i));
|
||||
ARR_DestroyInstance(tqe_blocks);
|
||||
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, NULL);
|
||||
|
||||
initialised = 0;
|
||||
@@ -280,6 +292,7 @@ allocate_tqe(void)
|
||||
}
|
||||
new_block[0].next = NULL;
|
||||
tqe_free_list = &(new_block[TQE_ALLOC_QUANTUM - 1]);
|
||||
ARR_AppendElement(tqe_blocks, &new_block);
|
||||
}
|
||||
|
||||
result = tqe_free_list;
|
||||
@@ -498,12 +511,15 @@ SCH_RemoveTimeout(SCH_TimeoutID id)
|
||||
|
||||
static void
|
||||
dispatch_timeouts(struct timespec *now) {
|
||||
unsigned long n_done, n_entries_on_start;
|
||||
TimerQueueEntry *ptr;
|
||||
SCH_TimeoutHandler handler;
|
||||
SCH_ArbitraryArgument arg;
|
||||
int n_done = 0, n_entries_on_start = n_timer_queue_entries;
|
||||
|
||||
while (1) {
|
||||
n_entries_on_start = n_timer_queue_entries;
|
||||
n_done = 0;
|
||||
|
||||
do {
|
||||
LCL_ReadRawTime(now);
|
||||
|
||||
if (!(n_timer_queue_entries > 0 &&
|
||||
@@ -526,16 +542,21 @@ dispatch_timeouts(struct timespec *now) {
|
||||
/* Increment count of timeouts handled */
|
||||
++n_done;
|
||||
|
||||
/* If more timeouts were handled than there were in the timer queue on
|
||||
start and there are now, assume some code is scheduling timeouts with
|
||||
negative delays and abort. Make the actual limit higher in case the
|
||||
machine is temporarily overloaded and dispatching the handlers takes
|
||||
more time than was delay of a scheduled timeout. */
|
||||
if (n_done > n_timer_queue_entries * 4 &&
|
||||
n_done > n_entries_on_start * 4) {
|
||||
/* If the number of dispatched timeouts is significantly larger than the
|
||||
length of the queue on start and now, assume there is a bug causing
|
||||
an infinite loop by constantly adding a timeout with a zero or negative
|
||||
delay. Check the actual rate of timeouts to avoid false positives in
|
||||
case the execution slowed down so much (e.g. due to memory thrashing)
|
||||
that it repeatedly takes more time to handle the timeout than is its
|
||||
delay. This is a safety mechanism intended to stop a full-speed flood
|
||||
of NTP requests due to a bug in the NTP polling. */
|
||||
|
||||
if (n_done > 20 &&
|
||||
n_done > 4 * MAX(n_timer_queue_entries, n_entries_on_start) &&
|
||||
fabs(UTI_DiffTimespecsToDouble(now, &last_select_ts_raw)) / n_done < 0.01)
|
||||
LOG_FATAL("Possible infinite loop in scheduling");
|
||||
}
|
||||
}
|
||||
|
||||
} while (!need_to_exit);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
1
sched.h
1
sched.h
@@ -37,6 +37,7 @@ typedef enum {
|
||||
SCH_NtpClientClass,
|
||||
SCH_NtpPeerClass,
|
||||
SCH_NtpBroadcastClass,
|
||||
SCH_PhcPollClass,
|
||||
SCH_NumberOfClasses /* needs to be last */
|
||||
} SCH_TimeoutClass;
|
||||
|
||||
|
||||
4
siv.h
4
siv.h
@@ -53,6 +53,10 @@ extern int SIV_GetKeyLength(SIV_Algorithm algorithm);
|
||||
|
||||
extern int SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length);
|
||||
|
||||
extern int SIV_GetMinNonceLength(SIV_Instance instance);
|
||||
|
||||
extern int SIV_GetMaxNonceLength(SIV_Instance instance);
|
||||
|
||||
extern int SIV_GetTagLength(SIV_Instance instance);
|
||||
|
||||
extern int SIV_Encrypt(SIV_Instance instance,
|
||||
|
||||
39
siv_gnutls.c
39
siv_gnutls.c
@@ -102,8 +102,11 @@ SIV_CreateInstance(SIV_Algorithm algorithm)
|
||||
init_gnutls();
|
||||
|
||||
/* Check if the cipher is actually supported */
|
||||
if (gnutls_cipher_get_tag_size(calgo) == 0)
|
||||
if (gnutls_cipher_get_tag_size(calgo) == 0) {
|
||||
if (instance_counter == 0)
|
||||
deinit_gnutls();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
instance = MallocNew(struct SIV_Instance_Record);
|
||||
instance->algorithm = calgo;
|
||||
@@ -162,17 +165,29 @@ SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
|
||||
datum.data = (unsigned char *)key;
|
||||
datum.size = length;
|
||||
|
||||
/* Initialise a new cipher with the provided key (gnutls does not seem to
|
||||
have a function to change the key directly) */
|
||||
#ifdef HAVE_GNUTLS_AEAD_CIPHER_SET_KEY
|
||||
if (instance->cipher) {
|
||||
r = gnutls_aead_cipher_set_key(instance->cipher, &datum);
|
||||
if (r < 0) {
|
||||
DEBUG_LOG("Could not set cipher key : %s", gnutls_strerror(r));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initialise a new cipher with the provided key */
|
||||
r = gnutls_aead_cipher_init(&cipher, instance->algorithm, &datum);
|
||||
if (r < 0) {
|
||||
DEBUG_LOG("Could not initialise %s : %s", "cipher", gnutls_strerror(r));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Replace the previous cipher */
|
||||
/* Destroy the previous cipher (if its key could not be changed directly) */
|
||||
if (instance->cipher)
|
||||
gnutls_aead_cipher_deinit(instance->cipher);
|
||||
|
||||
instance->cipher = cipher;
|
||||
|
||||
return 1;
|
||||
@@ -180,6 +195,22 @@ SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetMinNonceLength(SIV_Instance instance)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetMaxNonceLength(SIV_Instance instance)
|
||||
{
|
||||
return INT_MAX;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetTagLength(SIV_Instance instance)
|
||||
{
|
||||
|
||||
139
siv_nettle.c
139
siv_nettle.c
@@ -34,12 +34,25 @@
|
||||
#include "siv_nettle_int.c"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
#include <nettle/siv-gcm.h>
|
||||
#endif
|
||||
|
||||
#include "memory.h"
|
||||
#include "siv.h"
|
||||
|
||||
struct SIV_Instance_Record {
|
||||
struct siv_cmac_aes128_ctx siv;
|
||||
SIV_Algorithm algorithm;
|
||||
int key_set;
|
||||
int min_nonce_length;
|
||||
int max_nonce_length;
|
||||
int tag_length;
|
||||
union {
|
||||
struct siv_cmac_aes128_ctx cmac_aes128;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
struct aes128_ctx aes128;
|
||||
#endif
|
||||
} ctx;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -49,12 +62,30 @@ SIV_CreateInstance(SIV_Algorithm algorithm)
|
||||
{
|
||||
SIV_Instance instance;
|
||||
|
||||
if (algorithm != AEAD_AES_SIV_CMAC_256)
|
||||
if (SIV_GetKeyLength(algorithm) <= 0)
|
||||
return NULL;
|
||||
|
||||
instance = MallocNew(struct SIV_Instance_Record);
|
||||
instance->algorithm = algorithm;
|
||||
instance->key_set = 0;
|
||||
|
||||
switch (algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
instance->min_nonce_length = SIV_MIN_NONCE_SIZE;
|
||||
instance->max_nonce_length = INT_MAX;
|
||||
instance->tag_length = SIV_DIGEST_SIZE;
|
||||
break;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
case AEAD_AES_128_GCM_SIV:
|
||||
instance->min_nonce_length = SIV_GCM_NONCE_SIZE;
|
||||
instance->max_nonce_length = SIV_GCM_NONCE_SIZE;
|
||||
instance->tag_length = SIV_GCM_DIGEST_SIZE;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@@ -71,11 +102,18 @@ SIV_DestroyInstance(SIV_Instance instance)
|
||||
int
|
||||
SIV_GetKeyLength(SIV_Algorithm algorithm)
|
||||
{
|
||||
assert(32 <= SIV_MAX_KEY_LENGTH);
|
||||
assert(2 * AES128_KEY_SIZE <= SIV_MAX_KEY_LENGTH);
|
||||
|
||||
if (algorithm == AEAD_AES_SIV_CMAC_256)
|
||||
return 32;
|
||||
return 0;
|
||||
switch (algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
return 2 * AES128_KEY_SIZE;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
case AEAD_AES_128_GCM_SIV:
|
||||
return AES128_KEY_SIZE;
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -83,10 +121,21 @@ SIV_GetKeyLength(SIV_Algorithm algorithm)
|
||||
int
|
||||
SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
|
||||
{
|
||||
if (length != 32)
|
||||
if (length != SIV_GetKeyLength(instance->algorithm))
|
||||
return 0;
|
||||
|
||||
siv_cmac_aes128_set_key(&instance->siv, key);
|
||||
switch (instance->algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
siv_cmac_aes128_set_key(&instance->ctx.cmac_aes128, key);
|
||||
break;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
case AEAD_AES_128_GCM_SIV:
|
||||
aes128_set_encrypt_key(&instance->ctx.aes128, key);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
instance->key_set = 1;
|
||||
|
||||
@@ -95,12 +144,28 @@ SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetMinNonceLength(SIV_Instance instance)
|
||||
{
|
||||
return instance->min_nonce_length;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetMaxNonceLength(SIV_Instance instance)
|
||||
{
|
||||
return instance->max_nonce_length;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetTagLength(SIV_Instance instance)
|
||||
{
|
||||
assert(SIV_DIGEST_SIZE <= SIV_MAX_TAG_LENGTH);
|
||||
|
||||
return SIV_DIGEST_SIZE;
|
||||
if (instance->tag_length < 1 || instance->tag_length > SIV_MAX_TAG_LENGTH)
|
||||
assert(0);
|
||||
return instance->tag_length;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -115,16 +180,31 @@ SIV_Encrypt(SIV_Instance instance,
|
||||
if (!instance->key_set)
|
||||
return 0;
|
||||
|
||||
if (nonce_length < SIV_MIN_NONCE_SIZE || assoc_length < 0 ||
|
||||
if (nonce_length < instance->min_nonce_length ||
|
||||
nonce_length > instance->max_nonce_length || assoc_length < 0 ||
|
||||
plaintext_length < 0 || plaintext_length > ciphertext_length ||
|
||||
plaintext_length + SIV_DIGEST_SIZE != ciphertext_length)
|
||||
plaintext_length + SIV_GetTagLength(instance) != ciphertext_length)
|
||||
return 0;
|
||||
|
||||
assert(assoc && plaintext);
|
||||
|
||||
siv_cmac_aes128_encrypt_message(&instance->siv, nonce_length, nonce,
|
||||
assoc_length, assoc,
|
||||
ciphertext_length, ciphertext, plaintext);
|
||||
switch (instance->algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
siv_cmac_aes128_encrypt_message(&instance->ctx.cmac_aes128,
|
||||
nonce_length, nonce, assoc_length, assoc,
|
||||
ciphertext_length, ciphertext, plaintext);
|
||||
break;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
case AEAD_AES_128_GCM_SIV:
|
||||
siv_gcm_aes128_encrypt_message(&instance->ctx.aes128,
|
||||
nonce_length, nonce, assoc_length, assoc,
|
||||
ciphertext_length, ciphertext, plaintext);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -140,17 +220,32 @@ SIV_Decrypt(SIV_Instance instance,
|
||||
if (!instance->key_set)
|
||||
return 0;
|
||||
|
||||
if (nonce_length < SIV_MIN_NONCE_SIZE || assoc_length < 0 ||
|
||||
if (nonce_length < instance->min_nonce_length ||
|
||||
nonce_length > instance->max_nonce_length || assoc_length < 0 ||
|
||||
plaintext_length < 0 || plaintext_length > ciphertext_length ||
|
||||
plaintext_length + SIV_DIGEST_SIZE != ciphertext_length)
|
||||
plaintext_length + SIV_GetTagLength(instance) != ciphertext_length)
|
||||
return 0;
|
||||
|
||||
assert(assoc && plaintext);
|
||||
|
||||
if (!siv_cmac_aes128_decrypt_message(&instance->siv, nonce_length, nonce,
|
||||
assoc_length, assoc,
|
||||
plaintext_length, plaintext, ciphertext))
|
||||
return 0;
|
||||
switch (instance->algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
if (!siv_cmac_aes128_decrypt_message(&instance->ctx.cmac_aes128,
|
||||
nonce_length, nonce, assoc_length, assoc,
|
||||
plaintext_length, plaintext, ciphertext))
|
||||
return 0;
|
||||
break;
|
||||
#ifdef HAVE_NETTLE_SIV_GCM
|
||||
case AEAD_AES_128_GCM_SIV:
|
||||
if (!siv_gcm_aes128_decrypt_message(&instance->ctx.aes128,
|
||||
nonce_length, nonce, assoc_length, assoc,
|
||||
plaintext_length, plaintext, ciphertext))
|
||||
return 0;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
4
smooth.c
4
smooth.c
@@ -302,7 +302,7 @@ SMT_Activate(struct timespec *now)
|
||||
if (!enabled || !locked)
|
||||
return;
|
||||
|
||||
LOG(LOGS_INFO, "Time smoothing activated%s", leap_only_mode ?
|
||||
LOG(LOGS_INFO, "Activated %s%s", "time smoothing", leap_only_mode ?
|
||||
" (leap seconds only)" : "");
|
||||
locked = 0;
|
||||
last_update = *now;
|
||||
@@ -322,6 +322,8 @@ SMT_Reset(struct timespec *now)
|
||||
|
||||
for (i = 0; i < NUM_STAGES; i++)
|
||||
stages[i].wander = stages[i].length = 0.0;
|
||||
|
||||
LOG(LOGS_INFO, "Reset %s", "time smoothing");
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
66
socket.c
66
socket.c
@@ -40,6 +40,7 @@
|
||||
#include "array.h"
|
||||
#include "logging.h"
|
||||
#include "privops.h"
|
||||
#include "ptp.h"
|
||||
#include "util.h"
|
||||
|
||||
#define INVALID_SOCK_FD (-4)
|
||||
@@ -58,10 +59,16 @@ struct Message {
|
||||
union sockaddr_all name;
|
||||
struct iovec iov;
|
||||
/* Buffer of sufficient length for all expected messages */
|
||||
union {
|
||||
NTP_Packet ntp_msg;
|
||||
CMD_Request cmd_request;
|
||||
CMD_Reply cmd_reply;
|
||||
struct {
|
||||
/* Extra space for Ethernet, IPv4/IPv6, and UDP headers in
|
||||
timestamped messages received from the Linux error queue */
|
||||
uint8_t l234_headers[64];
|
||||
union {
|
||||
NTP_Packet ntp_msg;
|
||||
PTP_NtpMessage ptp_msg;
|
||||
CMD_Request cmd_request;
|
||||
CMD_Reply cmd_reply;
|
||||
} msg;
|
||||
} msg_buf;
|
||||
/* Aligned buffer for control messages */
|
||||
struct cmsghdr cmsg_buf[CMSG_BUF_SIZE / sizeof (struct cmsghdr)];
|
||||
@@ -498,6 +505,8 @@ bind_unix_address(int sock_fd, const char *addr, int flags)
|
||||
{
|
||||
union sockaddr_all saddr;
|
||||
|
||||
memset(&saddr, 0, sizeof (saddr));
|
||||
|
||||
if (snprintf(saddr.un.sun_path, sizeof (saddr.un.sun_path), "%s", addr) >=
|
||||
sizeof (saddr.un.sun_path)) {
|
||||
DEBUG_LOG("Unix socket path %s too long", addr);
|
||||
@@ -530,6 +539,8 @@ connect_unix_address(int sock_fd, const char *addr)
|
||||
{
|
||||
union sockaddr_all saddr;
|
||||
|
||||
memset(&saddr, 0, sizeof (saddr));
|
||||
|
||||
if (snprintf(saddr.un.sun_path, sizeof (saddr.un.sun_path), "%s", addr) >=
|
||||
sizeof (saddr.un.sun_path)) {
|
||||
DEBUG_LOG("Unix socket path %s too long", addr);
|
||||
@@ -737,6 +748,17 @@ init_message_nonaddress(SCK_Message *message)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
match_cmsg(struct cmsghdr *cmsg, int level, int type, size_t length)
|
||||
{
|
||||
if (cmsg->cmsg_type == type && cmsg->cmsg_level == level &&
|
||||
(length == 0 || cmsg->cmsg_len == CMSG_LEN(length)))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
SCK_Message *message)
|
||||
@@ -794,8 +816,10 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
}
|
||||
|
||||
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
|
||||
if (0) {
|
||||
}
|
||||
#ifdef HAVE_IN_PKTINFO
|
||||
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
|
||||
else if (match_cmsg(cmsg, IPPROTO_IP, IP_PKTINFO, sizeof (struct in_pktinfo))) {
|
||||
struct in_pktinfo ipi;
|
||||
|
||||
if (message->addr_type != SCK_ADDR_IP)
|
||||
@@ -807,7 +831,7 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
message->if_index = ipi.ipi_ifindex;
|
||||
}
|
||||
#elif defined(IP_RECVDSTADDR)
|
||||
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
|
||||
else if (match_cmsg(cmsg, IPPROTO_IP, IP_RECVDSTADDR, sizeof (struct in_addr))) {
|
||||
struct in_addr addr;
|
||||
|
||||
if (message->addr_type != SCK_ADDR_IP)
|
||||
@@ -818,9 +842,8 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
message->local_addr.ip.family = IPADDR_INET4;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IN6_PKTINFO
|
||||
if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
|
||||
else if (match_cmsg(cmsg, IPPROTO_IPV6, IPV6_PKTINFO, sizeof (struct in6_pktinfo))) {
|
||||
struct in6_pktinfo ipi;
|
||||
|
||||
if (message->addr_type != SCK_ADDR_IP)
|
||||
@@ -833,25 +856,23 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
message->if_index = ipi.ipi6_ifindex;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCM_TIMESTAMP
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMP) {
|
||||
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMP, sizeof (struct timeval))) {
|
||||
struct timeval tv;
|
||||
|
||||
memcpy(&tv, CMSG_DATA(cmsg), sizeof (tv));
|
||||
UTI_TimevalToTimespec(&tv, &message->timestamp.kernel);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCM_TIMESTAMPNS
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPNS) {
|
||||
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof (message->timestamp.kernel))) {
|
||||
memcpy(&message->timestamp.kernel, CMSG_DATA(cmsg), sizeof (message->timestamp.kernel));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING_PKTINFO) {
|
||||
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
|
||||
sizeof (struct scm_ts_pktinfo))) {
|
||||
struct scm_ts_pktinfo ts_pktinfo;
|
||||
|
||||
memcpy(&ts_pktinfo, CMSG_DATA(cmsg), sizeof (ts_pktinfo));
|
||||
@@ -859,17 +880,17 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
message->timestamp.l2_length = ts_pktinfo.pkt_length;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING) {
|
||||
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPING,
|
||||
sizeof (struct scm_timestamping))) {
|
||||
struct scm_timestamping ts3;
|
||||
|
||||
memcpy(&ts3, CMSG_DATA(cmsg), sizeof (ts3));
|
||||
message->timestamp.kernel = ts3.ts[0];
|
||||
message->timestamp.hw = ts3.ts[2];
|
||||
}
|
||||
|
||||
if ((cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) ||
|
||||
(cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_RECVERR)) {
|
||||
else if ((match_cmsg(cmsg, SOL_IP, IP_RECVERR, 0) ||
|
||||
match_cmsg(cmsg, SOL_IPV6, IPV6_RECVERR, 0)) &&
|
||||
cmsg->cmsg_len >= CMSG_LEN(sizeof (struct sock_extended_err))) {
|
||||
struct sock_extended_err err;
|
||||
|
||||
memcpy(&err, CMSG_DATA(cmsg), sizeof (err));
|
||||
@@ -881,8 +902,7 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
||||
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_RIGHTS, 0)) {
|
||||
if (!(flags & SCK_FLAG_MSG_DESCRIPTOR) || cmsg->cmsg_len != CMSG_LEN(sizeof (int))) {
|
||||
int i, fd;
|
||||
|
||||
@@ -896,6 +916,10 @@ process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
|
||||
memcpy(&message->descriptor, CMSG_DATA(cmsg), sizeof (message->descriptor));
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_LOG("Unexpected control message level=%d type=%d len=%d",
|
||||
cmsg->cmsg_level, cmsg->cmsg_type, (int)cmsg->cmsg_len);
|
||||
}
|
||||
}
|
||||
|
||||
if (!r && message->descriptor != INVALID_SOCK_FD)
|
||||
|
||||
301
sources.c
301
sources.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2011-2016, 2018, 2020
|
||||
* Copyright (C) Miroslav Lichvar 2011-2016, 2018, 2020-2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -54,7 +54,6 @@ static int initialised = 0;
|
||||
/* ================================================== */
|
||||
/* Structure used to hold info for selecting between sources */
|
||||
struct SelectInfo {
|
||||
int stratum;
|
||||
int select_ok;
|
||||
double std_dev;
|
||||
double root_distance;
|
||||
@@ -69,6 +68,7 @@ struct SelectInfo {
|
||||
typedef enum {
|
||||
SRC_OK, /* OK so far, not a final status! */
|
||||
SRC_UNSELECTABLE, /* Has noselect option set */
|
||||
SRC_UNSYNCHRONISED, /* Provides samples but not unsynchronised */
|
||||
SRC_BAD_STATS, /* Doesn't have valid stats data */
|
||||
SRC_BAD_DISTANCE, /* Has root distance longer than allowed maximum */
|
||||
SRC_JITTERY, /* Had std dev larger than allowed maximum */
|
||||
@@ -132,11 +132,18 @@ struct SRC_Instance_Record {
|
||||
|
||||
struct SelectInfo sel_info;
|
||||
|
||||
/* Latest leap status */
|
||||
/* Current stratum */
|
||||
int stratum;
|
||||
|
||||
/* Current leap status */
|
||||
NTP_Leap leap;
|
||||
|
||||
/* Flag indicating the source has a leap second vote */
|
||||
int leap_vote;
|
||||
|
||||
/* Flag indicating the source was already reported as
|
||||
a falseticker since the last selection change */
|
||||
int reported_falseticker;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -162,6 +169,8 @@ static int max_n_sources; /* Capacity of the table */
|
||||
static int selected_source_index; /* Which source index is currently
|
||||
selected (set to INVALID_SOURCE
|
||||
if no current valid reference) */
|
||||
static int reported_no_majority; /* Flag to avoid repeated log message
|
||||
about no majority */
|
||||
|
||||
/* Score needed to replace the currently selected source */
|
||||
#define SCORE_LIMIT 10.0
|
||||
@@ -175,6 +184,11 @@ static double reselect_distance;
|
||||
static double stratum_weight;
|
||||
static double combine_limit;
|
||||
|
||||
static LOG_FileID logfileid;
|
||||
|
||||
/* Identifier of the dump file */
|
||||
#define DUMP_IDENTIFIER "SRC0\n"
|
||||
|
||||
/* ================================================== */
|
||||
/* Forward prototype */
|
||||
|
||||
@@ -183,6 +197,7 @@ static void slew_sources(struct timespec *raw, struct timespec *cooked, double d
|
||||
double doffset, LCL_ChangeType change_type, void *anything);
|
||||
static void add_dispersion(double dispersion, void *anything);
|
||||
static char *source_to_string(SRC_Instance inst);
|
||||
static char get_status_char(SRC_Status status);
|
||||
|
||||
/* ================================================== */
|
||||
/* Initialisation function */
|
||||
@@ -202,6 +217,10 @@ void SRC_Initialise(void) {
|
||||
|
||||
LCL_AddParameterChangeHandler(slew_sources, NULL);
|
||||
LCL_AddDispersionNotifyHandler(add_dispersion, NULL);
|
||||
|
||||
logfileid = CNF_GetLogSelection() ? LOG_FileOpen("selection",
|
||||
" Date (UTC) Time IP Address S EOpts Reach Score Last sample Low end High end")
|
||||
: -1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -283,6 +302,9 @@ void SRC_DestroyInstance(SRC_Instance instance)
|
||||
int dead_index, i;
|
||||
|
||||
assert(initialised);
|
||||
if (instance->index < 0 || instance->index >= n_sources ||
|
||||
instance != sources[instance->index])
|
||||
assert(0);
|
||||
|
||||
SST_DeleteInstance(instance->stats);
|
||||
dead_index = instance->index;
|
||||
@@ -313,8 +335,10 @@ SRC_ResetInstance(SRC_Instance instance)
|
||||
instance->distant = 0;
|
||||
instance->status = SRC_BAD_STATS;
|
||||
instance->sel_score = 1.0;
|
||||
instance->stratum = 0;
|
||||
instance->leap = LEAP_Unsynchronised;
|
||||
instance->leap_vote = 0;
|
||||
instance->reported_falseticker = 0;
|
||||
|
||||
memset(&instance->sel_info, 0, sizeof (instance->sel_info));
|
||||
|
||||
@@ -371,8 +395,10 @@ get_leap_status(void)
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SRC_SetLeapStatus(SRC_Instance inst, NTP_Leap leap)
|
||||
SRC_UpdateStatus(SRC_Instance inst, int stratum, NTP_Leap leap)
|
||||
{
|
||||
inst->stratum = stratum;
|
||||
|
||||
if (REF_IsLeapSecondClose(NULL, 0.0))
|
||||
return;
|
||||
|
||||
@@ -398,9 +424,9 @@ SRC_AccumulateSample(SRC_Instance inst, NTP_Sample *sample)
|
||||
|
||||
assert(initialised);
|
||||
|
||||
DEBUG_LOG("src=%s ts=%s offset=%e delay=%e disp=%e stratum=%d",
|
||||
DEBUG_LOG("src=%s ts=%s offset=%e delay=%e disp=%e",
|
||||
source_to_string(inst), UTI_TimespecToString(&sample->time), -sample->offset,
|
||||
sample->root_delay, sample->root_dispersion, sample->stratum);
|
||||
sample->root_delay, sample->root_dispersion);
|
||||
|
||||
if (REF_IsLeapSecondClose(&sample->time, sample->offset)) {
|
||||
LOG(LOGS_INFO, "Dropping sample around leap second");
|
||||
@@ -564,17 +590,17 @@ update_sel_options(void)
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
log_selection_message(const char *format, const char *arg)
|
||||
log_selection_message(LOG_Severity severity, const char *format, const char *arg)
|
||||
{
|
||||
if (REF_GetMode() != REF_ModeNormal)
|
||||
return;
|
||||
LOG(LOGS_INFO, format, arg);
|
||||
LOG(severity, format, arg);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
log_selection_source(const char *format, SRC_Instance inst)
|
||||
log_selection_source(LOG_Severity severity, const char *format, SRC_Instance inst)
|
||||
{
|
||||
char buf[320], *name, *ntp_name;
|
||||
|
||||
@@ -586,7 +612,7 @@ log_selection_source(const char *format, SRC_Instance inst)
|
||||
else
|
||||
snprintf(buf, sizeof (buf), "%s", name);
|
||||
|
||||
log_selection_message(format, buf);
|
||||
log_selection_message(severity, format, buf);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -631,13 +657,33 @@ source_to_string(SRC_Instance inst)
|
||||
static void
|
||||
mark_source(SRC_Instance inst, SRC_Status status)
|
||||
{
|
||||
struct timespec now;
|
||||
|
||||
inst->status = status;
|
||||
|
||||
DEBUG_LOG("%s status=%d options=%x reach=%o/%d updates=%d distant=%d leap=%d vote=%d lo=%f hi=%f",
|
||||
source_to_string(inst), (int)inst->status, (unsigned int)inst->sel_options,
|
||||
(unsigned int)inst->reachability, inst->reachability_size, inst->updates,
|
||||
DEBUG_LOG("%s status=%c options=%x reach=%o/%d updates=%d distant=%d leap=%d vote=%d lo=%f hi=%f",
|
||||
source_to_string(inst), get_status_char(inst->status),
|
||||
(unsigned int)inst->sel_options, (unsigned int)inst->reachability,
|
||||
inst->reachability_size, inst->updates,
|
||||
inst->distant, (int)inst->leap, inst->leap_vote,
|
||||
inst->sel_info.lo_limit, inst->sel_info.hi_limit);
|
||||
|
||||
if (logfileid == -1)
|
||||
return;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||
|
||||
LOG_FileWrite(logfileid,
|
||||
"%s %-15s %c -%c%c%c%c %4o %5.2f %10.3e %10.3e %10.3e",
|
||||
UTI_TimeToLogForm(now.tv_sec), source_to_string(inst),
|
||||
get_status_char(inst->status),
|
||||
inst->sel_options & SRC_SELECT_NOSELECT ? 'N' : '-',
|
||||
inst->sel_options & SRC_SELECT_PREFER ? 'P' : '-',
|
||||
inst->sel_options & SRC_SELECT_TRUST ? 'T' : '-',
|
||||
inst->sel_options & SRC_SELECT_REQUIRE ? 'R' : '-',
|
||||
(unsigned int)inst->reachability, inst->sel_score,
|
||||
inst->sel_info.last_sample_ago,
|
||||
inst->sel_info.lo_limit, inst->sel_info.hi_limit);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -714,8 +760,8 @@ combine_sources(int n_sel_sources, struct timespec *ref_time, double *offset,
|
||||
offset_weight = 1.0 / sources[index]->sel_info.root_distance;
|
||||
frequency_weight = 1.0 / SQUARE(src_frequency_sd);
|
||||
|
||||
DEBUG_LOG("combining index=%d oweight=%e offset=%e osd=%e fweight=%e freq=%e fsd=%e skew=%e",
|
||||
index, offset_weight, src_offset, src_offset_sd,
|
||||
DEBUG_LOG("combining %s oweight=%e offset=%e osd=%e fweight=%e freq=%e fsd=%e skew=%e",
|
||||
source_to_string(sources[index]), offset_weight, src_offset, src_offset_sd,
|
||||
frequency_weight, src_frequency, src_frequency_sd, src_skew);
|
||||
|
||||
sum_offset_weight += offset_weight;
|
||||
@@ -771,7 +817,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
if (n_sources == 0) {
|
||||
/* In this case, we clearly cannot synchronise to anything */
|
||||
if (selected_source_index != INVALID_SOURCE) {
|
||||
log_selection_message("Can't synchronise: no sources", NULL);
|
||||
log_selection_message(LOGS_INFO, "Can't synchronise: no sources", NULL);
|
||||
selected_source_index = INVALID_SOURCE;
|
||||
}
|
||||
return;
|
||||
@@ -807,8 +853,14 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Ignore sources which are not synchronised */
|
||||
if (sources[i]->leap == LEAP_Unsynchronised) {
|
||||
mark_source(sources[i], SRC_UNSYNCHRONISED);
|
||||
continue;
|
||||
}
|
||||
|
||||
si = &sources[i]->sel_info;
|
||||
SST_GetSelectionData(sources[i]->stats, &now, &si->stratum,
|
||||
SST_GetSelectionData(sources[i]->stats, &now,
|
||||
&si->lo_limit, &si->hi_limit, &si->root_distance,
|
||||
&si->std_dev, &first_sample_ago,
|
||||
&si->last_sample_ago, &si->select_ok);
|
||||
@@ -890,10 +942,10 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
source can settle down to a state where only one server is serving its
|
||||
local unsychronised time and others are synchronised to it. */
|
||||
|
||||
if (si->stratum >= orphan_stratum && sources[i]->type == SRC_NTP) {
|
||||
if (sources[i]->stratum >= orphan_stratum && sources[i]->type == SRC_NTP) {
|
||||
mark_source(sources[i], SRC_ORPHAN);
|
||||
|
||||
if (si->stratum == orphan_stratum && sources[i]->reachability &&
|
||||
if (sources[i]->stratum == orphan_stratum && sources[i]->reachability &&
|
||||
(orphan_source == INVALID_SOURCE ||
|
||||
sources[i]->ref_id < sources[orphan_source]->ref_id))
|
||||
orphan_source = i;
|
||||
@@ -958,7 +1010,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
if (n_endpoints == 0) {
|
||||
/* No sources provided valid endpoints */
|
||||
if (selected_source_index != INVALID_SOURCE) {
|
||||
log_selection_message("Can't synchronise: no selectable sources", NULL);
|
||||
log_selection_message(LOGS_INFO, "Can't synchronise: no selectable sources", NULL);
|
||||
selected_source_index = INVALID_SOURCE;
|
||||
}
|
||||
return;
|
||||
@@ -1038,8 +1090,12 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
(best_trust_depth > 0 && best_trust_depth <= n_sel_trust_sources / 2)) {
|
||||
/* Could not even get half the reachable (trusted) sources to agree */
|
||||
|
||||
if (!reported_no_majority) {
|
||||
log_selection_message(LOGS_WARN, "Can't synchronise: no majority", NULL);
|
||||
reported_no_majority = 1;
|
||||
}
|
||||
|
||||
if (selected_source_index != INVALID_SOURCE) {
|
||||
log_selection_message("Can't synchronise: no majority", NULL);
|
||||
REF_SetUnsynchronised();
|
||||
selected_source_index = INVALID_SOURCE;
|
||||
}
|
||||
@@ -1085,12 +1141,16 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
sel_req_source = 0;
|
||||
} else {
|
||||
mark_source(sources[i], SRC_FALSETICKER);
|
||||
if (!sources[i]->reported_falseticker) {
|
||||
log_selection_source(LOGS_WARN, "Detected falseticker %s", sources[i]);
|
||||
sources[i]->reported_falseticker = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!n_sel_sources || sel_req_source || n_sel_sources < CNF_GetMinSources()) {
|
||||
if (selected_source_index != INVALID_SOURCE) {
|
||||
log_selection_message("Can't synchronise: %s selectable sources",
|
||||
log_selection_message(LOGS_INFO, "Can't synchronise: %s selectable sources",
|
||||
!n_sel_sources ? "no" :
|
||||
sel_req_source ? "no required source in" : "not enough");
|
||||
selected_source_index = INVALID_SOURCE;
|
||||
@@ -1131,10 +1191,10 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
/* Find minimum stratum */
|
||||
|
||||
index = sel_sources[0];
|
||||
min_stratum = sources[index]->sel_info.stratum;
|
||||
min_stratum = sources[index]->stratum;
|
||||
for (i = 1; i < n_sel_sources; i++) {
|
||||
index = sel_sources[i];
|
||||
stratum = sources[index]->sel_info.stratum;
|
||||
stratum = sources[index]->stratum;
|
||||
if (stratum < min_stratum)
|
||||
min_stratum = stratum;
|
||||
}
|
||||
@@ -1147,7 +1207,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
|
||||
if (selected_source_index != INVALID_SOURCE)
|
||||
sel_src_distance = sources[selected_source_index]->sel_info.root_distance +
|
||||
(sources[selected_source_index]->sel_info.stratum - min_stratum) * stratum_weight;
|
||||
(sources[selected_source_index]->stratum - min_stratum) * stratum_weight;
|
||||
|
||||
for (i = 0; i < n_sources; i++) {
|
||||
/* Reset score for non-selectable sources */
|
||||
@@ -1159,7 +1219,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
}
|
||||
|
||||
distance = sources[i]->sel_info.root_distance +
|
||||
(sources[i]->sel_info.stratum - min_stratum) * stratum_weight;
|
||||
(sources[i]->stratum - min_stratum) * stratum_weight;
|
||||
if (sources[i]->type == SRC_NTP)
|
||||
distance += reselect_distance;
|
||||
|
||||
@@ -1207,13 +1267,16 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
}
|
||||
|
||||
selected_source_index = max_score_index;
|
||||
log_selection_source("Selected source %s", sources[selected_source_index]);
|
||||
log_selection_source(LOGS_INFO, "Selected source %s", sources[selected_source_index]);
|
||||
|
||||
/* New source has been selected, reset all scores */
|
||||
for (i = 0; i < n_sources; i++) {
|
||||
sources[i]->sel_score = 1.0;
|
||||
sources[i]->distant = 0;
|
||||
sources[i]->reported_falseticker = 0;
|
||||
}
|
||||
|
||||
reported_no_majority = 0;
|
||||
}
|
||||
|
||||
mark_source(sources[selected_source_index], SRC_SELECTED);
|
||||
@@ -1247,7 +1310,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
|
||||
combined = combine_sources(n_sel_sources, &ref_time, &src_offset, &src_offset_sd,
|
||||
&src_frequency, &src_frequency_sd, &src_skew);
|
||||
|
||||
REF_SetReference(sources[selected_source_index]->sel_info.stratum,
|
||||
REF_SetReference(sources[selected_source_index]->stratum,
|
||||
leap_status, combined,
|
||||
sources[selected_source_index]->ref_id,
|
||||
sources[selected_source_index]->ip_addr,
|
||||
@@ -1320,24 +1383,60 @@ add_dispersion(double dispersion, void *anything)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static
|
||||
FILE *open_dumpfile(SRC_Instance inst, char mode)
|
||||
static int
|
||||
get_dumpfile(SRC_Instance inst, char *filename, size_t len)
|
||||
{
|
||||
char filename[64], *dumpdir;
|
||||
/* Use the IP address, or reference ID with reference clocks */
|
||||
switch (inst->type) {
|
||||
case SRC_NTP:
|
||||
if (!UTI_IsIPReal(inst->ip_addr) ||
|
||||
snprintf(filename, len, "%s", source_to_string(inst)) >= len)
|
||||
return 0;
|
||||
break;
|
||||
case SRC_REFCLOCK:
|
||||
if (snprintf(filename, len, "refid:%08"PRIx32, inst->ref_id) >= len)
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
save_source(SRC_Instance inst)
|
||||
{
|
||||
char filename[64], *dumpdir, *ntp_name;
|
||||
FILE *f;
|
||||
|
||||
dumpdir = CNF_GetDumpDir();
|
||||
if (!dumpdir)
|
||||
return NULL;
|
||||
return;
|
||||
|
||||
/* Include IP address in the name for NTP sources, or reference ID in hex */
|
||||
if (inst->type == SRC_NTP && UTI_IsIPReal(inst->ip_addr))
|
||||
snprintf(filename, sizeof (filename), "%s", source_to_string(inst));
|
||||
else if (inst->type == SRC_REFCLOCK)
|
||||
snprintf(filename, sizeof (filename), "refid:%08"PRIx32, inst->ref_id);
|
||||
else
|
||||
return NULL;
|
||||
if (!get_dumpfile(inst, filename, sizeof (filename)))
|
||||
return;
|
||||
|
||||
return UTI_OpenFile(dumpdir, filename, ".dat", mode, 0644);
|
||||
f = UTI_OpenFile(dumpdir, filename, ".dat", 'w', 0644);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->ip_addr) : ".";
|
||||
|
||||
if (fprintf(f, "%s%s\n%d %o %d %d %d\n",
|
||||
DUMP_IDENTIFIER, ntp_name, inst->authenticated,
|
||||
(unsigned int)inst->reachability, inst->reachability_size,
|
||||
inst->stratum, (int)inst->leap) < 0 ||
|
||||
!SST_SaveToFile(inst->stats, f)) {
|
||||
fclose(f);
|
||||
if (!UTI_RemoveFile(dumpdir, filename, ".dat"))
|
||||
;
|
||||
return;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1346,16 +1445,60 @@ FILE *open_dumpfile(SRC_Instance inst, char mode)
|
||||
void
|
||||
SRC_DumpSources(void)
|
||||
{
|
||||
FILE *out;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_sources; i++) {
|
||||
out = open_dumpfile(sources[i], 'w');
|
||||
if (!out)
|
||||
continue;
|
||||
SST_SaveToFile(sources[i]->stats, out);
|
||||
fclose(out);
|
||||
for (i = 0; i < n_sources; i++)
|
||||
save_source(sources[i]);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define MAX_WORDS 1
|
||||
|
||||
static void
|
||||
load_source(SRC_Instance inst)
|
||||
{
|
||||
char filename[64], line[256], *dumpdir, *ntp_name, *words[MAX_WORDS];
|
||||
int auth, leap, reach_size, stratum;
|
||||
unsigned int reach;
|
||||
FILE *f;
|
||||
|
||||
dumpdir = CNF_GetDumpDir();
|
||||
if (!dumpdir)
|
||||
return;
|
||||
|
||||
if (!get_dumpfile(inst, filename, sizeof (filename)))
|
||||
return;
|
||||
|
||||
f = UTI_OpenFile(dumpdir, filename, ".dat", 'r', 0);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->ip_addr) : NULL;
|
||||
|
||||
if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
|
||||
(inst->type == SRC_NTP && (!ntp_name || strcmp(words[0], ntp_name) != 0)) ||
|
||||
!fgets(line, sizeof (line), f) ||
|
||||
sscanf(words[0], "%d %o %d %d %d",
|
||||
&auth, &reach, &reach_size, &stratum, &leap) != 5 ||
|
||||
(!auth && inst->authenticated) ||
|
||||
stratum < 0 || stratum >= NTP_MAX_STRATUM ||
|
||||
leap < LEAP_Normal || leap >= LEAP_Unsynchronised ||
|
||||
!SST_LoadFromFile(inst->stats, f)) {
|
||||
LOG(LOGS_WARN, "Could not load dump file for %s", source_to_string(inst));
|
||||
fclose(f);
|
||||
return;
|
||||
}
|
||||
|
||||
inst->reachability = reach & ((1U << SOURCE_REACH_BITS) - 1);
|
||||
inst->reachability_size = CLAMP(0, reach_size, SOURCE_REACH_BITS);
|
||||
inst->stratum = stratum;
|
||||
inst->leap = leap;
|
||||
|
||||
LOG(LOGS_INFO, "Loaded dump file for %s", source_to_string(inst));
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1363,21 +1506,17 @@ SRC_DumpSources(void)
|
||||
void
|
||||
SRC_ReloadSources(void)
|
||||
{
|
||||
FILE *in;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_sources; i++) {
|
||||
in = open_dumpfile(sources[i], 'r');
|
||||
if (!in)
|
||||
continue;
|
||||
if (!SST_LoadFromFile(sources[i]->stats, in))
|
||||
LOG(LOGS_WARN, "Could not load dump file for %s",
|
||||
source_to_string(sources[i]));
|
||||
else
|
||||
LOG(LOGS_INFO, "Loaded dump file for %s",
|
||||
source_to_string(sources[i]));
|
||||
fclose(in);
|
||||
load_source(sources[i]);
|
||||
|
||||
/* Allow an immediate update of the reference */
|
||||
sources[i]->updates++;
|
||||
}
|
||||
|
||||
/* Select sources and set the reference */
|
||||
SRC_SelectSource(NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1428,6 +1567,8 @@ SRC_ResetSources(void)
|
||||
|
||||
for (i = 0; i < n_sources; i++)
|
||||
SRC_ResetInstance(sources[i]);
|
||||
|
||||
LOG(LOGS_INFO, "Reset all sources");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1475,6 +1616,46 @@ SRC_ActiveSources(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static SRC_Instance
|
||||
find_source(IPAddr *ip, uint32_t ref_id)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_sources; i++) {
|
||||
if ((ip->family != IPADDR_UNSPEC && sources[i]->type == SRC_NTP &&
|
||||
UTI_CompareIPs(ip, sources[i]->ip_addr, NULL) == 0) ||
|
||||
(ip->family == IPADDR_UNSPEC && sources[i]->type == SRC_REFCLOCK &&
|
||||
ref_id == sources[i]->ref_id))
|
||||
return sources[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask)
|
||||
{
|
||||
SRC_Instance inst;
|
||||
|
||||
inst = find_source(ip, ref_id);
|
||||
if (!inst)
|
||||
return 0;
|
||||
|
||||
if ((inst->conf_sel_options & mask) == options)
|
||||
return 1;
|
||||
|
||||
inst->conf_sel_options = (inst->conf_sel_options & ~mask) | options;
|
||||
LOG(LOGS_INFO, "Source %s selection options modified", source_to_string(inst));
|
||||
|
||||
update_sel_options();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now)
|
||||
{
|
||||
@@ -1492,6 +1673,8 @@ SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now)
|
||||
report->ip_addr.family = IPADDR_INET4;
|
||||
}
|
||||
|
||||
report->stratum = src->stratum;
|
||||
|
||||
switch (src->status) {
|
||||
case SRC_FALSETICKER:
|
||||
report->state = RPT_FALSETICKER;
|
||||
@@ -1556,6 +1739,8 @@ get_status_char(SRC_Status status)
|
||||
switch (status) {
|
||||
case SRC_UNSELECTABLE:
|
||||
return 'N';
|
||||
case SRC_UNSYNCHRONISED:
|
||||
return 's';
|
||||
case SRC_BAD_STATS:
|
||||
return 'M';
|
||||
case SRC_BAD_DISTANCE:
|
||||
|
||||
@@ -87,8 +87,8 @@ extern void SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr);
|
||||
/* Function to get access to the sourcestats instance */
|
||||
extern SST_Stats SRC_GetSourcestats(SRC_Instance instance);
|
||||
|
||||
/* Function to set the current leap status according to the source */
|
||||
extern void SRC_SetLeapStatus(SRC_Instance instance, NTP_Leap leap);
|
||||
/* Function to update the stratum and leap status of the source */
|
||||
extern void SRC_UpdateStatus(SRC_Instance instance, int stratum, NTP_Leap leap);
|
||||
|
||||
/* Function to accumulate a new sample from the source */
|
||||
extern void SRC_AccumulateSample(SRC_Instance instance, NTP_Sample *sample);
|
||||
@@ -131,6 +131,10 @@ extern int SRC_IsReachable(SRC_Instance inst);
|
||||
extern int SRC_ReadNumberOfSources(void);
|
||||
extern int SRC_ActiveSources(void);
|
||||
|
||||
/* Modify selection options of an NTP source specified by address, or
|
||||
refclock specified by its reference ID */
|
||||
extern int SRC_ModifySelectOptions(IPAddr *ip, uint32_t ref_id, int options, int mask);
|
||||
|
||||
extern int SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now);
|
||||
extern int SRC_ReportSourcestats(int index, RPT_SourcestatsReport *report, struct timespec *now);
|
||||
extern int SRC_GetSelectReport(int index, RPT_SelectReport *report);
|
||||
|
||||
163
sourcestats.c
163
sourcestats.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2011-2014, 2016-2018
|
||||
* Copyright (C) Miroslav Lichvar 2011-2014, 2016-2018, 2021
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
@@ -80,7 +80,7 @@ static LOG_FileID logfileid;
|
||||
|
||||
struct SST_Stats_Record {
|
||||
|
||||
/* Reference ID and IP address of source, used for logging to statistics log */
|
||||
/* Reference ID and IP address (NULL if not an NTP source) */
|
||||
uint32_t refid;
|
||||
IPAddr *ip_addr;
|
||||
|
||||
@@ -177,9 +177,6 @@ struct SST_Stats_Record {
|
||||
/* This array contains the root dispersions of each sample at the
|
||||
time of the measurements */
|
||||
double root_dispersions[MAX_SAMPLES];
|
||||
|
||||
/* The stratum from the last accumulated sample */
|
||||
int stratum;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -214,8 +211,8 @@ SST_CreateInstance(uint32_t refid, IPAddr *addr, int min_samples, int max_sample
|
||||
SST_Stats inst;
|
||||
inst = MallocNew(struct SST_Stats_Record);
|
||||
|
||||
inst->min_samples = min_samples;
|
||||
inst->max_samples = max_samples;
|
||||
inst->max_samples = max_samples > 0 ? CLAMP(1, max_samples, MAX_SAMPLES) : MAX_SAMPLES;
|
||||
inst->min_samples = CLAMP(1, min_samples, inst->max_samples);
|
||||
inst->fixed_min_delay = min_delay;
|
||||
inst->fixed_asymmetry = asymmetry;
|
||||
|
||||
@@ -321,7 +318,6 @@ SST_AccumulateSample(SST_Stats inst, NTP_Sample *sample)
|
||||
inst->peer_dispersions[m] = sample->peer_dispersion;
|
||||
inst->root_delays[m] = sample->root_delay;
|
||||
inst->root_dispersions[m] = sample->root_dispersion;
|
||||
inst->stratum = sample->stratum;
|
||||
|
||||
if (inst->peer_delays[n] < inst->fixed_min_delay)
|
||||
inst->peer_delays[n] = 2.0 * inst->fixed_min_delay - inst->peer_delays[n];
|
||||
@@ -650,7 +646,6 @@ SST_GetFrequencyRange(SST_Stats inst,
|
||||
|
||||
void
|
||||
SST_GetSelectionData(SST_Stats inst, struct timespec *now,
|
||||
int *stratum,
|
||||
double *offset_lo_limit,
|
||||
double *offset_hi_limit,
|
||||
double *root_distance,
|
||||
@@ -670,7 +665,6 @@ SST_GetSelectionData(SST_Stats inst, struct timespec *now,
|
||||
i = get_runsbuf_index(inst, inst->best_single_sample);
|
||||
j = get_buf_index(inst, inst->best_single_sample);
|
||||
|
||||
*stratum = inst->stratum;
|
||||
*std_dev = inst->std_dev;
|
||||
|
||||
sample_elapsed = fabs(UTI_DiffTimespecsToDouble(now, &inst->sample_times[i]));
|
||||
@@ -704,7 +698,8 @@ SST_GetSelectionData(SST_Stats inst, struct timespec *now,
|
||||
|
||||
/* If maxsamples is too small to have a successful regression, enable the
|
||||
selection as a special case for a fast update/print-once reference mode */
|
||||
if (!*select_ok && inst->n_samples < 3 && inst->n_samples == inst->max_samples) {
|
||||
if (!*select_ok && inst->n_samples < MIN_SAMPLES_FOR_REGRESS &&
|
||||
inst->n_samples == inst->max_samples) {
|
||||
*std_dev = CNF_GetMaxJitter();
|
||||
*select_ok = 1;
|
||||
}
|
||||
@@ -785,6 +780,22 @@ SST_SlewSamples(SST_Stats inst, struct timespec *when, double dfreq, double doff
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SST_CorrectOffset(SST_Stats inst, double doffset)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!inst->n_samples)
|
||||
return;
|
||||
|
||||
for (i = -inst->runs_samples; i < inst->n_samples; i++)
|
||||
inst->offsets[get_runsbuf_index(inst, i)] += doffset;
|
||||
|
||||
inst->estimated_offset += doffset;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SST_AddDispersion(SST_Stats inst, double dispersion)
|
||||
{
|
||||
@@ -804,7 +815,7 @@ SST_PredictOffset(SST_Stats inst, struct timespec *when)
|
||||
{
|
||||
double elapsed;
|
||||
|
||||
if (inst->n_samples < 3) {
|
||||
if (inst->n_samples < MIN_SAMPLES_FOR_REGRESS) {
|
||||
/* We don't have any useful statistics, and presumably the poll
|
||||
interval is minimal. We can't do any useful prediction other
|
||||
than use the latest sample or zero if we don't have any samples */
|
||||
@@ -858,38 +869,30 @@ SST_GetDelayTestData(SST_Stats inst, struct timespec *sample_time,
|
||||
/* This is used to save the register to a file, so that we can reload
|
||||
it after restarting the daemon */
|
||||
|
||||
void
|
||||
int
|
||||
SST_SaveToFile(SST_Stats inst, FILE *out)
|
||||
{
|
||||
int m, i, j;
|
||||
|
||||
fprintf(out, "%d\n", inst->n_samples);
|
||||
if (inst->n_samples < 1)
|
||||
return 0;
|
||||
|
||||
if (fprintf(out, "%d %d\n", inst->n_samples, inst->asymmetry_run) < 0)
|
||||
return 0;
|
||||
|
||||
for(m = 0; m < inst->n_samples; m++) {
|
||||
i = get_runsbuf_index(inst, m);
|
||||
j = get_buf_index(inst, m);
|
||||
|
||||
fprintf(out,
|
||||
#ifdef HAVE_LONG_TIME_T
|
||||
"%08"PRIx64" %08lx %.6e %.6e %.6e %.6e %.6e %.6e %.6e %d\n",
|
||||
(uint64_t)inst->sample_times[i].tv_sec,
|
||||
#else
|
||||
"%08lx %08lx %.6e %.6e %.6e %.6e %.6e %.6e %.6e %d\n",
|
||||
(unsigned long)inst->sample_times[i].tv_sec,
|
||||
#endif
|
||||
(unsigned long)inst->sample_times[i].tv_nsec / 1000,
|
||||
inst->offsets[i],
|
||||
inst->orig_offsets[j],
|
||||
inst->peer_delays[i],
|
||||
inst->peer_dispersions[j],
|
||||
inst->root_delays[j],
|
||||
inst->root_dispersions[j],
|
||||
1.0, /* used to be inst->weights[i] */
|
||||
inst->stratum /* used to be an array */);
|
||||
|
||||
if (fprintf(out, "%s %.6e %.6e %.6e %.6e %.6e %.6e\n",
|
||||
UTI_TimespecToString(&inst->sample_times[i]),
|
||||
inst->offsets[i], inst->orig_offsets[j],
|
||||
inst->peer_delays[i], inst->peer_dispersions[j],
|
||||
inst->root_delays[j], inst->root_dispersions[j]) < 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(out, "%d\n", inst->asymmetry_run);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -898,65 +901,47 @@ SST_SaveToFile(SST_Stats inst, FILE *out)
|
||||
int
|
||||
SST_LoadFromFile(SST_Stats inst, FILE *in)
|
||||
{
|
||||
#ifdef HAVE_LONG_TIME_T
|
||||
uint64_t sec;
|
||||
#else
|
||||
unsigned long sec;
|
||||
#endif
|
||||
unsigned long usec;
|
||||
int i;
|
||||
char line[1024];
|
||||
double weight;
|
||||
int i, n_samples, arun;
|
||||
struct timespec now;
|
||||
double sample_time;
|
||||
char line[256];
|
||||
|
||||
if (!fgets(line, sizeof (line), in) ||
|
||||
sscanf(line, "%d %d", &n_samples, &arun) != 2 ||
|
||||
n_samples < 1 || n_samples > MAX_SAMPLES)
|
||||
return 0;
|
||||
|
||||
SST_ResetInstance(inst);
|
||||
|
||||
if (fgets(line, sizeof(line), in) &&
|
||||
sscanf(line, "%d", &inst->n_samples) == 1 &&
|
||||
inst->n_samples >= 0 && inst->n_samples <= MAX_SAMPLES) {
|
||||
LCL_ReadCookedTime(&now, NULL);
|
||||
|
||||
for (i=0; i<inst->n_samples; i++) {
|
||||
if (!fgets(line, sizeof(line), in) ||
|
||||
(sscanf(line,
|
||||
#ifdef HAVE_LONG_TIME_T
|
||||
"%"SCNx64"%lx%lf%lf%lf%lf%lf%lf%lf%d\n",
|
||||
#else
|
||||
"%lx%lx%lf%lf%lf%lf%lf%lf%lf%d\n",
|
||||
#endif
|
||||
&(sec), &(usec),
|
||||
&(inst->offsets[i]),
|
||||
&(inst->orig_offsets[i]),
|
||||
&(inst->peer_delays[i]),
|
||||
&(inst->peer_dispersions[i]),
|
||||
&(inst->root_delays[i]),
|
||||
&(inst->root_dispersions[i]),
|
||||
&weight, /* not used anymore */
|
||||
&inst->stratum) != 10)) {
|
||||
for (i = 0; i < n_samples; i++) {
|
||||
if (!fgets(line, sizeof (line), in) ||
|
||||
sscanf(line, "%lf %lf %lf %lf %lf %lf %lf",
|
||||
&sample_time, &inst->offsets[i], &inst->orig_offsets[i],
|
||||
&inst->peer_delays[i], &inst->peer_dispersions[i],
|
||||
&inst->root_delays[i], &inst->root_dispersions[i]) != 7)
|
||||
return 0;
|
||||
|
||||
/* This is the branch taken if the read FAILED */
|
||||
if (!UTI_IsTimeOffsetSane(&now, sample_time - UTI_TimespecToDouble(&now)))
|
||||
return 0;
|
||||
|
||||
inst->n_samples = 0; /* Load abandoned if any sign of corruption */
|
||||
return 0;
|
||||
} else {
|
||||
/* Some resolution is lost in the double format, but that's ok */
|
||||
UTI_DoubleToTimespec(sample_time, &inst->sample_times[i]);
|
||||
|
||||
/* This is the branch taken if the read is SUCCESSFUL */
|
||||
inst->sample_times[i].tv_sec = sec;
|
||||
inst->sample_times[i].tv_nsec = 1000 * usec;
|
||||
UTI_NormaliseTimespec(&inst->sample_times[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* This field was not saved in older versions */
|
||||
if (!fgets(line, sizeof(line), in) || sscanf(line, "%d\n", &inst->asymmetry_run) != 1)
|
||||
inst->asymmetry_run = 0;
|
||||
} else {
|
||||
inst->n_samples = 0; /* Load abandoned if any sign of corruption */
|
||||
return 0;
|
||||
/* Make sure the samples are sane and they are in order */
|
||||
if (!UTI_IsTimeOffsetSane(&inst->sample_times[i], -inst->offsets[i]) ||
|
||||
UTI_CompareTimespecs(&now, &inst->sample_times[i]) < 0 ||
|
||||
!(fabs(inst->peer_delays[i]) < 1.0e6 && fabs(inst->peer_dispersions[i]) < 1.0e6 &&
|
||||
fabs(inst->root_delays[i]) < 1.0e6 && fabs(inst->root_dispersions[i]) < 1.0e6) ||
|
||||
(i > 0 && UTI_CompareTimespecs(&inst->sample_times[i],
|
||||
&inst->sample_times[i - 1]) <= 0))
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!inst->n_samples)
|
||||
return 1;
|
||||
|
||||
inst->n_samples = n_samples;
|
||||
inst->last_sample = inst->n_samples - 1;
|
||||
inst->asymmetry_run = CLAMP(-MAX_ASYMMETRY_RUN, arun, MAX_ASYMMETRY_RUN);
|
||||
|
||||
find_min_delay_sample(inst);
|
||||
SST_DoNewRegression(inst);
|
||||
@@ -978,11 +963,11 @@ SST_DoSourceReport(SST_Stats inst, RPT_SourceReport *report, struct timespec *no
|
||||
report->orig_latest_meas = inst->orig_offsets[j];
|
||||
report->latest_meas = inst->offsets[i];
|
||||
report->latest_meas_err = 0.5*inst->root_delays[j] + inst->root_dispersions[j];
|
||||
report->stratum = inst->stratum;
|
||||
|
||||
/* Align the sample time to reduce the leak of the receive timestamp */
|
||||
/* Align the sample time to reduce the leak of the NTP receive timestamp */
|
||||
last_sample_time = inst->sample_times[i];
|
||||
last_sample_time.tv_nsec = 0;
|
||||
if (inst->ip_addr)
|
||||
last_sample_time.tv_nsec = 0;
|
||||
report->latest_meas_ago = UTI_DiffTimespecsToDouble(now, &last_sample_time);
|
||||
} else {
|
||||
report->latest_meas_ago = (uint32_t)-1;
|
||||
@@ -1003,6 +988,14 @@ SST_Samples(SST_Stats inst)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SST_GetMinSamples(SST_Stats inst)
|
||||
{
|
||||
return inst->min_samples;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SST_DoSourcestatsReport(SST_Stats inst, RPT_SourcestatsReport *report, struct timespec *now)
|
||||
{
|
||||
|
||||
@@ -69,7 +69,6 @@ extern void SST_GetFrequencyRange(SST_Stats inst, double *lo, double *hi);
|
||||
/* Get data needed for selection */
|
||||
extern void
|
||||
SST_GetSelectionData(SST_Stats inst, struct timespec *now,
|
||||
int *stratum,
|
||||
double *offset_lo_limit,
|
||||
double *offset_hi_limit,
|
||||
double *root_distance,
|
||||
@@ -103,6 +102,10 @@ SST_GetTrackingData(SST_Stats inst, struct timespec *ref_time,
|
||||
|
||||
extern void SST_SlewSamples(SST_Stats inst, struct timespec *when, double dfreq, double doffset);
|
||||
|
||||
/* This routine corrects already accumulated samples to improve the
|
||||
frequency estimate when a new sample is accumulated */
|
||||
extern void SST_CorrectOffset(SST_Stats inst, double doffset);
|
||||
|
||||
/* This routine is called when an indeterminate offset is introduced
|
||||
into the local time. */
|
||||
extern void SST_AddDispersion(SST_Stats inst, double dispersion);
|
||||
@@ -120,7 +123,7 @@ extern int SST_GetDelayTestData(SST_Stats inst, struct timespec *sample_time,
|
||||
double *last_sample_ago, double *predicted_offset,
|
||||
double *min_delay, double *skew, double *std_dev);
|
||||
|
||||
extern void SST_SaveToFile(SST_Stats inst, FILE *out);
|
||||
extern int SST_SaveToFile(SST_Stats inst, FILE *out);
|
||||
|
||||
extern int SST_LoadFromFile(SST_Stats inst, FILE *in);
|
||||
|
||||
@@ -130,6 +133,8 @@ extern void SST_DoSourcestatsReport(SST_Stats inst, RPT_SourcestatsReport *repor
|
||||
|
||||
extern int SST_Samples(SST_Stats inst);
|
||||
|
||||
extern int SST_GetMinSamples(SST_Stats inst);
|
||||
|
||||
extern double SST_GetJitterAsymmetry(SST_Stats inst);
|
||||
|
||||
#endif /* GOT_SOURCESTATS_H */
|
||||
|
||||
@@ -54,10 +54,14 @@ typedef struct {
|
||||
int sel_options;
|
||||
int nts;
|
||||
int nts_port;
|
||||
int copy;
|
||||
int ext_fields;
|
||||
uint32_t authkey;
|
||||
uint32_t cert_set;
|
||||
double max_delay;
|
||||
double max_delay_ratio;
|
||||
double max_delay_dev_ratio;
|
||||
double max_delay_quant;
|
||||
double min_delay;
|
||||
double asymmetry;
|
||||
double offset;
|
||||
@@ -77,6 +81,7 @@ typedef struct {
|
||||
#define SRC_DEFAULT_MAXSAMPLES (-1)
|
||||
#define SRC_DEFAULT_ASYMMETRY 1.0
|
||||
#define SRC_DEFAULT_NTSPORT 4460
|
||||
#define SRC_DEFAULT_CERTSET 0
|
||||
#define INACTIVE_AUTHKEY 0
|
||||
|
||||
/* Flags for source selection */
|
||||
|
||||
3
stubs.c
3
stubs.c
@@ -491,7 +491,8 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
||||
}
|
||||
|
||||
NNC_Instance
|
||||
NNC_CreateInstance(IPSockAddr *nts_address, const char *name, const IPSockAddr *ntp_address)
|
||||
NNC_CreateInstance(IPSockAddr *nts_address, const char *name, uint32_t cert_set,
|
||||
uint16_t ntp_port)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -73,13 +73,28 @@ static double slew_freq;
|
||||
/* Time (raw) of last update of slewing frequency and offset */
|
||||
static struct timespec slew_start;
|
||||
|
||||
/* Limits for the slew timeout */
|
||||
#define MIN_SLEW_TIMEOUT 1.0
|
||||
#define MAX_SLEW_TIMEOUT 1.0e4
|
||||
/* Limits for the slew length */
|
||||
#define MIN_SLEW_DURATION 1.0
|
||||
#define MAX_SLEW_DURATION 1.0e4
|
||||
|
||||
/* Scheduler timeout ID for ending of the currently running slew */
|
||||
static SCH_TimeoutID slew_timeout_id;
|
||||
|
||||
/* Scheduled duration of the currently running slew */
|
||||
static double slew_duration;
|
||||
|
||||
/* Expected delay in ending of the slew due to process scheduling and
|
||||
execution time, tracked as a decaying maximum value */
|
||||
static double slew_excess_duration;
|
||||
|
||||
/* Maximum accepted excess duration to ignore large jumps after resuming
|
||||
suspended system and other reasons (which should be handled in the
|
||||
scheduler), a constant to determine the minimum slew duration to avoid
|
||||
oscillations due to the excess, and the decay constant */
|
||||
#define MAX_SLEW_EXCESS_DURATION 100.0
|
||||
#define MIN_SLEW_DURATION_EXCESS_RATIO 5.0
|
||||
#define SLEW_EXCESS_DURATION_DECAY 0.9
|
||||
|
||||
/* Suggested offset correction rate (correction time * offset) */
|
||||
static double correction_rate;
|
||||
|
||||
@@ -109,12 +124,7 @@ static void
|
||||
handle_step(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
double doffset, LCL_ChangeType change_type, void *anything)
|
||||
{
|
||||
if (change_type == LCL_ChangeUnknownStep) {
|
||||
/* Reset offset and slewing */
|
||||
slew_start = *raw;
|
||||
offset_register = 0.0;
|
||||
update_slew();
|
||||
} else if (change_type == LCL_ChangeStep) {
|
||||
if (change_type == LCL_ChangeStep) {
|
||||
UTI_AddDoubleToTimespec(&slew_start, -doffset, &slew_start);
|
||||
}
|
||||
}
|
||||
@@ -169,8 +179,8 @@ clamp_freq(double freq)
|
||||
static void
|
||||
update_slew(void)
|
||||
{
|
||||
double old_slew_freq, total_freq, corr_freq, duration, excess_duration;
|
||||
struct timespec now, end_of_slew;
|
||||
double old_slew_freq, total_freq, corr_freq, duration;
|
||||
|
||||
/* Remove currently running timeout */
|
||||
SCH_RemoveTimeout(slew_timeout_id);
|
||||
@@ -183,13 +193,25 @@ update_slew(void)
|
||||
|
||||
stop_fastslew(&now);
|
||||
|
||||
/* Estimate how long should the next slew take */
|
||||
/* Update the maximum excess duration, decaying even when the slew did
|
||||
not time out (i.e. frequency was set or offset accrued), but add a small
|
||||
value to avoid denormals */
|
||||
slew_excess_duration = (slew_excess_duration + 1.0e-9) * SLEW_EXCESS_DURATION_DECAY;
|
||||
excess_duration = duration - slew_duration;
|
||||
if (slew_excess_duration < excess_duration &&
|
||||
excess_duration <= MAX_SLEW_EXCESS_DURATION)
|
||||
slew_excess_duration = excess_duration;
|
||||
|
||||
/* Calculate the duration of the new slew, considering the current correction
|
||||
rate and previous delays in stopping of the slew */
|
||||
if (fabs(offset_register) < MIN_OFFSET_CORRECTION) {
|
||||
duration = MAX_SLEW_TIMEOUT;
|
||||
duration = MAX_SLEW_DURATION;
|
||||
} else {
|
||||
duration = correction_rate / fabs(offset_register);
|
||||
if (duration < MIN_SLEW_TIMEOUT)
|
||||
duration = MIN_SLEW_TIMEOUT;
|
||||
if (duration < MIN_SLEW_DURATION)
|
||||
duration = MIN_SLEW_DURATION;
|
||||
if (duration < MIN_SLEW_DURATION_EXCESS_RATIO * slew_excess_duration)
|
||||
duration = MIN_SLEW_DURATION_EXCESS_RATIO * slew_excess_duration;
|
||||
}
|
||||
|
||||
/* Get frequency offset needed to slew the offset in the duration
|
||||
@@ -232,23 +254,25 @@ update_slew(void)
|
||||
maximum timeout and try again on the next update. */
|
||||
if (fabs(offset_register) < MIN_OFFSET_CORRECTION ||
|
||||
offset_register * slew_freq <= 0.0) {
|
||||
duration = MAX_SLEW_TIMEOUT;
|
||||
duration = MAX_SLEW_DURATION;
|
||||
} else {
|
||||
duration = offset_register / slew_freq;
|
||||
if (duration < MIN_SLEW_TIMEOUT)
|
||||
duration = MIN_SLEW_TIMEOUT;
|
||||
else if (duration > MAX_SLEW_TIMEOUT)
|
||||
duration = MAX_SLEW_TIMEOUT;
|
||||
if (duration < MIN_SLEW_DURATION)
|
||||
duration = MIN_SLEW_DURATION;
|
||||
else if (duration > MAX_SLEW_DURATION)
|
||||
duration = MAX_SLEW_DURATION;
|
||||
}
|
||||
|
||||
/* Restart timer for the next update */
|
||||
UTI_AddDoubleToTimespec(&now, duration, &end_of_slew);
|
||||
slew_timeout_id = SCH_AddTimeout(&end_of_slew, handle_end_of_slew, NULL);
|
||||
slew_start = now;
|
||||
slew_duration = duration;
|
||||
|
||||
DEBUG_LOG("slew offset=%e corr_rate=%e base_freq=%f total_freq=%f slew_freq=%e duration=%f slew_error=%e",
|
||||
offset_register, correction_rate, base_freq, total_freq, slew_freq,
|
||||
duration, slew_error);
|
||||
DEBUG_LOG("slew offset=%e corr_rate=%e base_freq=%f total_freq=%f slew_freq=%e"
|
||||
" duration=%f excess=%f slew_error=%e",
|
||||
offset_register, correction_rate, base_freq, total_freq, slew_freq,
|
||||
slew_duration, slew_excess_duration, slew_error);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -385,6 +409,7 @@ SYS_Generic_CompleteFreqDriver(double max_set_freq_ppm, double max_set_freq_dela
|
||||
base_freq = (*drv_read_freq)();
|
||||
slew_freq = 0.0;
|
||||
offset_register = 0.0;
|
||||
slew_excess_duration = 0.0;
|
||||
|
||||
max_corr_freq = CNF_GetMaxSlewRate() / 1.0e6;
|
||||
|
||||
|
||||
246
sys_linux.c
246
sys_linux.c
@@ -35,6 +35,7 @@
|
||||
|
||||
#if defined(FEAT_PHC) || defined(HAVE_LINUX_TIMESTAMPING)
|
||||
#include <linux/ptp_clock.h>
|
||||
#include <poll.h>
|
||||
#endif
|
||||
|
||||
#ifdef FEAT_SCFILTER
|
||||
@@ -97,21 +98,6 @@ static int have_setoffset;
|
||||
updated in the kernel */
|
||||
static int tick_update_hz;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
inline static long
|
||||
our_round(double x)
|
||||
{
|
||||
long y;
|
||||
|
||||
if (x > 0.0)
|
||||
y = x + 0.5;
|
||||
else
|
||||
y = x - 0.5;
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Positive means currently fast of true time, i.e. jump backwards */
|
||||
|
||||
@@ -149,7 +135,7 @@ set_frequency(double freq_ppm)
|
||||
double required_freq;
|
||||
int required_delta_tick;
|
||||
|
||||
required_delta_tick = our_round(freq_ppm / dhz);
|
||||
required_delta_tick = round(freq_ppm / dhz);
|
||||
|
||||
/* Older kernels (pre-2.6.18) don't apply the frequency offset exactly as
|
||||
set by adjtimex() and a scaling constant (that depends on the internal
|
||||
@@ -486,7 +472,7 @@ void check_seccomp_applicability(void)
|
||||
void
|
||||
SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
{
|
||||
const int syscalls[] = {
|
||||
const int allowed[] = {
|
||||
/* Clock */
|
||||
SCMP_SYS(adjtimex),
|
||||
SCMP_SYS(clock_adjtime),
|
||||
@@ -503,11 +489,18 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
|
||||
/* Process */
|
||||
SCMP_SYS(clone),
|
||||
#ifdef __NR_clone3
|
||||
SCMP_SYS(clone3),
|
||||
#endif
|
||||
SCMP_SYS(exit),
|
||||
SCMP_SYS(exit_group),
|
||||
SCMP_SYS(getpid),
|
||||
SCMP_SYS(getrlimit),
|
||||
SCMP_SYS(getuid),
|
||||
SCMP_SYS(getuid32),
|
||||
#ifdef __NR_rseq
|
||||
SCMP_SYS(rseq),
|
||||
#endif
|
||||
SCMP_SYS(rt_sigaction),
|
||||
SCMP_SYS(rt_sigreturn),
|
||||
SCMP_SYS(rt_sigprocmask),
|
||||
@@ -537,6 +530,7 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
SCMP_SYS(fchownat),
|
||||
SCMP_SYS(fstat),
|
||||
SCMP_SYS(fstat64),
|
||||
SCMP_SYS(fstatat64),
|
||||
SCMP_SYS(getdents),
|
||||
SCMP_SYS(getdents64),
|
||||
SCMP_SYS(lseek),
|
||||
@@ -547,11 +541,16 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
SCMP_SYS(readlinkat),
|
||||
SCMP_SYS(rename),
|
||||
SCMP_SYS(renameat),
|
||||
#ifdef __NR_renameat2
|
||||
SCMP_SYS(renameat2),
|
||||
#endif
|
||||
SCMP_SYS(stat),
|
||||
SCMP_SYS(stat64),
|
||||
SCMP_SYS(statfs),
|
||||
SCMP_SYS(statfs64),
|
||||
#ifdef __NR_statx
|
||||
SCMP_SYS(statx),
|
||||
#endif
|
||||
SCMP_SYS(unlink),
|
||||
SCMP_SYS(unlinkat),
|
||||
|
||||
@@ -588,6 +587,7 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
#ifdef __NR_ppoll_time64
|
||||
SCMP_SYS(ppoll_time64),
|
||||
#endif
|
||||
SCMP_SYS(pread64),
|
||||
SCMP_SYS(pselect6),
|
||||
#ifdef __NR_pselect6_time64
|
||||
SCMP_SYS(pselect6_time64),
|
||||
@@ -607,6 +607,22 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
SCMP_SYS(uname),
|
||||
};
|
||||
|
||||
const int denied_any[] = {
|
||||
SCMP_SYS(execve),
|
||||
#ifdef __NR_execveat
|
||||
SCMP_SYS(execveat),
|
||||
#endif
|
||||
SCMP_SYS(fork),
|
||||
SCMP_SYS(ptrace),
|
||||
SCMP_SYS(vfork),
|
||||
};
|
||||
|
||||
const int denied_ntske[] = {
|
||||
SCMP_SYS(ioctl),
|
||||
SCMP_SYS(setsockopt),
|
||||
SCMP_SYS(socket),
|
||||
};
|
||||
|
||||
const int socket_domains[] = {
|
||||
AF_NETLINK, AF_UNIX, AF_INET,
|
||||
#ifdef FEAT_IPV6
|
||||
@@ -615,9 +631,15 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
};
|
||||
|
||||
const static int socket_options[][2] = {
|
||||
{ SOL_IP, IP_PKTINFO }, { SOL_IP, IP_FREEBIND },
|
||||
{ SOL_IP, IP_PKTINFO }, { SOL_IP, IP_FREEBIND }, { SOL_IP, IP_TOS },
|
||||
#ifdef FEAT_IPV6
|
||||
{ SOL_IPV6, IPV6_V6ONLY }, { SOL_IPV6, IPV6_RECVPKTINFO },
|
||||
#ifdef IPV6_TCLASS
|
||||
{ SOL_IPV6, IPV6_TCLASS },
|
||||
#endif
|
||||
#endif
|
||||
#ifdef SO_BINDTODEVICE
|
||||
{ SOL_SOCKET, SO_BINDTODEVICE },
|
||||
#endif
|
||||
{ SOL_SOCKET, SO_BROADCAST }, { SOL_SOCKET, SO_REUSEADDR },
|
||||
#ifdef SO_REUSEPORT
|
||||
@@ -656,31 +678,65 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
#endif
|
||||
};
|
||||
|
||||
unsigned int default_action, deny_action;
|
||||
scmp_filter_ctx *ctx;
|
||||
int i;
|
||||
|
||||
/* Sign of the level determines the deny action (kill or SIGSYS).
|
||||
At level 1, selected syscalls are allowed, others are denied.
|
||||
At level 2, selected syscalls are denied, others are allowed. */
|
||||
|
||||
deny_action = level > 0 ? SCMP_ACT_KILL : SCMP_ACT_TRAP;
|
||||
if (level < 0)
|
||||
level = -level;
|
||||
|
||||
switch (level) {
|
||||
case 1:
|
||||
default_action = deny_action;
|
||||
break;
|
||||
case 2:
|
||||
default_action = SCMP_ACT_ALLOW;
|
||||
break;
|
||||
default:
|
||||
LOG_FATAL("Unsupported filter level");
|
||||
}
|
||||
|
||||
if (context == SYS_MAIN_PROCESS) {
|
||||
/* Check if the chronyd configuration is supported */
|
||||
check_seccomp_applicability();
|
||||
|
||||
/* Start the helper process, which will run without any seccomp filter. It
|
||||
will be used for getaddrinfo(), for which it's difficult to maintain a
|
||||
list of required system calls (with glibc it depends on what NSS modules
|
||||
are installed and enabled on the system). */
|
||||
PRV_StartHelper();
|
||||
/* At level 1, start a helper process which will not have a seccomp filter.
|
||||
It will be used for getaddrinfo(), for which it is difficult to maintain
|
||||
a list of required system calls (with glibc it depends on what NSS
|
||||
modules are installed and enabled on the system). */
|
||||
if (default_action != SCMP_ACT_ALLOW)
|
||||
PRV_StartHelper();
|
||||
}
|
||||
|
||||
ctx = seccomp_init(level > 0 ? SCMP_ACT_KILL : SCMP_ACT_TRAP);
|
||||
ctx = seccomp_init(default_action);
|
||||
if (ctx == NULL)
|
||||
LOG_FATAL("Failed to initialize seccomp");
|
||||
|
||||
/* Add system calls that are always allowed */
|
||||
for (i = 0; i < (sizeof (syscalls) / sizeof (*syscalls)); i++) {
|
||||
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscalls[i], 0) < 0)
|
||||
goto add_failed;
|
||||
if (default_action != SCMP_ACT_ALLOW) {
|
||||
for (i = 0; i < sizeof (allowed) / sizeof (*allowed); i++) {
|
||||
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, allowed[i], 0) < 0)
|
||||
goto add_failed;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < sizeof (denied_any) / sizeof (*denied_any); i++) {
|
||||
if (seccomp_rule_add(ctx, deny_action, denied_any[i], 0) < 0)
|
||||
goto add_failed;
|
||||
}
|
||||
|
||||
if (context == SYS_NTSKE_HELPER) {
|
||||
for (i = 0; i < sizeof (denied_ntske) / sizeof (*denied_ntske); i++) {
|
||||
if (seccomp_rule_add(ctx, deny_action, denied_ntske[i], 0) < 0)
|
||||
goto add_failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (context == SYS_MAIN_PROCESS) {
|
||||
if (default_action != SCMP_ACT_ALLOW && context == SYS_MAIN_PROCESS) {
|
||||
/* Allow opening sockets in selected domains */
|
||||
for (i = 0; i < sizeof (socket_domains) / sizeof (*socket_domains); i++) {
|
||||
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
|
||||
@@ -690,10 +746,9 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
|
||||
/* Allow selected socket options */
|
||||
for (i = 0; i < sizeof (socket_options) / sizeof (*socket_options); i++) {
|
||||
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 3,
|
||||
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
|
||||
SCMP_A1(SCMP_CMP_EQ, socket_options[i][0]),
|
||||
SCMP_A2(SCMP_CMP_EQ, socket_options[i][1]),
|
||||
SCMP_A4(SCMP_CMP_LE, sizeof (int))) < 0)
|
||||
SCMP_A2(SCMP_CMP_EQ, socket_options[i][1])))
|
||||
goto add_failed;
|
||||
}
|
||||
|
||||
@@ -717,7 +772,8 @@ SYS_Linux_EnableSystemCallFilter(int level, SYS_ProcessContext context)
|
||||
if (seccomp_load(ctx) < 0)
|
||||
LOG_FATAL("Failed to load seccomp rules");
|
||||
|
||||
LOG(context == SYS_MAIN_PROCESS ? LOGS_INFO : LOGS_DEBUG, "Loaded seccomp filter");
|
||||
LOG(context == SYS_MAIN_PROCESS ? LOGS_INFO : LOGS_DEBUG,
|
||||
"Loaded seccomp filter (level %d)", level);
|
||||
seccomp_release(ctx);
|
||||
return;
|
||||
|
||||
@@ -742,73 +798,25 @@ SYS_Linux_CheckKernelVersion(int req_major, int req_minor)
|
||||
|
||||
#if defined(FEAT_PHC) || defined(HAVE_LINUX_TIMESTAMPING)
|
||||
|
||||
#define PHC_READINGS 10
|
||||
|
||||
static int
|
||||
process_phc_readings(struct timespec ts[][3], int n, double precision,
|
||||
struct timespec *phc_ts, struct timespec *sys_ts, double *err)
|
||||
get_phc_readings(int phc_fd, int max_samples, struct timespec ts[][3])
|
||||
{
|
||||
double min_delay = 0.0, delays[PTP_MAX_SAMPLES], phc_sum, sys_sum, sys_prec;
|
||||
int i, combined;
|
||||
|
||||
if (n > PTP_MAX_SAMPLES)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
delays[i] = UTI_DiffTimespecsToDouble(&ts[i][2], &ts[i][0]);
|
||||
|
||||
if (delays[i] < 0.0) {
|
||||
/* Step in the middle of a PHC reading? */
|
||||
DEBUG_LOG("Bad PTP_SYS_OFFSET sample delay=%e", delays[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!i || delays[i] < min_delay)
|
||||
min_delay = delays[i];
|
||||
}
|
||||
|
||||
sys_prec = LCL_GetSysPrecisionAsQuantum();
|
||||
|
||||
/* Combine best readings */
|
||||
for (i = combined = 0, phc_sum = sys_sum = 0.0; i < n; i++) {
|
||||
if (delays[i] > min_delay + MAX(sys_prec, precision))
|
||||
continue;
|
||||
|
||||
phc_sum += UTI_DiffTimespecsToDouble(&ts[i][1], &ts[0][1]);
|
||||
sys_sum += UTI_DiffTimespecsToDouble(&ts[i][0], &ts[0][0]) + delays[i] / 2.0;
|
||||
combined++;
|
||||
}
|
||||
|
||||
assert(combined);
|
||||
|
||||
UTI_AddDoubleToTimespec(&ts[0][1], phc_sum / combined, phc_ts);
|
||||
UTI_AddDoubleToTimespec(&ts[0][0], sys_sum / combined, sys_ts);
|
||||
*err = MAX(min_delay / 2.0, precision);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
struct timespec *sys_ts, double *err)
|
||||
{
|
||||
struct timespec ts[PHC_READINGS][3];
|
||||
struct ptp_sys_offset sys_off;
|
||||
int i;
|
||||
|
||||
max_samples = CLAMP(0, max_samples, PTP_MAX_SAMPLES);
|
||||
|
||||
/* Silence valgrind */
|
||||
memset(&sys_off, 0, sizeof (sys_off));
|
||||
|
||||
sys_off.n_samples = PHC_READINGS;
|
||||
sys_off.n_samples = max_samples;
|
||||
|
||||
if (ioctl(phc_fd, PTP_SYS_OFFSET, &sys_off)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "PTP_SYS_OFFSET", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < PHC_READINGS; i++) {
|
||||
for (i = 0; i < max_samples; i++) {
|
||||
ts[i][0].tv_sec = sys_off.ts[i * 2].sec;
|
||||
ts[i][0].tv_nsec = sys_off.ts[i * 2].nsec;
|
||||
ts[i][1].tv_sec = sys_off.ts[i * 2 + 1].sec;
|
||||
@@ -817,31 +825,31 @@ get_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
ts[i][2].tv_nsec = sys_off.ts[i * 2 + 2].nsec;
|
||||
}
|
||||
|
||||
return process_phc_readings(ts, PHC_READINGS, precision, phc_ts, sys_ts, err);
|
||||
return max_samples;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_extended_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
struct timespec *sys_ts, double *err)
|
||||
get_extended_phc_readings(int phc_fd, int max_samples, struct timespec ts[][3])
|
||||
{
|
||||
#ifdef PTP_SYS_OFFSET_EXTENDED
|
||||
struct timespec ts[PHC_READINGS][3];
|
||||
struct ptp_sys_offset_extended sys_off;
|
||||
int i;
|
||||
|
||||
max_samples = CLAMP(0, max_samples, PTP_MAX_SAMPLES);
|
||||
|
||||
/* Silence valgrind */
|
||||
memset(&sys_off, 0, sizeof (sys_off));
|
||||
|
||||
sys_off.n_samples = PHC_READINGS;
|
||||
sys_off.n_samples = max_samples;
|
||||
|
||||
if (ioctl(phc_fd, PTP_SYS_OFFSET_EXTENDED, &sys_off)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "PTP_SYS_OFFSET_EXTENDED", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < PHC_READINGS; i++) {
|
||||
for (i = 0; i < max_samples; i++) {
|
||||
ts[i][0].tv_sec = sys_off.ts[i][0].sec;
|
||||
ts[i][0].tv_nsec = sys_off.ts[i][0].nsec;
|
||||
ts[i][1].tv_sec = sys_off.ts[i][1].sec;
|
||||
@@ -850,7 +858,7 @@ get_extended_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
ts[i][2].tv_nsec = sys_off.ts[i][2].nsec;
|
||||
}
|
||||
|
||||
return process_phc_readings(ts, PHC_READINGS, precision, phc_ts, sys_ts, err);
|
||||
return max_samples;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
@@ -859,12 +867,14 @@ get_extended_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_precise_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
struct timespec *sys_ts, double *err)
|
||||
get_precise_phc_readings(int phc_fd, int max_samples, struct timespec ts[][3])
|
||||
{
|
||||
#ifdef PTP_SYS_OFFSET_PRECISE
|
||||
struct ptp_sys_offset_precise sys_off;
|
||||
|
||||
if (max_samples < 1)
|
||||
return 0;
|
||||
|
||||
/* Silence valgrind */
|
||||
memset(&sys_off, 0, sizeof (sys_off));
|
||||
|
||||
@@ -874,11 +884,11 @@ get_precise_phc_sample(int phc_fd, double precision, struct timespec *phc_ts,
|
||||
return 0;
|
||||
}
|
||||
|
||||
phc_ts->tv_sec = sys_off.device.sec;
|
||||
phc_ts->tv_nsec = sys_off.device.nsec;
|
||||
sys_ts->tv_sec = sys_off.sys_realtime.sec;
|
||||
sys_ts->tv_nsec = sys_off.sys_realtime.nsec;
|
||||
*err = MAX(LCL_GetSysPrecisionAsQuantum(), precision);
|
||||
ts[0][0].tv_sec = sys_off.sys_realtime.sec;
|
||||
ts[0][0].tv_nsec = sys_off.sys_realtime.nsec;
|
||||
ts[0][1].tv_sec = sys_off.device.sec;
|
||||
ts[0][1].tv_nsec = sys_off.device.nsec;
|
||||
ts[0][2] = ts[0][0];
|
||||
|
||||
return 1;
|
||||
#else
|
||||
@@ -922,23 +932,23 @@ SYS_Linux_OpenPHC(const char *path, int phc_index)
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SYS_Linux_GetPHCSample(int fd, int nocrossts, double precision, int *reading_mode,
|
||||
struct timespec *phc_ts, struct timespec *sys_ts, double *err)
|
||||
SYS_Linux_GetPHCReadings(int fd, int nocrossts, int *reading_mode, int max_readings,
|
||||
struct timespec tss[][3])
|
||||
{
|
||||
if ((*reading_mode == 2 || !*reading_mode) && !nocrossts &&
|
||||
get_precise_phc_sample(fd, precision, phc_ts, sys_ts, err)) {
|
||||
int r = 0;
|
||||
|
||||
if ((*reading_mode == 2 || *reading_mode == 0) && !nocrossts &&
|
||||
(r = get_precise_phc_readings(fd, max_readings, tss)) > 0) {
|
||||
*reading_mode = 2;
|
||||
return 1;
|
||||
} else if ((*reading_mode == 3 || !*reading_mode) &&
|
||||
get_extended_phc_sample(fd, precision, phc_ts, sys_ts, err)) {
|
||||
} else if ((*reading_mode == 3 || *reading_mode == 0) &&
|
||||
(r = get_extended_phc_readings(fd, max_readings, tss)) > 0) {
|
||||
*reading_mode = 3;
|
||||
return 1;
|
||||
} else if ((*reading_mode == 1 || !*reading_mode) &&
|
||||
get_phc_sample(fd, precision, phc_ts, sys_ts, err)) {
|
||||
} else if ((*reading_mode == 1 || *reading_mode == 0) &&
|
||||
(r = get_phc_readings(fd, max_readings, tss)) > 0) {
|
||||
*reading_mode = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -956,7 +966,7 @@ SYS_Linux_SetPHCExtTimestamping(int fd, int pin, int channel,
|
||||
pin_desc.func = enable ? PTP_PF_EXTTS : PTP_PF_NONE;
|
||||
pin_desc.chan = channel;
|
||||
|
||||
if (ioctl(fd, PTP_PIN_SETFUNC, &pin_desc)) {
|
||||
if (pin >= 0 && ioctl(fd, PTP_PIN_SETFUNC, &pin_desc)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "PTP_PIN_SETFUNC", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
@@ -985,6 +995,16 @@ int
|
||||
SYS_Linux_ReadPHCExtTimestamp(int fd, struct timespec *phc_ts, int *channel)
|
||||
{
|
||||
struct ptp_extts_event extts_event;
|
||||
struct pollfd pfd;
|
||||
|
||||
/* Make sure the read will not block in case we have multiple
|
||||
descriptors of the same PHC (O_NONBLOCK does not work) */
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
if (poll(&pfd, 1, 0) != 1 || pfd.revents != POLLIN) {
|
||||
DEBUG_LOG("Missing PHC extts event");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (read(fd, &extts_event, sizeof (extts_event)) != sizeof (extts_event)) {
|
||||
DEBUG_LOG("Could not read PHC extts event");
|
||||
|
||||
@@ -41,8 +41,8 @@ extern int SYS_Linux_CheckKernelVersion(int req_major, int req_minor);
|
||||
|
||||
extern int SYS_Linux_OpenPHC(const char *path, int phc_index);
|
||||
|
||||
extern int SYS_Linux_GetPHCSample(int fd, int nocrossts, double precision, int *reading_mode,
|
||||
struct timespec *phc_ts, struct timespec *sys_ts, double *err);
|
||||
extern int SYS_Linux_GetPHCReadings(int fd, int nocrossts, int *reading_mode, int max_readings,
|
||||
struct timespec tss[][3]);
|
||||
|
||||
extern int SYS_Linux_SetPHCExtTimestamping(int fd, int pin, int channel,
|
||||
int rising, int falling, int enable);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user