mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 18:45:07 -05:00
Compare commits
783 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
d327cfea5a | ||
|
|
c94e7c72e7 | ||
|
|
f3aea33ad4 | ||
|
|
48709d9c4a | ||
|
|
4779adcb50 | ||
|
|
01e29ec685 | ||
|
|
e4cccc115d | ||
|
|
8e9716d5d4 | ||
|
|
a96d288027 | ||
|
|
545d2563ef | ||
|
|
1494ef1df3 | ||
|
|
698f270b5b | ||
|
|
f15f6a86b0 | ||
|
|
5d60d611ae | ||
|
|
6e71e902c8 | ||
|
|
473cb3c968 | ||
|
|
df43ebe9e0 | ||
|
|
642173e864 | ||
|
|
944cf6e318 | ||
|
|
a655eab34f | ||
|
|
f020d479e0 | ||
|
|
de752b28de | ||
|
|
f41d370e6a | ||
|
|
a97830d9d6 | ||
|
|
ea4fc47cda | ||
|
|
0e08ca7c89 | ||
|
|
068cd3c311 | ||
|
|
455b8e4b44 | ||
|
|
d9a363606b | ||
|
|
59ad433b6b | ||
|
|
35b3a42ed9 | ||
|
|
0639205617 | ||
|
|
3916c3366b | ||
|
|
f0a33e7b28 | ||
|
|
c9b8f8bc70 | ||
|
|
983b0723f6 | ||
|
|
02c38934ea | ||
|
|
c28c2cde43 | ||
|
|
349323dec7 | ||
|
|
ddfaf2e542 | ||
|
|
3177474ae8 | ||
|
|
cc535632d1 | ||
|
|
cb8ee57b9e | ||
|
|
c0b19b3fea | ||
|
|
8235da6885 | ||
|
|
f6625717cd | ||
|
|
fdfcabd79b | ||
|
|
2bb88b45c6 | ||
|
|
9820c22c1d | ||
|
|
bcd7bad467 | ||
|
|
83ea9fe284 | ||
|
|
c74d6e458d | ||
|
|
ff466439fc | ||
|
|
0fcdf4389b | ||
|
|
9cb9021c87 | ||
|
|
9c36236742 | ||
|
|
adebb027be | ||
|
|
7d3798d7cd | ||
|
|
b7c7c293e5 | ||
|
|
9ca250755f | ||
|
|
bd3b36865e | ||
|
|
538e1c5eb1 | ||
|
|
009f1a5ae8 | ||
|
|
4f1418abf9 | ||
|
|
79b348f075 | ||
|
|
9d88c028e2 | ||
|
|
51172b3510 | ||
|
|
892636036a | ||
|
|
4cf6b29397 | ||
|
|
571359b366 | ||
|
|
0f009e7718 | ||
|
|
24effd7340 | ||
|
|
5289fc5f80 | ||
|
|
ca49304bd6 | ||
|
|
b7fbac617d | ||
|
|
839e9aa4af | ||
|
|
c5ac15ad33 | ||
|
|
598cd10c34 | ||
|
|
1885729024 | ||
|
|
2127f63961 | ||
|
|
97a8b1e43b | ||
|
|
aeee1feda6 | ||
|
|
18d9243eb9 | ||
|
|
1aa4827b3b | ||
|
|
ed1077a788 | ||
|
|
356c475a6a | ||
|
|
9ac582fa35 | ||
|
|
8c75f44603 | ||
|
|
0a63ad95ce | ||
|
|
d274fe44da | ||
|
|
6d1cb58d8f | ||
|
|
784122d44f | ||
|
|
32fb8d41ca | ||
|
|
4993c35e11 | ||
|
|
6a5665ca58 | ||
|
|
e5cf006378 | ||
|
|
0e51552d2d | ||
|
|
cc007ad93b | ||
|
|
3096926547 | ||
|
|
d48f012809 | ||
|
|
def137bc80 | ||
|
|
3e0272e55f | ||
|
|
be503bbcf6 | ||
|
|
72bf3d26eb | ||
|
|
cc20ead3dc | ||
|
|
fd8fbcd090 | ||
|
|
77bd0f83fe | ||
|
|
32a82a38fd | ||
|
|
66e097e3e6 | ||
|
|
51d77d6cfc | ||
|
|
2bb0769516 | ||
|
|
58da0c0ad2 | ||
|
|
c10b66b579 | ||
|
|
55a90c3735 | ||
|
|
962afb9e7d | ||
|
|
7abd982f87 | ||
|
|
c099aac79c | ||
|
|
828e6ce30f | ||
|
|
dc08cbfe59 | ||
|
|
3bdcce6903 | ||
|
|
d93aa10bac | ||
|
|
de4ecc72d1 | ||
|
|
db54bfc0c1 | ||
|
|
72ee80debe | ||
|
|
a3436c26f0 | ||
|
|
b0f5024d56 | ||
|
|
eae4b2abe5 | ||
|
|
ff03b813b0 | ||
|
|
4e747da4b4 | ||
|
|
99e3c67a81 | ||
|
|
c4a2550518 | ||
|
|
4ef944b734 | ||
|
|
0f04baeb97 | ||
|
|
bf7f63eaed | ||
|
|
59cf4e0b96 | ||
|
|
3fc72c0cfa | ||
|
|
ad69f4f32b | ||
|
|
81c2b2e886 | ||
|
|
c9f03fb222 | ||
|
|
b0fe443632 | ||
|
|
8882fb21e0 | ||
|
|
7d551d34a0 | ||
|
|
feef0dd983 | ||
|
|
d29f7b7c70 | ||
|
|
e3cd248f0d | ||
|
|
27e20a568b | ||
|
|
80316de3b8 | ||
|
|
f9e2a5852d | ||
|
|
500c9cbf3b | ||
|
|
46714fec2d | ||
|
|
e1d9a57bd0 | ||
|
|
1b82604f61 | ||
|
|
d69ac07183 | ||
|
|
519796de37 | ||
|
|
ea4811b3b3 | ||
|
|
951f14ae06 | ||
|
|
428f9e4228 | ||
|
|
ea425bf01e | ||
|
|
8567a0e466 | ||
|
|
f6bf12bdcd | ||
|
|
e8968ea429 | ||
|
|
cf10ce1b68 | ||
|
|
15dc83420d | ||
|
|
37dbc211cd | ||
|
|
ed78cda6ad | ||
|
|
faff931a76 | ||
|
|
1e68671690 | ||
|
|
8eb167fd21 | ||
|
|
bc46174e98 | ||
|
|
b86c89460a | ||
|
|
03541f3626 | ||
|
|
39a462496a | ||
|
|
7ba8994838 | ||
|
|
8da025da99 | ||
|
|
5dc7242703 | ||
|
|
11bffa0d55 | ||
|
|
5f6f265f80 | ||
|
|
bf92314dc4 | ||
|
|
a3fda9f992 | ||
|
|
cd34b377aa | ||
|
|
145423068b | ||
|
|
fb4c3f31c0 | ||
|
|
60049f1551 | ||
|
|
e555548dda | ||
|
|
eedf61b3a2 | ||
|
|
ab54f76a38 | ||
|
|
f8df4789b1 | ||
|
|
6366ebc17e | ||
|
|
3a2d33d5a3 | ||
|
|
1afd5b23d7 | ||
|
|
17fb9e3709 | ||
|
|
7a7295992f | ||
|
|
526974366f | ||
|
|
51fe589aeb | ||
|
|
28cf4acf13 | ||
|
|
ee2220f2e7 | ||
|
|
a6ec6ec3ac | ||
|
|
4f5343f086 | ||
|
|
79c7384e5e | ||
|
|
75beeaf2b0 | ||
|
|
f4ed2abdca | ||
|
|
11a5c7337a | ||
|
|
972c476c5a | ||
|
|
a8c8f2f309 | ||
|
|
a0d2513be6 | ||
|
|
43dc0b3295 | ||
|
|
195ff5c51b | ||
|
|
e49aececce | ||
|
|
814b07c3a2 | ||
|
|
3470ab66f0 | ||
|
|
6901df5c18 | ||
|
|
bddb3b3228 | ||
|
|
dfe877144a | ||
|
|
59a9b7a9f6 | ||
|
|
ad8fb64276 | ||
|
|
436c1d3ea2 | ||
|
|
7fc5da5f80 | ||
|
|
105b3faa46 | ||
|
|
709223826f | ||
|
|
eace93f2af | ||
|
|
2775846db7 | ||
|
|
4aff08e95d | ||
|
|
958d66f8a7 | ||
|
|
85fa29c43d | ||
|
|
0344b9a9c9 | ||
|
|
04f6329773 | ||
|
|
d690faeb19 | ||
|
|
0b2e77ae64 | ||
|
|
2a4fd0a5c6 | ||
|
|
e569e1c9d9 | ||
|
|
7be360041c | ||
|
|
2fa83b541c | ||
|
|
8db9d59dac | ||
|
|
adcf073484 | ||
|
|
5296858411 | ||
|
|
d603426389 | ||
|
|
d3f4292968 | ||
|
|
4dde7198c8 | ||
|
|
b145d3ff51 | ||
|
|
9b98247d9c | ||
|
|
eedabb3d27 | ||
|
|
66dc2b6d6b | ||
|
|
bcdbbbd694 | ||
|
|
7b07e47c08 | ||
|
|
a608496faf | ||
|
|
c687224a11 | ||
|
|
a6f2a613f3 | ||
|
|
cfa39af345 | ||
|
|
8bab35c122 | ||
|
|
b20ef4cd7f | ||
|
|
b8b751a932 | ||
|
|
4a390841eb | ||
|
|
f506f44033 | ||
|
|
1f8355f154 | ||
|
|
ddc2761498 | ||
|
|
8b50a8298a | ||
|
|
3eab329042 | ||
|
|
552d3b53b1 | ||
|
|
8afd62d954 | ||
|
|
4883086fc1 | ||
|
|
2582be8754 | ||
|
|
ff9301567e | ||
|
|
e7a254265f | ||
|
|
d5311adafb | ||
|
|
18d7ea62b3 | ||
|
|
fb2849b230 | ||
|
|
fd59877692 | ||
|
|
bb0553e4c4 | ||
|
|
46f954490d | ||
|
|
31e6a50386 | ||
|
|
9a9c0d7b99 | ||
|
|
0c80f00d0b | ||
|
|
27b3bf48ea | ||
|
|
c3e34b8145 | ||
|
|
7bf3ec4aeb | ||
|
|
50204a125b | ||
|
|
111d170542 | ||
|
|
d6dd6f0bc9 | ||
|
|
44aac84feb | ||
|
|
c41508723f | ||
|
|
6043632f80 | ||
|
|
a420ed57a1 | ||
|
|
72f99033fe | ||
|
|
2d798bc4cf | ||
|
|
881d07fa0a | ||
|
|
c5306bed39 | ||
|
|
934b8712a5 | ||
|
|
1d4690eb64 | ||
|
|
e6848b1e3f | ||
|
|
3e537416a9 | ||
|
|
26a1ed8bc3 | ||
|
|
24538fe3e9 | ||
|
|
e43d699973 | ||
|
|
1e727c4497 | ||
|
|
83010590af | ||
|
|
fa402a173a | ||
|
|
ca83d2a804 | ||
|
|
725beb360a | ||
|
|
86d29221f3 | ||
|
|
e8062b7ff1 | ||
|
|
9468fd4aa6 | ||
|
|
5ed9b888ff | ||
|
|
7661a6e95b | ||
|
|
aca1daf7c9 | ||
|
|
46cac4e22f | ||
|
|
56a102ed4d | ||
|
|
ca28dbd2c3 | ||
|
|
588785e160 | ||
|
|
cabcccd6c3 | ||
|
|
567e66a0bb | ||
|
|
b8ee6d6e56 | ||
|
|
9ea1e4e40f | ||
|
|
2d492eacb5 | ||
|
|
cb8660e79a | ||
|
|
d29bef93e9 | ||
|
|
5a09adebfd | ||
|
|
8c0ee9c175 | ||
|
|
f20fabdbf4 | ||
|
|
57cea56e6e | ||
|
|
db7d9639b4 | ||
|
|
beb40d63ed | ||
|
|
672b98dd3f | ||
|
|
a24d2713cd | ||
|
|
a5110d3ed9 | ||
|
|
9d1c1505b9 | ||
|
|
8c25632321 | ||
|
|
2507b66640 | ||
|
|
d7e3ad17ff | ||
|
|
84902d0e00 | ||
|
|
f7f3667bcb | ||
|
|
794cbfbbb5 | ||
|
|
1858104b5c | ||
|
|
994409a036 | ||
|
|
2d9eb5b6fa | ||
|
|
3477cbe28f | ||
|
|
8634158de0 | ||
|
|
3eaf0765b0 | ||
|
|
dd0679ad45 | ||
|
|
bfcd8ecc56 | ||
|
|
50e5865c73 | ||
|
|
93f6358916 | ||
|
|
9300854439 | ||
|
|
02914ac637 | ||
|
|
00fff161cf | ||
|
|
3763befd62 | ||
|
|
2ae008bcee | ||
|
|
ea41f7ab09 | ||
|
|
c673b1e8b7 | ||
|
|
2bf1ba22f2 | ||
|
|
dfc2f70876 | ||
|
|
0dba2b9689 | ||
|
|
e7fc2d31cc | ||
|
|
f231efb811 | ||
|
|
c4d6f98bed | ||
|
|
bff3f51d13 | ||
|
|
f5eb7daf20 | ||
|
|
d66b2f2b24 | ||
|
|
a57e1eb542 | ||
|
|
25bdee7a0e | ||
|
|
f6001202ec | ||
|
|
0cf506c929 | ||
|
|
d05e9fb2ec | ||
|
|
54d7e3e94a | ||
|
|
c7223f4c8f | ||
|
|
07badbede7 | ||
|
|
468cfeeb71 | ||
|
|
b3fc549622 | ||
|
|
077dbd5692 | ||
|
|
e18903a6b5 | ||
|
|
7dfd4ae556 | ||
|
|
429c4468b0 | ||
|
|
7a4c396bba | ||
|
|
88f846f656 | ||
|
|
27c8a64977 | ||
|
|
2fc8edacb8 | ||
|
|
903fa247f8 | ||
|
|
96771d6857 | ||
|
|
f4c6a00b2a | ||
|
|
990f8cd89b | ||
|
|
813ea71b50 | ||
|
|
e8be384cdf | ||
|
|
61773a2c07 | ||
|
|
510aa8b050 | ||
|
|
57957ab6cf | ||
|
|
e8069a0179 | ||
|
|
f3f840551a | ||
|
|
10a42c1e04 | ||
|
|
4a219ecbf1 | ||
|
|
0d298bfc4c | ||
|
|
792c241e3a | ||
|
|
6336a87855 | ||
|
|
f5721b1212 | ||
|
|
7d3e9180c6 | ||
|
|
03b8ca186a | ||
|
|
435cbef31a | ||
|
|
4adcf58368 | ||
|
|
004986310d | ||
|
|
7a88e0a87b | ||
|
|
64e21d6281 | ||
|
|
9ef7ea2bcb | ||
|
|
6d1796d6be | ||
|
|
fcaba98101 | ||
|
|
9bbda5c964 | ||
|
|
2c81d38861 | ||
|
|
78fec3f05a | ||
|
|
392a1a5ff6 | ||
|
|
219a414cb7 | ||
|
|
58fc81441b | ||
|
|
02ada36838 | ||
|
|
81978f0ba0 | ||
|
|
622769cdfd | ||
|
|
3038047f9b | ||
|
|
3e3f045ab7 | ||
|
|
a6d9f41eda | ||
|
|
bf6a4e1a81 | ||
|
|
5982d96b75 | ||
|
|
28e3e4cdca | ||
|
|
24134c78e8 | ||
|
|
5e8ed72b89 | ||
|
|
45e41b7ac1 | ||
|
|
27fd751915 | ||
|
|
4d26cfc92b | ||
|
|
d78680912e | ||
|
|
47e4cb31b2 | ||
|
|
91da65a782 | ||
|
|
bb1c02e9f5 | ||
|
|
c651ea9b6b | ||
|
|
207f9fb128 | ||
|
|
f06c1cfa97 | ||
|
|
6cd47bff8f | ||
|
|
2de24cfd82 | ||
|
|
86a3ef9ed1 | ||
|
|
3f8c57c8f2 | ||
|
|
ca96946416 | ||
|
|
e5b9b6d701 | ||
|
|
8cb689a5e6 | ||
|
|
2270234115 | ||
|
|
a073f383e6 | ||
|
|
8e74655b03 | ||
|
|
70fa3a6905 | ||
|
|
1227873b88 | ||
|
|
d30e73d0d9 | ||
|
|
9e7a7008de | ||
|
|
62d6aed6a6 | ||
|
|
ffb9887cce | ||
|
|
9220c9b8a2 | ||
|
|
2e28b19112 | ||
|
|
636a4e2794 | ||
|
|
5c9e1e0b69 | ||
|
|
64fd1b8ba5 | ||
|
|
69d3913f3e | ||
|
|
08fd011b6a | ||
|
|
c172268cfe | ||
|
|
94b014865c | ||
|
|
099aaf2cb1 | ||
|
|
4481a8b24f | ||
|
|
b626fe661e | ||
|
|
ba8fcd145d | ||
|
|
981d09de40 | ||
|
|
86a99bb257 | ||
|
|
3093a11cd0 | ||
|
|
058b788d38 | ||
|
|
66a42fa493 | ||
|
|
a85f63cc15 | ||
|
|
bbe1e69dcc | ||
|
|
1b52bba7b9 | ||
|
|
c5c80ef400 | ||
|
|
a78031ce0d | ||
|
|
34e9dd13ce | ||
|
|
6e52a9be7a | ||
|
|
69c6dffd63 | ||
|
|
2ddd0ae231 | ||
|
|
79db0b7eca | ||
|
|
2ebba7fbaa | ||
|
|
e392d1fde9 | ||
|
|
d7c93ec950 | ||
|
|
6af39d63aa | ||
|
|
cc8414b1b3 | ||
|
|
6b44055e3d | ||
|
|
9f9c6cc6ab | ||
|
|
f176193d35 | ||
|
|
e8bc41e862 | ||
|
|
91dbe3c6c2 | ||
|
|
3e876d4218 | ||
|
|
31b1f2e8a1 | ||
|
|
4169e94b1d | ||
|
|
948ecf8431 | ||
|
|
91f3f97ea7 | ||
|
|
65bb65b440 | ||
|
|
ea6e8d85a3 | ||
|
|
add932501f | ||
|
|
89390a738f | ||
|
|
ac4f6ab93b | ||
|
|
dbcb1b9b0b | ||
|
|
6375307798 | ||
|
|
fb78e60d26 | ||
|
|
b822c7164f | ||
|
|
aa295730a0 | ||
|
|
69d70703b2 | ||
|
|
b2b6ef00dc | ||
|
|
5dc86c236b | ||
|
|
2563dd9d29 | ||
|
|
a899e3df33 | ||
|
|
692cea49f8 | ||
|
|
bcedacaa3d | ||
|
|
be3c1b5243 | ||
|
|
e626ec6c37 | ||
|
|
49d52b547f | ||
|
|
74a546a9e7 | ||
|
|
d1777087c1 | ||
|
|
cf7b5363cd | ||
|
|
7f3183cc72 | ||
|
|
f1b8da085b | ||
|
|
09dfca49ec | ||
|
|
88e0ec07aa | ||
|
|
0adc8e8f92 | ||
|
|
5fc7674e36 | ||
|
|
018977044a | ||
|
|
cc49d8e6e6 | ||
|
|
933bd017b4 | ||
|
|
d558b33d85 | ||
|
|
9268bf2cff | ||
|
|
dbf2c22467 | ||
|
|
af4fe92095 | ||
|
|
e034a07be8 | ||
|
|
eb8c9ad601 | ||
|
|
6847536669 | ||
|
|
f5206db9b0 | ||
|
|
6ab2ed0da6 | ||
|
|
7352e470e1 | ||
|
|
5bc9c0d07a | ||
|
|
a2146e82ef | ||
|
|
6e10e6740c | ||
|
|
bfaa10f2b0 | ||
|
|
9f167a7997 | ||
|
|
6908163464 | ||
|
|
671daf06b8 | ||
|
|
b189a5386b | ||
|
|
7889d108c2 | ||
|
|
3cfa8ce9d3 | ||
|
|
570573fe28 | ||
|
|
62b1a11736 | ||
|
|
c00d517e12 | ||
|
|
001f3d5e27 | ||
|
|
6045023a49 | ||
|
|
bba29a0ee7 | ||
|
|
cffc856b50 | ||
|
|
419077e04b | ||
|
|
7db9d4acea | ||
|
|
8d5b86efe7 | ||
|
|
6cf16aea7b | ||
|
|
870545d3cb | ||
|
|
2a030c0d0c | ||
|
|
0b709ab1bc | ||
|
|
a1f2f17385 | ||
|
|
2240eefbd0 | ||
|
|
706d0c281a | ||
|
|
ca73e34f30 | ||
|
|
cca2ef4649 | ||
|
|
05d9edbf8f | ||
|
|
c5bdc52a59 | ||
|
|
74f0c0924a | ||
|
|
05492d1d23 | ||
|
|
eea343b93f | ||
|
|
afff06c88c | ||
|
|
c0717a27f6 | ||
|
|
159bd73f76 | ||
|
|
9931a9166b | ||
|
|
8aa4ae027b | ||
|
|
dcce79fdbe | ||
|
|
189aafde9d | ||
|
|
99e3045df4 | ||
|
|
c498c21fad | ||
|
|
6bef8aa0e9 | ||
|
|
108d112272 | ||
|
|
05078e4252 | ||
|
|
4ceb9e4cd0 | ||
|
|
a9f237a395 | ||
|
|
e7ca560c3d | ||
|
|
d9f86f6f70 | ||
|
|
879d936277 | ||
|
|
5bb2bf9361 | ||
|
|
a8167b7959 | ||
|
|
b33b682356 | ||
|
|
2c47602c33 | ||
|
|
59d1b41716 | ||
|
|
5b75d4afef | ||
|
|
e15c7cd236 | ||
|
|
9bc774d6af | ||
|
|
9b34556952 | ||
|
|
9a6369d8f1 | ||
|
|
49cdd6bf09 | ||
|
|
63fe34e890 | ||
|
|
85465afb62 | ||
|
|
339cb06a49 | ||
|
|
10150bfcab | ||
|
|
e50dc739d8 | ||
|
|
26e08abe71 | ||
|
|
7637faa0d0 | ||
|
|
8a57a28177 | ||
|
|
34db671b57 | ||
|
|
8b9021bf34 | ||
|
|
ce6b896948 | ||
|
|
2962fc6286 | ||
|
|
76bed76289 | ||
|
|
113f2ebec0 | ||
|
|
7c5bd948bb | ||
|
|
8cbc68f28f | ||
|
|
bf7aa52394 | ||
|
|
366345790d | ||
|
|
f881c153bf | ||
|
|
19f3ab2225 | ||
|
|
fd1e80802f | ||
|
|
4b7cb161a8 | ||
|
|
7848794222 | ||
|
|
94822d5156 | ||
|
|
e3f840aae9 | ||
|
|
5aae563277 | ||
|
|
02de782fa3 | ||
|
|
3f6df33feb | ||
|
|
a94f5fe007 | ||
|
|
63f0234748 | ||
|
|
47921c7c0c | ||
|
|
42a85f685e | ||
|
|
feca2399e4 | ||
|
|
d34e611ec8 | ||
|
|
02098ed830 | ||
|
|
aa4228bf1b | ||
|
|
b296441708 | ||
|
|
b827475378 | ||
|
|
78a6698ae1 | ||
|
|
e7b6feb34b | ||
|
|
84be834385 | ||
|
|
e83d808dfd | ||
|
|
35a68d5b59 | ||
|
|
3c593137b0 | ||
|
|
deaf0ffed3 | ||
|
|
af145e871e | ||
|
|
fbca570d0b | ||
|
|
448ef779c2 | ||
|
|
499a69e611 | ||
|
|
58c2915878 | ||
|
|
eda4b111d3 | ||
|
|
c6dd749687 | ||
|
|
d2a96f5fbc | ||
|
|
499f513d40 | ||
|
|
8b1f68b1b4 | ||
|
|
8e4c776900 | ||
|
|
d0eb9427c2 | ||
|
|
7d100b89fc | ||
|
|
a4bd7f1800 | ||
|
|
5308e0a25f | ||
|
|
da862158bf | ||
|
|
7b98443a13 | ||
|
|
4da9f74d24 | ||
|
|
e41042e258 | ||
|
|
5581466c63 | ||
|
|
e79a6c2116 | ||
|
|
666ece122e | ||
|
|
2c7ab98370 | ||
|
|
f0f18a02a7 | ||
|
|
c5d8af0285 | ||
|
|
0ce15a8472 | ||
|
|
da60629201 | ||
|
|
2343e7a89c | ||
|
|
45f27f4f5e | ||
|
|
0bc112f8b4 | ||
|
|
bfc2fa645c | ||
|
|
11111804fd | ||
|
|
87ec67247e | ||
|
|
0df8328ceb | ||
|
|
b563048ee2 | ||
|
|
e8096330be | ||
|
|
b1647dbcb7 | ||
|
|
4ddadd5622 | ||
|
|
3e854006c7 | ||
|
|
2c4c235147 | ||
|
|
6863e43269 | ||
|
|
de8708f331 | ||
|
|
d0b2486036 | ||
|
|
5384a93645 | ||
|
|
4bbc768652 | ||
|
|
fead915b45 | ||
|
|
5422e49026 | ||
|
|
77a1f27a1d | ||
|
|
b45d864f73 | ||
|
|
f35c81c871 | ||
|
|
a349b2803c | ||
|
|
f5d1b8fb74 | ||
|
|
a0fe71eef1 | ||
|
|
154b39cf7a | ||
|
|
6f54210db2 | ||
|
|
f6539449c5 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,9 +1,12 @@
|
||||
.deps
|
||||
.vimrc
|
||||
*.gcda
|
||||
*.gcno
|
||||
*.o
|
||||
*.swp
|
||||
*.dSYM
|
||||
*.DS_Store
|
||||
core.*
|
||||
tags
|
||||
/RELEASES
|
||||
/Makefile
|
||||
|
||||
47
Makefile.in
47
Makefile.in
@@ -21,44 +21,42 @@
|
||||
#
|
||||
# Makefile template
|
||||
|
||||
SYSCONFDIR=@SYSCONFDIR@
|
||||
BINDIR=@BINDIR@
|
||||
SBINDIR=@SBINDIR@
|
||||
LOCALSTATEDIR=@LOCALSTATEDIR@
|
||||
CHRONYVARDIR=@CHRONYVARDIR@
|
||||
SYSCONFDIR = @SYSCONFDIR@
|
||||
BINDIR = @BINDIR@
|
||||
SBINDIR = @SBINDIR@
|
||||
LOCALSTATEDIR = @LOCALSTATEDIR@
|
||||
CHRONYVARDIR = @CHRONYVARDIR@
|
||||
DESTDIR =
|
||||
|
||||
CC = @CC@
|
||||
CFLAGS = @CFLAGS@
|
||||
CPPFLAGS = @CPPFLAGS@
|
||||
LDFLAGS = @LDFLAGS@
|
||||
|
||||
DESTDIR=
|
||||
|
||||
HASH_OBJ = @HASH_OBJ@
|
||||
EXTRA_OBJS = @EXTRA_OBJS@
|
||||
|
||||
OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o \
|
||||
reference.o regress.o rtc.o sched.o sources.o sourcestats.o stubs.o \
|
||||
smooth.o sys.o sys_null.o tempcomp.o util.o $(HASH_OBJ)
|
||||
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)
|
||||
|
||||
EXTRA_OBJS=@EXTRA_OBJECTS@
|
||||
EXTRA_CLI_OBJS = @EXTRA_CLI_OBJS@
|
||||
|
||||
CLI_OBJS = array.o client.o cmdparse.o getdate.o memory.o nameserv.o \
|
||||
pktlength.o util.o $(HASH_OBJ)
|
||||
pktlength.o socket.o util.o $(EXTRA_CLI_OBJS)
|
||||
|
||||
ALL_OBJS = $(OBJS) $(EXTRA_OBJS) $(CLI_OBJS)
|
||||
ALL_OBJS = $(OBJS) $(CLI_OBJS)
|
||||
|
||||
LDFLAGS = @LDFLAGS@
|
||||
LIBS = @LIBS@
|
||||
|
||||
EXTRA_LIBS=@EXTRA_LIBS@
|
||||
EXTRA_CLI_LIBS=@EXTRA_CLI_LIBS@
|
||||
EXTRA_LIBS = @EXTRA_LIBS@
|
||||
EXTRA_CLI_LIBS = @EXTRA_CLI_LIBS@
|
||||
|
||||
# Until we have a main procedure we can link, just build object files
|
||||
# to test compilation
|
||||
|
||||
all : chronyd chronyc
|
||||
|
||||
chronyd : $(OBJS) $(EXTRA_OBJS)
|
||||
$(CC) $(CFLAGS) -o chronyd $(OBJS) $(EXTRA_OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_LIBS)
|
||||
chronyd : $(OBJS)
|
||||
$(CC) $(CFLAGS) -o chronyd $(OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_LIBS)
|
||||
|
||||
chronyc : $(CLI_OBJS)
|
||||
$(CC) $(CFLAGS) -o chronyc $(CLI_OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_CLI_LIBS)
|
||||
@@ -70,7 +68,9 @@ distclean : clean
|
||||
-rm -f Makefile config.h config.log
|
||||
|
||||
clean :
|
||||
-rm -f *.o *.s chronyc chronyd core *~
|
||||
$(MAKE) -C test/unit clean
|
||||
-rm -f *.o *.s chronyc chronyd core.* *~
|
||||
-rm -f *.gcda *.gcno
|
||||
-rm -rf .deps
|
||||
-rm -rf *.dSYM
|
||||
|
||||
@@ -112,10 +112,15 @@ install-docs :
|
||||
quickcheck : chronyd chronyc
|
||||
$(MAKE) -C test/unit check
|
||||
cd test/simulation && ./run
|
||||
cd test/system && ./run
|
||||
|
||||
check : chronyd chronyc
|
||||
$(MAKE) -C test/unit check
|
||||
cd test/simulation && ./run -i 20 -m 2
|
||||
cd test/system && ./run
|
||||
|
||||
print-chronyd-objects :
|
||||
@echo $(OBJS)
|
||||
|
||||
Makefile : Makefile.in configure
|
||||
@echo
|
||||
@@ -129,4 +134,6 @@ Makefile : Makefile.in configure
|
||||
.deps/%.d: %.c | .deps
|
||||
@$(CC) -MM $(CPPFLAGS) -MT '$(<:%.c=%.o) $@' $< -o $@
|
||||
|
||||
ifndef NODEPS
|
||||
-include $(ALL_OBJS:%.o=.deps/%.d)
|
||||
endif
|
||||
|
||||
146
NEWS
146
NEWS
@@ -1,3 +1,149 @@
|
||||
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
|
||||
* 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
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add support for Network Time Security (NTS) authentication
|
||||
* Add support for AES-CMAC keys (AES128, AES256) with Nettle
|
||||
* Add authselectmode directive to control selection of unauthenticated sources
|
||||
* Add binddevice, bindacqdevice, bindcmddevice directives
|
||||
* Add confdir directive to better support fragmented configuration
|
||||
* Add sourcedir directive and "reload sources" command to support dynamic
|
||||
NTP sources specified in files
|
||||
* Add clockprecision directive
|
||||
* Add dscp directive to set Differentiated Services Code Point (DSCP)
|
||||
* Add -L option to limit log messages by severity
|
||||
* Add -p option to print whole configuration with included files
|
||||
* Add -U option to allow start under non-root user
|
||||
* Allow maxsamples to be set to 1 for faster update with -q/-Q option
|
||||
* Avoid replacing NTP sources with sources that have unreachable address
|
||||
* Improve pools to repeat name resolution to get "maxsources" sources
|
||||
* Improve source selection with trusted sources
|
||||
* Improve NTP loop test to prevent synchronisation to itself
|
||||
* Repeat iburst when NTP source is switched from offline state to online
|
||||
* Update clock synchronisation status and leap status more frequently
|
||||
* Update seccomp filter
|
||||
* Add "add pool" command
|
||||
* Add "reset sources" command to drop all measurements
|
||||
* Add authdata command to print details about NTP authentication
|
||||
* Add selectdata command to print details about source selection
|
||||
* Add -N option and sourcename command to print original names of sources
|
||||
* Add -a option to some commands to print also unresolved sources
|
||||
* Add -k, -p, -r options to clients command to select, limit, reset data
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Don't set interface for NTP responses to allow asymmetric routing
|
||||
* Handle RTCs that don't support interrupts
|
||||
* Respond to command requests with correct address on multihomed hosts
|
||||
|
||||
Removed features
|
||||
----------------
|
||||
* Drop support for RIPEMD keys (RMD128, RMD160, RMD256, RMD320)
|
||||
* Drop support for long (non-standard) MACs in NTPv4 packets (chrony 2.x
|
||||
clients using non-MD5/SHA1 keys need to use option "version 3")
|
||||
* Drop support for line editing with GNU Readline
|
||||
|
||||
New in version 3.5.1
|
||||
====================
|
||||
|
||||
Security fixes
|
||||
--------------
|
||||
* Create new file when writing pidfile (CVE-2020-14367)
|
||||
|
||||
New in version 3.5
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add support for more accurate reading of PHC on Linux 5.0
|
||||
* Add support for hardware timestamping on interfaces with read-only
|
||||
timestamping configuration
|
||||
* Add support for memory locking and real-time priority on FreeBSD,
|
||||
NetBSD, Solaris
|
||||
* Update seccomp filter to work on more architectures
|
||||
* Validate refclock driver options
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Fix bindaddress directive on FreeBSD
|
||||
* Fix transposition of hardware RX timestamp on Linux 4.13 and later
|
||||
* Fix building on non-glibc systems
|
||||
|
||||
New in version 3.4
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add filter option to server/pool/peer directive
|
||||
* Add minsamples and maxsamples options to hwtimestamp directive
|
||||
* Add support for faster frequency adjustments in Linux 4.19
|
||||
* Change default pidfile to /var/run/chrony/chronyd.pid to allow
|
||||
chronyd without root privileges to remove it on exit
|
||||
* Disable sub-second polling intervals for distant NTP sources
|
||||
* Extend range of supported sub-second polling intervals
|
||||
* Get/set IPv4 destination/source address of NTP packets on FreeBSD
|
||||
* Make burst options and command useful with short polling intervals
|
||||
* Modify auto_offline option to activate when sending request failed
|
||||
* Respond from interface that received NTP request if possible
|
||||
* Add onoffline command to switch between online and offline state
|
||||
according to current system network configuration
|
||||
* Improve example NetworkManager dispatcher script
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Avoid waiting in Linux getrandom system call
|
||||
* Fix PPS support on FreeBSD and NetBSD
|
||||
|
||||
New in version 3.3
|
||||
==================
|
||||
|
||||
Enhancements
|
||||
------------
|
||||
* Add burst option to server/pool directive
|
||||
* Add stratum and tai options to refclock directive
|
||||
* Add support for Nettle crypto library
|
||||
* Add workaround for missing kernel receive timestamps on Linux
|
||||
* Wait for late hardware transmit timestamps
|
||||
* Improve source selection with unreachable sources
|
||||
* Improve protection against replay attacks on symmetric mode
|
||||
* Allow PHC refclock to use socket in /var/run/chrony
|
||||
* Add shutdown command to stop chronyd
|
||||
* Simplify format of response to manual list command
|
||||
* Improve handling of unknown responses in chronyc
|
||||
|
||||
Bug fixes
|
||||
---------
|
||||
* Respond to NTPv1 client requests with zero mode
|
||||
* Fix -x option to not require CAP_SYS_TIME under non-root user
|
||||
* Fix acquisitionport directive to work with privilege separation
|
||||
* Fix handling of socket errors on Linux to avoid high CPU usage
|
||||
* Fix chronyc to not get stuck in infinite loop after clock step
|
||||
|
||||
New in version 3.2
|
||||
==================
|
||||
|
||||
|
||||
170
README
170
README
@@ -29,28 +29,22 @@ 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
|
||||
likely require a porting exercise. You would need to start from one
|
||||
of the existing system-specific drivers and look into the quirks of
|
||||
certain system calls and the kernel on your target system.
|
||||
likely require a porting exercise.
|
||||
|
||||
How do I set it up?
|
||||
===================
|
||||
|
||||
The file INSTALL gives instructions. On supported systems the
|
||||
compilation process should be automatic.
|
||||
|
||||
You will need an ANSI C compiler -- gcc is recommended.
|
||||
|
||||
The manual (in texinfo and text formats) describes how to set the
|
||||
software up for the less straightforward cases.
|
||||
compilation process should be automatic. You will need a C compiler,
|
||||
e.g. gcc or clang.
|
||||
|
||||
What documentation is there?
|
||||
============================
|
||||
|
||||
A manual is supplied in Texinfo format (chrony.texi) and
|
||||
ready-formatted plain text (chrony.txt) in the distribution.
|
||||
The distribution includes manual pages and a document containing
|
||||
Frequently Asked Questions (FAQ).
|
||||
|
||||
There is also information available on the chrony web pages, accessible
|
||||
The documentation is also available on the chrony web pages, accessible
|
||||
through the URL
|
||||
|
||||
https://chrony.tuxfamily.org/
|
||||
@@ -59,24 +53,20 @@ Where are new versions announced?
|
||||
=================================
|
||||
|
||||
There is a low volume mailing list where new versions and other
|
||||
important news relating to chrony is announced. You can join this list
|
||||
important news relating to chrony are announced. You can join this list
|
||||
by sending mail with the subject "subscribe" to
|
||||
|
||||
chrony-announce-request@chrony.tuxfamily.org
|
||||
|
||||
These messages will be copied to chrony-users (see below).
|
||||
|
||||
How can I get support for chrony?
|
||||
and where can I discuss new features, possible bugs etc?
|
||||
========================================================
|
||||
=================================
|
||||
|
||||
There are 3 mailing lists relating to chrony. chrony-announce was
|
||||
mentioned above. chrony-users is a users' discussion list, e.g. for
|
||||
general questions and answers about using chrony. chrony-dev is a more
|
||||
technical list, e.g. for discussing how new features should be
|
||||
implemented, exchange of information between developers etc. To
|
||||
subscribe to either of these lists, send a message with the subject
|
||||
"subscribe" to
|
||||
There are two other mailing lists relating to chrony. chrony-users is a
|
||||
discussion list for users, e.g. for questions about chrony configuration
|
||||
and bug reports. chrony-dev is a more technical list for developers,
|
||||
e.g. for submitting patches and discussing how new features should be
|
||||
implemented. To subscribe to either of these lists, send a message with
|
||||
the subject "subscribe" to
|
||||
|
||||
chrony-users-request@chrony.tuxfamily.org
|
||||
or
|
||||
@@ -84,12 +74,6 @@ chrony-dev-request@chrony.tuxfamily.org
|
||||
|
||||
as applicable.
|
||||
|
||||
When you are reporting a bug, please send us all the information you can.
|
||||
Unfortunately, chrony has proven to be one of those programs where it is very
|
||||
difficult to reproduce bugs in a different environment. So we may have to
|
||||
interact with you quite a lot to obtain enough extra logging and tracing to
|
||||
pin-point the problem in some cases. Please be patient and plan for this!
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
@@ -104,135 +88,73 @@ Miroslav Lichvar <mlichvar@redhat.com>
|
||||
Acknowledgements
|
||||
================
|
||||
|
||||
In writing the chronyd program, extensive use has been made of RFC 1305
|
||||
and RFC 5905, written by David Mills. The source code of the NTP reference
|
||||
implementation has been used to check the details of the protocol.
|
||||
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.
|
||||
|
||||
The following people have provided patches and other major contributions
|
||||
to the program :
|
||||
to chrony:
|
||||
|
||||
Lonnie Abelbeck <lonnie@abelbeck.com>
|
||||
Patch to add tab-completion to chronyc
|
||||
|
||||
Benny Lyne Amorsen <benny@amorsen.dk>
|
||||
Patch to add minstratum option
|
||||
|
||||
Andrew Bishop <amb@gedanken.demon.co.uk>
|
||||
Fixes for bugs in logging when in daemon mode
|
||||
Fixes for compiler warnings
|
||||
Robustness improvements for drift file
|
||||
Improve installation (directory checking etc)
|
||||
Entries in contrib directory
|
||||
Improvements to 'sources' and 'sourcestats' output from chronyc
|
||||
Improvements to documentation
|
||||
Investigation of required dosynctodr behaviour for various Solaris
|
||||
versions.
|
||||
|
||||
Vincent Blut <vincent.debian@free.fr>
|
||||
Stephan I. Boettcher <stephan@nevis1.columbia.edu>
|
||||
Entries in contrib directory
|
||||
|
||||
David Bohman <debohman@gmail.com>
|
||||
Goswin Brederlow <brederlo@informatik.uni-tuebingen.de>
|
||||
Leigh Brown <leigh@solinno.co.uk>
|
||||
Erik Bryer <ebryer@spots.ab.ca>
|
||||
Entries in contrib directory
|
||||
|
||||
Jonathan Cameron <jic23@cam.ac.uk>
|
||||
Bryan Christianson <bryan@whatroute.net>
|
||||
Support for macOS
|
||||
Support for privilege separation
|
||||
Entries in contrib directory
|
||||
|
||||
Juliusz Chroboczek <jch@pps.jussieu.fr>
|
||||
Fix install rule in Makefile if chronyd file is in use.
|
||||
|
||||
Kamil Dudka <kdudka@redhat.com>
|
||||
Christian Ehrhardt <christian.ehrhardt@canonical.com>
|
||||
Paul Elliott <pelliott@io.com>
|
||||
DNSchrony (in contrib directory), a tool for handling NTP servers
|
||||
with variable IP addresses.
|
||||
|
||||
Robert Fairley <rfairley@redhat.com>
|
||||
Stefan R. Filipek <srfilipek@gmail.com>
|
||||
Mike Fleetwood <mike@rockover.demon.co.uk>
|
||||
Fixes for compiler warnings
|
||||
|
||||
Alexander Gretencord <arutha@gmx.de>
|
||||
Changes to installation directory system to make it easier for
|
||||
package builders.
|
||||
|
||||
Andrew Griffiths <agriffit@redhat.com>
|
||||
Patch to add support for seccomp filter
|
||||
|
||||
Walter Haidinger <walter.haidinger@gmx.at>
|
||||
Providing me with login access to a Linux installation where v1.12
|
||||
wouldn't compile, so I could develop the fixes for v1.13. Also, for
|
||||
providing the disc space so I can keep an independent backup of the
|
||||
sources.
|
||||
|
||||
Juergen Hannken-Illjes <hannken@eis.cs.tu-bs.de>
|
||||
Port to NetBSD
|
||||
|
||||
John Hasler <john@dhh.gt.org>
|
||||
Project and website at tuxfamily.org
|
||||
Changes to support 64 bit machines (i.e. those where
|
||||
sizeof(unsigned long) > 4)
|
||||
Bug fix to initstepslew directive
|
||||
Fix to remove potential buffer overrun errors.
|
||||
Memory locking and real-time scheduler support
|
||||
Fix fault where chronyd enters an endless loop
|
||||
|
||||
Tjalling Hattink <t.hattink@fugro.nl>
|
||||
Fix scheduler to allow stepping clock from timeout handler
|
||||
Patch to take leap second in PPS refclock from locked source
|
||||
Patch to make reading of RTC for initial trim more reliable
|
||||
|
||||
Liam Hatton <me@liamhatton.com>
|
||||
Advice on configuring for Linux on PPC
|
||||
|
||||
Jachym Holecek <jakym@volny.cz>
|
||||
Patch to make Linux real time clock work with devfs
|
||||
|
||||
Håkan Johansson <f96hajo@chalmers.se>
|
||||
Patch to avoid large values in sources and sourcestats output
|
||||
|
||||
Jim Knoble <jmknoble@pobox.com>
|
||||
Fixes for compiler warnings
|
||||
|
||||
Antti Jrvinen <costello@iki.fi>
|
||||
Advice on configuring for BSD/386
|
||||
|
||||
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>
|
||||
Kevin Lyda <kevin@ie.suberic.net>
|
||||
Paul Menzel <paulepanter@users.sourceforge.net>
|
||||
Vladimir Michl <vladimir.michl@seznam.cz>
|
||||
Victor Moroz <vim@prv.adlum.ru>
|
||||
Patch to support Linux with HZ!=100
|
||||
|
||||
Kalle Olavi Niemitalo <tosi@stekt.oulu.fi>
|
||||
acquisitionport support
|
||||
|
||||
Frank Otto <sandwichmacher@web.de>
|
||||
Handling arbitrary HZ values
|
||||
|
||||
Denny Page <dennypage@me.com>
|
||||
Advice on support for hardware timestamping
|
||||
|
||||
Chris Perl <cperl@janestreet.com>
|
||||
Gautier PHILIPPON <gautier.philippon@ensimag.grenoble-inp.fr>
|
||||
Patch to add refresh command to chronyc
|
||||
|
||||
Andreas Piesk <apiesk@virbus.de>
|
||||
Patch to make chronyc use the readline library if available
|
||||
|
||||
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>
|
||||
Patch to reply correctly on multihomed hosts
|
||||
|
||||
Bill Unruh <unruh@physics.ubc.ca>
|
||||
Advice on statistics
|
||||
|
||||
Stephen Wadeley <swadeley@redhat.com>
|
||||
Improvements to man pages
|
||||
|
||||
Bernhard Weiss <lisnablagh@web.de>
|
||||
Wolfgang Weisselberg <weissel@netcologne.de>
|
||||
Entries in contrib directory
|
||||
|
||||
Bernhard M. Wiedemann <bwiedemann@suse.de>
|
||||
Joachim Wiedorn <ad_debian@joonet.de>
|
||||
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
|
||||
Many robustness and security improvements
|
||||
|
||||
Ulrich Windl <ulrich.windl@rz.uni-regensburg.de> for the
|
||||
Providing me with information about the Linux 2.2 kernel
|
||||
functionality compared to 2.0.
|
||||
|
||||
Ulrich Windl <ulrich.windl@rz.uni-regensburg.de>
|
||||
Michael Witten <mfwitten@gmail.com>
|
||||
Doug Woodward <dougw@whistler.com>
|
||||
Advice on configuring for Solaris 2.8 on x86
|
||||
Thomas Zajic <zlatko@zlatko.fdns.net>
|
||||
|
||||
Many other people have contributed bug reports and suggestions. We are sorry
|
||||
we cannot identify all of you individually.
|
||||
|
||||
@@ -30,16 +30,19 @@
|
||||
#include "sysincl.h"
|
||||
|
||||
/* This type is used to represent an IPv4 address or IPv6 address.
|
||||
Addresses which are not resolved yet can be represented with an ID.
|
||||
All parts are in HOST order, NOT network order. */
|
||||
|
||||
#define IPADDR_UNSPEC 0
|
||||
#define IPADDR_INET4 1
|
||||
#define IPADDR_INET6 2
|
||||
#define IPADDR_ID 3
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
uint32_t in4;
|
||||
uint8_t in6[16];
|
||||
uint32_t id;
|
||||
} addr;
|
||||
uint16_t family;
|
||||
uint16_t _pad;
|
||||
@@ -47,8 +50,10 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
unsigned short port;
|
||||
} NTP_Remote_Address;
|
||||
uint16_t port;
|
||||
} IPSockAddr;
|
||||
|
||||
typedef IPSockAddr NTP_Remote_Address;
|
||||
|
||||
#define INVALID_IF_INDEX -1
|
||||
|
||||
|
||||
@@ -247,6 +247,8 @@ set_subnet_(ADF_AuthTable table,
|
||||
set_subnet(&table->base6, ip6, 4, 0, new_state, delete_children) == ADF_SUCCESS)
|
||||
return ADF_SUCCESS;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ADF_BADSUBNET;
|
||||
@@ -359,9 +361,9 @@ ADF_IsAllowed(ADF_AuthTable table,
|
||||
case IPADDR_INET6:
|
||||
split_ip6(ip_addr, ip6);
|
||||
return check_ip_in_node(&table->base6, ip6);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
139
candm.h
139
candm.h
@@ -99,7 +99,17 @@
|
||||
#define REQ_ADD_PEER2 59
|
||||
#define REQ_ADD_SERVER3 60
|
||||
#define REQ_ADD_PEER3 61
|
||||
#define N_REQUEST_TYPES 62
|
||||
#define REQ_SHUTDOWN 62
|
||||
#define REQ_ONOFFLINE 63
|
||||
#define REQ_ADD_SOURCE 64
|
||||
#define REQ_NTP_SOURCE_NAME 65
|
||||
#define REQ_RESET_SOURCES 66
|
||||
#define REQ_AUTH_DATA 67
|
||||
#define REQ_CLIENT_ACCESSES_BY_INDEX3 68
|
||||
#define REQ_SELECT_DATA 69
|
||||
#define REQ_RELOAD_SOURCES 70
|
||||
#define REQ_DOFFSET2 71
|
||||
#define N_REQUEST_TYPES 72
|
||||
|
||||
/* Structure used to exchange timespecs independent of time_t size */
|
||||
typedef struct {
|
||||
@@ -243,6 +253,11 @@ typedef struct {
|
||||
int32_t EOR;
|
||||
} REQ_Ac_Check;
|
||||
|
||||
/* Source types in NTP source requests */
|
||||
#define REQ_ADDSRC_SERVER 1
|
||||
#define REQ_ADDSRC_PEER 2
|
||||
#define REQ_ADDSRC_POOL 3
|
||||
|
||||
/* Flags used in NTP source requests */
|
||||
#define REQ_ADDSRC_ONLINE 0x1
|
||||
#define REQ_ADDSRC_AUTOOFFLINE 0x2
|
||||
@@ -252,9 +267,13 @@ typedef struct {
|
||||
#define REQ_ADDSRC_TRUST 0x20
|
||||
#define REQ_ADDSRC_REQUIRE 0x40
|
||||
#define REQ_ADDSRC_INTERLEAVED 0x80
|
||||
#define REQ_ADDSRC_BURST 0x100
|
||||
#define REQ_ADDSRC_NTS 0x200
|
||||
#define REQ_ADDSRC_COPY 0x400
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
uint32_t type;
|
||||
uint8_t name[256];
|
||||
uint32_t port;
|
||||
int32_t minpoll;
|
||||
int32_t maxpoll;
|
||||
@@ -266,6 +285,7 @@ typedef struct {
|
||||
int32_t min_samples;
|
||||
int32_t max_samples;
|
||||
uint32_t authkey;
|
||||
uint32_t nts_port;
|
||||
Float max_delay;
|
||||
Float max_delay_ratio;
|
||||
Float max_delay_dev_ratio;
|
||||
@@ -273,7 +293,9 @@ typedef struct {
|
||||
Float asymmetry;
|
||||
Float offset;
|
||||
uint32_t flags;
|
||||
uint32_t reserved[4];
|
||||
int32_t filter_length;
|
||||
uint32_t cert_set;
|
||||
uint32_t reserved[2];
|
||||
int32_t EOR;
|
||||
} REQ_NTP_Source;
|
||||
|
||||
@@ -288,8 +310,7 @@ typedef struct {
|
||||
} REQ_Dfreq;
|
||||
|
||||
typedef struct {
|
||||
int32_t sec;
|
||||
int32_t usec;
|
||||
Float doffset;
|
||||
int32_t EOR;
|
||||
} REQ_Doffset;
|
||||
|
||||
@@ -305,6 +326,8 @@ typedef struct {
|
||||
typedef struct {
|
||||
uint32_t first_index;
|
||||
uint32_t n_clients;
|
||||
uint32_t min_hits;
|
||||
uint32_t reset;
|
||||
int32_t EOR;
|
||||
} REQ_ClientAccessesByIndex;
|
||||
|
||||
@@ -331,6 +354,21 @@ typedef struct {
|
||||
int32_t EOR;
|
||||
} REQ_NTPData;
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
int32_t EOR;
|
||||
} REQ_NTPSourceName;
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
int32_t EOR;
|
||||
} REQ_AuthData;
|
||||
|
||||
typedef struct {
|
||||
uint32_t index;
|
||||
int32_t EOR;
|
||||
} REQ_SelectData;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define PKT_TYPE_CMD_REQUEST 1
|
||||
@@ -367,9 +405,10 @@ typedef struct {
|
||||
domain socket.
|
||||
|
||||
Version 6 (no authentication) : changed format of client accesses by index
|
||||
(using new request/reply types) and manual timestamp, new fields and flags
|
||||
in NTP source request and report, new commands: ntpdata, refresh,
|
||||
serverstats
|
||||
(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
|
||||
*/
|
||||
|
||||
#define PROTO_VERSION_NUMBER 6
|
||||
@@ -383,8 +422,8 @@ typedef struct {
|
||||
#define PROTO_VERSION_PADDING 6
|
||||
|
||||
/* The maximum length of padding in request packet, currently
|
||||
defined by MANUAL_LIST */
|
||||
#define MAX_PADDING_LENGTH 396
|
||||
defined by CLIENT_ACCESSES_BY_INDEX3 */
|
||||
#define MAX_PADDING_LENGTH 484
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -433,6 +472,9 @@ typedef struct {
|
||||
REQ_ReselectDistance reselect_distance;
|
||||
REQ_SmoothTime smoothtime;
|
||||
REQ_NTPData ntp_data;
|
||||
REQ_NTPSourceName ntp_source_name;
|
||||
REQ_AuthData auth_data;
|
||||
REQ_SelectData select_data;
|
||||
} data; /* Command specific parameters */
|
||||
|
||||
/* Padding used to prevent traffic amplification. It only defines the
|
||||
@@ -468,7 +510,13 @@ typedef struct {
|
||||
#define RPY_CLIENT_ACCESSES_BY_INDEX2 15
|
||||
#define RPY_NTP_DATA 16
|
||||
#define RPY_MANUAL_TIMESTAMP2 17
|
||||
#define N_REPLY_TYPES 18
|
||||
#define RPY_MANUAL_LIST2 18
|
||||
#define RPY_NTP_SOURCE_NAME 19
|
||||
#define RPY_AUTH_DATA 20
|
||||
#define RPY_CLIENT_ACCESSES_BY_INDEX3 21
|
||||
#define RPY_SERVER_STATS2 22
|
||||
#define RPY_SELECT_DATA 23
|
||||
#define N_REPLY_TYPES 24
|
||||
|
||||
/* Status codes */
|
||||
#define STT_SUCCESS 0
|
||||
@@ -492,6 +540,7 @@ typedef struct {
|
||||
#define STT_INVALIDAF 17
|
||||
#define STT_BADPKTVERSION 18
|
||||
#define STT_BADPKTLENGTH 19
|
||||
#define STT_INVALIDNAME 21
|
||||
|
||||
typedef struct {
|
||||
int32_t EOR;
|
||||
@@ -506,17 +555,12 @@ typedef struct {
|
||||
#define RPY_SD_MD_PEER 1
|
||||
#define RPY_SD_MD_REF 2
|
||||
|
||||
#define RPY_SD_ST_SYNC 0
|
||||
#define RPY_SD_ST_UNREACH 1
|
||||
#define RPY_SD_ST_SELECTED 0
|
||||
#define RPY_SD_ST_NONSELECTABLE 1
|
||||
#define RPY_SD_ST_FALSETICKER 2
|
||||
#define RPY_SD_ST_JITTERY 3
|
||||
#define RPY_SD_ST_CANDIDATE 4
|
||||
#define RPY_SD_ST_OUTLIER 5
|
||||
|
||||
#define RPY_SD_FLAG_NOSELECT 0x1
|
||||
#define RPY_SD_FLAG_PREFER 0x2
|
||||
#define RPY_SD_FLAG_TRUST 0x4
|
||||
#define RPY_SD_FLAG_REQUIRE 0x8
|
||||
#define RPY_SD_ST_UNSELECTED 4
|
||||
#define RPY_SD_ST_SELECTABLE 5
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
@@ -585,14 +629,17 @@ typedef struct {
|
||||
typedef struct {
|
||||
IPAddr ip;
|
||||
uint32_t ntp_hits;
|
||||
uint32_t nke_hits;
|
||||
uint32_t cmd_hits;
|
||||
uint32_t ntp_drops;
|
||||
uint32_t nke_drops;
|
||||
uint32_t cmd_drops;
|
||||
int8_t ntp_interval;
|
||||
int8_t nke_interval;
|
||||
int8_t cmd_interval;
|
||||
int8_t ntp_timeout_interval;
|
||||
int8_t pad;
|
||||
uint32_t last_ntp_hit_ago;
|
||||
uint32_t last_nke_hit_ago;
|
||||
uint32_t last_cmd_hit_ago;
|
||||
} RPY_ClientAccesses_Client;
|
||||
|
||||
@@ -606,10 +653,13 @@ typedef struct {
|
||||
|
||||
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;
|
||||
int32_t EOR;
|
||||
} RPY_ServerStats;
|
||||
|
||||
@@ -683,6 +733,50 @@ typedef struct {
|
||||
int32_t EOR;
|
||||
} RPY_NTPData;
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[256];
|
||||
int32_t EOR;
|
||||
} RPY_NTPSourceName;
|
||||
|
||||
#define RPY_AD_MD_NONE 0
|
||||
#define RPY_AD_MD_SYMMETRIC 1
|
||||
#define RPY_AD_MD_NTS 2
|
||||
|
||||
typedef struct {
|
||||
uint16_t mode;
|
||||
uint16_t key_type;
|
||||
uint32_t key_id;
|
||||
uint16_t key_length;
|
||||
uint16_t ke_attempts;
|
||||
uint32_t last_ke_ago;
|
||||
uint16_t cookies;
|
||||
uint16_t cookie_length;
|
||||
uint16_t nak;
|
||||
uint16_t pad;
|
||||
int32_t EOR;
|
||||
} RPY_AuthData;
|
||||
|
||||
#define RPY_SD_OPTION_NOSELECT 0x1
|
||||
#define RPY_SD_OPTION_PREFER 0x2
|
||||
#define RPY_SD_OPTION_TRUST 0x4
|
||||
#define RPY_SD_OPTION_REQUIRE 0x8
|
||||
|
||||
typedef struct {
|
||||
uint32_t ref_id;
|
||||
IPAddr ip_addr;
|
||||
uint8_t state_char;
|
||||
uint8_t authentication;
|
||||
uint8_t leap;
|
||||
uint8_t pad;
|
||||
uint16_t conf_options;
|
||||
uint16_t eff_options;
|
||||
uint32_t last_sample_ago;
|
||||
Float score;
|
||||
Float lo_limit;
|
||||
Float hi_limit;
|
||||
int32_t EOR;
|
||||
} RPY_SelectData;
|
||||
|
||||
typedef struct {
|
||||
uint8_t version;
|
||||
uint8_t pkt_type;
|
||||
@@ -712,6 +806,9 @@ typedef struct {
|
||||
RPY_Activity activity;
|
||||
RPY_Smoothing smoothing;
|
||||
RPY_NTPData ntp_data;
|
||||
RPY_NTPSourceName ntp_source_name;
|
||||
RPY_AuthData auth_data;
|
||||
RPY_SelectData select_data;
|
||||
} data; /* Reply specific parameters */
|
||||
|
||||
} CMD_Reply;
|
||||
|
||||
335
clientlog.c
335
clientlog.c
@@ -44,20 +44,17 @@
|
||||
#include "util.h"
|
||||
#include "logging.h"
|
||||
|
||||
#define MAX_SERVICES 3
|
||||
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
uint32_t last_ntp_hit;
|
||||
uint32_t last_cmd_hit;
|
||||
uint32_t ntp_hits;
|
||||
uint32_t cmd_hits;
|
||||
uint16_t ntp_drops;
|
||||
uint16_t cmd_drops;
|
||||
uint16_t ntp_tokens;
|
||||
uint16_t cmd_tokens;
|
||||
int8_t ntp_rate;
|
||||
int8_t cmd_rate;
|
||||
uint32_t last_hit[MAX_SERVICES];
|
||||
uint32_t hits[MAX_SERVICES];
|
||||
uint16_t drops[MAX_SERVICES];
|
||||
uint16_t tokens[MAX_SERVICES];
|
||||
int8_t rate[MAX_SERVICES];
|
||||
int8_t ntp_timeout_rate;
|
||||
uint8_t flags;
|
||||
uint8_t drop_flags;
|
||||
NTP_int64 ntp_rx_ts;
|
||||
NTP_int64 ntp_tx_ts;
|
||||
} Record;
|
||||
@@ -104,15 +101,12 @@ static uint32_t ts_offset;
|
||||
#define MIN_LIMIT_BURST 1
|
||||
#define MAX_LIMIT_BURST 255
|
||||
|
||||
static uint16_t max_ntp_tokens;
|
||||
static uint16_t max_cmd_tokens;
|
||||
static uint16_t ntp_tokens_per_packet;
|
||||
static uint16_t cmd_tokens_per_packet;
|
||||
static uint16_t max_tokens[MAX_SERVICES];
|
||||
static uint16_t tokens_per_hit[MAX_SERVICES];
|
||||
|
||||
/* Reduction of token rates to avoid overflow of 16-bit counters. Negative
|
||||
shift is used for coarse limiting with intervals shorter than -TS_FRAC. */
|
||||
static int ntp_token_shift;
|
||||
static int cmd_token_shift;
|
||||
static int token_shift[MAX_SERVICES];
|
||||
|
||||
/* Rates at which responses are randomly allowed (in log2) when the
|
||||
buckets don't have enough tokens. This is necessary in order to
|
||||
@@ -122,23 +116,18 @@ static int cmd_token_shift;
|
||||
#define MIN_LEAK_RATE 1
|
||||
#define MAX_LEAK_RATE 4
|
||||
|
||||
static int ntp_leak_rate;
|
||||
static int cmd_leak_rate;
|
||||
static int leak_rate[MAX_SERVICES];
|
||||
|
||||
/* Flag indicating whether the last response was dropped */
|
||||
#define FLAG_NTP_DROPPED 0x1
|
||||
|
||||
/* NTP limit interval in log2 */
|
||||
static int ntp_limit_interval;
|
||||
/* Limit intervals in log2 */
|
||||
static int limit_interval[MAX_SERVICES];
|
||||
|
||||
/* Flag indicating whether facility is turned on or not */
|
||||
static int active;
|
||||
|
||||
/* Global statistics */
|
||||
static uint32_t total_ntp_hits;
|
||||
static uint32_t total_cmd_hits;
|
||||
static uint32_t total_ntp_drops;
|
||||
static uint32_t total_cmd_drops;
|
||||
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;
|
||||
|
||||
#define NSEC_PER_SEC 1000000000U
|
||||
@@ -161,12 +150,28 @@ compare_ts(uint32_t x, uint32_t y)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
compare_total_hits(Record *x, Record *y)
|
||||
{
|
||||
uint32_t x_hits, y_hits;
|
||||
int i;
|
||||
|
||||
for (i = 0, x_hits = y_hits = 0; i < MAX_SERVICES; i++) {
|
||||
x_hits += x->hits[i];
|
||||
y_hits += y->hits[i];
|
||||
}
|
||||
|
||||
return x_hits > y_hits ? 1 : -1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static Record *
|
||||
get_record(IPAddr *ip)
|
||||
{
|
||||
unsigned int first, i;
|
||||
time_t last_hit, oldest_hit = 0;
|
||||
uint32_t last_hit = 0, oldest_hit = 0;
|
||||
Record *record, *oldest_record;
|
||||
unsigned int first, i, j;
|
||||
|
||||
if (!active || (ip->family != IPADDR_INET4 && ip->family != IPADDR_INET6))
|
||||
return NULL;
|
||||
@@ -184,12 +189,13 @@ get_record(IPAddr *ip)
|
||||
if (record->ip_addr.family == IPADDR_UNSPEC)
|
||||
break;
|
||||
|
||||
last_hit = compare_ts(record->last_ntp_hit, record->last_cmd_hit) > 0 ?
|
||||
record->last_ntp_hit : record->last_cmd_hit;
|
||||
for (j = 0; j < MAX_SERVICES; j++) {
|
||||
if (j == 0 || compare_ts(last_hit, record->last_hit[j]) < 0)
|
||||
last_hit = record->last_hit[j];
|
||||
}
|
||||
|
||||
if (!oldest_record || compare_ts(oldest_hit, last_hit) > 0 ||
|
||||
(oldest_hit == last_hit && record->ntp_hits + record->cmd_hits <
|
||||
oldest_record->ntp_hits + oldest_record->cmd_hits)) {
|
||||
(oldest_hit == last_hit && compare_total_hits(oldest_record, record) > 0)) {
|
||||
oldest_record = record;
|
||||
oldest_hit = last_hit;
|
||||
}
|
||||
@@ -211,14 +217,18 @@ get_record(IPAddr *ip)
|
||||
}
|
||||
|
||||
record->ip_addr = *ip;
|
||||
record->last_ntp_hit = record->last_cmd_hit = INVALID_TS;
|
||||
record->ntp_hits = record->cmd_hits = 0;
|
||||
record->ntp_drops = record->cmd_drops = 0;
|
||||
record->ntp_tokens = max_ntp_tokens;
|
||||
record->cmd_tokens = max_cmd_tokens;
|
||||
record->ntp_rate = record->cmd_rate = INVALID_RATE;
|
||||
for (i = 0; i < MAX_SERVICES; i++)
|
||||
record->last_hit[i] = INVALID_TS;
|
||||
for (i = 0; i < MAX_SERVICES; i++)
|
||||
record->hits[i] = 0;
|
||||
for (i = 0; i < MAX_SERVICES; i++)
|
||||
record->drops[i] = 0;
|
||||
for (i = 0; i < MAX_SERVICES; i++)
|
||||
record->tokens[i] = max_tokens[i];
|
||||
for (i = 0; i < MAX_SERVICES; i++)
|
||||
record->rate[i] = INVALID_RATE;
|
||||
record->ntp_timeout_rate = INVALID_RATE;
|
||||
record->flags = 0;
|
||||
record->drop_flags = 0;
|
||||
UTI_ZeroNtp64(&record->ntp_rx_ts);
|
||||
UTI_ZeroNtp64(&record->ntp_tx_ts);
|
||||
|
||||
@@ -306,31 +316,43 @@ set_bucket_params(int interval, int burst, uint16_t *max_tokens,
|
||||
void
|
||||
CLG_Initialise(void)
|
||||
{
|
||||
int interval, burst, leak_rate;
|
||||
int i, interval, burst, lrate;
|
||||
|
||||
max_ntp_tokens = max_cmd_tokens = 0;
|
||||
ntp_tokens_per_packet = cmd_tokens_per_packet = 0;
|
||||
ntp_token_shift = cmd_token_shift = 0;
|
||||
ntp_leak_rate = cmd_leak_rate = 0;
|
||||
ntp_limit_interval = MIN_LIMIT_INTERVAL;
|
||||
for (i = 0; i < MAX_SERVICES; i++) {
|
||||
max_tokens[i] = 0;
|
||||
tokens_per_hit[i] = 0;
|
||||
token_shift[i] = 0;
|
||||
leak_rate[i] = 0;
|
||||
limit_interval[i] = MIN_LIMIT_INTERVAL;
|
||||
|
||||
if (CNF_GetNTPRateLimit(&interval, &burst, &leak_rate)) {
|
||||
set_bucket_params(interval, burst, &max_ntp_tokens, &ntp_tokens_per_packet,
|
||||
&ntp_token_shift);
|
||||
ntp_leak_rate = CLAMP(MIN_LEAK_RATE, leak_rate, MAX_LEAK_RATE);
|
||||
ntp_limit_interval = CLAMP(MIN_LIMIT_INTERVAL, interval, MAX_LIMIT_INTERVAL);
|
||||
}
|
||||
switch (i) {
|
||||
case CLG_NTP:
|
||||
if (!CNF_GetNTPRateLimit(&interval, &burst, &lrate))
|
||||
continue;
|
||||
break;
|
||||
case CLG_NTSKE:
|
||||
if (!CNF_GetNtsRateLimit(&interval, &burst, &lrate))
|
||||
continue;
|
||||
break;
|
||||
case CLG_CMDMON:
|
||||
if (!CNF_GetCommandRateLimit(&interval, &burst, &lrate))
|
||||
continue;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if (CNF_GetCommandRateLimit(&interval, &burst, &leak_rate)) {
|
||||
set_bucket_params(interval, burst, &max_cmd_tokens, &cmd_tokens_per_packet,
|
||||
&cmd_token_shift);
|
||||
cmd_leak_rate = CLAMP(MIN_LEAK_RATE, leak_rate, MAX_LEAK_RATE);
|
||||
set_bucket_params(interval, burst, &max_tokens[i], &tokens_per_hit[i], &token_shift[i]);
|
||||
leak_rate[i] = CLAMP(MIN_LEAK_RATE, lrate, MAX_LEAK_RATE);
|
||||
limit_interval[i] = CLAMP(MIN_LIMIT_INTERVAL, interval, MAX_LIMIT_INTERVAL);
|
||||
}
|
||||
|
||||
active = !CNF_GetNoClientLog();
|
||||
if (!active) {
|
||||
if (ntp_leak_rate || cmd_leak_rate)
|
||||
LOG_FATAL("ratelimit cannot be used with noclientlog");
|
||||
for (i = 0; i < MAX_SERVICES; i++) {
|
||||
if (leak_rate[i] != 0)
|
||||
LOG_FATAL("Rate limiting cannot be enabled with noclientlog");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -339,6 +361,7 @@ CLG_Initialise(void)
|
||||
table where two copies exist at the same time. */
|
||||
max_slots = CNF_GetClientLogLimit() / (sizeof (Record) * 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));
|
||||
|
||||
slots = 0;
|
||||
records = NULL;
|
||||
@@ -380,30 +403,33 @@ get_ts_from_timespec(struct timespec *ts)
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_record(struct timespec *now, uint32_t *last_hit, uint32_t *hits,
|
||||
uint16_t *tokens, uint32_t max_tokens, int token_shift, int8_t *rate)
|
||||
update_record(CLG_Service service, Record *record, struct timespec *now)
|
||||
{
|
||||
uint32_t interval, now_ts, prev_hit, new_tokens;
|
||||
int interval2;
|
||||
uint32_t interval, now_ts, prev_hit, tokens;
|
||||
int interval2, tshift, mtokens;
|
||||
int8_t *rate;
|
||||
|
||||
now_ts = get_ts_from_timespec(now);
|
||||
|
||||
prev_hit = *last_hit;
|
||||
*last_hit = now_ts;
|
||||
(*hits)++;
|
||||
prev_hit = record->last_hit[service];
|
||||
record->last_hit[service] = now_ts;
|
||||
record->hits[service]++;
|
||||
|
||||
interval = now_ts - prev_hit;
|
||||
|
||||
if (prev_hit == INVALID_TS || (int32_t)interval < 0)
|
||||
return;
|
||||
|
||||
if (token_shift >= 0)
|
||||
new_tokens = (now_ts >> token_shift) - (prev_hit >> token_shift);
|
||||
else if (now_ts - prev_hit > max_tokens)
|
||||
new_tokens = max_tokens;
|
||||
tshift = token_shift[service];
|
||||
mtokens = max_tokens[service];
|
||||
|
||||
if (tshift >= 0)
|
||||
tokens = (now_ts >> tshift) - (prev_hit >> tshift);
|
||||
else if (now_ts - prev_hit > mtokens)
|
||||
tokens = mtokens;
|
||||
else
|
||||
new_tokens = (now_ts - prev_hit) << -token_shift;
|
||||
*tokens = MIN(*tokens + new_tokens, max_tokens);
|
||||
tokens = (now_ts - prev_hit) << -tshift;
|
||||
record->tokens[service] = MIN(record->tokens[service] + tokens, mtokens);
|
||||
|
||||
/* Convert the interval to scaled and rounded log2 */
|
||||
if (interval) {
|
||||
@@ -418,6 +444,11 @@ update_record(struct timespec *now, uint32_t *last_hit, uint32_t *hits,
|
||||
interval2 = -RATE_SCALE * (TS_FRAC + 1);
|
||||
}
|
||||
|
||||
/* For the NTP service, update one of the two rates depending on whether
|
||||
the previous request of the client had a reply or it timed out */
|
||||
rate = service == CLG_NTP && record->drop_flags & (1U << service) ?
|
||||
&record->ntp_timeout_rate : &record->rate[service];
|
||||
|
||||
/* Update the rate in a rough approximation of exponential moving average */
|
||||
if (*rate == INVALID_RATE) {
|
||||
*rate = -interval2;
|
||||
@@ -457,50 +488,33 @@ CLG_GetClientIndex(IPAddr *client)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_LogNTPAccess(IPAddr *client, struct timespec *now)
|
||||
static void
|
||||
check_service_number(CLG_Service service)
|
||||
{
|
||||
Record *record;
|
||||
|
||||
total_ntp_hits++;
|
||||
|
||||
record = get_record(client);
|
||||
if (record == NULL)
|
||||
return -1;
|
||||
|
||||
/* Update one of the two rates depending on whether the previous request
|
||||
of the client had a reply or it timed out */
|
||||
update_record(now, &record->last_ntp_hit, &record->ntp_hits,
|
||||
&record->ntp_tokens, max_ntp_tokens, ntp_token_shift,
|
||||
record->flags & FLAG_NTP_DROPPED ?
|
||||
&record->ntp_timeout_rate : &record->ntp_rate);
|
||||
|
||||
DEBUG_LOG("NTP hits %"PRIu32" rate %d trate %d tokens %d",
|
||||
record->ntp_hits, record->ntp_rate, record->ntp_timeout_rate,
|
||||
record->ntp_tokens);
|
||||
|
||||
return get_index(record);
|
||||
assert(service >= 0 && service <= MAX_SERVICES);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_LogCommandAccess(IPAddr *client, struct timespec *now)
|
||||
CLG_LogServiceAccess(CLG_Service service, IPAddr *client, struct timespec *now)
|
||||
{
|
||||
Record *record;
|
||||
|
||||
total_cmd_hits++;
|
||||
check_service_number(service);
|
||||
|
||||
total_hits[service]++;
|
||||
|
||||
record = get_record(client);
|
||||
if (record == NULL)
|
||||
return -1;
|
||||
|
||||
update_record(now, &record->last_cmd_hit, &record->cmd_hits,
|
||||
&record->cmd_tokens, max_cmd_tokens, cmd_token_shift,
|
||||
&record->cmd_rate);
|
||||
update_record(service, record, now);
|
||||
|
||||
DEBUG_LOG("Cmd hits %"PRIu32" rate %d tokens %d",
|
||||
record->cmd_hits, record->cmd_rate, record->cmd_tokens);
|
||||
DEBUG_LOG("service %d hits %"PRIu32" rate %d trate %d tokens %d",
|
||||
(int)service, record->hits[service], record->rate[service],
|
||||
service == CLG_NTP ? record->ntp_timeout_rate : INVALID_RATE,
|
||||
record->tokens[service]);
|
||||
|
||||
return get_index(record);
|
||||
}
|
||||
@@ -530,71 +544,53 @@ limit_response_random(int leak_rate)
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_LimitNTPResponseRate(int index)
|
||||
CLG_LimitServiceRate(CLG_Service service, int index)
|
||||
{
|
||||
Record *record;
|
||||
int drop;
|
||||
|
||||
if (!ntp_tokens_per_packet)
|
||||
check_service_number(service);
|
||||
|
||||
if (tokens_per_hit[service] == 0)
|
||||
return 0;
|
||||
|
||||
record = ARR_GetElement(records, index);
|
||||
record->flags &= ~FLAG_NTP_DROPPED;
|
||||
record->drop_flags &= ~(1U << service);
|
||||
|
||||
if (record->ntp_tokens >= ntp_tokens_per_packet) {
|
||||
record->ntp_tokens -= ntp_tokens_per_packet;
|
||||
if (record->tokens[service] >= tokens_per_hit[service]) {
|
||||
record->tokens[service] -= tokens_per_hit[service];
|
||||
return 0;
|
||||
}
|
||||
|
||||
drop = limit_response_random(ntp_leak_rate);
|
||||
drop = limit_response_random(leak_rate[service]);
|
||||
|
||||
/* Poorly implemented clients may send new requests at even a higher rate
|
||||
/* Poorly implemented NTP clients can send requests at a higher rate
|
||||
when they are not getting replies. If the request rate seems to be more
|
||||
than twice as much as when replies are sent, give up on rate limiting to
|
||||
reduce the amount of traffic. Invert the sense of the leak to respond to
|
||||
most of the requests, but still keep the estimated rate updated. */
|
||||
if (record->ntp_timeout_rate != INVALID_RATE &&
|
||||
record->ntp_timeout_rate > record->ntp_rate + RATE_SCALE)
|
||||
if (service == CLG_NTP && record->ntp_timeout_rate != INVALID_RATE &&
|
||||
record->ntp_timeout_rate > record->rate[service] + RATE_SCALE)
|
||||
drop = !drop;
|
||||
|
||||
if (!drop) {
|
||||
record->ntp_tokens = 0;
|
||||
record->tokens[service] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
record->flags |= FLAG_NTP_DROPPED;
|
||||
record->ntp_drops++;
|
||||
total_ntp_drops++;
|
||||
record->drop_flags |= 1U << service;
|
||||
record->drops[service]++;
|
||||
total_drops[service]++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_LimitCommandResponseRate(int index)
|
||||
void
|
||||
CLG_LogAuthNtpRequest(void)
|
||||
{
|
||||
Record *record;
|
||||
|
||||
if (!cmd_tokens_per_packet)
|
||||
return 0;
|
||||
|
||||
record = ARR_GetElement(records, index);
|
||||
|
||||
if (record->cmd_tokens >= cmd_tokens_per_packet) {
|
||||
record->cmd_tokens -= cmd_tokens_per_packet;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!limit_response_random(cmd_leak_rate)) {
|
||||
record->cmd_tokens = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
record->cmd_drops++;
|
||||
total_cmd_drops++;
|
||||
|
||||
return 1;
|
||||
total_ntp_auth_hits++;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -614,7 +610,7 @@ void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts)
|
||||
int
|
||||
CLG_GetNtpMinPoll(void)
|
||||
{
|
||||
return ntp_limit_interval;
|
||||
return limit_interval[CLG_NTP];
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -653,10 +649,12 @@ static uint32_t get_last_ago(uint32_t x, uint32_t y)
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CLG_GetClientAccessReportByIndex(int index, RPT_ClientAccessByIndex_Report *report, struct timespec *now)
|
||||
CLG_GetClientAccessReportByIndex(int index, int reset, uint32_t min_hits,
|
||||
RPT_ClientAccessByIndex_Report *report, struct timespec *now)
|
||||
{
|
||||
Record *record;
|
||||
uint32_t now_ts;
|
||||
int i, r;
|
||||
|
||||
if (!active || index < 0 || index >= ARR_GetSize(records))
|
||||
return 0;
|
||||
@@ -666,20 +664,44 @@ CLG_GetClientAccessReportByIndex(int index, RPT_ClientAccessByIndex_Report *repo
|
||||
if (record->ip_addr.family == IPADDR_UNSPEC)
|
||||
return 0;
|
||||
|
||||
now_ts = get_ts_from_timespec(now);
|
||||
if (min_hits == 0) {
|
||||
r = 1;
|
||||
} else {
|
||||
for (i = r = 0; i < MAX_SERVICES; i++) {
|
||||
if (record->hits[i] >= min_hits) {
|
||||
r = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
report->ip_addr = record->ip_addr;
|
||||
report->ntp_hits = record->ntp_hits;
|
||||
report->cmd_hits = record->cmd_hits;
|
||||
report->ntp_drops = record->ntp_drops;
|
||||
report->cmd_drops = record->cmd_drops;
|
||||
report->ntp_interval = get_interval(record->ntp_rate);
|
||||
report->cmd_interval = get_interval(record->cmd_rate);
|
||||
report->ntp_timeout_interval = get_interval(record->ntp_timeout_rate);
|
||||
report->last_ntp_hit_ago = get_last_ago(now_ts, record->last_ntp_hit);
|
||||
report->last_cmd_hit_ago = get_last_ago(now_ts, record->last_cmd_hit);
|
||||
if (r) {
|
||||
now_ts = get_ts_from_timespec(now);
|
||||
|
||||
return 1;
|
||||
report->ip_addr = record->ip_addr;
|
||||
report->ntp_hits = record->hits[CLG_NTP];
|
||||
report->nke_hits = record->hits[CLG_NTSKE];
|
||||
report->cmd_hits = record->hits[CLG_CMDMON];
|
||||
report->ntp_drops = record->drops[CLG_NTP];
|
||||
report->nke_drops = record->drops[CLG_NTSKE];
|
||||
report->cmd_drops = record->drops[CLG_CMDMON];
|
||||
report->ntp_interval = get_interval(record->rate[CLG_NTP]);
|
||||
report->nke_interval = get_interval(record->rate[CLG_NTSKE]);
|
||||
report->cmd_interval = get_interval(record->rate[CLG_CMDMON]);
|
||||
report->ntp_timeout_interval = get_interval(record->ntp_timeout_rate);
|
||||
report->last_ntp_hit_ago = get_last_ago(now_ts, record->last_hit[CLG_NTP]);
|
||||
report->last_nke_hit_ago = get_last_ago(now_ts, record->last_hit[CLG_NTSKE]);
|
||||
report->last_cmd_hit_ago = get_last_ago(now_ts, record->last_hit[CLG_CMDMON]);
|
||||
}
|
||||
|
||||
if (reset) {
|
||||
for (i = 0; i < MAX_SERVICES; i++) {
|
||||
record->hits[i] = 0;
|
||||
record->drops[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -687,9 +709,12 @@ CLG_GetClientAccessReportByIndex(int index, RPT_ClientAccessByIndex_Report *repo
|
||||
void
|
||||
CLG_GetServerStatsReport(RPT_ServerStatsReport *report)
|
||||
{
|
||||
report->ntp_hits = total_ntp_hits;
|
||||
report->cmd_hits = total_cmd_hits;
|
||||
report->ntp_drops = total_ntp_drops;
|
||||
report->cmd_drops = total_cmd_drops;
|
||||
report->ntp_hits = total_hits[CLG_NTP];
|
||||
report->nke_hits = total_hits[CLG_NTSKE];
|
||||
report->cmd_hits = total_hits[CLG_CMDMON];
|
||||
report->ntp_drops = total_drops[CLG_NTP];
|
||||
report->nke_drops = total_drops[CLG_NTSKE];
|
||||
report->cmd_drops = total_drops[CLG_CMDMON];
|
||||
report->log_drops = total_record_drops;
|
||||
report->ntp_auth_hits = total_ntp_auth_hits;
|
||||
}
|
||||
|
||||
17
clientlog.h
17
clientlog.h
@@ -31,20 +31,27 @@
|
||||
#include "sysincl.h"
|
||||
#include "reports.h"
|
||||
|
||||
typedef enum {
|
||||
CLG_NTP = 0,
|
||||
CLG_NTSKE,
|
||||
CLG_CMDMON,
|
||||
} CLG_Service;
|
||||
|
||||
extern void CLG_Initialise(void);
|
||||
extern void CLG_Finalise(void);
|
||||
extern int CLG_GetClientIndex(IPAddr *client);
|
||||
extern int CLG_LogNTPAccess(IPAddr *client, struct timespec *now);
|
||||
extern int CLG_LogCommandAccess(IPAddr *client, struct timespec *now);
|
||||
extern int CLG_LimitNTPResponseRate(int index);
|
||||
extern int CLG_LimitCommandResponseRate(int index);
|
||||
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 int CLG_GetNtpMinPoll(void);
|
||||
|
||||
/* And some reporting functions, for use by chronyc. */
|
||||
|
||||
extern int CLG_GetNumberOfIndices(void);
|
||||
extern int CLG_GetClientAccessReportByIndex(int index, RPT_ClientAccessByIndex_Report *report, struct timespec *now);
|
||||
extern int CLG_GetClientAccessReportByIndex(int index, int reset, uint32_t min_hits,
|
||||
RPT_ClientAccessByIndex_Report *report,
|
||||
struct timespec *now);
|
||||
extern void CLG_GetServerStatsReport(RPT_ServerStatsReport *report);
|
||||
|
||||
#endif /* GOT_CLIENTLOG_H */
|
||||
|
||||
48
cmac.h
Normal file
48
cmac.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019
|
||||
*
|
||||
* 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 CMAC.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef GOT_CMAC_H
|
||||
#define GOT_CMAC_H
|
||||
|
||||
/* Avoid overlapping with the hash enumeration */
|
||||
typedef enum {
|
||||
CMC_INVALID = 0,
|
||||
CMC_AES128 = 13,
|
||||
CMC_AES256 = 14,
|
||||
} CMC_Algorithm;
|
||||
|
||||
typedef struct CMC_Instance_Record *CMC_Instance;
|
||||
|
||||
extern int CMC_GetKeyLength(CMC_Algorithm algorithm);
|
||||
extern CMC_Instance CMC_CreateInstance(CMC_Algorithm algorithm, const unsigned char *key,
|
||||
int length);
|
||||
extern int CMC_Hash(CMC_Instance inst, const void *in, int in_len,
|
||||
unsigned char *out, int out_len);
|
||||
extern void CMC_DestroyInstance(CMC_Instance inst);
|
||||
|
||||
#endif
|
||||
|
||||
117
cmac_nettle.c
Normal file
117
cmac_nettle.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Support for AES128 and AES256 CMAC in Nettle.
|
||||
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <nettle/cmac.h>
|
||||
|
||||
#include "cmac.h"
|
||||
#include "memory.h"
|
||||
|
||||
struct CMC_Instance_Record {
|
||||
int key_length;
|
||||
union {
|
||||
struct cmac_aes128_ctx aes128;
|
||||
struct cmac_aes256_ctx aes256;
|
||||
} context;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CMC_GetKeyLength(CMC_Algorithm algorithm)
|
||||
{
|
||||
if (algorithm == CMC_AES128)
|
||||
return AES128_KEY_SIZE;
|
||||
else if (algorithm == CMC_AES256)
|
||||
return AES256_KEY_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
CMC_Instance
|
||||
CMC_CreateInstance(CMC_Algorithm algorithm, const unsigned char *key, int length)
|
||||
{
|
||||
CMC_Instance inst;
|
||||
|
||||
if (length <= 0 || length != CMC_GetKeyLength(algorithm))
|
||||
return NULL;
|
||||
|
||||
inst = MallocNew(struct CMC_Instance_Record);
|
||||
inst->key_length = length;
|
||||
|
||||
switch (length) {
|
||||
case AES128_KEY_SIZE:
|
||||
cmac_aes128_set_key(&inst->context.aes128, key);
|
||||
break;
|
||||
case AES256_KEY_SIZE:
|
||||
cmac_aes256_set_key(&inst->context.aes256, key);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CMC_Hash(CMC_Instance inst, const void *in, int in_len, unsigned char *out, int out_len)
|
||||
{
|
||||
if (in_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
if (out_len > CMAC128_DIGEST_SIZE)
|
||||
out_len = CMAC128_DIGEST_SIZE;
|
||||
|
||||
switch (inst->key_length) {
|
||||
case AES128_KEY_SIZE:
|
||||
cmac_aes128_update(&inst->context.aes128, in_len, in);
|
||||
cmac_aes128_digest(&inst->context.aes128, out_len, out);
|
||||
break;
|
||||
case AES256_KEY_SIZE:
|
||||
cmac_aes256_update(&inst->context.aes256, in_len, in);
|
||||
cmac_aes256_digest(&inst->context.aes256, out_len, out);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
CMC_DestroyInstance(CMC_Instance inst)
|
||||
{
|
||||
Free(inst);
|
||||
}
|
||||
2
cmdmon.h
2
cmdmon.h
@@ -29,7 +29,7 @@
|
||||
|
||||
#include "addressing.h"
|
||||
|
||||
extern void CAM_Initialise(int family);
|
||||
extern void CAM_Initialise(void);
|
||||
|
||||
extern void CAM_Finalise(void);
|
||||
|
||||
|
||||
33
cmdparse.c
33
cmdparse.c
@@ -48,9 +48,10 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
src->port = SRC_DEFAULT_PORT;
|
||||
src->params.minpoll = SRC_DEFAULT_MINPOLL;
|
||||
src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
|
||||
src->params.online = 1;
|
||||
src->params.connectivity = SRC_ONLINE;
|
||||
src->params.auto_offline = 0;
|
||||
src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
|
||||
src->params.burst = 0;
|
||||
src->params.iburst = 0;
|
||||
src->params.min_stratum = SRC_DEFAULT_MINSTRATUM;
|
||||
src->params.poll_target = SRC_DEFAULT_POLLTARGET;
|
||||
@@ -58,9 +59,14 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
src->params.max_sources = SRC_DEFAULT_MAXSOURCES;
|
||||
src->params.min_samples = SRC_DEFAULT_MINSAMPLES;
|
||||
src->params.max_samples = SRC_DEFAULT_MAXSAMPLES;
|
||||
src->params.filter_length = 0;
|
||||
src->params.interleaved = 0;
|
||||
src->params.sel_options = 0;
|
||||
src->params.nts = 0;
|
||||
src->params.nts_port = SRC_DEFAULT_NTSPORT;
|
||||
src->params.copy = 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;
|
||||
@@ -84,10 +90,14 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
|
||||
if (!strcasecmp(cmd, "auto_offline")) {
|
||||
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.online = 0;
|
||||
src->params.connectivity = SRC_OFFLINE;
|
||||
} else if (!strcasecmp(cmd, "noselect")) {
|
||||
src->params.sel_options |= SRC_SELECT_NOSELECT;
|
||||
} else if (!strcasecmp(cmd, "prefer")) {
|
||||
@@ -96,6 +106,9 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
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)
|
||||
@@ -103,6 +116,9 @@ 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, "filter")) {
|
||||
if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "maxdelay")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
|
||||
return 0;
|
||||
@@ -133,11 +149,16 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
||||
} else if (!strcasecmp(cmd, "minstratum")) {
|
||||
if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "nts")) {
|
||||
src->params.nts = 1;
|
||||
} else if (!strcasecmp(cmd, "ntsport")) {
|
||||
if (sscanf(line, "%d%n", &src->params.nts_port, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "offset")) {
|
||||
if (sscanf(line, "%lf%n", &src->params.offset, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "port")) {
|
||||
if (sscanf(line, "%hu%n", &src->port, &n) != 1)
|
||||
if (sscanf(line, "%d%n", &src->port, &n) != 1)
|
||||
return 0;
|
||||
} else if (!strcasecmp(cmd, "polltarget")) {
|
||||
if (sscanf(line, "%d%n", &src->params.poll_target, &n) != 1)
|
||||
@@ -254,7 +275,7 @@ CPS_SplitWord(char *line)
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
|
||||
CPS_ParseKey(char *line, uint32_t *id, const char **type, char **key)
|
||||
{
|
||||
char *s1, *s2, *s3, *s4;
|
||||
|
||||
@@ -271,10 +292,10 @@ CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
|
||||
return 0;
|
||||
|
||||
if (*s3) {
|
||||
*hash = s2;
|
||||
*type = s2;
|
||||
*key = s3;
|
||||
} else {
|
||||
*hash = "MD5";
|
||||
*type = "MD5";
|
||||
*key = s2;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
unsigned short port;
|
||||
int port;
|
||||
SourceParameters params;
|
||||
} CPS_NTP_Source;
|
||||
|
||||
@@ -49,6 +49,6 @@ extern void CPS_NormalizeLine(char *line);
|
||||
extern char *CPS_SplitWord(char *line);
|
||||
|
||||
/* Parse a key from keyfile */
|
||||
extern int CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key);
|
||||
extern int CPS_ParseKey(char *line, uint32_t *id, const char **type, char **key);
|
||||
|
||||
#endif /* GOT_CMDPARSE_H */
|
||||
|
||||
26
conf.h
26
conf.h
@@ -30,10 +30,13 @@
|
||||
|
||||
#include "addressing.h"
|
||||
#include "reference.h"
|
||||
#include "sources.h"
|
||||
|
||||
extern void CNF_Initialise(int restarted, int client_only);
|
||||
extern void CNF_Finalise(void);
|
||||
|
||||
extern void CNF_EnablePrint(void);
|
||||
|
||||
extern char *CNF_GetRtcDevice(void);
|
||||
|
||||
extern void CNF_ReadFile(const char *filename);
|
||||
@@ -46,6 +49,8 @@ extern void CNF_AddSources(void);
|
||||
extern void CNF_AddBroadcasts(void);
|
||||
extern void CNF_AddRefclocks(void);
|
||||
|
||||
extern void CNF_ReloadSources(void);
|
||||
|
||||
extern int CNF_GetAcquisitionPort(void);
|
||||
extern int CNF_GetNTPPort(void);
|
||||
extern char *CNF_GetDriftFile(void);
|
||||
@@ -74,7 +79,11 @@ extern void CNF_GetFallbackDrifts(int *min, int *max);
|
||||
extern void CNF_GetBindAddress(int family, IPAddr *addr);
|
||||
extern void CNF_GetBindAcquisitionAddress(int family, IPAddr *addr);
|
||||
extern void CNF_GetBindCommandAddress(int family, IPAddr *addr);
|
||||
extern char *CNF_GetBindNtpInterface(void);
|
||||
extern char *CNF_GetBindAcquisitionInterface(void);
|
||||
extern char *CNF_GetBindCommandInterface(void);
|
||||
extern char *CNF_GetBindCommandPath(void);
|
||||
extern int CNF_GetNtpDscp(void);
|
||||
extern char *CNF_GetNtpSigndSocket(void);
|
||||
extern char *CNF_GetPidFile(void);
|
||||
extern REF_LeapMode CNF_GetLeapSecMode(void);
|
||||
@@ -86,7 +95,9 @@ extern double CNF_GetMaxClockError(void);
|
||||
extern double CNF_GetMaxDrift(void);
|
||||
extern double CNF_GetCorrectionTimeRatio(void);
|
||||
extern double CNF_GetMaxSlewRate(void);
|
||||
extern double CNF_GetClockPrecision(void);
|
||||
|
||||
extern SRC_AuthSelectMode CNF_GetAuthSelectMode(void);
|
||||
extern double CNF_GetMaxDistance(void);
|
||||
extern double CNF_GetMaxJitter(void);
|
||||
extern double CNF_GetReselectDistance(void);
|
||||
@@ -101,6 +112,7 @@ extern int CNF_GetSchedPriority(void);
|
||||
extern int CNF_GetLockMemory(void);
|
||||
|
||||
extern int CNF_GetNTPRateLimit(int *interval, int *burst, int *leak);
|
||||
extern int CNF_GetNtsRateLimit(int *interval, int *burst, int *leak);
|
||||
extern int CNF_GetCommandRateLimit(int *interval, int *burst, int *leak);
|
||||
extern void CNF_GetSmooth(double *max_freq, double *max_wander, int *leap_only);
|
||||
extern void CNF_GetTempComp(char **file, double *interval, char **point_file, double *T0, double *k0, double *k1, double *k2);
|
||||
@@ -128,6 +140,8 @@ typedef enum {
|
||||
typedef struct {
|
||||
char *name;
|
||||
int minpoll;
|
||||
int min_samples;
|
||||
int max_samples;
|
||||
int nocrossts;
|
||||
CNF_HwTs_RxFilter rxfilter;
|
||||
double precision;
|
||||
@@ -137,4 +151,16 @@ typedef struct {
|
||||
|
||||
extern int CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface);
|
||||
|
||||
extern char *CNF_GetNtsDumpDir(void);
|
||||
extern char *CNF_GetNtsNtpServer(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 int CNF_GetNtsTrustedCertsPaths(const char ***paths, uint32_t **ids);
|
||||
extern int CNF_GetNoSystemCert(void);
|
||||
extern int CNF_GetNoCertTimeCheck(void);
|
||||
|
||||
#endif /* GOT_CONF_H */
|
||||
|
||||
354
configure
vendored
354
configure
vendored
@@ -5,7 +5,8 @@
|
||||
#
|
||||
# Copyright (C) Richard P. Curnow 1997-2003
|
||||
# Copyright (C) Bryan Christianson 2016
|
||||
# Copyright (C) Miroslav Lichvar 2009, 2012-2016
|
||||
# Copyright (C) Miroslav Lichvar 2009, 2012-2020
|
||||
# Copyright (C) Stefan R. Filipek 2019
|
||||
#
|
||||
# =======================================================================
|
||||
|
||||
@@ -32,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 ]
|
||||
@@ -49,11 +50,39 @@ test_code () {
|
||||
echo "No"
|
||||
result=1
|
||||
fi
|
||||
rm -f docheck.c docheck
|
||||
rm -f conftest.c conftest
|
||||
echo >> config.log
|
||||
return $result
|
||||
}
|
||||
#}}}
|
||||
#{{{ test_executable
|
||||
test_executable () {
|
||||
name=$1
|
||||
executable=$2
|
||||
options=$3
|
||||
|
||||
printf "%s" "Checking for $name : "
|
||||
|
||||
echo $executable $options >> config.log
|
||||
$executable $options >> config.log 2>&1
|
||||
|
||||
if [ $? -eq 0 ]
|
||||
then
|
||||
echo "Yes"
|
||||
result=0
|
||||
else
|
||||
echo "No"
|
||||
result=1
|
||||
fi
|
||||
echo >> config.log
|
||||
return $result
|
||||
}
|
||||
#}}}
|
||||
#{{{ pkg_config
|
||||
pkg_config () {
|
||||
$PKG_CONFIG "$@" 2>> config.log
|
||||
}
|
||||
#}}}
|
||||
#{{{ usage
|
||||
usage () {
|
||||
cat <<EOF
|
||||
@@ -79,14 +108,13 @@ for instance \`--prefix=$HOME'.
|
||||
|
||||
For better control, use the options below.
|
||||
--disable-readline Disable line editing support
|
||||
--without-readline Don't use GNU readline even if it is available
|
||||
--without-editline Don't use editline even if it is available
|
||||
--with-readline-includes=DIR Specify where readline include directory is
|
||||
--with-readline-library=DIR Specify where readline lib directory is
|
||||
--with-ncurses-library=DIR Specify where ncurses lib directory is
|
||||
--disable-sechash Disable support for hashes other than MD5
|
||||
--without-nettle Don't use nettle even if it is available
|
||||
--without-nss Don't use NSS even if it is available
|
||||
--without-tomcrypt Don't use libtomcrypt even if it is available
|
||||
--disable-nts Disable NTS support
|
||||
--without-gnutls Don't use gnutls even if it is available
|
||||
--disable-cmdmon Disable command and monitoring support
|
||||
--disable-ntp Disable NTP support
|
||||
--disable-refclock Disable reference clock support
|
||||
@@ -107,7 +135,7 @@ For better control, use the options below.
|
||||
since 1970-01-01 [50*365 days ago]
|
||||
--with-user=USER Specify default chronyd user [root]
|
||||
--with-hwclockfile=PATH Specify default path to hwclock(8) adjtime file
|
||||
--with-pidfile=PATH Specify default pidfile [/var/run/chronyd.pid]
|
||||
--with-pidfile=PATH Specify default pidfile [/var/run/chrony/chronyd.pid]
|
||||
--with-rtcdevice=PATH Specify default path to RTC device [/dev/rtc]
|
||||
--with-sendmail=PATH Path to sendmail binary [/usr/lib/sendmail]
|
||||
--enable-debug Enable debugging support
|
||||
@@ -135,6 +163,11 @@ Some influential environment variables:
|
||||
headers in a nonstandard directory <include dir>
|
||||
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
|
||||
nonstandard directory <lib dir>
|
||||
PKG_CONFIG path to pkg-config utility
|
||||
PKG_CONFIG_PATH
|
||||
directories to add to pkg-config's search path
|
||||
PKG_CONFIG_LIBDIR
|
||||
path overriding pkg-config's built-in search path
|
||||
|
||||
Use these variables to override the choices made by \`configure' or to help
|
||||
it to find libraries and programs with nonstandard names/locations.
|
||||
@@ -152,13 +185,6 @@ add_def () {
|
||||
fi
|
||||
}
|
||||
#}}}
|
||||
#{{{ pkg_config
|
||||
pkg_config () {
|
||||
type pkg-config > /dev/null 2> /dev/null || return 1
|
||||
|
||||
pkg-config $@ 2> /dev/null
|
||||
}
|
||||
#}}}
|
||||
#{{{ get_features
|
||||
get_features () {
|
||||
ff=1
|
||||
@@ -184,22 +210,24 @@ OPERATINGSYSTEM=`uname -s`
|
||||
VERSION=`uname -r`
|
||||
MACHINE=`uname -m`
|
||||
|
||||
LIBS=""
|
||||
EXTRA_LIBS=""
|
||||
EXTRA_CLI_LIBS=""
|
||||
EXTRA_OBJECTS=""
|
||||
EXTRA_DEFS=""
|
||||
SYSDEFS=""
|
||||
EXTRA_CLI_OBJECTS=""
|
||||
|
||||
feat_debug=0
|
||||
feat_cmdmon=1
|
||||
feat_ntp=1
|
||||
feat_refclock=1
|
||||
feat_readline=1
|
||||
try_readline=1
|
||||
try_editline=1
|
||||
feat_sechash=1
|
||||
try_nettle=1
|
||||
try_nss=1
|
||||
try_tomcrypt=1
|
||||
feat_nts=1
|
||||
try_gnutls=1
|
||||
feat_rtc=1
|
||||
try_rtc=0
|
||||
feat_droproot=1
|
||||
@@ -208,9 +236,6 @@ try_clockctl=0
|
||||
feat_scfilter=0
|
||||
try_seccomp=-1
|
||||
priv_ops=""
|
||||
readline_lib=""
|
||||
readline_inc=""
|
||||
ncurses_lib=""
|
||||
feat_ipv6=1
|
||||
feat_phc=1
|
||||
try_phc=0
|
||||
@@ -225,9 +250,10 @@ feat_timestamping=1
|
||||
try_timestamping=0
|
||||
feat_ntp_signd=0
|
||||
ntp_era_split=""
|
||||
use_pthread=0
|
||||
default_user="root"
|
||||
default_hwclockfile=""
|
||||
default_pidfile="/var/run/chronyd.pid"
|
||||
default_pidfile="/var/run/chrony/chronyd.pid"
|
||||
default_rtcdevice="/dev/rtc"
|
||||
mail_program="/usr/lib/sendmail"
|
||||
|
||||
@@ -240,21 +266,9 @@ do
|
||||
--disable-readline )
|
||||
feat_readline=0
|
||||
;;
|
||||
--without-readline )
|
||||
try_readline=0
|
||||
;;
|
||||
--without-editline )
|
||||
try_editline=0
|
||||
;;
|
||||
--with-readline-library=* )
|
||||
readline_lib=-L`echo $option | sed -e 's/^.*=//;'`
|
||||
;;
|
||||
--with-readline-includes=* )
|
||||
readline_inc=-I`echo $option | sed -e 's/^.*=//;'`
|
||||
;;
|
||||
--with-ncurses-library=* )
|
||||
ncurses_lib=-L`echo $option | sed -e 's/^.*=//;'`
|
||||
;;
|
||||
--prefix=* | --install_prefix=* )
|
||||
SETPREFIX=`echo $option | sed -e 's/[^=]*=//;'`
|
||||
;;
|
||||
@@ -360,12 +374,21 @@ do
|
||||
--disable-sechash )
|
||||
feat_sechash=0
|
||||
;;
|
||||
--without-nettle )
|
||||
try_nettle=0
|
||||
;;
|
||||
--without-nss )
|
||||
try_nss=0
|
||||
;;
|
||||
--without-tomcrypt )
|
||||
try_tomcrypt=0
|
||||
;;
|
||||
--disable-nts )
|
||||
feat_nts=0
|
||||
;;
|
||||
--without-gnutls )
|
||||
try_gnutls=0
|
||||
;;
|
||||
--host-system=* )
|
||||
OPERATINGSYSTEM=`echo $option | sed -e 's/^.*=//;'`
|
||||
;;
|
||||
@@ -390,7 +413,7 @@ SYSTEM=${OPERATINGSYSTEM}-${MACHINE}
|
||||
|
||||
case $OPERATINGSYSTEM in
|
||||
Linux)
|
||||
EXTRA_OBJECTS="sys_generic.o sys_linux.o sys_timex.o"
|
||||
EXTRA_OBJECTS="sys_generic.o sys_linux.o sys_timex.o sys_posix.o"
|
||||
[ $try_libcap != "0" ] && try_libcap=1
|
||||
try_rtc=1
|
||||
[ $try_seccomp != "0" ] && try_seccomp=1
|
||||
@@ -405,7 +428,9 @@ case $OPERATINGSYSTEM in
|
||||
# recvmmsg() seems to be broken on FreeBSD 11.0 and it's just
|
||||
# a wrapper around recvmsg()
|
||||
try_recvmmsg=0
|
||||
EXTRA_OBJECTS="sys_generic.o sys_netbsd.o sys_timex.o"
|
||||
EXTRA_OBJECTS="sys_generic.o sys_netbsd.o sys_timex.o sys_posix.o"
|
||||
try_setsched=1
|
||||
try_lockmem=1
|
||||
add_def FREEBSD
|
||||
if [ $feat_droproot = "1" ]; then
|
||||
add_def FEAT_PRIVDROP
|
||||
@@ -414,15 +439,16 @@ case $OPERATINGSYSTEM in
|
||||
echo "Configuring for $SYSTEM"
|
||||
;;
|
||||
NetBSD)
|
||||
EXTRA_OBJECTS="sys_generic.o sys_netbsd.o sys_timex.o"
|
||||
EXTRA_OBJECTS="sys_generic.o sys_netbsd.o sys_timex.o sys_posix.o"
|
||||
try_clockctl=1
|
||||
try_setsched=1
|
||||
try_lockmem=1
|
||||
add_def NETBSD
|
||||
echo "Configuring for $SYSTEM"
|
||||
;;
|
||||
Darwin)
|
||||
EXTRA_OBJECTS="sys_macosx.o"
|
||||
EXTRA_LIBS="-lresolv"
|
||||
EXTRA_CLI_LIBS="-lresolv"
|
||||
LIBS="$LIBS -lresolv"
|
||||
add_def MACOSX
|
||||
if [ $feat_droproot = "1" ]; then
|
||||
add_def FEAT_PRIVDROP
|
||||
@@ -440,9 +466,10 @@ case $OPERATINGSYSTEM in
|
||||
echo "Configuring for macOS (" $SYSTEM "macOS version" $VERSION ")"
|
||||
;;
|
||||
SunOS)
|
||||
EXTRA_OBJECTS="sys_generic.o sys_solaris.o sys_timex.o"
|
||||
EXTRA_LIBS="-lsocket -lnsl -lresolv"
|
||||
EXTRA_CLI_LIBS="-lsocket -lnsl -lresolv"
|
||||
EXTRA_OBJECTS="sys_generic.o sys_solaris.o sys_timex.o sys_posix.o"
|
||||
LIBS="$LIBS -lsocket -lnsl -lresolv"
|
||||
try_setsched=1
|
||||
try_lockmem=1
|
||||
add_def SOLARIS
|
||||
# These are needed to have msg_control in struct msghdr
|
||||
add_def __EXTENSIONS__
|
||||
@@ -472,7 +499,7 @@ fi
|
||||
|
||||
if [ $feat_ntp = "1" ]; then
|
||||
add_def FEAT_NTP
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS ntp_core.o ntp_io.o ntp_sources.o"
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS ntp_auth.o ntp_core.o ntp_ext.o ntp_io.o ntp_sources.o"
|
||||
if [ $feat_ntp_signd = "1" ]; then
|
||||
add_def FEAT_SIGND
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS ntp_signd.o"
|
||||
@@ -540,6 +567,16 @@ if [ "x$MYCC" = "xgcc" ] || [ "x$MYCC" = "xclang" ]; then
|
||||
MYCFLAGS="$MYCFLAGS -Wmissing-prototypes -Wall"
|
||||
fi
|
||||
|
||||
if [ "x$PKG_CONFIG" = "x" ]; then
|
||||
PKG_CONFIG=pkg-config
|
||||
fi
|
||||
|
||||
if ! test_executable "pkg-config" $PKG_CONFIG --version; then
|
||||
try_nettle=0
|
||||
try_nss=0
|
||||
try_gnutls=0
|
||||
fi
|
||||
|
||||
if test_code '64-bit time_t' 'time.h' '' '' '
|
||||
char x[sizeof(time_t) > 4 ? 1 : -1] = {0};
|
||||
return x[0];'
|
||||
@@ -550,7 +587,11 @@ then
|
||||
split_seconds=$ntp_era_split
|
||||
split_days=0
|
||||
else
|
||||
split_seconds=`date '+%s'`
|
||||
if [ "x$SOURCE_DATE_EPOCH" != "x" ]; then
|
||||
split_seconds=$SOURCE_DATE_EPOCH
|
||||
else
|
||||
split_seconds=`date '+%s'`
|
||||
fi
|
||||
if [ "x$split_seconds" = "x" ]; then
|
||||
echo "error: could not get current time, --with-ntp-era option is needed"
|
||||
exit 1
|
||||
@@ -575,25 +616,15 @@ then
|
||||
fi
|
||||
|
||||
MATHCODE='return (int) pow(2.0, log(sqrt((double)argc)));'
|
||||
if test_code 'math' 'math.h' '' '' "$MATHCODE"; then
|
||||
LIBS=""
|
||||
else
|
||||
if ! test_code 'math' 'math.h' '' '' "$MATHCODE"; then
|
||||
if test_code 'math in -lm' 'math.h' '' '-lm' "$MATHCODE"; then
|
||||
LIBS="-lm"
|
||||
LIBS="$LIBS -lm"
|
||||
else
|
||||
echo "error: could not compile/link a program which uses sqrt(), log(), pow()"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if test_code '<stdint.h>' 'stdint.h' '' '' ''; then
|
||||
add_def HAVE_STDINT_H
|
||||
fi
|
||||
|
||||
if test_code '<inttypes.h>' 'inttypes.h' '' '' ''; then
|
||||
add_def HAVE_INTTYPES_H
|
||||
fi
|
||||
|
||||
if test_code 'struct in_pktinfo' 'sys/socket.h netinet/in.h' '' '' '
|
||||
struct in_pktinfo ipi;
|
||||
return sizeof (ipi.ipi_spec_dst.s_addr) + IP_PKTINFO;'
|
||||
@@ -602,10 +633,11 @@ then
|
||||
fi
|
||||
|
||||
if [ $feat_ipv6 = "1" ] && \
|
||||
test_code 'IPv6 support' 'arpa/inet.h sys/socket.h netinet/in.h' '' "$EXTRA_LIBS" '
|
||||
test_code 'IPv6 support' 'arpa/inet.h sys/socket.h netinet/in.h' '' "$LIBS" '
|
||||
struct sockaddr_in6 n;
|
||||
char p[100];
|
||||
n.sin6_addr = in6addr_any;
|
||||
n.sin6_scope_id = 0;
|
||||
return !inet_ntop(AF_INET6, &n.sin6_addr.s6_addr, p, sizeof(p));'
|
||||
then
|
||||
add_def FEAT_IPV6
|
||||
@@ -623,6 +655,20 @@ 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);'
|
||||
@@ -638,40 +684,42 @@ if [ $try_clock_gettime = "1" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if test_code 'getaddrinfo()' 'sys/types.h sys/socket.h netdb.h' '' "$EXTRA_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" ] && \
|
||||
test_code 'pthread' 'pthread.h' '-pthread' '' \
|
||||
'return pthread_create((void *)1, NULL, (void *)1, NULL);'
|
||||
test_code 'pthread' 'pthread.h' '-pthread' '' '
|
||||
pthread_t thread;
|
||||
return (int)pthread_create(&thread, NULL, (void *)1, NULL);'
|
||||
then
|
||||
add_def FEAT_ASYNCDNS
|
||||
add_def USE_PTHREAD_ASYNCDNS
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o"
|
||||
MYCFLAGS="$MYCFLAGS -pthread"
|
||||
use_pthread=1
|
||||
fi
|
||||
|
||||
if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
|
||||
add_def HAVE_ARC4RANDOM
|
||||
fi
|
||||
|
||||
if test_code 'getrandom()' 'stdlib.h sys/random.h' '' '' \
|
||||
'return getrandom(NULL, 256, 0);'; then
|
||||
add_def HAVE_GETRANDOM
|
||||
else
|
||||
if test_code 'getrandom()' 'stdlib.h sys/random.h' '' '' \
|
||||
'return getrandom(NULL, 256, 0);'; then
|
||||
add_def HAVE_GETRANDOM
|
||||
fi
|
||||
fi
|
||||
|
||||
RECVMMSG_CODE='
|
||||
struct mmsghdr hdr;
|
||||
return !recvmmsg(0, &hdr, 1, MSG_DONTWAIT, 0);'
|
||||
if [ $try_recvmmsg = "1" ]; then
|
||||
if test_code 'recvmmsg()' 'sys/socket.h' '' "$EXTRA_LIBS" "$RECVMMSG_CODE"; then
|
||||
if test_code 'recvmmsg()' 'sys/socket.h' '' "$LIBS" "$RECVMMSG_CODE"; then
|
||||
add_def HAVE_RECVMMSG
|
||||
else
|
||||
if test_code 'recvmmsg() with _GNU_SOURCE' 'sys/socket.h' '-D_GNU_SOURCE' \
|
||||
"$EXTRA_LIBS" "$RECVMMSG_CODE"
|
||||
"$LIBS" "$RECVMMSG_CODE"
|
||||
then
|
||||
add_def _GNU_SOURCE
|
||||
add_def HAVE_RECVMMSG
|
||||
@@ -696,6 +744,7 @@ then
|
||||
struct scm_ts_pktinfo pktinfo;
|
||||
pktinfo.if_index = pktinfo.pkt_length = 0;
|
||||
return pktinfo.if_index + pktinfo.pkt_length + HWTSTAMP_FILTER_NTP_ALL +
|
||||
SCM_TIMESTAMPING_PKTINFO +
|
||||
SOF_TIMESTAMPING_OPT_PKTINFO + SOF_TIMESTAMPING_OPT_TX_SWHW;'; then
|
||||
add_def HAVE_LINUX_TIMESTAMPING_RXFILTER_NTP 1
|
||||
add_def HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO 1
|
||||
@@ -705,11 +754,11 @@ fi
|
||||
|
||||
timepps_h=""
|
||||
if [ $feat_refclock = "1" ] && [ $feat_pps = "1" ]; then
|
||||
if test_code '<sys/timepps.h>' 'sys/timepps.h' '' '' ''; then
|
||||
if test_code '<sys/timepps.h>' 'inttypes.h time.h sys/timepps.h' '' '' ''; then
|
||||
timepps_h="sys/timepps.h"
|
||||
add_def HAVE_SYS_TIMEPPS_H
|
||||
else
|
||||
if test_code '<timepps.h>' 'timepps.h' '' '' ''; then
|
||||
if test_code '<timepps.h>' 'inttypes.h time.h timepps.h' '' '' ''; then
|
||||
timepps_h="timepps.h"
|
||||
add_def HAVE_TIMEPPS_H
|
||||
fi
|
||||
@@ -717,10 +766,11 @@ if [ $feat_refclock = "1" ] && [ $feat_pps = "1" ]; then
|
||||
fi
|
||||
|
||||
if [ "x$timepps_h" != "x" ] && \
|
||||
test_code 'PPSAPI' "string.h $timepps_h" '' '' '
|
||||
test_code 'PPSAPI' "inttypes.h string.h time.h $timepps_h" '' '' '
|
||||
pps_handle_t h = 0;
|
||||
pps_info_t i;
|
||||
struct timespec ts;
|
||||
ts.tv_sec = ts.tv_nsec = 0;
|
||||
return time_pps_fetch(h, PPS_TSFMT_TSPEC, &i, &ts);'
|
||||
then
|
||||
add_def FEAT_PPS
|
||||
@@ -749,10 +799,12 @@ if [ $feat_scfilter = "1" ] && [ $try_seccomp = "1" ] && \
|
||||
'seccomp_init(SCMP_ACT_KILL);'
|
||||
then
|
||||
add_def FEAT_SCFILTER
|
||||
# NAME2IPADDRESS shouldn't be enabled with other operations as the helper
|
||||
# process works on one request at the time and the async resolver could
|
||||
# block the main thread
|
||||
priv_ops="NAME2IPADDRESS RELOADDNS"
|
||||
if [ $feat_ntp = "1" ]; then
|
||||
# NAME2IPADDRESS shouldn't be enabled together with a privops operation
|
||||
# used by the main thread as the helper process works on one request at
|
||||
# a time and the async resolver would block the main thread
|
||||
priv_ops="NAME2IPADDRESS RELOADDNS"
|
||||
fi
|
||||
EXTRA_LIBS="$EXTRA_LIBS -lseccomp"
|
||||
fi
|
||||
|
||||
@@ -784,25 +836,34 @@ fi
|
||||
|
||||
if [ $try_setsched = "1" ] && \
|
||||
test_code \
|
||||
'sched_setscheduler()' \
|
||||
'sched.h' '' '' '
|
||||
'pthread_setschedparam()' \
|
||||
'pthread.h sched.h' '-pthread' '' '
|
||||
struct sched_param sched;
|
||||
sched_get_priority_max(SCHED_FIFO);
|
||||
sched_setscheduler(0, SCHED_FIFO, &sched);'
|
||||
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);'
|
||||
then
|
||||
add_def HAVE_SCHED_SETSCHEDULER
|
||||
add_def HAVE_PTHREAD_SETSCHEDPARAM
|
||||
use_pthread=1
|
||||
fi
|
||||
|
||||
if [ $try_lockmem = "1" ] && \
|
||||
test_code \
|
||||
'mlockall()' \
|
||||
'sys/mman.h sys/resource.h' '' '' '
|
||||
struct rlimit rlim;
|
||||
setrlimit(RLIMIT_MEMLOCK, &rlim);
|
||||
'sys/mman.h' '' '' '
|
||||
mlockall(MCL_CURRENT|MCL_FUTURE);'
|
||||
then
|
||||
add_def HAVE_MLOCKALL
|
||||
fi
|
||||
if [ $try_lockmem = "1" ] && \
|
||||
test_code \
|
||||
'setrlimit(RLIMIT_MEMLOCK, ...)' \
|
||||
'sys/resource.h' '' '' '
|
||||
struct rlimit rlim;
|
||||
rlim.rlim_max = rlim.rlim_cur = RLIM_INFINITY;
|
||||
setrlimit(RLIMIT_MEMLOCK, &rlim);'
|
||||
then
|
||||
add_def HAVE_SETRLIMIT_MEMLOCK
|
||||
fi
|
||||
|
||||
if [ $feat_forcednsretry = "1" ]
|
||||
then
|
||||
@@ -812,37 +873,11 @@ fi
|
||||
READLINE_LINK=""
|
||||
if [ $feat_readline = "1" ]; then
|
||||
if [ $try_editline = "1" ]; then
|
||||
if test_code editline 'stdio.h editline/readline.h' \
|
||||
"$readline_inc" "$readline_lib -ledit" \
|
||||
if test_code editline 'stdio.h editline/readline.h' '' '-ledit' \
|
||||
'add_history(readline("prompt"));'
|
||||
then
|
||||
add_def FEAT_READLINE
|
||||
add_def USE_EDITLINE
|
||||
MYCPPFLAGS="$MYCPPFLAGS $readline_inc"
|
||||
READLINE_LINK="$readline_lib -ledit"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "x$READLINE_LINK" = "x" ] && [ $try_readline = "1" ]; then
|
||||
if test_code readline 'stdio.h readline/readline.h readline/history.h' \
|
||||
"$readline_inc" "$readline_lib -lreadline" \
|
||||
'add_history(readline("prompt"));'
|
||||
then
|
||||
add_def FEAT_READLINE
|
||||
MYCPPFLAGS="$MYCPPFLAGS $readline_inc"
|
||||
READLINE_LINK="$readline_lib -lreadline"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "x$READLINE_LINK" = "x" ] && [ $try_readline = "1" ]; then
|
||||
if test_code 'readline with -lncurses' \
|
||||
'stdio.h readline/readline.h readline/history.h' \
|
||||
"$readline_inc" "$readline_lib $ncurses_lib -lreadline -lncurses" \
|
||||
'add_history(readline("prompt"));'
|
||||
then
|
||||
add_def FEAT_READLINE
|
||||
MYCPPFLAGS="$MYCPPFLAGS $readline_inc"
|
||||
READLINE_LINK="$readline_lib $ncurses_lib -lreadline -lncurses"
|
||||
READLINE_LINK="-ledit"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -852,16 +887,37 @@ fi
|
||||
HASH_OBJ="hash_intmd5.o"
|
||||
HASH_LINK=""
|
||||
|
||||
if [ $feat_sechash = "1" ] && [ $try_nss = "1" ]; then
|
||||
if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_nettle = "1" ]; then
|
||||
test_cflags="`pkg_config --cflags nettle`"
|
||||
test_link="`pkg_config --libs nettle`"
|
||||
if test_code 'nettle' 'nettle/nettle-meta.h nettle/sha2.h' \
|
||||
"$test_cflags" "$test_link" \
|
||||
'return nettle_hashes[0]->context_size;'
|
||||
then
|
||||
HASH_OBJ="hash_nettle.o"
|
||||
HASH_LINK="$test_link"
|
||||
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||
add_def FEAT_SECHASH
|
||||
|
||||
if test_code 'CMAC in nettle' 'nettle/cmac.h' "$test_cflags" "$test_link" \
|
||||
'cmac128_update(NULL, NULL, NULL, 0, NULL);'
|
||||
then
|
||||
add_def HAVE_CMAC
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS cmac_nettle.o"
|
||||
EXTRA_CLI_OBJECTS="$EXTRA_CLI_OBJECTS cmac_nettle.o"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_nss = "1" ]; then
|
||||
test_cflags="`pkg_config --cflags nss`"
|
||||
test_link="`pkg_config --libs-only-L nss` -lfreebl3"
|
||||
test_link="`pkg_config --libs-only-L nss` -lfreebl3 -lnssutil3"
|
||||
if test_code 'NSS' 'nss.h hasht.h nsslowhash.h' \
|
||||
"$test_cflags" "$test_link" \
|
||||
'NSSLOWHASH_Begin(NSSLOWHASH_NewContext(NSSLOW_Init(), HASH_AlgSHA512));'
|
||||
then
|
||||
HASH_OBJ="hash_nss.o"
|
||||
HASH_LINK="$test_link"
|
||||
LIBS="$LIBS $HASH_LINK"
|
||||
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||
add_def FEAT_SECHASH
|
||||
fi
|
||||
@@ -873,12 +929,62 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]
|
||||
then
|
||||
HASH_OBJ="hash_tomcrypt.o"
|
||||
HASH_LINK="-ltomcrypt"
|
||||
LIBS="$LIBS $HASH_LINK"
|
||||
MYCPPFLAGS="$MYCPPFLAGS -I/usr/include/tomcrypt"
|
||||
add_def FEAT_SECHASH
|
||||
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);'
|
||||
then
|
||||
if test_code 'SIV in nettle' \
|
||||
'nettle/siv-cmac.h' "" "$LIBS" \
|
||||
'siv_cmac_aes128_set_key(NULL, NULL);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_nettle.o"
|
||||
add_def HAVE_SIV
|
||||
add_def HAVE_NETTLE_SIV_CMAC
|
||||
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);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_gnutls.o"
|
||||
add_def HAVE_SIV
|
||||
else
|
||||
if test_code 'AES128 in nettle' 'nettle/aes.h' '' "$LIBS" \
|
||||
'aes128_set_encrypt_key(NULL, NULL);'
|
||||
then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_nettle.o"
|
||||
add_def HAVE_SIV
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if grep '#define HAVE_SIV' config.h > /dev/null; then
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS nts_ke_client.o nts_ke_server.o nts_ke_session.o"
|
||||
EXTRA_OBJECTS="$EXTRA_OBJECTS nts_ntp_auth.o nts_ntp_client.o nts_ntp_server.o"
|
||||
LIBS="$LIBS $test_link"
|
||||
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||
add_def FEAT_NTS
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $use_pthread = "1" ]; then
|
||||
MYCFLAGS="$MYCFLAGS -pthread"
|
||||
fi
|
||||
|
||||
SYSCONFDIR=/etc
|
||||
if [ "x$SETSYSCONFDIR" != "x" ]; then
|
||||
SYSCONFDIR=$SETSYSCONFDIR
|
||||
@@ -942,9 +1048,9 @@ add_def DEFAULT_USER "\"$default_user\""
|
||||
add_def DEFAULT_COMMAND_SOCKET "\"$CHRONYRUNDIR/chronyd.sock\""
|
||||
add_def MAIL_PROGRAM "\"$mail_program\""
|
||||
|
||||
common_features="`get_features IPV6 DEBUG`"
|
||||
common_features="`get_features SECHASH IPV6 DEBUG`"
|
||||
chronyc_features="`get_features READLINE`"
|
||||
chronyd_features="`get_features CMDMON NTP REFCLOCK RTC PRIVDROP SCFILTER SECHASH SIGND ASYNCDNS`"
|
||||
chronyd_features="`get_features CMDMON NTP REFCLOCK RTC PRIVDROP SCFILTER SIGND ASYNCDNS NTS`"
|
||||
add_def CHRONYC_FEATURES "\"$chronyc_features $common_features\""
|
||||
add_def CHRONYD_FEATURES "\"$chronyd_features $common_features\""
|
||||
echo "Features : $chronyd_features $chronyc_features $common_features"
|
||||
@@ -960,15 +1066,15 @@ add_def CHRONY_VERSION "\"${CHRONY_VERSION}\""
|
||||
for f in Makefile doc/Makefile test/unit/Makefile
|
||||
do
|
||||
echo Creating $f
|
||||
sed -e "s%@EXTRA_OBJECTS@%${EXTRA_OBJECTS}%;\
|
||||
sed -e "s%@EXTRA_OBJS@%${EXTRA_OBJECTS}%;\
|
||||
s%@EXTRA_CLI_OBJS@%${EXTRA_CLI_OBJECTS}%;\
|
||||
s%@CC@%${MYCC}%;\
|
||||
s%@CFLAGS@%${MYCFLAGS}%;\
|
||||
s%@CPPFLAGS@%${MYCPPFLAGS}%;\
|
||||
s%@LIBS@%${LIBS}%;\
|
||||
s%@LDFLAGS@%${MYLDFLAGS}%;\
|
||||
s%@LIBS@%${LIBS}%;\
|
||||
s%@EXTRA_LIBS@%${EXTRA_LIBS}%;\
|
||||
s%@EXTRA_CLI_LIBS@%${EXTRA_CLI_LIBS}%;\
|
||||
s%@HASH_OBJ@%${HASH_OBJ}%;\
|
||||
s%@SYSCONFDIR@%${SYSCONFDIR}%;\
|
||||
s%@BINDIR@%${BINDIR}%;\
|
||||
s%@SBINDIR@%${SBINDIR}%;\
|
||||
|
||||
1051
doc/chrony.conf.adoc
1051
doc/chrony.conf.adoc
File diff suppressed because it is too large
Load Diff
409
doc/chronyc.adoc
409
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
|
||||
// Copyright (C) Miroslav Lichvar 2009-2017, 2019-2020
|
||||
//
|
||||
// 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,7 +39,7 @@ running.
|
||||
If no commands are specified on the command line, *chronyc* will expect input
|
||||
from the user. The prompt _chronyc>_ will be displayed when it is being run
|
||||
from a terminal. If *chronyc*'s input or output are redirected from or to a file,
|
||||
the prompt is not shown.
|
||||
the prompt will not be shown.
|
||||
|
||||
There are two ways *chronyc* can access *chronyd*. One is the Internet
|
||||
Protocol (IPv4 or IPv6) and the other is a Unix domain socket, which is
|
||||
@@ -51,7 +51,8 @@ running under a non-root user), it will try to connect to 127.0.0.1 and then
|
||||
|
||||
Only the following monitoring commands, which do not affect the behaviour of
|
||||
*chronyd*, are allowed from the network: *activity*, *manual list*,
|
||||
*rtcdata*, *smoothing*, *sources*, *sourcestats*, *tracking*, *waitsync*. The
|
||||
*rtcdata*, *smoothing*, *sourcename*, *sources*, *sourcestats*, *tracking*,
|
||||
*waitsync*. The
|
||||
set of hosts from which *chronyd* will accept these commands can be configured
|
||||
with the <<chrony.conf.adoc#cmdallow,*cmdallow*>> directive in the *chronyd*'s
|
||||
configuration file or the <<cmdallow,*cmdallow*>> command in *chronyc*. By
|
||||
@@ -59,9 +60,7 @@ default, the commands are accepted only from localhost (127.0.0.1 or ::1).
|
||||
|
||||
All other commands are allowed only through the Unix domain socket. When sent
|
||||
over the network, *chronyd* will respond with a '`Not authorised`' error, even
|
||||
if it is from localhost. In chrony versions before 2.2 they were allowed
|
||||
from the network if they were authenticated with a password, but that is no
|
||||
longer supported.
|
||||
if it is from localhost.
|
||||
|
||||
Having full access to *chronyd* via *chronyc* is more or less equivalent to
|
||||
being able to modify the *chronyd*'s configuration file and restart it.
|
||||
@@ -78,11 +77,17 @@ With this option hostnames will be resolved only to IPv6 addresses.
|
||||
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 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)
|
||||
format. IP addresses will not be resolved to hostnames, time will be printed as
|
||||
number of seconds since the epoch and values in seconds will not be converted
|
||||
to other units.
|
||||
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.
|
||||
|
||||
*-d*::
|
||||
This option enables printing of debugging messages if *chronyc* was compiled
|
||||
@@ -112,10 +117,14 @@ This option is ignored and is provided only for compatibility.
|
||||
*-a*::
|
||||
This option is ignored and is provided only for compatibility.
|
||||
|
||||
*-v*::
|
||||
*-v*, *--version*::
|
||||
With this option *chronyc* displays its version number on the terminal and
|
||||
exits.
|
||||
|
||||
*--help*::
|
||||
With this option *chronyc* displays a help message on the terminal and
|
||||
exits.
|
||||
|
||||
== COMMANDS
|
||||
|
||||
This section describes each of the commands available within the *chronyc*
|
||||
@@ -175,10 +184,12 @@ 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 in server mode). The value reported
|
||||
reports to NTP clients when it is operating as a server). The value reported
|
||||
on this line is the difference due to this effect.
|
||||
*Last offset*:::
|
||||
This is the estimated local offset on the last clock update.
|
||||
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
|
||||
time sources.
|
||||
*RMS offset*:::
|
||||
This is a long-term average of the offset value.
|
||||
*Frequency*:::
|
||||
@@ -284,15 +295,18 @@ milliseconds.
|
||||
|
||||
=== Time sources
|
||||
|
||||
[[sources]]*sources* [*-v*]::
|
||||
[[sources]]*sources* [*-a*] [*-v*]::
|
||||
This command displays information about the current time sources that *chronyd*
|
||||
is accessing.
|
||||
+
|
||||
The optional argument *-v* can be specified, meaning _verbose_. In this case,
|
||||
If the *-a* option is specified, all sources are displayed, including those that
|
||||
do not have a known address yet. Such sources have an identifier in the format
|
||||
_ID#XXXXXXXXXX_, which can be used in other commands expecting a source address.
|
||||
+
|
||||
The *-v* option enables a verbose output. In this case,
|
||||
extra caption lines are shown as a reminder of the meanings of the columns.
|
||||
+
|
||||
----
|
||||
210 Number of sources = 3
|
||||
MS Name/IP address Stratum Poll Reach LastRx Last sample
|
||||
===============================================================================
|
||||
#* GPS0 0 4 377 11 -479ns[ -621ns] +/- 134ns
|
||||
@@ -306,18 +320,23 @@ The columns are as follows:
|
||||
This indicates the mode of the source. _^_ means a server, _=_ means a peer
|
||||
and _#_ indicates a locally connected reference clock.
|
||||
*S*:::
|
||||
This column indicates the state of the source.
|
||||
* _*_ indicates the source to which *chronyd* is currently synchronised.
|
||||
* _+_ indicates acceptable sources which are combined with the selected
|
||||
source.
|
||||
* _-_ indicates acceptable sources which are excluded by the combining
|
||||
algorithm.
|
||||
* _?_ indicates sources to which connectivity has been lost or whose packets
|
||||
do not pass all tests. It is also shown at start-up, until at least 3 samples
|
||||
have been gathered from it.
|
||||
* _x_ indicates a clock which *chronyd* thinks is a falseticker (i.e. its
|
||||
time is inconsistent with a majority of other sources).
|
||||
This column indicates the selection state of the source.
|
||||
* _*_ indicates the best source which is currently selected for
|
||||
synchronisation.
|
||||
* _+_ indicates other sources selected for synchronisation, which are combined
|
||||
with the best source.
|
||||
* _-_ indicates a source which is considered to be selectable for
|
||||
synchronisation, but not currently selected.
|
||||
* _x_ indicates a source which *chronyd* thinks is a falseticker (i.e. its
|
||||
time is inconsistent with a majority of other sources, or sources specified
|
||||
with the *trust* option).
|
||||
* _~_ indicates a source whose time appears to have too much variability.
|
||||
* _?_ indicates a source which is not considered to be selectable for
|
||||
synchronisation for other reasons (e.g. unreachable, not synchronised, or
|
||||
does not have enough measurements).
|
||||
{blank}:::
|
||||
The <<selectdata,*selectdata*>> command can be used to get more details about
|
||||
the selection state.
|
||||
*Name/IP address*:::
|
||||
This shows the name or the IP address of the source, or reference ID for reference
|
||||
clocks.
|
||||
@@ -338,8 +357,9 @@ register has 8 bits and is updated on every received or missed packet from
|
||||
the source. A value of 377 indicates that a valid reply was received for all
|
||||
from the last eight transmissions.
|
||||
*LastRx*:::
|
||||
This column shows how long ago the last sample was received from the source.
|
||||
This is normally in seconds. The letters _m_, _h_, _d_ or _y_ indicate
|
||||
This column shows how long ago the last good sample (which is shown in the next
|
||||
column) was received from the source. Measurements that failed some tests are
|
||||
ignored. This is normally in seconds. The letters _m_, _h_, _d_ or _y_ indicate
|
||||
minutes, hours, days, or years.
|
||||
*Last sample*:::
|
||||
This column shows the offset between the local clock and the source at the
|
||||
@@ -352,18 +372,21 @@ since. The number following the _+/-_ indicator shows the margin of error in
|
||||
the measurement. Positive offsets indicate that the local clock is ahead of
|
||||
the source.
|
||||
|
||||
[[sourcestats]]*sourcestats* [*-v*]::
|
||||
[[sourcestats]]*sourcestats* [*-a*] [*-v*]::
|
||||
The *sourcestats* command displays information about the drift rate and offset
|
||||
estimation process for each of the sources currently being examined by
|
||||
*chronyd*.
|
||||
+
|
||||
The optional argument *-v* can be specified, meaning _verbose_. In this case,
|
||||
If the *-a* option is specified, all sources are displayed, including those that
|
||||
do not have a known address yet. Such sources have an identifier in the format
|
||||
_ID#XXXXXXXXXX_, which can be used in other commands expecting a source address.
|
||||
+
|
||||
The *-v* option enables a verbose output. In this case,
|
||||
extra caption lines are shown as a reminder of the meanings of the columns.
|
||||
+
|
||||
An example report is:
|
||||
+
|
||||
----
|
||||
210 Number of sources = 1
|
||||
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
|
||||
===============================================================================
|
||||
foo.example.net 11 5 46m -0.001 0.045 1us 25us
|
||||
@@ -399,6 +422,98 @@ This is the estimated offset of the source.
|
||||
*Std Dev*:::
|
||||
This is the estimated sample standard deviation.
|
||||
|
||||
[[selectdata]]*selectdata* [*-a*] [*-v*]::
|
||||
The *selectdata* command displays information specific to the selection of time
|
||||
sources. If the *-a* option is specified, all sources are displayed, including
|
||||
those that do not have a known address yet. With the *-v* option, extra caption
|
||||
lines are shown as a reminder of the meanings of the columns.
|
||||
+
|
||||
An example of the output is shown below.
|
||||
+
|
||||
----
|
||||
S Name/IP Address Auth COpts EOpts Last Score Interval Leap
|
||||
=======================================================================
|
||||
D foo.example.net Y ----- --TR- 4 1.0 -61ms +62ms N
|
||||
* bar.example.net N ----- ----- 0 1.0 -6846us +7305us N
|
||||
+ baz.example.net N ----- ----- 10 1.0 -7381us +7355us N
|
||||
----
|
||||
+
|
||||
The columns are as follows:
|
||||
+
|
||||
*S*:::
|
||||
This column indicates the state of the source after the last source selection.
|
||||
It is similar to the state reported by the *sources* command, but more
|
||||
states are reported.
|
||||
{blank}:::
|
||||
The following states indicate the source is not considered selectable for
|
||||
synchronisation:
|
||||
* _N_ - has the *noselect* option.
|
||||
* _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).
|
||||
* _~_ - has a jitter larger than the maximum jitter (configured by the
|
||||
<<chrony.conf.adoc#maxjitter,*maxjitter*>> directive).
|
||||
* _w_ - waits for other sources to get out of the _M_ state.
|
||||
* _S_ - has older measurements than other sources.
|
||||
* _O_ - has a stratum equal or larger than the orphan stratum (configured by
|
||||
the <<chrony.conf.adoc#local,*local*>> directive).
|
||||
* _T_ - does not fully agree with sources that have the *trust* option.
|
||||
* _x_ - does not agree with other sources (falseticker).
|
||||
{blank}:::
|
||||
The following states indicate the source is considered selectable, but it is
|
||||
not currently used for synchronisation:
|
||||
* _W_ - waits for other sources to be selectable (required by the
|
||||
<<chrony.conf.adoc#minsources,*minsources*>> directive, or
|
||||
the *require* option of another source).
|
||||
* _P_ - another selectable source is preferred due to the *prefer* option.
|
||||
* _U_ - waits for a new measurement (after selecting a different best source).
|
||||
* _D_ - has, or recently had, a root distance which is too large to be combined
|
||||
with other sources (configured by the
|
||||
<<chrony.conf.adoc#combinelimit,*combinelimit*>> directive).
|
||||
{blank}:::
|
||||
The following states indicate the source is used for synchronisation of the
|
||||
local clock:
|
||||
* _+_ - combined with the best source.
|
||||
* _*_ - selected as the best source to update the reference data (e.g. root
|
||||
delay, root dispersion).
|
||||
*Name/IP address*:::
|
||||
This column shows the name or IP address of the source if it is an NTP server,
|
||||
or the reference ID if it is a reference clock.
|
||||
*Auth*:::
|
||||
This column indicites whether an authentication mechanism is enabled for the
|
||||
source. _Y_ means yes and _N_ means no.
|
||||
*COpts*:::
|
||||
This column displays the configured selection options of the source.
|
||||
* _N_ indicates the *noselect* option.
|
||||
* _P_ indicates the *prefer* option.
|
||||
* _T_ indicates the *trust* option.
|
||||
* _R_ indicates the *require* option.
|
||||
*EOpts*:::
|
||||
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.
|
||||
*Last*:::
|
||||
This column displays how long ago was the last measurement of the source made
|
||||
when the selection was performed.
|
||||
*Score*:::
|
||||
This column displays the current score against the source in the _*_ state. The
|
||||
scoring system avoids frequent reselection when multiple sources have a similar
|
||||
root distance. A value larger than 1 indicates this source was better than the
|
||||
_*_ source in recent selections. If the score reaches 10, the best source will
|
||||
be reselected and the scores will be reset to 1.
|
||||
*Interval*:::
|
||||
This column displays the lower and upper endpoint of the interval which was
|
||||
expected to contain the true offset of the local clock considering the root
|
||||
distance at the time of the selection.
|
||||
*Leap*:::
|
||||
This column displays the current leap status of the source.
|
||||
* _N_ indicates the normal status (no leap second).
|
||||
* _+_ indicates that a leap second will be inserted at the end of the month.
|
||||
* _-_ 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).
|
||||
|
||||
[[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
|
||||
@@ -439,10 +554,82 @@ the offline state.
|
||||
the name of the server or peer was not resolved to an address yet; this source is
|
||||
not visible in the *sources* and *sourcestats* reports.
|
||||
|
||||
[[authdata]]*authdata* [*-a*]::
|
||||
The *authdata* command displays information specific to authentication of NTP
|
||||
sources. If the *-a* option is specified, all sources are displayed, including
|
||||
those that do not have a known address yet. An example of the output is
|
||||
shown below.
|
||||
+
|
||||
----
|
||||
Name/IP address Mode KeyID Type KLen Last Atmp NAK Cook CLen
|
||||
=========================================================================
|
||||
foo.example.net NTS 1 15 256 135m 0 0 8 100
|
||||
bar.example.net SK 30 13 128 - 0 0 0 0
|
||||
baz.example.net - 0 0 0 - 0 0 0 0
|
||||
----
|
||||
+
|
||||
The columns are as follows:
|
||||
+
|
||||
*Name/IP address*:::
|
||||
This column shows the name or the IP address of the source.
|
||||
*Mode*:::
|
||||
This column shows which mechanism authenticates NTP packets received from the
|
||||
source. _NTS_ means Network Time Security, _SK_ means a symmetric key, and _-_
|
||||
means authentication is disabled.
|
||||
*KeyID*:::
|
||||
This column shows an identifier of the key used for authentication. With a
|
||||
symmetric key, it is the ID from the <<chrony.conf.adoc#keyfile,key file>>.
|
||||
With NTS, it is a number starting at zero and incremented by one with each
|
||||
successful key establishment using the NTS-KE protocol, i.e. it shows how many
|
||||
times the key establishment was performed with this source.
|
||||
*Type*:::
|
||||
This columns shows an identifier of the algorithm used for authentication.
|
||||
With a symmetric key, it is the hash function or cipher specified in the key
|
||||
file. With NTS, it is an authenticated encryption with associated data (AEAD)
|
||||
algorithm, which is negotiated in the NTS-KE protocol. The following values can
|
||||
be reported:
|
||||
* 1: MD5
|
||||
* 2: SHA1
|
||||
* 3: SHA256
|
||||
* 4: SHA384
|
||||
* 5: SHA512
|
||||
* 6: SHA3-224
|
||||
* 7: SHA3-256
|
||||
* 8: SHA3-384
|
||||
* 9: SHA3-512
|
||||
* 10: TIGER
|
||||
* 11: WHIRLPOOL
|
||||
* 13: AES128
|
||||
* 14: AES256
|
||||
* 15: AEAD-AES-SIV-CMAC-256
|
||||
*KLen*:::
|
||||
This column shows the length of the key in bits.
|
||||
*Last*:::
|
||||
This column shows how long ago the last successful key establishment was
|
||||
performed. It is in seconds, or letters _m_, _h_, _d_ or _y_ indicate minutes,
|
||||
hours, days, or years.
|
||||
*Atmp*:::
|
||||
This column shows the number of attempts to perform the key establishment since
|
||||
the last successful key establishment. A number larger than 1 indicates a
|
||||
problem with the network or server.
|
||||
*NAK*:::
|
||||
This column shows whether an NTS NAK was received since the last request.
|
||||
A NAK indicates that authentication failed on the server side due to
|
||||
*chronyd* using a cookie which is no longer valid and that it needs to perform
|
||||
the key establishment again in order to get new cookies.
|
||||
*Cook*:::
|
||||
This column shows the number of NTS cookies that *chronyd* currently has. If
|
||||
the key establishment was successful, a number smaller than 8 indicates a
|
||||
problem with the network or server.
|
||||
*CLen*:::
|
||||
This column shows the length in bytes of the NTS cookie which will be used in
|
||||
the next request.
|
||||
|
||||
[[ntpdata]]*ntpdata* [_address_]::
|
||||
The *ntpdata* command displays the last valid measurement and other
|
||||
NTP-specific information about the specified NTP source, or all NTP sources if
|
||||
no address was specified. An example of the output is shown below.
|
||||
NTP-specific information about the specified NTP source, or all NTP sources
|
||||
(with a known address) if no address was specified. An example of the output is
|
||||
shown below.
|
||||
+
|
||||
----
|
||||
Remote address : 203.0.113.15 (CB00710F)
|
||||
@@ -525,15 +712,13 @@ The number of all packets received from the source.
|
||||
*Total valid RX*:::
|
||||
The number of valid packets received from the source.
|
||||
|
||||
[[add_peer]]*add peer* _address_ [_option_]...::
|
||||
[[add_peer]]*add peer* _name_ [_option_]...::
|
||||
The *add peer* command allows a new NTP peer to be added whilst
|
||||
*chronyd* is running.
|
||||
+
|
||||
Following the words *add peer*, the syntax of the following
|
||||
parameters and options is similar to that for the
|
||||
parameters and options is identical to that for the
|
||||
<<chrony.conf.adoc#peer,*peer*>> directive in the configuration file.
|
||||
The following peer options can be set in the command: *port*, *minpoll*,
|
||||
*maxpoll*, *presend*, *maxdelayratio*, *maxdelay*, *key*.
|
||||
+
|
||||
An example of using this command is shown below.
|
||||
+
|
||||
@@ -541,15 +726,27 @@ An example of using this command is shown below.
|
||||
add peer foo.example.net minpoll 6 maxpoll 10 key 25
|
||||
----
|
||||
|
||||
[[add_server]]*add server* _address_ [_option_]...::
|
||||
[[add_pool]]*add pool* _name_ [_option_]...::
|
||||
The *add pool* command allows a pool of NTP servers to be added whilst
|
||||
*chronyd* is running.
|
||||
+
|
||||
Following the words *add pool*, the syntax of the following parameters and
|
||||
options is identical to that for the <<chrony.conf.adoc#pool,*pool*>>
|
||||
directive in the configuration file.
|
||||
+
|
||||
An example of using this command is shown below:
|
||||
+
|
||||
----
|
||||
add pool foo.example.net maxsources 3 iburst
|
||||
----
|
||||
|
||||
[[add_server]]*add server* _name_ [_option_]...::
|
||||
The *add server* command allows a new NTP server to be added whilst
|
||||
*chronyd* is running.
|
||||
+
|
||||
Following the words *add server*, the syntax of the following parameters and
|
||||
options is similar to that for the <<chrony.conf.adoc#server,*server*>>
|
||||
options is identical to that for the <<chrony.conf.adoc#server,*server*>>
|
||||
directive in the configuration file.
|
||||
The following server options can be set in the command: *port*, *minpoll*,
|
||||
*maxpoll*, *presend*, *maxdelayratio*, *maxdelay*, *key*.
|
||||
+
|
||||
An example of using this command is shown below:
|
||||
+
|
||||
@@ -600,7 +797,7 @@ alternative to the form with mask.
|
||||
_address_:::
|
||||
This is an IP address or a hostname. The burst command is applied only to
|
||||
that source.
|
||||
::
|
||||
{blank}::
|
||||
+
|
||||
If no _mask_ or _masked-address_ arguments are provided, every source will be
|
||||
matched.
|
||||
@@ -687,7 +884,8 @@ the loaded periods. The *offline* and *online* commands can be used to achieve
|
||||
this.
|
||||
+
|
||||
There are four forms of the *offline* command. The first form is a wildcard,
|
||||
meaning all sources. The second form allows an IP address mask and a masked
|
||||
meaning all sources (including sources that do not have a known address yet).
|
||||
The second form allows an IP address mask and a masked
|
||||
address to be specified. The third form uses CIDR notation. The fourth form
|
||||
uses an IP address or a hostname. These forms are illustrated below.
|
||||
+
|
||||
@@ -722,6 +920,14 @@ particular source or sources has been restored.
|
||||
+
|
||||
The syntax is identical to that of the <<offline,*offline*>> command.
|
||||
|
||||
[[onoffline]]
|
||||
*onoffline*::
|
||||
The *onoffline* command tells *chronyd* to switch all sources that have a known
|
||||
address to the online or
|
||||
offline status according to the current network configuration. A source is
|
||||
considered online if it is possible to send requests to it, i.e. a network
|
||||
route to the source is present.
|
||||
|
||||
[[polltarget]]*polltarget* _address_ _polltarget_::
|
||||
The *polltarget* command is used to modify the poll target for one of the
|
||||
current set of sources. It is equivalent to the *polltarget* option in the
|
||||
@@ -736,6 +942,20 @@ Sources that stop responding will be replaced with newly resolved addresses
|
||||
automatically after 8 polling intervals, but this command can still be useful
|
||||
to replace them immediately and not wait until they are marked as unreachable.
|
||||
|
||||
[[reload]]*reload* *sources*::
|
||||
The *reload sources* command causes *chronyd* to re-read all _*.sources_ files
|
||||
from the directories specified by the
|
||||
<<chrony.conf.adoc#sourcedir,*sourcedir*>> directive.
|
||||
|
||||
[[sourcename]]*sourcename* _address_::
|
||||
The *sourcename* command prints the original hostname or address that was
|
||||
specified for an NTP source in the configuration file, or the *add* command.
|
||||
This command is an alternative to the *-N* option, which can be useful in
|
||||
scripts.
|
||||
+
|
||||
Note that different NTP sources can share the same name, e.g. servers from a
|
||||
pool.
|
||||
|
||||
=== Manual time input
|
||||
|
||||
[[manual]]
|
||||
@@ -772,7 +992,7 @@ The columns are as as follows:
|
||||
. The regression residual at this point, in seconds. This allows '`outliers`'
|
||||
to be easily spotted, so that they can be deleted using the *manual delete*
|
||||
command.
|
||||
::
|
||||
{blank}::
|
||||
+
|
||||
The *delete* form of the command deletes a single sample. The parameter is the
|
||||
index of the sample, as shown in the first column of the output from *manual
|
||||
@@ -843,10 +1063,17 @@ This command can be used to examine the effect of a series of *allow*, *allow
|
||||
all*, *deny*, and *deny all* commands specified either via *chronyc*, or in
|
||||
*chronyd*'s configuration file.
|
||||
|
||||
[[clients]]*clients*::
|
||||
[[clients]]*clients* [*-p* _packets_] [*-k*] [*-r*]::
|
||||
This command shows a list of clients that have accessed the server, through
|
||||
either the NTP or command ports. It does not include accesses over
|
||||
the Unix domain command socket. There are no arguments.
|
||||
the NTP, command, or NTS-KE port. It does not include accesses over the Unix
|
||||
domain command socket.
|
||||
+
|
||||
The *-p* option specifies the minimum number of received NTP or command
|
||||
packets, or accepted NTS-KE connections, needed to include a client in the
|
||||
list. The default value is 0, i.e. all clients are reported. With the *-k*
|
||||
option the last four columns will show the NTS-KE accesses instead of command
|
||||
accesses. If the *-r* option is specified, *chronyd* will reset the counters of
|
||||
received and dropped packets or connections after reporting the current values.
|
||||
+
|
||||
An example of the output is:
|
||||
+
|
||||
@@ -871,20 +1098,22 @@ The columns are as follows:
|
||||
. The average interval between NTP packets.
|
||||
. The average interval between NTP packets after limiting the response rate.
|
||||
. Time since the last NTP packet was received
|
||||
. The number of command packets received from the client.
|
||||
. The number of command packets dropped to limit the response rate.
|
||||
. The average interval between command packets.
|
||||
. Time since the last command packet was received.
|
||||
. The number of command packets or NTS-KE connections received/accepted from
|
||||
the client.
|
||||
. The number of command packets or NTS-KE connections dropped to limit the
|
||||
response rate.
|
||||
. The average interval between command packets or NTS-KE connections.
|
||||
. Time since the last command packet or NTS-KE connection was
|
||||
received/accepted.
|
||||
|
||||
[[serverstats]]*serverstats*::
|
||||
The *serverstats* command displays how many valid NTP and command requests
|
||||
*chronyd* as a server received from clients, how many of them were dropped to
|
||||
limit the response rate as configured by the
|
||||
<<chrony.conf.adoc#ratelimit,*ratelimit*>> and
|
||||
<<chrony.conf.adoc#cmdratelimit,*cmdratelimit*>> directives, and how many
|
||||
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. An example of
|
||||
the output is shown below.
|
||||
<<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.
|
||||
+
|
||||
----
|
||||
NTP packets received : 1598
|
||||
@@ -892,6 +1121,9 @@ NTP packets dropped : 8
|
||||
Command packets received : 19
|
||||
Command packets dropped : 0
|
||||
Client log records dropped : 0
|
||||
NTS-KE connections accepted: 3
|
||||
NTS-KE connections dropped : 0
|
||||
Authenticated NTP packets : 189
|
||||
----
|
||||
|
||||
[[allow]]*allow* [*all*] [_subnet_]::
|
||||
@@ -901,11 +1133,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
|
||||
@@ -920,11 +1149,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
|
||||
@@ -1077,7 +1303,7 @@ more than 1 second away from the system clock):
|
||||
error).
|
||||
. Save the RTC parameters to the RTC file (specified with the
|
||||
<<chrony.conf.adoc#rtcfile,*rtcfile*>> directive in the configuration file).
|
||||
::
|
||||
{blank}::
|
||||
+
|
||||
The last step is done as a precaution against the computer suffering a power
|
||||
failure before either the daemon exits or the <<writertc,*writertc*>> command
|
||||
@@ -1110,23 +1336,38 @@ purged. An example of how to do this is shown below.
|
||||
----
|
||||
# mv /var/log/chrony/measurements.log /var/log/chrony/measurements1.log
|
||||
# chronyc cyclelogs
|
||||
# ls -l /var/log/chrony
|
||||
-rw-r--r-- 1 root root 0 Jun 8 18:17 measurements.log
|
||||
-rw-r--r-- 1 root root 12345 Jun 8 18:17 measurements1.log
|
||||
# rm -f measurements1.log
|
||||
# rm /var/log/chrony/measurements1.log
|
||||
----
|
||||
|
||||
[[dump]]*dump*::
|
||||
The *dump* command causes *chronyd* to write its current history of
|
||||
measurements for each of its sources to dump files in the directory specified
|
||||
in the configuration file by the <<chrony.conf.adoc#dumpdir,*dumpdir*>>
|
||||
directive and also write server NTS keys and client NTS cookies to the
|
||||
directory specified by the <<chrony.conf.adoc#ntsdumpdir1,*ntsdumpdir*>>
|
||||
directive. Note that *chronyd* does this automatically when it exits. This
|
||||
command is mainly useful for inspection of the history whilst *chronyd* is
|
||||
running.
|
||||
command is mainly useful for inspection whilst *chronyd* is running.
|
||||
|
||||
[[rekey]]*rekey*::
|
||||
The *rekey* command causes *chronyd* to re-read the key file specified in the
|
||||
configuration file by the <<chrony.conf.adoc#keyfile,*keyfile*>> directive.
|
||||
configuration file by the <<chrony.conf.adoc#keyfile,*keyfile*>> directive. It
|
||||
also re-reads the server NTS keys if
|
||||
<<chrony.conf.adoc#ntsdumpdir2,*ntsdumpdir*>> is specified and
|
||||
<<chrony.conf.adoc#ntsrotate,automatic rotation>> is disabled in the
|
||||
configuration file.
|
||||
|
||||
[[reset]]*reset* *sources*::
|
||||
The *reset sources* command causes *chronyd* to drop all measurements and
|
||||
switch to the unsynchronised state. This command can help *chronyd* with
|
||||
recovery when the measurements are known to be no longer valid or accurate,
|
||||
e.g. due to moving the computer to a different network, or resuming the
|
||||
computer from a low-power state (which resets the system clock). *chronyd* will
|
||||
drop the measurements automatically when it detects the clock has made an
|
||||
unexpected jump, but the detection is not completely reliable.
|
||||
|
||||
[[shutdown]]*shutdown*::
|
||||
The *shutdown* command causes *chronyd* to exit. This is equivalent to sending
|
||||
the process the SIGTERM signal.
|
||||
|
||||
=== Client commands
|
||||
|
||||
@@ -1176,10 +1417,10 @@ generated from the _/dev/urandom_ device and it is printed to standard output.
|
||||
+
|
||||
The command has three optional arguments. The first argument is the key number
|
||||
(by default 1), which will be specified with the *key* option of the *server*
|
||||
or *peer* directives in the configuration file. The second argument is the hash
|
||||
function (by default SHA1 or MD5 if SHA1 is not available) and the third
|
||||
argument is the number of bits the key should have, between 80 and 4096 bits
|
||||
(by default 160 bits).
|
||||
or *peer* directives in the configuration file. The second argument is the name
|
||||
of the hash function or cipher (by default SHA1, or MD5 if SHA1 is not
|
||||
available). The third argument is the length of the key in bits if a hash
|
||||
function was selected, between 80 and 4096 bits (by default 160 bits).
|
||||
+
|
||||
An example is:
|
||||
+
|
||||
@@ -1189,7 +1430,13 @@ keygen 73 SHA1 256
|
||||
+
|
||||
which generates a 256-bit SHA1 key with number 73. The printed line should
|
||||
then be securely transferred and added to the key files on both server and
|
||||
client, or peers.
|
||||
client, or peers. A different key should be generated for each client or peer.
|
||||
+
|
||||
An example using the AES128 cipher is:
|
||||
+
|
||||
----
|
||||
keygen 151 AES128
|
||||
----
|
||||
|
||||
[[exit]]*exit*::
|
||||
[[quit]]*quit*::
|
||||
|
||||
@@ -41,7 +41,7 @@ If no configuration directives are specified on the command line, *chronyd*
|
||||
will read them from a configuration file. The compiled-in default location of
|
||||
the file is _@SYSCONFDIR@/chrony.conf_.
|
||||
|
||||
Information messages and warnings will be logged to syslog.
|
||||
Informational messages, warnings, and errors will be logged to syslog.
|
||||
|
||||
== OPTIONS
|
||||
|
||||
@@ -55,20 +55,32 @@ IPv6 sockets will be created.
|
||||
|
||||
*-f* _file_::
|
||||
This option can be used to specify an alternate location for the configuration
|
||||
file (default _@SYSCONFDIR@/chrony.conf_).
|
||||
file. The compiled-in default value is _@SYSCONFDIR@/chrony.conf_.
|
||||
|
||||
*-n*::
|
||||
When run in this mode, the program will not detach itself from the terminal.
|
||||
|
||||
*-d*::
|
||||
When run in this mode, the program will not detach itself from the terminal,
|
||||
and all messages will be written to the terminal instead of syslog. When
|
||||
*chronyd* was compiled with debugging support, this option can be used twice to
|
||||
print also debugging messages.
|
||||
and all messages will be written to the terminal instead of syslog. If
|
||||
*chronyd* was compiled with enabled support for debugging, this option can be
|
||||
used twice to enable debug messages.
|
||||
|
||||
*-l* _file_::
|
||||
This option specifies a file which should be used for logging instead of syslog
|
||||
or terminal.
|
||||
This option enables writing of log messages to a file instead of syslog or the
|
||||
terminal.
|
||||
|
||||
*-L* _level_::
|
||||
This option specifies the minimum severity level of messages to be written to
|
||||
the log file, syslog, or terminal. The following levels can be specified:
|
||||
0 (informational), 1 (warning), 2 (non-fatal error), and 3 (fatal error). The
|
||||
default value is 0.
|
||||
|
||||
*-p*::
|
||||
When run in this mode, *chronyd* will print the configuration and exit. It will
|
||||
not detach from the terminal. This option can be used to verify the syntax of
|
||||
the configuration and get the whole configuration, even if it is split into
|
||||
multiple files and read by the *include* or *confdir* directive.
|
||||
|
||||
*-q*::
|
||||
When run in this mode, *chronyd* will set the system clock once and exit. It
|
||||
@@ -81,9 +93,9 @@ started without root privileges.
|
||||
|
||||
*-r*::
|
||||
This option will try to reload and then delete files containing sample
|
||||
histories for each of the servers and reference clocks being used. These
|
||||
histories are created by using the <<chronyc.adoc#dump,*dump*>> command in
|
||||
*chronyc*, or by setting the <<chrony.conf.adoc#dumponexit,*dumponexit*>>
|
||||
histories for each of the servers and reference clocks being used. The
|
||||
files are expected to be in the directory specified by the
|
||||
<<chrony.conf.adoc#dumpdir,*dumpdir*>>
|
||||
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
|
||||
@@ -125,47 +137,62 @@ running, but still allow it to adjust the frequency of the system clock.
|
||||
*-u* _user_::
|
||||
This option sets the name of the system user to which *chronyd* will switch
|
||||
after start in order to drop root privileges. It overrides the
|
||||
<<chrony.conf.adoc#user,*user*>> directive (default _@DEFAULT_USER@_).
|
||||
<<chrony.conf.adoc#user,*user*>> directive. The compiled-in default value is
|
||||
_@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.
|
||||
The child process retains root privileges, but can only perform a very limited
|
||||
range of privileged system calls on behalf of the parent.
|
||||
|
||||
*-U*::
|
||||
This option disables a check for root privileges to allow *chronyd* to be
|
||||
started under a non-root user, assuming the process will have all capabilities
|
||||
(e.g. provided by the service manager) and access to all files, directories,
|
||||
and devices, needed to operate correctly in the specified configuration. Note
|
||||
that different capabilities might be needed with different configurations and
|
||||
different Linux kernel versions. Starting *chronyd* under a non-root user is
|
||||
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 SYSSIG
|
||||
signal is thrown instead and in level 0 the filter is disabled (default 0).
|
||||
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.
|
||||
+
|
||||
It's recommended to enable the filter only when it's known to work on the
|
||||
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 may make different
|
||||
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.
|
||||
|
||||
*-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 (the default) to disable the thread time
|
||||
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.
|
||||
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.
|
||||
|
||||
*-x*::
|
||||
This option disables the control of the system clock. *chronyd* will not make
|
||||
any adjustments of the clock, but it will still track its offset and frequency
|
||||
relative to the estimated true time, and be able to operate as an NTP server.
|
||||
This allows *chronyd* to run without the capability to adjust or set the system
|
||||
clock (e.g. in some containers).
|
||||
This option disables the control of the system clock. *chronyd* will not try to
|
||||
make any adjustments of the clock. It will assume the clock is free running and
|
||||
still track its offset and frequency relative to the estimated true time. This
|
||||
option allows *chronyd* to be started without the capability to adjust or set
|
||||
the system clock (e.g. in some containers) to operate as an NTP server.
|
||||
|
||||
*-v*::
|
||||
*-v*, *--version*::
|
||||
With this option *chronyd* will print version number to the terminal and exit.
|
||||
|
||||
*-h*, *--help*::
|
||||
With this option *chronyd* will print a help message to the terminal and exit.
|
||||
|
||||
== FILES
|
||||
|
||||
_@SYSCONFDIR@/chrony.conf_
|
||||
|
||||
672
doc/faq.adoc
672
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
|
||||
// Copyright (C) Miroslav Lichvar 2014-2016, 2020
|
||||
//
|
||||
// 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
|
||||
@@ -24,17 +24,20 @@
|
||||
|
||||
=== How does `chrony` compare to `ntpd`?
|
||||
|
||||
`chronyd` was designed to work well in a wide range of conditions and it can
|
||||
usually synchronise the system clock faster and with better time accuracy. It
|
||||
doesn't implement some of the less useful NTP modes like broadcast client or
|
||||
multicast server/client.
|
||||
`chrony` and `ntpd` are two different implementations of the Network Time
|
||||
Protocol (NTP).
|
||||
|
||||
`chrony` is a newer implementation, which was designed to work well in a wider
|
||||
range of conditions. It can usually synchronise the system clock faster and
|
||||
with better time accuracy. It has many features, but it does not implement some
|
||||
of the less useful NTP modes like broadcast client or multicast server/client.
|
||||
|
||||
If your computer is connected to the Internet only for few minutes at a time,
|
||||
the network connection is often congested, you turn your computer off or
|
||||
suspend it frequently, the clock is not very stable (e.g. there are rapid
|
||||
changes in the temperature or it's a virtual machine), or you want to use NTP
|
||||
changes in the temperature or it is a virtual machine), or you want to use NTP
|
||||
on an isolated network with no hardware reference clocks in sight, `chrony`
|
||||
will probably work much better for you.
|
||||
will probably work better for you.
|
||||
|
||||
For a more detailed comparison of features and performance, see the
|
||||
https://chrony.tuxfamily.org/comparison.html[comparison page] on the `chrony`
|
||||
@@ -46,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.
|
||||
@@ -59,13 +64,13 @@ slewing, which would take a very long time. The `makestep` directive does
|
||||
that.
|
||||
|
||||
In order to keep the real-time clock (RTC) close to the true time, so the
|
||||
system time is reasonably close to the true time when it's initialised on the
|
||||
system time is reasonably close to the true time when it is initialised on the
|
||||
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
|
||||
http://www.pool.ntp.org/[pool.ntp.org] project, the minimal _chrony.conf_ file
|
||||
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:
|
||||
|
||||
----
|
||||
@@ -75,52 +80,43 @@ makestep 1 3
|
||||
rtcsync
|
||||
----
|
||||
|
||||
=== How do I make an NTP server from an NTP client?
|
||||
=== 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.
|
||||
|
||||
=== I have several computers on a LAN. Should be all clients of an external server?
|
||||
----
|
||||
allow 192.168.1.0/24
|
||||
----
|
||||
|
||||
The best configuration is usually to make one computer the server, with
|
||||
the others as clients of it. Add a `local` directive to the server's
|
||||
_chrony.conf_ file. This configuration will be better because
|
||||
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?
|
||||
|
||||
It depends on the requirements. Usually, the best configuration is to make one
|
||||
computer the server, with the others as clients of it. Add a `local` directive
|
||||
to the server's _chrony.conf_ file. This configuration will be better because
|
||||
|
||||
* the load on the external connection is less
|
||||
* the load on the external NTP server(s) is less
|
||||
* if your external connection goes down, the computers on the LAN
|
||||
will maintain a common time with each other.
|
||||
|
||||
=== Must I specify servers by IP address if DNS is not available on chronyd start?
|
||||
=== Must I specify servers by IP address if DNS is not available on `chronyd` start?
|
||||
|
||||
No. Starting from version 1.25, `chronyd` will keep trying to resolve
|
||||
No, `chronyd` will keep trying to resolve
|
||||
the names specified by the `server`, `pool`, and `peer` directives in an
|
||||
increasing interval until it succeeds. The `online` command can be issued from
|
||||
`chronyc` to force `chronyd` to try to resolve the names immediately.
|
||||
|
||||
=== How can I make `chronyd` more secure?
|
||||
|
||||
If you don't need to serve time to NTP clients or peers, you can add `port 0`
|
||||
to the _chrony.conf_ file to completely disable the NTP server functionality
|
||||
and prevent NTP requests from reaching `chronyd`. Starting from version 2.0,
|
||||
the NTP server port is open only when client access is allowed by the `allow`
|
||||
directive or command, an NTP peer is configured, or the `broadcast` directive
|
||||
is used.
|
||||
|
||||
If you don't need to use `chronyc` remotely, you can add the following
|
||||
directives to the configuration file to bind the command sockets to the
|
||||
loopback interface. This is done by default since version 2.0.
|
||||
|
||||
----
|
||||
bindcmdaddress 127.0.0.1
|
||||
bindcmdaddress ::1
|
||||
----
|
||||
|
||||
If you don't need to use `chronyc` at all or you need to run `chronyc` only
|
||||
If you do not need to use `chronyc`, or you want to run `chronyc` only
|
||||
under the root or _chrony_ user (which can access `chronyd` through a Unix
|
||||
domain socket since version 2.2), you can disable the internet command sockets
|
||||
completely by adding `cmdport 0` to the configuration file.
|
||||
domain socket), you can disable the IPv4 and IPv6 command sockets (by default
|
||||
listening on localhost) by adding `cmdport 0` to the configuration file.
|
||||
|
||||
You can specify an unprivileged user with the `-u` option, or the `user`
|
||||
directive in the _chrony.conf_ file, to which `chronyd` will switch after start
|
||||
@@ -133,29 +129,144 @@ a very limited range of privileged system calls on behalf of the parent.
|
||||
Also, if `chronyd` is compiled with support for the Linux secure computing
|
||||
(seccomp) facility, you can enable a system call filter with the `-F` option.
|
||||
It will significantly reduce the kernel attack surface and possibly prevent
|
||||
kernel exploits from the `chronyd` process if it's compromised. It's
|
||||
recommended to enable the filter only when it's known to work on the version of
|
||||
kernel exploits from the `chronyd` process if it is compromised. 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 may make different system calls.
|
||||
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
|
||||
`authselmode` 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's 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
|
||||
@@ -163,14 +274,19 @@ default values are 6 (64 seconds) for `minpoll`, 10 (1024 seconds) for
|
||||
`maxpoll` and 8 (samples) for `polltarget`. The default values should be used
|
||||
for general servers on the Internet. With your own NTP servers, or if you have
|
||||
permission to poll some servers more frequently, setting these options for
|
||||
shorter polling intervals may significantly improve the accuracy of the system
|
||||
clock.
|
||||
shorter polling intervals might significantly improve the accuracy of the
|
||||
system clock.
|
||||
|
||||
The optimal polling interval depends mainly on two factors, stability of the
|
||||
network latency and stability of the system clock (which mainly depends on the
|
||||
temperature sensitivity of the crystal oscillator and the maximum rate of the
|
||||
temperature change).
|
||||
|
||||
Generally, if the `sourcestats` command usually reports a small number of
|
||||
samples retained for a source (e.g. fewer than 16), a shorter polling interval
|
||||
should be considered. If the number of samples is usually at the maximum of 64,
|
||||
a longer polling interval might work better.
|
||||
|
||||
An example of the directive for an NTP server on the Internet that you are
|
||||
allowed to poll frequently could be
|
||||
|
||||
@@ -178,15 +294,15 @@ allowed to poll frequently could be
|
||||
server foo.example.net minpoll 4 maxpoll 6 polltarget 16
|
||||
----
|
||||
|
||||
An example using very short polling intervals for a server located in the same
|
||||
An example using shorter polling intervals with a server located in the same
|
||||
LAN could be
|
||||
|
||||
----
|
||||
server ntp.local minpoll 2 maxpoll 4 polltarget 30
|
||||
----
|
||||
|
||||
The maxdelay options are useful to ignore measurements with larger delay (e.g.
|
||||
due to congestion in the network) and improve the stability of the
|
||||
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
|
||||
|
||||
@@ -194,28 +310,179 @@ with local NTP server
|
||||
server ntp.local minpoll 2 maxpoll 4 polltarget 30 maxdelaydevratio 2
|
||||
----
|
||||
|
||||
If your server supports the interleaved mode, the `xleave` option should be
|
||||
added to the `server` directive in order to allow the server to send the
|
||||
client more accurate hardware or kernel transmit timestamps. When combined with
|
||||
local hardware timestamping, sub-microsecond accuracy may be possible. An
|
||||
example could be
|
||||
If your server supports the interleaved mode (e.g. it is running `chronyd`),
|
||||
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:
|
||||
|
||||
----
|
||||
server ntp.local minpoll 2 maxpoll 2 xleave
|
||||
server ntp.local minpoll 2 maxpoll 4 xleave
|
||||
----
|
||||
|
||||
When combined with local hardware timestamping, good network switches, and even
|
||||
shorter polling intervals, a sub-microsecond accuracy and stability of a few
|
||||
tens of nanoseconds might be possible. For example:
|
||||
|
||||
----
|
||||
server ntp.local minpoll 0 maxpoll 0 xleave
|
||||
hwtimestamp eth0
|
||||
----
|
||||
|
||||
For best stability, the CPU should be running at a constant frequency (i.e.
|
||||
disabled power saving and performance boosting). Energy-Efficient Ethernet
|
||||
(EEE) should be disabled in the network. The switches should be configured to
|
||||
prioritize NTP packets, especially if the network is expected to be heavily
|
||||
loaded. The `dscp` directive can be used to set the Differentiated Services
|
||||
Code Point in transmitted NTP packets if needed.
|
||||
|
||||
If it is acceptable for NTP clients in the network to send requests at a high
|
||||
rate, a sub-second polling interval can be specified. A median filter
|
||||
can be enabled in order to update the clock at a reduced rate with more stable
|
||||
measurements. For example:
|
||||
|
||||
----
|
||||
server ntp.local minpoll -6 maxpoll -6 filter 15 xleave
|
||||
hwtimestamp eth0 minpoll -6
|
||||
----
|
||||
|
||||
=== Does `chronyd` have an ntpdate mode?
|
||||
|
||||
Yes. With the `-q` option `chronyd` will set the system clock once and exit.
|
||||
With the `-Q` option it will print the measured offset without setting the
|
||||
clock. If you don't want to use a configuration file, NTP servers can be
|
||||
clock. If you do not want to use a configuration file, NTP servers can be
|
||||
specified on the command line. For example:
|
||||
|
||||
----
|
||||
# chronyd -q 'pool pool.ntp.org iburst'
|
||||
----
|
||||
|
||||
The command above would normally take about 5 seconds if the servers were
|
||||
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 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.
|
||||
|
||||
----
|
||||
# chronyd -q -t 1 'server pool.ntp.org iburst maxsamples 1'
|
||||
----
|
||||
|
||||
It is not recommended to run `chronyd` with the `-q` option periodically (e.g.
|
||||
from a cron job) as a replacement for the daemon mode, because it performs
|
||||
significantly worse (e.g. the clock is stepped and its frequency is not
|
||||
corrected). If you must run it this way and you are using a public NTP server,
|
||||
make sure `chronyd` does not always start around the first second of a minute,
|
||||
e.g. by adding a random sleep before the `chronyd` command. Public servers
|
||||
typically receive large bursts of requests around the first second as there is
|
||||
a large number of NTP clients started from cron with no delay.
|
||||
|
||||
=== Can `chronyd` be configured to control the clock like `ntpd`?
|
||||
|
||||
It is not possible to perfectly emulate `ntpd`, but there are some options that
|
||||
can configure `chronyd` to behave more like `ntpd` if there is a reason to
|
||||
prefer that.
|
||||
|
||||
In the following example the `minsamples` directive slows down the response to
|
||||
changes in the frequency and offset of the clock. The `maxslewrate` and
|
||||
`corrtimeratio` directives reduce the maximum frequency error due to an offset
|
||||
correction and the `maxdrift` directive reduces the maximum assumed frequency
|
||||
error of the clock. The `makestep` directive enables a step threshold and the
|
||||
`maxchange` directive enables a panic threshold. The `maxclockerror` directive
|
||||
increases the minimum dispersion rate.
|
||||
|
||||
----
|
||||
minsamples 32
|
||||
maxslewrate 500
|
||||
corrtimeratio 100
|
||||
maxdrift 500
|
||||
makestep 0.128 -1
|
||||
maxchange 1000 1 1
|
||||
maxclockerror 15
|
||||
----
|
||||
|
||||
Note that increasing `minsamples` might cause the offsets in the `tracking` and
|
||||
`sourcestats` reports/logs to be significantly smaller than the actual offsets
|
||||
and be unsuitable for monitoring.
|
||||
|
||||
=== Can NTP server be separated from NTP client?
|
||||
|
||||
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.
|
||||
|
||||
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?
|
||||
|
||||
With the `smoothtime` and `leapsecmode` directives it is possible to enable a
|
||||
server leap smear in order to hide leap seconds from clients and force them to
|
||||
follow a slow server's adjustment instead.
|
||||
|
||||
This feature should be used only in local networks and only when necessary,
|
||||
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 other sources (e.g. with the `leapsectz` directive in
|
||||
`chrony`) and they will not work correctly with a leap smearing server.
|
||||
|
||||
=== Does `chrony` support PTP?
|
||||
|
||||
No, the Precision Time Protocol (PTP) is not supported 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
|
||||
routers) in order to make more stable and accurate measurements. PTP relies on
|
||||
the hardware support. NTP does not rely on any support in the hardware, but if
|
||||
it had the same support as PTP, it could perform equally well.
|
||||
|
||||
On Linux, `chrony` supports hardware clocks that some NICs have for PTP. They
|
||||
are called PTP hardware clocks (PHC). They can be used as reference clocks
|
||||
(specified by the `refclock` directive) and for hardware timestamping of NTP
|
||||
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.
|
||||
|
||||
=== What happened to the `commandkey` and `generatecommandkey` directives?
|
||||
|
||||
They were removed in version 2.2. Authentication is no longer supported in the
|
||||
@@ -232,18 +499,17 @@ following questions.
|
||||
|
||||
=== Behind a firewall?
|
||||
|
||||
Check the `Reach` value printed by the ``chronyc``'s `sources` command. If it's
|
||||
zero, it means `chronyd` did not get any valid responses from the NTP server
|
||||
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 may be blocked. Try using a tool like `wireshark` or `tcpdump` to see
|
||||
if you're getting any responses from the server.
|
||||
packets 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
|
||||
@@ -253,9 +519,10 @@ MS Name/IP address Stratum Poll Reach LastRx Last sample
|
||||
|
||||
=== Are NTP servers specified with the `offline` option?
|
||||
|
||||
Check that you're using ``chronyc``'s `online` and `offline` commands
|
||||
appropriately. The `activity` command prints the number of sources that are
|
||||
currently online and offline. For example:
|
||||
Check that the ``chronyc``'s `online` and `offline` commands are used
|
||||
appropriately (e.g. in the system networking scripts). The `activity` command
|
||||
prints the number of sources that are currently online and offline. For
|
||||
example:
|
||||
|
||||
----
|
||||
200 OK
|
||||
@@ -266,6 +533,19 @@ currently online and offline. For 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
|
||||
@@ -282,9 +562,9 @@ makestep 1 3
|
||||
----
|
||||
|
||||
the clock would be stepped in the first three updates if its offset was larger
|
||||
than one second. Normally, it's recommended to allow the step only in the first
|
||||
than one second. Normally, it is recommended to allow the step only in the first
|
||||
few updates, but in some cases (e.g. a computer without an RTC or virtual
|
||||
machine which can be suspended and resumed with an incorrect time) it may be
|
||||
machine which can be suspended and resumed with an incorrect time) it might be
|
||||
necessary to allow the step on any clock update. The example above would change
|
||||
to
|
||||
|
||||
@@ -292,6 +572,139 @@ 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.
|
||||
|
||||
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
|
||||
dispersion (e.g. three seconds or more), which causes `chronyd` to ignore the
|
||||
server for being too inaccurate. The `sources` command might show a valid
|
||||
measurement, but the server is not selected for synchronisation. You can check
|
||||
the root dispersion of the server with the ``chronyc``'s `ntpdata` command.
|
||||
|
||||
The `maxdistance` value needs to be increased in _chrony.conf_ to enable
|
||||
synchronisation to such a server. For example:
|
||||
|
||||
----
|
||||
maxdistance 16.0
|
||||
----
|
||||
|
||||
=== An unreachable source is selected?
|
||||
|
||||
When `chronyd` is configured with multiple time sources, it tries to select the
|
||||
most accurate and stable sources for synchronisation of the system clock. They
|
||||
are marked with the _*_ or _+_ symbol in the report printed by the `sources`
|
||||
command.
|
||||
|
||||
When the best source (marked with the _*_ symbol) becomes unreachable (e.g. NTP
|
||||
server stops responding), `chronyd` will not immediately switch
|
||||
to the second best source in an attempt to minimise the error of the clock. It
|
||||
will let the clock run free for as long as its estimated error (in terms of
|
||||
root distance) based on previous measurements is smaller than the estimated
|
||||
error of the second source, and there is still an interval which contains some
|
||||
measurements from both sources.
|
||||
|
||||
If the first source was significantly better than the second source, it can
|
||||
take many hours before the second source is selected, depending on its polling
|
||||
interval. You can force a faster reselection by increasing the clock error rate
|
||||
(`maxclockerror` directive), shortening the polling interval (`maxpoll`
|
||||
option), or reducing the number of samples (`maxsamples` option).
|
||||
|
||||
=== Does selected source drop new measurements?
|
||||
|
||||
`chronyd` can drop a large number of successive NTP measurements if they are
|
||||
not passing some of the NTP tests. The `sources` command can report for a
|
||||
selected source the fully-reachable value of 377 in the Reach column and at the
|
||||
same time a LastRx value that is much larger than the current polling interval.
|
||||
If the source is online, this indicates that a number of measurements was
|
||||
dropped. You can use the `ntpdata` command to check the NTP tests for the last
|
||||
measurement. Usually, it is the test C which fails.
|
||||
|
||||
This can be an issue when there is a long-lasting increase in the measured
|
||||
delay, e.g. due to a routing change in the network. Unfortunately, `chronyd`
|
||||
does not know for how long it should wait for the delay to come back to the
|
||||
original values, or whether it is a permanent increase and it should start from
|
||||
scratch.
|
||||
|
||||
The test C is an adaptive filter. It can take many hours before it accepts
|
||||
a measurement with the larger delay, and even much longer before it drops all
|
||||
measurements with smaller delay, which determine an expected delay used by the
|
||||
test. You can use the `reset sources` command to drop all measurements
|
||||
immediately (available in chrony 4.0 and later). If this issue happens
|
||||
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 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
|
||||
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.
|
||||
|
||||
One approach to find out a good value of the `offset` option is to configure
|
||||
the reference clocks with the `noselect` option and compare them to an NTP
|
||||
server. For example, if the `sourcestats` command showed
|
||||
|
||||
----
|
||||
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
|
||||
==============================================================================
|
||||
PPS0 0 0 0 +0.000 2000.000 +0ns 4000ms
|
||||
NMEA 58 30 231 -96.494 38.406 +504ms 6080us
|
||||
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
|
||||
able to determine the seconds corresponding to the pulses and allow the samples
|
||||
to be used for synchronisation.
|
||||
|
||||
== Issues with `chronyc`
|
||||
|
||||
=== I keep getting the error `506 Cannot talk to daemon`
|
||||
@@ -299,31 +712,33 @@ makestep 1 -1
|
||||
When accessing `chronyd` remotely, make sure that the _chrony.conf_ file (on
|
||||
the computer where `chronyd` is running) has a `cmdallow` entry for the
|
||||
computer you are running `chronyc` on and an appropriate `bindcmdaddress`
|
||||
directive. This isn't necessary for localhost.
|
||||
directive. This is not necessary for localhost.
|
||||
|
||||
Perhaps `chronyd` is not running. Try using the `ps` command (e.g. on Linux,
|
||||
`ps -auxw`) to see if it's running. Or try `netstat -a` and see if the ports
|
||||
123/udp and 323/udp are listening. If `chronyd` is not running, you may have a
|
||||
problem with the way you are trying to start it (e.g. at boot time).
|
||||
`ps -auxw`) to see if it is running. Or try `netstat -a` and see if the UDP
|
||||
port 323 is listening. If `chronyd` is not running, you might have a problem
|
||||
with the way you are trying to start it (e.g. at boot time).
|
||||
|
||||
Perhaps you have a firewall set up in a way that blocks packets on port
|
||||
323/udp. You need to amend the firewall configuration in this case.
|
||||
Perhaps you have a firewall set up in a way that blocks packets on the UDP
|
||||
port 323. You need to amend the firewall configuration in this case.
|
||||
|
||||
=== I keep getting the error `501 Not authorised`
|
||||
|
||||
Since version 2.2, the `password` command doesn't do anything and `chronyc`
|
||||
needs to run locally under the root or _chrony_ user, which are allowed to
|
||||
access the ``chronyd``'s Unix domain command socket.
|
||||
This error indicates that `chronyc` sent the command to `chronyd` using a UDP
|
||||
socket instead of the Unix domain socket (e.g. _/var/run/chrony/chronyd.sock_),
|
||||
which is required for some commands. For security reasons, only the root and
|
||||
_chrony_ users are allowed to access the socket.
|
||||
|
||||
With older versions, you need to authenticate with the `password` command first
|
||||
or use the `-a` option to authenticate automatically on start. The
|
||||
configuration file needs to specify a file which contains keys (`keyfile`
|
||||
directive) and which key in the key file should be used for `chronyc`
|
||||
authentication (`commandkey` directive).
|
||||
It is also possible that the socket does not exist. `chronyd` will not create
|
||||
the socket if the directory has a wrong owner or permissions. In this case
|
||||
there should be an error message from `chronyd` in the system log.
|
||||
|
||||
=== Why does `chronyc tracking` always print an IPv4 address as reference ID?
|
||||
=== What is the reference ID reported by the `tracking` command?
|
||||
|
||||
The reference ID is a 32-bit value and in versions before 3.0 it was printed in
|
||||
The reference ID is a 32-bit value used in NTP to prevent synchronisation
|
||||
loops.
|
||||
|
||||
In `chrony` versions before 3.0 it was printed in the
|
||||
quad-dotted notation, even if the reference source did not actually have an
|
||||
IPv4 address. For IPv4 addresses, the reference ID is equal to the address, but
|
||||
for IPv6 addresses it is the first 32 bits of the MD5 sum of the address. For
|
||||
@@ -347,12 +762,12 @@ Only by the source code. See _cmdmon.c_ (`chronyd` side) and _client.c_
|
||||
=== What is the real-time clock (RTC)?
|
||||
|
||||
This is the clock which keeps the time even when your computer is turned off.
|
||||
It is used to initialise the system clock on boot. It normally doesn't drift
|
||||
It is used to initialise the system clock on boot. It normally does not drift
|
||||
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.
|
||||
@@ -362,17 +777,17 @@ monitor the rate at which the RTC gains or loses time. When `chronyd` is
|
||||
started with the `-s` option on the next boot, it will set the system time from
|
||||
the RTC and also compensate for the drift it has measured previously. The
|
||||
`rtcautotrim` directive can be used to keep the RTC close to the true time, but
|
||||
it's not strictly necessary if its only purpose is to set the system clock when
|
||||
it is not strictly necessary if its only purpose is to set the system clock when
|
||||
`chronyd` is started on boot. See the documentation for details.
|
||||
|
||||
=== I want to use ``chronyd``'s RTC support. Must I disable `hwclock`?
|
||||
=== 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's important to disable `hwclock` in the shutdown
|
||||
procedure. If you don't, it will over-write the RTC with a new value, unknown
|
||||
(`rtcfile` directive), it is important to disable `hwclock` in the shutdown
|
||||
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.
|
||||
@@ -391,24 +806,53 @@ things
|
||||
|
||||
=== I get `Could not open /dev/rtc, Device or resource busy` in my syslog file
|
||||
|
||||
Some other program running on the system may be using the device.
|
||||
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
|
||||
last modification time of the drift file, which should correspond to the system
|
||||
time when `chronyd` was previously stopped. The initial system time will be
|
||||
increasing across reboots and applications started after `chronyd` will not
|
||||
observe backward steps.
|
||||
|
||||
== NTP-specific issues
|
||||
|
||||
=== Can `chronyd` be driven from broadcast NTP servers?
|
||||
=== Can `chronyd` be driven from broadcast/multicast NTP servers?
|
||||
|
||||
No, the broadcast client mode is not supported and there is currently no plan
|
||||
to implement it. The broadcast and multicast modes are inherently less
|
||||
accurate and less secure (even with authentication) than the ordinary
|
||||
server/client mode and they are not as useful as they used to be. Even with
|
||||
very modest hardware a single NTP server can serve time to hundreds of
|
||||
thousands of clients using the ordinary mode.
|
||||
No, the broadcast/multicast client mode is not supported and there is currently
|
||||
no plan to implement it. While this mode can simplify configuration
|
||||
of clients in large networks, it is inherently less accurate and less secure
|
||||
(even with authentication) than the ordinary client/server mode.
|
||||
|
||||
When configuring a large number of clients in a network, it is recommended to
|
||||
use the `pool` directive with a DNS name which resolves to addresses of
|
||||
multiple NTP servers. The clients will automatically replace the servers when
|
||||
they become unreachable, or otherwise unsuitable for synchronisation, with new
|
||||
servers from the pool.
|
||||
|
||||
Even with very modest hardware, an NTP server can serve time to hundreds of
|
||||
thousands of clients using the ordinary client/server mode.
|
||||
|
||||
=== Can `chronyd` transmit broadcast NTP packets?
|
||||
|
||||
Yes, the `broadcast` directive can be used to enable the broadcast server mode
|
||||
to serve time to clients in the network which support the broadcast client mode
|
||||
(it's not supported in `chronyd`, see the previous question).
|
||||
(it is not supported in `chronyd`). Note that this mode should generally be
|
||||
avoided. See the previous question.
|
||||
|
||||
=== Can `chronyd` keep the system clock a fixed offset away from real time?
|
||||
|
||||
@@ -424,9 +868,21 @@ offline, `chronyd` would make new measurements immediately after issuing the
|
||||
`online` command.
|
||||
|
||||
Unless the network connection lasts only few minutes (less than the maximum
|
||||
polling interval), the delay is usually not a problem, and it may be acceptable
|
||||
polling interval), the delay is usually not a problem, and it might be acceptable
|
||||
to keep all sources online all the time.
|
||||
|
||||
=== Why is an offset measured between two computers synchronised to each another?
|
||||
|
||||
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 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.
|
||||
|
||||
== Operating systems
|
||||
|
||||
=== Does `chrony` support Windows?
|
||||
|
||||
@@ -22,6 +22,26 @@ The software is distributed as source code which has to be compiled. The source
|
||||
code is supplied in the form of a gzipped tar file, which unpacks to a
|
||||
subdirectory identifying the name and version of the program.
|
||||
|
||||
A C compiler (e.g. `gcc` or `clang`) and GNU Make are needed to build `chrony`.
|
||||
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`)
|
||||
* libcap: dropping root privileges on Linux (`DROPROOT`)
|
||||
* libseccomp: system call filter on Linux (`SCFILTER`)
|
||||
* GnuTLS and Nettle: Network Time Security (`NTS`)
|
||||
* Editline: line editing in `chronyc` (`READLINE`)
|
||||
* timepps.h header: PPS reference clock
|
||||
* Asciidoctor: documentation in HTML format
|
||||
* Bash: test suite
|
||||
|
||||
The following programs are needed when building `chrony` from the git
|
||||
repository instead of a released tar file:
|
||||
|
||||
* Asciidoctor: manual pages
|
||||
* Bison: parser for chronyc settime command
|
||||
|
||||
After unpacking the source code, change directory into it, and type
|
||||
|
||||
----
|
||||
@@ -29,8 +49,8 @@ After unpacking the source code, change directory into it, and type
|
||||
----
|
||||
|
||||
This is a shell script that automatically determines the system type. There is
|
||||
a single optional parameter, `--prefix` which indicates the directory tree
|
||||
where the software should be installed. For example,
|
||||
an optional parameter `--prefix`, which indicates the directory tree where the
|
||||
software should be installed. For example,
|
||||
|
||||
----
|
||||
./configure --prefix=/opt/free
|
||||
@@ -40,11 +60,11 @@ will install the `chronyd` daemon into `/opt/free/sbin` and the `chronyc`
|
||||
control program into `/opt/free/bin`. The default value for the prefix is
|
||||
`/usr/local`.
|
||||
|
||||
The configure script assumes you want to use gcc as your compiler. If you want
|
||||
to use a different compiler, you can configure this way:
|
||||
The `configure` script assumes you want to use `gcc` as your compiler. If you
|
||||
want to use a different compiler, you can configure this way:
|
||||
|
||||
----
|
||||
CC=cc CFLAGS=-O ./configure --prefix=/opt/free
|
||||
CC=cc ./configure --prefix=/opt/free
|
||||
----
|
||||
|
||||
for Bourne-family shells, or
|
||||
@@ -63,11 +83,26 @@ shown. Otherwise, `Makefile` will be generated.
|
||||
On Linux, if development files for the libcap library are available, `chronyd`
|
||||
will be built with support for dropping root privileges. On other systems no
|
||||
extra library is needed. The default user which `chronyd` should run as can be
|
||||
specified with the `--with-user` option of the configure script.
|
||||
specified with the `--with-user` option of the `configure` script.
|
||||
|
||||
If development files for the editline or readline library are available,
|
||||
If development files for the POSIX threads library are available, `chronyd`
|
||||
will be built with support for asynchronous resolving of hostnames specified in
|
||||
the `server`, `peer`, and `pool` directives. This allows `chronyd` operating as
|
||||
a server to respond to client requests when resolving a hostname. If you don't
|
||||
want to enable the support, specify the `--disable-asyncdns` flag to
|
||||
`configure`.
|
||||
|
||||
If development files for the https://www.lysator.liu.se/~nisse/nettle/[Nettle],
|
||||
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS[NSS], or
|
||||
https://www.libtom.net/LibTomCrypt/[libtomcrypt] library are available,
|
||||
`chronyd` will be built with support for other cryptographic hash functions
|
||||
than MD5, which can be used for NTP authentication with a symmetric key. If you
|
||||
don't want to enable the support, specify the `--disable-sechash` flag to
|
||||
`configure`.
|
||||
|
||||
If development files for the editline library are available,
|
||||
`chronyc` will be built with line editing support. If you don't want this,
|
||||
specify the `--disable-readline` flag to configure.
|
||||
specify the `--disable-readline` flag to `configure`.
|
||||
|
||||
If a `timepps.h` header is available (e.g. from the
|
||||
http://linuxpps.org[LinuxPPS project]), `chronyd` will be built with PPS API
|
||||
@@ -75,6 +110,9 @@ reference clock driver. If the header is installed in a location that isn't
|
||||
normally searched by the compiler, you can add it to the searched locations by
|
||||
setting the `CPPFLAGS` variable to `-I/path/to/timepps`.
|
||||
|
||||
The `--help` option can be specified to `configure` to print all options
|
||||
supported by the script.
|
||||
|
||||
Now type
|
||||
|
||||
----
|
||||
@@ -122,51 +160,24 @@ unprivileged user for `chronyd` and specify it with the `-u` command-line
|
||||
option or the `user` directive in the configuration file, or set the default
|
||||
user with the `--with-user` configure option before building.
|
||||
|
||||
== Support for line editing libraries
|
||||
== Support for system call filtering
|
||||
|
||||
`chronyc` can be built with support for line editing, this allows you to use
|
||||
the cursor keys to replay and edit old commands. Two libraries are supported
|
||||
which provide such functionality, editline and GNU readline.
|
||||
|
||||
Please note that readline since version 6.0 is licensed under GPLv3+ which is
|
||||
incompatible with chrony's license GPLv2. You should use editline instead if
|
||||
you don't want to use older readline versions.
|
||||
|
||||
The configure script will automatically enable the line editing support if one
|
||||
of the supported libraries is available. If they are both available, the
|
||||
editline library will be used.
|
||||
|
||||
If you don't want to use it (in which case chronyc will use a minimal command
|
||||
line interface), invoke configure like this:
|
||||
|
||||
----
|
||||
./configure --disable-readline other-options...
|
||||
----
|
||||
|
||||
If you have editline, readline or ncurses installed in locations that aren't
|
||||
normally searched by the compiler and linker, you need to use extra options:
|
||||
|
||||
`--with-readline-includes=directory_name`::
|
||||
This defines the name of the directory above the one where `readline.h` is.
|
||||
`readline.h` is assumed to be in `editline` or `readline` subdirectory of the
|
||||
named directory.
|
||||
|
||||
`--with-readline-library=directory_name`::
|
||||
This defines the directory containing the `libedit.a` or `libedit.so` file,
|
||||
or `libreadline.a` or `libreadline.so` file.
|
||||
|
||||
`--with-ncurses-library=directory_name`::
|
||||
This defines the directory containing the `libncurses.a` or `libncurses.so`
|
||||
file.
|
||||
`chronyd` can be built with support for the Linux secure computing (seccomp)
|
||||
facility. This requires development files for the
|
||||
https://github.com/seccomp/libseccomp[libseccomp] library and the
|
||||
`--enable-scfilter` option specified to `configure`. The `-F` option of
|
||||
`chronyd` will enable a system call filter, which should significantly reduce
|
||||
the kernel attack surface and possibly prevent kernel exploits from `chronyd`
|
||||
if it is compromised.
|
||||
|
||||
== Extra options for package builders
|
||||
|
||||
The configure and make procedures have some extra options that may be useful if
|
||||
you are building a distribution package for chrony.
|
||||
The `configure` and `make` procedures have some extra options that may be
|
||||
useful if you are building a distribution package for `chrony`.
|
||||
|
||||
The `--mandir=DIR` option to configure specifies an install directory for the
|
||||
man pages. This overrides the `man` subdirectory of the argument to the
|
||||
--prefix option.
|
||||
The `--mandir=DIR` option to `configure` specifies an installation directory
|
||||
for the man pages. This overrides the `man` subdirectory of the argument to the
|
||||
`--prefix` option.
|
||||
|
||||
----
|
||||
./configure --prefix=/usr --mandir=/usr/share/man
|
||||
@@ -174,8 +185,8 @@ man pages. This overrides the `man` subdirectory of the argument to the
|
||||
|
||||
to set both options together.
|
||||
|
||||
The final option is the `DESTDIR` option to the make command. For example, you
|
||||
could use the commands
|
||||
The final option is the `DESTDIR` option to the `make` command. For example,
|
||||
you could use the commands
|
||||
|
||||
----
|
||||
./configure --prefix=/usr --mandir=/usr/share/man
|
||||
|
||||
@@ -8,9 +8,11 @@ Wants=time-sync.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
# Wait up to ~10 minutes for chronyd to synchronize and the remaining
|
||||
# clock correction to be less than 0.1 seconds
|
||||
ExecStart=/usr/bin/chronyc -h 127.0.0.1,::1 waitsync 600 0.1 0.0 1
|
||||
# Wait for chronyd to update the clock and the remaining
|
||||
# correction to be less than 0.1 seconds
|
||||
ExecStart=/usr/bin/chronyc -h 127.0.0.1,::1 waitsync 0 0.1 0.0 1
|
||||
# Wait for at most 3 minutes
|
||||
TimeoutStartSec=180
|
||||
RemainAfterExit=yes
|
||||
StandardOutput=null
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Use public servers from the pool.ntp.org project.
|
||||
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
|
||||
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
|
||||
pool pool.ntp.org iburst
|
||||
|
||||
# Record the rate at which the system clock gains/losses time.
|
||||
@@ -25,9 +25,18 @@ rtcsync
|
||||
# Serve time even if not synchronized to a time source.
|
||||
#local stratum 10
|
||||
|
||||
# Require authentication (nts or key option) for all NTP sources.
|
||||
#authselectmode require
|
||||
|
||||
# Specify file containing keys for NTP authentication.
|
||||
#keyfile /etc/chrony.keys
|
||||
|
||||
# Save NTS keys and cookies.
|
||||
ntsdumpdir /var/lib/chrony
|
||||
|
||||
# Insert/delete leap seconds by slewing instead of stepping.
|
||||
#leapsecmode slew
|
||||
|
||||
# Get TAI-UTC offset and leap seconds from the system tz database.
|
||||
#leapsectz right/UTC
|
||||
|
||||
|
||||
@@ -57,6 +57,20 @@
|
||||
|
||||
! maxdrift 100
|
||||
|
||||
# By default, chronyd allows synchronisation to an unauthenticated NTP
|
||||
# source (i.e. specified without the nts and key options) if it agrees with
|
||||
# a majority of authenticated NTP sources, or if no authenticated source is
|
||||
# specified. If you don't want chronyd to ever synchronise to an
|
||||
# unauthenticated NTP source, uncomment the first from the following lines.
|
||||
# If you don't want to synchronise to an unauthenticated NTP source only
|
||||
# when an authenticated source is specified, uncomment the second line.
|
||||
# If you want chronyd to ignore authentication in the source selection,
|
||||
# uncomment the third line.
|
||||
|
||||
! authselectmode require
|
||||
! authselectmode prefer
|
||||
! authselectmode ignore
|
||||
|
||||
#######################################################################
|
||||
### FILENAMES ETC
|
||||
# Chrony likes to keep information about your computer's clock in files.
|
||||
@@ -72,22 +86,37 @@ driftfile /var/lib/chrony/drift
|
||||
|
||||
! keyfile /etc/chrony.keys
|
||||
|
||||
# If you specify an NTP server with the nts option to enable authentication
|
||||
# with the Network Time Security (NTS) mechanism, or enable server NTS with
|
||||
# the ntsservercert and ntsserverkey directives below, the following line will
|
||||
# allow the client/server to save the NTS keys and cookies in order to reduce
|
||||
# the number of key establishments (NTS-KE sessions).
|
||||
|
||||
ntsdumpdir /var/lib/chrony
|
||||
|
||||
# If chronyd is configured to act as an NTP server and you want to enable NTS
|
||||
# for its clients, you will need a TLS certificate and private key. Uncomment
|
||||
# and edit the following lines to specify the locations of the certificate and
|
||||
# key.
|
||||
|
||||
! ntsservercert /etc/.../foo.example.net.crt
|
||||
! ntsserverkey /etc/.../foo.example.net.key
|
||||
|
||||
# chronyd can save the measurement history for the servers to files when
|
||||
# it it exits. This is useful in 2 situations:
|
||||
# it exits. This is useful in 2 situations:
|
||||
#
|
||||
# 1. On Linux, if you stop chronyd and restart it with '-r' (e.g. after
|
||||
# 1. If you stop chronyd and restart it with the '-r' option (e.g. after
|
||||
# an upgrade), the old measurements will still be relevant when chronyd
|
||||
# is restarted. This will reduce the time needed to get accurate
|
||||
# gain/loss measurements, especially with a dial-up link.
|
||||
# gain/loss measurements.
|
||||
#
|
||||
# 2. Again on Linux, if you use the RTC support and start chronyd with
|
||||
# 2. On Linux, if you use the RTC support and start chronyd with
|
||||
# '-r -s' on bootup, measurements from the last boot will still be
|
||||
# useful (the real time clock is used to 'flywheel' chronyd between
|
||||
# boots).
|
||||
#
|
||||
# Enable these two options to use this.
|
||||
# Uncomment the following line to use this.
|
||||
|
||||
! dumponexit
|
||||
! dumpdir /var/lib/chrony
|
||||
|
||||
# chronyd writes its process ID to a file. If you try to start a second
|
||||
@@ -95,7 +124,7 @@ driftfile /var/lib/chrony/drift
|
||||
# still running and bail out. If you want to change the path to the PID
|
||||
# file, uncomment this line and edit it. The default path is shown.
|
||||
|
||||
! pidfile /var/run/chronyd.pid
|
||||
! pidfile /var/run/chrony/chronyd.pid
|
||||
|
||||
# If the system timezone database is kept up to date and includes the
|
||||
# right/UTC timezone, chronyd can use it to determine the current
|
||||
@@ -116,6 +145,18 @@ driftfile /var/lib/chrony/drift
|
||||
|
||||
! makestep 1.0 3
|
||||
|
||||
#######################################################################
|
||||
### LEAP SECONDS
|
||||
# A leap second is an occasional one-second correction of the UTC
|
||||
# time scale. By default, chronyd tells the kernel to insert/delete
|
||||
# the leap second, which makes a backward/forward step to correct the
|
||||
# clock for it. As with the makestep directive, this jump can upset
|
||||
# some applications. If you prefer chronyd to make a gradual
|
||||
# correction, causing the clock to be off for a longer time, uncomment
|
||||
# the following line.
|
||||
|
||||
! leapsecmode slew
|
||||
|
||||
#######################################################################
|
||||
### LOGGING
|
||||
# If you want to log information about the time measurements chronyd has
|
||||
@@ -135,8 +176,6 @@ driftfile /var/lib/chrony/drift
|
||||
#######################################################################
|
||||
### ACTING AS AN NTP SERVER
|
||||
# You might want the computer to be an NTP server for other computers.
|
||||
# e.g. you might be running chronyd on a dial-up machine that has a LAN
|
||||
# sitting behind it with several 'satellite' computers on it.
|
||||
#
|
||||
# By default, chronyd does not allow any clients to access it. You need
|
||||
# to explicitly enable access using 'allow' and 'deny' directives.
|
||||
@@ -152,15 +191,6 @@ driftfile /var/lib/chrony/drift
|
||||
# You can have as many allow and deny directives as you need. The order
|
||||
# is unimportant.
|
||||
|
||||
# If you want chronyd to act as an NTP broadcast server, enable and edit
|
||||
# (and maybe copy) the following line. This means that a broadcast
|
||||
# packet is sent to the address 192.168.1.255 every 60 seconds. The
|
||||
# address MUST correspond to the broadcast address of one of the network
|
||||
# interfaces on your machine. If you have multiple network interfaces,
|
||||
# add a broadcast line for each.
|
||||
|
||||
! broadcast 60 192.168.1.255
|
||||
|
||||
# If you want to present your computer's time for others to synchronise
|
||||
# with, even if you don't seem to be synchronised to any NTP servers
|
||||
# yourself, enable the following line. The value 10 may be varied
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# This is an example chrony keys file. It is used for NTP authentication with
|
||||
# symmetric keys. It should be readable only by root or the user to which
|
||||
# chronyd is configured to switch to after start.
|
||||
# This is an example chrony keys file. It enables authentication of NTP
|
||||
# packets with symmetric keys when its location is specified by the keyfile
|
||||
# directive in chrony.conf(5). It should be readable only by root and the
|
||||
# user under which chronyd is running.
|
||||
#
|
||||
# Don't use the example keys! It's recommended to generate random keys using
|
||||
# the chronyc keygen command.
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#!/bin/sh
|
||||
# This is a NetworkManager dispatcher script for chronyd to set its NTP sources
|
||||
# online or offline when a network interface is configured or removed
|
||||
|
||||
export LC_ALL=C
|
||||
|
||||
# Check if there is a default route
|
||||
|
||||
if /sbin/ip route list 2> /dev/null | grep -q '^default'; then
|
||||
chronyc online > /dev/null 2>&1
|
||||
exit 0
|
||||
fi
|
||||
|
||||
sources=$(chronyc -c -n sources 2> /dev/null)
|
||||
|
||||
[ $? -ne 0 ] && exit 0
|
||||
|
||||
# Check each configured source if it has a route
|
||||
|
||||
echo "$sources" | while IFS=, read mode state address rest; do
|
||||
[ "$mode" != '^' ] && [ "$mode" != '=' ] && continue
|
||||
|
||||
/sbin/ip route get "$address" > /dev/null 2>&1 && command="online" || command="offline"
|
||||
|
||||
# Set priority of sources so that the selected source is set as
|
||||
# last if offline to avoid unnecessary reselection
|
||||
[ "$state" != '*' ] && priority=1 || priority=2
|
||||
|
||||
echo "$priority $command $address"
|
||||
|
||||
done | sort | while read priority command address; do
|
||||
echo "$command $address"
|
||||
done | chronyc > /dev/null 2>&1
|
||||
|
||||
exit 0
|
||||
43
examples/chrony.nm-dispatcher.dhcp
Normal file
43
examples/chrony.nm-dispatcher.dhcp
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/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.
|
||||
|
||||
export LC_ALL=C
|
||||
|
||||
interface=$1
|
||||
action=$2
|
||||
|
||||
chronyc=/usr/bin/chronyc
|
||||
default_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
|
||||
|
||||
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"
|
||||
done
|
||||
$chronyc reload sources > /dev/null 2>&1 || :
|
||||
}
|
||||
|
||||
clear_servers_from_dhcp() {
|
||||
if [ -f "$dhcp_server_file" ]; then
|
||||
rm -f "$dhcp_server_file"
|
||||
$chronyc reload sources > /dev/null 2>&1 || :
|
||||
fi
|
||||
}
|
||||
|
||||
mkdir -p $server_dir
|
||||
|
||||
if [ "$action" = "up" ] || [ "$action" = "dhcp4-change" ]; then
|
||||
add_servers_from_dhcp
|
||||
elif [ "$action" = "down" ]; then
|
||||
clear_servers_from_dhcp
|
||||
fi
|
||||
|
||||
exit 0
|
||||
17
examples/chrony.nm-dispatcher.onoffline
Normal file
17
examples/chrony.nm-dispatcher.onoffline
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
# This is a NetworkManager dispatcher / networkd-dispatcher script for
|
||||
# chronyd to set its NTP sources online or offline when a network interface
|
||||
# is configured or removed
|
||||
|
||||
export LC_ALL=C
|
||||
|
||||
chronyc=/usr/bin/chronyc
|
||||
|
||||
# For NetworkManager consider only up/down events
|
||||
[ $# -ge 2 ] && [ "$2" != "up" ] && [ "$2" != "down" ] && exit 0
|
||||
|
||||
# Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
|
||||
|
||||
$chronyc onoffline > /dev/null 2>&1
|
||||
|
||||
exit 0
|
||||
@@ -1,46 +0,0 @@
|
||||
%global chrony_version @@VERSION@@
|
||||
%if 0%(echo %{chrony_version} | grep -q pre && echo 1)
|
||||
%global prerelease %(echo %{chrony_version} | sed 's/.*-//')
|
||||
%endif
|
||||
Summary: An NTP client/server
|
||||
Name: chrony
|
||||
Version: %(echo %{chrony_version} | sed 's/-.*//')
|
||||
Release: %{!?prerelease:1}%{?prerelease:0.1.%{prerelease}}
|
||||
Source: chrony-%{version}%{?prerelease:-%{prerelease}}.tar.gz
|
||||
License: GPLv2
|
||||
Group: Applications/Utilities
|
||||
BuildRoot: %{_tmppath}/%{name}-%{version}-root-%(id -u -n)
|
||||
|
||||
%description
|
||||
chrony is a client and server for the Network Time Protocol (NTP).
|
||||
This program keeps your computer's clock accurate. It was specially
|
||||
designed to support systems with intermittent Internet connections,
|
||||
but it also works well in permanently connected environments. It can
|
||||
also use hardware reference clocks, the system real-time clock, or
|
||||
manual input as time references.
|
||||
|
||||
%prep
|
||||
%setup -q -n %{name}-%{version}%{?prerelease:-%{prerelease}}
|
||||
|
||||
%build
|
||||
./configure \
|
||||
--prefix=%{_prefix} \
|
||||
--bindir=%{_bindir} \
|
||||
--sbindir=%{_sbindir} \
|
||||
--mandir=%{_mandir}
|
||||
make
|
||||
|
||||
%install
|
||||
rm -rf $RPM_BUILD_ROOT
|
||||
make install DESTDIR=$RPM_BUILD_ROOT
|
||||
|
||||
%files
|
||||
%{_sbindir}/chronyd
|
||||
%{_bindir}/chronyc
|
||||
%{_mandir}/man1/chronyc.1.gz
|
||||
%{_mandir}/man5/chrony.conf.5.gz
|
||||
%{_mandir}/man8/chronyd.8.gz
|
||||
%doc README FAQ NEWS COPYING
|
||||
%doc examples/chrony.conf.example*
|
||||
%doc examples/chrony.keys.example
|
||||
|
||||
@@ -7,7 +7,7 @@ ConditionCapability=CAP_SYS_TIME
|
||||
|
||||
[Service]
|
||||
Type=forking
|
||||
PIDFile=/var/run/chronyd.pid
|
||||
PIDFile=/run/chrony/chronyd.pid
|
||||
EnvironmentFile=-/etc/sysconfig/chronyd
|
||||
ExecStart=/usr/sbin/chronyd $OPTIONS
|
||||
PrivateTmp=yes
|
||||
|
||||
23
hash.h
23
hash.h
@@ -31,12 +31,25 @@
|
||||
/* length of hash values produced by SHA512 */
|
||||
#define MAX_HASH_LENGTH 64
|
||||
|
||||
extern int HSH_GetHashId(const char *name);
|
||||
typedef enum {
|
||||
HSH_INVALID = 0,
|
||||
HSH_MD5 = 1,
|
||||
HSH_SHA1 = 2,
|
||||
HSH_SHA256 = 3,
|
||||
HSH_SHA384 = 4,
|
||||
HSH_SHA512 = 5,
|
||||
HSH_SHA3_224 = 6,
|
||||
HSH_SHA3_256 = 7,
|
||||
HSH_SHA3_384 = 8,
|
||||
HSH_SHA3_512 = 9,
|
||||
HSH_TIGER = 10,
|
||||
HSH_WHIRLPOOL = 11,
|
||||
} HSH_Algorithm;
|
||||
|
||||
extern unsigned int HSH_Hash(int id,
|
||||
const unsigned char *in1, unsigned int in1_len,
|
||||
const unsigned char *in2, unsigned int in2_len,
|
||||
unsigned char *out, unsigned int out_len);
|
||||
extern int HSH_GetHashId(HSH_Algorithm algorithm);
|
||||
|
||||
extern int HSH_Hash(int id, const void *in1, int in1_len, const void *in2, int in2_len,
|
||||
unsigned char *out, int out_len);
|
||||
|
||||
extern void HSH_Finalise(void);
|
||||
|
||||
|
||||
@@ -29,27 +29,27 @@
|
||||
#include "sysincl.h"
|
||||
#include "hash.h"
|
||||
#include "memory.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "md5.c"
|
||||
|
||||
static MD5_CTX ctx;
|
||||
|
||||
int
|
||||
HSH_GetHashId(const char *name)
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
/* only MD5 is supported */
|
||||
if (strcmp(name, "MD5"))
|
||||
if (algorithm != HSH_MD5)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
||||
const unsigned char *in2, unsigned int in2_len,
|
||||
unsigned char *out, unsigned int out_len)
|
||||
int
|
||||
HSH_Hash(int id, const void *in1, int in1_len, const void *in2, int in2_len,
|
||||
unsigned char *out, int out_len)
|
||||
{
|
||||
if (out_len < 16)
|
||||
if (in1_len < 0 || in2_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
MD5Init(&ctx);
|
||||
@@ -58,9 +58,11 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
||||
MD5Update(&ctx, in2, in2_len);
|
||||
MD5Final(&ctx);
|
||||
|
||||
memcpy(out, ctx.digest, 16);
|
||||
out_len = MIN(out_len, 16);
|
||||
|
||||
return 16;
|
||||
memcpy(out, ctx.digest, out_len);
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
121
hash_nettle.c
Normal file
121
hash_nettle.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2018
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Routines implementing crypto hashing using the nettle library.
|
||||
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <nettle/nettle-meta.h>
|
||||
|
||||
#include "hash.h"
|
||||
#include "memory.h"
|
||||
|
||||
struct hash {
|
||||
const HSH_Algorithm algorithm;
|
||||
const char *int_name;
|
||||
const struct nettle_hash *nettle_hash;
|
||||
void *context;
|
||||
};
|
||||
|
||||
static struct hash hashes[] = {
|
||||
{ HSH_MD5, "md5", NULL, NULL },
|
||||
{ HSH_SHA1, "sha1", NULL, NULL },
|
||||
{ HSH_SHA256, "sha256", NULL, NULL },
|
||||
{ HSH_SHA384, "sha384", NULL, NULL },
|
||||
{ HSH_SHA512, "sha512", NULL, NULL },
|
||||
{ HSH_SHA3_224, "sha3_224", NULL, NULL },
|
||||
{ HSH_SHA3_256, "sha3_256", NULL, NULL },
|
||||
{ HSH_SHA3_384, "sha3_384", NULL, NULL },
|
||||
{ HSH_SHA3_512, "sha3_512", NULL, NULL },
|
||||
{ 0, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
int
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int id, nid;
|
||||
|
||||
for (id = 0; hashes[id].algorithm != 0; id++) {
|
||||
if (hashes[id].algorithm == algorithm)
|
||||
break;
|
||||
}
|
||||
|
||||
if (hashes[id].algorithm == 0)
|
||||
return -1;
|
||||
|
||||
if (hashes[id].context)
|
||||
return id;
|
||||
|
||||
for (nid = 0; nettle_hashes[nid]; nid++) {
|
||||
if (!strcmp(hashes[id].int_name, nettle_hashes[nid]->name))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!nettle_hashes[nid] || !nettle_hashes[nid]->context_size || !nettle_hashes[nid]->init)
|
||||
return -1;
|
||||
|
||||
hashes[id].nettle_hash = nettle_hashes[nid];
|
||||
hashes[id].context = Malloc(hashes[id].nettle_hash->context_size);
|
||||
|
||||
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)
|
||||
{
|
||||
const struct nettle_hash *hash;
|
||||
void *context;
|
||||
|
||||
if (in1_len < 0 || in2_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
hash = hashes[id].nettle_hash;
|
||||
context = hashes[id].context;
|
||||
|
||||
if (out_len > hash->digest_size)
|
||||
out_len = hash->digest_size;
|
||||
|
||||
hash->init(context);
|
||||
hash->update(context, in1_len, in1);
|
||||
if (in2)
|
||||
hash->update(context, in2_len, in2);
|
||||
hash->digest(context, out_len, out);
|
||||
|
||||
return out_len;
|
||||
}
|
||||
|
||||
void
|
||||
HSH_Finalise(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].context)
|
||||
Free(hashes[i].context);
|
||||
}
|
||||
}
|
||||
43
hash_nss.c
43
hash_nss.c
@@ -32,35 +32,36 @@
|
||||
#include <nsslowhash.h>
|
||||
|
||||
#include "hash.h"
|
||||
#include "util.h"
|
||||
|
||||
static NSSLOWInitContext *ictx;
|
||||
|
||||
struct hash {
|
||||
HASH_HashType type;
|
||||
const char *name;
|
||||
HSH_Algorithm algorithm;
|
||||
NSSLOWHASHContext *context;
|
||||
};
|
||||
|
||||
static struct hash hashes[] = {
|
||||
{ HASH_AlgMD5, "MD5", NULL },
|
||||
{ HASH_AlgSHA1, "SHA1", NULL },
|
||||
{ HASH_AlgSHA256, "SHA256", NULL },
|
||||
{ HASH_AlgSHA384, "SHA384", NULL },
|
||||
{ HASH_AlgSHA512, "SHA512", NULL },
|
||||
{ 0, NULL, NULL }
|
||||
{ HASH_AlgMD5, HSH_MD5, NULL },
|
||||
{ HASH_AlgSHA1, HSH_SHA1, NULL },
|
||||
{ HASH_AlgSHA256, HSH_SHA256, NULL },
|
||||
{ HASH_AlgSHA384, HSH_SHA384, NULL },
|
||||
{ HASH_AlgSHA512, HSH_SHA512, NULL },
|
||||
{ 0, 0, NULL }
|
||||
};
|
||||
|
||||
int
|
||||
HSH_GetHashId(const char *name)
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; hashes[i].name; i++) {
|
||||
if (!strcmp(name, hashes[i].name))
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].algorithm == algorithm)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!hashes[i].name)
|
||||
if (hashes[i].algorithm == 0)
|
||||
return -1; /* not found */
|
||||
|
||||
if (!ictx && !(ictx = NSSLOW_Init()))
|
||||
@@ -73,18 +74,24 @@ HSH_GetHashId(const char *name)
|
||||
return i;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
||||
const unsigned char *in2, unsigned int in2_len,
|
||||
unsigned char *out, unsigned int out_len)
|
||||
int
|
||||
HSH_Hash(int id, const void *in1, int in1_len, const void *in2, int in2_len,
|
||||
unsigned char *out, int out_len)
|
||||
{
|
||||
unsigned int ret;
|
||||
unsigned char buf[MAX_HASH_LENGTH];
|
||||
unsigned int ret = 0;
|
||||
|
||||
if (in1_len < 0 || in2_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
NSSLOWHASH_Begin(hashes[id].context);
|
||||
NSSLOWHASH_Update(hashes[id].context, in1, in1_len);
|
||||
if (in2)
|
||||
NSSLOWHASH_Update(hashes[id].context, in2, in2_len);
|
||||
NSSLOWHASH_End(hashes[id].context, out, &ret, out_len);
|
||||
NSSLOWHASH_End(hashes[id].context, buf, &ret, sizeof (buf));
|
||||
|
||||
ret = MIN(ret, out_len);
|
||||
memcpy(out, buf, ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -94,7 +101,7 @@ HSH_Finalise(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; hashes[i].name; i++) {
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].context)
|
||||
NSSLOWHASH_Destroy(hashes[i].context);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2012
|
||||
* Copyright (C) Miroslav Lichvar 2012, 2018
|
||||
*
|
||||
* 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
|
||||
@@ -29,59 +29,54 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "hash.h"
|
||||
#include "util.h"
|
||||
|
||||
struct hash {
|
||||
const char *name;
|
||||
HSH_Algorithm algorithm;
|
||||
const char *int_name;
|
||||
const struct ltc_hash_descriptor *desc;
|
||||
};
|
||||
|
||||
static const struct hash hashes[] = {
|
||||
{ "MD5", "md5", &md5_desc },
|
||||
#ifdef LTC_RIPEMD128
|
||||
{ "RMD128", "rmd128", &rmd128_desc },
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD160
|
||||
{ "RMD160", "rmd160", &rmd160_desc },
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD256
|
||||
{ "RMD256", "rmd256", &rmd256_desc },
|
||||
#endif
|
||||
#ifdef LTC_RIPEMD320
|
||||
{ "RMD320", "rmd320", &rmd320_desc },
|
||||
#endif
|
||||
{ HSH_MD5, "md5", &md5_desc },
|
||||
#ifdef LTC_SHA1
|
||||
{ "SHA1", "sha1", &sha1_desc },
|
||||
{ HSH_SHA1, "sha1", &sha1_desc },
|
||||
#endif
|
||||
#ifdef LTC_SHA256
|
||||
{ "SHA256", "sha256", &sha256_desc },
|
||||
{ HSH_SHA256, "sha256", &sha256_desc },
|
||||
#endif
|
||||
#ifdef LTC_SHA384
|
||||
{ "SHA384", "sha384", &sha384_desc },
|
||||
{ HSH_SHA384, "sha384", &sha384_desc },
|
||||
#endif
|
||||
#ifdef LTC_SHA512
|
||||
{ "SHA512", "sha512", &sha512_desc },
|
||||
{ HSH_SHA512, "sha512", &sha512_desc },
|
||||
#endif
|
||||
#ifdef LTC_SHA3
|
||||
{ HSH_SHA3_224, "sha3-224", &sha3_224_desc },
|
||||
{ HSH_SHA3_256, "sha3-256", &sha3_256_desc },
|
||||
{ HSH_SHA3_384, "sha3-384", &sha3_384_desc },
|
||||
{ HSH_SHA3_512, "sha3-512", &sha3_512_desc },
|
||||
#endif
|
||||
#ifdef LTC_TIGER
|
||||
{ "TIGER", "tiger", &tiger_desc },
|
||||
{ HSH_TIGER, "tiger", &tiger_desc },
|
||||
#endif
|
||||
#ifdef LTC_WHIRLPOOL
|
||||
{ "WHIRLPOOL", "whirlpool", &whirlpool_desc },
|
||||
{ HSH_WHIRLPOOL, "whirlpool", &whirlpool_desc },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
{ 0, NULL, NULL }
|
||||
};
|
||||
|
||||
int
|
||||
HSH_GetHashId(const char *name)
|
||||
HSH_GetHashId(HSH_Algorithm algorithm)
|
||||
{
|
||||
int i, h;
|
||||
|
||||
for (i = 0; hashes[i].name; i++) {
|
||||
if (!strcmp(name, hashes[i].name))
|
||||
for (i = 0; hashes[i].algorithm != 0; i++) {
|
||||
if (hashes[i].algorithm == algorithm)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!hashes[i].name)
|
||||
if (hashes[i].algorithm == 0)
|
||||
return -1; /* not found */
|
||||
|
||||
h = find_hash(hashes[i].int_name);
|
||||
@@ -94,24 +89,31 @@ HSH_GetHashId(const char *name)
|
||||
return find_hash(hashes[i].int_name);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
||||
const unsigned char *in2, unsigned int in2_len,
|
||||
unsigned char *out, unsigned int out_len)
|
||||
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];
|
||||
unsigned long len;
|
||||
int r;
|
||||
|
||||
len = out_len;
|
||||
if (in1_len < 0 || in2_len < 0 || out_len < 0)
|
||||
return 0;
|
||||
|
||||
len = sizeof (buf);
|
||||
if (in2)
|
||||
r = hash_memory_multi(id, out, &len,
|
||||
in1, (unsigned long)in1_len, in2, (unsigned long)in2_len, NULL, 0);
|
||||
r = hash_memory_multi(id, buf, &len,
|
||||
in1, (unsigned long)in1_len,
|
||||
in2, (unsigned long)in2_len, NULL, 0);
|
||||
else
|
||||
r = hash_memory(id, in1, in1_len, out, &len);
|
||||
r = hash_memory(id, in1, in1_len, buf, &len);
|
||||
|
||||
if (r != CRYPT_OK)
|
||||
return 0;
|
||||
|
||||
len = MIN(len, out_len);
|
||||
memcpy(out, buf, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
42
hwclock.c
42
hwclock.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2016-2017
|
||||
* Copyright (C) Miroslav Lichvar 2016-2018
|
||||
*
|
||||
* 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
|
||||
@@ -36,8 +36,9 @@
|
||||
#include "regress.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Maximum number of samples per clock */
|
||||
#define MAX_SAMPLES 16
|
||||
/* Minimum and maximum number of samples per clock */
|
||||
#define MIN_SAMPLES 2
|
||||
#define MAX_SAMPLES 64
|
||||
|
||||
/* Maximum acceptable frequency offset of the clock */
|
||||
#define MAX_FREQ_OFFSET (2.0 / 3.0)
|
||||
@@ -49,10 +50,12 @@ struct HCL_Instance_Record {
|
||||
|
||||
/* Samples stored as intervals (uncorrected for frequency error)
|
||||
relative to local_ref and hw_ref */
|
||||
double x_data[MAX_SAMPLES];
|
||||
double y_data[MAX_SAMPLES];
|
||||
double *x_data;
|
||||
double *y_data;
|
||||
|
||||
/* Number of samples */
|
||||
/* Minimum, maximum and current number of samples */
|
||||
int min_samples;
|
||||
int max_samples;
|
||||
int n_samples;
|
||||
|
||||
/* Maximum error of the last sample */
|
||||
@@ -89,13 +92,21 @@ handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
/* ================================================== */
|
||||
|
||||
HCL_Instance
|
||||
HCL_CreateInstance(double min_separation)
|
||||
HCL_CreateInstance(int min_samples, int max_samples, double min_separation)
|
||||
{
|
||||
HCL_Instance clock;
|
||||
|
||||
min_samples = CLAMP(MIN_SAMPLES, min_samples, MAX_SAMPLES);
|
||||
max_samples = CLAMP(MIN_SAMPLES, max_samples, MAX_SAMPLES);
|
||||
max_samples = MAX(min_samples, max_samples);
|
||||
|
||||
clock = MallocNew(struct HCL_Instance_Record);
|
||||
clock->x_data[MAX_SAMPLES - 1] = 0.0;
|
||||
clock->y_data[MAX_SAMPLES - 1] = 0.0;
|
||||
clock->x_data = MallocArray(double, max_samples);
|
||||
clock->y_data = MallocArray(double, max_samples);
|
||||
clock->x_data[max_samples - 1] = 0.0;
|
||||
clock->y_data[max_samples - 1] = 0.0;
|
||||
clock->min_samples = min_samples;
|
||||
clock->max_samples = max_samples;
|
||||
clock->n_samples = 0;
|
||||
clock->valid_coefs = 0;
|
||||
clock->min_separation = min_separation;
|
||||
@@ -110,6 +121,8 @@ HCL_CreateInstance(double min_separation)
|
||||
void HCL_DestroyInstance(HCL_Instance clock)
|
||||
{
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, clock);
|
||||
Free(clock->y_data);
|
||||
Free(clock->x_data);
|
||||
Free(clock);
|
||||
}
|
||||
|
||||
@@ -138,7 +151,7 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
|
||||
/* Shift old samples */
|
||||
if (clock->n_samples) {
|
||||
if (clock->n_samples >= MAX_SAMPLES)
|
||||
if (clock->n_samples >= clock->max_samples)
|
||||
clock->n_samples--;
|
||||
|
||||
hw_delta = UTI_DiffTimespecsToDouble(hw_ts, &clock->hw_ref);
|
||||
@@ -149,7 +162,7 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
DEBUG_LOG("HW clock reset interval=%f", local_delta);
|
||||
}
|
||||
|
||||
for (i = MAX_SAMPLES - clock->n_samples; i < MAX_SAMPLES; i++) {
|
||||
for (i = clock->max_samples - clock->n_samples; i < clock->max_samples; i++) {
|
||||
clock->y_data[i - 1] = clock->y_data[i] - hw_delta;
|
||||
clock->x_data[i - 1] = clock->x_data[i] - local_delta;
|
||||
}
|
||||
@@ -162,8 +175,8 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
|
||||
/* Get new coefficients */
|
||||
clock->valid_coefs =
|
||||
RGR_FindBestRobustRegression(clock->x_data + MAX_SAMPLES - clock->n_samples,
|
||||
clock->y_data + MAX_SAMPLES - clock->n_samples,
|
||||
RGR_FindBestRobustRegression(clock->x_data + clock->max_samples - clock->n_samples,
|
||||
clock->y_data + clock->max_samples - clock->n_samples,
|
||||
clock->n_samples, 1.0e-10, &clock->offset, &raw_freq,
|
||||
&n_runs, &best_start);
|
||||
|
||||
@@ -175,7 +188,8 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
||||
clock->frequency = raw_freq / local_freq;
|
||||
|
||||
/* Drop unneeded samples */
|
||||
clock->n_samples -= best_start;
|
||||
if (clock->n_samples > clock->min_samples)
|
||||
clock->n_samples -= MIN(best_start, clock->n_samples - clock->min_samples);
|
||||
|
||||
/* If the fit doesn't cross the error interval of the last sample,
|
||||
or the frequency is not sane, drop all samples and start again */
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
typedef struct HCL_Instance_Record *HCL_Instance;
|
||||
|
||||
/* Create a new HW clock instance */
|
||||
extern HCL_Instance HCL_CreateInstance(double min_separation);
|
||||
extern HCL_Instance HCL_CreateInstance(int min_samples, int max_samples,
|
||||
double min_separation);
|
||||
|
||||
/* Destroy a HW clock instance */
|
||||
extern void HCL_DestroyInstance(HCL_Instance clock);
|
||||
|
||||
267
keys.c
267
keys.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2012-2016
|
||||
* Copyright (C) Miroslav Lichvar 2012-2016, 2019-2020
|
||||
*
|
||||
* 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
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
#include "array.h"
|
||||
#include "keys.h"
|
||||
#include "cmac.h"
|
||||
#include "cmdparse.h"
|
||||
#include "conf.h"
|
||||
#include "memory.h"
|
||||
@@ -42,12 +43,23 @@
|
||||
/* Consider 80 bits as the absolute minimum for a secure key */
|
||||
#define MIN_SECURE_KEY_LENGTH 10
|
||||
|
||||
typedef enum {
|
||||
NTP_MAC,
|
||||
CMAC,
|
||||
} KeyClass;
|
||||
|
||||
typedef struct {
|
||||
uint32_t id;
|
||||
char *val;
|
||||
int len;
|
||||
int hash_id;
|
||||
int auth_delay;
|
||||
int type;
|
||||
int length;
|
||||
KeyClass class;
|
||||
union {
|
||||
struct {
|
||||
unsigned char *value;
|
||||
int hash_id;
|
||||
} ntp_mac;
|
||||
CMC_Instance cmac;
|
||||
} data;
|
||||
} Key;
|
||||
|
||||
static ARR_Instance keys;
|
||||
@@ -62,9 +74,21 @@ static void
|
||||
free_keys(void)
|
||||
{
|
||||
unsigned int i;
|
||||
Key *key;
|
||||
|
||||
for (i = 0; i < ARR_GetSize(keys); i++)
|
||||
Free(((Key *)ARR_GetElement(keys, i))->val);
|
||||
for (i = 0; i < ARR_GetSize(keys); i++) {
|
||||
key = ARR_GetElement(keys, i);
|
||||
switch (key->class) {
|
||||
case NTP_MAC:
|
||||
Free(key->data.ntp_mac.value);
|
||||
break;
|
||||
case CMAC:
|
||||
CMC_DestroyInstance(key->data.cmac);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
ARR_SetSize(keys, 0);
|
||||
cache_valid = 0;
|
||||
@@ -98,60 +122,18 @@ get_key(unsigned int index)
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Decode key encoded in ASCII or HEX */
|
||||
|
||||
static int
|
||||
determine_hash_delay(uint32_t key_id)
|
||||
decode_key(char *key)
|
||||
{
|
||||
NTP_Packet pkt;
|
||||
struct timespec before, after;
|
||||
double diff, min_diff;
|
||||
int i, nsecs;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
LCL_ReadRawTime(&before);
|
||||
KEY_GenerateAuth(key_id, (unsigned char *)&pkt, NTP_NORMAL_PACKET_LENGTH,
|
||||
(unsigned char *)&pkt.auth_data, sizeof (pkt.auth_data));
|
||||
LCL_ReadRawTime(&after);
|
||||
|
||||
diff = UTI_DiffTimespecsToDouble(&after, &before);
|
||||
|
||||
if (i == 0 || min_diff > diff)
|
||||
min_diff = diff;
|
||||
}
|
||||
|
||||
/* Add on a bit extra to allow for copying, conversions etc */
|
||||
nsecs = 1.0625e9 * min_diff;
|
||||
|
||||
DEBUG_LOG("authentication delay for key %"PRIu32": %d nsecs", key_id, nsecs);
|
||||
|
||||
return nsecs;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* Decode password encoded in ASCII or HEX */
|
||||
|
||||
static int
|
||||
decode_password(char *key)
|
||||
{
|
||||
int i, j, len = strlen(key);
|
||||
char buf[3], *p;
|
||||
int len = strlen(key);
|
||||
|
||||
if (!strncmp(key, "ASCII:", 6)) {
|
||||
memmove(key, key + 6, len - 6);
|
||||
return len - 6;
|
||||
} else if (!strncmp(key, "HEX:", 4)) {
|
||||
if ((len - 4) % 2)
|
||||
return 0;
|
||||
|
||||
for (i = 0, j = 4; j + 1 < len; i++, j += 2) {
|
||||
buf[0] = key[j], buf[1] = key[j + 1], buf[2] = '\0';
|
||||
key[i] = strtol(buf, &p, 16);
|
||||
|
||||
if (p != buf + 2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return i;
|
||||
return UTI_HexToBytes(key + 4, key, len);
|
||||
} else {
|
||||
/* assume ASCII */
|
||||
return len;
|
||||
@@ -183,11 +165,13 @@ compare_keys_by_id(const void *a, const void *b)
|
||||
void
|
||||
KEY_Reload(void)
|
||||
{
|
||||
unsigned int i, line_number;
|
||||
unsigned int i, line_number, key_length, cmac_key_length;
|
||||
FILE *in;
|
||||
uint32_t key_id;
|
||||
char line[2048], *keyval, *key_file;
|
||||
const char *hashname;
|
||||
char line[2048], *key_file, *key_value;
|
||||
const char *key_type;
|
||||
HSH_Algorithm hash_algorithm;
|
||||
CMC_Algorithm cmac_algorithm;
|
||||
int hash_id;
|
||||
Key key;
|
||||
|
||||
free_keys();
|
||||
@@ -198,7 +182,7 @@ KEY_Reload(void)
|
||||
if (!key_file)
|
||||
return;
|
||||
|
||||
in = fopen(key_file, "r");
|
||||
in = UTI_OpenFile(NULL, key_file, NULL, 'r', 0);
|
||||
if (!in) {
|
||||
LOG(LOGS_WARN, "Could not open keyfile %s", key_file);
|
||||
return;
|
||||
@@ -211,26 +195,56 @@ KEY_Reload(void)
|
||||
if (!*line)
|
||||
continue;
|
||||
|
||||
if (!CPS_ParseKey(line, &key_id, &hashname, &keyval)) {
|
||||
LOG(LOGS_WARN, "Could not parse key at line %d in file %s", line_number, key_file);
|
||||
memset(&key, 0, sizeof (key));
|
||||
|
||||
if (!CPS_ParseKey(line, &key.id, &key_type, &key_value)) {
|
||||
LOG(LOGS_WARN, "Could not parse key at line %u in file %s", line_number, key_file);
|
||||
continue;
|
||||
}
|
||||
|
||||
key.hash_id = HSH_GetHashId(hashname);
|
||||
if (key.hash_id < 0) {
|
||||
LOG(LOGS_WARN, "Unknown hash function in key %"PRIu32, key_id);
|
||||
key_length = decode_key(key_value);
|
||||
if (key_length == 0) {
|
||||
LOG(LOGS_WARN, "Could not decode key %"PRIu32, key.id);
|
||||
continue;
|
||||
}
|
||||
|
||||
key.len = decode_password(keyval);
|
||||
if (!key.len) {
|
||||
LOG(LOGS_WARN, "Could not decode password in key %"PRIu32, key_id);
|
||||
hash_algorithm = UTI_HashNameToAlgorithm(key_type);
|
||||
cmac_algorithm = UTI_CmacNameToAlgorithm(key_type);
|
||||
|
||||
if (hash_algorithm != 0) {
|
||||
hash_id = HSH_GetHashId(hash_algorithm);
|
||||
if (hash_id < 0) {
|
||||
LOG(LOGS_WARN, "Unsupported %s in key %"PRIu32, "hash function", key.id);
|
||||
continue;
|
||||
}
|
||||
key.class = NTP_MAC;
|
||||
key.type = hash_algorithm;
|
||||
key.length = key_length;
|
||||
key.data.ntp_mac.value = MallocArray(unsigned char, key_length);
|
||||
memcpy(key.data.ntp_mac.value, key_value, key_length);
|
||||
key.data.ntp_mac.hash_id = hash_id;
|
||||
} else if (cmac_algorithm != 0) {
|
||||
cmac_key_length = CMC_GetKeyLength(cmac_algorithm);
|
||||
if (cmac_key_length == 0) {
|
||||
LOG(LOGS_WARN, "Unsupported %s in key %"PRIu32, "cipher", key.id);
|
||||
continue;
|
||||
} else if (cmac_key_length != key_length) {
|
||||
LOG(LOGS_WARN, "Invalid length of %s key %"PRIu32" (expected %u bits)",
|
||||
key_type, key.id, 8 * cmac_key_length);
|
||||
continue;
|
||||
}
|
||||
|
||||
key.class = CMAC;
|
||||
key.type = cmac_algorithm;
|
||||
key.length = key_length;
|
||||
key.data.cmac = CMC_CreateInstance(cmac_algorithm, (unsigned char *)key_value,
|
||||
key_length);
|
||||
assert(key.data.cmac);
|
||||
} else {
|
||||
LOG(LOGS_WARN, "Invalid type in key %"PRIu32, key.id);
|
||||
continue;
|
||||
}
|
||||
|
||||
key.id = key_id;
|
||||
key.val = MallocArray(char, key.len);
|
||||
memcpy(key.val, keyval, key.len);
|
||||
ARR_AppendElement(keys, &key);
|
||||
}
|
||||
|
||||
@@ -249,9 +263,6 @@ KEY_Reload(void)
|
||||
|
||||
/* Erase any passwords from stack */
|
||||
memset(line, 0, sizeof (line));
|
||||
|
||||
for (i = 0; i < ARR_GetSize(keys); i++)
|
||||
get_key(i)->auth_delay = determine_hash_delay(get_key(i)->id);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -307,21 +318,6 @@ KEY_KeyKnown(uint32_t key_id)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
KEY_GetAuthDelay(uint32_t key_id)
|
||||
{
|
||||
Key *key;
|
||||
|
||||
key = get_key_by_id(key_id);
|
||||
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return key->auth_delay;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
KEY_GetAuthLength(uint32_t key_id)
|
||||
{
|
||||
@@ -333,7 +329,15 @@ KEY_GetAuthLength(uint32_t key_id)
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return HSH_Hash(key->hash_id, buf, 0, buf, 0, buf, sizeof (buf));
|
||||
switch (key->class) {
|
||||
case NTP_MAC:
|
||||
return HSH_Hash(key->data.ntp_mac.hash_id, buf, 0, buf, 0, buf, sizeof (buf));
|
||||
case CMAC:
|
||||
return CMC_Hash(key->data.cmac, buf, 0, buf, sizeof (buf));
|
||||
default:
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -348,39 +352,13 @@ KEY_CheckKeyLength(uint32_t key_id)
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return key->len >= MIN_SECURE_KEY_LENGTH;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
generate_ntp_auth(int hash_id, const unsigned char *key, int key_len,
|
||||
const unsigned char *data, int data_len,
|
||||
unsigned char *auth, int auth_len)
|
||||
{
|
||||
return HSH_Hash(hash_id, key, key_len, data, data_len, auth, auth_len);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_ntp_auth(int hash_id, const unsigned char *key, int key_len,
|
||||
const unsigned char *data, int data_len,
|
||||
const unsigned char *auth, int auth_len, int trunc_len)
|
||||
{
|
||||
unsigned char buf[MAX_HASH_LENGTH];
|
||||
int hash_len;
|
||||
|
||||
hash_len = generate_ntp_auth(hash_id, key, key_len, data, data_len, buf, sizeof (buf));
|
||||
|
||||
return MIN(hash_len, trunc_len) == auth_len && !memcmp(buf, auth, auth_len);
|
||||
return key->length >= MIN_SECURE_KEY_LENGTH;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||
unsigned char *auth, int auth_len)
|
||||
KEY_GetKeyInfo(uint32_t key_id, int *type, int *bits)
|
||||
{
|
||||
Key *key;
|
||||
|
||||
@@ -389,14 +367,62 @@ KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return generate_ntp_auth(key->hash_id, (unsigned char *)key->val, key->len,
|
||||
data, data_len, auth, auth_len);
|
||||
*type = key->type;
|
||||
*bits = 8 * key->length;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
generate_auth(Key *key, const void *data, int data_len, unsigned char *auth, int auth_len)
|
||||
{
|
||||
switch (key->class) {
|
||||
case NTP_MAC:
|
||||
return HSH_Hash(key->data.ntp_mac.hash_id, key->data.ntp_mac.value,
|
||||
key->length, data, data_len, auth, auth_len);
|
||||
case CMAC:
|
||||
return CMC_Hash(key->data.cmac, data, data_len, auth, auth_len);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_auth(Key *key, const void *data, int data_len,
|
||||
const unsigned char *auth, int auth_len, int trunc_len)
|
||||
{
|
||||
unsigned char buf[MAX_HASH_LENGTH];
|
||||
int hash_len;
|
||||
|
||||
hash_len = generate_auth(key, data, data_len, buf, sizeof (buf));
|
||||
|
||||
return MIN(hash_len, trunc_len) == auth_len && !memcmp(buf, auth, auth_len);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||
KEY_GenerateAuth(uint32_t key_id, const void *data, int data_len,
|
||||
unsigned char *auth, int auth_len)
|
||||
{
|
||||
Key *key;
|
||||
|
||||
key = get_key_by_id(key_id);
|
||||
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return generate_auth(key, data, data_len, auth, auth_len);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
KEY_CheckAuth(uint32_t key_id, const void *data, int data_len,
|
||||
const unsigned char *auth, int auth_len, int trunc_len)
|
||||
{
|
||||
Key *key;
|
||||
@@ -406,6 +432,5 @@ KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
return check_ntp_auth(key->hash_id, (unsigned char *)key->val, key->len,
|
||||
data, data_len, auth, auth_len, trunc_len);
|
||||
return check_auth(key, data, data_len, auth, auth_len, trunc_len);
|
||||
}
|
||||
|
||||
9
keys.h
9
keys.h
@@ -34,15 +34,14 @@ extern void KEY_Finalise(void);
|
||||
|
||||
extern void KEY_Reload(void);
|
||||
|
||||
extern int KEY_GetKey(uint32_t key_id, char **key, int *len);
|
||||
extern int KEY_KeyKnown(uint32_t key_id);
|
||||
extern int KEY_GetAuthDelay(uint32_t key_id);
|
||||
extern int KEY_GetAuthLength(uint32_t key_id);
|
||||
extern int KEY_CheckKeyLength(uint32_t key_id);
|
||||
extern int KEY_GetKeyInfo(uint32_t key_id, int *type, int *bits);
|
||||
|
||||
extern int KEY_GenerateAuth(uint32_t key_id, const unsigned char *data,
|
||||
int data_len, unsigned char *auth, int auth_len);
|
||||
extern int KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||
extern int KEY_GenerateAuth(uint32_t key_id, const void *data, int data_len,
|
||||
unsigned char *auth, int auth_len);
|
||||
extern int KEY_CheckAuth(uint32_t key_id, const void *data, int data_len,
|
||||
const unsigned char *auth, int auth_len, int trunc_len);
|
||||
|
||||
#endif /* GOT_KEYS_H */
|
||||
|
||||
65
local.c
65
local.c
@@ -108,8 +108,8 @@ static double max_clock_error;
|
||||
|
||||
#define NSEC_PER_SEC 1000000000
|
||||
|
||||
static void
|
||||
calculate_sys_precision(void)
|
||||
static double
|
||||
measure_clock_precision(void)
|
||||
{
|
||||
struct timespec ts, old_ts;
|
||||
int iters, diff, best;
|
||||
@@ -135,18 +135,7 @@ calculate_sys_precision(void)
|
||||
|
||||
assert(best > 0);
|
||||
|
||||
precision_quantum = 1.0e-9 * best;
|
||||
|
||||
/* Get rounded log2 value of the measured precision */
|
||||
precision_log = 0;
|
||||
while (best < 707106781) {
|
||||
precision_log--;
|
||||
best *= 2;
|
||||
}
|
||||
|
||||
assert(precision_log >= -30);
|
||||
|
||||
DEBUG_LOG("Clock precision %.9f (%d)", precision_quantum, precision_log);
|
||||
return 1.0e-9 * best;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -170,7 +159,16 @@ LCL_Initialise(void)
|
||||
current_freq_ppm = 0.0;
|
||||
temp_comp_ppm = 0.0;
|
||||
|
||||
calculate_sys_precision();
|
||||
precision_quantum = CNF_GetClockPrecision();
|
||||
if (precision_quantum <= 0.0)
|
||||
precision_quantum = measure_clock_precision();
|
||||
|
||||
precision_quantum = CLAMP(1.0e-9, precision_quantum, 1.0);
|
||||
precision_log = round(log(precision_quantum) / log(2.0));
|
||||
/* NTP code doesn't support smaller log than -30 */
|
||||
assert(precision_log >= -30);
|
||||
|
||||
DEBUG_LOG("Clock precision %.9f (%d)", precision_quantum, precision_log);
|
||||
|
||||
/* This is the maximum allowed frequency offset in ppm, the time must
|
||||
never stop or run backwards */
|
||||
@@ -185,13 +183,11 @@ LCL_Initialise(void)
|
||||
void
|
||||
LCL_Finalise(void)
|
||||
{
|
||||
while (change_list.next != &change_list)
|
||||
LCL_RemoveParameterChangeHandler(change_list.next->handler,
|
||||
change_list.next->anything);
|
||||
|
||||
while (dispersion_notify_list.next != &dispersion_notify_list)
|
||||
LCL_RemoveDispersionNotifyHandler(dispersion_notify_list.next->handler,
|
||||
dispersion_notify_list.next->anything);
|
||||
/* Make sure all handlers have been removed */
|
||||
if (change_list.next != &change_list)
|
||||
assert(0);
|
||||
if (dispersion_notify_list.next != &dispersion_notify_list)
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -509,7 +505,7 @@ LCL_AccumulateDeltaFrequency(double dfreq)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
int
|
||||
LCL_AccumulateOffset(double offset, double corr_rate)
|
||||
{
|
||||
struct timespec raw, cooked;
|
||||
@@ -521,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;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -590,7 +588,7 @@ LCL_NotifyLeap(int leap)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
int
|
||||
LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate)
|
||||
{
|
||||
struct timespec raw, cooked;
|
||||
@@ -602,7 +600,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;
|
||||
|
||||
@@ -624,6 +622,8 @@ 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;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -691,6 +691,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)
|
||||
{
|
||||
|
||||
7
local.h
7
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,7 @@ 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);
|
||||
|
||||
/* Routine to read the system precision as a log to base 2 value. */
|
||||
extern int LCL_GetSysPrecisionAsLog(void);
|
||||
@@ -197,6 +197,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);
|
||||
|
||||
131
logging.c
131
logging.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2011-2014
|
||||
* Copyright (C) Miroslav Lichvar 2011-2014, 2018-2020
|
||||
*
|
||||
* 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
|
||||
@@ -29,26 +29,25 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include "conf.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "util.h"
|
||||
|
||||
/* This is used by DEBUG_LOG macro */
|
||||
int log_debug_enabled = 0;
|
||||
LOG_Severity log_min_severity = LOGS_INFO;
|
||||
|
||||
/* ================================================== */
|
||||
/* Flag indicating we have initialised */
|
||||
static int initialised = 0;
|
||||
|
||||
static FILE *file_log;
|
||||
static FILE *file_log = NULL;
|
||||
static int system_log = 0;
|
||||
|
||||
static int parent_fd = 0;
|
||||
|
||||
#define DEBUG_LEVEL_PRINT_FUNCTION 2
|
||||
#define DEBUG_LEVEL_PRINT_DEBUG 2
|
||||
static int debug_level = 0;
|
||||
|
||||
struct LogFile {
|
||||
const char *name;
|
||||
const char *banner;
|
||||
@@ -63,14 +62,18 @@ static int n_filelogs = 0;
|
||||
|
||||
static struct LogFile logfiles[MAX_FILELOGS];
|
||||
|
||||
/* Global prefix for debug messages */
|
||||
static char *debug_prefix;
|
||||
|
||||
/* ================================================== */
|
||||
/* Init function */
|
||||
|
||||
void
|
||||
LOG_Initialise(void)
|
||||
{
|
||||
debug_prefix = Strdup("");
|
||||
initialised = 1;
|
||||
file_log = stderr;
|
||||
LOG_OpenFileLog(NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -79,14 +82,16 @@ LOG_Initialise(void)
|
||||
void
|
||||
LOG_Finalise(void)
|
||||
{
|
||||
if (system_log) {
|
||||
if (system_log)
|
||||
closelog();
|
||||
} else {
|
||||
|
||||
if (file_log)
|
||||
fclose(file_log);
|
||||
}
|
||||
|
||||
LOG_CycleLogFiles();
|
||||
|
||||
Free(debug_prefix);
|
||||
|
||||
initialised = 0;
|
||||
}
|
||||
|
||||
@@ -116,7 +121,7 @@ static void log_message(int fatal, LOG_Severity severity, const char *message)
|
||||
assert(0);
|
||||
}
|
||||
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
|
||||
} else {
|
||||
} else if (file_log) {
|
||||
fprintf(file_log, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||
}
|
||||
}
|
||||
@@ -132,17 +137,21 @@ void LOG_Message(LOG_Severity severity,
|
||||
char buf[2048];
|
||||
va_list other_args;
|
||||
time_t t;
|
||||
struct tm stm;
|
||||
struct tm *tm;
|
||||
|
||||
if (!system_log) {
|
||||
assert(initialised);
|
||||
|
||||
if (!system_log && file_log && severity >= log_min_severity) {
|
||||
/* Don't clutter up syslog with timestamps and internal debugging info */
|
||||
time(&t);
|
||||
stm = *gmtime(&t);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
|
||||
fprintf(file_log, "%s ", buf);
|
||||
tm = gmtime(&t);
|
||||
if (tm) {
|
||||
strftime(buf, sizeof (buf), "%Y-%m-%dT%H:%M:%SZ", tm);
|
||||
fprintf(file_log, "%s ", buf);
|
||||
}
|
||||
#if DEBUG > 0
|
||||
if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
|
||||
fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
|
||||
if (log_min_severity <= LOGS_DEBUG)
|
||||
fprintf(file_log, "%s%s:%d:(%s) ", debug_prefix, filename, line_number, function_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -155,22 +164,23 @@ void LOG_Message(LOG_Severity severity,
|
||||
case LOGS_INFO:
|
||||
case LOGS_WARN:
|
||||
case LOGS_ERR:
|
||||
log_message(0, severity, buf);
|
||||
if (severity >= log_min_severity)
|
||||
log_message(0, severity, buf);
|
||||
break;
|
||||
case LOGS_FATAL:
|
||||
log_message(1, severity, buf);
|
||||
if (severity >= log_min_severity)
|
||||
log_message(1, severity, buf);
|
||||
|
||||
/* With syslog, send the message also to the grandparent
|
||||
process or write it to stderr if not detached */
|
||||
if (system_log) {
|
||||
if (parent_fd > 0) {
|
||||
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
||||
; /* Not much we can do here */
|
||||
} else if (parent_fd == 0) {
|
||||
system_log = 0;
|
||||
log_message(1, severity, buf);
|
||||
}
|
||||
/* Send the message also to the foreground process if it is
|
||||
still running, or stderr if it is still open */
|
||||
if (parent_fd > 0) {
|
||||
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
||||
; /* Not much we can do here */
|
||||
} else if (system_log && parent_fd == 0) {
|
||||
system_log = 0;
|
||||
log_message(1, severity, buf);
|
||||
}
|
||||
exit(1);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
@@ -184,13 +194,18 @@ LOG_OpenFileLog(const char *log_file)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
f = fopen(log_file, "a");
|
||||
if (!f)
|
||||
LOG_FATAL("Could not open log file %s", log_file);
|
||||
if (log_file) {
|
||||
f = UTI_OpenFile(NULL, log_file, NULL, 'A', 0640);
|
||||
} else {
|
||||
f = stderr;
|
||||
}
|
||||
|
||||
/* Enable line buffering */
|
||||
setvbuf(f, NULL, _IOLBF, BUFSIZ);
|
||||
|
||||
if (file_log && file_log != stderr)
|
||||
fclose(file_log);
|
||||
|
||||
file_log = f;
|
||||
}
|
||||
|
||||
@@ -206,12 +221,27 @@ LOG_OpenSystemLog(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void LOG_SetDebugLevel(int level)
|
||||
void LOG_SetMinSeverity(LOG_Severity severity)
|
||||
{
|
||||
debug_level = level;
|
||||
if (level >= DEBUG_LEVEL_PRINT_DEBUG) {
|
||||
log_debug_enabled = 1;
|
||||
}
|
||||
/* Don't print any debug messages in a non-debug build */
|
||||
log_min_severity = CLAMP(DEBUG > 0 ? LOGS_DEBUG : LOGS_INFO, severity, LOGS_FATAL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
LOG_Severity
|
||||
LOG_GetMinSeverity(void)
|
||||
{
|
||||
return log_min_severity;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_SetDebugPrefix(const char *prefix)
|
||||
{
|
||||
Free(debug_prefix);
|
||||
debug_prefix = Strdup(prefix);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -220,6 +250,8 @@ void
|
||||
LOG_SetParentFd(int fd)
|
||||
{
|
||||
parent_fd = fd;
|
||||
if (file_log == stderr)
|
||||
file_log = NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -237,7 +269,10 @@ LOG_CloseParentFd()
|
||||
LOG_FileID
|
||||
LOG_FileOpen(const char *name, const char *banner)
|
||||
{
|
||||
assert(n_filelogs < MAX_FILELOGS);
|
||||
if (n_filelogs >= MAX_FILELOGS) {
|
||||
assert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
logfiles[n_filelogs].name = name;
|
||||
logfiles[n_filelogs].banner = banner;
|
||||
@@ -259,24 +294,20 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
|
||||
return;
|
||||
|
||||
if (!logfiles[id].file) {
|
||||
char filename[512], *logdir = CNF_GetLogDir();
|
||||
char *logdir = CNF_GetLogDir();
|
||||
|
||||
if (logdir[0] == '\0') {
|
||||
if (!logdir) {
|
||||
LOG(LOGS_WARN, "logdir not specified");
|
||||
logfiles[id].name = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
if (snprintf(filename, sizeof(filename), "%s/%s.log",
|
||||
logdir, logfiles[id].name) >= sizeof (filename) ||
|
||||
!(logfiles[id].file = fopen(filename, "a"))) {
|
||||
LOG(LOGS_WARN, "Could not open log file %s", filename);
|
||||
logfiles[id].file = UTI_OpenFile(logdir, logfiles[id].name, ".log", 'a', 0644);
|
||||
if (!logfiles[id].file) {
|
||||
/* Disable the log */
|
||||
logfiles[id].name = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Close on exec */
|
||||
UTI_FdSetCloexec(fileno(logfiles[id].file));
|
||||
}
|
||||
|
||||
banner = CNF_GetLogBanner();
|
||||
@@ -284,7 +315,7 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
|
||||
char bannerline[256];
|
||||
int i, bannerlen;
|
||||
|
||||
bannerlen = strlen(logfiles[id].banner);
|
||||
bannerlen = MIN(strlen(logfiles[id].banner), sizeof (bannerline) - 1);
|
||||
|
||||
for (i = 0; i < bannerlen; i++)
|
||||
bannerline[i] = '=';
|
||||
|
||||
32
logging.h
32
logging.h
@@ -31,9 +31,6 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
/* Flag indicating whether debug messages are logged */
|
||||
extern int log_debug_enabled;
|
||||
|
||||
/* Line logging macros. If the compiler is GNU C, we take advantage of
|
||||
being able to get the function name also. */
|
||||
|
||||
@@ -55,7 +52,7 @@ extern int log_debug_enabled;
|
||||
|
||||
#define DEBUG_LOG(...) \
|
||||
do { \
|
||||
if (DEBUG && log_debug_enabled) \
|
||||
if (DEBUG && log_min_severity == LOGS_DEBUG) \
|
||||
LOG_MESSAGE(LOGS_DEBUG, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
@@ -69,13 +66,16 @@ extern int log_debug_enabled;
|
||||
|
||||
/* Definition of severity */
|
||||
typedef enum {
|
||||
LOGS_INFO,
|
||||
LOGS_DEBUG = -1,
|
||||
LOGS_INFO = 0,
|
||||
LOGS_WARN,
|
||||
LOGS_ERR,
|
||||
LOGS_FATAL,
|
||||
LOGS_DEBUG
|
||||
} LOG_Severity;
|
||||
|
||||
/* Minimum severity of messages to be logged */
|
||||
extern LOG_Severity log_min_severity;
|
||||
|
||||
/* Init function */
|
||||
extern void LOG_Initialise(void);
|
||||
|
||||
@@ -92,20 +92,24 @@ FORMAT_ATTRIBUTE_PRINTF(2, 3)
|
||||
extern void LOG_Message(LOG_Severity severity, const char *format, ...);
|
||||
#endif
|
||||
|
||||
/* Set debug level:
|
||||
0, 1 - only non-debug messages are logged
|
||||
2 - debug messages are logged too, all messages are prefixed with
|
||||
filename, line, and function name
|
||||
*/
|
||||
extern void LOG_SetDebugLevel(int level);
|
||||
/* Set the minimum severity of a message to be logged or printed to terminal.
|
||||
If the severity is LOGS_DEBUG and DEBUG is enabled, all messages will be
|
||||
prefixed with the filename, line number, and function name. */
|
||||
extern void LOG_SetMinSeverity(LOG_Severity severity);
|
||||
|
||||
/* Log messages to a file instead of stderr */
|
||||
/* Get the minimum severity */
|
||||
extern LOG_Severity LOG_GetMinSeverity(void);
|
||||
|
||||
/* Set a prefix for debug messages */
|
||||
extern void LOG_SetDebugPrefix(const char *prefix);
|
||||
|
||||
/* Log messages to a file instead of stderr, or stderr again if NULL */
|
||||
extern void LOG_OpenFileLog(const char *log_file);
|
||||
|
||||
/* Log messages to syslog instead of stderr */
|
||||
extern void LOG_OpenSystemLog(void);
|
||||
|
||||
/* Send fatal message also to the foreground process */
|
||||
/* Stop using stderr and send fatal message to the foreground process */
|
||||
extern void LOG_SetParentFd(int fd);
|
||||
|
||||
/* Close the pipe to the foreground process so it can exit */
|
||||
|
||||
157
main.c
157
main.c
@@ -4,7 +4,7 @@
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) John G. Hasler 2009
|
||||
* Copyright (C) Miroslav Lichvar 2012-2017
|
||||
* Copyright (C) Miroslav Lichvar 2012-2020
|
||||
*
|
||||
* 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,9 @@
|
||||
#include "ntp_signd.h"
|
||||
#include "ntp_sources.h"
|
||||
#include "ntp_core.h"
|
||||
#include "nts_ke_server.h"
|
||||
#include "nts_ntp_server.h"
|
||||
#include "socket.h"
|
||||
#include "sources.h"
|
||||
#include "sourcestats.h"
|
||||
#include "reference.h"
|
||||
@@ -87,11 +90,11 @@ delete_pidfile(void)
|
||||
{
|
||||
const char *pidfile = CNF_GetPidFile();
|
||||
|
||||
if (!pidfile[0])
|
||||
if (!pidfile)
|
||||
return;
|
||||
|
||||
/* Don't care if this fails, there's not a lot we can do */
|
||||
unlink(pidfile);
|
||||
if (!UTI_RemoveFile(NULL, pidfile, NULL))
|
||||
;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -101,9 +104,8 @@ MAI_CleanupAndExit(void)
|
||||
{
|
||||
if (!initialised) exit(exit_status);
|
||||
|
||||
if (CNF_GetDumpDir()[0] != '\0') {
|
||||
SRC_DumpSources();
|
||||
}
|
||||
LCL_CancelOffsetCorrection();
|
||||
SRC_DumpSources();
|
||||
|
||||
/* Don't update clock when removing sources */
|
||||
REF_SetMode(REF_ModeIgnore);
|
||||
@@ -112,18 +114,23 @@ MAI_CleanupAndExit(void)
|
||||
TMC_Finalise();
|
||||
MNL_Finalise();
|
||||
CLG_Finalise();
|
||||
NKS_Finalise();
|
||||
NNS_Finalise();
|
||||
NSD_Finalise();
|
||||
NSR_Finalise();
|
||||
SST_Finalise();
|
||||
NCR_Finalise();
|
||||
NIO_Finalise();
|
||||
CAM_Finalise();
|
||||
|
||||
KEY_Finalise();
|
||||
RCL_Finalise();
|
||||
SRC_Finalise();
|
||||
REF_Finalise();
|
||||
RTC_Finalise();
|
||||
SYS_Finalise();
|
||||
|
||||
SCK_Finalise();
|
||||
SCH_Finalise();
|
||||
LCL_Finalise();
|
||||
PRV_Finalise();
|
||||
@@ -142,7 +149,6 @@ MAI_CleanupAndExit(void)
|
||||
static void
|
||||
signal_cleanup(int x)
|
||||
{
|
||||
if (!initialised) exit(0);
|
||||
SCH_QuitProgram();
|
||||
}
|
||||
|
||||
@@ -178,7 +184,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();
|
||||
}
|
||||
@@ -253,7 +259,10 @@ check_pidfile(void)
|
||||
FILE *in;
|
||||
int pid, count;
|
||||
|
||||
in = fopen(pidfile, "r");
|
||||
if (!pidfile)
|
||||
return;
|
||||
|
||||
in = UTI_OpenFile(NULL, pidfile, NULL, 'r', 0);
|
||||
if (!in)
|
||||
return;
|
||||
|
||||
@@ -278,20 +287,18 @@ write_pidfile(void)
|
||||
const char *pidfile = CNF_GetPidFile();
|
||||
FILE *out;
|
||||
|
||||
if (!pidfile[0])
|
||||
if (!pidfile)
|
||||
return;
|
||||
|
||||
out = fopen(pidfile, "w");
|
||||
if (!out) {
|
||||
LOG_FATAL("Could not open %s : %s", pidfile, strerror(errno));
|
||||
} else {
|
||||
fprintf(out, "%d\n", (int)getpid());
|
||||
fclose(out);
|
||||
}
|
||||
out = UTI_OpenFile(NULL, pidfile, NULL, 'W', 0644);
|
||||
fprintf(out, "%d\n", (int)getpid());
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define DEV_NULL "/dev/null"
|
||||
|
||||
static void
|
||||
go_daemon(void)
|
||||
{
|
||||
@@ -352,6 +359,13 @@ go_daemon(void)
|
||||
}
|
||||
|
||||
LOG_SetParentFd(pipefd[1]);
|
||||
|
||||
/* Open /dev/null as new stdin/out/err */
|
||||
errno = 0;
|
||||
if (open(DEV_NULL, O_RDONLY) != STDIN_FILENO ||
|
||||
open(DEV_NULL, O_WRONLY) != STDOUT_FILENO ||
|
||||
open(DEV_NULL, O_RDWR) != STDERR_FILENO)
|
||||
LOG_FATAL("Could not open %s : %s", DEV_NULL, strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -361,8 +375,34 @@ go_daemon(void)
|
||||
static void
|
||||
print_help(const char *progname)
|
||||
{
|
||||
printf("Usage: %s [-4|-6] [-n|-d] [-q|-Q] [-r] [-R] [-s] [-t TIMEOUT] [-f FILE|COMMAND...]\n",
|
||||
progname);
|
||||
printf("Usage: %s [OPTION]... [DIRECTIVE]...\n\n"
|
||||
"Options:\n"
|
||||
" -4\t\tUse IPv4 addresses only\n"
|
||||
" -6\t\tUse IPv6 addresses only\n"
|
||||
" -f FILE\tSpecify configuration file (%s)\n"
|
||||
" -n\t\tDon't run as daemon\n"
|
||||
" -d\t\tDon't run as daemon and log to stderr\n"
|
||||
#if DEBUG > 0
|
||||
" -d -d\t\tEnable debug messages\n"
|
||||
#endif
|
||||
" -l FILE\tLog to file\n"
|
||||
" -L LEVEL\tSet logging threshold (0)\n"
|
||||
" -p\t\tPrint configuration and exit\n"
|
||||
" -q\t\tSet clock and exit\n"
|
||||
" -Q\t\tLog offset and exit\n"
|
||||
" -r\t\tReload dump files\n"
|
||||
" -R\t\tAdapt configuration for restart\n"
|
||||
" -s\t\tSet clock from RTC\n"
|
||||
" -t SECONDS\tExit after elapsed time\n"
|
||||
" -u USER\tSpecify user (%s)\n"
|
||||
" -U\t\tDon't check for root\n"
|
||||
" -F LEVEL\tSet system call filter level (0)\n"
|
||||
" -P PRIORITY\tSet process priority (0)\n"
|
||||
" -m\t\tLock memory\n"
|
||||
" -x\t\tDon't control clock\n"
|
||||
" -v, --version\tPrint version and exit\n"
|
||||
" -h, --help\tPrint usage and exit\n",
|
||||
progname, DEFAULT_CONF_FILE, DEFAULT_USER);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -395,16 +435,16 @@ int main
|
||||
char *user = NULL, *log_file = NULL;
|
||||
struct passwd *pw;
|
||||
int opt, debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
|
||||
int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = 0;
|
||||
int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = -1;
|
||||
int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
|
||||
int clock_control = 1, system_log = 1;
|
||||
int config_args = 0;
|
||||
int clock_control = 1, system_log = 1, log_severity = LOGS_INFO;
|
||||
int user_check = 1, config_args = 0, print_config = 0;
|
||||
|
||||
do_platform_checks();
|
||||
|
||||
LOG_Initialise();
|
||||
|
||||
/* Parse (undocumented) long command-line options */
|
||||
/* Parse long command-line options */
|
||||
for (optind = 1; optind < argc; optind++) {
|
||||
if (!strcmp("--help", argv[optind])) {
|
||||
print_help(progname);
|
||||
@@ -418,7 +458,7 @@ int main
|
||||
optind = 1;
|
||||
|
||||
/* Parse short command-line options */
|
||||
while ((opt = getopt(argc, argv, "46df:F:hl:mnP:qQrRst:u:vx")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "46df:F:hl:L:mnpP:qQrRst:u:Uvx")) != -1) {
|
||||
switch (opt) {
|
||||
case '4':
|
||||
case '6':
|
||||
@@ -438,12 +478,22 @@ int main
|
||||
case 'l':
|
||||
log_file = optarg;
|
||||
break;
|
||||
case 'L':
|
||||
log_severity = parse_int_arg(optarg);
|
||||
break;
|
||||
case 'm':
|
||||
lock_memory = 1;
|
||||
break;
|
||||
case 'n':
|
||||
nofork = 1;
|
||||
break;
|
||||
case 'p':
|
||||
print_config = 1;
|
||||
user_check = 0;
|
||||
nofork = 1;
|
||||
system_log = 0;
|
||||
log_severity = LOGS_WARN;
|
||||
break;
|
||||
case 'P':
|
||||
sched_priority = parse_int_arg(optarg);
|
||||
break;
|
||||
@@ -457,6 +507,7 @@ int main
|
||||
ref_mode = REF_ModePrintOnce;
|
||||
nofork = 1;
|
||||
client_only = 1;
|
||||
user_check = 0;
|
||||
clock_control = 0;
|
||||
system_log = 0;
|
||||
break;
|
||||
@@ -475,6 +526,9 @@ int main
|
||||
case 'u':
|
||||
user = optarg;
|
||||
break;
|
||||
case 'U':
|
||||
user_check = 0;
|
||||
break;
|
||||
case 'v':
|
||||
print_version();
|
||||
return 0;
|
||||
@@ -487,7 +541,7 @@ int main
|
||||
}
|
||||
}
|
||||
|
||||
if (getuid() && !client_only)
|
||||
if (user_check && getuid() != 0)
|
||||
LOG_FATAL("Not superuser");
|
||||
|
||||
/* Turn into a daemon */
|
||||
@@ -501,13 +555,15 @@ int main
|
||||
LOG_OpenSystemLog();
|
||||
}
|
||||
|
||||
LOG_SetDebugLevel(debug);
|
||||
LOG_SetMinSeverity(debug >= 2 ? LOGS_DEBUG : log_severity);
|
||||
|
||||
LOG(LOGS_INFO, "chronyd version %s starting (%s)", CHRONY_VERSION, CHRONYD_FEATURES);
|
||||
|
||||
DNS_SetAddressFamily(address_family);
|
||||
|
||||
CNF_Initialise(restarted, client_only);
|
||||
if (print_config)
|
||||
CNF_EnablePrint();
|
||||
|
||||
/* Parse the config file or the remaining command line arguments */
|
||||
config_args = argc - optind;
|
||||
@@ -518,15 +574,33 @@ int main
|
||||
CNF_ParseLine(NULL, config_args + optind - argc + 1, argv[optind]);
|
||||
}
|
||||
|
||||
if (print_config)
|
||||
return 0;
|
||||
|
||||
/* Check whether another chronyd may already be running */
|
||||
check_pidfile();
|
||||
|
||||
/* Write our pidfile to prevent other chronyds running */
|
||||
if (!user)
|
||||
user = CNF_GetUser();
|
||||
|
||||
pw = getpwnam(user);
|
||||
if (!pw)
|
||||
LOG_FATAL("Could not get user/group ID of %s", user);
|
||||
|
||||
/* Create directories for sockets, log files, and dump files */
|
||||
CNF_CreateDirs(pw->pw_uid, pw->pw_gid);
|
||||
|
||||
/* Write our pidfile to prevent other instances from running */
|
||||
write_pidfile();
|
||||
|
||||
PRV_Initialise();
|
||||
LCL_Initialise();
|
||||
SCH_Initialise();
|
||||
SCK_Initialise(address_family);
|
||||
|
||||
/* Start helper processes if needed */
|
||||
NKS_PreInitialise(pw->pw_uid, pw->pw_gid, scfilter_level);
|
||||
|
||||
SYS_Initialise(clock_control);
|
||||
RTC_Initialise(do_init_rtc);
|
||||
SRC_Initialise();
|
||||
@@ -534,8 +608,8 @@ int main
|
||||
KEY_Initialise();
|
||||
|
||||
/* Open privileged ports before dropping root */
|
||||
CAM_Initialise(address_family);
|
||||
NIO_Initialise(address_family);
|
||||
CAM_Initialise();
|
||||
NIO_Initialise();
|
||||
NCR_Initialise();
|
||||
CNF_SetupAccessRestrictions();
|
||||
|
||||
@@ -551,24 +625,19 @@ int main
|
||||
SYS_LockMemory();
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
user = CNF_GetUser();
|
||||
}
|
||||
|
||||
if ((pw = getpwnam(user)) == NULL)
|
||||
LOG_FATAL("Could not get %s uid/gid", user);
|
||||
|
||||
/* Create all directories before dropping root */
|
||||
CNF_CreateDirs(pw->pw_uid, pw->pw_gid);
|
||||
|
||||
/* Drop root privileges if the specified user has a non-zero UID */
|
||||
if (!geteuid() && (pw->pw_uid || pw->pw_gid))
|
||||
SYS_DropRoot(pw->pw_uid, pw->pw_gid);
|
||||
SYS_DropRoot(pw->pw_uid, pw->pw_gid, SYS_MAIN_PROCESS);
|
||||
|
||||
if (!geteuid())
|
||||
LOG(LOGS_WARN, "Running with root privileges");
|
||||
|
||||
REF_Initialise();
|
||||
SST_Initialise();
|
||||
NSR_Initialise();
|
||||
NSD_Initialise();
|
||||
NNS_Initialise();
|
||||
NKS_Initialise();
|
||||
CLG_Initialise();
|
||||
MNL_Initialise();
|
||||
TMC_Initialise();
|
||||
@@ -577,12 +646,12 @@ int main
|
||||
/* From now on, it is safe to do finalisation on exit */
|
||||
initialised = 1;
|
||||
|
||||
UTI_SetQuitSignalsHandler(signal_cleanup);
|
||||
UTI_SetQuitSignalsHandler(signal_cleanup, 1);
|
||||
|
||||
CAM_OpenUnixSocket();
|
||||
|
||||
if (scfilter_level)
|
||||
SYS_EnableSystemCallFilter(scfilter_level);
|
||||
SYS_EnableSystemCallFilter(scfilter_level, SYS_MAIN_PROCESS);
|
||||
|
||||
if (ref_mode == REF_ModeNormal && CNF_GetInitSources() > 0) {
|
||||
ref_mode = REF_ModeInitStepSlew;
|
||||
@@ -591,7 +660,7 @@ int main
|
||||
REF_SetModeEndHandler(reference_mode_end);
|
||||
REF_SetMode(ref_mode);
|
||||
|
||||
if (timeout > 0)
|
||||
if (timeout >= 0)
|
||||
SCH_AddTimeoutByDelay(timeout, quit_timeout, NULL);
|
||||
|
||||
if (do_init_rtc) {
|
||||
|
||||
@@ -36,8 +36,6 @@ cd RELEASES/$subdir || exit 1
|
||||
|
||||
echo $version > version.txt
|
||||
|
||||
sed -i -e "s%@@VERSION@@%${version}%" examples/chrony.spec
|
||||
|
||||
./configure && make -C doc man txt || exit 1
|
||||
|
||||
iconv -f utf-8 -t ascii//TRANSLIT < doc/installation.txt > INSTALL
|
||||
|
||||
1
manual.c
1
manual.c
@@ -92,6 +92,7 @@ MNL_Initialise(void)
|
||||
void
|
||||
MNL_Finalise(void)
|
||||
{
|
||||
LCL_RemoveParameterChangeHandler(slew_samples, NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
2
memory.h
2
memory.h
@@ -27,6 +27,8 @@
|
||||
#ifndef GOT_MEMORY_H
|
||||
#define GOT_MEMORY_H
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
/* Wrappers checking for errors */
|
||||
extern void *Malloc(size_t size);
|
||||
extern void *Realloc(void *ptr, size_t size);
|
||||
|
||||
110
nameserv.c
110
nameserv.c
@@ -30,7 +30,11 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <netdb.h>
|
||||
#include <resolv.h>
|
||||
|
||||
#include "nameserv.h"
|
||||
#include "socket.h"
|
||||
#include "util.h"
|
||||
|
||||
/* ================================================== */
|
||||
@@ -46,15 +50,39 @@ 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));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
switch (address_family) {
|
||||
case IPADDR_INET4:
|
||||
hints.ai_family = AF_INET;
|
||||
break;
|
||||
#ifdef FEAT_IPV6
|
||||
case IPADDR_INET6:
|
||||
hints.ai_family = AF_INET6;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
}
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
result = getaddrinfo(name, NULL, &hints, &res);
|
||||
|
||||
@@ -79,6 +107,9 @@ DNS_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
||||
case AF_INET6:
|
||||
if (address_family != IPADDR_UNSPEC && address_family != IPADDR_INET6)
|
||||
continue;
|
||||
/* Don't return an address that would lose a scope ID */
|
||||
if (((struct sockaddr_in6 *)ai->ai_addr)->sin6_scope_id != 0)
|
||||
continue;
|
||||
ip_addrs[i].family = IPADDR_INET6;
|
||||
memcpy(&ip_addrs[i].addr.in6, &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr.s6_addr,
|
||||
sizeof (ip_addrs->addr.in6));
|
||||
@@ -88,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
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -138,35 +130,21 @@ 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];
|
||||
|
||||
slen = UTI_IPAndPortToSockaddr(ip_addr, 0, (struct sockaddr *)&in6);
|
||||
if (!getnameinfo((struct sockaddr *)&in6, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
||||
result = hbuf;
|
||||
#else
|
||||
struct hostent *host;
|
||||
uint32_t addr;
|
||||
ip_saddr.ip_addr = *ip_addr;
|
||||
ip_saddr.port = 0;
|
||||
|
||||
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
|
||||
slen = SCK_IPSockAddrToSockaddr(&ip_saddr, (struct sockaddr *)&saddr, sizeof (saddr));
|
||||
if (!getnameinfo((struct sockaddr *)&saddr, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
||||
result = hbuf;
|
||||
|
||||
if (result == NULL)
|
||||
result = UTI_IPToString(ip_addr);
|
||||
|
||||
@@ -51,7 +51,7 @@ struct DNS_Async_Instance {
|
||||
int pipe[2];
|
||||
};
|
||||
|
||||
static int resolving_threads = 0;
|
||||
static pthread_mutex_t privops_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -60,7 +60,9 @@ start_resolving(void *anything)
|
||||
{
|
||||
struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything;
|
||||
|
||||
pthread_mutex_lock(&privops_lock);
|
||||
inst->status = PRV_Name2IPAddress(inst->name, inst->addresses, DNS_MAX_ADDRESSES);
|
||||
pthread_mutex_unlock(&privops_lock);
|
||||
|
||||
/* Notify the main thread that the result is ready */
|
||||
if (write(inst->pipe[1], "", 1) < 0)
|
||||
@@ -81,8 +83,6 @@ end_resolving(int fd, int event, void *anything)
|
||||
LOG_FATAL("pthread_join() failed");
|
||||
}
|
||||
|
||||
resolving_threads--;
|
||||
|
||||
SCH_RemoveFileHandler(inst->pipe[0]);
|
||||
close(inst->pipe[0]);
|
||||
close(inst->pipe[1]);
|
||||
@@ -116,9 +116,6 @@ DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *
|
||||
UTI_FdSetCloexec(inst->pipe[0]);
|
||||
UTI_FdSetCloexec(inst->pipe[1]);
|
||||
|
||||
resolving_threads++;
|
||||
assert(resolving_threads <= 1);
|
||||
|
||||
if (pthread_create(&inst->thread, NULL, start_resolving, inst)) {
|
||||
LOG_FATAL("pthread_create() failed");
|
||||
}
|
||||
|
||||
73
ntp.h
73
ntp.h
@@ -47,18 +47,21 @@ typedef uint32_t NTP_int32;
|
||||
/* Maximum stratum number (infinity) */
|
||||
#define NTP_MAX_STRATUM 16
|
||||
|
||||
/* The minimum valid length of an extension field */
|
||||
#define NTP_MIN_EXTENSION_LENGTH 16
|
||||
|
||||
/* The maximum assumed length of all extension fields in received
|
||||
packets (RFC 5905 doesn't specify a limit on length or number of
|
||||
extension fields in one packet) */
|
||||
#define NTP_MAX_EXTENSIONS_LENGTH 1024
|
||||
/* Invalid stratum number */
|
||||
#define NTP_INVALID_STRATUM 0
|
||||
|
||||
/* The minimum and maximum supported length of MAC */
|
||||
#define NTP_MIN_MAC_LENGTH (4 + 16)
|
||||
#define NTP_MAX_MAC_LENGTH (4 + MAX_HASH_LENGTH)
|
||||
|
||||
/* The minimum valid length of an extension field */
|
||||
#define NTP_MIN_EF_LENGTH 16
|
||||
|
||||
/* The maximum assumed length of all extension fields in an NTP packet,
|
||||
including a MAC (RFC 5905 doesn't specify a limit on length or number of
|
||||
extension fields in one packet) */
|
||||
#define NTP_MAX_EXTENSIONS_LENGTH (1024 + NTP_MAX_MAC_LENGTH)
|
||||
|
||||
/* The maximum length of MAC in NTPv4 packets which allows deterministic
|
||||
parsing of extension fields (RFC 7822) */
|
||||
#define NTP_MAX_V4_MAC_LENGTH (4 + 20)
|
||||
@@ -93,21 +96,10 @@ typedef struct {
|
||||
NTP_int64 receive_ts;
|
||||
NTP_int64 transmit_ts;
|
||||
|
||||
/* Optional extension fields, we don't send packets with them yet */
|
||||
/* uint8_t extensions[] */
|
||||
|
||||
/* Optional message authentication code (MAC) */
|
||||
NTP_int32 auth_keyid;
|
||||
uint8_t auth_data[NTP_MAX_MAC_LENGTH - 4];
|
||||
uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
} NTP_Packet;
|
||||
|
||||
#define NTP_NORMAL_PACKET_LENGTH (int)offsetof(NTP_Packet, auth_keyid)
|
||||
|
||||
/* The buffer used to hold a datagram read from the network */
|
||||
typedef struct {
|
||||
NTP_Packet ntp_pkt;
|
||||
uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
} NTP_Receive_Buffer;
|
||||
#define NTP_HEADER_LENGTH (int)offsetof(NTP_Packet, extensions)
|
||||
|
||||
/* Macros to work with the lvm field */
|
||||
#define NTP_LVM_TO_LEAP(lvm) (((lvm) >> 6) & 0x3)
|
||||
@@ -121,4 +113,45 @@ typedef struct {
|
||||
#define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
|
||||
#define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */
|
||||
|
||||
/* Enumeration for authentication modes of NTP packets */
|
||||
typedef enum {
|
||||
NTP_AUTH_NONE = 0, /* No authentication */
|
||||
NTP_AUTH_SYMMETRIC, /* NTP MAC or CMAC using a symmetric key
|
||||
(RFC 1305, RFC 5905, RFC 8573) */
|
||||
NTP_AUTH_MSSNTP, /* MS-SNTP authenticator field */
|
||||
NTP_AUTH_MSSNTP_EXT, /* MS-SNTP extended authenticator field */
|
||||
NTP_AUTH_NTS, /* Network Time Security (RFC 8915) */
|
||||
} NTP_AuthMode;
|
||||
|
||||
/* Structure describing an NTP packet */
|
||||
typedef struct {
|
||||
int length;
|
||||
int version;
|
||||
NTP_Mode mode;
|
||||
|
||||
int ext_fields;
|
||||
|
||||
struct {
|
||||
NTP_AuthMode mode;
|
||||
struct {
|
||||
int start;
|
||||
int length;
|
||||
uint32_t key_id;
|
||||
} mac;
|
||||
} auth;
|
||||
} NTP_PacketInfo;
|
||||
|
||||
/* Structure used to save NTP measurements. time is the local time at which
|
||||
the sample is to be considered to have been made and offset is the offset at
|
||||
the time (positive indicates that the local clock is slow relative to the
|
||||
source). root_delay/root_dispersion include peer_delay/peer_dispersion. */
|
||||
typedef struct {
|
||||
struct timespec time;
|
||||
double offset;
|
||||
double peer_delay;
|
||||
double peer_dispersion;
|
||||
double root_delay;
|
||||
double root_dispersion;
|
||||
} NTP_Sample;
|
||||
|
||||
#endif /* GOT_NTP_H */
|
||||
|
||||
495
ntp_auth.c
Normal file
495
ntp_auth.c
Normal file
@@ -0,0 +1,495 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019-2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
NTP authentication
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "keys.h"
|
||||
#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"
|
||||
#include "nts_ntp_server.h"
|
||||
#include "srcparams.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Structure to hold authentication configuration and state */
|
||||
|
||||
struct NAU_Instance_Record {
|
||||
NTP_AuthMode mode; /* Authentication mode of NTP packets */
|
||||
uint32_t key_id; /* Identifier of a symmetric key */
|
||||
NNC_Instance nts; /* Client NTS state */
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
generate_symmetric_auth(uint32_t key_id, NTP_Packet *packet, NTP_PacketInfo *info)
|
||||
{
|
||||
int auth_len, max_auth_len;
|
||||
|
||||
if (info->length + NTP_MIN_MAC_LENGTH > sizeof (*packet)) {
|
||||
DEBUG_LOG("Packet too long");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Truncate long MACs in NTPv4 packets to allow deterministic parsing
|
||||
of extension fields (RFC 7822) */
|
||||
max_auth_len = (info->version == 4 ? NTP_MAX_V4_MAC_LENGTH : NTP_MAX_MAC_LENGTH) - 4;
|
||||
max_auth_len = MIN(max_auth_len, sizeof (*packet) - info->length - 4);
|
||||
|
||||
auth_len = KEY_GenerateAuth(key_id, packet, info->length,
|
||||
(unsigned char *)packet + info->length + 4, max_auth_len);
|
||||
if (auth_len < NTP_MIN_MAC_LENGTH - 4) {
|
||||
DEBUG_LOG("Could not generate auth data with key %"PRIu32, key_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*(uint32_t *)((unsigned char *)packet + info->length) = htonl(key_id);
|
||||
|
||||
info->auth.mac.start = info->length;
|
||||
info->auth.mac.length = 4 + auth_len;
|
||||
info->auth.mac.key_id = key_id;
|
||||
info->length += info->auth.mac.length;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_symmetric_auth(NTP_Packet *packet, NTP_PacketInfo *info)
|
||||
{
|
||||
int trunc_len;
|
||||
|
||||
if (info->auth.mac.length < NTP_MIN_MAC_LENGTH)
|
||||
return 0;
|
||||
|
||||
trunc_len = info->version == 4 && info->auth.mac.length <= NTP_MAX_V4_MAC_LENGTH ?
|
||||
NTP_MAX_V4_MAC_LENGTH : NTP_MAX_MAC_LENGTH;
|
||||
|
||||
if (!KEY_CheckAuth(info->auth.mac.key_id, packet, info->auth.mac.start,
|
||||
(unsigned char *)packet + info->auth.mac.start + 4,
|
||||
info->auth.mac.length - 4, trunc_len - 4))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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)
|
||||
{
|
||||
NAU_Instance instance;
|
||||
|
||||
instance = MallocNew(struct NAU_Instance_Record);
|
||||
instance->mode = mode;
|
||||
instance->key_id = INACTIVE_AUTHKEY;
|
||||
instance->nts = NULL;
|
||||
|
||||
assert(sizeof (instance->key_id) == 4);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NAU_Instance
|
||||
NAU_CreateNoneInstance(void)
|
||||
{
|
||||
return create_instance(NTP_AUTH_NONE);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NAU_Instance
|
||||
NAU_CreateSymmetricInstance(uint32_t key_id)
|
||||
{
|
||||
NAU_Instance instance = create_instance(NTP_AUTH_SYMMETRIC);
|
||||
|
||||
instance->key_id = key_id;
|
||||
|
||||
if (!KEY_KeyKnown(key_id))
|
||||
LOG(LOGS_WARN, "Key %"PRIu32" is %s", key_id, "missing");
|
||||
else if (!KEY_CheckKeyLength(key_id))
|
||||
LOG(LOGS_WARN, "Key %"PRIu32" is %s", key_id, "too short");
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NAU_Instance
|
||||
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, cert_set, ntp_port);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NAU_DestroyInstance(NAU_Instance instance)
|
||||
{
|
||||
if (instance->mode == NTP_AUTH_NTS)
|
||||
NNC_DestroyInstance(instance->nts);
|
||||
Free(instance);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_IsAuthEnabled(NAU_Instance instance)
|
||||
{
|
||||
return instance->mode != NTP_AUTH_NONE;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_GetSuggestedNtpVersion(NAU_Instance instance)
|
||||
{
|
||||
/* If the MAC in NTPv4 packets would be truncated, prefer NTPv3 for
|
||||
compatibility with older chronyd servers */
|
||||
if (instance->mode == NTP_AUTH_SYMMETRIC &&
|
||||
KEY_GetAuthLength(instance->key_id) + sizeof (instance->key_id) > NTP_MAX_V4_MAC_LENGTH)
|
||||
return 3;
|
||||
|
||||
return NTP_VERSION;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_PrepareRequestAuth(NAU_Instance instance)
|
||||
{
|
||||
switch (instance->mode) {
|
||||
case NTP_AUTH_NTS:
|
||||
if (!NNC_PrepareForAuth(instance->nts))
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_GenerateRequestAuth(NAU_Instance instance, NTP_Packet *request, NTP_PacketInfo *info)
|
||||
{
|
||||
switch (instance->mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
break;
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
if (!generate_symmetric_auth(instance->key_id, request, info))
|
||||
return 0;
|
||||
break;
|
||||
case NTP_AUTH_NTS:
|
||||
if (!NNC_GenerateRequestAuth(instance->nts, request, info))
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
info->auth.mode = instance->mode;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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)
|
||||
{
|
||||
*kod = 0;
|
||||
|
||||
switch (info->auth.mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
break;
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
if (!check_symmetric_auth(request, info))
|
||||
return 0;
|
||||
break;
|
||||
case NTP_AUTH_MSSNTP:
|
||||
/* MS-SNTP requests are not authenticated */
|
||||
break;
|
||||
case NTP_AUTH_MSSNTP_EXT:
|
||||
/* Not supported yet */
|
||||
return 0;
|
||||
case NTP_AUTH_NTS:
|
||||
if (!NNS_CheckRequestAuth(request, info, kod))
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *request_info,
|
||||
NTP_Packet *response, NTP_PacketInfo *response_info,
|
||||
NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
uint32_t kod)
|
||||
{
|
||||
switch (request_info->auth.mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
break;
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
if (!generate_symmetric_auth(request_info->auth.mac.key_id, response, response_info))
|
||||
return 0;
|
||||
break;
|
||||
case NTP_AUTH_MSSNTP:
|
||||
/* Sign the packet asynchronously by ntp_signd */
|
||||
if (!NSD_SignAndSendPacket(request_info->auth.mac.key_id, response, response_info,
|
||||
remote_addr, local_addr))
|
||||
return 0;
|
||||
/* Don't send the original packet */
|
||||
return 0;
|
||||
case NTP_AUTH_NTS:
|
||||
if (!NNS_GenerateResponseAuth(request, request_info, response, response_info, kod))
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG("Could not authenticate response auth_mode=%d", (int)request_info->auth.mode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
response_info->auth.mode = request_info->auth.mode;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NAU_CheckResponseAuth(NAU_Instance instance, NTP_Packet *response, NTP_PacketInfo *info)
|
||||
{
|
||||
/* The authentication must match the expected mode */
|
||||
if (info->auth.mode != instance->mode)
|
||||
return 0;
|
||||
|
||||
switch (info->auth.mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
break;
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
/* Check if it is authenticated with the specified key */
|
||||
if (info->auth.mac.key_id != instance->key_id)
|
||||
return 0;
|
||||
/* and that the MAC is valid */
|
||||
if (!check_symmetric_auth(response, info))
|
||||
return 0;
|
||||
break;
|
||||
case NTP_AUTH_NTS:
|
||||
if (!NNC_CheckResponseAuth(instance->nts, response, info))
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NAU_ChangeAddress(NAU_Instance instance, IPAddr *address)
|
||||
{
|
||||
switch (instance->mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
break;
|
||||
case NTP_AUTH_NTS:
|
||||
NNC_ChangeAddress(instance->nts, address);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NAU_DumpData(NAU_Instance instance)
|
||||
{
|
||||
switch (instance->mode) {
|
||||
case NTP_AUTH_NTS:
|
||||
NNC_DumpData(instance->nts);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NAU_GetReport(NAU_Instance instance, RPT_AuthReport *report)
|
||||
{
|
||||
memset(report, 0, sizeof (*report));
|
||||
|
||||
report->mode = instance->mode;
|
||||
report->last_ke_ago = -1;
|
||||
|
||||
switch (instance->mode) {
|
||||
case NTP_AUTH_NONE:
|
||||
break;
|
||||
case NTP_AUTH_SYMMETRIC:
|
||||
report->key_id = instance->key_id;
|
||||
KEY_GetKeyInfo(instance->key_id, &report->key_type, &report->key_length);
|
||||
break;
|
||||
case NTP_AUTH_NTS:
|
||||
NNC_GetReport(instance->nts, report);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
87
ntp_auth.h
Normal file
87
ntp_auth.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019
|
||||
*
|
||||
* 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 NTP authentication
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTP_AUTH_H
|
||||
#define GOT_NTP_AUTH_H
|
||||
|
||||
#include "addressing.h"
|
||||
#include "ntp.h"
|
||||
#include "reports.h"
|
||||
|
||||
typedef struct NAU_Instance_Record *NAU_Instance;
|
||||
|
||||
/* Create an authenticator instance in a specific mode */
|
||||
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,
|
||||
uint32_t cert_set, uint16_t ntp_port);
|
||||
|
||||
/* Destroy an instance */
|
||||
extern void NAU_DestroyInstance(NAU_Instance instance);
|
||||
|
||||
/* Check if an instance is not in the None mode */
|
||||
extern int NAU_IsAuthEnabled(NAU_Instance instance);
|
||||
|
||||
/* Get NTP version recommended for better compatibility */
|
||||
extern int NAU_GetSuggestedNtpVersion(NAU_Instance instance);
|
||||
|
||||
/* Perform operations necessary for NAU_GenerateRequestAuth() */
|
||||
extern int NAU_PrepareRequestAuth(NAU_Instance instance);
|
||||
|
||||
/* Extend a request with data required by the authentication mode */
|
||||
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);
|
||||
|
||||
/* Extend a response with data required by the authentication mode. This
|
||||
function can be called only if the previous call of NAU_CheckRequestAuth()
|
||||
was on the same request. */
|
||||
extern int NAU_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *request_info,
|
||||
NTP_Packet *response, NTP_PacketInfo *response_info,
|
||||
NTP_Remote_Address *remote_addr,
|
||||
NTP_Local_Address *local_addr,
|
||||
uint32_t kod);
|
||||
|
||||
/* Verify that a response is authentic */
|
||||
extern int NAU_CheckResponseAuth(NAU_Instance instance, NTP_Packet *response,
|
||||
NTP_PacketInfo *info);
|
||||
|
||||
/* Change an authentication-specific address (e.g. after replacing a source) */
|
||||
extern void NAU_ChangeAddress(NAU_Instance instance, IPAddr *address);
|
||||
|
||||
/* Save authentication-specific data to speed up the next start */
|
||||
extern void NAU_DumpData(NAU_Instance instance);
|
||||
|
||||
/* Provide a report about the current authentication state */
|
||||
extern void NAU_GetReport(NAU_Instance instance, RPT_AuthReport *report);
|
||||
|
||||
#endif
|
||||
1033
ntp_core.c
1033
ntp_core.c
File diff suppressed because it is too large
Load Diff
20
ntp_core.h
20
ntp_core.h
@@ -59,7 +59,8 @@ extern void NCR_Initialise(void);
|
||||
extern void NCR_Finalise(void);
|
||||
|
||||
/* Get a new instance for a server or peer */
|
||||
extern NCR_Instance NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParameters *params);
|
||||
extern NCR_Instance NCR_CreateInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
|
||||
SourceParameters *params, const char *name);
|
||||
|
||||
/* Destroy an instance */
|
||||
extern void NCR_DestroyInstance(NCR_Instance instance);
|
||||
@@ -74,7 +75,8 @@ extern void NCR_ResetInstance(NCR_Instance inst);
|
||||
extern void NCR_ResetPoll(NCR_Instance instance);
|
||||
|
||||
/* Change the remote address of an instance */
|
||||
extern void NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr);
|
||||
extern void NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr,
|
||||
int ntp_only);
|
||||
|
||||
/* This routine is called when a new packet arrives off the network,
|
||||
and it relates to a source we have an ongoing protocol exchange with */
|
||||
@@ -99,12 +101,9 @@ extern void NCR_ProcessTxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Addr
|
||||
/* Slew receive and transmit times in instance records */
|
||||
extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset);
|
||||
|
||||
/* Take a particular source online (i.e. start sampling it) */
|
||||
extern void NCR_TakeSourceOnline(NCR_Instance inst);
|
||||
|
||||
/* Take a particular source offline (i.e. stop sampling it, without
|
||||
marking it unreachable in the source selection stuff) */
|
||||
extern void NCR_TakeSourceOffline(NCR_Instance inst);
|
||||
/* Take a particular source online (i.e. start sampling it) or offline
|
||||
(i.e. stop sampling it) */
|
||||
extern void NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity);
|
||||
|
||||
extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll);
|
||||
|
||||
@@ -123,6 +122,7 @@ extern void NCR_ModifyPolltarget(NCR_Instance inst, int new_poll_target);
|
||||
extern void NCR_InitiateSampleBurst(NCR_Instance inst, int n_good_samples, int n_total_samples);
|
||||
|
||||
extern void NCR_ReportSource(NCR_Instance inst, RPT_SourceReport *report, struct timespec *now);
|
||||
extern void NCR_GetAuthReport(NCR_Instance inst, RPT_AuthReport *report);
|
||||
extern void NCR_GetNTPReport(NCR_Instance inst, RPT_NTPReport *report);
|
||||
|
||||
extern int NCR_AddAccessRestriction(IPAddr *ip_addr, int subnet_bits, int allow, int all);
|
||||
@@ -137,6 +137,8 @@ extern uint32_t NCR_GetLocalRefid(NCR_Instance inst);
|
||||
|
||||
extern int NCR_IsSyncPeer(NCR_Instance instance);
|
||||
|
||||
extern void NCR_AddBroadcastDestination(IPAddr *addr, unsigned short port, int interval);
|
||||
extern void NCR_DumpAuthData(NCR_Instance inst);
|
||||
|
||||
extern void NCR_AddBroadcastDestination(NTP_Remote_Address *addr, int interval);
|
||||
|
||||
#endif /* GOT_NTP_CORE_H */
|
||||
|
||||
192
ntp_ext.c
Normal file
192
ntp_ext.c
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019-2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Functions for adding and parsing NTPv4 extension fields
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "ntp_ext.h"
|
||||
|
||||
struct ExtFieldHeader {
|
||||
uint16_t type;
|
||||
uint16_t length;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
format_field(unsigned char *buffer, int buffer_length, int start,
|
||||
int type, int body_length, int *length, void **body)
|
||||
{
|
||||
struct ExtFieldHeader *header;
|
||||
|
||||
if (buffer_length < 0 || start < 0 || buffer_length <= start ||
|
||||
buffer_length - start < sizeof (*header) || start % 4 != 0)
|
||||
return 0;
|
||||
|
||||
header = (struct ExtFieldHeader *)(buffer + start);
|
||||
|
||||
if (body_length < 0 || sizeof (*header) + body_length > 0xffff ||
|
||||
start + sizeof (*header) + body_length > buffer_length || body_length % 4 != 0)
|
||||
return 0;
|
||||
|
||||
header->type = htons(type);
|
||||
header->length = htons(sizeof (*header) + body_length);
|
||||
*length = sizeof (*header) + body_length;
|
||||
*body = header + 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NEF_SetField(unsigned char *buffer, int buffer_length, int start,
|
||||
int type, void *body, int body_length, int *length)
|
||||
{
|
||||
void *ef_body;
|
||||
|
||||
if (!format_field(buffer, buffer_length, start, type, body_length, length, &ef_body))
|
||||
return 0;
|
||||
|
||||
memcpy(ef_body, body, body_length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NEF_AddBlankField(NTP_Packet *packet, NTP_PacketInfo *info, int type, int body_length, void **body)
|
||||
{
|
||||
int ef_length, length = info->length;
|
||||
|
||||
if (length < NTP_HEADER_LENGTH || length >= sizeof (*packet) || length % 4 != 0)
|
||||
return 0;
|
||||
|
||||
/* Only NTPv4 packets can have extension fields */
|
||||
if (info->version != 4)
|
||||
return 0;
|
||||
|
||||
if (!format_field((unsigned char *)packet, sizeof (*packet), length,
|
||||
type, body_length, &ef_length, body))
|
||||
return 0;
|
||||
|
||||
if (ef_length < NTP_MIN_EF_LENGTH)
|
||||
return 0;
|
||||
|
||||
info->length += ef_length;
|
||||
info->ext_fields++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NEF_AddField(NTP_Packet *packet, NTP_PacketInfo *info,
|
||||
int type, void *body, int body_length)
|
||||
{
|
||||
void *ef_body;
|
||||
|
||||
if (!NEF_AddBlankField(packet, info, type, body_length, &ef_body))
|
||||
return 0;
|
||||
|
||||
memcpy(ef_body, body, body_length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NEF_ParseSingleField(unsigned char *buffer, int buffer_length, int start,
|
||||
int *length, int *type, void **body, int *body_length)
|
||||
{
|
||||
struct ExtFieldHeader *header;
|
||||
int ef_length;
|
||||
|
||||
if (buffer_length < 0 || start < 0 || buffer_length <= start ||
|
||||
buffer_length - start < sizeof (*header))
|
||||
return 0;
|
||||
|
||||
header = (struct ExtFieldHeader *)(buffer + start);
|
||||
|
||||
assert(sizeof (*header) == 4);
|
||||
|
||||
ef_length = ntohs(header->length);
|
||||
|
||||
if (ef_length < (int)(sizeof (*header)) || start + ef_length > buffer_length ||
|
||||
ef_length % 4 != 0)
|
||||
return 0;
|
||||
|
||||
if (length)
|
||||
*length = ef_length;
|
||||
if (type)
|
||||
*type = ntohs(header->type);
|
||||
if (body)
|
||||
*body = header + 1;
|
||||
if (body_length)
|
||||
*body_length = ef_length - sizeof (*header);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NEF_ParseField(NTP_Packet *packet, int packet_length, int start,
|
||||
int *length, int *type, void **body, int *body_length)
|
||||
{
|
||||
int ef_length;
|
||||
|
||||
if (packet_length <= NTP_HEADER_LENGTH || packet_length > sizeof (*packet) ||
|
||||
packet_length <= start || packet_length % 4 != 0 ||
|
||||
start < NTP_HEADER_LENGTH || start % 4 != 0)
|
||||
return 0;
|
||||
|
||||
/* Only NTPv4 packets have extension fields */
|
||||
if (NTP_LVM_TO_VERSION(packet->lvm) != 4)
|
||||
return 0;
|
||||
|
||||
/* Check if the remaining data is a MAC. RFC 7822 specifies the maximum
|
||||
length of a MAC in NTPv4 packets in order to enable deterministic
|
||||
parsing. */
|
||||
if (packet_length - start <= NTP_MAX_V4_MAC_LENGTH)
|
||||
return 0;
|
||||
|
||||
if (!NEF_ParseSingleField((unsigned char *)packet, packet_length, start,
|
||||
&ef_length, type, body, body_length))
|
||||
return 0;
|
||||
|
||||
if (ef_length < NTP_MIN_EF_LENGTH)
|
||||
return 0;
|
||||
|
||||
if (length)
|
||||
*length = ef_length;
|
||||
|
||||
return 1;
|
||||
}
|
||||
43
ntp_ext.h
Normal file
43
ntp_ext.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019
|
||||
*
|
||||
* 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 NTP extension fields
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTP_EXT_H
|
||||
#define GOT_NTP_EXT_H
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
extern int NEF_SetField(unsigned char *buffer, int buffer_length, int start,
|
||||
int type, void *body, int body_length, int *length);
|
||||
extern int NEF_AddBlankField(NTP_Packet *packet, NTP_PacketInfo *info, int type,
|
||||
int body_length, void **body);
|
||||
extern int NEF_AddField(NTP_Packet *packet, NTP_PacketInfo *info,
|
||||
int type, void *body, int body_length);
|
||||
extern int NEF_ParseSingleField(unsigned char *buffer, int buffer_length, int start,
|
||||
int *length, int *type, void **body, int *body_length);
|
||||
extern int NEF_ParseField(NTP_Packet *packet, int packet_length, int start,
|
||||
int *length, int *type, void **body, int *body_length);
|
||||
|
||||
#endif
|
||||
642
ntp_io.c
642
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
|
||||
* Copyright (C) Miroslav Lichvar 2009, 2013-2016, 2018-2020
|
||||
*
|
||||
* 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,11 +30,11 @@
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "array.h"
|
||||
#include "ntp_io.h"
|
||||
#include "ntp_core.h"
|
||||
#include "ntp_sources.h"
|
||||
#include "sched.h"
|
||||
#include "socket.h"
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "conf.h"
|
||||
@@ -46,54 +46,16 @@
|
||||
#endif
|
||||
|
||||
#define INVALID_SOCK_FD -1
|
||||
#define CMSGBUF_SIZE 256
|
||||
|
||||
union sockaddr_in46 {
|
||||
struct sockaddr_in in4;
|
||||
#ifdef FEAT_IPV6
|
||||
struct sockaddr_in6 in6;
|
||||
#endif
|
||||
struct sockaddr u;
|
||||
};
|
||||
|
||||
struct Message {
|
||||
union sockaddr_in46 name;
|
||||
struct iovec iov;
|
||||
NTP_Receive_Buffer buf;
|
||||
/* Aligned buffer for control messages */
|
||||
struct cmsghdr cmsgbuf[CMSGBUF_SIZE / sizeof (struct cmsghdr)];
|
||||
};
|
||||
|
||||
#ifdef HAVE_RECVMMSG
|
||||
#define MAX_RECV_MESSAGES 4
|
||||
#define MessageHeader mmsghdr
|
||||
#else
|
||||
/* Compatible with mmsghdr */
|
||||
struct MessageHeader {
|
||||
struct msghdr msg_hdr;
|
||||
unsigned int msg_len;
|
||||
};
|
||||
|
||||
#define MAX_RECV_MESSAGES 1
|
||||
#endif
|
||||
|
||||
/* Arrays of Message and MessageHeader */
|
||||
static ARR_Instance recv_messages;
|
||||
static ARR_Instance recv_headers;
|
||||
|
||||
/* The server/peer and client sockets for IPv4 and IPv6 */
|
||||
static int server_sock_fd4;
|
||||
static int client_sock_fd4;
|
||||
#ifdef FEAT_IPV6
|
||||
static int server_sock_fd6;
|
||||
static int client_sock_fd4;
|
||||
static int client_sock_fd6;
|
||||
#endif
|
||||
|
||||
/* Reference counters for server sockets to keep them open only when needed */
|
||||
static int server_sock_ref4;
|
||||
#ifdef FEAT_IPV6
|
||||
static int server_sock_ref6;
|
||||
#endif
|
||||
|
||||
/* Flag indicating we create a new connected client socket for each
|
||||
server instead of sharing client_sock_fd4 and client_sock_fd6 */
|
||||
@@ -105,6 +67,9 @@ static int separate_client_sockets;
|
||||
disabled */
|
||||
static int permanent_server_sockets;
|
||||
|
||||
/* Flag indicating the server IPv4 socket is bound to an address */
|
||||
static int bound_server_sock_fd4;
|
||||
|
||||
/* Flag indicating that we have been initialised */
|
||||
static int initialised=0;
|
||||
|
||||
@@ -116,155 +81,54 @@ static void read_from_socket(int sock_fd, int event, void *anything);
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
prepare_socket(int family, int port_number, int client_only)
|
||||
open_socket(int family, int local_port, int client_only, IPSockAddr *remote_addr)
|
||||
{
|
||||
union sockaddr_in46 my_addr;
|
||||
socklen_t my_addr_len;
|
||||
int sock_fd;
|
||||
IPAddr bind_address;
|
||||
int events = SCH_FILE_INPUT, on_off = 1;
|
||||
int sock_fd, sock_flags, dscp, events = SCH_FILE_INPUT;
|
||||
IPSockAddr local_addr;
|
||||
char *iface;
|
||||
|
||||
/* Open Internet domain UDP socket for NTP message transmissions */
|
||||
if (!SCK_IsIpFamilyEnabled(family))
|
||||
return INVALID_SOCK_FD;
|
||||
|
||||
sock_fd = socket(family, SOCK_DGRAM, 0);
|
||||
if (!client_only) {
|
||||
CNF_GetBindAddress(family, &local_addr.ip_addr);
|
||||
iface = CNF_GetBindNtpInterface();
|
||||
} else {
|
||||
CNF_GetBindAcquisitionAddress(family, &local_addr.ip_addr);
|
||||
iface = CNF_GetBindAcquisitionInterface();
|
||||
}
|
||||
|
||||
local_addr.port = local_port;
|
||||
|
||||
sock_flags = SCK_FLAG_RX_DEST_ADDR | SCK_FLAG_PRIV_BIND;
|
||||
if (!client_only)
|
||||
sock_flags |= SCK_FLAG_BROADCAST;
|
||||
|
||||
sock_fd = SCK_OpenUdpSocket(remote_addr, &local_addr, iface, sock_flags);
|
||||
if (sock_fd < 0) {
|
||||
if (!client_only) {
|
||||
LOG(LOGS_ERR, "Could not open %s NTP socket : %s",
|
||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
||||
} else {
|
||||
DEBUG_LOG("Could not open %s NTP socket : %s",
|
||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
||||
}
|
||||
if (!client_only)
|
||||
LOG(LOGS_ERR, "Could not open NTP socket on %s", UTI_IPSockAddrToString(&local_addr));
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
/* Close on exec */
|
||||
UTI_FdSetCloexec(sock_fd);
|
||||
|
||||
/* Prepare local address */
|
||||
memset(&my_addr, 0, sizeof (my_addr));
|
||||
my_addr_len = 0;
|
||||
|
||||
switch (family) {
|
||||
case AF_INET:
|
||||
if (!client_only)
|
||||
CNF_GetBindAddress(IPADDR_INET4, &bind_address);
|
||||
else
|
||||
CNF_GetBindAcquisitionAddress(IPADDR_INET4, &bind_address);
|
||||
|
||||
if (bind_address.family == IPADDR_INET4)
|
||||
my_addr.in4.sin_addr.s_addr = htonl(bind_address.addr.in4);
|
||||
else if (port_number)
|
||||
my_addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
else
|
||||
break;
|
||||
|
||||
my_addr.in4.sin_family = family;
|
||||
my_addr.in4.sin_port = htons(port_number);
|
||||
my_addr_len = sizeof (my_addr.in4);
|
||||
|
||||
break;
|
||||
#ifdef FEAT_IPV6
|
||||
case AF_INET6:
|
||||
if (!client_only)
|
||||
CNF_GetBindAddress(IPADDR_INET6, &bind_address);
|
||||
else
|
||||
CNF_GetBindAcquisitionAddress(IPADDR_INET6, &bind_address);
|
||||
|
||||
if (bind_address.family == IPADDR_INET6)
|
||||
memcpy(my_addr.in6.sin6_addr.s6_addr, bind_address.addr.in6,
|
||||
sizeof (my_addr.in6.sin6_addr.s6_addr));
|
||||
else if (port_number)
|
||||
my_addr.in6.sin6_addr = in6addr_any;
|
||||
else
|
||||
break;
|
||||
|
||||
my_addr.in6.sin6_family = family;
|
||||
my_addr.in6.sin6_port = htons(port_number);
|
||||
my_addr_len = sizeof (my_addr.in6);
|
||||
|
||||
break;
|
||||
dscp = CNF_GetNtpDscp();
|
||||
if (dscp > 0 && dscp < 64) {
|
||||
#ifdef IP_TOS
|
||||
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_TOS, dscp << 2))
|
||||
;
|
||||
#endif
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* Make the socket capable of re-using an old address if binding to a specific port */
|
||||
if (port_number &&
|
||||
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "SO_REUSEADDR");
|
||||
/* Don't quit - we might survive anyway */
|
||||
}
|
||||
|
||||
/* Make the socket capable of sending broadcast pkts - needed for NTP broadcast mode */
|
||||
if (!client_only &&
|
||||
setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "SO_BROADCAST");
|
||||
/* Don't quit - we might survive anyway */
|
||||
}
|
||||
if (!client_only && family == IPADDR_INET4 && local_addr.port > 0)
|
||||
bound_server_sock_fd4 = local_addr.ip_addr.addr.in4 != INADDR_ANY;
|
||||
|
||||
/* Enable kernel/HW timestamping of packets */
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
if (!NIO_Linux_SetTimestampSocketOptions(sock_fd, client_only, &events))
|
||||
#endif
|
||||
#ifdef SO_TIMESTAMPNS
|
||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPNS, (char *)&on_off, sizeof(on_off)) < 0)
|
||||
#endif
|
||||
#ifdef SO_TIMESTAMP
|
||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMP, (char *)&on_off, sizeof(on_off)) < 0)
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "SO_TIMESTAMP");
|
||||
#endif
|
||||
if (!SCK_EnableKernelRxTimestamping(sock_fd))
|
||||
;
|
||||
|
||||
#ifdef IP_FREEBIND
|
||||
/* Allow binding to address that doesn't exist yet */
|
||||
if (my_addr_len > 0 &&
|
||||
setsockopt(sock_fd, IPPROTO_IP, IP_FREEBIND, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "IP_FREEBIND");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (family == AF_INET) {
|
||||
#ifdef HAVE_IN_PKTINFO
|
||||
/* We want the local IP info on server sockets */
|
||||
if (setsockopt(sock_fd, IPPROTO_IP, IP_PKTINFO, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "IP_PKTINFO");
|
||||
/* Don't quit - we might survive anyway */
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#ifdef FEAT_IPV6
|
||||
else if (family == AF_INET6) {
|
||||
#ifdef IPV6_V6ONLY
|
||||
/* Receive IPv6 packets only */
|
||||
if (setsockopt(sock_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "IPV6_V6ONLY");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IN6_PKTINFO
|
||||
#ifdef IPV6_RECVPKTINFO
|
||||
if (setsockopt(sock_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "IPV6_RECVPKTINFO");
|
||||
}
|
||||
#else
|
||||
if (setsockopt(sock_fd, IPPROTO_IPV6, IPV6_PKTINFO, (char *)&on_off, sizeof(on_off)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "IPV6_PKTINFO");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Bind the socket if a port or address was specified */
|
||||
if (my_addr_len > 0 && PRV_BindSocket(sock_fd, &my_addr.u, my_addr_len) < 0) {
|
||||
LOG(LOGS_ERR, "Could not bind %s NTP socket : %s",
|
||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
||||
close(sock_fd);
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
/* Register handler for read and possibly exception events on the socket */
|
||||
SCH_AddFileHandler(sock_fd, events, read_from_socket, NULL);
|
||||
|
||||
@@ -274,40 +138,9 @@ prepare_socket(int family, int port_number, int client_only)
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
prepare_separate_client_socket(int family)
|
||||
open_separate_client_socket(IPSockAddr *remote_addr)
|
||||
{
|
||||
switch (family) {
|
||||
case IPADDR_INET4:
|
||||
return prepare_socket(AF_INET, 0, 1);
|
||||
#ifdef FEAT_IPV6
|
||||
case IPADDR_INET6:
|
||||
return prepare_socket(AF_INET6, 0, 1);
|
||||
#endif
|
||||
default:
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
connect_socket(int sock_fd, NTP_Remote_Address *remote_addr)
|
||||
{
|
||||
union sockaddr_in46 addr;
|
||||
socklen_t addr_len;
|
||||
|
||||
addr_len = UTI_IPAndPortToSockaddr(&remote_addr->ip_addr, remote_addr->port, &addr.u);
|
||||
|
||||
assert(addr_len);
|
||||
|
||||
if (connect(sock_fd, &addr.u, addr_len) < 0) {
|
||||
DEBUG_LOG("Could not connect NTP socket to %s:%d : %s",
|
||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
||||
strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return open_socket(remote_addr->ip_addr.family, 0, 1, remote_addr);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -318,46 +151,27 @@ 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);
|
||||
close(sock_fd);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
prepare_buffers(unsigned int n)
|
||||
{
|
||||
struct MessageHeader *hdr;
|
||||
struct Message *msg;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
msg = ARR_GetElement(recv_messages, i);
|
||||
hdr = ARR_GetElement(recv_headers, i);
|
||||
|
||||
msg->iov.iov_base = &msg->buf;
|
||||
msg->iov.iov_len = sizeof (msg->buf);
|
||||
hdr->msg_hdr.msg_name = &msg->name;
|
||||
hdr->msg_hdr.msg_namelen = sizeof (msg->name);
|
||||
hdr->msg_hdr.msg_iov = &msg->iov;
|
||||
hdr->msg_hdr.msg_iovlen = 1;
|
||||
hdr->msg_hdr.msg_control = &msg->cmsgbuf;
|
||||
hdr->msg_hdr.msg_controllen = sizeof (msg->cmsgbuf);
|
||||
hdr->msg_hdr.msg_flags = 0;
|
||||
hdr->msg_len = 0;
|
||||
}
|
||||
SCK_CloseSocket(sock_fd);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NIO_Initialise(int family)
|
||||
NIO_Initialise(void)
|
||||
{
|
||||
int server_port, client_port;
|
||||
|
||||
assert(!initialised);
|
||||
initialised = 1;
|
||||
|
||||
#ifdef PRIVOPS_BINDSOCKET
|
||||
SCK_SetPrivBind(PRV_BindSocket);
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
NIO_Linux_Initialise();
|
||||
#else
|
||||
@@ -368,12 +182,6 @@ NIO_Initialise(int family)
|
||||
}
|
||||
#endif
|
||||
|
||||
recv_messages = ARR_CreateInstance(sizeof (struct Message));
|
||||
ARR_SetSize(recv_messages, MAX_RECV_MESSAGES);
|
||||
recv_headers = ARR_CreateInstance(sizeof (struct MessageHeader));
|
||||
ARR_SetSize(recv_headers, MAX_RECV_MESSAGES);
|
||||
prepare_buffers(MAX_RECV_MESSAGES);
|
||||
|
||||
server_port = CNF_GetNTPPort();
|
||||
client_port = CNF_GetAcquisitionPort();
|
||||
|
||||
@@ -386,47 +194,31 @@ NIO_Initialise(int family)
|
||||
client_port == server_port);
|
||||
|
||||
server_sock_fd4 = INVALID_SOCK_FD;
|
||||
client_sock_fd4 = INVALID_SOCK_FD;
|
||||
server_sock_ref4 = 0;
|
||||
#ifdef FEAT_IPV6
|
||||
server_sock_fd6 = INVALID_SOCK_FD;
|
||||
client_sock_fd4 = INVALID_SOCK_FD;
|
||||
client_sock_fd6 = INVALID_SOCK_FD;
|
||||
server_sock_ref4 = 0;
|
||||
server_sock_ref6 = 0;
|
||||
#endif
|
||||
|
||||
if (family == IPADDR_UNSPEC || family == IPADDR_INET4) {
|
||||
if (permanent_server_sockets && server_port)
|
||||
server_sock_fd4 = prepare_socket(AF_INET, server_port, 0);
|
||||
if (!separate_client_sockets) {
|
||||
if (client_port != server_port || !server_port)
|
||||
client_sock_fd4 = prepare_socket(AF_INET, client_port, 1);
|
||||
else
|
||||
client_sock_fd4 = server_sock_fd4;
|
||||
if (permanent_server_sockets && server_port) {
|
||||
server_sock_fd4 = open_socket(IPADDR_INET4, server_port, 0, NULL);
|
||||
server_sock_fd6 = open_socket(IPADDR_INET6, server_port, 0, NULL);
|
||||
}
|
||||
|
||||
if (!separate_client_sockets) {
|
||||
if (client_port != server_port || !server_port) {
|
||||
client_sock_fd4 = open_socket(IPADDR_INET4, client_port, 1, NULL);
|
||||
client_sock_fd6 = open_socket(IPADDR_INET6, client_port, 1, NULL);
|
||||
} else {
|
||||
client_sock_fd4 = server_sock_fd4;
|
||||
client_sock_fd6 = server_sock_fd6;
|
||||
}
|
||||
}
|
||||
#ifdef FEAT_IPV6
|
||||
if (family == IPADDR_UNSPEC || family == IPADDR_INET6) {
|
||||
if (permanent_server_sockets && server_port)
|
||||
server_sock_fd6 = prepare_socket(AF_INET6, server_port, 0);
|
||||
if (!separate_client_sockets) {
|
||||
if (client_port != server_port || !server_port)
|
||||
client_sock_fd6 = prepare_socket(AF_INET6, client_port, 1);
|
||||
else
|
||||
client_sock_fd6 = server_sock_fd6;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((server_port && server_sock_fd4 == INVALID_SOCK_FD &&
|
||||
permanent_server_sockets
|
||||
#ifdef FEAT_IPV6
|
||||
&& server_sock_fd6 == INVALID_SOCK_FD
|
||||
#endif
|
||||
) || (!separate_client_sockets && client_sock_fd4 == INVALID_SOCK_FD
|
||||
#ifdef FEAT_IPV6
|
||||
&& client_sock_fd6 == INVALID_SOCK_FD
|
||||
#endif
|
||||
)) {
|
||||
if ((server_port && permanent_server_sockets &&
|
||||
server_sock_fd4 == INVALID_SOCK_FD && server_sock_fd6 == INVALID_SOCK_FD) ||
|
||||
(!separate_client_sockets &&
|
||||
client_sock_fd4 == INVALID_SOCK_FD && client_sock_fd6 == INVALID_SOCK_FD)) {
|
||||
LOG_FATAL("Could not open NTP sockets");
|
||||
}
|
||||
}
|
||||
@@ -440,14 +232,11 @@ NIO_Finalise(void)
|
||||
close_socket(client_sock_fd4);
|
||||
close_socket(server_sock_fd4);
|
||||
server_sock_fd4 = client_sock_fd4 = INVALID_SOCK_FD;
|
||||
#ifdef FEAT_IPV6
|
||||
|
||||
if (server_sock_fd6 != client_sock_fd6)
|
||||
close_socket(client_sock_fd6);
|
||||
close_socket(server_sock_fd6);
|
||||
server_sock_fd6 = client_sock_fd6 = INVALID_SOCK_FD;
|
||||
#endif
|
||||
ARR_DestroyInstance(recv_headers);
|
||||
ARR_DestroyInstance(recv_messages);
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
NIO_Linux_Finalise();
|
||||
@@ -462,25 +251,13 @@ int
|
||||
NIO_OpenClientSocket(NTP_Remote_Address *remote_addr)
|
||||
{
|
||||
if (separate_client_sockets) {
|
||||
int sock_fd = prepare_separate_client_socket(remote_addr->ip_addr.family);
|
||||
|
||||
if (sock_fd == INVALID_SOCK_FD)
|
||||
return INVALID_SOCK_FD;
|
||||
|
||||
if (!connect_socket(sock_fd, remote_addr)) {
|
||||
close_socket(sock_fd);
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
return sock_fd;
|
||||
return open_separate_client_socket(remote_addr);
|
||||
} else {
|
||||
switch (remote_addr->ip_addr.family) {
|
||||
case IPADDR_INET4:
|
||||
return client_sock_fd4;
|
||||
#ifdef FEAT_IPV6
|
||||
case IPADDR_INET6:
|
||||
return client_sock_fd6;
|
||||
#endif
|
||||
default:
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
@@ -497,20 +274,18 @@ NIO_OpenServerSocket(NTP_Remote_Address *remote_addr)
|
||||
if (permanent_server_sockets)
|
||||
return server_sock_fd4;
|
||||
if (server_sock_fd4 == INVALID_SOCK_FD)
|
||||
server_sock_fd4 = prepare_socket(AF_INET, CNF_GetNTPPort(), 0);
|
||||
server_sock_fd4 = open_socket(IPADDR_INET4, CNF_GetNTPPort(), 0, NULL);
|
||||
if (server_sock_fd4 != INVALID_SOCK_FD)
|
||||
server_sock_ref4++;
|
||||
return server_sock_fd4;
|
||||
#ifdef FEAT_IPV6
|
||||
case IPADDR_INET6:
|
||||
if (permanent_server_sockets)
|
||||
return server_sock_fd6;
|
||||
if (server_sock_fd6 == INVALID_SOCK_FD)
|
||||
server_sock_fd6 = prepare_socket(AF_INET6, CNF_GetNTPPort(), 0);
|
||||
server_sock_fd6 = open_socket(IPADDR_INET6, CNF_GetNTPPort(), 0, NULL);
|
||||
if (server_sock_fd6 != INVALID_SOCK_FD)
|
||||
server_sock_ref6++;
|
||||
return server_sock_fd6;
|
||||
#endif
|
||||
default:
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
@@ -538,16 +313,12 @@ NIO_CloseServerSocket(int sock_fd)
|
||||
close_socket(server_sock_fd4);
|
||||
server_sock_fd4 = INVALID_SOCK_FD;
|
||||
}
|
||||
}
|
||||
#ifdef FEAT_IPV6
|
||||
else if (sock_fd == server_sock_fd6) {
|
||||
} else if (sock_fd == server_sock_fd6) {
|
||||
if (--server_sock_ref6 <= 0) {
|
||||
close_socket(server_sock_fd6);
|
||||
server_sock_fd6 = INVALID_SOCK_FD;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
@@ -558,119 +329,76 @@ int
|
||||
NIO_IsServerSocket(int sock_fd)
|
||||
{
|
||||
return sock_fd != INVALID_SOCK_FD &&
|
||||
(sock_fd == server_sock_fd4
|
||||
#ifdef FEAT_IPV6
|
||||
|| sock_fd == server_sock_fd6
|
||||
#endif
|
||||
);
|
||||
(sock_fd == server_sock_fd4 || sock_fd == server_sock_fd6);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_IsServerSocketOpen(void)
|
||||
{
|
||||
return server_sock_fd4 != INVALID_SOCK_FD || server_sock_fd6 != INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_IsServerConnectable(NTP_Remote_Address *remote_addr)
|
||||
{
|
||||
int sock_fd;
|
||||
|
||||
sock_fd = open_separate_client_socket(remote_addr);
|
||||
if (sock_fd == INVALID_SOCK_FD)
|
||||
return 0;
|
||||
|
||||
close_socket(sock_fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
process_message(struct msghdr *hdr, int length, int sock_fd)
|
||||
process_message(SCK_Message *message, int sock_fd, int event)
|
||||
{
|
||||
NTP_Remote_Address remote_addr;
|
||||
NTP_Local_Address local_addr;
|
||||
NTP_Local_Timestamp local_ts;
|
||||
struct timespec sched_ts;
|
||||
struct cmsghdr *cmsg;
|
||||
|
||||
SCH_GetLastEventTime(&local_ts.ts, &local_ts.err, NULL);
|
||||
local_ts.source = NTP_TS_DAEMON;
|
||||
sched_ts = local_ts.ts;
|
||||
|
||||
if (hdr->msg_namelen > sizeof (union sockaddr_in46)) {
|
||||
DEBUG_LOG("Truncated source address");
|
||||
if (message->addr_type != SCK_ADDR_IP) {
|
||||
DEBUG_LOG("Unexpected address type");
|
||||
return;
|
||||
}
|
||||
|
||||
if (hdr->msg_namelen >= sizeof (((struct sockaddr *)hdr->msg_name)->sa_family)) {
|
||||
UTI_SockaddrToIPAndPort((struct sockaddr *)hdr->msg_name,
|
||||
&remote_addr.ip_addr, &remote_addr.port);
|
||||
} else {
|
||||
remote_addr.ip_addr.family = IPADDR_UNSPEC;
|
||||
remote_addr.port = 0;
|
||||
}
|
||||
|
||||
local_addr.ip_addr.family = IPADDR_UNSPEC;
|
||||
local_addr.if_index = INVALID_IF_INDEX;
|
||||
local_addr.ip_addr = message->local_addr.ip;
|
||||
local_addr.if_index = message->if_index;;
|
||||
local_addr.sock_fd = sock_fd;
|
||||
|
||||
if (hdr->msg_flags & MSG_TRUNC) {
|
||||
DEBUG_LOG("Received truncated message from %s:%d",
|
||||
UTI_IPToString(&remote_addr.ip_addr), remote_addr.port);
|
||||
return;
|
||||
}
|
||||
|
||||
if (hdr->msg_flags & MSG_CTRUNC) {
|
||||
DEBUG_LOG("Truncated control message");
|
||||
/* Continue */
|
||||
}
|
||||
|
||||
for (cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) {
|
||||
#ifdef HAVE_IN_PKTINFO
|
||||
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
|
||||
struct in_pktinfo ipi;
|
||||
|
||||
memcpy(&ipi, CMSG_DATA(cmsg), sizeof(ipi));
|
||||
local_addr.ip_addr.addr.in4 = ntohl(ipi.ipi_addr.s_addr);
|
||||
local_addr.ip_addr.family = IPADDR_INET4;
|
||||
local_addr.if_index = ipi.ipi_ifindex;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IN6_PKTINFO
|
||||
if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
|
||||
struct in6_pktinfo ipi;
|
||||
|
||||
memcpy(&ipi, CMSG_DATA(cmsg), sizeof(ipi));
|
||||
memcpy(&local_addr.ip_addr.addr.in6, &ipi.ipi6_addr.s6_addr,
|
||||
sizeof (local_addr.ip_addr.addr.in6));
|
||||
local_addr.ip_addr.family = IPADDR_INET6;
|
||||
local_addr.if_index = ipi.ipi6_ifindex;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCM_TIMESTAMP
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMP) {
|
||||
struct timeval tv;
|
||||
struct timespec ts;
|
||||
|
||||
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
|
||||
UTI_TimevalToTimespec(&tv, &ts);
|
||||
LCL_CookTime(&ts, &local_ts.ts, &local_ts.err);
|
||||
local_ts.source = NTP_TS_KERNEL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCM_TIMESTAMPNS
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPNS) {
|
||||
struct timespec ts;
|
||||
|
||||
memcpy(&ts, CMSG_DATA(cmsg), sizeof (ts));
|
||||
LCL_CookTime(&ts, &local_ts.ts, &local_ts.err);
|
||||
local_ts.source = NTP_TS_KERNEL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
if (NIO_Linux_ProcessMessage(&remote_addr, &local_addr, &local_ts, hdr, length))
|
||||
if (NIO_Linux_ProcessMessage(message, &local_addr, &local_ts, event))
|
||||
return;
|
||||
#else
|
||||
if (!UTI_IsZeroTimespec(&message->timestamp.kernel)) {
|
||||
LCL_CookTime(&message->timestamp.kernel, &local_ts.ts, &local_ts.err);
|
||||
local_ts.source = NTP_TS_KERNEL;
|
||||
}
|
||||
#endif
|
||||
|
||||
DEBUG_LOG("Received %d bytes from %s:%d to %s fd=%d if=%d tss=%d delay=%.9f",
|
||||
length, UTI_IPToString(&remote_addr.ip_addr), remote_addr.port,
|
||||
UTI_IPToString(&local_addr.ip_addr), local_addr.sock_fd, local_addr.if_index,
|
||||
local_ts.source, UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts));
|
||||
if (local_ts.source != NTP_TS_DAEMON)
|
||||
DEBUG_LOG("Updated RX timestamp delay=%.9f tss=%u",
|
||||
UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts), local_ts.source);
|
||||
|
||||
/* Just ignore the packet if it's not of a recognized length */
|
||||
if (length < NTP_NORMAL_PACKET_LENGTH || length > sizeof (NTP_Receive_Buffer))
|
||||
if (message->length < NTP_HEADER_LENGTH || message->length > sizeof (NTP_Packet)) {
|
||||
DEBUG_LOG("Unexpected length");
|
||||
return;
|
||||
}
|
||||
|
||||
NSR_ProcessRx(&remote_addr, &local_addr, &local_ts,
|
||||
(NTP_Packet *)hdr->msg_iov[0].iov_base, length);
|
||||
NSR_ProcessRx(&message->remote_addr.ip, &local_addr, &local_ts, message->data, message->length);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -678,49 +406,28 @@ process_message(struct msghdr *hdr, int length, int sock_fd)
|
||||
static void
|
||||
read_from_socket(int sock_fd, int event, void *anything)
|
||||
{
|
||||
/* This should only be called when there is something
|
||||
to read, otherwise it may block */
|
||||
SCK_Message *messages;
|
||||
int i, received, flags = 0;
|
||||
|
||||
struct MessageHeader *hdr;
|
||||
unsigned int i, n;
|
||||
int status, flags = 0;
|
||||
|
||||
hdr = ARR_GetElements(recv_headers);
|
||||
n = ARR_GetSize(recv_headers);
|
||||
assert(n >= 1);
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
if (NIO_Linux_ProcessEvent(sock_fd, event))
|
||||
return;
|
||||
#endif
|
||||
|
||||
if (event == SCH_FILE_EXCEPTION) {
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
flags |= MSG_ERRQUEUE;
|
||||
flags |= SCK_FLAG_MSG_ERRQUEUE;
|
||||
#else
|
||||
assert(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HAVE_RECVMMSG
|
||||
status = recvmmsg(sock_fd, hdr, n, flags | MSG_DONTWAIT, NULL);
|
||||
if (status >= 0)
|
||||
n = status;
|
||||
#else
|
||||
n = 1;
|
||||
status = recvmsg(sock_fd, &hdr[0].msg_hdr, flags);
|
||||
if (status >= 0)
|
||||
hdr[0].msg_len = status;
|
||||
#endif
|
||||
|
||||
if (status < 0) {
|
||||
DEBUG_LOG("Could not receive from fd %d : %s", sock_fd,
|
||||
strerror(errno));
|
||||
messages = SCK_ReceiveMessages(sock_fd, flags, &received);
|
||||
if (!messages)
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
hdr = ARR_GetElement(recv_headers, i);
|
||||
process_message(&hdr->msg_hdr, hdr->msg_len, sock_fd);
|
||||
}
|
||||
|
||||
/* Restore the buffers to their original state */
|
||||
prepare_buffers(n);
|
||||
for (i = 0; i < received; i++)
|
||||
process_message(&messages[i], sock_fd, event);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -730,102 +437,47 @@ int
|
||||
NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||
NTP_Local_Address *local_addr, int length, int process_tx)
|
||||
{
|
||||
union sockaddr_in46 remote;
|
||||
struct msghdr msg;
|
||||
struct iovec iov;
|
||||
struct cmsghdr *cmsg, cmsgbuf[CMSGBUF_SIZE / sizeof (struct cmsghdr)];
|
||||
int cmsglen;
|
||||
socklen_t addrlen = 0;
|
||||
SCK_Message message;
|
||||
|
||||
assert(initialised);
|
||||
|
||||
if (local_addr->sock_fd == INVALID_SOCK_FD) {
|
||||
DEBUG_LOG("No socket to send to %s:%d",
|
||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port);
|
||||
DEBUG_LOG("No socket to send to %s", UTI_IPSockAddrToString(remote_addr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Don't set address with connected socket */
|
||||
SCK_InitMessage(&message, SCK_ADDR_IP);
|
||||
|
||||
message.data = packet;
|
||||
message.length = length;
|
||||
|
||||
/* Specify remote address if the socket is not connected */
|
||||
if (NIO_IsServerSocket(local_addr->sock_fd) || !separate_client_sockets) {
|
||||
addrlen = UTI_IPAndPortToSockaddr(&remote_addr->ip_addr, remote_addr->port,
|
||||
&remote.u);
|
||||
if (!addrlen)
|
||||
return 0;
|
||||
message.remote_addr.ip.ip_addr = remote_addr->ip_addr;
|
||||
message.remote_addr.ip.port = remote_addr->port;
|
||||
}
|
||||
|
||||
if (addrlen) {
|
||||
msg.msg_name = &remote.u;
|
||||
msg.msg_namelen = addrlen;
|
||||
} else {
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
}
|
||||
message.local_addr.ip = local_addr->ip_addr;
|
||||
|
||||
iov.iov_base = packet;
|
||||
iov.iov_len = length;
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgbuf;
|
||||
msg.msg_controllen = sizeof(cmsgbuf);
|
||||
msg.msg_flags = 0;
|
||||
cmsglen = 0;
|
||||
/* Don't require responses to non-link-local addresses to use the same
|
||||
interface */
|
||||
message.if_index = SCK_IsLinkLocalIPAddress(&message.remote_addr.ip.ip_addr) ?
|
||||
local_addr->if_index : INVALID_IF_INDEX;
|
||||
|
||||
#ifdef HAVE_IN_PKTINFO
|
||||
if (local_addr->ip_addr.family == IPADDR_INET4) {
|
||||
struct in_pktinfo *ipi;
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
memset(cmsg, 0, CMSG_SPACE(sizeof(struct in_pktinfo)));
|
||||
cmsglen += CMSG_SPACE(sizeof(struct in_pktinfo));
|
||||
|
||||
cmsg->cmsg_level = IPPROTO_IP;
|
||||
cmsg->cmsg_type = IP_PKTINFO;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
|
||||
|
||||
ipi = (struct in_pktinfo *) CMSG_DATA(cmsg);
|
||||
ipi->ipi_spec_dst.s_addr = htonl(local_addr->ip_addr.addr.in4);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IN6_PKTINFO
|
||||
if (local_addr->ip_addr.family == IPADDR_INET6) {
|
||||
struct in6_pktinfo *ipi;
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
memset(cmsg, 0, CMSG_SPACE(sizeof(struct in6_pktinfo)));
|
||||
cmsglen += CMSG_SPACE(sizeof(struct in6_pktinfo));
|
||||
|
||||
cmsg->cmsg_level = IPPROTO_IPV6;
|
||||
cmsg->cmsg_type = IPV6_PKTINFO;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
|
||||
|
||||
ipi = (struct in6_pktinfo *) CMSG_DATA(cmsg);
|
||||
memcpy(&ipi->ipi6_addr.s6_addr, &local_addr->ip_addr.addr.in6,
|
||||
sizeof(ipi->ipi6_addr.s6_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))
|
||||
message.local_addr.ip.family = IPADDR_UNSPEC;
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||
if (process_tx)
|
||||
cmsglen = NIO_Linux_RequestTxTimestamp(&msg, cmsglen, local_addr->sock_fd);
|
||||
NIO_Linux_RequestTxTimestamp(&message, local_addr->sock_fd);
|
||||
#endif
|
||||
|
||||
msg.msg_controllen = cmsglen;
|
||||
/* This is apparently required on some systems */
|
||||
if (!cmsglen)
|
||||
msg.msg_control = NULL;
|
||||
|
||||
if (sendmsg(local_addr->sock_fd, &msg, 0) < 0) {
|
||||
DEBUG_LOG("Could not send to %s:%d from %s fd %d : %s",
|
||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
||||
UTI_IPToString(&local_addr->ip_addr), local_addr->sock_fd,
|
||||
strerror(errno));
|
||||
if (!SCK_SendMessage(local_addr->sock_fd, &message, 0))
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Sent %d bytes to %s:%d from %s fd %d", length,
|
||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
||||
UTI_IPToString(&local_addr->ip_addr), local_addr->sock_fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
8
ntp_io.h
8
ntp_io.h
@@ -33,7 +33,7 @@
|
||||
#include "addressing.h"
|
||||
|
||||
/* Function to initialise the module. */
|
||||
extern void NIO_Initialise(int family);
|
||||
extern void NIO_Initialise(void);
|
||||
|
||||
/* Function to finalise the module */
|
||||
extern void NIO_Finalise(void);
|
||||
@@ -53,6 +53,12 @@ extern void NIO_CloseServerSocket(int sock_fd);
|
||||
/* Function to check if socket is a server socket */
|
||||
extern int NIO_IsServerSocket(int sock_fd);
|
||||
|
||||
/* Function to check if a server socket is currently open */
|
||||
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 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);
|
||||
|
||||
379
ntp_io_linux.c
379
ntp_io_linux.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2016-2017
|
||||
* Copyright (C) Miroslav Lichvar 2016-2019
|
||||
*
|
||||
* 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
|
||||
@@ -29,7 +29,6 @@
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <ifaddrs.h>
|
||||
#include <linux/errqueue.h>
|
||||
#include <linux/ethtool.h>
|
||||
#include <linux/net_tstamp.h>
|
||||
#include <linux/sockios.h>
|
||||
@@ -45,17 +44,10 @@
|
||||
#include "ntp_io_linux.h"
|
||||
#include "ntp_sources.h"
|
||||
#include "sched.h"
|
||||
#include "socket.h"
|
||||
#include "sys_linux.h"
|
||||
#include "util.h"
|
||||
|
||||
union sockaddr_in46 {
|
||||
struct sockaddr_in in4;
|
||||
#ifdef FEAT_IPV6
|
||||
struct sockaddr_in6 in6;
|
||||
#endif
|
||||
struct sockaddr u;
|
||||
};
|
||||
|
||||
struct Interface {
|
||||
char name[IF_NAMESIZE];
|
||||
int if_index;
|
||||
@@ -94,6 +86,27 @@ 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
|
||||
enabling the timestamping and sending a request */
|
||||
static int dummy_rxts_socket;
|
||||
|
||||
#define INVALID_SOCK_FD -3
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
@@ -102,7 +115,7 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
struct ethtool_ts_info ts_info;
|
||||
struct hwtstamp_config ts_config;
|
||||
struct ifreq req;
|
||||
int sock_fd, if_index, phc_fd, req_hwts_flags;
|
||||
int sock_fd, if_index, phc_fd, req_hwts_flags, rx_filter;
|
||||
unsigned int i;
|
||||
struct Interface *iface;
|
||||
|
||||
@@ -112,7 +125,7 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
return 1;
|
||||
}
|
||||
|
||||
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
sock_fd = SCK_OpenUdpSocket(NULL, NULL, NULL, 0);
|
||||
if (sock_fd < 0)
|
||||
return 0;
|
||||
|
||||
@@ -121,13 +134,13 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
|
||||
if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", conf_iface->name) >=
|
||||
sizeof (req.ifr_name)) {
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ioctl(sock_fd, SIOCGIFINDEX, &req)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCGIFINDEX", strerror(errno));
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -138,7 +151,7 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
|
||||
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -146,47 +159,65 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
SOF_TIMESTAMPING_RAW_HARDWARE;
|
||||
if ((ts_info.so_timestamping & req_hwts_flags) != req_hwts_flags) {
|
||||
DEBUG_LOG("HW timestamping not supported on %s", req.ifr_name);
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ts_config.flags = 0;
|
||||
ts_config.tx_type = HWTSTAMP_TX_ON;
|
||||
if (ts_info.phc_index < 0) {
|
||||
DEBUG_LOG("PHC missing on %s", req.ifr_name);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (conf_iface->rxfilter) {
|
||||
case CNF_HWTS_RXFILTER_ANY:
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING_RXFILTER_NTP
|
||||
if (ts_info.rx_filters & (1 << HWTSTAMP_FILTER_NTP_ALL))
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||
rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||
else
|
||||
#endif
|
||||
if (ts_info.rx_filters & (1 << HWTSTAMP_FILTER_ALL))
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
|
||||
rx_filter = HWTSTAMP_FILTER_ALL;
|
||||
else
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_NONE;
|
||||
rx_filter = HWTSTAMP_FILTER_NONE;
|
||||
break;
|
||||
case CNF_HWTS_RXFILTER_NONE:
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_NONE;
|
||||
rx_filter = HWTSTAMP_FILTER_NONE;
|
||||
break;
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING_RXFILTER_NTP
|
||||
case CNF_HWTS_RXFILTER_NTP:
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||
rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
|
||||
rx_filter = HWTSTAMP_FILTER_ALL;
|
||||
break;
|
||||
}
|
||||
|
||||
ts_config.flags = 0;
|
||||
ts_config.tx_type = HWTSTAMP_TX_ON;
|
||||
ts_config.rx_filter = rx_filter;
|
||||
req.ifr_data = (char *)&ts_config;
|
||||
|
||||
if (ioctl(sock_fd, SIOCSHWTSTAMP, &req)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCSHWTSTAMP", strerror(errno));
|
||||
close(sock_fd);
|
||||
return 0;
|
||||
LOG(errno == EPERM ? LOGS_ERR : LOGS_DEBUG,
|
||||
"ioctl(%s) failed : %s", "SIOCSHWTSTAMP", strerror(errno));
|
||||
|
||||
/* Check the current timestamping configuration in case this interface
|
||||
allows only reading of the configuration and it was already configured
|
||||
as requested */
|
||||
req.ifr_data = (char *)&ts_config;
|
||||
#ifdef SIOCGHWTSTAMP
|
||||
if (ioctl(sock_fd, SIOCGHWTSTAMP, &req) ||
|
||||
ts_config.tx_type != HWTSTAMP_TX_ON || ts_config.rx_filter != rx_filter)
|
||||
#endif
|
||||
{
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
|
||||
phc_fd = SYS_Linux_OpenPHC(NULL, ts_info.phc_index);
|
||||
if (phc_fd < 0)
|
||||
@@ -209,7 +240,8 @@ add_interface(CNF_HwTsInterface *conf_iface)
|
||||
iface->tx_comp = conf_iface->tx_comp;
|
||||
iface->rx_comp = conf_iface->rx_comp;
|
||||
|
||||
iface->clock = HCL_CreateInstance(UTI_Log2ToDouble(MAX(conf_iface->minpoll, MIN_PHC_POLL)));
|
||||
iface->clock = HCL_CreateInstance(conf_iface->min_samples, conf_iface->max_samples,
|
||||
UTI_Log2ToDouble(MAX(conf_iface->minpoll, MIN_PHC_POLL)));
|
||||
|
||||
LOG(LOGS_INFO, "Enabled HW timestamping %son %s",
|
||||
ts_config.rx_filter == HWTSTAMP_FILTER_NONE ? "(TX only) " : "", iface->name);
|
||||
@@ -252,9 +284,9 @@ update_interface_speed(struct Interface *iface)
|
||||
{
|
||||
struct ethtool_cmd cmd;
|
||||
struct ifreq req;
|
||||
int sock_fd;
|
||||
int sock_fd, link_speed;
|
||||
|
||||
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
sock_fd = SCK_OpenUdpSocket(NULL, NULL, NULL, 0);
|
||||
if (sock_fd < 0)
|
||||
return;
|
||||
|
||||
@@ -267,13 +299,18 @@ update_interface_speed(struct Interface *iface)
|
||||
|
||||
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
||||
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
|
||||
iface->link_speed = ethtool_cmd_speed(&cmd);
|
||||
link_speed = ethtool_cmd_speed(&cmd);
|
||||
|
||||
if (iface->link_speed != link_speed) {
|
||||
iface->link_speed = link_speed;
|
||||
DEBUG_LOG("Updated speed of %s to %d Mb/s", iface->name, link_speed);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -284,23 +321,41 @@ check_timestamping_option(int option)
|
||||
{
|
||||
int sock_fd;
|
||||
|
||||
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
sock_fd = SCK_OpenUdpSocket(NULL, NULL, NULL, 0);
|
||||
if (sock_fd < 0)
|
||||
return 0;
|
||||
|
||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &option, sizeof (option)) < 0) {
|
||||
DEBUG_LOG("Could not enable timestamping option %x", option);
|
||||
close(sock_fd);
|
||||
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, option)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
open_dummy_socket(void)
|
||||
{
|
||||
int sock_fd, events = 0;
|
||||
|
||||
sock_fd = SCK_OpenUdpSocket(NULL, NULL, NULL, 0);
|
||||
if (sock_fd < 0)
|
||||
return INVALID_SOCK_FD;
|
||||
|
||||
if (!NIO_Linux_SetTimestampSocketOptions(sock_fd, 1, &events)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
return sock_fd;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NIO_Linux_Initialise(void)
|
||||
{
|
||||
@@ -350,6 +405,10 @@ 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;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -360,6 +419,9 @@ NIO_Linux_Finalise(void)
|
||||
struct Interface *iface;
|
||||
unsigned int i;
|
||||
|
||||
if (dummy_rxts_socket != INVALID_SOCK_FD)
|
||||
SCK_CloseSocket(dummy_rxts_socket);
|
||||
|
||||
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
||||
iface = ARR_GetElement(interfaces, i);
|
||||
HCL_DestroyInstance(iface->clock);
|
||||
@@ -388,14 +450,12 @@ NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events)
|
||||
if (client_only || permanent_ts_options)
|
||||
flags |= ts_tx_flags;
|
||||
|
||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_SELECT_ERR_QUEUE, &val, sizeof (val)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "SO_SELECT_ERR_QUEUE");
|
||||
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_SELECT_ERR_QUEUE, val)) {
|
||||
ts_flags = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof (flags)) < 0) {
|
||||
LOG(LOGS_ERR, "Could not set %s socket option", "SO_TIMESTAMPING");
|
||||
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, flags)) {
|
||||
ts_flags = 0;
|
||||
return 0;
|
||||
}
|
||||
@@ -406,6 +466,73 @@ 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)
|
||||
{
|
||||
@@ -454,7 +581,11 @@ process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
|
||||
if (rx_ntp_length && iface->link_speed) {
|
||||
if (!l2_length)
|
||||
l2_length = (family == IPADDR_INET4 ? iface->l2_udp4_ntp_start :
|
||||
iface->l2_udp6_ntp_start) + rx_ntp_length + 4;
|
||||
iface->l2_udp6_ntp_start) + rx_ntp_length;
|
||||
|
||||
/* Include the frame check sequence (FCS) */
|
||||
l2_length += 4;
|
||||
|
||||
rx_correction = l2_length / (1.0e6 / 8 * iface->link_speed);
|
||||
|
||||
UTI_AddDoubleToTimespec(hw_ts, rx_correction, hw_ts);
|
||||
@@ -488,7 +619,6 @@ static int
|
||||
extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
||||
{
|
||||
unsigned char *msg_start = msg;
|
||||
union sockaddr_in46 addr;
|
||||
|
||||
remote_addr->ip_addr.family = IPADDR_UNSPEC;
|
||||
remote_addr->port = 0;
|
||||
@@ -511,19 +641,21 @@ extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
||||
/* Parse destination address and port from IPv4/IPv6 and UDP headers */
|
||||
if (len >= 20 && msg[0] >> 4 == 4) {
|
||||
int ihl = (msg[0] & 0xf) * 4;
|
||||
uint32_t addr;
|
||||
|
||||
if (len < ihl + 8 || msg[9] != 17)
|
||||
return 0;
|
||||
|
||||
memcpy(&addr.in4.sin_addr.s_addr, msg + 16, sizeof (uint32_t));
|
||||
addr.in4.sin_port = *(uint16_t *)(msg + ihl + 2);
|
||||
addr.in4.sin_family = AF_INET;
|
||||
memcpy(&addr, msg + 16, sizeof (addr));
|
||||
remote_addr->ip_addr.addr.in4 = ntohl(addr);
|
||||
remote_addr->port = ntohs(*(uint16_t *)(msg + ihl + 2));
|
||||
remote_addr->ip_addr.family = IPADDR_INET4;
|
||||
len -= ihl + 8, msg += ihl + 8;
|
||||
#ifdef FEAT_IPV6
|
||||
} else if (len >= 48 && msg[0] >> 4 == 6) {
|
||||
int eh_len, next_header = msg[6];
|
||||
|
||||
memcpy(&addr.in6.sin6_addr.s6_addr, msg + 24, 16);
|
||||
memcpy(&remote_addr->ip_addr.addr.in6, msg + 24, sizeof (remote_addr->ip_addr.addr.in6));
|
||||
len -= 40, msg += 40;
|
||||
|
||||
/* Skip IPv6 extension headers if present */
|
||||
@@ -555,16 +687,14 @@ extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
||||
len -= eh_len, msg += eh_len;
|
||||
}
|
||||
|
||||
addr.in6.sin6_port = *(uint16_t *)(msg + 2);
|
||||
addr.in6.sin6_family = AF_INET6;
|
||||
remote_addr->port = ntohs(*(uint16_t *)(msg + 2));
|
||||
remote_addr->ip_addr.family = IPADDR_INET6;
|
||||
len -= 8, msg += 8;
|
||||
#endif
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
UTI_SockaddrToIPAndPort(&addr.u, &remote_addr->ip_addr, &remote_addr->port);
|
||||
|
||||
/* Move the message to fix alignment of its fields */
|
||||
if (len > 0)
|
||||
memmove(msg_start, msg, len);
|
||||
@@ -575,67 +705,47 @@ extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
NTP_Local_Timestamp *local_ts, struct msghdr *hdr, int length)
|
||||
NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr,
|
||||
NTP_Local_Timestamp *local_ts, int event)
|
||||
{
|
||||
struct Interface *iface;
|
||||
struct cmsghdr *cmsg;
|
||||
int is_tx, ts_if_index, l2_length;
|
||||
|
||||
is_tx = hdr->msg_flags & MSG_ERRQUEUE;
|
||||
is_tx = event == SCH_FILE_EXCEPTION;
|
||||
iface = NULL;
|
||||
ts_if_index = local_addr->if_index;
|
||||
l2_length = 0;
|
||||
|
||||
for (cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) {
|
||||
#ifdef HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING_PKTINFO) {
|
||||
struct scm_ts_pktinfo ts_pktinfo;
|
||||
ts_if_index = message->timestamp.if_index;
|
||||
if (ts_if_index == INVALID_IF_INDEX)
|
||||
ts_if_index = message->if_index;
|
||||
l2_length = message->timestamp.l2_length;
|
||||
|
||||
memcpy(&ts_pktinfo, CMSG_DATA(cmsg), sizeof (ts_pktinfo));
|
||||
|
||||
ts_if_index = ts_pktinfo.if_index;
|
||||
l2_length = ts_pktinfo.pkt_length;
|
||||
|
||||
DEBUG_LOG("Received HW timestamp info if=%d length=%d", ts_if_index, l2_length);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING) {
|
||||
struct scm_timestamping ts3;
|
||||
|
||||
memcpy(&ts3, CMSG_DATA(cmsg), sizeof (ts3));
|
||||
|
||||
if (!UTI_IsZeroTimespec(&ts3.ts[2])) {
|
||||
iface = get_interface(ts_if_index);
|
||||
if (iface) {
|
||||
process_hw_timestamp(iface, &ts3.ts[2], local_ts, !is_tx ? length : 0,
|
||||
remote_addr->ip_addr.family, l2_length);
|
||||
} else {
|
||||
DEBUG_LOG("HW clock not found for interface %d", ts_if_index);
|
||||
}
|
||||
}
|
||||
|
||||
if (local_ts->source == NTP_TS_DAEMON && !UTI_IsZeroTimespec(&ts3.ts[0]) &&
|
||||
(!is_tx || UTI_IsZeroTimespec(&ts3.ts[2]))) {
|
||||
LCL_CookTime(&ts3.ts[0], &local_ts->ts, &local_ts->err);
|
||||
local_ts->source = NTP_TS_KERNEL;
|
||||
}
|
||||
if (!UTI_IsZeroTimespec(&message->timestamp.hw)) {
|
||||
iface = get_interface(ts_if_index);
|
||||
if (iface) {
|
||||
process_hw_timestamp(iface, &message->timestamp.hw, local_ts, !is_tx ? message->length : 0,
|
||||
message->remote_addr.ip.ip_addr.family, l2_length);
|
||||
} else {
|
||||
DEBUG_LOG("HW clock not found for interface %d", ts_if_index);
|
||||
}
|
||||
|
||||
if ((cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) ||
|
||||
(cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_RECVERR)) {
|
||||
struct sock_extended_err err;
|
||||
/* 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);
|
||||
}
|
||||
|
||||
memcpy(&err, CMSG_DATA(cmsg), sizeof (err));
|
||||
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;
|
||||
}
|
||||
|
||||
if (err.ee_errno != ENOMSG || err.ee_info != SCM_TSTAMP_SND ||
|
||||
err.ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
|
||||
DEBUG_LOG("Unknown extended error");
|
||||
/* Drop the message */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* If the kernel is slow with enabling RX timestamping, open a dummy
|
||||
socket to keep the kernel RX timestamping permanently enabled */
|
||||
if (!is_tx && local_ts->source == NTP_TS_DAEMON && ts_flags) {
|
||||
DEBUG_LOG("Missing kernel RX timestamp");
|
||||
if (dummy_rxts_socket == INVALID_SOCK_FD)
|
||||
dummy_rxts_socket = open_dummy_socket();
|
||||
}
|
||||
|
||||
/* Return the message if it's not received from the error queue */
|
||||
@@ -645,19 +755,19 @@ NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *loc
|
||||
/* The data from the error queue includes all layers up to UDP. We have to
|
||||
extract the UDP data and also the destination address with port as there
|
||||
currently doesn't seem to be a better way to get them both. */
|
||||
l2_length = length;
|
||||
length = extract_udp_data(hdr->msg_iov[0].iov_base, remote_addr, length);
|
||||
l2_length = message->length;
|
||||
message->length = extract_udp_data(message->data, &message->remote_addr.ip, message->length);
|
||||
|
||||
DEBUG_LOG("Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%d",
|
||||
l2_length, length, UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
||||
local_addr->sock_fd, local_addr->if_index, local_ts->source);
|
||||
DEBUG_LOG("Extracted message for %s fd=%d len=%d",
|
||||
UTI_IPSockAddrToString(&message->remote_addr.ip),
|
||||
local_addr->sock_fd, message->length);
|
||||
|
||||
/* Update assumed position of UDP data at layer 2 for next received packet */
|
||||
if (iface && length) {
|
||||
if (remote_addr->ip_addr.family == IPADDR_INET4)
|
||||
iface->l2_udp4_ntp_start = l2_length - length;
|
||||
else if (remote_addr->ip_addr.family == IPADDR_INET6)
|
||||
iface->l2_udp6_ntp_start = l2_length - length;
|
||||
if (iface && message->length) {
|
||||
if (message->remote_addr.ip.ip_addr.family == IPADDR_INET4)
|
||||
iface->l2_udp4_ntp_start = l2_length - message->length;
|
||||
else if (message->remote_addr.ip.ip_addr.family == IPADDR_INET6)
|
||||
iface->l2_udp6_ntp_start = l2_length - message->length;
|
||||
}
|
||||
|
||||
/* Drop the message if it has no timestamp or its processing failed */
|
||||
@@ -666,38 +776,39 @@ NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *loc
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (length < NTP_NORMAL_PACKET_LENGTH)
|
||||
if (message->length < NTP_HEADER_LENGTH)
|
||||
return 1;
|
||||
|
||||
NSR_ProcessTx(remote_addr, local_addr, local_ts,
|
||||
(NTP_Packet *)hdr->msg_iov[0].iov_base, length);
|
||||
NSR_ProcessTx(&message->remote_addr.ip, local_addr, local_ts, message->data, message->length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NIO_Linux_RequestTxTimestamp(struct msghdr *msg, int cmsglen, int sock_fd)
|
||||
void
|
||||
NIO_Linux_RequestTxTimestamp(SCK_Message *message, int sock_fd)
|
||||
{
|
||||
struct cmsghdr *cmsg;
|
||||
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 cmsglen;
|
||||
return;
|
||||
|
||||
/* Add control message that will enable TX timestamping for this message.
|
||||
Don't use CMSG_NXTHDR as the one in glibc is buggy for creating new
|
||||
control messages. */
|
||||
cmsg = (struct cmsghdr *)((char *)CMSG_FIRSTHDR(msg) + cmsglen);
|
||||
memset(cmsg, 0, CMSG_SPACE(sizeof (ts_tx_flags)));
|
||||
cmsglen += CMSG_SPACE(sizeof (ts_tx_flags));
|
||||
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SO_TIMESTAMPING;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof (ts_tx_flags));
|
||||
|
||||
memcpy(CMSG_DATA(cmsg), &ts_tx_flags, sizeof (ts_tx_flags));
|
||||
|
||||
return cmsglen;
|
||||
message->timestamp.tx_flags = ts_tx_flags;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NIO_Linux_NotifySocketClosing(int sock_fd)
|
||||
{
|
||||
resume_socket(sock_fd);
|
||||
}
|
||||
|
||||
@@ -24,13 +24,24 @@
|
||||
This is the header file for the Linux-specific NTP socket I/O bits.
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTP_IO_LINUX_H
|
||||
#define GOT_NTP_IO_LINUX_H
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
extern void NIO_Linux_Initialise(void);
|
||||
|
||||
extern void NIO_Linux_Finalise(void);
|
||||
|
||||
extern int NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events);
|
||||
|
||||
extern int NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
NTP_Local_Timestamp *local_ts, struct msghdr *hdr, int length);
|
||||
extern int NIO_Linux_ProcessEvent(int sock_fd, int event);
|
||||
|
||||
extern int NIO_Linux_RequestTxTimestamp(struct msghdr *msg, int cmsglen, int sock_fd);
|
||||
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
|
||||
|
||||
82
ntp_signd.c
82
ntp_signd.c
@@ -34,6 +34,7 @@
|
||||
#include "ntp_io.h"
|
||||
#include "ntp_signd.h"
|
||||
#include "sched.h"
|
||||
#include "socket.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Declarations per samba/source4/librpc/idl/ntp_signd.idl */
|
||||
@@ -90,19 +91,11 @@ static ARR_Instance queue;
|
||||
static unsigned int queue_head;
|
||||
static unsigned int queue_tail;
|
||||
|
||||
#define INVALID_SOCK_FD -1
|
||||
#define INVALID_SOCK_FD (-6)
|
||||
|
||||
/* Unix domain socket connected to ntp_signd */
|
||||
static int sock_fd;
|
||||
|
||||
#define MIN_AUTH_DELAY 1.0e-5
|
||||
#define MAX_AUTH_DELAY 1.0e-2
|
||||
|
||||
/* Average time needed for signing one packet. This is used to adjust the
|
||||
transmit timestamp in NTP packets. The timestamp won't be very accurate as
|
||||
the delay is variable, but it should be good enough for MS-SNTP clients. */
|
||||
static double auth_delay;
|
||||
|
||||
/* Flag indicating if the MS-SNTP authentication is enabled */
|
||||
static int enabled;
|
||||
|
||||
@@ -116,7 +109,7 @@ static void
|
||||
close_socket(void)
|
||||
{
|
||||
SCH_RemoveFileHandler(sock_fd);
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
sock_fd = INVALID_SOCK_FD;
|
||||
|
||||
/* Empty the queue */
|
||||
@@ -128,35 +121,23 @@ close_socket(void)
|
||||
static int
|
||||
open_socket(void)
|
||||
{
|
||||
struct sockaddr_un s;
|
||||
char path[PATH_MAX];
|
||||
|
||||
if (sock_fd >= 0)
|
||||
if (sock_fd != INVALID_SOCK_FD)
|
||||
return 1;
|
||||
|
||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock_fd < 0) {
|
||||
DEBUG_LOG("Could not open signd socket : %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
UTI_FdSetCloexec(sock_fd);
|
||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_write_socket, NULL);
|
||||
|
||||
s.sun_family = AF_UNIX;
|
||||
if (snprintf(s.sun_path, sizeof (s.sun_path), "%s/socket",
|
||||
CNF_GetNtpSigndSocket()) >= sizeof (s.sun_path)) {
|
||||
if (snprintf(path, sizeof (path), "%s/socket", CNF_GetNtpSigndSocket()) >= sizeof (path)) {
|
||||
DEBUG_LOG("signd socket path too long");
|
||||
close_socket();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (connect(sock_fd, (struct sockaddr *)&s, sizeof (s)) < 0) {
|
||||
DEBUG_LOG("Could not connect to signd : %s", strerror(errno));
|
||||
close_socket();
|
||||
sock_fd = SCK_OpenUnixStreamSocket(path, NULL, 0);
|
||||
if (sock_fd < 0) {
|
||||
sock_fd = INVALID_SOCK_FD;
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Connected to signd");
|
||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_write_socket, NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -194,10 +175,6 @@ process_response(SignInstance *inst)
|
||||
NIO_SendPacket(&inst->response.signed_packet, &inst->remote_addr, &inst->local_addr,
|
||||
ntohl(inst->response.length) + sizeof (inst->response.length) -
|
||||
offsetof(SigndResponse, signed_packet), 0);
|
||||
|
||||
/* Update exponential moving average of the authentication delay */
|
||||
delay = CLAMP(MIN_AUTH_DELAY, delay, MAX_AUTH_DELAY);
|
||||
auth_delay += 0.1 * (delay - auth_delay);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -218,16 +195,14 @@ read_write_socket(int sock_fd, int event, void *anything)
|
||||
if (!inst->sent)
|
||||
SCH_GetLastEventTime(NULL, NULL, &inst->request_ts);
|
||||
|
||||
s = send(sock_fd, (char *)&inst->request + inst->sent,
|
||||
inst->request_length - inst->sent, 0);
|
||||
s = SCK_Send(sock_fd, (char *)&inst->request + inst->sent,
|
||||
inst->request_length - inst->sent, 0);
|
||||
|
||||
if (s < 0) {
|
||||
DEBUG_LOG("signd socket error: %s", strerror(errno));
|
||||
close_socket();
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Sent %d bytes to signd", s);
|
||||
inst->sent += s;
|
||||
|
||||
/* Try again later if the request is not complete yet */
|
||||
@@ -235,7 +210,7 @@ read_write_socket(int sock_fd, int event, void *anything)
|
||||
return;
|
||||
|
||||
/* Disable output and wait for a response */
|
||||
SCH_SetFileHandlerEvents(sock_fd, SCH_FILE_INPUT);
|
||||
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_OUTPUT, 0);
|
||||
}
|
||||
|
||||
if (event == SCH_FILE_INPUT) {
|
||||
@@ -246,20 +221,14 @@ read_write_socket(int sock_fd, int event, void *anything)
|
||||
}
|
||||
|
||||
assert(inst->received < sizeof (inst->response));
|
||||
s = recv(sock_fd, (char *)&inst->response + inst->received,
|
||||
sizeof (inst->response) - inst->received, 0);
|
||||
s = SCK_Receive(sock_fd, (char *)&inst->response + inst->received,
|
||||
sizeof (inst->response) - inst->received, 0);
|
||||
|
||||
if (s <= 0) {
|
||||
if (s < 0)
|
||||
DEBUG_LOG("signd socket error: %s", strerror(errno));
|
||||
else
|
||||
DEBUG_LOG("signd socket closed");
|
||||
|
||||
close_socket();
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Received %d bytes from signd", s);
|
||||
inst->received += s;
|
||||
|
||||
if (inst->received < sizeof (inst->response.length))
|
||||
@@ -283,7 +252,7 @@ read_write_socket(int sock_fd, int event, void *anything)
|
||||
/* Move the head and enable output for the next packet */
|
||||
queue_head = NEXT_QUEUE_INDEX(queue_head);
|
||||
if (!IS_QUEUE_EMPTY())
|
||||
SCH_SetFileHandlerEvents(sock_fd, SCH_FILE_INPUT | SCH_FILE_OUTPUT);
|
||||
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_OUTPUT, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,7 +262,6 @@ void
|
||||
NSD_Initialise()
|
||||
{
|
||||
sock_fd = INVALID_SOCK_FD;
|
||||
auth_delay = MIN_AUTH_DELAY;
|
||||
enabled = CNF_GetNtpSigndSocket() && CNF_GetNtpSigndSocket()[0];
|
||||
|
||||
if (!enabled)
|
||||
@@ -320,15 +288,9 @@ NSD_Finalise()
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
extern int NSD_GetAuthDelay(uint32_t key_id)
|
||||
{
|
||||
return 1.0e9 * auth_delay;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr, int length)
|
||||
NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_PacketInfo *info,
|
||||
NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr)
|
||||
{
|
||||
SignInstance *inst;
|
||||
|
||||
@@ -342,7 +304,7 @@ NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *r
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (length != NTP_NORMAL_PACKET_LENGTH) {
|
||||
if (info->length != NTP_HEADER_LENGTH) {
|
||||
DEBUG_LOG("Invalid packet length");
|
||||
return 0;
|
||||
}
|
||||
@@ -355,7 +317,7 @@ NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *r
|
||||
inst->local_addr = *local_addr;
|
||||
inst->sent = 0;
|
||||
inst->received = 0;
|
||||
inst->request_length = offsetof(SigndRequest, packet_to_sign) + length;
|
||||
inst->request_length = offsetof(SigndRequest, packet_to_sign) + info->length;
|
||||
|
||||
/* The length field doesn't include itself */
|
||||
inst->request.length = htonl(inst->request_length - sizeof (inst->request.length));
|
||||
@@ -365,11 +327,11 @@ NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *r
|
||||
inst->request._pad = 0;
|
||||
inst->request.key_id = htonl(key_id);
|
||||
|
||||
memcpy(&inst->request.packet_to_sign, packet, length);
|
||||
memcpy(&inst->request.packet_to_sign, packet, info->length);
|
||||
|
||||
/* Enable output if there was no pending request */
|
||||
if (IS_QUEUE_EMPTY())
|
||||
SCH_SetFileHandlerEvents(sock_fd, SCH_FILE_INPUT | SCH_FILE_OUTPUT);
|
||||
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_OUTPUT, 1);
|
||||
|
||||
queue_tail = NEXT_QUEUE_INDEX(queue_tail);
|
||||
|
||||
|
||||
@@ -35,10 +35,8 @@ extern void NSD_Initialise(void);
|
||||
/* Finalisation function */
|
||||
extern void NSD_Finalise(void);
|
||||
|
||||
/* Function to get an estimate of delay due to signing */
|
||||
extern int NSD_GetAuthDelay(uint32_t key_id);
|
||||
|
||||
/* Function to sign an NTP packet and send it */
|
||||
extern int NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr, int length);
|
||||
extern int NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_PacketInfo *info,
|
||||
NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr);
|
||||
|
||||
#endif
|
||||
|
||||
943
ntp_sources.c
943
ntp_sources.c
File diff suppressed because it is too large
Load Diff
@@ -44,16 +44,21 @@ typedef enum {
|
||||
NSR_NoSuchSource, /* Remove - attempt to remove a source that is not known */
|
||||
NSR_AlreadyInUse, /* AddSource - attempt to add a source that is already known */
|
||||
NSR_TooManySources, /* AddSource - too many sources already present */
|
||||
NSR_InvalidAF /* AddSource - attempt to add a source with invalid address family */
|
||||
NSR_InvalidAF, /* AddSource - attempt to add a source with invalid address family */
|
||||
NSR_InvalidName, /* AddSourceByName - attempt to add a source with invalid name */
|
||||
NSR_UnresolvedName, /* AddSourceByName - name will be resolved later */
|
||||
} NSR_Status;
|
||||
|
||||
/* Procedure to add a new server or peer source. */
|
||||
extern NSR_Status NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParameters *params);
|
||||
extern NSR_Status NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
|
||||
SourceParameters *params, uint32_t *conf_id);
|
||||
|
||||
/* Procedure to add a new server, peer source, or pool of servers specified by
|
||||
name instead of address. The name is resolved in exponentially increasing
|
||||
intervals until it succeeds or fails with a non-temporary error. */
|
||||
extern void NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params);
|
||||
intervals until it succeeds or fails with a non-temporary error. If the
|
||||
name is an address, it is equivalent to NSR_AddSource(). */
|
||||
extern NSR_Status NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
|
||||
SourceParameters *params, uint32_t *conf_id);
|
||||
|
||||
/* Function type for handlers to be called back when an attempt
|
||||
* (possibly unsuccessful) to resolve unresolved sources ends */
|
||||
@@ -72,7 +77,10 @@ extern void NSR_StartSources(void);
|
||||
extern void NSR_AutoStartSources(void);
|
||||
|
||||
/* Procedure to remove a source */
|
||||
extern NSR_Status NSR_RemoveSource(NTP_Remote_Address *remote_addr);
|
||||
extern NSR_Status NSR_RemoveSource(IPAddr *address);
|
||||
|
||||
/* Procedure to remove all sources matching a configuration ID */
|
||||
extern void NSR_RemoveSourcesById(uint32_t conf_id);
|
||||
|
||||
/* Procedure to remove all sources */
|
||||
extern void NSR_RemoveAllSources(void);
|
||||
@@ -83,9 +91,18 @@ 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. 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 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 */
|
||||
extern void NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||
NTP_Local_Timestamp *rx_ts, NTP_Packet *message, int length);
|
||||
@@ -102,14 +119,9 @@ extern void NSR_Initialise(void);
|
||||
extern void NSR_Finalise(void);
|
||||
|
||||
/* This routine is used to indicate that sources whose IP addresses
|
||||
match a particular subnet should be set online again. Returns a
|
||||
flag indicating whether any hosts matched the address */
|
||||
extern int NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address);
|
||||
|
||||
/* This routine is used to indicate that sources whose IP addresses
|
||||
match a particular subnet should be set offline. Returns a flag
|
||||
indicating whether any hosts matched the address */
|
||||
extern int NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address);
|
||||
match a particular subnet should be set online or offline. It returns
|
||||
a flag indicating whether any hosts matched the address. */
|
||||
extern int NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity);
|
||||
|
||||
extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll);
|
||||
|
||||
@@ -129,8 +141,12 @@ extern int NSR_InitiateSampleBurst(int n_good_samples, int n_total_samples, IPAd
|
||||
|
||||
extern void NSR_ReportSource(RPT_SourceReport *report, struct timespec *now);
|
||||
|
||||
extern int NSR_GetAuthReport(IPAddr *address, RPT_AuthReport *report);
|
||||
|
||||
extern int NSR_GetNTPReport(RPT_NTPReport *report);
|
||||
|
||||
extern void NSR_GetActivityReport(RPT_ActivityReport *report);
|
||||
|
||||
extern void NSR_DumpAuthData(void);
|
||||
|
||||
#endif /* GOT_NTP_SOURCES_H */
|
||||
|
||||
81
nts_ke.h
Normal file
81
nts_ke.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 the NTS Key Establishment protocol
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_KE_H
|
||||
#define GOT_NTS_KE_H
|
||||
|
||||
#include "siv.h"
|
||||
|
||||
#define NKE_PORT 4460
|
||||
|
||||
#define NKE_RECORD_CRITICAL_BIT (1U << 15)
|
||||
#define NKE_RECORD_END_OF_MESSAGE 0
|
||||
#define NKE_RECORD_NEXT_PROTOCOL 1
|
||||
#define NKE_RECORD_ERROR 2
|
||||
#define NKE_RECORD_WARNING 3
|
||||
#define NKE_RECORD_AEAD_ALGORITHM 4
|
||||
#define NKE_RECORD_COOKIE 5
|
||||
#define NKE_RECORD_NTPV4_SERVER_NEGOTIATION 6
|
||||
#define NKE_RECORD_NTPV4_PORT_NEGOTIATION 7
|
||||
|
||||
#define NKE_NEXT_PROTOCOL_NTPV4 0
|
||||
|
||||
#define NKE_ERROR_UNRECOGNIZED_CRITICAL_RECORD 0
|
||||
#define NKE_ERROR_BAD_REQUEST 1
|
||||
#define NKE_ERROR_INTERNAL_SERVER_ERROR 2
|
||||
|
||||
#define NKE_ALPN_NAME "ntske/1"
|
||||
#define NKE_EXPORTER_LABEL "EXPORTER-network-time-security"
|
||||
#define NKE_EXPORTER_CONTEXT_C2S "\x0\x0\x0\xf\x0"
|
||||
#define NKE_EXPORTER_CONTEXT_S2C "\x0\x0\x0\xf\x1"
|
||||
|
||||
#define NKE_MAX_MESSAGE_LENGTH 16384
|
||||
#define NKE_MAX_RECORD_BODY_LENGTH 256
|
||||
#define NKE_MAX_COOKIE_LENGTH 256
|
||||
#define NKE_MAX_COOKIES 8
|
||||
#define NKE_MAX_KEY_LENGTH SIV_MAX_KEY_LENGTH
|
||||
|
||||
#define NKE_RETRY_FACTOR2_CONNECT 4
|
||||
#define NKE_RETRY_FACTOR2_TLS 10
|
||||
#define NKE_MAX_RETRY_INTERVAL2 19
|
||||
|
||||
typedef struct {
|
||||
int length;
|
||||
unsigned char key[NKE_MAX_KEY_LENGTH];
|
||||
} NKE_Key;
|
||||
|
||||
typedef struct {
|
||||
SIV_Algorithm algorithm;
|
||||
NKE_Key c2s;
|
||||
NKE_Key s2c;
|
||||
} NKE_Context;
|
||||
|
||||
typedef struct {
|
||||
int length;
|
||||
unsigned char cookie[NKE_MAX_COOKIE_LENGTH];
|
||||
} NKE_Cookie;
|
||||
|
||||
#endif
|
||||
434
nts_ke_client.c
Normal file
434
nts_ke_client.c
Normal file
@@ -0,0 +1,434 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
NTS-KE client
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ke_client.h"
|
||||
|
||||
#include "conf.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "nameserv_async.h"
|
||||
#include "nts_ke_session.h"
|
||||
#include "siv.h"
|
||||
#include "socket.h"
|
||||
#include "util.h"
|
||||
|
||||
#define CLIENT_TIMEOUT 16.0
|
||||
|
||||
struct NKC_Instance_Record {
|
||||
char *name;
|
||||
IPSockAddr address;
|
||||
NKSN_Credentials credentials;
|
||||
NKSN_Instance session;
|
||||
int destroying;
|
||||
int got_response;
|
||||
int resolving_name;
|
||||
|
||||
NKE_Context context;
|
||||
NKE_Cookie cookies[NKE_MAX_COOKIES];
|
||||
int num_cookies;
|
||||
char server_name[NKE_MAX_RECORD_BODY_LENGTH + 2];
|
||||
IPSockAddr ntp_address;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static NKSN_Credentials default_credentials = NULL;
|
||||
static int default_credentials_refs = 0;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *arg)
|
||||
{
|
||||
NKC_Instance inst = arg;
|
||||
int i;
|
||||
|
||||
inst->resolving_name = 0;
|
||||
|
||||
if (inst->destroying) {
|
||||
Free(inst);
|
||||
return;
|
||||
}
|
||||
|
||||
if (status != DNS_Success || n_addrs < 1) {
|
||||
LOG(LOGS_ERR, "Could not resolve NTP server %s from %s", inst->server_name, inst->name);
|
||||
/* Force restart */
|
||||
inst->got_response = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
inst->ntp_address.ip_addr = ip_addrs[0];
|
||||
|
||||
/* Prefer an address in the same family as the NTS-KE server */
|
||||
for (i = 0; i < n_addrs; i++) {
|
||||
DEBUG_LOG("%s resolved to %s", inst->server_name, UTI_IPToString(&ip_addrs[i]));
|
||||
if (ip_addrs[i].family == inst->address.ip_addr.family) {
|
||||
inst->ntp_address.ip_addr = ip_addrs[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
prepare_request(NKC_Instance inst)
|
||||
{
|
||||
NKSN_Instance session = inst->session;
|
||||
uint16_t datum;
|
||||
|
||||
NKSN_BeginMessage(session);
|
||||
|
||||
datum = htons(NKE_NEXT_PROTOCOL_NTPV4);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
|
||||
datum = htons(AEAD_AES_SIV_CMAC_256);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_AEAD_ALGORITHM, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
|
||||
if (!NKSN_EndMessage(session))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
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)];
|
||||
|
||||
assert(NKE_MAX_COOKIE_LENGTH % sizeof (uint16_t) == 0);
|
||||
assert(sizeof (uint16_t) == 2);
|
||||
|
||||
inst->num_cookies = 0;
|
||||
inst->ntp_address.ip_addr.family = IPADDR_UNSPEC;
|
||||
inst->ntp_address.port = 0;
|
||||
inst->server_name[0] = '\0';
|
||||
|
||||
while (!error) {
|
||||
if (!NKSN_GetRecord(inst->session, &critical, &type, &length, &data, sizeof (data)))
|
||||
break;
|
||||
|
||||
switch (type) {
|
||||
case NKE_RECORD_NEXT_PROTOCOL:
|
||||
if (!critical || length != 2 || ntohs(data[0]) != NKE_NEXT_PROTOCOL_NTPV4) {
|
||||
DEBUG_LOG("Unexpected NTS-KE next protocol");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
next_protocol = NKE_NEXT_PROTOCOL_NTPV4;
|
||||
break;
|
||||
case NKE_RECORD_AEAD_ALGORITHM:
|
||||
if (length != 2 || ntohs(data[0]) != AEAD_AES_SIV_CMAC_256) {
|
||||
DEBUG_LOG("Unexpected NTS-KE AEAD algorithm");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
aead_algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
inst->context.algorithm = aead_algorithm;
|
||||
break;
|
||||
case NKE_RECORD_ERROR:
|
||||
if (length == 2)
|
||||
DEBUG_LOG("NTS-KE error %d", ntohs(data[0]));
|
||||
error = 1;
|
||||
break;
|
||||
case NKE_RECORD_WARNING:
|
||||
if (length == 2)
|
||||
DEBUG_LOG("NTS-KE warning %d", ntohs(data[0]));
|
||||
error = 1;
|
||||
break;
|
||||
case NKE_RECORD_COOKIE:
|
||||
DEBUG_LOG("Got cookie length=%d", length);
|
||||
|
||||
if (length < 1 || length > NKE_MAX_COOKIE_LENGTH || length % 4 != 0 ||
|
||||
inst->num_cookies >= NKE_MAX_COOKIES) {
|
||||
DEBUG_LOG("Unexpected length/cookie");
|
||||
break;
|
||||
}
|
||||
|
||||
assert(NKE_MAX_COOKIE_LENGTH == sizeof (inst->cookies[inst->num_cookies].cookie));
|
||||
assert(NKE_MAX_COOKIES == sizeof (inst->cookies) /
|
||||
sizeof (inst->cookies[inst->num_cookies]));
|
||||
inst->cookies[inst->num_cookies].length = length;
|
||||
memcpy(inst->cookies[inst->num_cookies].cookie, data, length);
|
||||
|
||||
inst->num_cookies++;
|
||||
break;
|
||||
case NKE_RECORD_NTPV4_SERVER_NEGOTIATION:
|
||||
if (length < 1 || length >= sizeof (inst->server_name)) {
|
||||
DEBUG_LOG("Invalid server name");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(inst->server_name, data, length);
|
||||
inst->server_name[length] = '\0';
|
||||
|
||||
/* Make sure the name is printable and has no spaces */
|
||||
for (i = 0; i < length && isgraph((unsigned char)inst->server_name[i]); i++)
|
||||
;
|
||||
if (i != length) {
|
||||
DEBUG_LOG("Invalid server name");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Negotiated server %s", inst->server_name);
|
||||
break;
|
||||
case NKE_RECORD_NTPV4_PORT_NEGOTIATION:
|
||||
if (length != 2) {
|
||||
DEBUG_LOG("Invalid port");
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
inst->ntp_address.port = ntohs(data[0]);
|
||||
DEBUG_LOG("Negotiated port %d", inst->ntp_address.port);
|
||||
break;
|
||||
default:
|
||||
DEBUG_LOG("Unknown record type=%d length=%d critical=%d", type, length, critical);
|
||||
if (critical)
|
||||
error = 1;
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_LOG("NTS-KE response: error=%d next=%d aead=%d",
|
||||
error, next_protocol, aead_algorithm);
|
||||
|
||||
if (error || inst->num_cookies == 0 ||
|
||||
next_protocol != NKE_NEXT_PROTOCOL_NTPV4 ||
|
||||
aead_algorithm != AEAD_AES_SIV_CMAC_256)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
handle_message(void *arg)
|
||||
{
|
||||
NKC_Instance inst = arg;
|
||||
|
||||
if (!process_response(inst)) {
|
||||
LOG(LOGS_ERR, "Received invalid NTS-KE response from %s", inst->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!NKSN_GetKeys(inst->session, inst->context.algorithm,
|
||||
&inst->context.c2s, &inst->context.s2c))
|
||||
return 0;
|
||||
|
||||
if (inst->server_name[0] != '\0') {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
inst->got_response = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NKC_Instance
|
||||
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);
|
||||
|
||||
inst->address = *address;
|
||||
inst->name = Strdup(name);
|
||||
inst->session = NKSN_CreateInstance(0, inst->name, handle_message, inst);
|
||||
inst->resolving_name = 0;
|
||||
inst->destroying = 0;
|
||||
inst->got_response = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKC_DestroyInstance(NKC_Instance inst)
|
||||
{
|
||||
NKSN_DestroyInstance(inst->session);
|
||||
|
||||
Free(inst->name);
|
||||
|
||||
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
|
||||
the instance later */
|
||||
if (inst->resolving_name) {
|
||||
inst->destroying = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
Free(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKC_Start(NKC_Instance inst)
|
||||
{
|
||||
IPSockAddr local_addr;
|
||||
char label[512], *iface;
|
||||
int sock_fd;
|
||||
|
||||
assert(!NKC_IsActive(inst));
|
||||
|
||||
inst->got_response = 0;
|
||||
|
||||
if (!inst->credentials) {
|
||||
DEBUG_LOG("Missing client credentials");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Follow the bindacqaddress and bindacqdevice settings */
|
||||
CNF_GetBindAcquisitionAddress(inst->address.ip_addr.family, &local_addr.ip_addr);
|
||||
local_addr.port = 0;
|
||||
iface = CNF_GetBindAcquisitionInterface();
|
||||
|
||||
/* Make a label containing both the address and name of the server */
|
||||
if (snprintf(label, sizeof (label), "%s (%s)",
|
||||
UTI_IPSockAddrToString(&inst->address), inst->name) >= sizeof (label))
|
||||
;
|
||||
|
||||
sock_fd = SCK_OpenTcpSocket(&inst->address, &local_addr, iface, 0);
|
||||
if (sock_fd < 0) {
|
||||
LOG(LOGS_ERR, "Could not connect to %s", label);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Start an NTS-KE session */
|
||||
if (!NKSN_StartSession(inst->session, sock_fd, label, inst->credentials, CLIENT_TIMEOUT)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Send a request */
|
||||
if (!prepare_request(inst)) {
|
||||
DEBUG_LOG("Could not prepare NTS-KE request");
|
||||
NKSN_StopSession(inst->session);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKC_IsActive(NKC_Instance inst)
|
||||
{
|
||||
return !NKSN_IsStopped(inst->session) || inst->resolving_name;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKC_GetNtsData(NKC_Instance inst, NKE_Context *context,
|
||||
NKE_Cookie *cookies, int *num_cookies, int max_cookies,
|
||||
IPSockAddr *ntp_address)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!inst->got_response || inst->resolving_name)
|
||||
return 0;
|
||||
|
||||
*context = inst->context;
|
||||
|
||||
for (i = 0; i < inst->num_cookies && i < max_cookies; i++)
|
||||
cookies[i] = inst->cookies[i];
|
||||
*num_cookies = i;
|
||||
|
||||
*ntp_address = inst->ntp_address;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKC_GetRetryFactor(NKC_Instance inst)
|
||||
{
|
||||
return NKSN_GetRetryFactor(inst->session);
|
||||
}
|
||||
56
nts_ke_client.h
Normal file
56
nts_ke_client.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 the NTS-KE client
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_KE_CLIENT_H
|
||||
#define GOT_NTS_KE_CLIENT_H
|
||||
|
||||
#include "addressing.h"
|
||||
#include "nts_ke.h"
|
||||
|
||||
typedef struct NKC_Instance_Record *NKC_Instance;
|
||||
|
||||
/* Create a client NTS-KE instance */
|
||||
extern NKC_Instance NKC_CreateInstance(IPSockAddr *address, const char *name, uint32_t cert_set);
|
||||
|
||||
/* Destroy an instance */
|
||||
extern void NKC_DestroyInstance(NKC_Instance inst);
|
||||
|
||||
/* Connect to the server, start an NTS-KE session, send an NTS-KE request, and
|
||||
process the response (asynchronously) */
|
||||
extern int NKC_Start(NKC_Instance inst);
|
||||
|
||||
/* Check if the client is still running */
|
||||
extern int NKC_IsActive(NKC_Instance inst);
|
||||
|
||||
/* Get the NTS data if the session was successful */
|
||||
extern int NKC_GetNtsData(NKC_Instance inst, NKE_Context *context,
|
||||
NKE_Cookie *cookies, int *num_cookies, int max_cookies,
|
||||
IPSockAddr *ntp_address);
|
||||
|
||||
/* Get a factor to calculate retry interval (in log2 seconds) */
|
||||
extern int NKC_GetRetryFactor(NKC_Instance inst);
|
||||
|
||||
#endif
|
||||
963
nts_ke_server.c
Normal file
963
nts_ke_server.c
Normal file
@@ -0,0 +1,963 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
NTS-KE server
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ke_server.h"
|
||||
|
||||
#include "array.h"
|
||||
#include "conf.h"
|
||||
#include "clientlog.h"
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "ntp_core.h"
|
||||
#include "nts_ke_session.h"
|
||||
#include "privops.h"
|
||||
#include "siv.h"
|
||||
#include "socket.h"
|
||||
#include "sched.h"
|
||||
#include "sys.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SERVER_TIMEOUT 2.0
|
||||
|
||||
#define SERVER_COOKIE_SIV AEAD_AES_SIV_CMAC_256
|
||||
#define SERVER_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 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_Instance siv;
|
||||
} ServerKey;
|
||||
|
||||
typedef struct {
|
||||
uint32_t key_id;
|
||||
unsigned char key[SIV_MAX_KEY_LENGTH];
|
||||
IPAddr client_addr;
|
||||
uint16_t client_port;
|
||||
uint16_t _pad;
|
||||
} HelperRequest;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static ServerKey server_keys[MAX_SERVER_KEYS];
|
||||
static int current_server_key;
|
||||
static double last_server_key_ts;
|
||||
static int key_rotation_interval;
|
||||
|
||||
static int server_sock_fd4;
|
||||
static int server_sock_fd6;
|
||||
|
||||
static int helper_sock_fd;
|
||||
static int is_helper;
|
||||
|
||||
static int initialised = 0;
|
||||
|
||||
/* Array of NKSN instances */
|
||||
static ARR_Instance sessions;
|
||||
static NKSN_Credentials server_credentials;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int handle_message(void *arg);
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
handle_client(int sock_fd, IPSockAddr *addr)
|
||||
{
|
||||
NKSN_Instance inst, *instp;
|
||||
int i;
|
||||
|
||||
/* Leave at least half of the descriptors which can handled by select()
|
||||
to other use */
|
||||
if (sock_fd > FD_SETSIZE / 2) {
|
||||
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||
UTI_IPSockAddrToString(addr), "too many descriptors");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Find an unused server slot or one with an already stopped session */
|
||||
for (i = 0, inst = NULL; i < ARR_GetSize(sessions); i++) {
|
||||
instp = ARR_GetElement(sessions, i);
|
||||
if (!*instp) {
|
||||
/* NULL handler arg will be replaced with the session instance */
|
||||
inst = NKSN_CreateInstance(1, NULL, handle_message, NULL);
|
||||
*instp = inst;
|
||||
break;
|
||||
} else if (NKSN_IsStopped(*instp)) {
|
||||
inst = *instp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!inst) {
|
||||
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||
UTI_IPSockAddrToString(addr), "too many connections");
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(server_credentials);
|
||||
|
||||
if (!NKSN_StartSession(inst, sock_fd, UTI_IPSockAddrToString(addr),
|
||||
server_credentials, SERVER_TIMEOUT))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
handle_helper_request(int fd, int event, void *arg)
|
||||
{
|
||||
SCK_Message *message;
|
||||
HelperRequest *req;
|
||||
IPSockAddr client_addr;
|
||||
int sock_fd;
|
||||
|
||||
/* Receive the helper request with the NTS-KE session socket.
|
||||
With multiple helpers EAGAIN errors are expected here. */
|
||||
message = SCK_ReceiveMessage(fd, SCK_FLAG_MSG_DESCRIPTOR);
|
||||
if (!message)
|
||||
return;
|
||||
|
||||
sock_fd = message->descriptor;
|
||||
if (sock_fd < 0) {
|
||||
/* Message with no descriptor is a shutdown command */
|
||||
SCH_QuitProgram();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!initialised) {
|
||||
DEBUG_LOG("Uninitialised helper");
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message->length != sizeof (HelperRequest))
|
||||
LOG_FATAL("Invalid helper request");
|
||||
|
||||
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));
|
||||
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");
|
||||
|
||||
if (!handle_client(sock_fd, &client_addr)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Accepted helper request fd=%d", sock_fd);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
accept_connection(int listening_fd, int event, void *arg)
|
||||
{
|
||||
SCK_Message message;
|
||||
IPSockAddr addr;
|
||||
int log_index, sock_fd;
|
||||
struct timespec now;
|
||||
|
||||
sock_fd = SCK_AcceptConnection(listening_fd, &addr);
|
||||
if (sock_fd < 0)
|
||||
return;
|
||||
|
||||
if (!NCR_CheckAccessRestriction(&addr.ip_addr)) {
|
||||
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||
UTI_IPSockAddrToString(&addr), "access denied");
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||
|
||||
log_index = CLG_LogServiceAccess(CLG_NTSKE, &addr.ip_addr, &now);
|
||||
if (log_index >= 0 && CLG_LimitServiceRate(CLG_NTSKE, log_index)) {
|
||||
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||
UTI_IPSockAddrToString(&addr), "rate limit");
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Pass the socket to a helper process if enabled. Otherwise, handle the
|
||||
client in the main process. */
|
||||
if (helper_sock_fd != INVALID_SOCK_FD) {
|
||||
HelperRequest req;
|
||||
|
||||
memset(&req, 0, sizeof (req));
|
||||
|
||||
/* Include the current server key and client address in the request */
|
||||
req.key_id = htonl(server_keys[current_server_key].id);
|
||||
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);
|
||||
req.client_port = htons(addr.port);
|
||||
|
||||
SCK_InitMessage(&message, SCK_ADDR_UNSPEC);
|
||||
message.data = &req;
|
||||
message.length = sizeof (req);
|
||||
message.descriptor = sock_fd;
|
||||
|
||||
errno = 0;
|
||||
if (!SCK_SendMessage(helper_sock_fd, &message, SCK_FLAG_MSG_DESCRIPTOR)) {
|
||||
/* If sending failed with EPIPE, it means all helpers closed their end of
|
||||
the socket (e.g. due to a fatal error) */
|
||||
if (errno == EPIPE)
|
||||
LOG_FATAL("NTS-KE helpers failed");
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
SCK_CloseSocket(sock_fd);
|
||||
} else {
|
||||
if (!handle_client(sock_fd, &addr)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_LOG("Accepted connection from %s fd=%d", UTI_IPSockAddrToString(&addr), sock_fd);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
open_socket(int family)
|
||||
{
|
||||
IPSockAddr local_addr;
|
||||
int backlog, sock_fd;
|
||||
char *iface;
|
||||
|
||||
if (!SCK_IsIpFamilyEnabled(family))
|
||||
return INVALID_SOCK_FD;
|
||||
|
||||
CNF_GetBindAddress(family, &local_addr.ip_addr);
|
||||
local_addr.port = CNF_GetNtsServerPort();
|
||||
iface = CNF_GetBindNtpInterface();
|
||||
|
||||
sock_fd = SCK_OpenTcpSocket(NULL, &local_addr, iface, 0);
|
||||
if (sock_fd < 0) {
|
||||
LOG(LOGS_ERR, "Could not open NTS-KE socket on %s", UTI_IPSockAddrToString(&local_addr));
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
/* Set the maximum number of waiting connections on the socket to the maximum
|
||||
number of concurrent sessions */
|
||||
backlog = MAX(CNF_GetNtsServerProcesses(), 1) * CNF_GetNtsServerConnections();
|
||||
|
||||
if (!SCK_ListenOnSocket(sock_fd, backlog)) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
return INVALID_SOCK_FD;
|
||||
}
|
||||
|
||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, accept_connection, NULL);
|
||||
|
||||
return sock_fd;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
helper_signal(int x)
|
||||
{
|
||||
SCH_QuitProgram();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
prepare_response(NKSN_Instance session, int error, int next_protocol, int aead_algorithm)
|
||||
{
|
||||
NKE_Context context;
|
||||
NKE_Cookie cookie;
|
||||
char *ntp_server;
|
||||
uint16_t datum;
|
||||
int i;
|
||||
|
||||
DEBUG_LOG("NTS KE response: error=%d next=%d aead=%d", error, next_protocol, aead_algorithm);
|
||||
|
||||
NKSN_BeginMessage(session);
|
||||
|
||||
if (error >= 0) {
|
||||
datum = htons(error);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_ERROR, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
} else if (next_protocol < 0) {
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, NULL, 0))
|
||||
return 0;
|
||||
} else if (aead_algorithm < 0) {
|
||||
datum = htons(next_protocol);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_AEAD_ALGORITHM, NULL, 0))
|
||||
return 0;
|
||||
} else {
|
||||
datum = htons(next_protocol);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NEXT_PROTOCOL, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
|
||||
datum = htons(aead_algorithm);
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_AEAD_ALGORITHM, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
|
||||
if (CNF_GetNTPPort() != NTP_PORT) {
|
||||
datum = htons(CNF_GetNTPPort());
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NTPV4_PORT_NEGOTIATION, &datum, sizeof (datum)))
|
||||
return 0;
|
||||
}
|
||||
|
||||
ntp_server = CNF_GetNtsNtpServer();
|
||||
if (ntp_server) {
|
||||
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NTPV4_SERVER_NEGOTIATION,
|
||||
ntp_server, strlen(ntp_server)))
|
||||
return 0;
|
||||
}
|
||||
|
||||
context.algorithm = aead_algorithm;
|
||||
|
||||
if (!NKSN_GetKeys(session, aead_algorithm, &context.c2s, &context.s2c))
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < NKE_MAX_COOKIES; i++) {
|
||||
if (!NKS_GenerateCookie(&context, &cookie))
|
||||
return 0;
|
||||
if (!NKSN_AddRecord(session, 0, NKE_RECORD_COOKIE, cookie.cookie, cookie.length))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!NKSN_EndMessage(session))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
process_request(NKSN_Instance session)
|
||||
{
|
||||
int next_protocol_records = 0, aead_algorithm_records = 0;
|
||||
int next_protocol_values = 0, aead_algorithm_values = 0;
|
||||
int next_protocol = -1, aead_algorithm = -1, error = -1;
|
||||
int i, critical, type, length;
|
||||
uint16_t data[NKE_MAX_RECORD_BODY_LENGTH / sizeof (uint16_t)];
|
||||
|
||||
assert(NKE_MAX_RECORD_BODY_LENGTH % sizeof (uint16_t) == 0);
|
||||
assert(sizeof (uint16_t) == 2);
|
||||
|
||||
while (error < 0) {
|
||||
if (!NKSN_GetRecord(session, &critical, &type, &length, &data, sizeof (data)))
|
||||
break;
|
||||
|
||||
switch (type) {
|
||||
case NKE_RECORD_NEXT_PROTOCOL:
|
||||
if (!critical || length < 2 || length % 2 != 0) {
|
||||
error = NKE_ERROR_BAD_REQUEST;
|
||||
break;
|
||||
}
|
||||
|
||||
next_protocol_records++;
|
||||
|
||||
for (i = 0; i < MIN(length, sizeof (data)) / 2; i++) {
|
||||
next_protocol_values++;
|
||||
if (ntohs(data[i]) == NKE_NEXT_PROTOCOL_NTPV4)
|
||||
next_protocol = NKE_NEXT_PROTOCOL_NTPV4;
|
||||
}
|
||||
break;
|
||||
case NKE_RECORD_AEAD_ALGORITHM:
|
||||
if (length < 2 || length % 2 != 0) {
|
||||
error = NKE_ERROR_BAD_REQUEST;
|
||||
break;
|
||||
}
|
||||
|
||||
aead_algorithm_records++;
|
||||
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case NKE_RECORD_ERROR:
|
||||
case NKE_RECORD_WARNING:
|
||||
case NKE_RECORD_COOKIE:
|
||||
error = NKE_ERROR_BAD_REQUEST;
|
||||
break;
|
||||
default:
|
||||
if (critical)
|
||||
error = NKE_ERROR_UNRECOGNIZED_CRITICAL_RECORD;
|
||||
}
|
||||
}
|
||||
|
||||
if (error < 0) {
|
||||
if (next_protocol_records != 1 || next_protocol_values < 1 ||
|
||||
(next_protocol == NKE_NEXT_PROTOCOL_NTPV4 &&
|
||||
(aead_algorithm_records != 1 || aead_algorithm_values < 1)))
|
||||
error = NKE_ERROR_BAD_REQUEST;
|
||||
}
|
||||
|
||||
if (!prepare_response(session, error, next_protocol, aead_algorithm))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
handle_message(void *arg)
|
||||
{
|
||||
NKSN_Instance session = arg;
|
||||
|
||||
return process_request(session);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
generate_key(int index)
|
||||
{
|
||||
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))
|
||||
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));
|
||||
|
||||
/* Encode the index in the lowest bits of the ID */
|
||||
server_keys[index].id &= -1U << KEY_ID_INDEX_BITS;
|
||||
server_keys[index].id |= index;
|
||||
|
||||
DEBUG_LOG("Generated server key %"PRIX32, server_keys[index].id);
|
||||
|
||||
last_server_key_ts = SCH_GetLastEventMonoTime();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
save_keys(void)
|
||||
{
|
||||
char buf[SIV_MAX_KEY_LENGTH * 2 + 1], *dump_dir;
|
||||
int i, index, key_length;
|
||||
double last_key_age;
|
||||
FILE *f;
|
||||
|
||||
/* Don't save the keys if rotation is disabled to enable an external
|
||||
management of the keys (e.g. share them with another server) */
|
||||
if (key_rotation_interval == 0)
|
||||
return;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
if (!dump_dir)
|
||||
return;
|
||||
|
||||
f = UTI_OpenFile(dump_dir, DUMP_FILENAME, ".tmp", 'w', 0600);
|
||||
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)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||
index = (current_server_key + i + 1 + FUTURE_KEYS) % MAX_SERVER_KEYS;
|
||||
|
||||
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)
|
||||
goto error;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
/* Rename the temporary file, or remove it if that fails */
|
||||
if (!UTI_RenameTempFile(dump_dir, DUMP_FILENAME, ".tmp", NULL)) {
|
||||
if (!UTI_RemoveFile(dump_dir, DUMP_FILENAME, ".tmp"))
|
||||
;
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s server keys", "save");
|
||||
fclose(f);
|
||||
|
||||
if (!UTI_RemoveFile(dump_dir, DUMP_FILENAME, NULL))
|
||||
;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define MAX_WORDS 2
|
||||
|
||||
static int
|
||||
load_keys(void)
|
||||
{
|
||||
char *dump_dir, line[1024], *words[MAX_WORDS];
|
||||
unsigned char key[SIV_MAX_KEY_LENGTH];
|
||||
int i, index, key_length, algorithm;
|
||||
double key_age;
|
||||
FILE *f;
|
||||
uint32_t id;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
if (!dump_dir)
|
||||
return 0;
|
||||
|
||||
f = UTI_OpenFile(dump_dir, DUMP_FILENAME, NULL, 'r', 0);
|
||||
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)
|
||||
goto error;
|
||||
|
||||
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||
last_server_key_ts = SCH_GetLastEventMonoTime() - MAX(key_age, 0.0);
|
||||
|
||||
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)
|
||||
goto error;
|
||||
|
||||
if (UTI_HexToBytes(words[1], key, sizeof (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;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s server keys", "load");
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
key_timeout(void *arg)
|
||||
{
|
||||
current_server_key = (current_server_key + 1) % MAX_SERVER_KEYS;
|
||||
generate_key((current_server_key + FUTURE_KEYS) % MAX_SERVER_KEYS);
|
||||
save_keys();
|
||||
|
||||
SCH_AddTimeoutByDelay(key_rotation_interval, key_timeout, NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
run_helper(uid_t uid, gid_t gid, int scfilter_level)
|
||||
{
|
||||
LOG_Severity log_severity;
|
||||
|
||||
/* Finish minimal initialisation and run using the scheduler loop
|
||||
similarly to the main process */
|
||||
|
||||
DEBUG_LOG("Helper started");
|
||||
|
||||
/* Suppress a log message about disabled clock control */
|
||||
log_severity = LOG_GetMinSeverity();
|
||||
LOG_SetMinSeverity(LOGS_ERR);
|
||||
|
||||
SYS_Initialise(0);
|
||||
LOG_SetMinSeverity(log_severity);
|
||||
|
||||
if (!geteuid() && (uid || gid))
|
||||
SYS_DropRoot(uid, gid, SYS_NTSKE_HELPER);
|
||||
|
||||
NKS_Initialise();
|
||||
|
||||
UTI_SetQuitSignalsHandler(helper_signal, 1);
|
||||
if (scfilter_level != 0)
|
||||
SYS_EnableSystemCallFilter(scfilter_level, SYS_NTSKE_HELPER);
|
||||
|
||||
SCH_MainLoop();
|
||||
|
||||
DEBUG_LOG("Helper exiting");
|
||||
|
||||
NKS_Finalise();
|
||||
SCK_Finalise();
|
||||
SYS_Finalise();
|
||||
SCH_Finalise();
|
||||
LCL_Finalise();
|
||||
PRV_Finalise();
|
||||
CNF_Finalise();
|
||||
LOG_Finalise();
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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_GetNtsServerCertAndKeyFiles(&certs, &keys) <= 0)
|
||||
return;
|
||||
|
||||
processes = CNF_GetNtsServerProcesses();
|
||||
if (processes <= 0)
|
||||
return;
|
||||
|
||||
/* Start helper processes to perform (computationally expensive) NTS-KE
|
||||
sessions with clients on sockets forwarded from the main process */
|
||||
|
||||
sock_fd1 = SCK_OpenUnixSocketPair(0, &sock_fd2);
|
||||
if (sock_fd1 < 0)
|
||||
LOG_FATAL("Could not open socket pair");
|
||||
|
||||
for (i = 0; i < processes; i++) {
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0)
|
||||
LOG_FATAL("fork() failed : %s", strerror(errno));
|
||||
|
||||
if (pid > 0)
|
||||
continue;
|
||||
|
||||
is_helper = 1;
|
||||
|
||||
snprintf(prefix, sizeof (prefix), "nks#%d:", i + 1);
|
||||
LOG_SetDebugPrefix(prefix);
|
||||
LOG_CloseParentFd();
|
||||
|
||||
SCK_CloseSocket(sock_fd1);
|
||||
SCH_AddFileHandler(sock_fd2, SCH_FILE_INPUT, handle_helper_request, NULL);
|
||||
|
||||
run_helper(uid, gid, scfilter_level);
|
||||
}
|
||||
|
||||
SCK_CloseSocket(sock_fd2);
|
||||
helper_sock_fd = sock_fd1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKS_Initialise(void)
|
||||
{
|
||||
const char **certs, **keys;
|
||||
int i, n_certs_keys;
|
||||
double key_delay;
|
||||
|
||||
server_sock_fd4 = INVALID_SOCK_FD;
|
||||
server_sock_fd6 = INVALID_SOCK_FD;
|
||||
|
||||
n_certs_keys = CNF_GetNtsServerCertAndKeyFiles(&certs, &keys);
|
||||
if (n_certs_keys <= 0)
|
||||
return;
|
||||
|
||||
if (helper_sock_fd == INVALID_SOCK_FD) {
|
||||
server_credentials = NKSN_CreateServerCertCredentials(certs, keys, n_certs_keys);
|
||||
if (!server_credentials)
|
||||
return;
|
||||
} else {
|
||||
server_credentials = NULL;
|
||||
}
|
||||
|
||||
sessions = ARR_CreateInstance(sizeof (NKSN_Instance));
|
||||
for (i = 0; i < CNF_GetNtsServerConnections(); i++)
|
||||
*(NKSN_Instance *)ARR_GetNewElement(sessions) = NULL;
|
||||
|
||||
/* 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);
|
||||
generate_key(i);
|
||||
}
|
||||
|
||||
current_server_key = MAX_SERVER_KEYS - 1;
|
||||
|
||||
if (!is_helper) {
|
||||
server_sock_fd4 = open_socket(IPADDR_INET4);
|
||||
server_sock_fd6 = open_socket(IPADDR_INET6);
|
||||
|
||||
key_rotation_interval = MAX(CNF_GetNtsRotate(), 0);
|
||||
|
||||
/* Reload saved keys, or save the new keys */
|
||||
if (!load_keys())
|
||||
save_keys();
|
||||
|
||||
if (key_rotation_interval > 0) {
|
||||
key_delay = key_rotation_interval - (SCH_GetLastEventMonoTime() - last_server_key_ts);
|
||||
SCH_AddTimeoutByDelay(MAX(key_delay, 0.0), key_timeout, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
initialised = 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKS_Finalise(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!initialised)
|
||||
return;
|
||||
|
||||
if (helper_sock_fd != INVALID_SOCK_FD) {
|
||||
/* Send the helpers a request to exit */
|
||||
for (i = 0; i < CNF_GetNtsServerProcesses(); i++) {
|
||||
if (!SCK_Send(helper_sock_fd, "", 1, 0))
|
||||
;
|
||||
}
|
||||
SCK_CloseSocket(helper_sock_fd);
|
||||
}
|
||||
if (server_sock_fd4 != INVALID_SOCK_FD)
|
||||
SCK_CloseSocket(server_sock_fd4);
|
||||
if (server_sock_fd6 != INVALID_SOCK_FD)
|
||||
SCK_CloseSocket(server_sock_fd6);
|
||||
|
||||
if (!is_helper)
|
||||
save_keys();
|
||||
|
||||
for (i = 0; i < MAX_SERVER_KEYS; i++)
|
||||
SIV_DestroyInstance(server_keys[i].siv);
|
||||
|
||||
for (i = 0; i < ARR_GetSize(sessions); i++) {
|
||||
NKSN_Instance session = *(NKSN_Instance *)ARR_GetElement(sessions, i);
|
||||
if (session)
|
||||
NKSN_DestroyInstance(session);
|
||||
}
|
||||
ARR_DestroyInstance(sessions);
|
||||
|
||||
if (server_credentials)
|
||||
NKSN_DestroyCertCredentials(server_credentials);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKS_DumpKeys(void)
|
||||
{
|
||||
save_keys();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKS_ReloadKeys(void)
|
||||
{
|
||||
/* Don't load the keys if they are expected to be generated by this server
|
||||
instance (i.e. they are already loaded) to not delay the next rotation */
|
||||
if (key_rotation_interval > 0)
|
||||
return;
|
||||
|
||||
load_keys();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
/* A server cookie consists of key ID, nonce, and encrypted C2S+S2C keys */
|
||||
|
||||
int
|
||||
NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
||||
{
|
||||
unsigned char plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
int plaintext_length, tag_length;
|
||||
ServerCookieHeader *header;
|
||||
ServerKey *key;
|
||||
|
||||
if (!initialised) {
|
||||
DEBUG_LOG("NTS server disabled");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The algorithm is hardcoded for now */
|
||||
if (context->algorithm != AEAD_AES_SIV_CMAC_256) {
|
||||
DEBUG_LOG("Unexpected SIV algorithm");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (context->c2s.length < 0 || context->c2s.length > NKE_MAX_KEY_LENGTH ||
|
||||
context->s2c.length < 0 || context->s2c.length > NKE_MAX_KEY_LENGTH) {
|
||||
DEBUG_LOG("Invalid key length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
key = &server_keys[current_server_key];
|
||||
|
||||
header = (ServerCookieHeader *)cookie->cookie;
|
||||
|
||||
header->key_id = htonl(key->id);
|
||||
UTI_GetRandomBytes(header->nonce, sizeof (header->nonce));
|
||||
|
||||
plaintext_length = context->c2s.length + context->s2c.length;
|
||||
assert(plaintext_length <= sizeof (plaintext));
|
||||
memcpy(plaintext, context->c2s.key, context->c2s.length);
|
||||
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;
|
||||
assert(cookie->length <= sizeof (cookie->cookie));
|
||||
ciphertext = cookie->cookie + sizeof (*header);
|
||||
|
||||
if (!SIV_Encrypt(key->siv, header->nonce, sizeof (header->nonce),
|
||||
"", 0,
|
||||
plaintext, plaintext_length,
|
||||
ciphertext, plaintext_length + tag_length)) {
|
||||
DEBUG_LOG("Could not encrypt cookie");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context)
|
||||
{
|
||||
unsigned char plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||
int ciphertext_length, plaintext_length, tag_length;
|
||||
ServerCookieHeader *header;
|
||||
ServerKey *key;
|
||||
uint32_t key_id;
|
||||
|
||||
if (!initialised) {
|
||||
DEBUG_LOG("NTS server disabled");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cookie->length <= (int)sizeof (*header)) {
|
||||
DEBUG_LOG("Invalid cookie length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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];
|
||||
if (key_id != key->id) {
|
||||
DEBUG_LOG("Unknown key %"PRIX32, key_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
tag_length = SIV_GetTagLength(key->siv);
|
||||
if (tag_length >= ciphertext_length) {
|
||||
DEBUG_LOG("Invalid cookie length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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),
|
||||
"", 0,
|
||||
ciphertext, ciphertext_length,
|
||||
plaintext, plaintext_length)) {
|
||||
DEBUG_LOG("Could not decrypt cookie");
|
||||
return 0;
|
||||
}
|
||||
|
||||
context->algorithm = AEAD_AES_SIV_CMAC_256;
|
||||
|
||||
context->c2s.length = plaintext_length / 2;
|
||||
context->s2c.length = plaintext_length / 2;
|
||||
assert(context->c2s.length <= sizeof (context->c2s.key));
|
||||
|
||||
memcpy(context->c2s.key, plaintext, context->c2s.length);
|
||||
memcpy(context->s2c.key, plaintext + context->c2s.length, context->s2c.length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
49
nts_ke_server.h
Normal file
49
nts_ke_server.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 the NTS-KE server
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_KE_SERVER_H
|
||||
#define GOT_NTS_KE_SERVER_H
|
||||
|
||||
#include "nts_ke.h"
|
||||
|
||||
/* Init and fini functions */
|
||||
extern void NKS_PreInitialise(uid_t uid, gid_t gid, int scfilter_level);
|
||||
extern void NKS_Initialise(void);
|
||||
extern void NKS_Finalise(void);
|
||||
|
||||
/* Save the current server keys */
|
||||
extern void NKS_DumpKeys(void);
|
||||
|
||||
/* Reload the keys */
|
||||
extern void NKS_ReloadKeys(void);
|
||||
|
||||
/* Generate an NTS cookie with a given context */
|
||||
extern int NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie);
|
||||
|
||||
/* Validate a cookie and decode the context */
|
||||
extern int NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Context *context);
|
||||
|
||||
#endif
|
||||
920
nts_ke_session.c
Normal file
920
nts_ke_session.c
Normal file
@@ -0,0 +1,920 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
NTS-KE session used by server and client
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ke_session.h"
|
||||
|
||||
#include "conf.h"
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "siv.h"
|
||||
#include "socket.h"
|
||||
#include "sched.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <gnutls/gnutls.h>
|
||||
#include <gnutls/x509.h>
|
||||
|
||||
#define INVALID_SOCK_FD (-8)
|
||||
|
||||
struct RecordHeader {
|
||||
uint16_t type;
|
||||
uint16_t body_length;
|
||||
};
|
||||
|
||||
struct Message {
|
||||
int length;
|
||||
int sent;
|
||||
int parsed;
|
||||
int complete;
|
||||
unsigned char data[NKE_MAX_MESSAGE_LENGTH];
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
KE_WAIT_CONNECT,
|
||||
KE_HANDSHAKE,
|
||||
KE_SEND,
|
||||
KE_RECEIVE,
|
||||
KE_SHUTDOWN,
|
||||
KE_STOPPED,
|
||||
} KeState;
|
||||
|
||||
struct NKSN_Instance_Record {
|
||||
int server;
|
||||
char *server_name;
|
||||
NKSN_MessageHandler handler;
|
||||
void *handler_arg;
|
||||
|
||||
KeState state;
|
||||
int sock_fd;
|
||||
char *label;
|
||||
gnutls_session_t tls_session;
|
||||
SCH_TimeoutID timeout_id;
|
||||
int retry_factor;
|
||||
|
||||
struct Message message;
|
||||
int new_message;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static gnutls_priority_t priority_cache;
|
||||
|
||||
static int credentials_counter = 0;
|
||||
|
||||
static int clock_updates = 0;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
reset_message(struct Message *message)
|
||||
{
|
||||
message->length = 0;
|
||||
message->sent = 0;
|
||||
message->parsed = 0;
|
||||
message->complete = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
add_record(struct Message *message, int critical, int type, const void *body, int body_length)
|
||||
{
|
||||
struct RecordHeader header;
|
||||
|
||||
assert(message->length <= sizeof (message->data));
|
||||
|
||||
if (body_length < 0 || body_length > 0xffff || type < 0 || type > 0x7fff ||
|
||||
message->length + sizeof (header) + body_length > sizeof (message->data))
|
||||
return 0;
|
||||
|
||||
header.type = htons(!!critical * NKE_RECORD_CRITICAL_BIT | type);
|
||||
header.body_length = htons(body_length);
|
||||
|
||||
memcpy(&message->data[message->length], &header, sizeof (header));
|
||||
message->length += sizeof (header);
|
||||
|
||||
if (body_length > 0) {
|
||||
memcpy(&message->data[message->length], body, body_length);
|
||||
message->length += body_length;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
reset_message_parsing(struct Message *message)
|
||||
{
|
||||
message->parsed = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_record(struct Message *message, int *critical, int *type, int *body_length,
|
||||
void *body, int buffer_length)
|
||||
{
|
||||
struct RecordHeader header;
|
||||
int blen, rlen;
|
||||
|
||||
if (message->length < message->parsed + sizeof (header) ||
|
||||
buffer_length < 0)
|
||||
return 0;
|
||||
|
||||
memcpy(&header, &message->data[message->parsed], sizeof (header));
|
||||
|
||||
blen = ntohs(header.body_length);
|
||||
rlen = sizeof (header) + blen;
|
||||
assert(blen >= 0 && rlen > 0);
|
||||
|
||||
if (message->length < message->parsed + rlen)
|
||||
return 0;
|
||||
|
||||
if (critical)
|
||||
*critical = !!(ntohs(header.type) & NKE_RECORD_CRITICAL_BIT);
|
||||
if (type)
|
||||
*type = ntohs(header.type) & ~NKE_RECORD_CRITICAL_BIT;
|
||||
if (body)
|
||||
memcpy(body, &message->data[message->parsed + sizeof (header)], MIN(buffer_length, blen));
|
||||
if (body_length)
|
||||
*body_length = blen;
|
||||
|
||||
message->parsed += rlen;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_message_format(struct Message *message, int eof)
|
||||
{
|
||||
int critical = 0, type = -1, length = -1, ends = 0;
|
||||
|
||||
reset_message_parsing(message);
|
||||
message->complete = 0;
|
||||
|
||||
while (get_record(message, &critical, &type, &length, NULL, 0)) {
|
||||
if (type == NKE_RECORD_END_OF_MESSAGE) {
|
||||
if (!critical || length != 0 || ends > 0)
|
||||
return 0;
|
||||
ends++;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the message cannot be fully parsed, but more data may be coming,
|
||||
consider the format to be ok */
|
||||
if (message->length == 0 || message->parsed < message->length)
|
||||
return !eof;
|
||||
|
||||
if (type != NKE_RECORD_END_OF_MESSAGE)
|
||||
return !eof;
|
||||
|
||||
message->complete = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static gnutls_session_t
|
||||
create_tls_session(int server_mode, int sock_fd, const char *server_name,
|
||||
gnutls_certificate_credentials_t credentials,
|
||||
gnutls_priority_t priority)
|
||||
{
|
||||
unsigned char alpn_name[sizeof (NKE_ALPN_NAME)];
|
||||
gnutls_session_t session;
|
||||
gnutls_datum_t alpn;
|
||||
unsigned int flags;
|
||||
int r;
|
||||
|
||||
r = gnutls_init(&session, GNUTLS_NONBLOCK | GNUTLS_NO_TICKETS |
|
||||
(server_mode ? GNUTLS_SERVER : GNUTLS_CLIENT));
|
||||
if (r < 0) {
|
||||
LOG(LOGS_ERR, "Could not %s TLS session : %s", "create", gnutls_strerror(r));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!server_mode) {
|
||||
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;
|
||||
|
||||
if (clock_updates < CNF_GetNoCertTimeCheck()) {
|
||||
flags |= GNUTLS_VERIFY_DISABLE_TIME_CHECKS | GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS;
|
||||
DEBUG_LOG("Disabled time checks");
|
||||
}
|
||||
|
||||
gnutls_session_set_verify_cert(session, server_name, flags);
|
||||
}
|
||||
|
||||
r = gnutls_priority_set(session, priority);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
r = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, credentials);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
memcpy(alpn_name, NKE_ALPN_NAME, sizeof (alpn_name));
|
||||
alpn.data = alpn_name;
|
||||
alpn.size = sizeof (alpn_name) - 1;
|
||||
|
||||
r = gnutls_alpn_set_protocols(session, &alpn, 1, 0);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
gnutls_transport_set_int(session, sock_fd);
|
||||
|
||||
return session;
|
||||
|
||||
error:
|
||||
LOG(LOGS_ERR, "Could not %s TLS session : %s", "set", gnutls_strerror(r));
|
||||
gnutls_deinit(session);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
stop_session(NKSN_Instance inst)
|
||||
{
|
||||
if (inst->state == KE_STOPPED)
|
||||
return;
|
||||
|
||||
inst->state = KE_STOPPED;
|
||||
|
||||
SCH_RemoveFileHandler(inst->sock_fd);
|
||||
SCK_CloseSocket(inst->sock_fd);
|
||||
inst->sock_fd = INVALID_SOCK_FD;
|
||||
|
||||
Free(inst->label);
|
||||
inst->label = NULL;
|
||||
|
||||
gnutls_deinit(inst->tls_session);
|
||||
inst->tls_session = NULL;
|
||||
|
||||
SCH_RemoveTimeout(inst->timeout_id);
|
||||
inst->timeout_id = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
session_timeout(void *arg)
|
||||
{
|
||||
NKSN_Instance inst = arg;
|
||||
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR, "NTS-KE session with %s timed out", inst->label);
|
||||
|
||||
inst->timeout_id = 0;
|
||||
stop_session(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_alpn(NKSN_Instance inst)
|
||||
{
|
||||
gnutls_datum_t alpn;
|
||||
|
||||
if (gnutls_alpn_get_selected_protocol(inst->tls_session, &alpn) < 0 ||
|
||||
alpn.size != sizeof (NKE_ALPN_NAME) - 1 ||
|
||||
memcmp(alpn.data, NKE_ALPN_NAME, sizeof (NKE_ALPN_NAME) - 1) != 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
set_input_output(NKSN_Instance inst, int output)
|
||||
{
|
||||
SCH_SetFileHandlerEvent(inst->sock_fd, SCH_FILE_INPUT, !output);
|
||||
SCH_SetFileHandlerEvent(inst->sock_fd, SCH_FILE_OUTPUT, output);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
change_state(NKSN_Instance inst, KeState state)
|
||||
{
|
||||
int output;
|
||||
|
||||
switch (state) {
|
||||
case KE_HANDSHAKE:
|
||||
output = !inst->server;
|
||||
break;
|
||||
case KE_WAIT_CONNECT:
|
||||
case KE_SEND:
|
||||
case KE_SHUTDOWN:
|
||||
output = 1;
|
||||
break;
|
||||
case KE_RECEIVE:
|
||||
output = 0;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
set_input_output(inst, output);
|
||||
|
||||
inst->state = state;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
handle_event(NKSN_Instance inst, int event)
|
||||
{
|
||||
struct Message *message = &inst->message;
|
||||
int r;
|
||||
|
||||
DEBUG_LOG("Session event %d fd=%d state=%d", event, inst->sock_fd, (int)inst->state);
|
||||
|
||||
switch (inst->state) {
|
||||
case KE_WAIT_CONNECT:
|
||||
/* Check if connect() succeeded */
|
||||
if (event != SCH_FILE_OUTPUT)
|
||||
return 0;
|
||||
|
||||
/* Get the socket error */
|
||||
if (!SCK_GetIntOption(inst->sock_fd, SOL_SOCKET, SO_ERROR, &r))
|
||||
r = EINVAL;
|
||||
|
||||
if (r != 0) {
|
||||
LOG(LOGS_ERR, "Could not connect to %s : %s", inst->label, strerror(r));
|
||||
stop_session(inst);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Connected to %s", inst->label);
|
||||
|
||||
change_state(inst, KE_HANDSHAKE);
|
||||
return 0;
|
||||
|
||||
case KE_HANDSHAKE:
|
||||
r = gnutls_handshake(inst->tls_session);
|
||||
|
||||
if (r < 0) {
|
||||
if (gnutls_error_is_fatal(r)) {
|
||||
gnutls_datum_t cert_error;
|
||||
|
||||
/* Get a description of verification errors */
|
||||
if (r != GNUTLS_E_CERTIFICATE_VERIFICATION_ERROR ||
|
||||
gnutls_certificate_verification_status_print(
|
||||
gnutls_session_get_verify_cert_status(inst->tls_session),
|
||||
gnutls_certificate_type_get(inst->tls_session), &cert_error, 0) < 0)
|
||||
cert_error.data = NULL;
|
||||
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR,
|
||||
"TLS handshake with %s failed : %s%s%s", inst->label, gnutls_strerror(r),
|
||||
cert_error.data ? " " : "", cert_error.data ? (const char *)cert_error.data : "");
|
||||
|
||||
if (cert_error.data)
|
||||
gnutls_free(cert_error.data);
|
||||
|
||||
stop_session(inst);
|
||||
|
||||
/* Increase the retry interval if the handshake did not fail due
|
||||
to the other end closing the connection */
|
||||
if (r != GNUTLS_E_PULL_ERROR && r != GNUTLS_E_PREMATURE_TERMINATION)
|
||||
inst->retry_factor = NKE_RETRY_FACTOR2_TLS;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Disable output when the handshake is trying to receive data */
|
||||
set_input_output(inst, gnutls_record_get_direction(inst->tls_session));
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->retry_factor = NKE_RETRY_FACTOR2_TLS;
|
||||
|
||||
if (DEBUG) {
|
||||
char *description = gnutls_session_get_desc(inst->tls_session);
|
||||
DEBUG_LOG("Handshake with %s completed %s",
|
||||
inst->label, description ? description : "");
|
||||
gnutls_free(description);
|
||||
}
|
||||
|
||||
if (!check_alpn(inst)) {
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR, "NTS-KE not supported by %s", inst->label);
|
||||
stop_session(inst);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Client will send a request to the server */
|
||||
change_state(inst, inst->server ? KE_RECEIVE : KE_SEND);
|
||||
return 0;
|
||||
|
||||
case KE_SEND:
|
||||
assert(inst->new_message && message->complete);
|
||||
assert(message->length <= sizeof (message->data) && message->length > message->sent);
|
||||
|
||||
r = gnutls_record_send(inst->tls_session, &message->data[message->sent],
|
||||
message->length - message->sent);
|
||||
|
||||
if (r < 0) {
|
||||
if (gnutls_error_is_fatal(r)) {
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR,
|
||||
"Could not send NTS-KE message to %s : %s", inst->label, gnutls_strerror(r));
|
||||
stop_session(inst);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Sent %d bytes to %s", r, inst->label);
|
||||
|
||||
message->sent += r;
|
||||
if (message->sent < message->length)
|
||||
return 0;
|
||||
|
||||
/* Client will receive a response */
|
||||
change_state(inst, inst->server ? KE_SHUTDOWN : KE_RECEIVE);
|
||||
reset_message(&inst->message);
|
||||
inst->new_message = 0;
|
||||
return 0;
|
||||
|
||||
case KE_RECEIVE:
|
||||
do {
|
||||
if (message->length >= sizeof (message->data)) {
|
||||
DEBUG_LOG("Message is too long");
|
||||
stop_session(inst);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = gnutls_record_recv(inst->tls_session, &message->data[message->length],
|
||||
sizeof (message->data) - message->length);
|
||||
|
||||
if (r < 0) {
|
||||
/* Handle a renegotiation request on both client and server as
|
||||
a protocol error */
|
||||
if (gnutls_error_is_fatal(r) || r == GNUTLS_E_REHANDSHAKE) {
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR,
|
||||
"Could not receive NTS-KE message from %s : %s",
|
||||
inst->label, gnutls_strerror(r));
|
||||
stop_session(inst);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Received %d bytes from %s", r, inst->label);
|
||||
|
||||
message->length += r;
|
||||
|
||||
} while (gnutls_record_check_pending(inst->tls_session) > 0);
|
||||
|
||||
if (!check_message_format(message, r == 0)) {
|
||||
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR,
|
||||
"Received invalid NTS-KE message from %s", inst->label);
|
||||
stop_session(inst);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Wait for more data if the message is not complete yet */
|
||||
if (!message->complete)
|
||||
return 0;
|
||||
|
||||
/* Server will send a response to the client */
|
||||
change_state(inst, inst->server ? KE_SEND : KE_SHUTDOWN);
|
||||
|
||||
/* Return success to process the received message */
|
||||
return 1;
|
||||
|
||||
case KE_SHUTDOWN:
|
||||
r = gnutls_bye(inst->tls_session, GNUTLS_SHUT_RDWR);
|
||||
|
||||
if (r < 0) {
|
||||
if (gnutls_error_is_fatal(r)) {
|
||||
DEBUG_LOG("Shutdown with %s failed : %s", inst->label, gnutls_strerror(r));
|
||||
stop_session(inst);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Disable output when the TLS shutdown is trying to receive data */
|
||||
set_input_output(inst, gnutls_record_get_direction(inst->tls_session));
|
||||
return 0;
|
||||
}
|
||||
|
||||
SCK_ShutdownConnection(inst->sock_fd);
|
||||
stop_session(inst);
|
||||
|
||||
DEBUG_LOG("Shutdown completed");
|
||||
return 0;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
read_write_socket(int fd, int event, void *arg)
|
||||
{
|
||||
NKSN_Instance inst = arg;
|
||||
|
||||
if (!handle_event(inst, event))
|
||||
return;
|
||||
|
||||
/* A valid message was received. Call the handler to process the message,
|
||||
and prepare a response if it is a server. */
|
||||
|
||||
reset_message_parsing(&inst->message);
|
||||
|
||||
if (!(inst->handler)(inst->handler_arg)) {
|
||||
stop_session(inst);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static time_t
|
||||
get_time(time_t *t)
|
||||
{
|
||||
struct timespec now;
|
||||
|
||||
LCL_ReadCookedTime(&now, NULL);
|
||||
if (t)
|
||||
*t = now.tv_sec;
|
||||
|
||||
return now.tv_sec;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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 && clock_updates < INT_MAX)
|
||||
clock_updates++;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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));
|
||||
|
||||
/* Prepare a priority cache for server and client NTS-KE sessions
|
||||
(the NTS specification requires TLS1.3 or later) */
|
||||
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));
|
||||
|
||||
/* Use our clock instead of the system clock in certificate verification */
|
||||
gnutls_global_set_time_function(get_time);
|
||||
|
||||
gnutls_initialised = 1;
|
||||
DEBUG_LOG("Initialised");
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_step, NULL);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
deinit_gnutls(void)
|
||||
{
|
||||
if (!gnutls_initialised || credentials_counter > 0)
|
||||
return;
|
||||
|
||||
LCL_RemoveParameterChangeHandler(handle_step, NULL);
|
||||
|
||||
gnutls_priority_deinit(priority_cache);
|
||||
gnutls_global_deinit();
|
||||
gnutls_initialised = 0;
|
||||
DEBUG_LOG("Deinitialised");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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 i, r;
|
||||
|
||||
init_gnutls();
|
||||
|
||||
r = gnutls_certificate_allocate_credentials(&credentials);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
if (certs && keys) {
|
||||
if (trusted_certs || trusted_certs_ids)
|
||||
assert(0);
|
||||
|
||||
for (i = 0; i < n_certs_keys; i++) {
|
||||
r = gnutls_certificate_set_x509_key_file(credentials, certs[i], keys[i],
|
||||
GNUTLS_X509_FMT_PEM);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
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 && 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 (NKSN_Credentials)credentials;
|
||||
|
||||
error:
|
||||
LOG(LOGS_ERR, "Could not set credentials : %s", gnutls_strerror(r));
|
||||
if (credentials)
|
||||
gnutls_certificate_free_credentials(credentials);
|
||||
deinit_gnutls();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NKSN_Credentials
|
||||
NKSN_CreateServerCertCredentials(const char **certs, const char **keys, int n_certs_keys)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NKSN_Instance
|
||||
NKSN_CreateInstance(int server_mode, const char *server_name,
|
||||
NKSN_MessageHandler handler, void *handler_arg)
|
||||
{
|
||||
NKSN_Instance inst;
|
||||
|
||||
inst = MallocNew(struct NKSN_Instance_Record);
|
||||
|
||||
inst->server = server_mode;
|
||||
inst->server_name = server_name ? Strdup(server_name) : NULL;
|
||||
inst->handler = handler;
|
||||
inst->handler_arg = handler_arg;
|
||||
/* Replace a NULL argument with the session itself */
|
||||
if (!inst->handler_arg)
|
||||
inst->handler_arg = inst;
|
||||
|
||||
inst->state = KE_STOPPED;
|
||||
inst->sock_fd = INVALID_SOCK_FD;
|
||||
inst->label = NULL;
|
||||
inst->tls_session = NULL;
|
||||
inst->timeout_id = 0;
|
||||
inst->retry_factor = NKE_RETRY_FACTOR2_CONNECT;
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKSN_DestroyInstance(NKSN_Instance inst)
|
||||
{
|
||||
stop_session(inst);
|
||||
|
||||
Free(inst->server_name);
|
||||
Free(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_StartSession(NKSN_Instance inst, int sock_fd, const char *label,
|
||||
NKSN_Credentials credentials, double timeout)
|
||||
{
|
||||
assert(inst->state == KE_STOPPED);
|
||||
|
||||
inst->tls_session = create_tls_session(inst->server, sock_fd, inst->server_name,
|
||||
(gnutls_certificate_credentials_t)credentials,
|
||||
priority_cache);
|
||||
if (!inst->tls_session)
|
||||
return 0;
|
||||
|
||||
inst->sock_fd = sock_fd;
|
||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_write_socket, inst);
|
||||
|
||||
inst->label = Strdup(label);
|
||||
inst->timeout_id = SCH_AddTimeoutByDelay(timeout, session_timeout, inst);
|
||||
inst->retry_factor = NKE_RETRY_FACTOR2_CONNECT;
|
||||
|
||||
reset_message(&inst->message);
|
||||
inst->new_message = 0;
|
||||
|
||||
change_state(inst, inst->server ? KE_HANDSHAKE : KE_WAIT_CONNECT);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKSN_BeginMessage(NKSN_Instance inst)
|
||||
{
|
||||
reset_message(&inst->message);
|
||||
inst->new_message = 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_AddRecord(NKSN_Instance inst, int critical, int type, const void *body, int body_length)
|
||||
{
|
||||
assert(inst->new_message && !inst->message.complete);
|
||||
assert(type != NKE_RECORD_END_OF_MESSAGE);
|
||||
|
||||
return add_record(&inst->message, critical, type, body, body_length);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_EndMessage(NKSN_Instance inst)
|
||||
{
|
||||
assert(!inst->message.complete);
|
||||
|
||||
/* Terminate the message */
|
||||
if (!add_record(&inst->message, 1, NKE_RECORD_END_OF_MESSAGE, NULL, 0))
|
||||
return 0;
|
||||
|
||||
inst->message.complete = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_GetRecord(NKSN_Instance inst, int *critical, int *type, int *body_length,
|
||||
void *body, int buffer_length)
|
||||
{
|
||||
int type2;
|
||||
|
||||
assert(inst->message.complete);
|
||||
|
||||
if (body_length)
|
||||
*body_length = 0;
|
||||
|
||||
if (!get_record(&inst->message, critical, &type2, body_length, body, buffer_length))
|
||||
return 0;
|
||||
|
||||
/* Hide the end-of-message record */
|
||||
if (type2 == NKE_RECORD_END_OF_MESSAGE)
|
||||
return 0;
|
||||
|
||||
if (type)
|
||||
*type = type2;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_GetKeys(NKSN_Instance inst, SIV_Algorithm siv, NKE_Key *c2s, NKE_Key *s2c)
|
||||
{
|
||||
int length = SIV_GetKeyLength(siv);
|
||||
|
||||
if (length <= 0 || length > sizeof (c2s->key) || length > sizeof (s2c->key)) {
|
||||
DEBUG_LOG("Invalid algorithm");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gnutls_prf_rfc5705(inst->tls_session,
|
||||
sizeof (NKE_EXPORTER_LABEL) - 1, NKE_EXPORTER_LABEL,
|
||||
sizeof (NKE_EXPORTER_CONTEXT_C2S) - 1, NKE_EXPORTER_CONTEXT_C2S,
|
||||
length, (char *)c2s->key) < 0 ||
|
||||
gnutls_prf_rfc5705(inst->tls_session,
|
||||
sizeof (NKE_EXPORTER_LABEL) - 1, NKE_EXPORTER_LABEL,
|
||||
sizeof (NKE_EXPORTER_CONTEXT_S2C) - 1, NKE_EXPORTER_CONTEXT_S2C,
|
||||
length, (char *)s2c->key) < 0) {
|
||||
DEBUG_LOG("Could not export key");
|
||||
return 0;
|
||||
}
|
||||
|
||||
c2s->length = length;
|
||||
s2c->length = length;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_IsStopped(NKSN_Instance inst)
|
||||
{
|
||||
return inst->state == KE_STOPPED;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NKSN_StopSession(NKSN_Instance inst)
|
||||
{
|
||||
stop_session(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NKSN_GetRetryFactor(NKSN_Instance inst)
|
||||
{
|
||||
return inst->retry_factor;
|
||||
}
|
||||
93
nts_ke_session.h
Normal file
93
nts_ke_session.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 the NTS-KE session
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_KE_SESSION_H
|
||||
#define GOT_NTS_KE_SESSION_H
|
||||
|
||||
#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 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 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(NKSN_Credentials credentials);
|
||||
|
||||
/* Create an instance */
|
||||
extern NKSN_Instance NKSN_CreateInstance(int server_mode, const char *server_name,
|
||||
NKSN_MessageHandler handler, void *handler_arg);
|
||||
|
||||
/* Destroy an instance */
|
||||
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,
|
||||
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. */
|
||||
extern void NKSN_BeginMessage(NKSN_Instance inst);
|
||||
|
||||
/* Add a record to the message */
|
||||
extern int NKSN_AddRecord(NKSN_Instance inst, int critical, int type,
|
||||
const void *body, int body_length);
|
||||
|
||||
/* Terminate the message */
|
||||
extern int NKSN_EndMessage(NKSN_Instance inst);
|
||||
|
||||
/* Get the next record from the received message. This function should be
|
||||
called from the message handler. */
|
||||
extern int NKSN_GetRecord(NKSN_Instance inst, int *critical, int *type, int *body_length,
|
||||
void *body, int buffer_length);
|
||||
|
||||
/* Export NTS keys for a specified algorithm */
|
||||
extern int NKSN_GetKeys(NKSN_Instance inst, SIV_Algorithm siv, NKE_Key *c2s, NKE_Key *s2c);
|
||||
|
||||
/* Check if the session has stopped */
|
||||
extern int NKSN_IsStopped(NKSN_Instance inst);
|
||||
|
||||
/* Stop the session */
|
||||
extern void NKSN_StopSession(NKSN_Instance inst);
|
||||
|
||||
/* Get a factor to calculate retry interval (in log2 seconds)
|
||||
based on the session state or how it was terminated */
|
||||
extern int NKSN_GetRetryFactor(NKSN_Instance inst);
|
||||
|
||||
#endif
|
||||
41
nts_ntp.h
Normal file
41
nts_ntp.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 the NTS-NTP protocol
|
||||
*/
|
||||
|
||||
#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
|
||||
#define NTS_MIN_UNPADDED_NONCE_LENGTH 16
|
||||
#define NTS_MAX_COOKIES 8
|
||||
|
||||
#endif
|
||||
183
nts_ntp_auth.c
Normal file
183
nts_ntp_auth.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
NTS Authenticator and Encrypted Extension Fields extension field
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ntp_auth.h"
|
||||
|
||||
#include "logging.h"
|
||||
#include "ntp_ext.h"
|
||||
#include "nts_ntp.h"
|
||||
#include "siv.h"
|
||||
#include "util.h"
|
||||
|
||||
struct AuthHeader {
|
||||
uint16_t nonce_length;
|
||||
uint16_t ciphertext_length;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_padding_length(int length)
|
||||
{
|
||||
return length % 4U ? 4 - length % 4U : 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_padded_length(int length)
|
||||
{
|
||||
return length + get_padding_length(length);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const unsigned char *plaintext, int plaintext_length,
|
||||
int min_ef_length)
|
||||
{
|
||||
int auth_length, ciphertext_length, assoc_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) {
|
||||
DEBUG_LOG("Invalid nonce/plaintext length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
assoc_length = info->length;
|
||||
ciphertext_length = SIV_GetTagLength(siv) + plaintext_length;
|
||||
nonce_padding = get_padding_length(nonce_length);
|
||||
ciphertext_padding = get_padding_length(ciphertext_length);
|
||||
min_ef_length = get_padded_length(min_ef_length);
|
||||
|
||||
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);
|
||||
auth_length += additional_padding;
|
||||
|
||||
if (!NEF_AddBlankField(packet, info, NTP_EF_NTS_AUTH_AND_EEF, auth_length,
|
||||
(void **)&header)) {
|
||||
DEBUG_LOG("Could not add EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
header->nonce_length = htons(nonce_length);
|
||||
header->ciphertext_length = htons(ciphertext_length);
|
||||
|
||||
body = (unsigned char *)(header + 1);
|
||||
ciphertext = body + nonce_length + nonce_padding;
|
||||
|
||||
if ((unsigned char *)header + auth_length !=
|
||||
ciphertext + ciphertext_length + ciphertext_padding + additional_padding)
|
||||
assert(0);
|
||||
|
||||
memcpy(body, nonce, nonce_length);
|
||||
memset(body + nonce_length, 0, nonce_padding);
|
||||
|
||||
if (!SIV_Encrypt(siv, nonce, nonce_length, packet, assoc_length,
|
||||
plaintext, plaintext_length, ciphertext, ciphertext_length)) {
|
||||
DEBUG_LOG("SIV encrypt failed");
|
||||
info->length = assoc_length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(ciphertext + ciphertext_length, 0, ciphertext_padding + additional_padding);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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;
|
||||
unsigned char *nonce, *ciphertext;
|
||||
int ef_type, ef_body_length;
|
||||
void *ef_body;
|
||||
struct AuthHeader *header;
|
||||
|
||||
if (buffer_length < 0)
|
||||
return 0;
|
||||
|
||||
if (!NEF_ParseField(packet, info->length, ef_start,
|
||||
NULL, &ef_type, &ef_body, &ef_body_length))
|
||||
return 0;
|
||||
|
||||
if (ef_type != NTP_EF_NTS_AUTH_AND_EEF || ef_body_length < sizeof (*header))
|
||||
return 0;
|
||||
|
||||
header = ef_body;
|
||||
|
||||
nonce_length = ntohs(header->nonce_length);
|
||||
ciphertext_length = ntohs(header->ciphertext_length);
|
||||
|
||||
if (get_padded_length(nonce_length) +
|
||||
get_padded_length(ciphertext_length) > ef_body_length)
|
||||
return 0;
|
||||
|
||||
nonce = (unsigned char *)(header + 1);
|
||||
ciphertext = nonce + get_padded_length(nonce_length);
|
||||
|
||||
siv_tag_length = SIV_GetTagLength(siv);
|
||||
|
||||
if (nonce_length < 1 ||
|
||||
ciphertext_length < siv_tag_length ||
|
||||
ciphertext_length - siv_tag_length > buffer_length) {
|
||||
DEBUG_LOG("Unexpected nonce/ciphertext length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ef_body_length < sizeof (*header) +
|
||||
NTS_MIN_UNPADDED_NONCE_LENGTH + get_padded_length(ciphertext_length)) {
|
||||
DEBUG_LOG("Missing padding");
|
||||
return 0;
|
||||
}
|
||||
|
||||
*plaintext_length = ciphertext_length - siv_tag_length;
|
||||
assert(*plaintext_length >= 0);
|
||||
|
||||
if (!SIV_Decrypt(siv, nonce, nonce_length, packet, ef_start,
|
||||
ciphertext, ciphertext_length, plaintext, *plaintext_length)) {
|
||||
DEBUG_LOG("SIV decrypt failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
43
nts_ntp_auth.h
Normal file
43
nts_ntp_auth.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 for NTS Authenticator and Encrypted Extension Fields
|
||||
extension field
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_NTP_AUTH_H
|
||||
#define GOT_NTS_NTP_AUTH_H
|
||||
|
||||
#include "ntp.h"
|
||||
#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 *plaintext, int plaintext_length,
|
||||
int min_ef_length);
|
||||
|
||||
extern int NNA_DecryptAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
||||
int ef_start, unsigned char *plaintext, int buffer_length,
|
||||
int *plaintext_length);
|
||||
|
||||
#endif
|
||||
707
nts_ntp_client.c
Normal file
707
nts_ntp_client.c
Normal file
@@ -0,0 +1,707 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Client NTS-NTP authentication
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ntp_client.h"
|
||||
|
||||
#include "conf.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "ntp.h"
|
||||
#include "ntp_ext.h"
|
||||
#include "ntp_sources.h"
|
||||
#include "nts_ke_client.h"
|
||||
#include "nts_ntp.h"
|
||||
#include "nts_ntp_auth.h"
|
||||
#include "sched.h"
|
||||
#include "siv.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Maximum length of all cookies to avoid IP fragmentation */
|
||||
#define MAX_TOTAL_COOKIE_LENGTH (8 * 108)
|
||||
|
||||
/* Magic string of files containing keys and cookies */
|
||||
#define DUMP_IDENTIFIER "NNC0\n"
|
||||
|
||||
struct NNC_Instance_Record {
|
||||
/* 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 nke_attempts;
|
||||
double next_nke_attempt;
|
||||
double last_nke_success;
|
||||
|
||||
NKE_Context context;
|
||||
unsigned int context_id;
|
||||
NKE_Cookie cookies[NTS_MAX_COOKIES];
|
||||
int num_cookies;
|
||||
int cookie_index;
|
||||
int auth_ready;
|
||||
int nak_response;
|
||||
int ok_response;
|
||||
unsigned char nonce[NTS_MIN_UNPADDED_NONCE_LENGTH];
|
||||
unsigned char uniq_id[NTS_MIN_UNIQ_ID_LENGTH];
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void save_cookies(NNC_Instance inst);
|
||||
static void load_cookies(NNC_Instance inst);
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
reset_instance(NNC_Instance inst)
|
||||
{
|
||||
if (inst->nke)
|
||||
NKC_DestroyInstance(inst->nke);
|
||||
inst->nke = NULL;
|
||||
if (inst->siv)
|
||||
SIV_DestroyInstance(inst->siv);
|
||||
inst->siv = NULL;
|
||||
|
||||
inst->nke_attempts = 0;
|
||||
inst->next_nke_attempt = 0.0;
|
||||
inst->last_nke_success = 0.0;
|
||||
|
||||
memset(&inst->context, 0, sizeof (inst->context));
|
||||
inst->context_id = 0;
|
||||
memset(inst->cookies, 0, sizeof (inst->cookies));
|
||||
inst->num_cookies = 0;
|
||||
inst->cookie_index = 0;
|
||||
inst->auth_ready = 0;
|
||||
inst->nak_response = 0;
|
||||
inst->ok_response = 1;
|
||||
memset(inst->nonce, 0, sizeof (inst->nonce));
|
||||
memset(inst->uniq_id, 0, sizeof (inst->uniq_id));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
NNC_Instance
|
||||
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->nts_address = *nts_address;
|
||||
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;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NNC_DestroyInstance(NNC_Instance inst)
|
||||
{
|
||||
save_cookies(inst);
|
||||
|
||||
reset_instance(inst);
|
||||
|
||||
Free(inst->name);
|
||||
Free(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
check_cookies(NNC_Instance inst)
|
||||
{
|
||||
/* Force a new NTS-KE session if a NAK was received without a valid response,
|
||||
or the keys encrypting the cookies need to be refreshed */
|
||||
if (inst->num_cookies > 0 &&
|
||||
((inst->nak_response && !inst->ok_response) ||
|
||||
SCH_GetLastEventMonoTime() - inst->last_nke_success > CNF_GetNtsRefresh())) {
|
||||
inst->num_cookies = 0;
|
||||
DEBUG_LOG("Dropped cookies");
|
||||
}
|
||||
|
||||
return inst->num_cookies > 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
set_ntp_address(NNC_Instance inst, NTP_Remote_Address *negotiated_address)
|
||||
{
|
||||
NTP_Remote_Address old_address, new_address;
|
||||
|
||||
old_address = inst->ntp_address;
|
||||
new_address = *negotiated_address;
|
||||
|
||||
if (new_address.ip_addr.family == IPADDR_UNSPEC)
|
||||
new_address.ip_addr = inst->nts_address.ip_addr;
|
||||
if (new_address.port == 0)
|
||||
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)
|
||||
/* Nothing to do */
|
||||
return 1;
|
||||
|
||||
if (NSR_UpdateSourceNtpAddress(&old_address, &new_address) != NSR_Success) {
|
||||
LOG(LOGS_ERR, "Could not change %s to negotiated address %s",
|
||||
UTI_IPToString(&old_address.ip_addr), UTI_IPToString(&new_address.ip_addr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->ntp_address = new_address;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_next_nke_attempt(NNC_Instance inst, double now)
|
||||
{
|
||||
int factor, interval;
|
||||
|
||||
if (!inst->nke)
|
||||
return;
|
||||
|
||||
factor = NKC_GetRetryFactor(inst->nke);
|
||||
interval = MIN(factor + inst->nke_attempts - 1, NKE_MAX_RETRY_INTERVAL2);
|
||||
inst->next_nke_attempt = now + UTI_Log2ToDouble(interval);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
get_cookies(NNC_Instance inst)
|
||||
{
|
||||
NTP_Remote_Address ntp_address;
|
||||
double now;
|
||||
int got_data;
|
||||
|
||||
assert(inst->num_cookies == 0);
|
||||
|
||||
now = SCH_GetLastEventMonoTime();
|
||||
|
||||
/* Create and start a new NTS-KE session if not already present */
|
||||
if (!inst->nke) {
|
||||
if (now < inst->next_nke_attempt) {
|
||||
DEBUG_LOG("Limiting NTS-KE request rate (%f seconds)",
|
||||
inst->next_nke_attempt - now);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
update_next_nke_attempt(inst, now);
|
||||
|
||||
/* Wait until the session stops */
|
||||
if (NKC_IsActive(inst->nke))
|
||||
return 0;
|
||||
|
||||
assert(sizeof (inst->cookies) / sizeof (inst->cookies[0]) == NTS_MAX_COOKIES);
|
||||
|
||||
/* Get the new keys, cookies and NTP address if the session was successful */
|
||||
got_data = NKC_GetNtsData(inst->nke, &inst->context,
|
||||
inst->cookies, &inst->num_cookies, NTS_MAX_COOKIES,
|
||||
&ntp_address);
|
||||
|
||||
NKC_DestroyInstance(inst->nke);
|
||||
inst->nke = NULL;
|
||||
|
||||
if (!got_data)
|
||||
return 0;
|
||||
|
||||
if (inst->siv)
|
||||
SIV_DestroyInstance(inst->siv);
|
||||
inst->siv = NULL;
|
||||
|
||||
inst->context_id++;
|
||||
|
||||
/* Force a new session if the NTP address is used by another source, with
|
||||
an expectation that it will eventually get a non-conflicting address */
|
||||
if (!set_ntp_address(inst, &ntp_address)) {
|
||||
inst->num_cookies = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->last_nke_success = now;
|
||||
inst->cookie_index = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNC_PrepareForAuth(NNC_Instance inst)
|
||||
{
|
||||
inst->auth_ready = 0;
|
||||
|
||||
/* Prepare data for the next request and invalidate any responses to the
|
||||
previous request */
|
||||
UTI_GetRandomBytes(inst->uniq_id, sizeof (inst->uniq_id));
|
||||
UTI_GetRandomBytes(inst->nonce, sizeof (inst->nonce));
|
||||
|
||||
/* Get new cookies if there are not any, or they are no longer usable */
|
||||
if (!check_cookies(inst)) {
|
||||
if (!get_cookies(inst))
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->nak_response = 0;
|
||||
|
||||
if (!inst->siv)
|
||||
inst->siv = SIV_CreateInstance(inst->context.algorithm);
|
||||
|
||||
if (!inst->siv ||
|
||||
!SIV_SetKey(inst->siv, inst->context.c2s.key, inst->context.c2s.length)) {
|
||||
DEBUG_LOG("Could not set SIV key");
|
||||
return 0;
|
||||
}
|
||||
|
||||
inst->auth_ready = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNC_GenerateRequestAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||
NTP_PacketInfo *info)
|
||||
{
|
||||
NKE_Cookie *cookie;
|
||||
int i, req_cookies;
|
||||
void *ef_body;
|
||||
|
||||
if (!inst->auth_ready)
|
||||
return 0;
|
||||
|
||||
inst->auth_ready = 0;
|
||||
|
||||
if (inst->num_cookies <= 0 || !inst->siv)
|
||||
return 0;
|
||||
|
||||
if (info->mode != MODE_CLIENT)
|
||||
return 0;
|
||||
|
||||
cookie = &inst->cookies[inst->cookie_index];
|
||||
inst->num_cookies--;
|
||||
inst->cookie_index = (inst->cookie_index + 1) % NTS_MAX_COOKIES;
|
||||
|
||||
req_cookies = MIN(NTS_MAX_COOKIES - inst->num_cookies,
|
||||
MAX_TOTAL_COOKIE_LENGTH / (cookie->length + 4));
|
||||
|
||||
if (!NEF_AddField(packet, info, NTP_EF_NTS_UNIQUE_IDENTIFIER,
|
||||
inst->uniq_id, sizeof (inst->uniq_id)))
|
||||
return 0;
|
||||
|
||||
if (!NEF_AddField(packet, info, NTP_EF_NTS_COOKIE,
|
||||
cookie->cookie, cookie->length))
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < req_cookies - 1; i++) {
|
||||
if (!NEF_AddBlankField(packet, info, NTP_EF_NTS_COOKIE_PLACEHOLDER,
|
||||
cookie->length, &ef_body))
|
||||
return 0;
|
||||
memset(ef_body, 0, cookie->length);
|
||||
}
|
||||
|
||||
if (!NNA_GenerateAuthEF(packet, info, inst->siv, inst->nonce, sizeof (inst->nonce),
|
||||
(const unsigned char *)"", 0, NTP_MAX_V4_MAC_LENGTH + 4))
|
||||
return 0;
|
||||
|
||||
inst->ok_response = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
parse_encrypted_efs(NNC_Instance inst, unsigned char *plaintext, int length)
|
||||
{
|
||||
int ef_length, parsed;
|
||||
|
||||
for (parsed = 0; parsed < length; parsed += ef_length) {
|
||||
if (!NEF_ParseSingleField(plaintext, length, parsed, &ef_length, NULL, NULL, NULL)) {
|
||||
DEBUG_LOG("Could not parse encrypted EF");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
extract_cookies(NNC_Instance inst, unsigned char *plaintext, int length)
|
||||
{
|
||||
int ef_type, ef_body_length, ef_length, parsed, index, acceptable, saved;
|
||||
void *ef_body;
|
||||
|
||||
acceptable = saved = 0;
|
||||
|
||||
for (parsed = 0; parsed < length; parsed += ef_length) {
|
||||
if (!NEF_ParseSingleField(plaintext, length, parsed,
|
||||
&ef_length, &ef_type, &ef_body, &ef_body_length))
|
||||
return 0;
|
||||
|
||||
if (ef_type != NTP_EF_NTS_COOKIE)
|
||||
continue;
|
||||
|
||||
if (ef_length < NTP_MIN_EF_LENGTH || ef_body_length > sizeof (inst->cookies[0].cookie)) {
|
||||
DEBUG_LOG("Unexpected cookie length %d", ef_body_length);
|
||||
continue;
|
||||
}
|
||||
|
||||
acceptable++;
|
||||
|
||||
if (inst->num_cookies >= NTS_MAX_COOKIES)
|
||||
continue;
|
||||
|
||||
index = (inst->cookie_index + inst->num_cookies) % NTS_MAX_COOKIES;
|
||||
assert(index >= 0 && index < NTS_MAX_COOKIES);
|
||||
assert(sizeof (inst->cookies) / sizeof (inst->cookies[0]) == NTS_MAX_COOKIES);
|
||||
|
||||
memcpy(inst->cookies[index].cookie, ef_body, ef_body_length);
|
||||
inst->cookies[index].length = ef_body_length;
|
||||
inst->num_cookies++;
|
||||
|
||||
saved++;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Extracted %d cookies (saved %d)", acceptable, saved);
|
||||
|
||||
return acceptable > 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNC_CheckResponseAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||
NTP_PacketInfo *info)
|
||||
{
|
||||
int ef_type, ef_body_length, ef_length, parsed, plaintext_length;
|
||||
int has_valid_uniq_id = 0, has_valid_auth = 0;
|
||||
unsigned char plaintext[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
void *ef_body;
|
||||
|
||||
if (info->ext_fields == 0 || info->mode != MODE_SERVER)
|
||||
return 0;
|
||||
|
||||
/* Accept at most one response per request */
|
||||
if (inst->ok_response || inst->auth_ready)
|
||||
return 0;
|
||||
|
||||
if (!inst->siv ||
|
||||
!SIV_SetKey(inst->siv, inst->context.s2c.key, inst->context.s2c.length)) {
|
||||
DEBUG_LOG("Could not set SIV key");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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() */
|
||||
return 0;
|
||||
|
||||
switch (ef_type) {
|
||||
case NTP_EF_NTS_UNIQUE_IDENTIFIER:
|
||||
if (ef_body_length != sizeof (inst->uniq_id) ||
|
||||
memcmp(ef_body, inst->uniq_id, sizeof (inst->uniq_id)) != 0) {
|
||||
DEBUG_LOG("Invalid uniq id");
|
||||
return 0;
|
||||
}
|
||||
has_valid_uniq_id = 1;
|
||||
break;
|
||||
case NTP_EF_NTS_COOKIE:
|
||||
DEBUG_LOG("Unencrypted cookie");
|
||||
break;
|
||||
case NTP_EF_NTS_AUTH_AND_EEF:
|
||||
if (parsed + ef_length != info->length) {
|
||||
DEBUG_LOG("Auth not last EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!NNA_DecryptAuthEF(packet, info, inst->siv, parsed,
|
||||
plaintext, sizeof (plaintext), &plaintext_length))
|
||||
return 0;
|
||||
|
||||
if (!parse_encrypted_efs(inst, plaintext, plaintext_length))
|
||||
return 0;
|
||||
|
||||
has_valid_auth = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!has_valid_uniq_id || !has_valid_auth) {
|
||||
if (has_valid_uniq_id && packet->stratum == NTP_INVALID_STRATUM &&
|
||||
ntohl(packet->reference_id) == NTP_KOD_NTS_NAK) {
|
||||
DEBUG_LOG("NTS NAK");
|
||||
inst->nak_response = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_LOG("Missing NTS EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!extract_cookies(inst, plaintext, plaintext_length))
|
||||
return 0;
|
||||
|
||||
inst->ok_response = 1;
|
||||
|
||||
/* At this point we know the client interoperates with the server. Allow a
|
||||
new NTS-KE session to be started as soon as the cookies run out. */
|
||||
inst->nke_attempts = 0;
|
||||
inst->next_nke_attempt = 0.0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
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);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
save_cookies(NNC_Instance inst)
|
||||
{
|
||||
char buf[2 * NKE_MAX_COOKIE_LENGTH + 2], *dump_dir, *filename;
|
||||
struct timespec now;
|
||||
double context_time;
|
||||
FILE *f;
|
||||
int i;
|
||||
|
||||
if (inst->num_cookies < 1 || !UTI_IsIPReal(&inst->nts_address.ip_addr))
|
||||
return;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
if (!dump_dir)
|
||||
return;
|
||||
|
||||
filename = UTI_IPToString(&inst->nts_address.ip_addr);
|
||||
|
||||
f = UTI_OpenFile(dump_dir, filename, ".tmp", 'w', 0600);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||
context_time = inst->last_nke_success - SCH_GetLastEventMonoTime();
|
||||
context_time += UTI_TimespecToDouble(&now);
|
||||
|
||||
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,
|
||||
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 ||
|
||||
!UTI_BytesToHex(inst->context.c2s.key, inst->context.c2s.length, buf, sizeof (buf)) ||
|
||||
fprintf(f, "%s\n", buf) < 0)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < inst->num_cookies; i++) {
|
||||
if (!UTI_BytesToHex(inst->cookies[i].cookie, inst->cookies[i].length, buf, sizeof (buf)) ||
|
||||
fprintf(f, "%s\n", buf) < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
if (!UTI_RenameTempFile(dump_dir, filename, ".tmp", ".nts"))
|
||||
;
|
||||
return;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s cookies for %s", "save", filename);
|
||||
fclose(f);
|
||||
|
||||
if (!UTI_RemoveFile(dump_dir, filename, ".nts"))
|
||||
;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define MAX_WORDS 4
|
||||
|
||||
static void
|
||||
load_cookies(NNC_Instance inst)
|
||||
{
|
||||
char line[2 * NKE_MAX_COOKIE_LENGTH + 2], *dump_dir, *filename, *words[MAX_WORDS];
|
||||
unsigned int context_id;
|
||||
int i, algorithm, port;
|
||||
double context_time;
|
||||
struct timespec now;
|
||||
IPSockAddr ntp_addr;
|
||||
FILE *f;
|
||||
|
||||
dump_dir = CNF_GetNtsDumpDir();
|
||||
if (!dump_dir)
|
||||
return;
|
||||
|
||||
filename = UTI_IPToString(&inst->nts_address.ip_addr);
|
||||
|
||||
f = UTI_OpenFile(dump_dir, filename, ".nts", 'r', 0);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
/* Don't load this file again */
|
||||
if (!UTI_RemoveFile(dump_dir, filename, ".nts"))
|
||||
;
|
||||
|
||||
if (inst->siv)
|
||||
SIV_DestroyInstance(inst->siv);
|
||||
inst->siv = NULL;
|
||||
|
||||
if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
|
||||
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 ||
|
||||
!UTI_StringToIP(words[0], &ntp_addr.ip_addr) || sscanf(words[1], "%d", &port) != 1 ||
|
||||
!fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 4 ||
|
||||
sscanf(words[0], "%u", &context_id) != 1 || sscanf(words[1], "%d", &algorithm) != 1)
|
||||
goto error;
|
||||
|
||||
inst->context.algorithm = algorithm;
|
||||
inst->context.s2c.length = UTI_HexToBytes(words[2], inst->context.s2c.key,
|
||||
sizeof (inst->context.s2c.key));
|
||||
inst->context.c2s.length = UTI_HexToBytes(words[3], inst->context.c2s.key,
|
||||
sizeof (inst->context.c2s.key));
|
||||
|
||||
if (inst->context.s2c.length != SIV_GetKeyLength(algorithm) ||
|
||||
inst->context.c2s.length != inst->context.s2c.length)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < NTS_MAX_COOKIES && fgets(line, sizeof (line), f); i++) {
|
||||
if (UTI_SplitString(line, words, MAX_WORDS) != 1)
|
||||
goto error;
|
||||
|
||||
inst->cookies[i].length = UTI_HexToBytes(words[0], inst->cookies[i].cookie,
|
||||
sizeof (inst->cookies[i].cookie));
|
||||
if (inst->cookies[i].length == 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
inst->num_cookies = i;
|
||||
|
||||
ntp_addr.port = port;
|
||||
if (!set_ntp_address(inst, &ntp_addr))
|
||||
goto error;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||
context_time -= UTI_TimespecToDouble(&now);
|
||||
if (context_time > 0)
|
||||
context_time = 0;
|
||||
inst->last_nke_success = context_time + SCH_GetLastEventMonoTime();
|
||||
inst->context_id = context_id;
|
||||
|
||||
DEBUG_LOG("Loaded %d cookies for %s", i, filename);
|
||||
return;
|
||||
|
||||
error:
|
||||
DEBUG_LOG("Could not %s cookies for %s", "load", filename);
|
||||
fclose(f);
|
||||
|
||||
memset(&inst->context, 0, sizeof (inst->context));
|
||||
inst->num_cookies = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NNC_DumpData(NNC_Instance inst)
|
||||
{
|
||||
save_cookies(inst);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NNC_GetReport(NNC_Instance inst, RPT_AuthReport *report)
|
||||
{
|
||||
report->key_id = inst->context_id;
|
||||
report->key_type = inst->context.algorithm;
|
||||
report->key_length = 8 * inst->context.s2c.length;
|
||||
report->ke_attempts = inst->nke_attempts;
|
||||
if (report->key_length > 0)
|
||||
report->last_ke_ago = SCH_GetLastEventMonoTime() - inst->last_nke_success;
|
||||
else
|
||||
report->last_ke_ago = -1;
|
||||
report->cookies = inst->num_cookies;
|
||||
report->cookie_length = inst->num_cookies > 0 ? inst->cookies[inst->cookie_index].length : 0;
|
||||
report->nak = inst->nak_response;
|
||||
}
|
||||
51
nts_ntp_client.h
Normal file
51
nts_ntp_client.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 client NTS-NTP authentication
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_NTP_CLIENT_H
|
||||
#define GOT_NTS_NTP_CLIENT_H
|
||||
|
||||
#include "addressing.h"
|
||||
#include "ntp.h"
|
||||
#include "reports.h"
|
||||
|
||||
typedef struct NNC_Instance_Record *NNC_Instance;
|
||||
|
||||
extern NNC_Instance NNC_CreateInstance(IPSockAddr *nts_address, const char *name,
|
||||
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,
|
||||
NTP_PacketInfo *info);
|
||||
extern int NNC_CheckResponseAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||
NTP_PacketInfo *info);
|
||||
|
||||
extern void NNC_ChangeAddress(NNC_Instance inst, IPAddr *address);
|
||||
|
||||
extern void NNC_DumpData(NNC_Instance inst);
|
||||
|
||||
extern void NNC_GetReport(NNC_Instance inst, RPT_AuthReport *report);
|
||||
|
||||
#endif
|
||||
283
nts_ntp_server.c
Normal file
283
nts_ntp_server.c
Normal file
@@ -0,0 +1,283 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Server NTS-NTP authentication
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include "nts_ntp_server.h"
|
||||
|
||||
#include "conf.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "ntp.h"
|
||||
#include "ntp_ext.h"
|
||||
#include "nts_ke_server.h"
|
||||
#include "nts_ntp.h"
|
||||
#include "nts_ntp_auth.h"
|
||||
#include "siv.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SERVER_SIV AEAD_AES_SIV_CMAC_256
|
||||
|
||||
struct NtsServer {
|
||||
SIV_Instance siv;
|
||||
unsigned char nonce[NTS_MIN_UNPADDED_NONCE_LENGTH];
|
||||
NKE_Cookie cookies[NTS_MAX_COOKIES];
|
||||
int num_cookies;
|
||||
NTP_int64 req_tx;
|
||||
};
|
||||
|
||||
/* The server instance handling all requests */
|
||||
struct NtsServer *server;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NNS_Initialise(void)
|
||||
{
|
||||
const char **certs, **keys;
|
||||
|
||||
/* Create an NTS-NTP server instance only if NTS-KE server is enabled */
|
||||
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");
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
NNS_Finalise(void)
|
||||
{
|
||||
if (!server)
|
||||
return;
|
||||
|
||||
SIV_DestroyInstance(server->siv);
|
||||
Free(server);
|
||||
server = NULL;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod)
|
||||
{
|
||||
int ef_type, ef_body_length, ef_length, has_uniq_id = 0, has_auth = 0, has_cookie = 0;
|
||||
int i, plaintext_length, parsed, requested_cookies, cookie_length = -1, auth_start = 0;
|
||||
unsigned char plaintext[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
NKE_Context context;
|
||||
NKE_Cookie cookie;
|
||||
void *ef_body;
|
||||
|
||||
*kod = 0;
|
||||
|
||||
if (!server)
|
||||
return 0;
|
||||
|
||||
server->num_cookies = 0;
|
||||
server->req_tx = packet->transmit_ts;
|
||||
|
||||
if (info->ext_fields == 0 || info->mode != MODE_CLIENT)
|
||||
return 0;
|
||||
|
||||
requested_cookies = 0;
|
||||
|
||||
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() */
|
||||
return 0;
|
||||
|
||||
switch (ef_type) {
|
||||
case NTP_EF_NTS_UNIQUE_IDENTIFIER:
|
||||
has_uniq_id = 1;
|
||||
break;
|
||||
case NTP_EF_NTS_COOKIE:
|
||||
if (has_cookie || ef_body_length > sizeof (cookie.cookie)) {
|
||||
DEBUG_LOG("Unexpected cookie/length");
|
||||
return 0;
|
||||
}
|
||||
cookie.length = ef_body_length;
|
||||
memcpy(cookie.cookie, ef_body, ef_body_length);
|
||||
has_cookie = 1;
|
||||
/* Fall through */
|
||||
case NTP_EF_NTS_COOKIE_PLACEHOLDER:
|
||||
requested_cookies++;
|
||||
|
||||
if (cookie_length >= 0 && cookie_length != ef_body_length) {
|
||||
DEBUG_LOG("Invalid cookie/placeholder length");
|
||||
return 0;
|
||||
}
|
||||
cookie_length = ef_body_length;
|
||||
break;
|
||||
case NTP_EF_NTS_AUTH_AND_EEF:
|
||||
if (parsed + ef_length != info->length) {
|
||||
DEBUG_LOG("Auth not last EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
auth_start = parsed;
|
||||
has_auth = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!has_uniq_id || !has_cookie || !has_auth) {
|
||||
DEBUG_LOG("Missing an NTS EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!NKS_DecodeCookie(&cookie, &context)) {
|
||||
*kod = NTP_KOD_NTS_NAK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (context.algorithm != SERVER_SIV) {
|
||||
DEBUG_LOG("Unexpected SIV");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!SIV_SetKey(server->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,
|
||||
plaintext, sizeof (plaintext), &plaintext_length)) {
|
||||
*kod = NTP_KOD_NTS_NAK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (parsed = 0; parsed < plaintext_length; parsed += ef_length) {
|
||||
if (!NEF_ParseSingleField(plaintext, plaintext_length, parsed,
|
||||
&ef_length, &ef_type, &ef_body, &ef_body_length)) {
|
||||
DEBUG_LOG("Could not parse encrypted EF");
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (ef_type) {
|
||||
case NTP_EF_NTS_COOKIE_PLACEHOLDER:
|
||||
if (cookie_length != ef_body_length) {
|
||||
DEBUG_LOG("Invalid cookie/placeholder length");
|
||||
return 0;
|
||||
}
|
||||
requested_cookies++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SIV_SetKey(server->siv, context.s2c.key, context.s2c.length)) {
|
||||
DEBUG_LOG("Could not set S2C key");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Prepare data for NNS_GenerateResponseAuth() to minimise the time spent
|
||||
there (when the TX timestamp is already set) */
|
||||
|
||||
UTI_GetRandomBytes(server->nonce, sizeof (server->nonce));
|
||||
|
||||
assert(sizeof (server->cookies) / sizeof (server->cookies[0]) == NTS_MAX_COOKIES);
|
||||
for (i = 0; i < NTS_MAX_COOKIES && i < requested_cookies; i++)
|
||||
if (!NKS_GenerateCookie(&context, &server->cookies[i]))
|
||||
return 0;
|
||||
|
||||
server->num_cookies = i;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
||||
NTP_Packet *response, NTP_PacketInfo *res_info,
|
||||
uint32_t kod)
|
||||
{
|
||||
int i, ef_type, ef_body_length, ef_length, parsed;
|
||||
void *ef_body;
|
||||
unsigned char plaintext[NTP_MAX_EXTENSIONS_LENGTH];
|
||||
int plaintext_length;
|
||||
|
||||
if (!server || req_info->mode != MODE_CLIENT || res_info->mode != MODE_SERVER)
|
||||
return 0;
|
||||
|
||||
/* Make sure this is a response to the request from the last call
|
||||
of NNS_CheckRequestAuth() */
|
||||
if (UTI_CompareNtp64(&server->req_tx, &request->transmit_ts) != 0)
|
||||
assert(0);
|
||||
|
||||
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() */
|
||||
return 0;
|
||||
|
||||
switch (ef_type) {
|
||||
case NTP_EF_NTS_UNIQUE_IDENTIFIER:
|
||||
/* Copy the ID from the request */
|
||||
if (!NEF_AddField(response, res_info, ef_type, ef_body, ef_body_length))
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* NTS NAK response does not have any other fields */
|
||||
if (kod)
|
||||
return 1;
|
||||
|
||||
for (i = 0, plaintext_length = 0; i < server->num_cookies; i++) {
|
||||
if (!NEF_SetField(plaintext, sizeof (plaintext), plaintext_length,
|
||||
NTP_EF_NTS_COOKIE, server->cookies[i].cookie,
|
||||
server->cookies[i].length, &ef_length))
|
||||
return 0;
|
||||
|
||||
plaintext_length += ef_length;
|
||||
assert(plaintext_length <= sizeof (plaintext));
|
||||
}
|
||||
|
||||
server->num_cookies = 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,
|
||||
server->nonce, sizeof (server->nonce),
|
||||
plaintext, plaintext_length,
|
||||
req_info->length - res_info->length))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
40
nts_ntp_server.h
Normal file
40
nts_ntp_server.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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 server NTS-NTP authentication
|
||||
*/
|
||||
|
||||
#ifndef GOT_NTS_NTP_SERVER_H
|
||||
#define GOT_NTS_NTP_SERVER_H
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
extern void NNS_Initialise(void);
|
||||
extern void NNS_Finalise(void);
|
||||
|
||||
extern int NNS_CheckRequestAuth(NTP_Packet *packet, NTP_PacketInfo *info, uint32_t *kod);
|
||||
extern int NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
||||
NTP_Packet *response, NTP_PacketInfo *res_info,
|
||||
uint32_t kod);
|
||||
|
||||
#endif
|
||||
48
pktlength.c
48
pktlength.c
@@ -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 */
|
||||
@@ -110,14 +110,25 @@ static const struct request_length request_lengths[] = {
|
||||
REQ_LENGTH_ENTRY(smoothtime, null), /* SMOOTHTIME */
|
||||
REQ_LENGTH_ENTRY(null, null), /* REFRESH */
|
||||
REQ_LENGTH_ENTRY(null, server_stats), /* SERVER_STATS */
|
||||
REQ_LENGTH_ENTRY(client_accesses_by_index,
|
||||
client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
||||
{ 0, 0 }, /* CLIENT_ACCESSES_BY_INDEX2 - not supported */
|
||||
REQ_LENGTH_ENTRY(local, null), /* LOCAL2 */
|
||||
REQ_LENGTH_ENTRY(ntp_data, ntp_data), /* NTP_DATA */
|
||||
{ 0, 0 }, /* ADD_SERVER2 */
|
||||
{ 0, 0 }, /* ADD_PEER2 */
|
||||
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_SERVER3 */
|
||||
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_PEER3 */
|
||||
{ 0, 0 }, /* ADD_SERVER3 */
|
||||
{ 0, 0 }, /* ADD_PEER3 */
|
||||
REQ_LENGTH_ENTRY(null, null), /* SHUTDOWN */
|
||||
REQ_LENGTH_ENTRY(null, null), /* ONOFFLINE */
|
||||
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_SOURCE */
|
||||
REQ_LENGTH_ENTRY(ntp_source_name,
|
||||
ntp_source_name), /* NTP_SOURCE_NAME */
|
||||
REQ_LENGTH_ENTRY(null, null), /* RESET_SOURCES */
|
||||
REQ_LENGTH_ENTRY(auth_data, auth_data), /* AUTH_DATA */
|
||||
REQ_LENGTH_ENTRY(client_accesses_by_index,
|
||||
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 */
|
||||
};
|
||||
|
||||
static const uint16_t reply_lengths[] = {
|
||||
@@ -132,13 +143,19 @@ static const uint16_t reply_lengths[] = {
|
||||
0, /* SUBNETS_ACCESSED - not supported */
|
||||
0, /* CLIENT_ACCESSES - not supported */
|
||||
0, /* CLIENT_ACCESSES_BY_INDEX - not supported */
|
||||
0, /* MANUAL_LIST - variable length */
|
||||
0, /* MANUAL_LIST - not supported */
|
||||
RPY_LENGTH_ENTRY(activity), /* ACTIVITY */
|
||||
RPY_LENGTH_ENTRY(smoothing), /* SMOOTHING */
|
||||
RPY_LENGTH_ENTRY(server_stats), /* SERVER_STATS */
|
||||
RPY_LENGTH_ENTRY(client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
||||
0, /* SERVER_STATS - not supported */
|
||||
0, /* CLIENT_ACCESSES_BY_INDEX2 - not supported */
|
||||
RPY_LENGTH_ENTRY(ntp_data), /* NTP_DATA */
|
||||
RPY_LENGTH_ENTRY(manual_timestamp), /* MANUAL_TIMESTAMP2 */
|
||||
RPY_LENGTH_ENTRY(manual_list), /* MANUAL_LIST2 */
|
||||
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 */
|
||||
RPY_LENGTH_ENTRY(select_data), /* SELECT_DATA */
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
@@ -195,21 +212,6 @@ PKL_ReplyLength(CMD_Reply *r)
|
||||
if (type < 1 || type >= N_REPLY_TYPES)
|
||||
return 0;
|
||||
|
||||
/* Length of MANUAL_LIST depends on number of samples stored in it */
|
||||
if (type == RPY_MANUAL_LIST) {
|
||||
uint32_t ns;
|
||||
|
||||
if (r->status != htons(STT_SUCCESS))
|
||||
return offsetof(CMD_Reply, data);
|
||||
|
||||
ns = ntohl(r->data.manual_list.n_samples);
|
||||
if (ns > MAX_MANUAL_LIST_SAMPLES)
|
||||
return 0;
|
||||
|
||||
return offsetof(CMD_Reply, data.manual_list.samples) +
|
||||
ns * sizeof (RPY_ManualListSample);
|
||||
}
|
||||
|
||||
return reply_lengths[type];
|
||||
}
|
||||
|
||||
|
||||
132
privops.c
132
privops.c
@@ -33,6 +33,7 @@
|
||||
#include "nameserv.h"
|
||||
#include "logging.h"
|
||||
#include "privops.h"
|
||||
#include "socket.h"
|
||||
#include "util.h"
|
||||
|
||||
#define OP_ADJUSTTIME 1024
|
||||
@@ -158,7 +159,7 @@ res_fatal(PrvResponse *res, const char *fmt, ...)
|
||||
static int
|
||||
send_response(int fd, const PrvResponse *res)
|
||||
{
|
||||
if (send(fd, res, sizeof (*res), 0) != sizeof (*res))
|
||||
if (SCK_Send(fd, res, sizeof (*res), 0) != sizeof (*res))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -170,37 +171,23 @@ send_response(int fd, const PrvResponse *res)
|
||||
static int
|
||||
receive_from_daemon(int fd, PrvRequest *req)
|
||||
{
|
||||
struct msghdr msg;
|
||||
struct cmsghdr *cmsg;
|
||||
struct iovec iov;
|
||||
char cmsgbuf[256];
|
||||
SCK_Message *message;
|
||||
|
||||
iov.iov_base = req;
|
||||
iov.iov_len = sizeof (*req);
|
||||
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = (void *)cmsgbuf;
|
||||
msg.msg_controllen = sizeof (cmsgbuf);
|
||||
msg.msg_flags = MSG_WAITALL;
|
||||
|
||||
/* read the data */
|
||||
if (recvmsg(fd, &msg, 0) != sizeof (*req))
|
||||
message = SCK_ReceiveMessage(fd, SCK_FLAG_MSG_DESCRIPTOR);
|
||||
if (!message || message->length != sizeof (*req))
|
||||
return 0;
|
||||
|
||||
memcpy(req, message->data, sizeof (*req));
|
||||
|
||||
if (req->op == OP_BINDSOCKET) {
|
||||
/* extract transferred descriptor */
|
||||
req->data.bind_socket.sock = -1;
|
||||
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
|
||||
memcpy(&req->data.bind_socket.sock, CMSG_DATA(cmsg), sizeof (int));
|
||||
}
|
||||
req->data.bind_socket.sock = message->descriptor;
|
||||
|
||||
/* return error if valid descriptor not found */
|
||||
if (req->data.bind_socket.sock < 0)
|
||||
return 0;
|
||||
} else if (message->descriptor >= 0) {
|
||||
SCK_CloseSocket(message->descriptor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -257,8 +244,7 @@ do_set_time(const ReqSetTime *req, PrvResponse *res)
|
||||
static void
|
||||
do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
||||
{
|
||||
unsigned short port;
|
||||
IPAddr ip;
|
||||
IPSockAddr ip_saddr;
|
||||
int sock_fd;
|
||||
struct sockaddr *sa;
|
||||
socklen_t sa_len;
|
||||
@@ -267,10 +253,11 @@ do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
||||
sa_len = req->sa_len;
|
||||
sock_fd = req->sock;
|
||||
|
||||
UTI_SockaddrToIPAndPort(sa, &ip, &port);
|
||||
if (port && port != CNF_GetNTPPort()) {
|
||||
close(sock_fd);
|
||||
res_fatal(res, "Invalid port %d", port);
|
||||
SCK_SockaddrToIPSockAddr(sa, sa_len, &ip_saddr);
|
||||
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||
ip_saddr.port != CNF_GetAcquisitionPort()) {
|
||||
SCK_CloseSocket(sock_fd);
|
||||
res_fatal(res, "Invalid port %d", ip_saddr.port);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -279,7 +266,7 @@ do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
||||
res->res_errno = errno;
|
||||
|
||||
/* sock is still open on daemon side, but we're done with it in the helper */
|
||||
close(sock_fd);
|
||||
SCK_CloseSocket(sock_fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -373,7 +360,7 @@ helper_main(int fd)
|
||||
send_response(fd, &res);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
SCK_CloseSocket(fd);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -386,7 +373,7 @@ receive_response(PrvResponse *res)
|
||||
{
|
||||
int resp_len;
|
||||
|
||||
resp_len = recv(helper_fd, res, sizeof (*res), 0);
|
||||
resp_len = SCK_Receive(helper_fd, res, sizeof (*res), 0);
|
||||
if (resp_len < 0)
|
||||
LOG_FATAL("Could not read from helper : %s", strerror(errno));
|
||||
if (resp_len != sizeof (*res))
|
||||
@@ -409,41 +396,22 @@ receive_response(PrvResponse *res)
|
||||
static void
|
||||
send_request(PrvRequest *req)
|
||||
{
|
||||
struct msghdr msg;
|
||||
struct iovec iov;
|
||||
char cmsgbuf[256];
|
||||
SCK_Message message;
|
||||
int flags;
|
||||
|
||||
iov.iov_base = req;
|
||||
iov.iov_len = sizeof (*req);
|
||||
SCK_InitMessage(&message, SCK_ADDR_UNSPEC);
|
||||
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = NULL;
|
||||
msg.msg_controllen = 0;
|
||||
msg.msg_flags = 0;
|
||||
message.data = req;
|
||||
message.length = sizeof (*req);
|
||||
flags = 0;
|
||||
|
||||
if (req->op == OP_BINDSOCKET) {
|
||||
/* send file descriptor as a control message */
|
||||
struct cmsghdr *cmsg;
|
||||
int *ptr_send_fd;
|
||||
|
||||
msg.msg_control = cmsgbuf;
|
||||
msg.msg_controllen = CMSG_SPACE(sizeof (int));
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
memset(cmsg, 0, CMSG_SPACE(sizeof (int)));
|
||||
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof (int));
|
||||
|
||||
ptr_send_fd = (int *)CMSG_DATA(cmsg);
|
||||
*ptr_send_fd = req->data.bind_socket.sock;
|
||||
message.descriptor = req->data.bind_socket.sock;
|
||||
flags |= SCK_FLAG_MSG_DESCRIPTOR;
|
||||
}
|
||||
|
||||
if (sendmsg(helper_fd, &msg, 0) < 0) {
|
||||
if (!SCK_SendMessage(helper_fd, &message, flags)) {
|
||||
/* don't try to send another request from exit() */
|
||||
helper_fd = -1;
|
||||
LOG_FATAL("Could not send to helper : %s", strerror(errno));
|
||||
@@ -573,13 +541,14 @@ PRV_SetTime(const struct timeval *tp, const struct timezone *tzp)
|
||||
int
|
||||
PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
||||
{
|
||||
IPSockAddr ip_saddr;
|
||||
PrvRequest req;
|
||||
PrvResponse res;
|
||||
IPAddr ip;
|
||||
unsigned short port;
|
||||
|
||||
UTI_SockaddrToIPAndPort(address, &ip, &port);
|
||||
assert(!port || port == CNF_GetNTPPort());
|
||||
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
|
||||
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||
ip_saddr.port != CNF_GetAcquisitionPort())
|
||||
assert(0);
|
||||
|
||||
if (!have_helper())
|
||||
return bind(sock, address, address_len);
|
||||
@@ -588,6 +557,7 @@ PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
||||
req.op = OP_BINDSOCKET;
|
||||
req.data.bind_socket.sock = sock;
|
||||
req.data.bind_socket.sa_len = address_len;
|
||||
assert(address_len <= sizeof (req.data.bind_socket.sa));
|
||||
memcpy(&req.data.bind_socket.sa.u, address, address_len);
|
||||
|
||||
submit_request(&req, &res);
|
||||
@@ -615,7 +585,6 @@ PRV_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
||||
req.op = OP_NAME2IPADDRESS;
|
||||
if (snprintf(req.data.name_to_ipaddress.name, sizeof (req.data.name_to_ipaddress.name),
|
||||
"%s", name) >= sizeof (req.data.name_to_ipaddress.name)) {
|
||||
DEBUG_LOG("Name too long");
|
||||
return DNS_Failure;
|
||||
}
|
||||
|
||||
@@ -669,20 +638,14 @@ void
|
||||
PRV_StartHelper(void)
|
||||
{
|
||||
pid_t pid;
|
||||
int fd, sock_pair[2];
|
||||
int fd, sock_fd1, sock_fd2;
|
||||
|
||||
if (have_helper())
|
||||
LOG_FATAL("Helper already running");
|
||||
|
||||
if (
|
||||
#ifdef SOCK_SEQPACKET
|
||||
socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock_pair) &&
|
||||
#endif
|
||||
socketpair(AF_UNIX, SOCK_DGRAM, 0, sock_pair))
|
||||
LOG_FATAL("socketpair() failed : %s", strerror(errno));
|
||||
|
||||
UTI_FdSetCloexec(sock_pair[0]);
|
||||
UTI_FdSetCloexec(sock_pair[1]);
|
||||
sock_fd1 = SCK_OpenUnixSocketPair(SCK_FLAG_BLOCK, &sock_fd2);
|
||||
if (sock_fd1 < 0)
|
||||
LOG_FATAL("Could not open socket pair");
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
@@ -690,23 +653,24 @@ PRV_StartHelper(void)
|
||||
|
||||
if (pid == 0) {
|
||||
/* child process */
|
||||
close(sock_pair[0]);
|
||||
SCK_CloseSocket(sock_fd1);
|
||||
|
||||
/* close other descriptors inherited from the parent process */
|
||||
for (fd = 0; fd < 1024; fd++) {
|
||||
if (fd != sock_pair[1])
|
||||
/* close other descriptors inherited from the parent process, except
|
||||
stdin, stdout, and stderr */
|
||||
for (fd = STDERR_FILENO + 1; fd < 1024; fd++) {
|
||||
if (fd != sock_fd2)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/* ignore signals, the process will exit on OP_QUIT request */
|
||||
UTI_SetQuitSignalsHandler(SIG_IGN);
|
||||
UTI_SetQuitSignalsHandler(SIG_IGN, 1);
|
||||
|
||||
helper_main(sock_pair[1]);
|
||||
helper_main(sock_fd2);
|
||||
|
||||
} else {
|
||||
/* parent process */
|
||||
close(sock_pair[1]);
|
||||
helper_fd = sock_pair[0];
|
||||
SCK_CloseSocket(sock_fd2);
|
||||
helper_fd = sock_fd1;
|
||||
helper_pid = pid;
|
||||
|
||||
/* stop the helper even when not exiting cleanly from the main function */
|
||||
|
||||
584
refclock.c
584
refclock.c
@@ -2,7 +2,7 @@
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014, 2016-2017
|
||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014, 2016-2019
|
||||
*
|
||||
* 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
|
||||
@@ -37,8 +37,12 @@
|
||||
#include "sources.h"
|
||||
#include "logging.h"
|
||||
#include "regress.h"
|
||||
#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;
|
||||
@@ -79,13 +83,15 @@ struct RCL_Instance_Record {
|
||||
int pps_rate;
|
||||
int pps_active;
|
||||
int max_lock_age;
|
||||
struct MedianFilter filter;
|
||||
int stratum;
|
||||
int tai;
|
||||
uint32_t ref_id;
|
||||
uint32_t lock_ref;
|
||||
double offset;
|
||||
double delay;
|
||||
double precision;
|
||||
double pulse_width;
|
||||
SPF_Instance filter;
|
||||
SCH_TimeoutID timeout_id;
|
||||
SRC_Instance source;
|
||||
};
|
||||
@@ -103,18 +109,6 @@ static void slew_samples(struct timespec *raw, struct timespec *cooked, double d
|
||||
static void add_dispersion(double dispersion, void *anything);
|
||||
static void log_sample(RCL_Instance instance, struct timespec *sample_time, int filtered, int pulse, double raw_offset, double cooked_offset, double dispersion);
|
||||
|
||||
static void filter_init(struct MedianFilter *filter, int length, double max_dispersion);
|
||||
static void filter_fini(struct MedianFilter *filter);
|
||||
static void filter_reset(struct MedianFilter *filter);
|
||||
static double filter_get_avg_sample_dispersion(struct MedianFilter *filter);
|
||||
static void filter_add_sample(struct MedianFilter *filter, struct timespec *sample_time, double offset, double dispersion);
|
||||
static int filter_get_last_sample(struct MedianFilter *filter, struct timespec *sample_time, double *offset, double *dispersion);
|
||||
static int filter_get_samples(struct MedianFilter *filter);
|
||||
static int filter_select_samples(struct MedianFilter *filter);
|
||||
static int filter_get_sample(struct MedianFilter *filter, struct timespec *sample_time, double *offset, double *dispersion);
|
||||
static void filter_slew_samples(struct MedianFilter *filter, struct timespec *when, double dfreq, double doffset);
|
||||
static void filter_add_dispersion(struct MedianFilter *filter, double dispersion);
|
||||
|
||||
static RCL_Instance
|
||||
get_refclock(unsigned int index)
|
||||
{
|
||||
@@ -149,7 +143,7 @@ RCL_Finalise(void)
|
||||
if (inst->driver->fini)
|
||||
inst->driver->fini(inst);
|
||||
|
||||
filter_fini(&inst->filter);
|
||||
SPF_DestroyInstance(inst->filter);
|
||||
Free(inst->driver_parameter);
|
||||
SRC_DestroyInstance(inst->source);
|
||||
Free(inst);
|
||||
@@ -181,16 +175,16 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
inst->driver = &RCL_PHC_driver;
|
||||
} else {
|
||||
LOG_FATAL("unknown refclock driver %s", params->driver_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!inst->driver->init && !inst->driver->poll) {
|
||||
if (!inst->driver->init && !inst->driver->poll)
|
||||
LOG_FATAL("refclock driver %s is not compiled in", params->driver_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (params->tai && !CNF_GetLeapSecTimezone())
|
||||
LOG_FATAL("refclock tai option requires leapsectz");
|
||||
|
||||
inst->data = NULL;
|
||||
inst->driver_parameter = params->driver_parameter;
|
||||
inst->driver_parameter = Strdup(params->driver_parameter);
|
||||
inst->driver_parameter_length = 0;
|
||||
inst->driver_poll = params->driver_poll;
|
||||
inst->poll = params->poll;
|
||||
@@ -200,6 +194,8 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
inst->pps_rate = params->pps_rate;
|
||||
inst->pps_active = 0;
|
||||
inst->max_lock_age = params->max_lock_age;
|
||||
inst->stratum = params->stratum;
|
||||
inst->tai = params->tai;
|
||||
inst->lock_ref = params->lock_ref_id;
|
||||
inst->offset = params->offset;
|
||||
inst->delay = params->delay;
|
||||
@@ -251,30 +247,30 @@ RCL_AddRefclock(RefclockParameters *params)
|
||||
}
|
||||
}
|
||||
|
||||
if (inst->driver->init)
|
||||
if (!inst->driver->init(inst)) {
|
||||
LOG_FATAL("refclock %s initialisation failed", params->driver_name);
|
||||
return 0;
|
||||
}
|
||||
if (inst->driver->init && !inst->driver->init(inst))
|
||||
LOG_FATAL("refclock %s initialisation failed", params->driver_name);
|
||||
|
||||
filter_init(&inst->filter, params->filter_length, params->max_dispersion);
|
||||
/* Require the filter to have at least 4 samples to produce a filtered
|
||||
sample, or be full for shorter lengths, and combine 60% of samples
|
||||
closest to the median */
|
||||
inst->filter = SPF_CreateInstance(MIN(params->filter_length, 4), params->filter_length,
|
||||
params->max_dispersion, 0.6);
|
||||
|
||||
inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, params->sel_options, NULL,
|
||||
params->min_samples, params->max_samples, 0.0, 0.0);
|
||||
inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, 0, params->sel_options,
|
||||
NULL, params->min_samples, params->max_samples,
|
||||
0.0, 0.0);
|
||||
|
||||
DEBUG_LOG("refclock %s refid=%s poll=%d dpoll=%d filter=%d",
|
||||
params->driver_name, UTI_RefidToString(inst->ref_id),
|
||||
inst->poll, inst->driver_poll, params->filter_length);
|
||||
|
||||
Free(params->driver_name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
RCL_StartRefclocks(void)
|
||||
{
|
||||
unsigned int i, j, n;
|
||||
unsigned int i, j, n, lock_index;
|
||||
|
||||
n = ARR_GetSize(refclocks);
|
||||
|
||||
@@ -284,13 +280,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)
|
||||
LOG(LOGS_WARN, "Invalid lock refid %s", UTI_RefidToString(inst->lock_ref));
|
||||
}
|
||||
|
||||
inst->lock_ref = lock_index;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,31 +345,100 @@ RCL_GetDriverParameter(RCL_Instance instance)
|
||||
return instance->driver_parameter;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_next_driver_option(RCL_Instance instance, char *option)
|
||||
{
|
||||
if (option == NULL)
|
||||
option = instance->driver_parameter;
|
||||
|
||||
option += strlen(option) + 1;
|
||||
|
||||
if (option >= instance->driver_parameter + instance->driver_parameter_length)
|
||||
return NULL;
|
||||
|
||||
return option;
|
||||
}
|
||||
|
||||
void
|
||||
RCL_CheckDriverOptions(RCL_Instance instance, const char **options)
|
||||
{
|
||||
char *option;
|
||||
int i, len;
|
||||
|
||||
for (option = get_next_driver_option(instance, NULL);
|
||||
option;
|
||||
option = get_next_driver_option(instance, option)) {
|
||||
for (i = 0; options && options[i]; i++) {
|
||||
len = strlen(options[i]);
|
||||
if (!strncmp(options[i], option, len) &&
|
||||
(option[len] == '=' || option[len] == '\0'))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!options || !options[i])
|
||||
LOG_FATAL("Invalid refclock driver option %s", option);
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
RCL_GetDriverOption(RCL_Instance instance, char *name)
|
||||
{
|
||||
char *s, *e;
|
||||
int n;
|
||||
char *option;
|
||||
int len;
|
||||
|
||||
s = instance->driver_parameter;
|
||||
e = s + instance->driver_parameter_length;
|
||||
n = strlen(name);
|
||||
len = strlen(name);
|
||||
|
||||
while (1) {
|
||||
s += strlen(s) + 1;
|
||||
if (s >= e)
|
||||
break;
|
||||
if (!strncmp(name, s, n)) {
|
||||
if (s[n] == '=')
|
||||
return s + n + 1;
|
||||
if (s[n] == '\0')
|
||||
return s + n;
|
||||
for (option = get_next_driver_option(instance, NULL);
|
||||
option;
|
||||
option = get_next_driver_option(instance, option)) {
|
||||
if (!strncmp(name, option, len)) {
|
||||
if (option[len] == '=')
|
||||
return option + len + 1;
|
||||
if (option[len] == '\0')
|
||||
return option + len;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
convert_tai_offset(struct timespec *sample_time, double *offset)
|
||||
{
|
||||
struct timespec tai_ts, utc_ts;
|
||||
int tai_offset;
|
||||
|
||||
/* Get approximate TAI-UTC offset for the reference time in TAI */
|
||||
UTI_AddDoubleToTimespec(sample_time, *offset, &tai_ts);
|
||||
tai_offset = REF_GetTaiOffset(&tai_ts);
|
||||
|
||||
/* Get TAI-UTC offset for the reference time in UTC +/- 1 second */
|
||||
UTI_AddDoubleToTimespec(&tai_ts, -tai_offset, &utc_ts);
|
||||
tai_offset = REF_GetTaiOffset(&utc_ts);
|
||||
|
||||
if (!tai_offset)
|
||||
return 0;
|
||||
|
||||
*offset -= tai_offset;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
accumulate_sample(RCL_Instance instance, struct timespec *sample_time, double offset, double dispersion)
|
||||
{
|
||||
NTP_Sample sample;
|
||||
|
||||
sample.time = *sample_time;
|
||||
sample.offset = offset;
|
||||
sample.peer_delay = instance->delay;
|
||||
sample.root_delay = instance->delay;
|
||||
sample.peer_dispersion = dispersion;
|
||||
sample.root_dispersion = dispersion;
|
||||
|
||||
return SPF_AccumulateSample(instance->filter, &sample);
|
||||
}
|
||||
|
||||
int
|
||||
RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap)
|
||||
{
|
||||
@@ -385,7 +468,15 @@ RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset
|
||||
return 0;
|
||||
}
|
||||
|
||||
filter_add_sample(&instance->filter, &cooked_time, offset - correction + instance->offset, dispersion);
|
||||
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))
|
||||
return 0;
|
||||
|
||||
instance->pps_active = 0;
|
||||
|
||||
log_sample(instance, &cooked_time, 0, 0, offset, offset - correction + instance->offset, dispersion);
|
||||
@@ -461,20 +552,19 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
|
||||
if (instance->lock_ref != -1) {
|
||||
RCL_Instance lock_refclock;
|
||||
struct timespec ref_sample_time;
|
||||
double sample_diff, ref_offset, ref_dispersion, shift;
|
||||
NTP_Sample ref_sample;
|
||||
double sample_diff, shift;
|
||||
|
||||
lock_refclock = get_refclock(instance->lock_ref);
|
||||
|
||||
if (!filter_get_last_sample(&lock_refclock->filter,
|
||||
&ref_sample_time, &ref_offset, &ref_dispersion)) {
|
||||
if (!SPF_GetLastSample(lock_refclock->filter, &ref_sample)) {
|
||||
DEBUG_LOG("refclock pulse ignored no ref sample");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ref_dispersion += filter_get_avg_sample_dispersion(&lock_refclock->filter);
|
||||
ref_sample.root_dispersion += SPF_GetAvgSampleDispersion(lock_refclock->filter);
|
||||
|
||||
sample_diff = UTI_DiffTimespecsToDouble(cooked_time, &ref_sample_time);
|
||||
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);
|
||||
@@ -482,26 +572,27 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
}
|
||||
|
||||
/* Align the offset to the reference sample */
|
||||
if ((ref_offset - offset) >= 0.0)
|
||||
shift = (long)((ref_offset - offset) * rate + 0.5) / (double)rate;
|
||||
if ((ref_sample.offset - offset) >= 0.0)
|
||||
shift = (long)((ref_sample.offset - offset) * rate + 0.5) / (double)rate;
|
||||
else
|
||||
shift = (long)((ref_offset - offset) * rate - 0.5) / (double)rate;
|
||||
shift = (long)((ref_sample.offset - offset) * rate - 0.5) / (double)rate;
|
||||
|
||||
offset += shift;
|
||||
|
||||
if (fabs(ref_offset - offset) + ref_dispersion + dispersion >= 0.2 / rate) {
|
||||
if (fabs(ref_sample.offset - offset) +
|
||||
ref_sample.root_dispersion + dispersion > PPS_LOCK_LIMIT / rate) {
|
||||
DEBUG_LOG("refclock pulse ignored offdiff=%.9f refdisp=%.9f disp=%.9f",
|
||||
ref_offset - offset, ref_dispersion, dispersion);
|
||||
ref_sample.offset - offset, ref_sample.root_dispersion, dispersion);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!check_pulse_edge(instance, ref_offset - offset, 0.0))
|
||||
if (!check_pulse_edge(instance, ref_sample.offset - offset, 0.0))
|
||||
return 0;
|
||||
|
||||
leap = lock_refclock->leap_status;
|
||||
|
||||
DEBUG_LOG("refclock pulse offset=%.9f offdiff=%.9f samplediff=%.9f",
|
||||
offset, ref_offset - offset, sample_diff);
|
||||
offset, ref_sample.offset - offset, sample_diff);
|
||||
} else {
|
||||
struct timespec ref_time;
|
||||
int is_synchronised, stratum;
|
||||
@@ -519,7 +610,7 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
DEBUG_LOG("refclock pulse ignored offset=%.9f sync=%d dist=%.9f",
|
||||
offset, leap != LEAP_Unsynchronised, distance);
|
||||
/* Drop also all stored samples */
|
||||
filter_reset(&instance->filter);
|
||||
SPF_DropSamples(instance->filter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -527,7 +618,9 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||
return 0;
|
||||
}
|
||||
|
||||
filter_add_sample(&instance->filter, cooked_time, offset, dispersion);
|
||||
if (!accumulate_sample(instance, cooked_time, offset, dispersion))
|
||||
return 0;
|
||||
|
||||
instance->leap_status = leap;
|
||||
instance->pps_active = 1;
|
||||
|
||||
@@ -556,17 +649,13 @@ RCL_GetDriverPoll(RCL_Instance instance)
|
||||
static int
|
||||
valid_sample_time(RCL_Instance instance, struct timespec *sample_time)
|
||||
{
|
||||
struct timespec now, last_sample_time;
|
||||
double diff, last_offset, last_dispersion;
|
||||
struct timespec now;
|
||||
double diff;
|
||||
|
||||
LCL_ReadCookedTime(&now, NULL);
|
||||
diff = UTI_DiffTimespecsToDouble(&now, sample_time);
|
||||
|
||||
if (diff < 0.0 || diff > UTI_Log2ToDouble(instance->poll + 1) ||
|
||||
(filter_get_samples(&instance->filter) > 0 &&
|
||||
filter_get_last_sample(&instance->filter, &last_sample_time,
|
||||
&last_offset, &last_dispersion) &&
|
||||
UTI_CompareTimespecs(&last_sample_time, sample_time) >= 0)) {
|
||||
if (diff < 0.0 || diff > UTI_Log2ToDouble(instance->poll + 1)) {
|
||||
DEBUG_LOG("%s refclock sample time %s not valid age=%.6f",
|
||||
UTI_RefidToString(instance->ref_id),
|
||||
UTI_TimespecToString(sample_time), diff);
|
||||
@@ -610,7 +699,8 @@ pps_stratum(RCL_Instance instance, struct timespec *ts)
|
||||
static void
|
||||
poll_timeout(void *arg)
|
||||
{
|
||||
int poll;
|
||||
NTP_Sample sample;
|
||||
int poll, stratum;
|
||||
|
||||
RCL_Instance inst = (RCL_Instance)arg;
|
||||
|
||||
@@ -623,26 +713,21 @@ poll_timeout(void *arg)
|
||||
}
|
||||
|
||||
if (!(inst->driver->poll && inst->driver_polled < (1 << (inst->poll - inst->driver_poll)))) {
|
||||
double offset, dispersion;
|
||||
struct timespec sample_time;
|
||||
int sample_ok, stratum;
|
||||
|
||||
sample_ok = filter_get_sample(&inst->filter, &sample_time, &offset, &dispersion);
|
||||
inst->driver_polled = 0;
|
||||
|
||||
if (sample_ok) {
|
||||
if (SPF_GetFilteredSample(inst->filter, &sample)) {
|
||||
/* Handle special case when PPS is used with the local reference */
|
||||
if (inst->pps_active && inst->lock_ref == -1)
|
||||
/* Handle special case when PPS is used with local stratum */
|
||||
stratum = pps_stratum(inst, &sample_time);
|
||||
stratum = pps_stratum(inst, &sample.time);
|
||||
else
|
||||
stratum = 0;
|
||||
stratum = inst->stratum;
|
||||
|
||||
SRC_UpdateReachability(inst->source, 1);
|
||||
SRC_AccumulateSample(inst->source, &sample_time, offset,
|
||||
inst->delay, dispersion, inst->delay, dispersion, stratum, inst->leap_status);
|
||||
SRC_UpdateStatus(inst->source, stratum, inst->leap_status);
|
||||
SRC_AccumulateSample(inst->source, &sample);
|
||||
SRC_SelectSource(inst->source);
|
||||
|
||||
log_sample(inst, &sample_time, 1, 0, 0.0, offset, dispersion);
|
||||
log_sample(inst, &sample.time, 1, 0, 0.0, sample.offset, sample.peer_dispersion);
|
||||
} else {
|
||||
SRC_UpdateReachability(inst->source, 0);
|
||||
}
|
||||
@@ -659,9 +744,9 @@ slew_samples(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||
|
||||
for (i = 0; i < ARR_GetSize(refclocks); i++) {
|
||||
if (change_type == LCL_ChangeUnknownStep)
|
||||
filter_reset(&get_refclock(i)->filter);
|
||||
SPF_DropSamples(get_refclock(i)->filter);
|
||||
else
|
||||
filter_slew_samples(&get_refclock(i)->filter, cooked, dfreq, doffset);
|
||||
SPF_SlewSamples(get_refclock(i)->filter, cooked, dfreq, doffset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -671,7 +756,7 @@ add_dispersion(double dispersion, void *anything)
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < ARR_GetSize(refclocks); i++)
|
||||
filter_add_dispersion(&get_refclock(i)->filter, dispersion);
|
||||
SPF_AddDispersion(get_refclock(i)->filter, dispersion);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -703,320 +788,3 @@ log_sample(RCL_Instance instance, struct timespec *sample_time, int filtered, in
|
||||
dispersion);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
filter_init(struct MedianFilter *filter, int length, double max_dispersion)
|
||||
{
|
||||
if (length < 1)
|
||||
length = 1;
|
||||
|
||||
filter->length = length;
|
||||
filter->index = -1;
|
||||
filter->used = 0;
|
||||
filter->last = -1;
|
||||
/* set first estimate to system precision */
|
||||
filter->avg_var_n = 0;
|
||||
filter->avg_var = LCL_GetSysPrecisionAsQuantum() * LCL_GetSysPrecisionAsQuantum();
|
||||
filter->max_var = max_dispersion * max_dispersion;
|
||||
filter->samples = MallocArray(struct FilterSample, filter->length);
|
||||
filter->selected = MallocArray(int, filter->length);
|
||||
filter->x_data = MallocArray(double, filter->length);
|
||||
filter->y_data = MallocArray(double, filter->length);
|
||||
filter->w_data = MallocArray(double, filter->length);
|
||||
}
|
||||
|
||||
static void
|
||||
filter_fini(struct MedianFilter *filter)
|
||||
{
|
||||
Free(filter->samples);
|
||||
Free(filter->selected);
|
||||
Free(filter->x_data);
|
||||
Free(filter->y_data);
|
||||
Free(filter->w_data);
|
||||
}
|
||||
|
||||
static void
|
||||
filter_reset(struct MedianFilter *filter)
|
||||
{
|
||||
filter->index = -1;
|
||||
filter->used = 0;
|
||||
}
|
||||
|
||||
static double
|
||||
filter_get_avg_sample_dispersion(struct MedianFilter *filter)
|
||||
{
|
||||
return sqrt(filter->avg_var);
|
||||
}
|
||||
|
||||
static void
|
||||
filter_add_sample(struct MedianFilter *filter, struct timespec *sample_time, double offset, double dispersion)
|
||||
{
|
||||
filter->index++;
|
||||
filter->index %= filter->length;
|
||||
filter->last = filter->index;
|
||||
if (filter->used < filter->length)
|
||||
filter->used++;
|
||||
|
||||
filter->samples[filter->index].sample_time = *sample_time;
|
||||
filter->samples[filter->index].offset = offset;
|
||||
filter->samples[filter->index].dispersion = dispersion;
|
||||
|
||||
DEBUG_LOG("filter sample %d t=%s offset=%.9f dispersion=%.9f",
|
||||
filter->index, UTI_TimespecToString(sample_time), offset, dispersion);
|
||||
}
|
||||
|
||||
static int
|
||||
filter_get_last_sample(struct MedianFilter *filter, struct timespec *sample_time, double *offset, double *dispersion)
|
||||
{
|
||||
if (filter->last < 0)
|
||||
return 0;
|
||||
|
||||
*sample_time = filter->samples[filter->last].sample_time;
|
||||
*offset = filter->samples[filter->last].offset;
|
||||
*dispersion = filter->samples[filter->last].dispersion;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
filter_get_samples(struct MedianFilter *filter)
|
||||
{
|
||||
return filter->used;
|
||||
}
|
||||
|
||||
static const struct FilterSample *tmp_sorted_array;
|
||||
|
||||
static int
|
||||
sample_compare(const void *a, const void *b)
|
||||
{
|
||||
const struct FilterSample *s1, *s2;
|
||||
|
||||
s1 = &tmp_sorted_array[*(int *)a];
|
||||
s2 = &tmp_sorted_array[*(int *)b];
|
||||
|
||||
if (s1->offset < s2->offset)
|
||||
return -1;
|
||||
else if (s1->offset > s2->offset)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
filter_select_samples(struct MedianFilter *filter)
|
||||
{
|
||||
int i, j, k, o, from, to, *selected;
|
||||
double min_dispersion;
|
||||
|
||||
if (filter->used < 1)
|
||||
return 0;
|
||||
|
||||
/* for lengths below 4 require full filter,
|
||||
for 4 and above require at least 4 samples */
|
||||
if ((filter->length < 4 && filter->used != filter->length) ||
|
||||
(filter->length >= 4 && filter->used < 4))
|
||||
return 0;
|
||||
|
||||
selected = filter->selected;
|
||||
|
||||
if (filter->used > 4) {
|
||||
/* select samples with dispersion better than 1.5 * minimum */
|
||||
|
||||
for (i = 1, min_dispersion = filter->samples[0].dispersion; i < filter->used; i++) {
|
||||
if (min_dispersion > filter->samples[i].dispersion)
|
||||
min_dispersion = filter->samples[i].dispersion;
|
||||
}
|
||||
|
||||
for (i = j = 0; i < filter->used; i++) {
|
||||
if (filter->samples[i].dispersion <= 1.5 * min_dispersion)
|
||||
selected[j++] = i;
|
||||
}
|
||||
} else {
|
||||
j = 0;
|
||||
}
|
||||
|
||||
if (j < 4) {
|
||||
/* select all samples */
|
||||
|
||||
for (j = 0; j < filter->used; j++)
|
||||
selected[j] = j;
|
||||
}
|
||||
|
||||
/* and sort their indices by offset */
|
||||
tmp_sorted_array = filter->samples;
|
||||
qsort(selected, j, sizeof (int), sample_compare);
|
||||
|
||||
/* select 60 percent of the samples closest to the median */
|
||||
if (j > 2) {
|
||||
from = j / 5;
|
||||
if (from < 1)
|
||||
from = 1;
|
||||
to = j - from;
|
||||
} else {
|
||||
from = 0;
|
||||
to = j;
|
||||
}
|
||||
|
||||
/* mark unused samples and sort the rest from oldest to newest */
|
||||
|
||||
o = filter->used - filter->index - 1;
|
||||
|
||||
for (i = 0; i < from; i++)
|
||||
selected[i] = -1;
|
||||
for (; i < to; i++)
|
||||
selected[i] = (selected[i] + o) % filter->used;
|
||||
for (; i < filter->used; i++)
|
||||
selected[i] = -1;
|
||||
|
||||
for (i = from; i < to; i++) {
|
||||
j = selected[i];
|
||||
selected[i] = -1;
|
||||
while (j != -1 && selected[j] != j) {
|
||||
k = selected[j];
|
||||
selected[j] = j;
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j = 0, k = -1; i < filter->used; i++) {
|
||||
if (selected[i] != -1)
|
||||
selected[j++] = (selected[i] + filter->used - o) % filter->used;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
static int
|
||||
filter_get_sample(struct MedianFilter *filter, struct timespec *sample_time, double *offset, double *dispersion)
|
||||
{
|
||||
struct FilterSample *s, *ls;
|
||||
int i, n, dof;
|
||||
double x, y, d, e, var, prev_avg_var;
|
||||
|
||||
n = filter_select_samples(filter);
|
||||
|
||||
if (n < 1)
|
||||
return 0;
|
||||
|
||||
ls = &filter->samples[filter->selected[n - 1]];
|
||||
|
||||
/* prepare data */
|
||||
for (i = 0; i < n; i++) {
|
||||
s = &filter->samples[filter->selected[i]];
|
||||
|
||||
filter->x_data[i] = UTI_DiffTimespecsToDouble(&s->sample_time, &ls->sample_time);
|
||||
filter->y_data[i] = s->offset;
|
||||
filter->w_data[i] = s->dispersion;
|
||||
}
|
||||
|
||||
/* mean offset, sample time and sample dispersion */
|
||||
for (i = 0, x = y = e = 0.0; i < n; i++) {
|
||||
x += filter->x_data[i];
|
||||
y += filter->y_data[i];
|
||||
e += filter->w_data[i];
|
||||
}
|
||||
x /= n;
|
||||
y /= n;
|
||||
e /= n;
|
||||
|
||||
if (n >= 4) {
|
||||
double b0, b1, s2, sb0, sb1;
|
||||
|
||||
/* set y axis to the mean sample time */
|
||||
for (i = 0; i < n; i++)
|
||||
filter->x_data[i] -= x;
|
||||
|
||||
/* make a linear fit and use the estimated standard deviation of intercept
|
||||
as dispersion */
|
||||
RGR_WeightedRegression(filter->x_data, filter->y_data, filter->w_data, n,
|
||||
&b0, &b1, &s2, &sb0, &sb1);
|
||||
var = s2;
|
||||
d = sb0;
|
||||
dof = n - 2;
|
||||
} else if (n >= 2) {
|
||||
for (i = 0, d = 0.0; i < n; i++)
|
||||
d += (filter->y_data[i] - y) * (filter->y_data[i] - y);
|
||||
var = d / (n - 1);
|
||||
d = sqrt(var);
|
||||
dof = n - 1;
|
||||
} else {
|
||||
var = filter->avg_var;
|
||||
d = sqrt(var);
|
||||
dof = 1;
|
||||
}
|
||||
|
||||
/* avoid having zero dispersion */
|
||||
if (var < 1e-20) {
|
||||
var = 1e-20;
|
||||
d = sqrt(var);
|
||||
}
|
||||
|
||||
/* drop the sample if variance is larger than allowed maximum */
|
||||
if (filter->max_var > 0.0 && var > filter->max_var) {
|
||||
DEBUG_LOG("filter dispersion too large disp=%.9f max=%.9f",
|
||||
sqrt(var), sqrt(filter->max_var));
|
||||
return 0;
|
||||
}
|
||||
|
||||
prev_avg_var = filter->avg_var;
|
||||
|
||||
/* update exponential moving average of the variance */
|
||||
if (filter->avg_var_n > 50) {
|
||||
filter->avg_var += dof / (dof + 50.0) * (var - filter->avg_var);
|
||||
} else {
|
||||
filter->avg_var = (filter->avg_var * filter->avg_var_n + var * dof) /
|
||||
(dof + filter->avg_var_n);
|
||||
if (filter->avg_var_n == 0)
|
||||
prev_avg_var = filter->avg_var;
|
||||
filter->avg_var_n += dof;
|
||||
}
|
||||
|
||||
/* reduce noise in sourcestats weights by using the long-term average
|
||||
instead of the estimated variance if it's not significantly lower */
|
||||
if (var * dof / RGR_GetChi2Coef(dof) < prev_avg_var)
|
||||
d = sqrt(filter->avg_var) * d / sqrt(var);
|
||||
|
||||
if (d < e)
|
||||
d = e;
|
||||
|
||||
UTI_AddDoubleToTimespec(&ls->sample_time, x, sample_time);
|
||||
*offset = y;
|
||||
*dispersion = d;
|
||||
|
||||
filter_reset(filter);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
filter_slew_samples(struct MedianFilter *filter, struct timespec *when, double dfreq, double doffset)
|
||||
{
|
||||
int i, first, last;
|
||||
double delta_time;
|
||||
struct timespec *sample;
|
||||
|
||||
if (filter->last < 0)
|
||||
return;
|
||||
|
||||
/* always slew the last sample as it may be needed by PPS refclocks */
|
||||
if (filter->used > 0) {
|
||||
first = 0;
|
||||
last = filter->used - 1;
|
||||
} else {
|
||||
first = last = filter->last;
|
||||
}
|
||||
|
||||
for (i = first; i <= last; i++) {
|
||||
sample = &filter->samples[i].sample_time;
|
||||
UTI_AdjustTimespec(sample, when, sample, &delta_time, dfreq, doffset);
|
||||
filter->samples[i].offset -= delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
filter_add_dispersion(struct MedianFilter *filter, double dispersion)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < filter->used; i++) {
|
||||
filter->samples[i].dispersion += dispersion;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ typedef struct {
|
||||
int max_samples;
|
||||
int sel_options;
|
||||
int max_lock_age;
|
||||
int stratum;
|
||||
int tai;
|
||||
uint32_t ref_id;
|
||||
uint32_t lock_ref_id;
|
||||
double offset;
|
||||
@@ -70,6 +72,7 @@ extern void RCL_ReportSource(RPT_SourceReport *report, struct timespec *now);
|
||||
extern void RCL_SetDriverData(RCL_Instance instance, void *data);
|
||||
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_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second);
|
||||
|
||||
@@ -56,17 +56,18 @@ static void read_ext_pulse(int sockfd, int event, void *anything);
|
||||
|
||||
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;
|
||||
char *path, *s;
|
||||
|
||||
RCL_CheckDriverOptions(instance, options);
|
||||
|
||||
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;
|
||||
@@ -80,7 +81,7 @@ 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(UTI_Log2ToDouble(RCL_GetDriverPoll(instance)));
|
||||
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))
|
||||
|
||||
@@ -48,59 +48,49 @@ struct pps_instance {
|
||||
};
|
||||
|
||||
static int pps_initialise(RCL_Instance instance) {
|
||||
const char *options[] = {"clear", NULL};
|
||||
pps_handle_t handle;
|
||||
pps_params_t params;
|
||||
struct pps_instance *pps;
|
||||
int fd, edge_clear, mode;
|
||||
char *path;
|
||||
|
||||
RCL_CheckDriverOptions(instance, options);
|
||||
|
||||
path = RCL_GetDriverParameter(instance);
|
||||
edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;
|
||||
|
||||
fd = open(path, O_RDWR);
|
||||
if (fd < 0) {
|
||||
LOG_FATAL("open() failed on %s", path);
|
||||
return 0;
|
||||
}
|
||||
if (fd < 0)
|
||||
LOG_FATAL("Could not open %s : %s", path, strerror(errno));
|
||||
|
||||
UTI_FdSetCloexec(fd);
|
||||
|
||||
if (time_pps_create(fd, &handle) < 0) {
|
||||
LOG_FATAL("time_pps_create() failed on %s", path);
|
||||
return 0;
|
||||
}
|
||||
if (time_pps_create(fd, &handle) < 0)
|
||||
LOG_FATAL("time_pps_create() failed on %s : %s", path, strerror(errno));
|
||||
|
||||
if (time_pps_getcap(handle, &mode) < 0) {
|
||||
LOG_FATAL("time_pps_getcap() failed on %s", path);
|
||||
return 0;
|
||||
}
|
||||
if (time_pps_getcap(handle, &mode) < 0)
|
||||
LOG_FATAL("time_pps_getcap() failed on %s : %s", path, strerror(errno));
|
||||
|
||||
if (time_pps_getparams(handle, ¶ms) < 0) {
|
||||
LOG_FATAL("time_pps_getparams() failed on %s", path);
|
||||
return 0;
|
||||
}
|
||||
if (time_pps_getparams(handle, ¶ms) < 0)
|
||||
LOG_FATAL("time_pps_getparams() failed on %s : %s", path, strerror(errno));
|
||||
|
||||
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) {
|
||||
LOG_FATAL("time_pps_setparams() failed on %s", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (time_pps_setparams(handle, ¶ms) < 0)
|
||||
LOG_FATAL("time_pps_setparams() failed on %s : %s", path, strerror(errno));
|
||||
|
||||
pps = MallocNew(struct pps_instance);
|
||||
pps->handle = handle;
|
||||
@@ -147,7 +137,7 @@ static int pps_poll(RCL_Instance instance)
|
||||
|
||||
if (seq == pps->last_seq || UTI_IsZeroTimespec(&ts)) {
|
||||
DEBUG_LOG("PPS sample ignored seq=%lu ts=%s",
|
||||
seq, UTI_TimespecToString(&ts));
|
||||
(unsigned long)seq, UTI_TimespecToString(&ts));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,23 +59,26 @@ struct shmTime {
|
||||
};
|
||||
|
||||
static int shm_initialise(RCL_Instance instance) {
|
||||
const char *options[] = {"perm", NULL};
|
||||
int id, param, perm;
|
||||
char *s;
|
||||
struct shmTime *shm;
|
||||
|
||||
RCL_CheckDriverOptions(instance, options);
|
||||
|
||||
param = atoi(RCL_GetDriverParameter(instance));
|
||||
s = RCL_GetDriverOption(instance, "perm");
|
||||
perm = s ? strtol(s, NULL, 8) & 0777 : 0600;
|
||||
|
||||
id = shmget(SHMKEY + param, sizeof (struct shmTime), IPC_CREAT | perm);
|
||||
if (id == -1) {
|
||||
LOG_FATAL("shmget() failed");
|
||||
LOG_FATAL("shmget() failed : %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
shm = (struct shmTime *)shmat(id, 0, 0);
|
||||
if ((long)shm == -1) {
|
||||
LOG_FATAL("shmat() failed");
|
||||
LOG_FATAL("shmat() failed : %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "logging.h"
|
||||
#include "util.h"
|
||||
#include "sched.h"
|
||||
#include "socket.h"
|
||||
|
||||
#define SOCK_MAGIC 0x534f434b
|
||||
|
||||
@@ -69,20 +70,19 @@ static void read_sample(int sockfd, int event, void *anything)
|
||||
s = recv(sockfd, &sample, sizeof (sample), 0);
|
||||
|
||||
if (s < 0) {
|
||||
LOG(LOGS_ERR, "Could not read SOCK sample : %s",
|
||||
strerror(errno));
|
||||
DEBUG_LOG("Could not read SOCK sample : %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
if (s != sizeof (sample)) {
|
||||
LOG(LOGS_WARN, "Unexpected length of SOCK sample : %d != %ld",
|
||||
s, (long)sizeof (sample));
|
||||
DEBUG_LOG("Unexpected length of SOCK sample : %d != %ld",
|
||||
s, (long)sizeof (sample));
|
||||
return;
|
||||
}
|
||||
|
||||
if (sample.magic != SOCK_MAGIC) {
|
||||
LOG(LOGS_WARN, "Unexpected magic number in SOCK sample : %x != %x",
|
||||
sample.magic, SOCK_MAGIC);
|
||||
DEBUG_LOG("Unexpected magic number in SOCK sample : %x != %x",
|
||||
(unsigned int)sample.magic, (unsigned int)SOCK_MAGIC);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,31 +98,16 @@ static void read_sample(int sockfd, int event, void *anything)
|
||||
|
||||
static int sock_initialise(RCL_Instance instance)
|
||||
{
|
||||
struct sockaddr_un s;
|
||||
int sockfd;
|
||||
char *path;
|
||||
|
||||
RCL_CheckDriverOptions(instance, NULL);
|
||||
|
||||
path = RCL_GetDriverParameter(instance);
|
||||
|
||||
s.sun_family = AF_UNIX;
|
||||
if (snprintf(s.sun_path, sizeof (s.sun_path), "%s", path) >= sizeof (s.sun_path)) {
|
||||
LOG_FATAL("path %s is too long", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
LOG_FATAL("socket() failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
UTI_FdSetCloexec(sockfd);
|
||||
|
||||
unlink(path);
|
||||
if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) {
|
||||
LOG_FATAL("bind() failed");
|
||||
return 0;
|
||||
}
|
||||
sockfd = SCK_OpenUnixDatagramSocket(NULL, path, 0);
|
||||
if (sockfd < 0)
|
||||
LOG_FATAL("Could not open socket %s", path);
|
||||
|
||||
RCL_SetDriverData(instance, (void *)(long)sockfd);
|
||||
SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
|
||||
@@ -135,7 +120,8 @@ static void sock_finalise(RCL_Instance instance)
|
||||
|
||||
sockfd = (long)RCL_GetDriverData(instance);
|
||||
SCH_RemoveFileHandler(sockfd);
|
||||
close(sockfd);
|
||||
SCK_RemoveSocket(sockfd);
|
||||
SCK_CloseSocket(sockfd);
|
||||
}
|
||||
|
||||
RefclockDriver RCL_SOCK_driver = {
|
||||
|
||||
443
reference.c
443
reference.c
@@ -3,7 +3,7 @@
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Richard P. Curnow 1997-2003
|
||||
* Copyright (C) Miroslav Lichvar 2009-2017
|
||||
* Copyright (C) Miroslav Lichvar 2009-2018, 2020
|
||||
*
|
||||
* 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
|
||||
@@ -42,11 +42,18 @@
|
||||
/* The minimum allowed skew */
|
||||
#define MIN_SKEW 1.0e-12
|
||||
|
||||
/* The update interval of the reference in the local reference mode */
|
||||
#define LOCAL_REF_UPDATE_INTERVAL 64.0
|
||||
|
||||
/* Interval between updates of the drift file */
|
||||
#define MAX_DRIFTFILE_AGE 3600.0
|
||||
|
||||
static int are_we_synchronised;
|
||||
static int enable_local_stratum;
|
||||
static int local_stratum;
|
||||
static int local_orphan;
|
||||
static double local_distance;
|
||||
static struct timespec local_ref_time;
|
||||
static NTP_Leap our_leap_status;
|
||||
static int our_leap_sec;
|
||||
static int our_tai_offset;
|
||||
@@ -58,6 +65,8 @@ static double our_skew;
|
||||
static double our_residual_freq;
|
||||
static double our_root_delay;
|
||||
static double our_root_dispersion;
|
||||
static double our_offset_sd;
|
||||
static double our_frequency_sd;
|
||||
|
||||
static double max_update_skew;
|
||||
|
||||
@@ -103,6 +112,9 @@ static void update_drift_file(double, double);
|
||||
/* Leap second handling mode */
|
||||
static REF_LeapMode leap_mode;
|
||||
|
||||
/* Time of UTC midnight of the upcoming or previous leap second */
|
||||
static time_t leap_when;
|
||||
|
||||
/* Flag indicating the clock was recently corrected for leap second and it may
|
||||
not have correct time yet (missing 23:59:60 in the UTC time scale) */
|
||||
static int leap_in_progress;
|
||||
@@ -134,8 +146,8 @@ static struct fb_drift *fb_drifts = NULL;
|
||||
static int next_fb_drift;
|
||||
static SCH_TimeoutID fb_drift_timeout_id;
|
||||
|
||||
/* Timestamp of last reference update */
|
||||
static struct timespec last_ref_update;
|
||||
/* Monotonic timestamp of the last reference update */
|
||||
static double last_ref_update;
|
||||
static double last_ref_update_interval;
|
||||
|
||||
/* ================================================== */
|
||||
@@ -160,9 +172,8 @@ handle_slew(struct timespec *raw,
|
||||
UTI_AdjustTimespec(&our_ref_time, cooked, &our_ref_time, &delta, dfreq, doffset);
|
||||
|
||||
if (change_type == LCL_ChangeUnknownStep) {
|
||||
UTI_ZeroTimespec(&last_ref_update);
|
||||
} else if (last_ref_update.tv_sec) {
|
||||
UTI_AdjustTimespec(&last_ref_update, cooked, &last_ref_update, &delta, dfreq, doffset);
|
||||
last_ref_update = 0.0;
|
||||
REF_SetUnsynchronised();
|
||||
}
|
||||
|
||||
/* When the clock was stepped, check if that doesn't change our leap status
|
||||
@@ -194,12 +205,14 @@ REF_Initialise(void)
|
||||
our_frequency_ppm = 0.0;
|
||||
our_skew = 1.0; /* i.e. rather bad */
|
||||
our_residual_freq = 0.0;
|
||||
our_frequency_sd = 0.0;
|
||||
our_offset_sd = 0.0;
|
||||
drift_file_age = 0.0;
|
||||
|
||||
/* Now see if we can get the drift file opened */
|
||||
drift_file = CNF_GetDriftFile();
|
||||
if (drift_file) {
|
||||
in = fopen(drift_file, "r");
|
||||
in = UTI_OpenFile(NULL, drift_file, NULL, 'r', 0);
|
||||
if (in) {
|
||||
if (fscanf(in, "%lf%lf", &file_freq_ppm, &file_skew_ppm) == 2) {
|
||||
/* We have read valid data */
|
||||
@@ -234,7 +247,9 @@ REF_Initialise(void)
|
||||
correction_time_ratio = CNF_GetCorrectionTimeRatio();
|
||||
|
||||
enable_local_stratum = CNF_AllowLocalReference(&local_stratum, &local_orphan, &local_distance);
|
||||
UTI_ZeroTimespec(&local_ref_time);
|
||||
|
||||
leap_when = 0;
|
||||
leap_timeout_id = 0;
|
||||
leap_in_progress = 0;
|
||||
leap_mode = CNF_GetLeapSecMode();
|
||||
@@ -269,7 +284,7 @@ REF_Initialise(void)
|
||||
}
|
||||
|
||||
UTI_ZeroTimespec(&our_ref_time);
|
||||
UTI_ZeroTimespec(&last_ref_update);
|
||||
last_ref_update = 0.0;
|
||||
last_ref_update_interval = 0.0;
|
||||
|
||||
LCL_AddParameterChangeHandler(handle_slew, NULL);
|
||||
@@ -289,6 +304,8 @@ REF_Finalise(void)
|
||||
update_drift_file(LCL_ReadAbsoluteFrequency(), our_skew);
|
||||
}
|
||||
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, NULL);
|
||||
|
||||
Free(fb_drifts);
|
||||
|
||||
initialised = 0;
|
||||
@@ -325,84 +342,26 @@ REF_GetLeapMode(void)
|
||||
return leap_mode;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static double
|
||||
Sqr(double x)
|
||||
{
|
||||
return x*x;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
#if 0
|
||||
static double
|
||||
Cube(double x)
|
||||
{
|
||||
return x*x*x;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ================================================== */
|
||||
/* Update the drift coefficients to the file. */
|
||||
|
||||
static void
|
||||
update_drift_file(double freq_ppm, double skew)
|
||||
{
|
||||
struct stat buf;
|
||||
char *temp_drift_file;
|
||||
FILE *out;
|
||||
int r1, r2;
|
||||
|
||||
/* Create a temporary file with a '.tmp' extension. */
|
||||
|
||||
temp_drift_file = (char*) Malloc(strlen(drift_file)+8);
|
||||
|
||||
if(!temp_drift_file) {
|
||||
out = UTI_OpenFile(NULL, drift_file, ".tmp", 'w', 0644);
|
||||
if (!out)
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(temp_drift_file,drift_file);
|
||||
strcat(temp_drift_file,".tmp");
|
||||
|
||||
out = fopen(temp_drift_file, "w");
|
||||
if (!out) {
|
||||
Free(temp_drift_file);
|
||||
LOG(LOGS_WARN, "Could not open temporary driftfile %s.tmp for writing",
|
||||
drift_file);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Write the frequency and skew parameters in ppm */
|
||||
r1 = fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
|
||||
r2 = fclose(out);
|
||||
if (r1 < 0 || r2) {
|
||||
Free(temp_drift_file);
|
||||
LOG(LOGS_WARN, "Could not write to temporary driftfile %s.tmp",
|
||||
drift_file);
|
||||
return;
|
||||
}
|
||||
fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
|
||||
fclose(out);
|
||||
|
||||
/* Clone the file attributes from the existing file if there is one. */
|
||||
|
||||
if (!stat(drift_file,&buf)) {
|
||||
if (chown(temp_drift_file,buf.st_uid,buf.st_gid) ||
|
||||
chmod(temp_drift_file,buf.st_mode & 0777)) {
|
||||
LOG(LOGS_WARN, "Could not change ownership or permissions of temporary driftfile %s.tmp",
|
||||
drift_file);
|
||||
}
|
||||
}
|
||||
|
||||
/* Rename the temporary file to the correct location (see rename(2) for details). */
|
||||
|
||||
if (rename(temp_drift_file,drift_file)) {
|
||||
unlink(temp_drift_file);
|
||||
Free(temp_drift_file);
|
||||
LOG(LOGS_WARN, "Could not replace old driftfile %s with new one %s.tmp",
|
||||
drift_file,drift_file);
|
||||
return;
|
||||
}
|
||||
|
||||
Free(temp_drift_file);
|
||||
/* Rename the temporary file to the correct location */
|
||||
if (!UTI_RenameTempFile(NULL, drift_file, ".tmp", NULL))
|
||||
;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -468,16 +427,16 @@ fb_drift_timeout(void *arg)
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
schedule_fb_drift(struct timespec *now)
|
||||
schedule_fb_drift(void)
|
||||
{
|
||||
int i, c, secs;
|
||||
double unsynchronised;
|
||||
struct timespec when;
|
||||
double unsynchronised, now;
|
||||
|
||||
if (fb_drift_timeout_id)
|
||||
return; /* already scheduled */
|
||||
|
||||
unsynchronised = UTI_DiffTimespecsToDouble(now, &last_ref_update);
|
||||
now = SCH_GetLastEventMonoTime();
|
||||
unsynchronised = now - last_ref_update;
|
||||
|
||||
for (c = secs = 0, i = fb_drift_min; i <= fb_drift_max; i++) {
|
||||
secs = 1 << i;
|
||||
@@ -499,8 +458,7 @@ schedule_fb_drift(struct timespec *now)
|
||||
|
||||
if (i <= fb_drift_max) {
|
||||
next_fb_drift = i;
|
||||
UTI_AddDoubleToTimespec(now, secs - unsynchronised, &when);
|
||||
fb_drift_timeout_id = SCH_AddTimeout(&when, fb_drift_timeout, NULL);
|
||||
fb_drift_timeout_id = SCH_AddTimeoutByDelay(secs - unsynchronised, fb_drift_timeout, NULL);
|
||||
DEBUG_LOG("Fallback drift %d scheduled", i);
|
||||
}
|
||||
}
|
||||
@@ -528,28 +486,35 @@ maybe_log_offset(double offset, time_t now)
|
||||
double abs_offset;
|
||||
FILE *p;
|
||||
char buffer[BUFLEN], host[BUFLEN];
|
||||
struct tm stm;
|
||||
struct tm *tm;
|
||||
|
||||
abs_offset = fabs(offset);
|
||||
|
||||
if (abs_offset > log_change_threshold) {
|
||||
LOG(LOGS_WARN, "System clock wrong by %.6f seconds, adjustment started",
|
||||
-offset);
|
||||
LOG(LOGS_WARN, "System clock wrong by %.6f seconds", -offset);
|
||||
}
|
||||
|
||||
if (do_mail_change &&
|
||||
(abs_offset > mail_change_threshold)) {
|
||||
snprintf(buffer, sizeof(buffer), "%s %." S_MAX_USER_LEN "s", MAIL_PROGRAM, mail_change_user);
|
||||
snprintf(buffer, sizeof (buffer), "%s -t", MAIL_PROGRAM);
|
||||
p = popen(buffer, "w");
|
||||
if (p) {
|
||||
if (gethostname(host, sizeof(host)) < 0) {
|
||||
strcpy(host, "<UNKNOWN>");
|
||||
}
|
||||
host[sizeof (host) - 1] = '\0';
|
||||
|
||||
fprintf(p, "To: %s\n", mail_change_user);
|
||||
fprintf(p, "Subject: chronyd reports change to system clock on node [%s]\n", host);
|
||||
fputs("\n", p);
|
||||
stm = *localtime(&now);
|
||||
strftime(buffer, sizeof(buffer), "On %A, %d %B %Y\n with the system clock reading %H:%M:%S (%Z)", &stm);
|
||||
fputs(buffer, p);
|
||||
|
||||
tm = localtime(&now);
|
||||
if (tm) {
|
||||
strftime(buffer, sizeof (buffer),
|
||||
"On %A, %d %B %Y\n with the system clock reading %H:%M:%S (%Z)", tm);
|
||||
fputs(buffer, p);
|
||||
}
|
||||
|
||||
/* If offset < 0 the local clock is slow, so we are applying a
|
||||
positive change to it to bring it into line, hence the
|
||||
negation of 'offset' in the next statement (and earlier) */
|
||||
@@ -592,8 +557,7 @@ is_offset_ok(double offset)
|
||||
return 1;
|
||||
}
|
||||
|
||||
offset = fabs(offset);
|
||||
if (offset > max_offset) {
|
||||
if (fabs(offset) > max_offset) {
|
||||
LOG(LOGS_WARN,
|
||||
"Adjustment of %.3f seconds exceeds the allowed maximum of %.3f seconds (%s) ",
|
||||
-offset, max_offset, !max_offset_ignore ? "exiting" : "ignored");
|
||||
@@ -760,10 +724,12 @@ set_leap_timeout(time_t now)
|
||||
if (!our_leap_sec)
|
||||
return;
|
||||
|
||||
leap_when = (now / (24 * 3600) + 1) * (24 * 3600);
|
||||
|
||||
/* Insert leap second at 0:00:00 UTC, delete at 23:59:59 UTC. If the clock
|
||||
will be corrected by the system, timeout slightly sooner to be sure it
|
||||
will happen before the system correction. */
|
||||
when.tv_sec = (now / (24 * 3600) + 1) * (24 * 3600);
|
||||
when.tv_sec = leap_when;
|
||||
when.tv_nsec = 0;
|
||||
if (our_leap_sec < 0)
|
||||
when.tv_sec--;
|
||||
@@ -807,7 +773,7 @@ update_leap_status(NTP_Leap leap, time_t now, int reset)
|
||||
}
|
||||
|
||||
if ((leap_sec != our_leap_sec || tai_offset != our_tai_offset)
|
||||
&& !REF_IsLeapSecondClose()) {
|
||||
&& !REF_IsLeapSecondClose(NULL, 0.0)) {
|
||||
our_leap_sec = leap_sec;
|
||||
our_tai_offset = tai_offset;
|
||||
|
||||
@@ -846,6 +812,20 @@ get_root_dispersion(struct timespec *ts)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_sync_status(struct timespec *now)
|
||||
{
|
||||
double elapsed;
|
||||
|
||||
elapsed = fabs(UTI_DiffTimespecsToDouble(now, &our_ref_time));
|
||||
|
||||
LCL_SetSyncStatus(are_we_synchronised,
|
||||
our_offset_sd + elapsed * our_frequency_sd,
|
||||
our_root_delay / 2.0 + get_root_dispersion(now));
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
write_log(struct timespec *now, int combined_sources, double freq,
|
||||
double offset, double offset_sd, double uncorrected_offset,
|
||||
@@ -929,34 +909,69 @@ special_mode_sync(int valid, double offset)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_SetReference(int stratum,
|
||||
NTP_Leap leap,
|
||||
int combined_sources,
|
||||
uint32_t ref_id,
|
||||
IPAddr *ref_ip,
|
||||
struct timespec *ref_time,
|
||||
double offset,
|
||||
double offset_sd,
|
||||
double frequency,
|
||||
double skew,
|
||||
double root_delay,
|
||||
double root_dispersion
|
||||
)
|
||||
static void
|
||||
get_clock_estimates(int manual,
|
||||
double measured_freq, double measured_skew,
|
||||
double *estimated_freq, double *estimated_skew,
|
||||
double *residual_freq)
|
||||
{
|
||||
double gain, expected_freq, expected_skew, extra_skew;
|
||||
|
||||
/* We assume that the local clock is running according to our previously
|
||||
determined value */
|
||||
expected_freq = 0.0;
|
||||
expected_skew = our_skew;
|
||||
|
||||
/* Set new frequency based on weighted average of the expected and measured
|
||||
skew. Disable updates that are based on totally unreliable frequency
|
||||
information unless it is a manual reference. */
|
||||
if (manual) {
|
||||
gain = 1.0;
|
||||
} else if (fabs(measured_skew) > max_update_skew) {
|
||||
DEBUG_LOG("Skew %f too large to track", measured_skew);
|
||||
gain = 0.0;
|
||||
} else {
|
||||
gain = 3.0 * SQUARE(expected_skew) /
|
||||
(3.0 * SQUARE(expected_skew) + SQUARE(measured_skew));
|
||||
}
|
||||
|
||||
gain = CLAMP(0.0, gain, 1.0);
|
||||
|
||||
*estimated_freq = expected_freq + gain * (measured_freq - expected_freq);
|
||||
*residual_freq = measured_freq - *estimated_freq;
|
||||
|
||||
extra_skew = sqrt(SQUARE(expected_freq - *estimated_freq) * (1.0 - gain) +
|
||||
SQUARE(measured_freq - *estimated_freq) * gain);
|
||||
|
||||
*estimated_skew = expected_skew + gain * (measured_skew - expected_skew) + extra_skew;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
fuzz_ref_time(struct timespec *ts)
|
||||
{
|
||||
uint32_t rnd;
|
||||
|
||||
/* Add a random value from interval [-1.0, 0.0] */
|
||||
UTI_GetRandomBytes(&rnd, sizeof (rnd));
|
||||
UTI_AddDoubleToTimespec(ts, -(double)rnd / (uint32_t)-1, ts);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_SetReference(int stratum, NTP_Leap leap, int combined_sources,
|
||||
uint32_t ref_id, IPAddr *ref_ip, struct timespec *ref_time,
|
||||
double offset, double offset_sd,
|
||||
double frequency, double frequency_sd, double skew,
|
||||
double root_delay, double root_dispersion)
|
||||
{
|
||||
double previous_skew, new_skew;
|
||||
double previous_freq, new_freq;
|
||||
double old_weight, new_weight, sum_weight;
|
||||
double delta_freq1, delta_freq2;
|
||||
double skew1, skew2;
|
||||
double our_offset;
|
||||
double our_frequency;
|
||||
double abs_freq_ppm;
|
||||
double update_interval;
|
||||
double elapsed, correction_rate, orig_root_distance;
|
||||
double uncorrected_offset, accumulate_offset, step_offset;
|
||||
double residual_frequency, local_abs_frequency;
|
||||
double elapsed, mono_now, update_interval, correction_rate, orig_root_distance;
|
||||
struct timespec now, raw_now;
|
||||
NTP_int64 ref_fuzz;
|
||||
int manual;
|
||||
|
||||
assert(initialised);
|
||||
|
||||
@@ -966,23 +981,32 @@ REF_SetReference(int stratum,
|
||||
return;
|
||||
}
|
||||
|
||||
/* Guard against dividing by zero and NaN */
|
||||
if (!(skew > MIN_SKEW))
|
||||
skew = MIN_SKEW;
|
||||
manual = leap == LEAP_Unsynchronised;
|
||||
|
||||
mono_now = SCH_GetLastEventMonoTime();
|
||||
LCL_ReadRawTime(&raw_now);
|
||||
LCL_GetOffsetCorrection(&raw_now, &uncorrected_offset, NULL);
|
||||
UTI_AddDoubleToTimespec(&raw_now, uncorrected_offset, &now);
|
||||
|
||||
elapsed = UTI_DiffTimespecsToDouble(&now, ref_time);
|
||||
our_offset = offset + elapsed * frequency;
|
||||
offset += elapsed * frequency;
|
||||
|
||||
if (!is_offset_ok(our_offset))
|
||||
if (last_ref_update != 0.0) {
|
||||
update_interval = mono_now - last_ref_update;
|
||||
} else {
|
||||
update_interval = 0.0;
|
||||
}
|
||||
|
||||
/* Get new estimates of the frequency and skew including the new data */
|
||||
get_clock_estimates(manual, frequency, skew,
|
||||
&frequency, &skew, &residual_frequency);
|
||||
|
||||
if (!is_offset_ok(offset))
|
||||
return;
|
||||
|
||||
orig_root_distance = our_root_delay / 2.0 + get_root_dispersion(&now);
|
||||
|
||||
are_we_synchronised = leap != LEAP_Unsynchronised ? 1 : 0;
|
||||
are_we_synchronised = leap != LEAP_Unsynchronised;
|
||||
our_stratum = stratum + 1;
|
||||
our_ref_id = ref_id;
|
||||
if (ref_ip)
|
||||
@@ -990,17 +1014,15 @@ REF_SetReference(int stratum,
|
||||
else
|
||||
our_ref_ip.family = IPADDR_UNSPEC;
|
||||
our_ref_time = *ref_time;
|
||||
our_skew = skew;
|
||||
our_residual_freq = residual_frequency;
|
||||
our_root_delay = root_delay;
|
||||
our_root_dispersion = root_dispersion;
|
||||
|
||||
if (last_ref_update.tv_sec) {
|
||||
update_interval = UTI_DiffTimespecsToDouble(&now, &last_ref_update);
|
||||
if (update_interval < 0.0)
|
||||
update_interval = 0.0;
|
||||
} else {
|
||||
update_interval = 0.0;
|
||||
}
|
||||
last_ref_update = now;
|
||||
our_frequency_sd = frequency_sd;
|
||||
our_offset_sd = offset_sd;
|
||||
last_ref_update = mono_now;
|
||||
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.
|
||||
@@ -1018,108 +1040,60 @@ REF_SetReference(int stratum,
|
||||
correction_rate = correction_time_ratio * 0.5 * offset_sd * update_interval;
|
||||
|
||||
/* Check if the clock should be stepped */
|
||||
if (is_step_limit_reached(our_offset, uncorrected_offset)) {
|
||||
if (is_step_limit_reached(offset, uncorrected_offset)) {
|
||||
/* Cancel the uncorrected offset and correct the total offset by step */
|
||||
accumulate_offset = uncorrected_offset;
|
||||
step_offset = our_offset - uncorrected_offset;
|
||||
step_offset = offset - uncorrected_offset;
|
||||
} else {
|
||||
accumulate_offset = our_offset;
|
||||
accumulate_offset = offset;
|
||||
step_offset = 0.0;
|
||||
}
|
||||
|
||||
/* Eliminate updates that are based on totally unreliable frequency
|
||||
information. Ignore this limit with manual reference. */
|
||||
|
||||
if (fabs(skew) < max_update_skew || leap == LEAP_Unsynchronised) {
|
||||
|
||||
previous_skew = our_skew;
|
||||
new_skew = skew;
|
||||
|
||||
previous_freq = 0.0; /* We assume that the local clock is running
|
||||
according to our previously determined
|
||||
value; note that this is a delta frequency
|
||||
--- absolute frequencies are only known in
|
||||
the local module. */
|
||||
new_freq = frequency;
|
||||
|
||||
/* Set new frequency based on weighted average of old and new skew. With
|
||||
manual reference the old frequency has no weight. */
|
||||
|
||||
old_weight = leap != LEAP_Unsynchronised ? 1.0 / Sqr(previous_skew) : 0.0;
|
||||
new_weight = 3.0 / Sqr(new_skew);
|
||||
|
||||
sum_weight = old_weight + new_weight;
|
||||
|
||||
our_frequency = (previous_freq * old_weight + new_freq * new_weight) / sum_weight;
|
||||
|
||||
delta_freq1 = previous_freq - our_frequency;
|
||||
delta_freq2 = new_freq - our_frequency;
|
||||
|
||||
skew1 = sqrt((Sqr(delta_freq1) * old_weight + Sqr(delta_freq2) * new_weight) / sum_weight);
|
||||
skew2 = (previous_skew * old_weight + new_skew * new_weight) / sum_weight;
|
||||
our_skew = skew1 + skew2;
|
||||
|
||||
our_residual_freq = new_freq - our_frequency;
|
||||
|
||||
LCL_AccumulateFrequencyAndOffset(our_frequency, accumulate_offset, correction_rate);
|
||||
/* Adjust the clock */
|
||||
LCL_AccumulateFrequencyAndOffset(frequency, accumulate_offset, correction_rate);
|
||||
|
||||
} else {
|
||||
DEBUG_LOG("Skew %f too large to track, offset=%f", skew, accumulate_offset);
|
||||
|
||||
LCL_AccumulateOffset(accumulate_offset, correction_rate);
|
||||
|
||||
our_residual_freq = frequency;
|
||||
}
|
||||
|
||||
update_leap_status(leap, raw_now.tv_sec, 0);
|
||||
maybe_log_offset(our_offset, raw_now.tv_sec);
|
||||
maybe_log_offset(offset, raw_now.tv_sec);
|
||||
|
||||
if (step_offset != 0.0) {
|
||||
if (LCL_ApplyStepOffset(step_offset))
|
||||
LOG(LOGS_WARN, "System clock was stepped by %.6f seconds", -step_offset);
|
||||
}
|
||||
|
||||
LCL_SetSyncStatus(are_we_synchronised, offset_sd, offset_sd + root_delay / 2.0 + root_dispersion);
|
||||
update_leap_status(leap, raw_now.tv_sec, 0);
|
||||
update_sync_status(&now);
|
||||
|
||||
/* Add a random error of up to one second to the reference time to make it
|
||||
less useful when disclosed to NTP and cmdmon clients for estimating
|
||||
receive timestamps in the interleaved symmetric NTP mode */
|
||||
UTI_GetNtp64Fuzz(&ref_fuzz, 0);
|
||||
UTI_TimespecToNtp64(&our_ref_time, &ref_fuzz, &ref_fuzz);
|
||||
UTI_Ntp64ToTimespec(&ref_fuzz, &our_ref_time);
|
||||
if (UTI_CompareTimespecs(&our_ref_time, ref_time) >= 0)
|
||||
our_ref_time.tv_sec--;
|
||||
fuzz_ref_time(&our_ref_time);
|
||||
|
||||
abs_freq_ppm = LCL_ReadAbsoluteFrequency();
|
||||
local_abs_frequency = LCL_ReadAbsoluteFrequency();
|
||||
|
||||
write_log(&now, combined_sources, abs_freq_ppm, our_offset, offset_sd,
|
||||
uncorrected_offset, orig_root_distance);
|
||||
write_log(&now, combined_sources, local_abs_frequency,
|
||||
offset, offset_sd, uncorrected_offset, orig_root_distance);
|
||||
|
||||
if (drift_file) {
|
||||
/* Update drift file at most once per hour */
|
||||
drift_file_age += update_interval;
|
||||
if (drift_file_age < 0.0 || drift_file_age > 3600.0) {
|
||||
update_drift_file(abs_freq_ppm, our_skew);
|
||||
if (drift_file_age >= MAX_DRIFTFILE_AGE) {
|
||||
update_drift_file(local_abs_frequency, our_skew);
|
||||
drift_file_age = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update fallback drifts */
|
||||
if (fb_drifts && are_we_synchronised) {
|
||||
update_fb_drifts(abs_freq_ppm, update_interval);
|
||||
schedule_fb_drift(&now);
|
||||
update_fb_drifts(local_abs_frequency, update_interval);
|
||||
schedule_fb_drift();
|
||||
}
|
||||
|
||||
last_ref_update_interval = update_interval;
|
||||
last_offset = our_offset;
|
||||
|
||||
/* Update the moving average of squares of offset, quickly on start */
|
||||
if (avg2_moving) {
|
||||
avg2_offset += 0.1 * (our_offset * our_offset - avg2_offset);
|
||||
avg2_offset += 0.1 * (SQUARE(offset) - avg2_offset);
|
||||
} else {
|
||||
if (avg2_offset > 0.0 && avg2_offset < our_offset * our_offset)
|
||||
if (avg2_offset > 0.0 && avg2_offset < SQUARE(offset))
|
||||
avg2_moving = 1;
|
||||
avg2_offset = our_offset * our_offset;
|
||||
avg2_offset = SQUARE(offset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1138,7 +1112,7 @@ REF_SetManualReference
|
||||
only supposed to be used with the local source option, really.
|
||||
Log as MANU in the tracking log, packets will have NTP_REFID_LOCAL. */
|
||||
REF_SetReference(0, LEAP_Unsynchronised, 1, 0x4D414E55UL, NULL,
|
||||
ref_time, offset, 0.0, frequency, skew, 0.0, 0.0);
|
||||
ref_time, offset, 0.0, frequency, skew, skew, 0.0, 0.0);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -1163,7 +1137,7 @@ REF_SetUnsynchronised(void)
|
||||
UTI_AddDoubleToTimespec(&now_raw, uncorrected_offset, &now);
|
||||
|
||||
if (fb_drifts) {
|
||||
schedule_fb_drift(&now);
|
||||
schedule_fb_drift();
|
||||
}
|
||||
|
||||
update_leap_status(LEAP_Unsynchronised, 0, 0);
|
||||
@@ -1180,6 +1154,25 @@ REF_SetUnsynchronised(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_UpdateLeapStatus(NTP_Leap leap)
|
||||
{
|
||||
struct timespec raw_now, now;
|
||||
|
||||
/* Wait for a full reference update if not already synchronised */
|
||||
if (!are_we_synchronised)
|
||||
return;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, &raw_now);
|
||||
|
||||
update_leap_status(leap, raw_now.tv_sec, 0);
|
||||
|
||||
/* Update also the synchronisation status */
|
||||
update_sync_status(&now);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_GetReferenceParams
|
||||
(
|
||||
@@ -1193,7 +1186,7 @@ REF_GetReferenceParams
|
||||
double *root_dispersion
|
||||
)
|
||||
{
|
||||
double dispersion;
|
||||
double dispersion, delta;
|
||||
|
||||
assert(initialised);
|
||||
|
||||
@@ -1225,13 +1218,17 @@ REF_GetReferenceParams
|
||||
|
||||
*stratum = local_stratum;
|
||||
*ref_id = NTP_REFID_LOCAL;
|
||||
/* Make the reference time be now less a second - this will
|
||||
scarcely affect the client, but will ensure that the transmit
|
||||
timestamp cannot come before this (which would cause test 7 to
|
||||
fail in the client's read routine) if the local system clock's
|
||||
read routine is broken in any way. */
|
||||
*ref_time = *local_time;
|
||||
--ref_time->tv_sec;
|
||||
|
||||
/* Keep the reference timestamp up to date. Adjust the timestamp to make
|
||||
sure that the transmit timestamp cannot come before this (which might
|
||||
fail a test of an NTP client). */
|
||||
delta = UTI_DiffTimespecsToDouble(local_time, &local_ref_time);
|
||||
if (delta > LOCAL_REF_UPDATE_INTERVAL || delta < 1.0) {
|
||||
UTI_AddDoubleToTimespec(local_time, -1.0, &local_ref_time);
|
||||
fuzz_ref_time(&local_ref_time);
|
||||
}
|
||||
|
||||
*ref_time = local_ref_time;
|
||||
|
||||
/* Not much else we can do for leap second bits - maybe need to
|
||||
have a way for the administrator to feed leap bits in */
|
||||
@@ -1333,22 +1330,24 @@ REF_DisableLocal(void)
|
||||
|
||||
#define LEAP_SECOND_CLOSE 5
|
||||
|
||||
int REF_IsLeapSecondClose(void)
|
||||
static int
|
||||
is_leap_close(time_t t)
|
||||
{
|
||||
return t >= leap_when - LEAP_SECOND_CLOSE && t < leap_when + LEAP_SECOND_CLOSE;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int REF_IsLeapSecondClose(struct timespec *ts, double offset)
|
||||
{
|
||||
struct timespec now, now_raw;
|
||||
time_t t;
|
||||
|
||||
if (!our_leap_sec)
|
||||
return 0;
|
||||
|
||||
SCH_GetLastEventTime(&now, NULL, &now_raw);
|
||||
|
||||
t = now.tv_sec > 0 ? now.tv_sec : -now.tv_sec;
|
||||
if ((t + LEAP_SECOND_CLOSE) % (24 * 3600) < 2 * LEAP_SECOND_CLOSE)
|
||||
if (is_leap_close(now.tv_sec) || is_leap_close(now_raw.tv_sec))
|
||||
return 1;
|
||||
|
||||
t = now_raw.tv_sec > 0 ? now_raw.tv_sec : -now_raw.tv_sec;
|
||||
if ((t + LEAP_SECOND_CLOSE) % (24 * 3600) < 2 * LEAP_SECOND_CLOSE)
|
||||
if (ts && (is_leap_close(ts->tv_sec) || is_leap_close(ts->tv_sec + offset)))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
@@ -1356,6 +1355,18 @@ int REF_IsLeapSecondClose(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
REF_GetTaiOffset(struct timespec *ts)
|
||||
{
|
||||
int tai_offset;
|
||||
|
||||
get_tz_leap(ts->tv_sec, &tai_offset);
|
||||
|
||||
return tai_offset;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
REF_GetTrackingReport(RPT_TrackingReport *rep)
|
||||
{
|
||||
|
||||
13
reference.h
13
reference.h
@@ -144,6 +144,7 @@ extern void REF_SetReference
|
||||
double offset,
|
||||
double offset_sd,
|
||||
double frequency,
|
||||
double frequency_sd,
|
||||
double skew,
|
||||
double root_delay,
|
||||
double root_dispersion
|
||||
@@ -161,6 +162,9 @@ extern void REF_SetManualReference
|
||||
extern void
|
||||
REF_SetUnsynchronised(void);
|
||||
|
||||
/* Announce a leap second before the full reference update */
|
||||
extern void REF_UpdateLeapStatus(NTP_Leap leap);
|
||||
|
||||
/* Return the current stratum of this host or 16 if the host is not
|
||||
synchronised */
|
||||
extern int REF_GetOurStratum(void);
|
||||
@@ -180,9 +184,12 @@ extern void REF_ModifyMakestep(int limit, double threshold);
|
||||
extern void REF_EnableLocal(int stratum, double distance, int orphan);
|
||||
extern void REF_DisableLocal(void);
|
||||
|
||||
/* Check if current raw or cooked time is close to a leap second
|
||||
and is better to discard any measurements */
|
||||
extern int REF_IsLeapSecondClose(void);
|
||||
/* Check if either of the current raw and cooked time, and optionally a
|
||||
provided timestamp with an offset, is close to a leap second */
|
||||
extern int REF_IsLeapSecondClose(struct timespec *ts, double offset);
|
||||
|
||||
/* Return TAI-UTC offset corresponding to a time in UTC if available */
|
||||
extern int REF_GetTaiOffset(struct timespec *ts);
|
||||
|
||||
extern void REF_GetTrackingReport(RPT_TrackingReport *rep);
|
||||
|
||||
|
||||
47
reports.h
47
reports.h
@@ -36,8 +36,14 @@ typedef struct {
|
||||
int stratum;
|
||||
int poll;
|
||||
enum {RPT_NTP_CLIENT, RPT_NTP_PEER, RPT_LOCAL_REFERENCE} mode;
|
||||
enum {RPT_SYNC, RPT_UNREACH, RPT_FALSETICKER, RPT_JITTERY, RPT_CANDIDATE, RPT_OUTLIER} state;
|
||||
int sel_options;
|
||||
enum {
|
||||
RPT_NONSELECTABLE,
|
||||
RPT_FALSETICKER,
|
||||
RPT_JITTERY,
|
||||
RPT_SELECTABLE,
|
||||
RPT_UNSELECTED,
|
||||
RPT_SELECTED,
|
||||
} state;
|
||||
|
||||
int reachability;
|
||||
unsigned long latest_meas_ago; /* seconds */
|
||||
@@ -78,8 +84,8 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
struct timespec ref_time;
|
||||
unsigned short n_samples;
|
||||
unsigned short n_runs;
|
||||
unsigned long n_samples;
|
||||
unsigned long n_runs;
|
||||
unsigned long span_seconds;
|
||||
double rtc_seconds_fast;
|
||||
double rtc_gain_rate_ppm;
|
||||
@@ -88,22 +94,29 @@ typedef struct {
|
||||
typedef struct {
|
||||
IPAddr ip_addr;
|
||||
uint32_t ntp_hits;
|
||||
uint32_t nke_hits;
|
||||
uint32_t cmd_hits;
|
||||
uint16_t ntp_drops;
|
||||
uint16_t nke_drops;
|
||||
uint16_t cmd_drops;
|
||||
int8_t ntp_interval;
|
||||
int8_t nke_interval;
|
||||
int8_t cmd_interval;
|
||||
int8_t ntp_timeout_interval;
|
||||
uint32_t last_ntp_hit_ago;
|
||||
uint32_t last_nke_hit_ago;
|
||||
uint32_t last_cmd_hit_ago;
|
||||
} 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;
|
||||
} RPT_ServerStatsReport;
|
||||
|
||||
typedef struct {
|
||||
@@ -160,4 +173,30 @@ typedef struct {
|
||||
uint32_t total_valid_count;
|
||||
} RPT_NTPReport;
|
||||
|
||||
typedef struct {
|
||||
NTP_AuthMode mode;
|
||||
uint32_t key_id;
|
||||
int key_type;
|
||||
int key_length;
|
||||
int ke_attempts;
|
||||
uint32_t last_ke_ago;
|
||||
int cookies;
|
||||
int cookie_length;
|
||||
int nak;
|
||||
} RPT_AuthReport;
|
||||
|
||||
typedef struct {
|
||||
uint32_t ref_id;
|
||||
IPAddr ip_addr;
|
||||
char state_char;
|
||||
int authentication;
|
||||
NTP_Leap leap;
|
||||
int conf_options;
|
||||
int eff_options;
|
||||
uint32_t last_sample_ago;
|
||||
double score;
|
||||
double lo_limit;
|
||||
double hi_limit;
|
||||
} RPT_SelectReport;
|
||||
|
||||
#endif /* GOT_REPORTS_H */
|
||||
|
||||
4
rtc.c
4
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");
|
||||
@@ -160,7 +162,7 @@ RTC_Initialise(int initial_set)
|
||||
void
|
||||
RTC_Finalise(void)
|
||||
{
|
||||
if (driver.fini) {
|
||||
if (driver_initialised) {
|
||||
(driver.fini)();
|
||||
}
|
||||
}
|
||||
|
||||
154
rtc_linux.c
154
rtc_linux.c
@@ -352,7 +352,7 @@ rtc_from_t(const time_t *t)
|
||||
|
||||
static time_t
|
||||
t_from_rtc(struct tm *stm) {
|
||||
struct tm temp1, temp2;
|
||||
struct tm temp1, temp2, *tm;
|
||||
long diff;
|
||||
time_t t1, t2;
|
||||
|
||||
@@ -360,12 +360,14 @@ t_from_rtc(struct tm *stm) {
|
||||
temp1.tm_isdst = 0;
|
||||
|
||||
t1 = mktime(&temp1);
|
||||
if (rtc_on_utc) {
|
||||
temp2 = *gmtime(&t1);
|
||||
} else {
|
||||
temp2 = *localtime(&t1);
|
||||
|
||||
tm = rtc_on_utc ? gmtime(&t1) : localtime(&t1);
|
||||
if (!tm) {
|
||||
DEBUG_LOG("gmtime()/localtime() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
temp2 = *tm;
|
||||
temp2.tm_isdst = 0;
|
||||
t2 = mktime(&temp2);
|
||||
diff = t2 - t1;
|
||||
@@ -388,12 +390,9 @@ read_hwclock_file(const char *hwclock_file)
|
||||
if (!hwclock_file || !hwclock_file[0])
|
||||
return;
|
||||
|
||||
in = fopen(hwclock_file, "r");
|
||||
if (!in) {
|
||||
LOG(LOGS_WARN, "Could not open %s : %s",
|
||||
hwclock_file, strerror(errno));
|
||||
in = UTI_OpenFile(NULL, hwclock_file, NULL, 'r', 0);
|
||||
if (!in)
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read third line from the file. */
|
||||
for (i = 0; i < 3; i++) {
|
||||
@@ -443,7 +442,8 @@ read_coefs_from_file(void)
|
||||
|
||||
tried_to_load_coefs = 1;
|
||||
|
||||
if (coefs_file_name && (in = fopen(coefs_file_name, "r"))) {
|
||||
if (coefs_file_name &&
|
||||
(in = UTI_OpenFile(NULL, coefs_file_name, NULL, 'r', 0))) {
|
||||
if (fscanf(in, "%d%ld%lf%lf",
|
||||
&valid_coefs_from_file,
|
||||
&file_ref_time,
|
||||
@@ -464,67 +464,40 @@ read_coefs_from_file(void)
|
||||
static int
|
||||
write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
||||
{
|
||||
struct stat buf;
|
||||
char *temp_coefs_file_name;
|
||||
FILE *out;
|
||||
int r1, r2;
|
||||
|
||||
/* Create a temporary file with a '.tmp' extension. */
|
||||
|
||||
temp_coefs_file_name = (char*) Malloc(strlen(coefs_file_name)+8);
|
||||
|
||||
if(!temp_coefs_file_name) {
|
||||
out = UTI_OpenFile(NULL, coefs_file_name, ".tmp", 'w', 0644);
|
||||
if (!out)
|
||||
return RTC_ST_BADFILE;
|
||||
}
|
||||
|
||||
strcpy(temp_coefs_file_name,coefs_file_name);
|
||||
strcat(temp_coefs_file_name,".tmp");
|
||||
|
||||
out = fopen(temp_coefs_file_name, "w");
|
||||
if (!out) {
|
||||
Free(temp_coefs_file_name);
|
||||
LOG(LOGS_WARN, "Could not open temporary RTC file %s.tmp for writing",
|
||||
coefs_file_name);
|
||||
return RTC_ST_BADFILE;
|
||||
}
|
||||
|
||||
/* Gain rate is written out in ppm */
|
||||
r1 = fprintf(out, "%1d %ld %.6f %.3f\n",
|
||||
valid, ref_time, offset, 1.0e6 * rate);
|
||||
r2 = fclose(out);
|
||||
if (r1 < 0 || r2) {
|
||||
Free(temp_coefs_file_name);
|
||||
LOG(LOGS_WARN, "Could not write to temporary RTC file %s.tmp",
|
||||
coefs_file_name);
|
||||
fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate);
|
||||
fclose(out);
|
||||
|
||||
/* Rename the temporary file to the correct location */
|
||||
if (!UTI_RenameTempFile(NULL, coefs_file_name, ".tmp", NULL))
|
||||
return RTC_ST_BADFILE;
|
||||
}
|
||||
|
||||
/* Clone the file attributes from the existing file if there is one. */
|
||||
|
||||
if (!stat(coefs_file_name,&buf)) {
|
||||
if (chown(temp_coefs_file_name,buf.st_uid,buf.st_gid) ||
|
||||
chmod(temp_coefs_file_name,buf.st_mode & 0777)) {
|
||||
LOG(LOGS_WARN,
|
||||
"Could not change ownership or permissions of temporary RTC file %s.tmp",
|
||||
coefs_file_name);
|
||||
}
|
||||
}
|
||||
|
||||
/* Rename the temporary file to the correct location (see rename(2) for details). */
|
||||
|
||||
if (rename(temp_coefs_file_name,coefs_file_name)) {
|
||||
unlink(temp_coefs_file_name);
|
||||
Free(temp_coefs_file_name);
|
||||
LOG(LOGS_WARN, "Could not replace old RTC file %s.tmp with new one %s",
|
||||
coefs_file_name, coefs_file_name);
|
||||
return RTC_ST_BADFILE;
|
||||
}
|
||||
|
||||
Free(temp_coefs_file_name);
|
||||
|
||||
return RTC_ST_OK;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
switch_interrupts(int on_off)
|
||||
{
|
||||
if (ioctl(fd, on_off ? RTC_UIE_ON : RTC_UIE_OFF, 0) < 0) {
|
||||
LOG(LOGS_ERR, "Could not %s RTC interrupt : %s",
|
||||
on_off ? "enable" : "disable", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (on_off)
|
||||
skip_interrupts = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
/* file_name is the name of the file where we save the RTC params
|
||||
@@ -534,6 +507,23 @@ write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
||||
int
|
||||
RTC_Linux_Initialise(void)
|
||||
{
|
||||
/* Try to open the device */
|
||||
fd = open(CNF_GetRtcDevice(), O_RDWR);
|
||||
if (fd < 0) {
|
||||
LOG(LOGS_ERR, "Could not open RTC device %s : %s",
|
||||
CNF_GetRtcDevice(), strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Make sure the RTC supports interrupts */
|
||||
if (!switch_interrupts(1) || !switch_interrupts(0)) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Close on exec */
|
||||
UTI_FdSetCloexec(fd);
|
||||
|
||||
rtc_sec = MallocArray(time_t, MAX_SAMPLES);
|
||||
rtc_trim = MallocArray(double, MAX_SAMPLES);
|
||||
system_times = MallocArray(struct timespec, MAX_SAMPLES);
|
||||
@@ -544,18 +534,6 @@ RTC_Linux_Initialise(void)
|
||||
/* In case it didn't get done by pre-init */
|
||||
coefs_file_name = CNF_GetRtcFile();
|
||||
|
||||
/* Try to open device */
|
||||
|
||||
fd = open (CNF_GetRtcDevice(), O_RDWR);
|
||||
if (fd < 0) {
|
||||
LOG(LOGS_ERR, "Could not open RTC device %s : %s",
|
||||
CNF_GetRtcDevice(), strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Close on exec */
|
||||
UTI_FdSetCloexec(fd);
|
||||
|
||||
n_samples = 0;
|
||||
n_samples_since_regression = 0;
|
||||
n_runs = 0;
|
||||
@@ -588,12 +566,17 @@ RTC_Linux_Finalise(void)
|
||||
/* Remove input file handler */
|
||||
if (fd >= 0) {
|
||||
SCH_RemoveFileHandler(fd);
|
||||
switch_interrupts(0);
|
||||
close(fd);
|
||||
|
||||
/* Save the RTC data */
|
||||
(void) RTC_Linux_WriteParameters();
|
||||
|
||||
}
|
||||
|
||||
if (rtc_sec)
|
||||
LCL_RemoveParameterChangeHandler(slew_samples, NULL);
|
||||
|
||||
Free(rtc_sec);
|
||||
Free(rtc_trim);
|
||||
Free(system_times);
|
||||
@@ -601,29 +584,6 @@ RTC_Linux_Finalise(void)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
switch_interrupts(int onoff)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (onoff) {
|
||||
status = ioctl(fd, RTC_UIE_ON, 0);
|
||||
if (status < 0) {
|
||||
LOG(LOGS_ERR, "Could not %s RTC interrupt : %s", "enable", strerror(errno));
|
||||
return;
|
||||
}
|
||||
skip_interrupts = 1;
|
||||
} else {
|
||||
status = ioctl(fd, RTC_UIE_OFF, 0);
|
||||
if (status < 0) {
|
||||
LOG(LOGS_ERR, "Could not %s RTC interrupt : %s", "disable", strerror(errno));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
measurement_timeout(void *any)
|
||||
{
|
||||
|
||||
451
samplefilt.c
Normal file
451
samplefilt.c
Normal file
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2014, 2016, 2018
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
Routines implementing a median sample filter.
|
||||
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "local.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "regress.h"
|
||||
#include "samplefilt.h"
|
||||
#include "util.h"
|
||||
|
||||
#define MIN_SAMPLES 1
|
||||
#define MAX_SAMPLES 256
|
||||
|
||||
struct SPF_Instance_Record {
|
||||
int min_samples;
|
||||
int max_samples;
|
||||
int index;
|
||||
int used;
|
||||
int last;
|
||||
int avg_var_n;
|
||||
double avg_var;
|
||||
double max_var;
|
||||
double combine_ratio;
|
||||
NTP_Sample *samples;
|
||||
int *selected;
|
||||
double *x_data;
|
||||
double *y_data;
|
||||
double *w_data;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
SPF_Instance
|
||||
SPF_CreateInstance(int min_samples, int max_samples, double max_dispersion, double combine_ratio)
|
||||
{
|
||||
SPF_Instance filter;
|
||||
|
||||
filter = MallocNew(struct SPF_Instance_Record);
|
||||
|
||||
min_samples = CLAMP(MIN_SAMPLES, min_samples, MAX_SAMPLES);
|
||||
max_samples = CLAMP(MIN_SAMPLES, max_samples, MAX_SAMPLES);
|
||||
max_samples = MAX(min_samples, max_samples);
|
||||
combine_ratio = CLAMP(0.0, combine_ratio, 1.0);
|
||||
|
||||
filter->min_samples = min_samples;
|
||||
filter->max_samples = max_samples;
|
||||
filter->index = -1;
|
||||
filter->used = 0;
|
||||
filter->last = -1;
|
||||
/* Set the first estimate to the system precision */
|
||||
filter->avg_var_n = 0;
|
||||
filter->avg_var = SQUARE(LCL_GetSysPrecisionAsQuantum());
|
||||
filter->max_var = SQUARE(max_dispersion);
|
||||
filter->combine_ratio = combine_ratio;
|
||||
filter->samples = MallocArray(NTP_Sample, filter->max_samples);
|
||||
filter->selected = MallocArray(int, filter->max_samples);
|
||||
filter->x_data = MallocArray(double, filter->max_samples);
|
||||
filter->y_data = MallocArray(double, filter->max_samples);
|
||||
filter->w_data = MallocArray(double, filter->max_samples);
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_DestroyInstance(SPF_Instance filter)
|
||||
{
|
||||
Free(filter->samples);
|
||||
Free(filter->selected);
|
||||
Free(filter->x_data);
|
||||
Free(filter->y_data);
|
||||
Free(filter->w_data);
|
||||
Free(filter);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
/* Check that samples times are strictly increasing */
|
||||
|
||||
static int
|
||||
check_sample(SPF_Instance filter, NTP_Sample *sample)
|
||||
{
|
||||
if (filter->used <= 0)
|
||||
return 1;
|
||||
|
||||
if (UTI_CompareTimespecs(&filter->samples[filter->last].time, &sample->time) >= 0) {
|
||||
DEBUG_LOG("filter non-increasing sample time %s", UTI_TimespecToString(&sample->time));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SPF_AccumulateSample(SPF_Instance filter, NTP_Sample *sample)
|
||||
{
|
||||
if (!check_sample(filter, sample))
|
||||
return 0;
|
||||
|
||||
filter->index++;
|
||||
filter->index %= filter->max_samples;
|
||||
filter->last = filter->index;
|
||||
if (filter->used < filter->max_samples)
|
||||
filter->used++;
|
||||
|
||||
filter->samples[filter->index] = *sample;
|
||||
|
||||
DEBUG_LOG("filter sample %d t=%s offset=%.9f peer_disp=%.9f",
|
||||
filter->index, UTI_TimespecToString(&sample->time),
|
||||
sample->offset, sample->peer_dispersion);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SPF_GetLastSample(SPF_Instance filter, NTP_Sample *sample)
|
||||
{
|
||||
if (filter->last < 0)
|
||||
return 0;
|
||||
|
||||
*sample = filter->samples[filter->last];
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SPF_GetNumberOfSamples(SPF_Instance filter)
|
||||
{
|
||||
return filter->used;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
double
|
||||
SPF_GetAvgSampleDispersion(SPF_Instance filter)
|
||||
{
|
||||
return sqrt(filter->avg_var);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_DropSamples(SPF_Instance filter)
|
||||
{
|
||||
filter->index = -1;
|
||||
filter->used = 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static const NTP_Sample *tmp_sort_samples;
|
||||
|
||||
static int
|
||||
compare_samples(const void *a, const void *b)
|
||||
{
|
||||
const NTP_Sample *s1, *s2;
|
||||
|
||||
s1 = &tmp_sort_samples[*(int *)a];
|
||||
s2 = &tmp_sort_samples[*(int *)b];
|
||||
|
||||
if (s1->offset < s2->offset)
|
||||
return -1;
|
||||
else if (s1->offset > s2->offset)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
select_samples(SPF_Instance filter)
|
||||
{
|
||||
int i, j, k, o, from, to, *selected;
|
||||
double min_dispersion;
|
||||
|
||||
if (filter->used < filter->min_samples)
|
||||
return 0;
|
||||
|
||||
selected = filter->selected;
|
||||
|
||||
/* With 4 or more samples, select those that have peer dispersion smaller
|
||||
than 1.5x of the minimum dispersion */
|
||||
if (filter->used > 4) {
|
||||
for (i = 1, min_dispersion = filter->samples[0].peer_dispersion; i < filter->used; i++) {
|
||||
if (min_dispersion > filter->samples[i].peer_dispersion)
|
||||
min_dispersion = filter->samples[i].peer_dispersion;
|
||||
}
|
||||
|
||||
for (i = j = 0; i < filter->used; i++) {
|
||||
if (filter->samples[i].peer_dispersion <= 1.5 * min_dispersion)
|
||||
selected[j++] = i;
|
||||
}
|
||||
} else {
|
||||
j = 0;
|
||||
}
|
||||
|
||||
if (j < 4) {
|
||||
/* Select all samples */
|
||||
|
||||
for (j = 0; j < filter->used; j++)
|
||||
selected[j] = j;
|
||||
}
|
||||
|
||||
/* And sort their indices by offset */
|
||||
tmp_sort_samples = filter->samples;
|
||||
qsort(selected, j, sizeof (int), compare_samples);
|
||||
|
||||
/* Select samples closest to the median */
|
||||
if (j > 2) {
|
||||
from = j * (1.0 - filter->combine_ratio) / 2.0;
|
||||
from = CLAMP(1, from, (j - 1) / 2);
|
||||
} else {
|
||||
from = 0;
|
||||
}
|
||||
|
||||
to = j - from;
|
||||
|
||||
/* Mark unused samples and sort the rest by their time */
|
||||
|
||||
o = filter->used - filter->index - 1;
|
||||
|
||||
for (i = 0; i < from; i++)
|
||||
selected[i] = -1;
|
||||
for (; i < to; i++)
|
||||
selected[i] = (selected[i] + o) % filter->used;
|
||||
for (; i < filter->used; i++)
|
||||
selected[i] = -1;
|
||||
|
||||
for (i = from; i < to; i++) {
|
||||
j = selected[i];
|
||||
selected[i] = -1;
|
||||
while (j != -1 && selected[j] != j) {
|
||||
k = selected[j];
|
||||
selected[j] = j;
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j = 0; i < filter->used; i++) {
|
||||
if (selected[i] != -1)
|
||||
selected[j++] = (selected[i] + filter->used - o) % filter->used;
|
||||
}
|
||||
|
||||
assert(j > 0 && j <= filter->max_samples);
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static int
|
||||
combine_selected_samples(SPF_Instance filter, int n, NTP_Sample *result)
|
||||
{
|
||||
double mean_peer_dispersion, mean_root_dispersion, mean_peer_delay, mean_root_delay;
|
||||
double mean_x, mean_y, disp, var, prev_avg_var;
|
||||
NTP_Sample *sample, *last_sample;
|
||||
int i, dof;
|
||||
|
||||
last_sample = &filter->samples[filter->selected[n - 1]];
|
||||
|
||||
/* Prepare data */
|
||||
for (i = 0; i < n; i++) {
|
||||
sample = &filter->samples[filter->selected[i]];
|
||||
|
||||
filter->x_data[i] = UTI_DiffTimespecsToDouble(&sample->time, &last_sample->time);
|
||||
filter->y_data[i] = sample->offset;
|
||||
filter->w_data[i] = sample->peer_dispersion;
|
||||
}
|
||||
|
||||
/* Calculate mean offset and interval since the last sample */
|
||||
for (i = 0, mean_x = mean_y = 0.0; i < n; i++) {
|
||||
mean_x += filter->x_data[i];
|
||||
mean_y += filter->y_data[i];
|
||||
}
|
||||
mean_x /= n;
|
||||
mean_y /= n;
|
||||
|
||||
if (n >= 4) {
|
||||
double b0, b1, s2, sb0, sb1;
|
||||
|
||||
/* Set y axis to the mean sample time */
|
||||
for (i = 0; i < n; i++)
|
||||
filter->x_data[i] -= mean_x;
|
||||
|
||||
/* Make a linear fit and use the estimated standard deviation of the
|
||||
intercept as dispersion */
|
||||
RGR_WeightedRegression(filter->x_data, filter->y_data, filter->w_data, n,
|
||||
&b0, &b1, &s2, &sb0, &sb1);
|
||||
var = s2;
|
||||
disp = sb0;
|
||||
dof = n - 2;
|
||||
} else if (n >= 2) {
|
||||
for (i = 0, disp = 0.0; i < n; i++)
|
||||
disp += (filter->y_data[i] - mean_y) * (filter->y_data[i] - mean_y);
|
||||
var = disp / (n - 1);
|
||||
disp = sqrt(var);
|
||||
dof = n - 1;
|
||||
} else {
|
||||
var = filter->avg_var;
|
||||
disp = sqrt(var);
|
||||
dof = 1;
|
||||
}
|
||||
|
||||
/* Avoid working with zero dispersion */
|
||||
if (var < 1e-20) {
|
||||
var = 1e-20;
|
||||
disp = sqrt(var);
|
||||
}
|
||||
|
||||
/* Drop the sample if the variance is larger than the maximum */
|
||||
if (filter->max_var > 0.0 && var > filter->max_var) {
|
||||
DEBUG_LOG("filter dispersion too large disp=%.9f max=%.9f",
|
||||
sqrt(var), sqrt(filter->max_var));
|
||||
return 0;
|
||||
}
|
||||
|
||||
prev_avg_var = filter->avg_var;
|
||||
|
||||
/* Update the exponential moving average of the variance */
|
||||
if (filter->avg_var_n > 50) {
|
||||
filter->avg_var += dof / (dof + 50.0) * (var - filter->avg_var);
|
||||
} else {
|
||||
filter->avg_var = (filter->avg_var * filter->avg_var_n + var * dof) /
|
||||
(dof + filter->avg_var_n);
|
||||
if (filter->avg_var_n == 0)
|
||||
prev_avg_var = filter->avg_var;
|
||||
filter->avg_var_n += dof;
|
||||
}
|
||||
|
||||
/* Use the long-term average of variance instead of the estimated value
|
||||
unless it is significantly smaller in order to reduce the noise in
|
||||
sourcestats weights */
|
||||
if (var * dof / RGR_GetChi2Coef(dof) < prev_avg_var)
|
||||
disp = sqrt(filter->avg_var) * disp / sqrt(var);
|
||||
|
||||
mean_peer_dispersion = mean_root_dispersion = mean_peer_delay = mean_root_delay = 0.0;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
sample = &filter->samples[filter->selected[i]];
|
||||
|
||||
mean_peer_dispersion += sample->peer_dispersion;
|
||||
mean_root_dispersion += sample->root_dispersion;
|
||||
mean_peer_delay += sample->peer_delay;
|
||||
mean_root_delay += sample->root_delay;
|
||||
}
|
||||
|
||||
mean_peer_dispersion /= n;
|
||||
mean_root_dispersion /= n;
|
||||
mean_peer_delay /= n;
|
||||
mean_root_delay /= n;
|
||||
|
||||
UTI_AddDoubleToTimespec(&last_sample->time, mean_x, &result->time);
|
||||
result->offset = mean_y;
|
||||
result->peer_dispersion = MAX(disp, mean_peer_dispersion);
|
||||
result->root_dispersion = MAX(disp, mean_root_dispersion);
|
||||
result->peer_delay = mean_peer_delay;
|
||||
result->root_delay = mean_root_delay;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SPF_GetFilteredSample(SPF_Instance filter, NTP_Sample *sample)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = select_samples(filter);
|
||||
|
||||
if (n < 1)
|
||||
return 0;
|
||||
|
||||
if (!combine_selected_samples(filter, n, sample))
|
||||
return 0;
|
||||
|
||||
SPF_DropSamples(filter);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_SlewSamples(SPF_Instance filter, struct timespec *when, double dfreq, double doffset)
|
||||
{
|
||||
int i, first, last;
|
||||
double delta_time;
|
||||
|
||||
if (filter->last < 0)
|
||||
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);
|
||||
filter->samples[i].offset -= delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SPF_AddDispersion(SPF_Instance filter, double dispersion)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < filter->used; i++) {
|
||||
filter->samples[i].peer_dispersion += dispersion;
|
||||
filter->samples[i].root_dispersion += dispersion;
|
||||
}
|
||||
}
|
||||
49
samplefilt.h
Normal file
49
samplefilt.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2018
|
||||
*
|
||||
* 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 sample filter.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef GOT_SAMPLEFILT_H
|
||||
#define GOT_SAMPLEFILT_H
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
typedef struct SPF_Instance_Record *SPF_Instance;
|
||||
|
||||
extern SPF_Instance SPF_CreateInstance(int min_samples, int max_samples,
|
||||
double max_dispersion, double combine_ratio);
|
||||
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 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_AddDispersion(SPF_Instance filter, double dispersion);
|
||||
|
||||
#endif
|
||||
96
sched.c
96
sched.c
@@ -65,6 +65,12 @@ static ARR_Instance file_handlers;
|
||||
static struct timespec last_select_ts, last_select_ts_raw;
|
||||
static double last_select_ts_err;
|
||||
|
||||
#define TS_MONO_PRECISION_NS 10000000U
|
||||
|
||||
/* Monotonic low-precision timestamp measuring interval since the start */
|
||||
static double last_select_ts_mono;
|
||||
static uint32_t last_select_ts_mono_ns;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
/* Variables to handler the timer queue */
|
||||
@@ -105,7 +111,8 @@ 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;
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
@@ -136,6 +143,8 @@ SCH_Initialise(void)
|
||||
|
||||
LCL_ReadRawTime(&last_select_ts_raw);
|
||||
last_select_ts = last_select_ts_raw;
|
||||
last_select_ts_mono = 0.0;
|
||||
last_select_ts_mono_ns = 0;
|
||||
|
||||
initialised = 1;
|
||||
}
|
||||
@@ -147,6 +156,8 @@ void
|
||||
SCH_Finalise(void) {
|
||||
ARR_DestroyInstance(file_handlers);
|
||||
|
||||
LCL_RemoveParameterChangeHandler(handle_slew, NULL);
|
||||
|
||||
initialised = 0;
|
||||
}
|
||||
|
||||
@@ -219,13 +230,16 @@ SCH_RemoveFileHandler(int fd)
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SCH_SetFileHandlerEvents(int fd, int events)
|
||||
SCH_SetFileHandlerEvent(int fd, int event, int enable)
|
||||
{
|
||||
FileHandlerEntry *ptr;
|
||||
|
||||
assert(events);
|
||||
ptr = ARR_GetElement(file_handlers, fd);
|
||||
ptr->events = events;
|
||||
|
||||
if (enable)
|
||||
ptr->events |= event;
|
||||
else
|
||||
ptr->events &= ~event;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -244,6 +258,14 @@ SCH_GetLastEventTime(struct timespec *cooked, double *err, struct timespec *raw)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
double
|
||||
SCH_GetLastEventMonoTime(void)
|
||||
{
|
||||
return last_select_ts_mono;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define TQE_ALLOC_QUANTUM 32
|
||||
|
||||
static TimerQueueEntry *
|
||||
@@ -477,12 +499,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 &&
|
||||
@@ -505,16 +530,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);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
@@ -531,7 +561,8 @@ dispatch_filehandlers(int nfd, fd_set *read_fds, fd_set *write_fds, fd_set *exce
|
||||
if (except_fds && FD_ISSET(fd, except_fds)) {
|
||||
/* This descriptor has an exception, dispatch its handler */
|
||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
||||
(ptr->handler)(fd, SCH_FILE_EXCEPTION, ptr->arg);
|
||||
if (ptr->handler)
|
||||
(ptr->handler)(fd, SCH_FILE_EXCEPTION, ptr->arg);
|
||||
nfd--;
|
||||
|
||||
/* Don't try to read from it now */
|
||||
@@ -544,14 +575,16 @@ dispatch_filehandlers(int nfd, fd_set *read_fds, fd_set *write_fds, fd_set *exce
|
||||
if (read_fds && FD_ISSET(fd, read_fds)) {
|
||||
/* This descriptor can be read from, dispatch its handler */
|
||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
||||
(ptr->handler)(fd, SCH_FILE_INPUT, ptr->arg);
|
||||
if (ptr->handler)
|
||||
(ptr->handler)(fd, SCH_FILE_INPUT, ptr->arg);
|
||||
nfd--;
|
||||
}
|
||||
|
||||
if (write_fds && FD_ISSET(fd, write_fds)) {
|
||||
/* This descriptor can be written to, dispatch its handler */
|
||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
||||
(ptr->handler)(fd, SCH_FILE_OUTPUT, ptr->arg);
|
||||
if (ptr->handler)
|
||||
(ptr->handler)(fd, SCH_FILE_OUTPUT, ptr->arg);
|
||||
nfd--;
|
||||
}
|
||||
}
|
||||
@@ -700,6 +733,31 @@ check_current_time(struct timespec *prev_raw, struct timespec *raw, int timeout,
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
update_monotonic_time(struct timespec *now, struct timespec *before)
|
||||
{
|
||||
struct timespec diff;
|
||||
|
||||
/* Avoid frequent floating-point operations and handle small
|
||||
increments to a large value */
|
||||
|
||||
UTI_DiffTimespecs(&diff, now, before);
|
||||
if (diff.tv_sec == 0) {
|
||||
last_select_ts_mono_ns += diff.tv_nsec;
|
||||
} else {
|
||||
last_select_ts_mono += fabs(UTI_TimespecToDouble(&diff) +
|
||||
last_select_ts_mono_ns / 1.0e9);
|
||||
last_select_ts_mono_ns = 0;
|
||||
}
|
||||
|
||||
if (last_select_ts_mono_ns > TS_MONO_PRECISION_NS) {
|
||||
last_select_ts_mono += last_select_ts_mono_ns / 1.0e9;
|
||||
last_select_ts_mono_ns = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SCH_MainLoop(void)
|
||||
{
|
||||
@@ -750,6 +808,8 @@ SCH_MainLoop(void)
|
||||
LCL_ReadRawTime(&now);
|
||||
LCL_CookTime(&now, &cooked, &err);
|
||||
|
||||
update_monotonic_time(&now, &last_select_ts_raw);
|
||||
|
||||
/* Check if the time didn't jump unexpectedly */
|
||||
if (!check_current_time(&saved_now, &now, status == 0, &saved_tv, ptv)) {
|
||||
/* Cook the time again after handling the step */
|
||||
|
||||
5
sched.h
5
sched.h
@@ -60,11 +60,14 @@ extern void SCH_Finalise(void);
|
||||
/* Register a handler for when select goes true on a file descriptor */
|
||||
extern void SCH_AddFileHandler(int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg);
|
||||
extern void SCH_RemoveFileHandler(int fd);
|
||||
extern void SCH_SetFileHandlerEvents(int fd, int events);
|
||||
extern void SCH_SetFileHandlerEvent(int fd, int event, int enable);
|
||||
|
||||
/* Get the time stamp taken after a file descriptor became ready or a timeout expired */
|
||||
extern void SCH_GetLastEventTime(struct timespec *cooked, double *err, struct timespec *raw);
|
||||
|
||||
/* Get a low-precision monotonic timestamp (starting at 0.0) */
|
||||
extern double SCH_GetLastEventMonoTime(void);
|
||||
|
||||
/* This queues a timeout to elapse at a given (raw) local time */
|
||||
extern SCH_TimeoutID SCH_AddTimeout(struct timespec *ts, SCH_TimeoutHandler handler, SCH_ArbitraryArgument arg);
|
||||
|
||||
|
||||
70
siv.h
Normal file
70
siv.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2019
|
||||
*
|
||||
* 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 Synthetic Initialization Vector (SIV) ciphers.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef GOT_SIV_H
|
||||
#define GOT_SIV_H
|
||||
|
||||
/* Maximum key length of all supported SIVs */
|
||||
#define SIV_MAX_KEY_LENGTH 32
|
||||
|
||||
/* Maximum difference between lengths of ciphertext and plaintext */
|
||||
#define SIV_MAX_TAG_LENGTH 16
|
||||
|
||||
/* Identifiers of SIV algorithms following the IANA AEAD registry */
|
||||
typedef enum {
|
||||
AEAD_AES_SIV_CMAC_256 = 15,
|
||||
AEAD_AES_SIV_CMAC_384 = 16,
|
||||
AEAD_AES_SIV_CMAC_512 = 17,
|
||||
AEAD_AES_128_GCM_SIV = 30,
|
||||
AEAD_AES_256_GCM_SIV = 31,
|
||||
} SIV_Algorithm;
|
||||
|
||||
typedef struct SIV_Instance_Record *SIV_Instance;
|
||||
|
||||
extern SIV_Instance SIV_CreateInstance(SIV_Algorithm algorithm);
|
||||
|
||||
extern void SIV_DestroyInstance(SIV_Instance instance);
|
||||
|
||||
extern int SIV_GetKeyLength(SIV_Algorithm algorithm);
|
||||
|
||||
extern int SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length);
|
||||
|
||||
extern int SIV_GetTagLength(SIV_Instance instance);
|
||||
|
||||
extern int SIV_Encrypt(SIV_Instance instance,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const void *assoc, int assoc_length,
|
||||
const void *plaintext, int plaintext_length,
|
||||
unsigned char *ciphertext, int ciphertext_length);
|
||||
|
||||
extern int SIV_Decrypt(SIV_Instance instance,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const void *assoc, int assoc_length,
|
||||
const unsigned char *ciphertext, int ciphertext_length,
|
||||
void *plaintext, int plaintext_length);
|
||||
|
||||
#endif
|
||||
256
siv_gnutls.c
Normal file
256
siv_gnutls.c
Normal file
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||
|
||||
**********************************************************************
|
||||
* Copyright (C) Miroslav Lichvar 2020
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
**********************************************************************
|
||||
|
||||
=======================================================================
|
||||
|
||||
SIV ciphers using the GnuTLS library
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sysincl.h"
|
||||
|
||||
#include <gnutls/crypto.h>
|
||||
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
#include "siv.h"
|
||||
|
||||
struct SIV_Instance_Record {
|
||||
gnutls_cipher_algorithm_t algorithm;
|
||||
gnutls_aead_cipher_hd_t cipher;
|
||||
};
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
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_cipher_algorithm_t
|
||||
get_cipher_algorithm(SIV_Algorithm algorithm)
|
||||
{
|
||||
switch (algorithm) {
|
||||
case AEAD_AES_SIV_CMAC_256:
|
||||
return GNUTLS_CIPHER_AES_128_SIV;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
SIV_Instance
|
||||
SIV_CreateInstance(SIV_Algorithm algorithm)
|
||||
{
|
||||
gnutls_cipher_algorithm_t calgo;
|
||||
SIV_Instance instance;
|
||||
|
||||
calgo = get_cipher_algorithm(algorithm);
|
||||
if (calgo == 0)
|
||||
return NULL;
|
||||
|
||||
if (instance_counter == 0)
|
||||
init_gnutls();
|
||||
|
||||
/* Check if the cipher is actually supported */
|
||||
if (gnutls_cipher_get_tag_size(calgo) == 0)
|
||||
return NULL;
|
||||
|
||||
instance = MallocNew(struct SIV_Instance_Record);
|
||||
instance->algorithm = calgo;
|
||||
instance->cipher = NULL;
|
||||
|
||||
instance_counter++;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
SIV_DestroyInstance(SIV_Instance instance)
|
||||
{
|
||||
if (instance->cipher)
|
||||
gnutls_aead_cipher_deinit(instance->cipher);
|
||||
Free(instance);
|
||||
|
||||
instance_counter--;
|
||||
if (instance_counter == 0)
|
||||
deinit_gnutls();
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetKeyLength(SIV_Algorithm algorithm)
|
||||
{
|
||||
gnutls_cipher_algorithm_t calgo = get_cipher_algorithm(algorithm);
|
||||
int len;
|
||||
|
||||
if (calgo == 0)
|
||||
return 0;
|
||||
|
||||
len = gnutls_cipher_get_key_size(calgo);
|
||||
|
||||
if (len < 1 || len > SIV_MAX_KEY_LENGTH)
|
||||
LOG_FATAL("Invalid key length");
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
|
||||
{
|
||||
gnutls_aead_cipher_hd_t cipher;
|
||||
gnutls_datum_t datum;
|
||||
int r;
|
||||
|
||||
if (length <= 0 || length != gnutls_cipher_get_key_size(instance->algorithm))
|
||||
return 0;
|
||||
|
||||
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) */
|
||||
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 */
|
||||
if (instance->cipher)
|
||||
gnutls_aead_cipher_deinit(instance->cipher);
|
||||
instance->cipher = cipher;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_GetTagLength(SIV_Instance instance)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = gnutls_cipher_get_tag_size(instance->algorithm);
|
||||
|
||||
if (len < 1 || len > SIV_MAX_TAG_LENGTH)
|
||||
LOG_FATAL("Invalid tag length");
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_Encrypt(SIV_Instance instance,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const void *assoc, int assoc_length,
|
||||
const void *plaintext, int plaintext_length,
|
||||
unsigned char *ciphertext, int ciphertext_length)
|
||||
{
|
||||
size_t clen = ciphertext_length;
|
||||
|
||||
if (!instance->cipher)
|
||||
return 0;
|
||||
|
||||
if (nonce_length < 1 || assoc_length < 0 ||
|
||||
plaintext_length < 0 || ciphertext_length < 0)
|
||||
return 0;
|
||||
|
||||
assert(assoc && plaintext);
|
||||
|
||||
if (gnutls_aead_cipher_encrypt(instance->cipher,
|
||||
nonce, nonce_length, assoc, assoc_length, 0,
|
||||
plaintext, plaintext_length, ciphertext, &clen) < 0)
|
||||
return 0;
|
||||
|
||||
if (clen != ciphertext_length)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
SIV_Decrypt(SIV_Instance instance,
|
||||
const unsigned char *nonce, int nonce_length,
|
||||
const void *assoc, int assoc_length,
|
||||
const unsigned char *ciphertext, int ciphertext_length,
|
||||
void *plaintext, int plaintext_length)
|
||||
{
|
||||
size_t plen = plaintext_length;
|
||||
|
||||
if (!instance->cipher)
|
||||
return 0;
|
||||
|
||||
if (nonce_length < 1 || assoc_length < 0 ||
|
||||
plaintext_length < 0 || ciphertext_length < 0)
|
||||
return 0;
|
||||
|
||||
assert(assoc && plaintext);
|
||||
|
||||
if (gnutls_aead_cipher_decrypt(instance->cipher,
|
||||
nonce, nonce_length, assoc, assoc_length, 0,
|
||||
ciphertext, ciphertext_length, plaintext, &plen) < 0)
|
||||
return 0;
|
||||
|
||||
if (plen != plaintext_length)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user