mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-04 12:35:08 -05:00
Compare commits
621 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
b8d546a0d1 | ||
|
|
04e6474b75 | ||
|
|
eb51c500e8 | ||
|
|
6f8fba9a3f | ||
|
|
750afc30f2 | ||
|
|
e0e6ec0d84 | ||
|
|
c9f50fc686 | ||
|
|
83c26b458b | ||
|
|
b711873f45 | ||
|
|
c68ca40ce4 | ||
|
|
51fe80ad95 | ||
|
|
7ffee73524 | ||
|
|
f40b0024bd | ||
|
|
a06c9909a6 | ||
|
|
aee42fada8 | ||
|
|
3e93068c43 | ||
|
|
36291b707b | ||
|
|
6dad2c24bf | ||
|
|
27cbf20d23 | ||
|
|
5c571bbbe7 | ||
|
|
33d65c8614 | ||
|
|
d87db7cdb8 | ||
|
|
45fa4750da | ||
|
|
8472fd8133 | ||
|
|
5ab645e310 | ||
|
|
8ccda538d3 | ||
|
|
b06d74ab73 | ||
|
|
d0964ffa83 | ||
|
|
3d08815efb | ||
|
|
a83f0d3cdc | ||
|
|
702db726d3 | ||
|
|
ed5c43204b | ||
|
|
f91bdd604d | ||
|
|
3a1dbb1354 | ||
|
|
4b511143b8 | ||
|
|
93076e7e1c | ||
|
|
1c51feb3c5 | ||
|
|
c2773dbc2f | ||
|
|
4534db84c4 | ||
|
|
be8215e181 | ||
|
|
ae82bbbace | ||
|
|
2b6ea41062 | ||
|
|
d9f745fe70 | ||
|
|
9aac179367 | ||
|
|
b896bb5a78 | ||
|
|
64c2fd9888 | ||
|
|
2668a12e4e | ||
|
|
e1645966ec | ||
|
|
4f1fc1ee78 | ||
|
|
d70df3daab | ||
|
|
554b9b06de | ||
|
|
f734bd1a7c | ||
|
|
77fc5c42b9 | ||
|
|
ea85bc43e0 | ||
|
|
e8fb11c433 | ||
|
|
01a29c7a11 | ||
|
|
6ec3dc1650 | ||
|
|
0c54cf316d | ||
|
|
bd3fb49a1e | ||
|
|
f6e72a80e1 | ||
|
|
c2ab1426e5 | ||
|
|
fa2c59d78d | ||
|
|
16afa8eb50 | ||
|
|
992590e99c | ||
|
|
0baa35eade | ||
|
|
2e0870ee0c | ||
|
|
43cd119d6d | ||
|
|
62cd319a51 | ||
|
|
d0f789425b | ||
|
|
30e6549692 | ||
|
|
043c7d7c9f | ||
|
|
1c277a8850 | ||
|
|
ccb94ac5fb | ||
|
|
778fce4039 | ||
|
|
9983185d6d | ||
|
|
7bd1c02781 | ||
|
|
760285218f | ||
|
|
4fe0e6b7fd | ||
|
|
0773a1e630 | ||
|
|
4a24368763 | ||
|
|
577290c5bc | ||
|
|
854ff69f78 | ||
|
|
29b0ad894c | ||
|
|
cde0a20307 | ||
|
|
a768578a26 | ||
|
|
5d838729ef | ||
|
|
d6b763dc24 | ||
|
|
95adb52a45 | ||
|
|
707d9a3484 | ||
|
|
1872d4d195 | ||
|
|
17f32c266e | ||
|
|
6207655ab2 | ||
|
|
5e1e31ad5f | ||
|
|
13111c1dd8 | ||
|
|
85c84073c1 | ||
|
|
c2944d8727 | ||
|
|
e118b9b1e8 | ||
|
|
7fb7f95979 | ||
|
|
cc507bffae | ||
|
|
0dbfe020ad | ||
|
|
018a1c42b0 | ||
|
|
c5735ebfe9 | ||
|
|
db93180ce1 | ||
|
|
39da10d939 | ||
|
|
f2da253bc3 | ||
|
|
934d4047f1 | ||
|
|
b799cfd1c4 | ||
|
|
b712c100d7 | ||
|
|
c049bce007 | ||
|
|
46fad717e5 | ||
|
|
ae0c3bbbe8 | ||
|
|
f95d57e0d9 | ||
|
|
a1cbd4eb82 | ||
|
|
6cbeb107db | ||
|
|
3a5566c6c3 | ||
|
|
73c548ad01 | ||
|
|
82203e12c8 | ||
|
|
1ca099473f | ||
|
|
eceb8d9937 | ||
|
|
4ba92bb6d6 | ||
|
|
f31f68ae8e | ||
|
|
cff15f91d4 | ||
|
|
6b74917954 | ||
|
|
1bf2384a1f | ||
|
|
54a12779e2 | ||
|
|
e8b06fef9f | ||
|
|
653d70ec4e | ||
|
|
abb09418b1 | ||
|
|
c103bebd9f | ||
|
|
935d855b47 | ||
|
|
f8f9100a0d | ||
|
|
6de7b98e76 | ||
|
|
c390351c65 | ||
|
|
768bce799b | ||
|
|
d3a30142e5 | ||
|
|
3a635fc51f | ||
|
|
10078566da | ||
|
|
c44346096c | ||
|
|
0ff449e6a6 | ||
|
|
f3a16383b9 | ||
|
|
539ef3f770 | ||
|
|
f282856c72 | ||
|
|
6db8ec1ba2 | ||
|
|
5187c08c90 | ||
|
|
c8076ac10d | ||
|
|
362d155558 | ||
|
|
7b7eb0a6e5 | ||
|
|
d96f49f67d | ||
|
|
43ba5d2126 | ||
|
|
48f7598fed | ||
|
|
510b22e96b | ||
|
|
0a0aff14d8 | ||
|
|
e225ac68bc | ||
|
|
58060c40a5 | ||
|
|
2ac1b3d5c4 | ||
|
|
c174566982 | ||
|
|
60fca19d40 | ||
|
|
8bcb15b02f | ||
|
|
65c2cebcd5 | ||
|
|
2a51b45a43 | ||
|
|
5ac791665e | ||
|
|
a4e3f83611 | ||
|
|
8a837f9c2b | ||
|
|
da2d33e9a8 | ||
|
|
4b98dadae9 | ||
|
|
86acea5c46 | ||
|
|
a60fc73e7b | ||
|
|
50f99ec5f4 | ||
|
|
31b6a14444 | ||
|
|
9df4d36157 | ||
|
|
b70f0b674f | ||
|
|
510784077f | ||
|
|
9800e397fb | ||
|
|
1436d9961f | ||
|
|
98f5d05925 | ||
|
|
7a937c7652 | ||
|
|
b198d76676 | ||
|
|
97d4203354 | ||
|
|
beaaaad162 | ||
|
|
4e78975909 | ||
|
|
99147ed8f2 | ||
|
|
dec0d3bfc2 | ||
|
|
cd84c99e70 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,9 +1,12 @@
|
|||||||
.deps
|
.deps
|
||||||
.vimrc
|
.vimrc
|
||||||
|
*.gcda
|
||||||
|
*.gcno
|
||||||
*.o
|
*.o
|
||||||
*.swp
|
*.swp
|
||||||
*.dSYM
|
*.dSYM
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
|
core.*
|
||||||
tags
|
tags
|
||||||
/RELEASES
|
/RELEASES
|
||||||
/Makefile
|
/Makefile
|
||||||
|
|||||||
55
Makefile.in
55
Makefile.in
@@ -21,56 +21,56 @@
|
|||||||
#
|
#
|
||||||
# Makefile template
|
# Makefile template
|
||||||
|
|
||||||
SYSCONFDIR=@SYSCONFDIR@
|
SYSCONFDIR = @SYSCONFDIR@
|
||||||
BINDIR=@BINDIR@
|
BINDIR = @BINDIR@
|
||||||
SBINDIR=@SBINDIR@
|
SBINDIR = @SBINDIR@
|
||||||
LOCALSTATEDIR=@LOCALSTATEDIR@
|
LOCALSTATEDIR = @LOCALSTATEDIR@
|
||||||
CHRONYVARDIR=@CHRONYVARDIR@
|
CHRONYVARDIR = @CHRONYVARDIR@
|
||||||
|
DESTDIR =
|
||||||
|
|
||||||
CC = @CC@
|
CC = @CC@
|
||||||
CFLAGS = @CFLAGS@
|
CFLAGS = @CFLAGS@
|
||||||
CPPFLAGS = @CPPFLAGS@
|
CPPFLAGS = @CPPFLAGS@
|
||||||
|
LDFLAGS = @LDFLAGS@
|
||||||
|
|
||||||
DESTDIR=
|
EXTRA_OBJS = @EXTRA_OBJS@
|
||||||
|
|
||||||
HASH_OBJ = @HASH_OBJ@
|
|
||||||
|
|
||||||
OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o \
|
OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o \
|
||||||
reference.o regress.o rtc.o sched.o sources.o sourcestats.o stubs.o \
|
reference.o regress.o rtc.o samplefilt.o sched.o socket.o sources.o sourcestats.o \
|
||||||
sys.o smooth.o tempcomp.o util.o $(HASH_OBJ)
|
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 \
|
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@
|
LIBS = @LIBS@
|
||||||
|
EXTRA_LIBS = @EXTRA_LIBS@
|
||||||
EXTRA_LIBS=@EXTRA_LIBS@
|
EXTRA_CLI_LIBS = @EXTRA_CLI_LIBS@
|
||||||
EXTRA_CLI_LIBS=@EXTRA_CLI_LIBS@
|
|
||||||
|
|
||||||
# Until we have a main procedure we can link, just build object files
|
# Until we have a main procedure we can link, just build object files
|
||||||
# to test compilation
|
# to test compilation
|
||||||
|
|
||||||
all : chronyd chronyc
|
all : chronyd chronyc
|
||||||
|
|
||||||
chronyd : $(OBJS) $(EXTRA_OBJS)
|
chronyd : $(OBJS)
|
||||||
$(CC) $(CFLAGS) -o chronyd $(OBJS) $(EXTRA_OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_LIBS)
|
$(CC) $(CFLAGS) -o chronyd $(OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_LIBS)
|
||||||
|
|
||||||
chronyc : $(CLI_OBJS)
|
chronyc : $(CLI_OBJS)
|
||||||
$(CC) $(CFLAGS) -o chronyc $(CLI_OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_CLI_LIBS)
|
$(CC) $(CFLAGS) -o chronyc $(CLI_OBJS) $(LDFLAGS) $(LIBS) $(EXTRA_CLI_LIBS)
|
||||||
|
|
||||||
distclean : clean
|
distclean : clean
|
||||||
-rm -f .DS_Store
|
|
||||||
-rm -f Makefile config.h config.log
|
|
||||||
$(MAKE) -C doc distclean
|
$(MAKE) -C doc distclean
|
||||||
$(MAKE) -C test/unit distclean
|
$(MAKE) -C test/unit distclean
|
||||||
|
-rm -f .DS_Store
|
||||||
|
-rm -f Makefile config.h config.log
|
||||||
|
|
||||||
clean :
|
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 .deps
|
||||||
-rm -rf *.dSYM
|
-rm -rf *.dSYM
|
||||||
|
|
||||||
@@ -109,9 +109,18 @@ install-docs :
|
|||||||
%.s : %.c
|
%.s : %.c
|
||||||
$(CC) $(CFLAGS) $(CPPFLAGS) -S $<
|
$(CC) $(CFLAGS) $(CPPFLAGS) -S $<
|
||||||
|
|
||||||
check : chronyd chronyc
|
quickcheck : chronyd chronyc
|
||||||
$(MAKE) -C test/unit check
|
$(MAKE) -C test/unit check
|
||||||
cd test/simulation && ./run
|
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
|
Makefile : Makefile.in configure
|
||||||
@echo
|
@echo
|
||||||
|
|||||||
147
NEWS
147
NEWS
@@ -1,3 +1,150 @@
|
|||||||
|
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 support for maxsamples of 1 for faster update with -q/-Q option
|
||||||
|
* Add -L option to limit log messages by severity
|
||||||
|
* Avoid replacing NTP sources with unreachable addresses
|
||||||
|
* Improve NTP loop test to prevent synchronisation to itself
|
||||||
|
* Update clock synchronisation status and leap status more frequently
|
||||||
|
* Update seccomp filter
|
||||||
|
* Add "add pool" command
|
||||||
|
* Add -N option and sourcename command to print original names of sources
|
||||||
|
* Add -a option to source/sourcestats command to print unresolved sources
|
||||||
|
* Add reset command to drop all measurements
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
---------
|
||||||
|
* 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)
|
||||||
|
|
||||||
|
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
|
||||||
|
==================
|
||||||
|
|
||||||
|
Enhancements
|
||||||
|
------------
|
||||||
|
* Improve stability with NTP sources and reference clocks
|
||||||
|
* Improve stability with hardware timestamping
|
||||||
|
* Improve support for NTP interleaved modes
|
||||||
|
* Control frequency of system clock on macOS 10.13 and later
|
||||||
|
* Set TAI-UTC offset of system clock with leapsectz directive
|
||||||
|
* Minimise data in client requests to improve privacy
|
||||||
|
* Allow transmit-only hardware timestamping
|
||||||
|
* Add support for new timestamping options introduced in Linux 4.13
|
||||||
|
* Add root delay, root dispersion and maximum error to tracking log
|
||||||
|
* Add mindelay and asymmetry options to server/peer/pool directive
|
||||||
|
* Add extpps option to PHC refclock to timestamp external PPS signal
|
||||||
|
* Add pps option to refclock directive to treat any refclock as PPS
|
||||||
|
* Add width option to refclock directive to filter wrong pulse edges
|
||||||
|
* Add rxfilter option to hwtimestamp directive
|
||||||
|
* Add -x option to disable control of system clock
|
||||||
|
* Add -l option to log to specified file instead of syslog
|
||||||
|
* Allow multiple command-line options to be specified together
|
||||||
|
* Allow starting without root privileges with -Q option
|
||||||
|
* Update seccomp filter for new glibc versions
|
||||||
|
* Dump history on exit by default with dumpdir directive
|
||||||
|
* Use hardening compiler options by default
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
---------
|
||||||
|
* Don't drop PHC samples with low-resolution system clock
|
||||||
|
* Ignore outliers in PHC tracking, RTC tracking, manual input
|
||||||
|
* Increase polling interval when peer is not responding
|
||||||
|
* Exit with error message when include directive fails
|
||||||
|
* Don't allow slash after hostname in allow/deny directive/command
|
||||||
|
* Try to connect to all addresses in chronyc before giving up
|
||||||
|
|
||||||
|
New in version 3.1
|
||||||
|
==================
|
||||||
|
|
||||||
|
Enhancements
|
||||||
|
------------
|
||||||
|
* Add support for precise cross timestamping of PHC on Linux
|
||||||
|
* Add minpoll, precision, nocrossts options to hwtimestamp directive
|
||||||
|
* Add rawmeasurements option to log directive and modify measurements
|
||||||
|
option to log only valid measurements from synchronised sources
|
||||||
|
* Allow sub-second polling interval with NTP sources
|
||||||
|
|
||||||
|
Bug fixes
|
||||||
|
---------
|
||||||
|
* Fix time smoothing in interleaved mode
|
||||||
|
|
||||||
New in version 3.0
|
New in version 3.0
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
|||||||
126
README
126
README
@@ -4,7 +4,7 @@ What is chrony?
|
|||||||
===============
|
===============
|
||||||
|
|
||||||
chrony is a versatile implementation of the Network Time Protocol (NTP).
|
chrony is a versatile implementation of the Network Time Protocol (NTP).
|
||||||
It can synchronize the system clock with NTP servers, reference clocks
|
It can synchronise the system clock with NTP servers, reference clocks
|
||||||
(e.g. GPS receiver), and manual input using wristwatch and keyboard.
|
(e.g. GPS receiver), and manual input using wristwatch and keyboard.
|
||||||
It can also operate as an NTPv4 (RFC 5905) server and peer to provide
|
It can also operate as an NTPv4 (RFC 5905) server and peer to provide
|
||||||
a time service to other computers in the network.
|
a time service to other computers in the network.
|
||||||
@@ -37,20 +37,16 @@ How do I set it up?
|
|||||||
===================
|
===================
|
||||||
|
|
||||||
The file INSTALL gives instructions. On supported systems the
|
The file INSTALL gives instructions. On supported systems the
|
||||||
compilation process should be automatic.
|
compilation process should be automatic. You will need a C compiler,
|
||||||
|
e.g. gcc or clang.
|
||||||
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.
|
|
||||||
|
|
||||||
What documentation is there?
|
What documentation is there?
|
||||||
============================
|
============================
|
||||||
|
|
||||||
A manual is supplied in Texinfo format (chrony.texi) and
|
The distribution includes manual pages and a document containing
|
||||||
ready-formatted plain text (chrony.txt) in the distribution.
|
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
|
through the URL
|
||||||
|
|
||||||
https://chrony.tuxfamily.org/
|
https://chrony.tuxfamily.org/
|
||||||
@@ -112,127 +108,57 @@ The following people have provided patches and other major contributions
|
|||||||
to the program :
|
to the program :
|
||||||
|
|
||||||
Lonnie Abelbeck <lonnie@abelbeck.com>
|
Lonnie Abelbeck <lonnie@abelbeck.com>
|
||||||
Patch to add tab-completion to chronyc
|
|
||||||
|
|
||||||
Benny Lyne Amorsen <benny@amorsen.dk>
|
Benny Lyne Amorsen <benny@amorsen.dk>
|
||||||
Patch to add minstratum option
|
|
||||||
|
|
||||||
Andrew Bishop <amb@gedanken.demon.co.uk>
|
Andrew Bishop <amb@gedanken.demon.co.uk>
|
||||||
Fixes for bugs in logging when in daemon mode
|
Vincent Blut <vincent.debian@free.fr>
|
||||||
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.
|
|
||||||
|
|
||||||
Stephan I. Boettcher <stephan@nevis1.columbia.edu>
|
Stephan I. Boettcher <stephan@nevis1.columbia.edu>
|
||||||
Entries in contrib directory
|
Goswin Brederlow <brederlo@informatik.uni-tuebingen.de>
|
||||||
|
Leigh Brown <leigh@solinno.co.uk>
|
||||||
Erik Bryer <ebryer@spots.ab.ca>
|
Erik Bryer <ebryer@spots.ab.ca>
|
||||||
Entries in contrib directory
|
Jonathan Cameron <jic23@cam.ac.uk>
|
||||||
|
|
||||||
Bryan Christianson <bryan@whatroute.net>
|
Bryan Christianson <bryan@whatroute.net>
|
||||||
Support for macOS
|
|
||||||
Support for privilege separation
|
|
||||||
Entries in contrib directory
|
|
||||||
|
|
||||||
Juliusz Chroboczek <jch@pps.jussieu.fr>
|
Juliusz Chroboczek <jch@pps.jussieu.fr>
|
||||||
Fix install rule in Makefile if chronyd file is in use.
|
Christian Ehrhardt <christian.ehrhardt@canonical.com>
|
||||||
|
|
||||||
Paul Elliott <pelliott@io.com>
|
Paul Elliott <pelliott@io.com>
|
||||||
DNSchrony (in contrib directory), a tool for handling NTP servers
|
Stefan R. Filipek <srfilipek@gmail.com>
|
||||||
with variable IP addresses.
|
|
||||||
|
|
||||||
Mike Fleetwood <mike@rockover.demon.co.uk>
|
Mike Fleetwood <mike@rockover.demon.co.uk>
|
||||||
Fixes for compiler warnings
|
|
||||||
|
|
||||||
Alexander Gretencord <arutha@gmx.de>
|
Alexander Gretencord <arutha@gmx.de>
|
||||||
Changes to installation directory system to make it easier for
|
|
||||||
package builders.
|
|
||||||
|
|
||||||
Andrew Griffiths <agriffit@redhat.com>
|
Andrew Griffiths <agriffit@redhat.com>
|
||||||
Patch to add support for seccomp filter
|
|
||||||
|
|
||||||
Walter Haidinger <walter.haidinger@gmx.at>
|
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>
|
Juergen Hannken-Illjes <hannken@eis.cs.tu-bs.de>
|
||||||
Port to NetBSD
|
|
||||||
|
|
||||||
John Hasler <john@dhh.gt.org>
|
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>
|
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>
|
Liam Hatton <me@liamhatton.com>
|
||||||
Advice on configuring for Linux on PPC
|
|
||||||
|
|
||||||
Jachym Holecek <jakym@volny.cz>
|
Jachym Holecek <jakym@volny.cz>
|
||||||
Patch to make Linux real time clock work with devfs
|
|
||||||
|
|
||||||
Håkan Johansson <f96hajo@chalmers.se>
|
Håkan Johansson <f96hajo@chalmers.se>
|
||||||
Patch to avoid large values in sources and sourcestats output
|
|
||||||
|
|
||||||
Jim Knoble <jmknoble@pobox.com>
|
Jim Knoble <jmknoble@pobox.com>
|
||||||
Fixes for compiler warnings
|
|
||||||
|
|
||||||
Antti Jrvinen <costello@iki.fi>
|
Antti Jrvinen <costello@iki.fi>
|
||||||
Advice on configuring for BSD/386
|
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>
|
Victor Moroz <vim@prv.adlum.ru>
|
||||||
Patch to support Linux with HZ!=100
|
|
||||||
|
|
||||||
Kalle Olavi Niemitalo <tosi@stekt.oulu.fi>
|
Kalle Olavi Niemitalo <tosi@stekt.oulu.fi>
|
||||||
acquisitionport support
|
|
||||||
|
|
||||||
Frank Otto <sandwichmacher@web.de>
|
Frank Otto <sandwichmacher@web.de>
|
||||||
Handling arbitrary HZ values
|
|
||||||
|
|
||||||
Denny Page <dennypage@me.com>
|
Denny Page <dennypage@me.com>
|
||||||
Advice on support for hardware timestamping
|
Chris Perl <cperl@janestreet.com>
|
||||||
|
|
||||||
Gautier PHILIPPON <gautier.philippon@ensimag.grenoble-inp.fr>
|
Gautier PHILIPPON <gautier.philippon@ensimag.grenoble-inp.fr>
|
||||||
Patch to add refresh command to chronyc
|
|
||||||
|
|
||||||
Andreas Piesk <apiesk@virbus.de>
|
Andreas Piesk <apiesk@virbus.de>
|
||||||
Patch to make chronyc use the readline library if available
|
Andreas Steinmetz <ast@domdv.de>
|
||||||
|
NAKAMURA Takumi <takumi@ps.sakura.ne.jp>
|
||||||
Timo Teras <timo.teras@iki.fi>
|
Timo Teras <timo.teras@iki.fi>
|
||||||
Patch to reply correctly on multihomed hosts
|
|
||||||
|
|
||||||
Bill Unruh <unruh@physics.ubc.ca>
|
Bill Unruh <unruh@physics.ubc.ca>
|
||||||
Advice on statistics
|
|
||||||
|
|
||||||
Stephen Wadeley <swadeley@redhat.com>
|
Stephen Wadeley <swadeley@redhat.com>
|
||||||
Improvements to man pages
|
Bernhard Weiss <lisnablagh@web.de>
|
||||||
|
|
||||||
Wolfgang Weisselberg <weissel@netcologne.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>
|
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
|
||||||
Many robustness and security improvements
|
Ulrich Windl <ulrich.windl@rz.uni-regensburg.de>
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Doug Woodward <dougw@whistler.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
|
Many other people have contributed bug reports and suggestions. We are sorry
|
||||||
we cannot identify all of you individually.
|
we cannot identify all of you individually.
|
||||||
|
|||||||
12
addressing.h
12
addressing.h
@@ -30,16 +30,19 @@
|
|||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
/* This type is used to represent an IPv4 address or IPv6 address.
|
/* 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. */
|
All parts are in HOST order, NOT network order. */
|
||||||
|
|
||||||
#define IPADDR_UNSPEC 0
|
#define IPADDR_UNSPEC 0
|
||||||
#define IPADDR_INET4 1
|
#define IPADDR_INET4 1
|
||||||
#define IPADDR_INET6 2
|
#define IPADDR_INET6 2
|
||||||
|
#define IPADDR_ID 3
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
union {
|
union {
|
||||||
uint32_t in4;
|
uint32_t in4;
|
||||||
uint8_t in6[16];
|
uint8_t in6[16];
|
||||||
|
uint32_t id;
|
||||||
} addr;
|
} addr;
|
||||||
uint16_t family;
|
uint16_t family;
|
||||||
uint16_t _pad;
|
uint16_t _pad;
|
||||||
@@ -47,11 +50,16 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
IPAddr ip_addr;
|
IPAddr ip_addr;
|
||||||
unsigned short port;
|
uint16_t port;
|
||||||
} NTP_Remote_Address;
|
} IPSockAddr;
|
||||||
|
|
||||||
|
typedef IPSockAddr NTP_Remote_Address;
|
||||||
|
|
||||||
|
#define INVALID_IF_INDEX -1
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
IPAddr ip_addr;
|
IPAddr ip_addr;
|
||||||
|
int if_index;
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
} NTP_Local_Address;
|
} NTP_Local_Address;
|
||||||
|
|
||||||
|
|||||||
@@ -247,6 +247,8 @@ set_subnet_(ADF_AuthTable table,
|
|||||||
set_subnet(&table->base6, ip6, 4, 0, new_state, delete_children) == ADF_SUCCESS)
|
set_subnet(&table->base6, ip6, 4, 0, new_state, delete_children) == ADF_SUCCESS)
|
||||||
return ADF_SUCCESS;
|
return ADF_SUCCESS;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ADF_BADSUBNET;
|
return ADF_BADSUBNET;
|
||||||
@@ -359,9 +361,9 @@ ADF_IsAllowed(ADF_AuthTable table,
|
|||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
split_ip6(ip_addr, ip6);
|
split_ip6(ip_addr, ip6);
|
||||||
return check_ip_in_node(&table->base6, ip6);
|
return check_ip_in_node(&table->base6, ip6);
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|||||||
7
array.c
7
array.c
@@ -66,8 +66,6 @@ ARR_DestroyInstance(ARR_Instance array)
|
|||||||
static void
|
static void
|
||||||
realloc_array(ARR_Instance array, unsigned int min_size)
|
realloc_array(ARR_Instance array, unsigned int min_size)
|
||||||
{
|
{
|
||||||
size_t data_size;
|
|
||||||
|
|
||||||
assert(min_size <= 2 * min_size);
|
assert(min_size <= 2 * min_size);
|
||||||
if (array->allocated >= min_size && array->allocated <= 2 * min_size)
|
if (array->allocated >= min_size && array->allocated <= 2 * min_size)
|
||||||
return;
|
return;
|
||||||
@@ -79,10 +77,7 @@ realloc_array(ARR_Instance array, unsigned int min_size)
|
|||||||
array->allocated = min_size;
|
array->allocated = min_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
data_size = (size_t)array->elem_size * array->allocated;
|
array->data = Realloc2(array->data, array->allocated, array->elem_size);
|
||||||
assert(data_size / array->elem_size == array->allocated);
|
|
||||||
|
|
||||||
array->data = Realloc(array->data, data_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
|
|||||||
49
candm.h
49
candm.h
@@ -97,7 +97,14 @@
|
|||||||
#define REQ_NTP_DATA 57
|
#define REQ_NTP_DATA 57
|
||||||
#define REQ_ADD_SERVER2 58
|
#define REQ_ADD_SERVER2 58
|
||||||
#define REQ_ADD_PEER2 59
|
#define REQ_ADD_PEER2 59
|
||||||
#define N_REQUEST_TYPES 60
|
#define REQ_ADD_SERVER3 60
|
||||||
|
#define REQ_ADD_PEER3 61
|
||||||
|
#define REQ_SHUTDOWN 62
|
||||||
|
#define REQ_ONOFFLINE 63
|
||||||
|
#define REQ_ADD_SOURCE 64
|
||||||
|
#define REQ_NTP_SOURCE_NAME 65
|
||||||
|
#define REQ_RESET 66
|
||||||
|
#define N_REQUEST_TYPES 67
|
||||||
|
|
||||||
/* Structure used to exchange timespecs independent of time_t size */
|
/* Structure used to exchange timespecs independent of time_t size */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -241,6 +248,11 @@ typedef struct {
|
|||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
} REQ_Ac_Check;
|
} 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 */
|
/* Flags used in NTP source requests */
|
||||||
#define REQ_ADDSRC_ONLINE 0x1
|
#define REQ_ADDSRC_ONLINE 0x1
|
||||||
#define REQ_ADDSRC_AUTOOFFLINE 0x2
|
#define REQ_ADDSRC_AUTOOFFLINE 0x2
|
||||||
@@ -250,9 +262,12 @@ typedef struct {
|
|||||||
#define REQ_ADDSRC_TRUST 0x20
|
#define REQ_ADDSRC_TRUST 0x20
|
||||||
#define REQ_ADDSRC_REQUIRE 0x40
|
#define REQ_ADDSRC_REQUIRE 0x40
|
||||||
#define REQ_ADDSRC_INTERLEAVED 0x80
|
#define REQ_ADDSRC_INTERLEAVED 0x80
|
||||||
|
#define REQ_ADDSRC_BURST 0x100
|
||||||
|
#define REQ_ADDSRC_NTS 0x200
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
IPAddr ip_addr;
|
uint32_t type;
|
||||||
|
int8_t name[256];
|
||||||
uint32_t port;
|
uint32_t port;
|
||||||
int32_t minpoll;
|
int32_t minpoll;
|
||||||
int32_t maxpoll;
|
int32_t maxpoll;
|
||||||
@@ -264,11 +279,16 @@ typedef struct {
|
|||||||
int32_t min_samples;
|
int32_t min_samples;
|
||||||
int32_t max_samples;
|
int32_t max_samples;
|
||||||
uint32_t authkey;
|
uint32_t authkey;
|
||||||
|
uint32_t nts_port;
|
||||||
Float max_delay;
|
Float max_delay;
|
||||||
Float max_delay_ratio;
|
Float max_delay_ratio;
|
||||||
Float max_delay_dev_ratio;
|
Float max_delay_dev_ratio;
|
||||||
|
Float min_delay;
|
||||||
|
Float asymmetry;
|
||||||
Float offset;
|
Float offset;
|
||||||
uint32_t flags;
|
uint32_t flags;
|
||||||
|
int32_t filter_length;
|
||||||
|
uint32_t reserved[3];
|
||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
} REQ_NTP_Source;
|
} REQ_NTP_Source;
|
||||||
|
|
||||||
@@ -326,6 +346,11 @@ typedef struct {
|
|||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
} REQ_NTPData;
|
} REQ_NTPData;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
IPAddr ip_addr;
|
||||||
|
int32_t EOR;
|
||||||
|
} REQ_NTPSourceName;
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
#define PKT_TYPE_CMD_REQUEST 1
|
#define PKT_TYPE_CMD_REQUEST 1
|
||||||
@@ -362,8 +387,9 @@ typedef struct {
|
|||||||
domain socket.
|
domain socket.
|
||||||
|
|
||||||
Version 6 (no authentication) : changed format of client accesses by index
|
Version 6 (no authentication) : changed format of client accesses by index
|
||||||
(using new request/reply types), new fields and flags in NTP source request
|
(using new request/reply types) and manual timestamp, added new fields and
|
||||||
and report, new commands: ntpdata, refresh, serverstats
|
flags to NTP source request and report, made length of manual list constant,
|
||||||
|
added new commands: ntpdata, refresh, serverstats, shutdown
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define PROTO_VERSION_NUMBER 6
|
#define PROTO_VERSION_NUMBER 6
|
||||||
@@ -427,6 +453,7 @@ typedef struct {
|
|||||||
REQ_ReselectDistance reselect_distance;
|
REQ_ReselectDistance reselect_distance;
|
||||||
REQ_SmoothTime smoothtime;
|
REQ_SmoothTime smoothtime;
|
||||||
REQ_NTPData ntp_data;
|
REQ_NTPData ntp_data;
|
||||||
|
REQ_NTPData ntp_source_name;
|
||||||
} data; /* Command specific parameters */
|
} data; /* Command specific parameters */
|
||||||
|
|
||||||
/* Padding used to prevent traffic amplification. It only defines the
|
/* Padding used to prevent traffic amplification. It only defines the
|
||||||
@@ -461,7 +488,10 @@ typedef struct {
|
|||||||
#define RPY_SERVER_STATS 14
|
#define RPY_SERVER_STATS 14
|
||||||
#define RPY_CLIENT_ACCESSES_BY_INDEX2 15
|
#define RPY_CLIENT_ACCESSES_BY_INDEX2 15
|
||||||
#define RPY_NTP_DATA 16
|
#define RPY_NTP_DATA 16
|
||||||
#define N_REPLY_TYPES 17
|
#define RPY_MANUAL_TIMESTAMP2 17
|
||||||
|
#define RPY_MANUAL_LIST2 18
|
||||||
|
#define RPY_NTP_SOURCE_NAME 19
|
||||||
|
#define N_REPLY_TYPES 20
|
||||||
|
|
||||||
/* Status codes */
|
/* Status codes */
|
||||||
#define STT_SUCCESS 0
|
#define STT_SUCCESS 0
|
||||||
@@ -485,6 +515,7 @@ typedef struct {
|
|||||||
#define STT_INVALIDAF 17
|
#define STT_INVALIDAF 17
|
||||||
#define STT_BADPKTVERSION 18
|
#define STT_BADPKTVERSION 18
|
||||||
#define STT_BADPKTLENGTH 19
|
#define STT_BADPKTLENGTH 19
|
||||||
|
#define STT_INVALIDNAME 21
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
@@ -569,7 +600,7 @@ typedef struct {
|
|||||||
} RPY_Rtc;
|
} RPY_Rtc;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t centiseconds;
|
Float offset;
|
||||||
Float dfreq_ppm;
|
Float dfreq_ppm;
|
||||||
Float new_afreq_ppm;
|
Float new_afreq_ppm;
|
||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
@@ -676,6 +707,11 @@ typedef struct {
|
|||||||
int32_t EOR;
|
int32_t EOR;
|
||||||
} RPY_NTPData;
|
} RPY_NTPData;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int8_t name[256];
|
||||||
|
int32_t EOR;
|
||||||
|
} RPY_NTPSourceName;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t version;
|
uint8_t version;
|
||||||
uint8_t pkt_type;
|
uint8_t pkt_type;
|
||||||
@@ -705,6 +741,7 @@ typedef struct {
|
|||||||
RPY_Activity activity;
|
RPY_Activity activity;
|
||||||
RPY_Smoothing smoothing;
|
RPY_Smoothing smoothing;
|
||||||
RPY_NTPData ntp_data;
|
RPY_NTPData ntp_data;
|
||||||
|
RPY_NTPSourceName ntp_source_name;
|
||||||
} data; /* Reply specific parameters */
|
} data; /* Reply specific parameters */
|
||||||
|
|
||||||
} CMD_Reply;
|
} CMD_Reply;
|
||||||
|
|||||||
23
clientlog.c
23
clientlog.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Miroslav Lichvar 2009, 2015-2016
|
* Copyright (C) Miroslav Lichvar 2009, 2015-2017
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -128,6 +128,9 @@ static int cmd_leak_rate;
|
|||||||
/* Flag indicating whether the last response was dropped */
|
/* Flag indicating whether the last response was dropped */
|
||||||
#define FLAG_NTP_DROPPED 0x1
|
#define FLAG_NTP_DROPPED 0x1
|
||||||
|
|
||||||
|
/* NTP limit interval in log2 */
|
||||||
|
static int ntp_limit_interval;
|
||||||
|
|
||||||
/* Flag indicating whether facility is turned on or not */
|
/* Flag indicating whether facility is turned on or not */
|
||||||
static int active;
|
static int active;
|
||||||
|
|
||||||
@@ -294,7 +297,7 @@ set_bucket_params(int interval, int burst, uint16_t *max_tokens,
|
|||||||
*tokens_per_packet = 1U << (TS_FRAC + interval - *token_shift);
|
*tokens_per_packet = 1U << (TS_FRAC + interval - *token_shift);
|
||||||
*max_tokens = *tokens_per_packet * burst;
|
*max_tokens = *tokens_per_packet * burst;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_ClientLog, "Tokens max %d packet %d shift %d",
|
DEBUG_LOG("Tokens max %d packet %d shift %d",
|
||||||
*max_tokens, *tokens_per_packet, *token_shift);
|
*max_tokens, *tokens_per_packet, *token_shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,11 +312,13 @@ CLG_Initialise(void)
|
|||||||
ntp_tokens_per_packet = cmd_tokens_per_packet = 0;
|
ntp_tokens_per_packet = cmd_tokens_per_packet = 0;
|
||||||
ntp_token_shift = cmd_token_shift = 0;
|
ntp_token_shift = cmd_token_shift = 0;
|
||||||
ntp_leak_rate = cmd_leak_rate = 0;
|
ntp_leak_rate = cmd_leak_rate = 0;
|
||||||
|
ntp_limit_interval = MIN_LIMIT_INTERVAL;
|
||||||
|
|
||||||
if (CNF_GetNTPRateLimit(&interval, &burst, &leak_rate)) {
|
if (CNF_GetNTPRateLimit(&interval, &burst, &leak_rate)) {
|
||||||
set_bucket_params(interval, burst, &max_ntp_tokens, &ntp_tokens_per_packet,
|
set_bucket_params(interval, burst, &max_ntp_tokens, &ntp_tokens_per_packet,
|
||||||
&ntp_token_shift);
|
&ntp_token_shift);
|
||||||
ntp_leak_rate = CLAMP(MIN_LEAK_RATE, leak_rate, MAX_LEAK_RATE);
|
ntp_leak_rate = CLAMP(MIN_LEAK_RATE, leak_rate, MAX_LEAK_RATE);
|
||||||
|
ntp_limit_interval = CLAMP(MIN_LIMIT_INTERVAL, interval, MAX_LIMIT_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CNF_GetCommandRateLimit(&interval, &burst, &leak_rate)) {
|
if (CNF_GetCommandRateLimit(&interval, &burst, &leak_rate)) {
|
||||||
@@ -325,7 +330,7 @@ CLG_Initialise(void)
|
|||||||
active = !CNF_GetNoClientLog();
|
active = !CNF_GetNoClientLog();
|
||||||
if (!active) {
|
if (!active) {
|
||||||
if (ntp_leak_rate || cmd_leak_rate)
|
if (ntp_leak_rate || cmd_leak_rate)
|
||||||
LOG_FATAL(LOGF_ClientLog, "ratelimit cannot be used with noclientlog");
|
LOG_FATAL("ratelimit cannot be used with noclientlog");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,7 +475,7 @@ CLG_LogNTPAccess(IPAddr *client, struct timespec *now)
|
|||||||
record->flags & FLAG_NTP_DROPPED ?
|
record->flags & FLAG_NTP_DROPPED ?
|
||||||
&record->ntp_timeout_rate : &record->ntp_rate);
|
&record->ntp_timeout_rate : &record->ntp_rate);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_ClientLog, "NTP hits %"PRIu32" rate %d trate %d tokens %d",
|
DEBUG_LOG("NTP hits %"PRIu32" rate %d trate %d tokens %d",
|
||||||
record->ntp_hits, record->ntp_rate, record->ntp_timeout_rate,
|
record->ntp_hits, record->ntp_rate, record->ntp_timeout_rate,
|
||||||
record->ntp_tokens);
|
record->ntp_tokens);
|
||||||
|
|
||||||
@@ -494,7 +499,7 @@ CLG_LogCommandAccess(IPAddr *client, struct timespec *now)
|
|||||||
&record->cmd_tokens, max_cmd_tokens, cmd_token_shift,
|
&record->cmd_tokens, max_cmd_tokens, cmd_token_shift,
|
||||||
&record->cmd_rate);
|
&record->cmd_rate);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_ClientLog, "Cmd hits %"PRIu32" rate %d tokens %d",
|
DEBUG_LOG("Cmd hits %"PRIu32" rate %d tokens %d",
|
||||||
record->cmd_hits, record->cmd_rate, record->cmd_tokens);
|
record->cmd_hits, record->cmd_rate, record->cmd_tokens);
|
||||||
|
|
||||||
return get_index(record);
|
return get_index(record);
|
||||||
@@ -606,6 +611,14 @@ void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CLG_GetNtpMinPoll(void)
|
||||||
|
{
|
||||||
|
return ntp_limit_interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
CLG_GetNumberOfIndices(void)
|
CLG_GetNumberOfIndices(void)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ extern int CLG_LogCommandAccess(IPAddr *client, struct timespec *now);
|
|||||||
extern int CLG_LimitNTPResponseRate(int index);
|
extern int CLG_LimitNTPResponseRate(int index);
|
||||||
extern int CLG_LimitCommandResponseRate(int index);
|
extern int CLG_LimitCommandResponseRate(int index);
|
||||||
extern void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts);
|
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. */
|
/* And some reporting functions, for use by chronyc. */
|
||||||
|
|
||||||
|
|||||||
41
cmac.h
Normal file
41
cmac.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
|
typedef struct CMC_Instance_Record *CMC_Instance;
|
||||||
|
|
||||||
|
extern unsigned int CMC_GetKeyLength(const char *cipher);
|
||||||
|
extern CMC_Instance CMC_CreateInstance(const char *cipher, const unsigned char *key,
|
||||||
|
unsigned int length);
|
||||||
|
extern unsigned int CMC_Hash(CMC_Instance inst, const unsigned char *in, unsigned int in_len,
|
||||||
|
unsigned char *out, unsigned int out_len);
|
||||||
|
extern void CMC_DestroyInstance(CMC_Instance inst);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
115
cmac_nettle.c
Normal file
115
cmac_nettle.c
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
unsigned int
|
||||||
|
CMC_GetKeyLength(const char *cipher)
|
||||||
|
{
|
||||||
|
if (strcmp(cipher, "AES128") == 0)
|
||||||
|
return AES128_KEY_SIZE;
|
||||||
|
else if (strcmp(cipher, "AES256") == 0)
|
||||||
|
return AES256_KEY_SIZE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
CMC_Instance
|
||||||
|
CMC_CreateInstance(const char *cipher, const unsigned char *key, unsigned int length)
|
||||||
|
{
|
||||||
|
CMC_Instance inst;
|
||||||
|
|
||||||
|
if (length == 0 || length != CMC_GetKeyLength(cipher))
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
unsigned int
|
||||||
|
CMC_Hash(CMC_Instance inst, const unsigned char *in, unsigned int in_len,
|
||||||
|
unsigned char *out, unsigned int out_len)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
572
cmdmon.c
572
cmdmon.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Miroslav Lichvar 2009-2016
|
* Copyright (C) Miroslav Lichvar 2009-2016, 2018
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -38,6 +38,7 @@
|
|||||||
#include "ntp_sources.h"
|
#include "ntp_sources.h"
|
||||||
#include "ntp_core.h"
|
#include "ntp_core.h"
|
||||||
#include "smooth.h"
|
#include "smooth.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "sources.h"
|
#include "sources.h"
|
||||||
#include "sourcestats.h"
|
#include "sourcestats.h"
|
||||||
#include "reference.h"
|
#include "reference.h"
|
||||||
@@ -53,21 +54,12 @@
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
union sockaddr_all {
|
#define INVALID_SOCK_FD (-5)
|
||||||
struct sockaddr_in in4;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
struct sockaddr_in6 in6;
|
|
||||||
#endif
|
|
||||||
struct sockaddr_un un;
|
|
||||||
struct sockaddr sa;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* File descriptors for command and monitoring sockets */
|
/* File descriptors for command and monitoring sockets */
|
||||||
static int sock_fdu;
|
static int sock_fdu;
|
||||||
static int sock_fd4;
|
static int sock_fd4;
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
static int sock_fd6;
|
static int sock_fd6;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Flag indicating whether this module has been initialised or not */
|
/* Flag indicating whether this module has been initialised or not */
|
||||||
static int initialised = 0;
|
static int initialised = 0;
|
||||||
@@ -136,6 +128,13 @@ static const char permissions[] = {
|
|||||||
PERMIT_AUTH, /* NTP_DATA */
|
PERMIT_AUTH, /* NTP_DATA */
|
||||||
PERMIT_AUTH, /* ADD_SERVER2 */
|
PERMIT_AUTH, /* ADD_SERVER2 */
|
||||||
PERMIT_AUTH, /* ADD_PEER2 */
|
PERMIT_AUTH, /* ADD_PEER2 */
|
||||||
|
PERMIT_AUTH, /* ADD_SERVER3 */
|
||||||
|
PERMIT_AUTH, /* ADD_PEER3 */
|
||||||
|
PERMIT_AUTH, /* SHUTDOWN */
|
||||||
|
PERMIT_AUTH, /* ONOFFLINE */
|
||||||
|
PERMIT_AUTH, /* ADD_SOURCE */
|
||||||
|
PERMIT_OPEN, /* NTP_SOURCE_NAME */
|
||||||
|
PERMIT_AUTH, /* RESET */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -151,99 +150,46 @@ static void read_from_cmd_socket(int sock_fd, int event, void *anything);
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
prepare_socket(int family, int port_number)
|
open_socket(int family)
|
||||||
{
|
{
|
||||||
int sock_fd;
|
IPSockAddr local_addr;
|
||||||
socklen_t my_addr_len;
|
const char *local_path;
|
||||||
union sockaddr_all my_addr;
|
int sock_fd, port;
|
||||||
IPAddr bind_address;
|
|
||||||
int on_off = 1;
|
|
||||||
|
|
||||||
sock_fd = socket(family, SOCK_DGRAM, 0);
|
|
||||||
if (sock_fd < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_CmdMon, "Could not open %s command socket : %s",
|
|
||||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Close on exec */
|
|
||||||
UTI_FdSetCloexec(sock_fd);
|
|
||||||
|
|
||||||
if (family != AF_UNIX) {
|
|
||||||
/* Allow reuse of port number */
|
|
||||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on_off, sizeof(on_off)) < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_CmdMon, "Could not set reuseaddr socket options");
|
|
||||||
/* Don't quit - we might survive anyway */
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef IP_FREEBIND
|
|
||||||
/* Allow binding to address that doesn't exist yet */
|
|
||||||
if (setsockopt(sock_fd, IPPROTO_IP, IP_FREEBIND, (char *)&on_off, sizeof(on_off)) < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_CmdMon, "Could not set free bind socket option");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
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, LOGF_CmdMon, "Could not request IPV6_V6ONLY socket option");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&my_addr, 0, sizeof (my_addr));
|
|
||||||
|
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case AF_INET:
|
case IPADDR_INET4:
|
||||||
my_addr_len = sizeof (my_addr.in4);
|
case IPADDR_INET6:
|
||||||
my_addr.in4.sin_family = family;
|
port = CNF_GetCommandPort();
|
||||||
my_addr.in4.sin_port = htons((unsigned short)port_number);
|
if (port == 0 || !SCK_IsFamilySupported(family))
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
|
||||||
CNF_GetBindCommandAddress(IPADDR_INET4, &bind_address);
|
CNF_GetBindCommandAddress(family, &local_addr.ip_addr);
|
||||||
|
if (local_addr.ip_addr.family != family)
|
||||||
|
SCK_GetLoopbackIPAddress(family, &local_addr.ip_addr);
|
||||||
|
local_addr.port = port;
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenUdpSocket(NULL, &local_addr, SCK_FLAG_RX_DEST_ADDR);
|
||||||
|
if (sock_fd < 0) {
|
||||||
|
LOG(LOGS_ERR, "Could not open command socket on %s",
|
||||||
|
UTI_IPSockAddrToString(&local_addr));
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
}
|
||||||
|
|
||||||
if (bind_address.family == IPADDR_INET4)
|
|
||||||
my_addr.in4.sin_addr.s_addr = htonl(bind_address.addr.in4);
|
|
||||||
else
|
|
||||||
my_addr.in4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
||||||
break;
|
break;
|
||||||
#ifdef FEAT_IPV6
|
case IPADDR_UNSPEC:
|
||||||
case AF_INET6:
|
local_path = CNF_GetBindCommandPath();
|
||||||
my_addr_len = sizeof (my_addr.in6);
|
|
||||||
my_addr.in6.sin6_family = family;
|
|
||||||
my_addr.in6.sin6_port = htons((unsigned short)port_number);
|
|
||||||
|
|
||||||
CNF_GetBindCommandAddress(IPADDR_INET6, &bind_address);
|
sock_fd = SCK_OpenUnixDatagramSocket(NULL, local_path, 0);
|
||||||
|
if (sock_fd < 0) {
|
||||||
|
LOG(LOGS_ERR, "Could not open command socket on %s", local_path);
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
}
|
||||||
|
|
||||||
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
|
|
||||||
my_addr.in6.sin6_addr = in6addr_loopback;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case AF_UNIX:
|
|
||||||
my_addr_len = sizeof (my_addr.un);
|
|
||||||
my_addr.un.sun_family = family;
|
|
||||||
if (snprintf(my_addr.un.sun_path, sizeof (my_addr.un.sun_path), "%s",
|
|
||||||
CNF_GetBindCommandPath()) >= sizeof (my_addr.un.sun_path))
|
|
||||||
LOG_FATAL(LOGF_CmdMon, "Unix socket path too long");
|
|
||||||
unlink(my_addr.un.sun_path);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sock_fd, &my_addr.sa, my_addr_len) < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_CmdMon, "Could not bind %s command socket : %s",
|
|
||||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
|
||||||
close(sock_fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Register handler for read events on the socket */
|
/* Register handler for read events on the socket */
|
||||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_from_cmd_socket, NULL);
|
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_from_cmd_socket, NULL);
|
||||||
|
|
||||||
@@ -276,7 +222,6 @@ do_size_checks(void)
|
|||||||
for (i = 1; i < N_REPLY_TYPES; i++) {
|
for (i = 1; i < N_REPLY_TYPES; i++) {
|
||||||
reply.reply = htons(i);
|
reply.reply = htons(i);
|
||||||
reply.status = STT_SUCCESS;
|
reply.status = STT_SUCCESS;
|
||||||
reply.data.manual_list.n_samples = htonl(MAX_MANUAL_LIST_SAMPLES);
|
|
||||||
reply_length = PKL_ReplyLength(&reply);
|
reply_length = PKL_ReplyLength(&reply);
|
||||||
if ((reply_length && reply_length < offsetof(CMD_Reply, data)) ||
|
if ((reply_length && reply_length < offsetof(CMD_Reply, data)) ||
|
||||||
reply_length > sizeof (CMD_Reply))
|
reply_length > sizeof (CMD_Reply))
|
||||||
@@ -289,37 +234,22 @@ do_size_checks(void)
|
|||||||
void
|
void
|
||||||
CAM_Initialise(int family)
|
CAM_Initialise(int family)
|
||||||
{
|
{
|
||||||
int port_number;
|
|
||||||
|
|
||||||
assert(!initialised);
|
assert(!initialised);
|
||||||
assert(sizeof (permissions) / sizeof (permissions[0]) == N_REQUEST_TYPES);
|
assert(sizeof (permissions) / sizeof (permissions[0]) == N_REQUEST_TYPES);
|
||||||
do_size_checks();
|
do_size_checks();
|
||||||
|
|
||||||
initialised = 1;
|
initialised = 1;
|
||||||
sock_fdu = -1;
|
sock_fdu = INVALID_SOCK_FD;
|
||||||
port_number = CNF_GetCommandPort();
|
sock_fd4 = INVALID_SOCK_FD;
|
||||||
|
sock_fd6 = INVALID_SOCK_FD;
|
||||||
|
|
||||||
if (port_number && (family == IPADDR_UNSPEC || family == IPADDR_INET4))
|
if (family == IPADDR_UNSPEC || family == IPADDR_INET4)
|
||||||
sock_fd4 = prepare_socket(AF_INET, port_number);
|
sock_fd4 = open_socket(IPADDR_INET4);
|
||||||
else
|
|
||||||
sock_fd4 = -1;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
if (port_number && (family == IPADDR_UNSPEC || family == IPADDR_INET6))
|
|
||||||
sock_fd6 = prepare_socket(AF_INET6, port_number);
|
|
||||||
else
|
|
||||||
sock_fd6 = -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (port_number && sock_fd4 < 0
|
if (family == IPADDR_UNSPEC || family == IPADDR_INET6)
|
||||||
#ifdef FEAT_IPV6
|
sock_fd6 = open_socket(IPADDR_INET6);
|
||||||
&& sock_fd6 < 0
|
|
||||||
#endif
|
|
||||||
) {
|
|
||||||
LOG_FATAL(LOGF_CmdMon, "Could not open any command socket");
|
|
||||||
}
|
|
||||||
|
|
||||||
access_auth_table = ADF_CreateTable();
|
access_auth_table = ADF_CreateTable();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -327,24 +257,24 @@ CAM_Initialise(int family)
|
|||||||
void
|
void
|
||||||
CAM_Finalise(void)
|
CAM_Finalise(void)
|
||||||
{
|
{
|
||||||
if (sock_fdu >= 0) {
|
if (sock_fdu != INVALID_SOCK_FD) {
|
||||||
SCH_RemoveFileHandler(sock_fdu);
|
SCH_RemoveFileHandler(sock_fdu);
|
||||||
close(sock_fdu);
|
SCK_RemoveSocket(sock_fdu);
|
||||||
unlink(CNF_GetBindCommandPath());
|
SCK_CloseSocket(sock_fdu);
|
||||||
|
sock_fdu = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
sock_fdu = -1;
|
|
||||||
if (sock_fd4 >= 0) {
|
if (sock_fd4 != INVALID_SOCK_FD) {
|
||||||
SCH_RemoveFileHandler(sock_fd4);
|
SCH_RemoveFileHandler(sock_fd4);
|
||||||
close(sock_fd4);
|
SCK_CloseSocket(sock_fd4);
|
||||||
|
sock_fd4 = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
sock_fd4 = -1;
|
|
||||||
#ifdef FEAT_IPV6
|
if (sock_fd6 != INVALID_SOCK_FD) {
|
||||||
if (sock_fd6 >= 0) {
|
|
||||||
SCH_RemoveFileHandler(sock_fd6);
|
SCH_RemoveFileHandler(sock_fd6);
|
||||||
close(sock_fd6);
|
SCK_CloseSocket(sock_fd6);
|
||||||
|
sock_fd6 = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
sock_fd6 = -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ADF_DestroyTable(access_auth_table);
|
ADF_DestroyTable(access_auth_table);
|
||||||
|
|
||||||
@@ -359,50 +289,18 @@ CAM_OpenUnixSocket(void)
|
|||||||
/* This is separated from CAM_Initialise() as it needs to be called when
|
/* This is separated from CAM_Initialise() as it needs to be called when
|
||||||
the process has already dropped the root privileges */
|
the process has already dropped the root privileges */
|
||||||
if (CNF_GetBindCommandPath()[0])
|
if (CNF_GetBindCommandPath()[0])
|
||||||
sock_fdu = prepare_socket(AF_UNIX, 0);
|
sock_fdu = open_socket(IPADDR_UNSPEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
transmit_reply(CMD_Reply *msg, union sockaddr_all *where_to)
|
transmit_reply(int sock_fd, SCK_Message *message)
|
||||||
{
|
{
|
||||||
int status;
|
message->length = PKL_ReplyLength((CMD_Reply *)message->data);
|
||||||
int tx_message_length;
|
|
||||||
int sock_fd;
|
|
||||||
socklen_t addrlen;
|
|
||||||
|
|
||||||
switch (where_to->sa.sa_family) {
|
if (!SCK_SendMessage(sock_fd, message, 0))
|
||||||
case AF_INET:
|
|
||||||
sock_fd = sock_fd4;
|
|
||||||
addrlen = sizeof (where_to->in4);
|
|
||||||
break;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
case AF_INET6:
|
|
||||||
sock_fd = sock_fd6;
|
|
||||||
addrlen = sizeof (where_to->in6);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case AF_UNIX:
|
|
||||||
sock_fd = sock_fdu;
|
|
||||||
addrlen = sizeof (where_to->un);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
tx_message_length = PKL_ReplyLength(msg);
|
|
||||||
status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
|
|
||||||
&where_to->sa, addrlen);
|
|
||||||
|
|
||||||
if (status < 0) {
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Could not send to %s fd %d : %s",
|
|
||||||
UTI_SockaddrToString(&where_to->sa), sock_fd, strerror(errno));
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Sent %d bytes to %s fd %d", status,
|
|
||||||
UTI_SockaddrToString(&where_to->sa), sock_fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -422,7 +320,7 @@ handle_online(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
|
|
||||||
UTI_IPNetworkToHost(&rx_message->data.online.mask, &mask);
|
UTI_IPNetworkToHost(&rx_message->data.online.mask, &mask);
|
||||||
UTI_IPNetworkToHost(&rx_message->data.online.address, &address);
|
UTI_IPNetworkToHost(&rx_message->data.online.address, &address);
|
||||||
if (!NSR_TakeSourcesOnline(&mask, &address))
|
if (!NSR_SetConnectivity(&mask, &address, SRC_ONLINE))
|
||||||
tx_message->status = htons(STT_NOSUCHSOURCE);
|
tx_message->status = htons(STT_NOSUCHSOURCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,12 +333,24 @@ handle_offline(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
|
|
||||||
UTI_IPNetworkToHost(&rx_message->data.offline.mask, &mask);
|
UTI_IPNetworkToHost(&rx_message->data.offline.mask, &mask);
|
||||||
UTI_IPNetworkToHost(&rx_message->data.offline.address, &address);
|
UTI_IPNetworkToHost(&rx_message->data.offline.address, &address);
|
||||||
if (!NSR_TakeSourcesOffline(&mask, &address))
|
if (!NSR_SetConnectivity(&mask, &address, SRC_OFFLINE))
|
||||||
tx_message->status = htons(STT_NOSUCHSOURCE);
|
tx_message->status = htons(STT_NOSUCHSOURCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_onoffline(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
|
{
|
||||||
|
IPAddr address, mask;
|
||||||
|
|
||||||
|
address.family = mask.family = IPADDR_UNSPEC;
|
||||||
|
if (!NSR_SetConnectivity(&mask, &address, SRC_MAYBE_ONLINE))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_burst(CMD_Request *rx_message, CMD_Reply *tx_message)
|
handle_burst(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
{
|
{
|
||||||
@@ -568,14 +478,13 @@ static void
|
|||||||
handle_settime(CMD_Request *rx_message, CMD_Reply *tx_message)
|
handle_settime(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
long offset_cs;
|
double offset, dfreq_ppm, new_afreq_ppm;
|
||||||
double dfreq_ppm, new_afreq_ppm;
|
|
||||||
UTI_TimespecNetworkToHost(&rx_message->data.settime.ts, &ts);
|
UTI_TimespecNetworkToHost(&rx_message->data.settime.ts, &ts);
|
||||||
if (!MNL_IsEnabled()) {
|
if (!MNL_IsEnabled()) {
|
||||||
tx_message->status = htons(STT_NOTENABLED);
|
tx_message->status = htons(STT_NOTENABLED);
|
||||||
} else if (MNL_AcceptTimestamp(&ts, &offset_cs, &dfreq_ppm, &new_afreq_ppm)) {
|
} else if (MNL_AcceptTimestamp(&ts, &offset, &dfreq_ppm, &new_afreq_ppm)) {
|
||||||
tx_message->reply = htons(RPY_MANUAL_TIMESTAMP);
|
tx_message->reply = htons(RPY_MANUAL_TIMESTAMP2);
|
||||||
tx_message->data.manual_timestamp.centiseconds = htonl((int32_t)offset_cs);
|
tx_message->data.manual_timestamp.offset = UTI_FloatHostToNetwork(offset);
|
||||||
tx_message->data.manual_timestamp.dfreq_ppm = UTI_FloatHostToNetwork(dfreq_ppm);
|
tx_message->data.manual_timestamp.dfreq_ppm = UTI_FloatHostToNetwork(dfreq_ppm);
|
||||||
tx_message->data.manual_timestamp.new_afreq_ppm = UTI_FloatHostToNetwork(new_afreq_ppm);
|
tx_message->data.manual_timestamp.new_afreq_ppm = UTI_FloatHostToNetwork(new_afreq_ppm);
|
||||||
} else {
|
} else {
|
||||||
@@ -769,14 +678,41 @@ handle_cmdaccheck(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_message)
|
handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
{
|
{
|
||||||
NTP_Remote_Address rem_addr;
|
NTP_Source_Type type;
|
||||||
SourceParameters params;
|
SourceParameters params;
|
||||||
NSR_Status status;
|
NSR_Status status;
|
||||||
|
char *name;
|
||||||
|
int pool, port;
|
||||||
|
|
||||||
UTI_IPNetworkToHost(&rx_message->data.ntp_source.ip_addr, &rem_addr.ip_addr);
|
switch (ntohl(rx_message->data.ntp_source.type)) {
|
||||||
rem_addr.port = (unsigned short)(ntohl(rx_message->data.ntp_source.port));
|
case REQ_ADDSRC_SERVER:
|
||||||
|
type = NTP_SERVER;
|
||||||
|
pool = 0;
|
||||||
|
break;
|
||||||
|
case REQ_ADDSRC_PEER:
|
||||||
|
type = NTP_PEER;
|
||||||
|
pool = 0;
|
||||||
|
break;
|
||||||
|
case REQ_ADDSRC_POOL:
|
||||||
|
type = NTP_SERVER;
|
||||||
|
pool = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
tx_message->status = htons(STT_INVALID);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
name = (char *)rx_message->data.ntp_source.name;
|
||||||
|
|
||||||
|
/* Make sure the name is terminated */
|
||||||
|
if (name[sizeof (rx_message->data.ntp_source.name) - 1] != '\0') {
|
||||||
|
tx_message->status = htons(STT_INVALIDNAME);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
port = (unsigned short)(ntohl(rx_message->data.ntp_source.port));
|
||||||
params.minpoll = ntohl(rx_message->data.ntp_source.minpoll);
|
params.minpoll = ntohl(rx_message->data.ntp_source.minpoll);
|
||||||
params.maxpoll = ntohl(rx_message->data.ntp_source.maxpoll);
|
params.maxpoll = ntohl(rx_message->data.ntp_source.maxpoll);
|
||||||
params.presend_minpoll = ntohl(rx_message->data.ntp_source.presend_minpoll);
|
params.presend_minpoll = ntohl(rx_message->data.ntp_source.presend_minpoll);
|
||||||
@@ -786,37 +722,49 @@ handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_m
|
|||||||
params.max_sources = ntohl(rx_message->data.ntp_source.max_sources);
|
params.max_sources = ntohl(rx_message->data.ntp_source.max_sources);
|
||||||
params.min_samples = ntohl(rx_message->data.ntp_source.min_samples);
|
params.min_samples = ntohl(rx_message->data.ntp_source.min_samples);
|
||||||
params.max_samples = ntohl(rx_message->data.ntp_source.max_samples);
|
params.max_samples = ntohl(rx_message->data.ntp_source.max_samples);
|
||||||
|
params.filter_length = ntohl(rx_message->data.ntp_source.filter_length);
|
||||||
params.authkey = ntohl(rx_message->data.ntp_source.authkey);
|
params.authkey = ntohl(rx_message->data.ntp_source.authkey);
|
||||||
|
params.nts_port = ntohl(rx_message->data.ntp_source.nts_port);
|
||||||
params.max_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay);
|
params.max_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay);
|
||||||
params.max_delay_ratio =
|
params.max_delay_ratio =
|
||||||
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_ratio);
|
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_ratio);
|
||||||
params.max_delay_dev_ratio =
|
params.max_delay_dev_ratio =
|
||||||
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_dev_ratio);
|
UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_dev_ratio);
|
||||||
|
params.min_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.min_delay);
|
||||||
|
params.asymmetry = UTI_FloatNetworkToHost(rx_message->data.ntp_source.asymmetry);
|
||||||
params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset);
|
params.offset = UTI_FloatNetworkToHost(rx_message->data.ntp_source.offset);
|
||||||
|
|
||||||
params.online = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ? 1 : 0;
|
params.connectivity = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ?
|
||||||
|
SRC_ONLINE : SRC_OFFLINE;
|
||||||
params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0;
|
params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0;
|
||||||
params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0;
|
params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0;
|
||||||
params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0;
|
params.interleaved = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_INTERLEAVED ? 1 : 0;
|
||||||
|
params.burst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_BURST ? 1 : 0;
|
||||||
|
params.nts = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NTS ? 1 : 0;
|
||||||
params.sel_options =
|
params.sel_options =
|
||||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_PREFER ? SRC_SELECT_PREFER : 0) |
|
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_PREFER ? SRC_SELECT_PREFER : 0) |
|
||||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NOSELECT ? SRC_SELECT_NOSELECT : 0) |
|
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NOSELECT ? SRC_SELECT_NOSELECT : 0) |
|
||||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_TRUST ? SRC_SELECT_TRUST : 0) |
|
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_TRUST ? SRC_SELECT_TRUST : 0) |
|
||||||
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_REQUIRE ? SRC_SELECT_REQUIRE : 0);
|
(ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_REQUIRE ? SRC_SELECT_REQUIRE : 0);
|
||||||
|
|
||||||
status = NSR_AddSource(&rem_addr, type, ¶ms);
|
status = NSR_AddSourceByName(name, port, pool, type, ¶ms);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case NSR_Success:
|
case NSR_Success:
|
||||||
break;
|
break;
|
||||||
|
case NSR_UnresolvedName:
|
||||||
|
/* Try to resolve the name now */
|
||||||
|
NSR_ResolveSources();
|
||||||
|
break;
|
||||||
case NSR_AlreadyInUse:
|
case NSR_AlreadyInUse:
|
||||||
tx_message->status = htons(STT_SOURCEALREADYKNOWN);
|
tx_message->status = htons(STT_SOURCEALREADYKNOWN);
|
||||||
break;
|
break;
|
||||||
case NSR_TooManySources:
|
case NSR_TooManySources:
|
||||||
tx_message->status = htons(STT_TOOMANYSOURCES);
|
tx_message->status = htons(STT_TOOMANYSOURCES);
|
||||||
break;
|
break;
|
||||||
case NSR_InvalidAF:
|
case NSR_InvalidName:
|
||||||
tx_message->status = htons(STT_INVALIDAF);
|
tx_message->status = htons(STT_INVALIDNAME);
|
||||||
break;
|
break;
|
||||||
|
case NSR_InvalidAF:
|
||||||
case NSR_NoSuchSource:
|
case NSR_NoSuchSource:
|
||||||
assert(0);
|
assert(0);
|
||||||
break;
|
break;
|
||||||
@@ -844,6 +792,8 @@ handle_del_source(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
case NSR_TooManySources:
|
case NSR_TooManySources:
|
||||||
case NSR_AlreadyInUse:
|
case NSR_AlreadyInUse:
|
||||||
case NSR_InvalidAF:
|
case NSR_InvalidAF:
|
||||||
|
case NSR_InvalidName:
|
||||||
|
case NSR_UnresolvedName:
|
||||||
assert(0);
|
assert(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -874,7 +824,7 @@ handle_dfreq(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
double dfreq;
|
double dfreq;
|
||||||
dfreq = UTI_FloatNetworkToHost(rx_message->data.dfreq.dfreq);
|
dfreq = UTI_FloatNetworkToHost(rx_message->data.dfreq.dfreq);
|
||||||
LCL_AccumulateDeltaFrequency(dfreq * 1.0e-6);
|
LCL_AccumulateDeltaFrequency(dfreq * 1.0e-6);
|
||||||
LOG(LOGS_INFO, LOGF_CmdMon, "Accumulated delta freq of %.3fppm", dfreq);
|
LOG(LOGS_INFO, "Accumulated delta freq of %.3fppm", dfreq);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -887,7 +837,7 @@ handle_doffset(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
sec = (int32_t)ntohl(rx_message->data.doffset.sec);
|
sec = (int32_t)ntohl(rx_message->data.doffset.sec);
|
||||||
usec = (int32_t)ntohl(rx_message->data.doffset.usec);
|
usec = (int32_t)ntohl(rx_message->data.doffset.usec);
|
||||||
doffset = (double) sec + 1.0e-6 * (double) usec;
|
doffset = (double) sec + 1.0e-6 * (double) usec;
|
||||||
LOG(LOGS_INFO, LOGF_CmdMon, "Accumulated delta offset of %.6f seconds", doffset);
|
LOG(LOGS_INFO, "Accumulated delta offset of %.6f seconds", doffset);
|
||||||
LCL_AccumulateOffset(doffset, 0.0);
|
LCL_AccumulateOffset(doffset, 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1065,9 +1015,6 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
tx_message->reply = htons(RPY_CLIENT_ACCESSES_BY_INDEX2);
|
tx_message->reply = htons(RPY_CLIENT_ACCESSES_BY_INDEX2);
|
||||||
tx_message->data.client_accesses_by_index.n_indices = htonl(n_indices);
|
tx_message->data.client_accesses_by_index.n_indices = htonl(n_indices);
|
||||||
|
|
||||||
memset(tx_message->data.client_accesses_by_index.clients, 0,
|
|
||||||
sizeof (tx_message->data.client_accesses_by_index.clients));
|
|
||||||
|
|
||||||
for (i = req_first_index, j = 0; i < (uint32_t)n_indices && j < req_n_clients; i++) {
|
for (i = req_first_index, j = 0; i < (uint32_t)n_indices && j < req_n_clients; i++) {
|
||||||
if (!CLG_GetClientAccessReportByIndex(i, &report, &now))
|
if (!CLG_GetClientAccessReportByIndex(i, &report, &now))
|
||||||
continue;
|
continue;
|
||||||
@@ -1100,10 +1047,11 @@ handle_manual_list(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
RPY_ManualListSample *sample;
|
RPY_ManualListSample *sample;
|
||||||
RPT_ManualSamplesReport report[MAX_MANUAL_LIST_SAMPLES];
|
RPT_ManualSamplesReport report[MAX_MANUAL_LIST_SAMPLES];
|
||||||
|
|
||||||
tx_message->reply = htons(RPY_MANUAL_LIST);
|
tx_message->reply = htons(RPY_MANUAL_LIST2);
|
||||||
|
|
||||||
MNL_ReportSamples(report, MAX_MANUAL_LIST_SAMPLES, &n_samples);
|
MNL_ReportSamples(report, MAX_MANUAL_LIST_SAMPLES, &n_samples);
|
||||||
tx_message->data.manual_list.n_samples = htonl(n_samples);
|
tx_message->data.manual_list.n_samples = htonl(n_samples);
|
||||||
|
|
||||||
for (i=0; i<n_samples; i++) {
|
for (i=0; i<n_samples; i++) {
|
||||||
sample = &tx_message->data.manual_list.samples[i];
|
sample = &tx_message->data.manual_list.samples[i];
|
||||||
UTI_TimespecHostToNetwork(&report[i].when, &sample->when);
|
UTI_TimespecHostToNetwork(&report[i].when, &sample->when);
|
||||||
@@ -1235,151 +1183,177 @@ handle_ntp_data(CMD_Request *rx_message, CMD_Reply *tx_message)
|
|||||||
memset(tx_message->data.ntp_data.reserved, 0xff, sizeof (tx_message->data.ntp_data.reserved));
|
memset(tx_message->data.ntp_data.reserved, 0xff, sizeof (tx_message->data.ntp_data.reserved));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_shutdown(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
|
{
|
||||||
|
LOG(LOGS_INFO, "Received shutdown command");
|
||||||
|
SCH_QuitProgram();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_ntp_source_name(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
|
{
|
||||||
|
IPAddr addr;
|
||||||
|
char *name;
|
||||||
|
|
||||||
|
UTI_IPNetworkToHost(&rx_message->data.ntp_data.ip_addr, &addr);
|
||||||
|
name = NSR_GetName(&addr);
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
tx_message->status = htons(STT_NOSUCHSOURCE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tx_message->reply = htons(RPY_NTP_SOURCE_NAME);
|
||||||
|
|
||||||
|
/* Avoid compiler warning */
|
||||||
|
if (strlen(name) >= sizeof (tx_message->data.ntp_source_name.name))
|
||||||
|
memcpy(tx_message->data.ntp_source_name.name, name,
|
||||||
|
sizeof (tx_message->data.ntp_source_name.name));
|
||||||
|
else
|
||||||
|
strncpy((char *)tx_message->data.ntp_source_name.name, name,
|
||||||
|
sizeof (tx_message->data.ntp_source_name.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_reset(CMD_Request *rx_message, CMD_Reply *tx_message)
|
||||||
|
{
|
||||||
|
struct timespec cooked_now, now;
|
||||||
|
|
||||||
|
SRC_ResetSources();
|
||||||
|
SCH_GetLastEventTime(&cooked_now, NULL, &now);
|
||||||
|
LCL_NotifyExternalTimeStep(&now, &cooked_now, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Read a packet and process it */
|
/* Read a packet and process it */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
read_from_cmd_socket(int sock_fd, int event, void *anything)
|
read_from_cmd_socket(int sock_fd, int event, void *anything)
|
||||||
{
|
{
|
||||||
|
SCK_Message sck_message;
|
||||||
CMD_Request rx_message;
|
CMD_Request rx_message;
|
||||||
CMD_Reply tx_message;
|
CMD_Reply tx_message;
|
||||||
int status, read_length, expected_length, rx_message_length;
|
IPAddr loopback_addr, remote_ip;
|
||||||
|
int read_length, expected_length;
|
||||||
int localhost, allowed, log_index;
|
int localhost, allowed, log_index;
|
||||||
union sockaddr_all where_from;
|
unsigned short rx_command;
|
||||||
socklen_t from_length;
|
|
||||||
IPAddr remote_ip;
|
|
||||||
unsigned short remote_port, rx_command;
|
|
||||||
struct timespec now, cooked_now;
|
struct timespec now, cooked_now;
|
||||||
|
|
||||||
rx_message_length = sizeof(rx_message);
|
if (!SCK_ReceiveMessage(sock_fd, &sck_message, 0))
|
||||||
from_length = sizeof(where_from);
|
|
||||||
|
|
||||||
status = recvfrom(sock_fd, (char *)&rx_message, rx_message_length, 0,
|
|
||||||
&where_from.sa, &from_length);
|
|
||||||
|
|
||||||
if (status < 0) {
|
|
||||||
LOG(LOGS_WARN, LOGF_CmdMon, "Error [%s] reading from control socket %d",
|
|
||||||
strerror(errno), sock_fd);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (from_length > sizeof (where_from) ||
|
read_length = sck_message.length;
|
||||||
from_length <= sizeof (where_from.sa.sa_family)) {
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Read command packet without source address");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
read_length = status;
|
|
||||||
|
|
||||||
/* Get current time cheaply */
|
/* Get current time cheaply */
|
||||||
SCH_GetLastEventTime(&cooked_now, NULL, &now);
|
SCH_GetLastEventTime(&cooked_now, NULL, &now);
|
||||||
|
|
||||||
UTI_SockaddrToIPAndPort(&where_from.sa, &remote_ip, &remote_port);
|
/* Check if it's from localhost (127.0.0.1, ::1, or Unix domain),
|
||||||
|
or an authorised address */
|
||||||
|
switch (sck_message.addr_type) {
|
||||||
|
case SCK_ADDR_IP:
|
||||||
|
assert(sock_fd == sock_fd4 || sock_fd == sock_fd6);
|
||||||
|
remote_ip = sck_message.remote_addr.ip.ip_addr;
|
||||||
|
SCK_GetLoopbackIPAddress(remote_ip.family, &loopback_addr);
|
||||||
|
localhost = UTI_CompareIPs(&remote_ip, &loopback_addr, NULL) == 0;
|
||||||
|
|
||||||
/* Check if it's from localhost (127.0.0.1, ::1, or Unix domain) */
|
if (!localhost && !ADF_IsAllowed(access_auth_table, &remote_ip)) {
|
||||||
switch (remote_ip.family) {
|
DEBUG_LOG("Unauthorised host %s",
|
||||||
case IPADDR_INET4:
|
UTI_IPSockAddrToString(&sck_message.remote_addr.ip));
|
||||||
assert(sock_fd == sock_fd4);
|
|
||||||
localhost = remote_ip.addr.in4 == INADDR_LOOPBACK;
|
|
||||||
break;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
case IPADDR_INET6:
|
|
||||||
assert(sock_fd == sock_fd6);
|
|
||||||
localhost = !memcmp(remote_ip.addr.in6, &in6addr_loopback,
|
|
||||||
sizeof (in6addr_loopback));
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case IPADDR_UNSPEC:
|
|
||||||
/* This should be the Unix domain socket */
|
|
||||||
if (where_from.sa.sa_family != AF_UNIX)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(remote_ip.family != IPADDR_UNSPEC);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case SCK_ADDR_UNIX:
|
||||||
assert(sock_fd == sock_fdu);
|
assert(sock_fd == sock_fdu);
|
||||||
|
remote_ip.family = IPADDR_UNSPEC;
|
||||||
localhost = 1;
|
localhost = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(0);
|
DEBUG_LOG("Unexpected address type");
|
||||||
}
|
return;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Received %d bytes from %s fd %d",
|
|
||||||
status, UTI_SockaddrToString(&where_from.sa), sock_fd);
|
|
||||||
|
|
||||||
if (!(localhost || ADF_IsAllowed(access_auth_table, &remote_ip))) {
|
|
||||||
/* The client is not allowed access, so don't waste any more time
|
|
||||||
on him. Note that localhost is always allowed access
|
|
||||||
regardless of the defined access rules - otherwise, we could
|
|
||||||
shut ourselves out completely! */
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read_length < offsetof(CMD_Request, data) ||
|
if (read_length < offsetof(CMD_Request, data) ||
|
||||||
read_length < offsetof(CMD_Reply, data) ||
|
read_length < offsetof(CMD_Reply, data) ||
|
||||||
rx_message.pkt_type != PKT_TYPE_CMD_REQUEST ||
|
read_length > sizeof (CMD_Request)) {
|
||||||
rx_message.res1 != 0 ||
|
|
||||||
rx_message.res2 != 0) {
|
|
||||||
|
|
||||||
/* We don't know how to process anything like this or an error reply
|
/* We don't know how to process anything like this or an error reply
|
||||||
would be larger than the request */
|
would be larger than the request */
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Command packet dropped");
|
DEBUG_LOG("Unexpected length");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
expected_length = PKL_CommandLength(&rx_message);
|
memcpy(&rx_message, sck_message.data, read_length);
|
||||||
rx_command = ntohs(rx_message.command);
|
|
||||||
|
|
||||||
tx_message.version = PROTO_VERSION_NUMBER;
|
if (rx_message.pkt_type != PKT_TYPE_CMD_REQUEST ||
|
||||||
tx_message.pkt_type = PKT_TYPE_CMD_REPLY;
|
rx_message.res1 != 0 ||
|
||||||
tx_message.res1 = 0;
|
rx_message.res2 != 0) {
|
||||||
tx_message.res2 = 0;
|
DEBUG_LOG("Command packet dropped");
|
||||||
tx_message.command = rx_message.command;
|
|
||||||
tx_message.reply = htons(RPY_NULL);
|
|
||||||
tx_message.status = htons(STT_SUCCESS);
|
|
||||||
tx_message.pad1 = 0;
|
|
||||||
tx_message.pad2 = 0;
|
|
||||||
tx_message.pad3 = 0;
|
|
||||||
tx_message.sequence = rx_message.sequence;
|
|
||||||
tx_message.pad4 = 0;
|
|
||||||
tx_message.pad5 = 0;
|
|
||||||
|
|
||||||
if (rx_message.version != PROTO_VERSION_NUMBER) {
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Command packet has invalid version (%d != %d)",
|
|
||||||
rx_message.version, PROTO_VERSION_NUMBER);
|
|
||||||
|
|
||||||
if (rx_message.version >= PROTO_VERSION_MISMATCH_COMPAT_SERVER) {
|
|
||||||
tx_message.status = htons(STT_BADPKTVERSION);
|
|
||||||
transmit_reply(&tx_message, &where_from);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rx_command >= N_REQUEST_TYPES ||
|
|
||||||
expected_length < (int)offsetof(CMD_Request, data)) {
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Command packet has invalid command %d", rx_command);
|
|
||||||
|
|
||||||
tx_message.status = htons(STT_INVALID);
|
|
||||||
transmit_reply(&tx_message, &where_from);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (read_length < expected_length) {
|
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Command packet is too short (%d < %d)", read_length,
|
|
||||||
expected_length);
|
|
||||||
|
|
||||||
tx_message.status = htons(STT_BADPKTLENGTH);
|
|
||||||
transmit_reply(&tx_message, &where_from);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* OK, we have a valid message. Now dispatch on message type and process it. */
|
|
||||||
|
|
||||||
log_index = CLG_LogCommandAccess(&remote_ip, &cooked_now);
|
log_index = CLG_LogCommandAccess(&remote_ip, &cooked_now);
|
||||||
|
|
||||||
/* Don't reply to all requests from hosts other than localhost if the rate
|
/* Don't reply to all requests from hosts other than localhost if the rate
|
||||||
is excessive */
|
is excessive */
|
||||||
if (!localhost && log_index >= 0 && CLG_LimitCommandResponseRate(log_index)) {
|
if (!localhost && log_index >= 0 && CLG_LimitCommandResponseRate(log_index)) {
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Command packet discarded to limit response rate");
|
DEBUG_LOG("Command packet discarded to limit response rate");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expected_length = PKL_CommandLength(&rx_message);
|
||||||
|
rx_command = ntohs(rx_message.command);
|
||||||
|
|
||||||
|
memset(&tx_message, 0, sizeof (tx_message));
|
||||||
|
sck_message.data = &tx_message;
|
||||||
|
sck_message.length = 0;
|
||||||
|
|
||||||
|
tx_message.version = PROTO_VERSION_NUMBER;
|
||||||
|
tx_message.pkt_type = PKT_TYPE_CMD_REPLY;
|
||||||
|
tx_message.command = rx_message.command;
|
||||||
|
tx_message.reply = htons(RPY_NULL);
|
||||||
|
tx_message.status = htons(STT_SUCCESS);
|
||||||
|
tx_message.sequence = rx_message.sequence;
|
||||||
|
|
||||||
|
if (rx_message.version != PROTO_VERSION_NUMBER) {
|
||||||
|
DEBUG_LOG("Command packet has invalid version (%d != %d)",
|
||||||
|
rx_message.version, PROTO_VERSION_NUMBER);
|
||||||
|
|
||||||
|
if (rx_message.version >= PROTO_VERSION_MISMATCH_COMPAT_SERVER) {
|
||||||
|
tx_message.status = htons(STT_BADPKTVERSION);
|
||||||
|
transmit_reply(sock_fd, &sck_message);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rx_command >= N_REQUEST_TYPES ||
|
||||||
|
expected_length < (int)offsetof(CMD_Request, data)) {
|
||||||
|
DEBUG_LOG("Command packet has invalid command %d", rx_command);
|
||||||
|
|
||||||
|
tx_message.status = htons(STT_INVALID);
|
||||||
|
transmit_reply(sock_fd, &sck_message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (read_length < expected_length) {
|
||||||
|
DEBUG_LOG("Command packet is too short (%d < %d)", read_length,
|
||||||
|
expected_length);
|
||||||
|
|
||||||
|
tx_message.status = htons(STT_BADPKTLENGTH);
|
||||||
|
transmit_reply(sock_fd, &sck_message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* OK, we have a valid message. Now dispatch on message type and process it. */
|
||||||
|
|
||||||
if (rx_command >= N_REQUEST_TYPES) {
|
if (rx_command >= N_REQUEST_TYPES) {
|
||||||
/* This should be already handled */
|
/* This should be already handled */
|
||||||
assert(0);
|
assert(0);
|
||||||
@@ -1387,7 +1361,7 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
|||||||
/* Check level of authority required to issue the command. All commands
|
/* Check level of authority required to issue the command. All commands
|
||||||
from the Unix domain socket (which is accessible only by the root and
|
from the Unix domain socket (which is accessible only by the root and
|
||||||
chrony user/group) are allowed. */
|
chrony user/group) are allowed. */
|
||||||
if (where_from.sa.sa_family == AF_UNIX) {
|
if (remote_ip.family == IPADDR_UNSPEC) {
|
||||||
assert(sock_fd == sock_fdu);
|
assert(sock_fd == sock_fdu);
|
||||||
allowed = 1;
|
allowed = 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -1526,12 +1500,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
|||||||
handle_cmdaccheck(&rx_message, &tx_message);
|
handle_cmdaccheck(&rx_message, &tx_message);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case REQ_ADD_SERVER2:
|
case REQ_ADD_SOURCE:
|
||||||
handle_add_source(NTP_SERVER, &rx_message, &tx_message);
|
handle_add_source(&rx_message, &tx_message);
|
||||||
break;
|
|
||||||
|
|
||||||
case REQ_ADD_PEER2:
|
|
||||||
handle_add_source(NTP_PEER, &rx_message, &tx_message);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case REQ_DEL_SOURCE:
|
case REQ_DEL_SOURCE:
|
||||||
@@ -1626,8 +1596,24 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
|||||||
handle_ntp_data(&rx_message, &tx_message);
|
handle_ntp_data(&rx_message, &tx_message);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case REQ_SHUTDOWN:
|
||||||
|
handle_shutdown(&rx_message, &tx_message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REQ_ONOFFLINE:
|
||||||
|
handle_onoffline(&rx_message, &tx_message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REQ_NTP_SOURCE_NAME:
|
||||||
|
handle_ntp_source_name(&rx_message, &tx_message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REQ_RESET:
|
||||||
|
handle_reset(&rx_message, &tx_message);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
DEBUG_LOG(LOGF_CmdMon, "Unhandled command %d", rx_command);
|
DEBUG_LOG("Unhandled command %d", rx_command);
|
||||||
tx_message.status = htons(STT_FAILED);
|
tx_message.status = htons(STT_FAILED);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1643,7 +1629,7 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
|||||||
static int do_it=1;
|
static int do_it=1;
|
||||||
|
|
||||||
if (do_it) {
|
if (do_it) {
|
||||||
transmit_reply(&tx_message, &where_from);
|
transmit_reply(sock_fd, &sck_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|||||||
32
cmdparse.c
32
cmdparse.c
@@ -48,9 +48,10 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
src->port = SRC_DEFAULT_PORT;
|
src->port = SRC_DEFAULT_PORT;
|
||||||
src->params.minpoll = SRC_DEFAULT_MINPOLL;
|
src->params.minpoll = SRC_DEFAULT_MINPOLL;
|
||||||
src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
|
src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
|
||||||
src->params.online = 1;
|
src->params.connectivity = SRC_ONLINE;
|
||||||
src->params.auto_offline = 0;
|
src->params.auto_offline = 0;
|
||||||
src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
|
src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
|
||||||
|
src->params.burst = 0;
|
||||||
src->params.iburst = 0;
|
src->params.iburst = 0;
|
||||||
src->params.min_stratum = SRC_DEFAULT_MINSTRATUM;
|
src->params.min_stratum = SRC_DEFAULT_MINSTRATUM;
|
||||||
src->params.poll_target = SRC_DEFAULT_POLLTARGET;
|
src->params.poll_target = SRC_DEFAULT_POLLTARGET;
|
||||||
@@ -58,12 +59,17 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
src->params.max_sources = SRC_DEFAULT_MAXSOURCES;
|
src->params.max_sources = SRC_DEFAULT_MAXSOURCES;
|
||||||
src->params.min_samples = SRC_DEFAULT_MINSAMPLES;
|
src->params.min_samples = SRC_DEFAULT_MINSAMPLES;
|
||||||
src->params.max_samples = SRC_DEFAULT_MAXSAMPLES;
|
src->params.max_samples = SRC_DEFAULT_MAXSAMPLES;
|
||||||
|
src->params.filter_length = 0;
|
||||||
src->params.interleaved = 0;
|
src->params.interleaved = 0;
|
||||||
src->params.sel_options = 0;
|
src->params.sel_options = 0;
|
||||||
|
src->params.nts = 0;
|
||||||
|
src->params.nts_port = SRC_DEFAULT_NTSPORT;
|
||||||
src->params.authkey = INACTIVE_AUTHKEY;
|
src->params.authkey = INACTIVE_AUTHKEY;
|
||||||
src->params.max_delay = SRC_DEFAULT_MAXDELAY;
|
src->params.max_delay = SRC_DEFAULT_MAXDELAY;
|
||||||
src->params.max_delay_ratio = SRC_DEFAULT_MAXDELAYRATIO;
|
src->params.max_delay_ratio = SRC_DEFAULT_MAXDELAYRATIO;
|
||||||
src->params.max_delay_dev_ratio = SRC_DEFAULT_MAXDELAYDEVRATIO;
|
src->params.max_delay_dev_ratio = SRC_DEFAULT_MAXDELAYDEVRATIO;
|
||||||
|
src->params.min_delay = 0.0;
|
||||||
|
src->params.asymmetry = SRC_DEFAULT_ASYMMETRY;
|
||||||
src->params.offset = 0.0;
|
src->params.offset = 0.0;
|
||||||
|
|
||||||
hostname = line;
|
hostname = line;
|
||||||
@@ -82,10 +88,12 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
|
|
||||||
if (!strcasecmp(cmd, "auto_offline")) {
|
if (!strcasecmp(cmd, "auto_offline")) {
|
||||||
src->params.auto_offline = 1;
|
src->params.auto_offline = 1;
|
||||||
|
} else if (!strcasecmp(cmd, "burst")) {
|
||||||
|
src->params.burst = 1;
|
||||||
} else if (!strcasecmp(cmd, "iburst")) {
|
} else if (!strcasecmp(cmd, "iburst")) {
|
||||||
src->params.iburst = 1;
|
src->params.iburst = 1;
|
||||||
} else if (!strcasecmp(cmd, "offline")) {
|
} else if (!strcasecmp(cmd, "offline")) {
|
||||||
src->params.online = 0;
|
src->params.connectivity = SRC_OFFLINE;
|
||||||
} else if (!strcasecmp(cmd, "noselect")) {
|
} else if (!strcasecmp(cmd, "noselect")) {
|
||||||
src->params.sel_options |= SRC_SELECT_NOSELECT;
|
src->params.sel_options |= SRC_SELECT_NOSELECT;
|
||||||
} else if (!strcasecmp(cmd, "prefer")) {
|
} else if (!strcasecmp(cmd, "prefer")) {
|
||||||
@@ -98,6 +106,12 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
|
if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
|
||||||
src->params.authkey == INACTIVE_AUTHKEY)
|
src->params.authkey == INACTIVE_AUTHKEY)
|
||||||
return 0;
|
return 0;
|
||||||
|
} 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")) {
|
} else if (!strcasecmp(cmd, "maxdelay")) {
|
||||||
if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
|
if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -116,6 +130,9 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
} else if (!strcasecmp(cmd, "maxsources")) {
|
} else if (!strcasecmp(cmd, "maxsources")) {
|
||||||
if (sscanf(line, "%d%n", &src->params.max_sources, &n) != 1)
|
if (sscanf(line, "%d%n", &src->params.max_sources, &n) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
} else if (!strcasecmp(cmd, "mindelay")) {
|
||||||
|
if (sscanf(line, "%lf%n", &src->params.min_delay, &n) != 1)
|
||||||
|
return 0;
|
||||||
} else if (!strcasecmp(cmd, "minpoll")) {
|
} else if (!strcasecmp(cmd, "minpoll")) {
|
||||||
if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1)
|
if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -125,6 +142,11 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
|
|||||||
} else if (!strcasecmp(cmd, "minstratum")) {
|
} else if (!strcasecmp(cmd, "minstratum")) {
|
||||||
if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
|
if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
|
||||||
return 0;
|
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")) {
|
} else if (!strcasecmp(cmd, "offset")) {
|
||||||
if (sscanf(line, "%lf%n", &src->params.offset, &n) != 1)
|
if (sscanf(line, "%lf%n", &src->params.offset, &n) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -246,7 +268,7 @@ CPS_SplitWord(char *line)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
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;
|
char *s1, *s2, *s3, *s4;
|
||||||
|
|
||||||
@@ -263,10 +285,10 @@ CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (*s3) {
|
if (*s3) {
|
||||||
*hash = s2;
|
*type = s2;
|
||||||
*key = s3;
|
*key = s3;
|
||||||
} else {
|
} else {
|
||||||
*hash = "MD5";
|
*type = "MD5";
|
||||||
*key = s2;
|
*key = s2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,6 @@ extern void CPS_NormalizeLine(char *line);
|
|||||||
extern char *CPS_SplitWord(char *line);
|
extern char *CPS_SplitWord(char *line);
|
||||||
|
|
||||||
/* Parse a key from keyfile */
|
/* 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 */
|
#endif /* GOT_CMDPARSE_H */
|
||||||
|
|||||||
279
conf.c
279
conf.c
@@ -79,7 +79,7 @@ static void parse_tempcomp(char *);
|
|||||||
static int restarted = 0;
|
static int restarted = 0;
|
||||||
static char *rtc_device;
|
static char *rtc_device;
|
||||||
static int acquisition_port = -1;
|
static int acquisition_port = -1;
|
||||||
static int ntp_port = 123;
|
static int ntp_port = NTP_PORT;
|
||||||
static char *keys_file = NULL;
|
static char *keys_file = NULL;
|
||||||
static char *drift_file = NULL;
|
static char *drift_file = NULL;
|
||||||
static char *rtc_file = NULL;
|
static char *rtc_file = NULL;
|
||||||
@@ -97,13 +97,13 @@ static double combine_limit = 3.0;
|
|||||||
|
|
||||||
static int cmd_port = DEFAULT_CANDM_PORT;
|
static int cmd_port = DEFAULT_CANDM_PORT;
|
||||||
|
|
||||||
|
static int raw_measurements = 0;
|
||||||
static int do_log_measurements = 0;
|
static int do_log_measurements = 0;
|
||||||
static int do_log_statistics = 0;
|
static int do_log_statistics = 0;
|
||||||
static int do_log_tracking = 0;
|
static int do_log_tracking = 0;
|
||||||
static int do_log_rtc = 0;
|
static int do_log_rtc = 0;
|
||||||
static int do_log_refclocks = 0;
|
static int do_log_refclocks = 0;
|
||||||
static int do_log_tempcomp = 0;
|
static int do_log_tempcomp = 0;
|
||||||
static int do_dump_on_exit = 0;
|
|
||||||
static int log_banner = 32;
|
static int log_banner = 32;
|
||||||
static char *logdir;
|
static char *logdir;
|
||||||
static char *dumpdir;
|
static char *dumpdir;
|
||||||
@@ -223,13 +223,21 @@ static char *leapsec_tz = NULL;
|
|||||||
/* Name of the user to which will be dropped root privileges. */
|
/* Name of the user to which will be dropped root privileges. */
|
||||||
static char *user;
|
static char *user;
|
||||||
|
|
||||||
typedef struct {
|
/* NTS cache dir, certificates, private key, and port */
|
||||||
char *name;
|
static char *nts_cachedir = NULL;
|
||||||
double tx_comp;
|
static char *nts_server_cert_file = NULL;
|
||||||
double rx_comp;
|
static char *nts_server_key_file = NULL;
|
||||||
} HwTs_Interface;
|
static int nts_server_port = 11443;
|
||||||
|
static int nts_server_processes = 1;
|
||||||
|
static int nts_server_connections = 100;
|
||||||
|
static int nts_refresh = 2419200; /* 4 weeks */
|
||||||
|
static int nts_rotate = 604800; /* 1 week */
|
||||||
|
static char *nts_trusted_cert_file = NULL;
|
||||||
|
|
||||||
/* Array of HwTs_Interface */
|
/* Flag disabling use of system trusted certificates */
|
||||||
|
static int no_system_cert = 0;
|
||||||
|
|
||||||
|
/* Array of CNF_HwTsInterface */
|
||||||
static ARR_Instance hwts_interfaces;
|
static ARR_Instance hwts_interfaces;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -276,7 +284,7 @@ static const char *processed_command;
|
|||||||
static void
|
static void
|
||||||
command_parse_error(void)
|
command_parse_error(void)
|
||||||
{
|
{
|
||||||
LOG_FATAL(LOGF_Configure, "Could not parse %s directive at line %d%s%s",
|
LOG_FATAL("Could not parse %s directive at line %d%s%s",
|
||||||
processed_command, line_number, processed_file ? " in file " : "",
|
processed_command, line_number, processed_file ? " in file " : "",
|
||||||
processed_file ? processed_file : "");
|
processed_file ? processed_file : "");
|
||||||
}
|
}
|
||||||
@@ -286,7 +294,7 @@ command_parse_error(void)
|
|||||||
static void
|
static void
|
||||||
other_parse_error(const char *message)
|
other_parse_error(const char *message)
|
||||||
{
|
{
|
||||||
LOG_FATAL(LOGF_Configure, "%s at line %d%s%s",
|
LOG_FATAL("%s at line %d%s%s",
|
||||||
message, line_number, processed_file ? " in file " : "",
|
message, line_number, processed_file ? " in file " : "",
|
||||||
processed_file ? processed_file : "");
|
processed_file ? processed_file : "");
|
||||||
}
|
}
|
||||||
@@ -319,7 +327,7 @@ check_number_of_args(char *line, int num)
|
|||||||
num -= get_number_of_args(line);
|
num -= get_number_of_args(line);
|
||||||
|
|
||||||
if (num) {
|
if (num) {
|
||||||
LOG_FATAL(LOGF_Configure, "%s arguments for %s directive at line %d%s%s",
|
LOG_FATAL("%s arguments for %s directive at line %d%s%s",
|
||||||
num > 0 ? "Missing" : "Too many",
|
num > 0 ? "Missing" : "Too many",
|
||||||
processed_command, line_number, processed_file ? " in file " : "",
|
processed_command, line_number, processed_file ? " in file " : "",
|
||||||
processed_file ? processed_file : "");
|
processed_file ? processed_file : "");
|
||||||
@@ -329,11 +337,11 @@ check_number_of_args(char *line, int num)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
CNF_Initialise(int r)
|
CNF_Initialise(int r, int client_only)
|
||||||
{
|
{
|
||||||
restarted = r;
|
restarted = r;
|
||||||
|
|
||||||
hwts_interfaces = ARR_CreateInstance(sizeof (HwTs_Interface));
|
hwts_interfaces = ARR_CreateInstance(sizeof (CNF_HwTsInterface));
|
||||||
|
|
||||||
init_sources = ARR_CreateInstance(sizeof (IPAddr));
|
init_sources = ARR_CreateInstance(sizeof (IPAddr));
|
||||||
ntp_sources = ARR_CreateInstance(sizeof (NTP_Source));
|
ntp_sources = ARR_CreateInstance(sizeof (NTP_Source));
|
||||||
@@ -345,11 +353,18 @@ CNF_Initialise(int r)
|
|||||||
|
|
||||||
dumpdir = Strdup("");
|
dumpdir = Strdup("");
|
||||||
logdir = Strdup("");
|
logdir = Strdup("");
|
||||||
bind_cmd_path = Strdup(DEFAULT_COMMAND_SOCKET);
|
|
||||||
pidfile = Strdup(DEFAULT_PID_FILE);
|
|
||||||
rtc_device = Strdup(DEFAULT_RTC_DEVICE);
|
rtc_device = Strdup(DEFAULT_RTC_DEVICE);
|
||||||
hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
|
hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
|
||||||
user = Strdup(DEFAULT_USER);
|
user = Strdup(DEFAULT_USER);
|
||||||
|
|
||||||
|
if (client_only) {
|
||||||
|
cmd_port = ntp_port = 0;
|
||||||
|
bind_cmd_path = Strdup("");
|
||||||
|
pidfile = Strdup("");
|
||||||
|
} else {
|
||||||
|
bind_cmd_path = Strdup(DEFAULT_COMMAND_SOCKET);
|
||||||
|
pidfile = Strdup(DEFAULT_PID_FILE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -360,7 +375,7 @@ CNF_Finalise(void)
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(hwts_interfaces); i++)
|
for (i = 0; i < ARR_GetSize(hwts_interfaces); i++)
|
||||||
Free(((HwTs_Interface *)ARR_GetElement(hwts_interfaces, i))->name);
|
Free(((CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, i))->name);
|
||||||
ARR_DestroyInstance(hwts_interfaces);
|
ARR_DestroyInstance(hwts_interfaces);
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(ntp_sources); i++)
|
for (i = 0; i < ARR_GetSize(ntp_sources); i++)
|
||||||
@@ -389,6 +404,10 @@ CNF_Finalise(void)
|
|||||||
Free(mail_user_on_change);
|
Free(mail_user_on_change);
|
||||||
Free(tempcomp_sensor_file);
|
Free(tempcomp_sensor_file);
|
||||||
Free(tempcomp_point_file);
|
Free(tempcomp_point_file);
|
||||||
|
Free(nts_cachedir);
|
||||||
|
Free(nts_server_cert_file);
|
||||||
|
Free(nts_server_key_file);
|
||||||
|
Free(nts_trusted_cert_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -401,14 +420,7 @@ CNF_ReadFile(const char *filename)
|
|||||||
char line[2048];
|
char line[2048];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
in = fopen(filename, "r");
|
in = UTI_OpenFile(NULL, filename, NULL, 'R', 0);
|
||||||
if (!in) {
|
|
||||||
LOG_FATAL(LOGF_Configure, "Could not open configuration file %s : %s",
|
|
||||||
filename, strerror(errno));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Configure, "Reading %s", filename);
|
|
||||||
|
|
||||||
for (i = 1; fgets(line, sizeof(line), in); i++) {
|
for (i = 1; fgets(line, sizeof(line), in); i++) {
|
||||||
CNF_ParseLine(filename, i, line);
|
CNF_ParseLine(filename, i, line);
|
||||||
@@ -474,7 +486,7 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
|||||||
} else if (!strcasecmp(command, "dumpdir")) {
|
} else if (!strcasecmp(command, "dumpdir")) {
|
||||||
parse_string(p, &dumpdir);
|
parse_string(p, &dumpdir);
|
||||||
} else if (!strcasecmp(command, "dumponexit")) {
|
} else if (!strcasecmp(command, "dumponexit")) {
|
||||||
do_dump_on_exit = parse_null(p);
|
/* Silently ignored */
|
||||||
} else if (!strcasecmp(command, "fallbackdrift")) {
|
} else if (!strcasecmp(command, "fallbackdrift")) {
|
||||||
parse_fallbackdrift(p);
|
parse_fallbackdrift(p);
|
||||||
} else if (!strcasecmp(command, "hwclockfile")) {
|
} else if (!strcasecmp(command, "hwclockfile")) {
|
||||||
@@ -519,6 +531,8 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
|||||||
parse_double(p, &max_drift);
|
parse_double(p, &max_drift);
|
||||||
} else if (!strcasecmp(command, "maxjitter")) {
|
} else if (!strcasecmp(command, "maxjitter")) {
|
||||||
parse_double(p, &max_jitter);
|
parse_double(p, &max_jitter);
|
||||||
|
} else if (!strcasecmp(command, "maxntsconnections")) {
|
||||||
|
parse_int(p, &nts_server_connections);
|
||||||
} else if (!strcasecmp(command, "maxsamples")) {
|
} else if (!strcasecmp(command, "maxsamples")) {
|
||||||
parse_int(p, &max_samples);
|
parse_int(p, &max_samples);
|
||||||
} else if (!strcasecmp(command, "maxslewrate")) {
|
} else if (!strcasecmp(command, "maxslewrate")) {
|
||||||
@@ -531,8 +545,26 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
|||||||
parse_int(p, &min_sources);
|
parse_int(p, &min_sources);
|
||||||
} else if (!strcasecmp(command, "noclientlog")) {
|
} else if (!strcasecmp(command, "noclientlog")) {
|
||||||
no_client_log = parse_null(p);
|
no_client_log = parse_null(p);
|
||||||
|
} else if (!strcasecmp(command, "nosystemcert")) {
|
||||||
|
no_system_cert = parse_null(p);
|
||||||
} else if (!strcasecmp(command, "ntpsigndsocket")) {
|
} else if (!strcasecmp(command, "ntpsigndsocket")) {
|
||||||
parse_string(p, &ntp_signd_socket);
|
parse_string(p, &ntp_signd_socket);
|
||||||
|
} else if (!strcasecmp(command, "ntstrustedcerts")) {
|
||||||
|
parse_string(p, &nts_trusted_cert_file);
|
||||||
|
} else if (!strcasecmp(command, "ntscachedir")) {
|
||||||
|
parse_string(p, &nts_cachedir);
|
||||||
|
} else if (!strcasecmp(command, "ntsport")) {
|
||||||
|
parse_int(p, &nts_server_port);
|
||||||
|
} else if (!strcasecmp(command, "ntsprocesses")) {
|
||||||
|
parse_int(p, &nts_server_processes);
|
||||||
|
} else if (!strcasecmp(command, "ntsrefresh")) {
|
||||||
|
parse_int(p, &nts_refresh);
|
||||||
|
} else if (!strcasecmp(command, "ntsrotate")) {
|
||||||
|
parse_int(p, &nts_rotate);
|
||||||
|
} else if (!strcasecmp(command, "ntsservercert")) {
|
||||||
|
parse_string(p, &nts_server_cert_file);
|
||||||
|
} else if (!strcasecmp(command, "ntsserverkey")) {
|
||||||
|
parse_string(p, &nts_server_key_file);
|
||||||
} else if (!strcasecmp(command, "peer")) {
|
} else if (!strcasecmp(command, "peer")) {
|
||||||
parse_source(p, NTP_PEER, 0);
|
parse_source(p, NTP_PEER, 0);
|
||||||
} else if (!strcasecmp(command, "pidfile")) {
|
} else if (!strcasecmp(command, "pidfile")) {
|
||||||
@@ -574,7 +606,7 @@ CNF_ParseLine(const char *filename, int number, char *line)
|
|||||||
!strcasecmp(command, "generatecommandkey") ||
|
!strcasecmp(command, "generatecommandkey") ||
|
||||||
!strcasecmp(command, "linux_freq_scale") ||
|
!strcasecmp(command, "linux_freq_scale") ||
|
||||||
!strcasecmp(command, "linux_hz")) {
|
!strcasecmp(command, "linux_hz")) {
|
||||||
LOG(LOGS_WARN, LOGF_Configure, "%s directive is no longer supported", command);
|
LOG(LOGS_WARN, "%s directive is no longer supported", command);
|
||||||
} else {
|
} else {
|
||||||
other_parse_error("Invalid command");
|
other_parse_error("Invalid command");
|
||||||
}
|
}
|
||||||
@@ -680,9 +712,9 @@ static void
|
|||||||
parse_refclock(char *line)
|
parse_refclock(char *line)
|
||||||
{
|
{
|
||||||
int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options;
|
int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options;
|
||||||
int max_lock_age;
|
int max_lock_age, pps_forced, stratum, tai;
|
||||||
uint32_t ref_id, lock_ref_id;
|
uint32_t ref_id, lock_ref_id;
|
||||||
double offset, delay, precision, max_dispersion;
|
double offset, delay, precision, max_dispersion, pulse_width;
|
||||||
char *p, *cmd, *name, *param;
|
char *p, *cmd, *name, *param;
|
||||||
unsigned char ref[5];
|
unsigned char ref[5];
|
||||||
RefclockParameters *refclock;
|
RefclockParameters *refclock;
|
||||||
@@ -690,6 +722,7 @@ parse_refclock(char *line)
|
|||||||
poll = 4;
|
poll = 4;
|
||||||
dpoll = 0;
|
dpoll = 0;
|
||||||
filter_length = 64;
|
filter_length = 64;
|
||||||
|
pps_forced = 0;
|
||||||
pps_rate = 0;
|
pps_rate = 0;
|
||||||
min_samples = SRC_DEFAULT_MINSAMPLES;
|
min_samples = SRC_DEFAULT_MINSAMPLES;
|
||||||
max_samples = SRC_DEFAULT_MAXSAMPLES;
|
max_samples = SRC_DEFAULT_MAXSAMPLES;
|
||||||
@@ -698,9 +731,12 @@ parse_refclock(char *line)
|
|||||||
delay = 1e-9;
|
delay = 1e-9;
|
||||||
precision = 0.0;
|
precision = 0.0;
|
||||||
max_dispersion = 0.0;
|
max_dispersion = 0.0;
|
||||||
|
pulse_width = 0.0;
|
||||||
ref_id = 0;
|
ref_id = 0;
|
||||||
max_lock_age = 2;
|
max_lock_age = 2;
|
||||||
lock_ref_id = 0;
|
lock_ref_id = 0;
|
||||||
|
stratum = 0;
|
||||||
|
tai = 0;
|
||||||
|
|
||||||
if (!*line) {
|
if (!*line) {
|
||||||
command_parse_error();
|
command_parse_error();
|
||||||
@@ -762,12 +798,25 @@ parse_refclock(char *line)
|
|||||||
} else if (!strcasecmp(cmd, "delay")) {
|
} else if (!strcasecmp(cmd, "delay")) {
|
||||||
if (sscanf(line, "%lf%n", &delay, &n) != 1)
|
if (sscanf(line, "%lf%n", &delay, &n) != 1)
|
||||||
break;
|
break;
|
||||||
|
} else if (!strcasecmp(cmd, "pps")) {
|
||||||
|
n = 0;
|
||||||
|
pps_forced = 1;
|
||||||
} else if (!strcasecmp(cmd, "precision")) {
|
} else if (!strcasecmp(cmd, "precision")) {
|
||||||
if (sscanf(line, "%lf%n", &precision, &n) != 1)
|
if (sscanf(line, "%lf%n", &precision, &n) != 1)
|
||||||
break;
|
break;
|
||||||
} else if (!strcasecmp(cmd, "maxdispersion")) {
|
} else if (!strcasecmp(cmd, "maxdispersion")) {
|
||||||
if (sscanf(line, "%lf%n", &max_dispersion, &n) != 1)
|
if (sscanf(line, "%lf%n", &max_dispersion, &n) != 1)
|
||||||
break;
|
break;
|
||||||
|
} else if (!strcasecmp(cmd, "stratum")) {
|
||||||
|
if (sscanf(line, "%d%n", &stratum, &n) != 1 ||
|
||||||
|
stratum >= NTP_MAX_STRATUM || stratum < 0)
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(cmd, "tai")) {
|
||||||
|
n = 0;
|
||||||
|
tai = 1;
|
||||||
|
} else if (!strcasecmp(cmd, "width")) {
|
||||||
|
if (sscanf(line, "%lf%n", &pulse_width, &n) != 1)
|
||||||
|
break;
|
||||||
} else if (!strcasecmp(cmd, "noselect")) {
|
} else if (!strcasecmp(cmd, "noselect")) {
|
||||||
n = 0;
|
n = 0;
|
||||||
sel_options |= SRC_SELECT_NOSELECT;
|
sel_options |= SRC_SELECT_NOSELECT;
|
||||||
@@ -797,14 +846,18 @@ parse_refclock(char *line)
|
|||||||
refclock->driver_poll = dpoll;
|
refclock->driver_poll = dpoll;
|
||||||
refclock->poll = poll;
|
refclock->poll = poll;
|
||||||
refclock->filter_length = filter_length;
|
refclock->filter_length = filter_length;
|
||||||
|
refclock->pps_forced = pps_forced;
|
||||||
refclock->pps_rate = pps_rate;
|
refclock->pps_rate = pps_rate;
|
||||||
refclock->min_samples = min_samples;
|
refclock->min_samples = min_samples;
|
||||||
refclock->max_samples = max_samples;
|
refclock->max_samples = max_samples;
|
||||||
refclock->sel_options = sel_options;
|
refclock->sel_options = sel_options;
|
||||||
|
refclock->stratum = stratum;
|
||||||
|
refclock->tai = tai;
|
||||||
refclock->offset = offset;
|
refclock->offset = offset;
|
||||||
refclock->delay = delay;
|
refclock->delay = delay;
|
||||||
refclock->precision = precision;
|
refclock->precision = precision;
|
||||||
refclock->max_dispersion = max_dispersion;
|
refclock->max_dispersion = max_dispersion;
|
||||||
|
refclock->pulse_width = pulse_width;
|
||||||
refclock->ref_id = ref_id;
|
refclock->ref_id = ref_id;
|
||||||
refclock->max_lock_age = max_lock_age;
|
refclock->max_lock_age = max_lock_age;
|
||||||
refclock->lock_ref_id = lock_ref_id;
|
refclock->lock_ref_id = lock_ref_id;
|
||||||
@@ -820,7 +873,10 @@ parse_log(char *line)
|
|||||||
log_name = line;
|
log_name = line;
|
||||||
line = CPS_SplitWord(line);
|
line = CPS_SplitWord(line);
|
||||||
if (*log_name) {
|
if (*log_name) {
|
||||||
if (!strcmp(log_name, "measurements")) {
|
if (!strcmp(log_name, "rawmeasurements")) {
|
||||||
|
do_log_measurements = 1;
|
||||||
|
raw_measurements = 1;
|
||||||
|
} else if (!strcmp(log_name, "measurements")) {
|
||||||
do_log_measurements = 1;
|
do_log_measurements = 1;
|
||||||
} else if (!strcmp(log_name, "statistics")) {
|
} else if (!strcmp(log_name, "statistics")) {
|
||||||
do_log_statistics = 1;
|
do_log_statistics = 1;
|
||||||
@@ -880,7 +936,7 @@ parse_initstepslew(char *line)
|
|||||||
if (DNS_Name2IPAddress(hostname, &ip_addr, 1) == DNS_Success) {
|
if (DNS_Name2IPAddress(hostname, &ip_addr, 1) == DNS_Success) {
|
||||||
ARR_AppendElement(init_sources, &ip_addr);
|
ARR_AppendElement(init_sources, &ip_addr);
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_WARN, LOGF_Configure, "Could not resolve address of initstepslew server %s", hostname);
|
LOG(LOGS_WARN, "Could not resolve address of initstepslew server %s", hostname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1057,7 +1113,7 @@ parse_allow_deny(char *line, ARR_Instance restrictions, int allow)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (DNS_Name2IPAddress(p, &ip_addr, 1) == DNS_Success) {
|
if (!slashpos && DNS_Name2IPAddress(p, &ip_addr, 1) == DNS_Success) {
|
||||||
new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
|
new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
|
||||||
new_node->allow = allow;
|
new_node->allow = allow;
|
||||||
new_node->all = all;
|
new_node->all = all;
|
||||||
@@ -1172,7 +1228,7 @@ parse_broadcast(char *line)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* default port */
|
/* default port */
|
||||||
port = 123;
|
port = NTP_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
destination = (NTP_Broadcast_Destination *)ARR_GetNewElement(broadcasts);
|
destination = (NTP_Broadcast_Destination *)ARR_GetNewElement(broadcasts);
|
||||||
@@ -1251,8 +1307,8 @@ parse_tempcomp(char *line)
|
|||||||
static void
|
static void
|
||||||
parse_hwtimestamp(char *line)
|
parse_hwtimestamp(char *line)
|
||||||
{
|
{
|
||||||
HwTs_Interface *iface;
|
CNF_HwTsInterface *iface;
|
||||||
char *p;
|
char *p, filter[5];
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
if (!*line) {
|
if (!*line) {
|
||||||
@@ -1265,18 +1321,50 @@ parse_hwtimestamp(char *line)
|
|||||||
|
|
||||||
iface = ARR_GetNewElement(hwts_interfaces);
|
iface = ARR_GetNewElement(hwts_interfaces);
|
||||||
iface->name = Strdup(p);
|
iface->name = Strdup(p);
|
||||||
|
iface->minpoll = 0;
|
||||||
|
iface->min_samples = 2;
|
||||||
|
iface->max_samples = 16;
|
||||||
|
iface->nocrossts = 0;
|
||||||
|
iface->rxfilter = CNF_HWTS_RXFILTER_ANY;
|
||||||
|
iface->precision = 100.0e-9;
|
||||||
iface->tx_comp = 0.0;
|
iface->tx_comp = 0.0;
|
||||||
iface->rx_comp = 0.0;
|
iface->rx_comp = 0.0;
|
||||||
|
|
||||||
for (p = line; *p; line += n, p = line) {
|
for (p = line; *p; line += n, p = line) {
|
||||||
line = CPS_SplitWord(line);
|
line = CPS_SplitWord(line);
|
||||||
|
|
||||||
if (!strcasecmp(p, "rxcomp")) {
|
if (!strcasecmp(p, "maxsamples")) {
|
||||||
|
if (sscanf(line, "%d%n", &iface->max_samples, &n) != 1)
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(p, "minpoll")) {
|
||||||
|
if (sscanf(line, "%d%n", &iface->minpoll, &n) != 1)
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(p, "minsamples")) {
|
||||||
|
if (sscanf(line, "%d%n", &iface->min_samples, &n) != 1)
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(p, "precision")) {
|
||||||
|
if (sscanf(line, "%lf%n", &iface->precision, &n) != 1)
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(p, "rxcomp")) {
|
||||||
if (sscanf(line, "%lf%n", &iface->rx_comp, &n) != 1)
|
if (sscanf(line, "%lf%n", &iface->rx_comp, &n) != 1)
|
||||||
break;
|
break;
|
||||||
} else if (!strcasecmp(p, "txcomp")) {
|
} else if (!strcasecmp(p, "txcomp")) {
|
||||||
if (sscanf(line, "%lf%n", &iface->tx_comp, &n) != 1)
|
if (sscanf(line, "%lf%n", &iface->tx_comp, &n) != 1)
|
||||||
break;
|
break;
|
||||||
|
} else if (!strcasecmp(p, "rxfilter")) {
|
||||||
|
if (sscanf(line, "%4s%n", filter, &n) != 1)
|
||||||
|
break;
|
||||||
|
if (!strcasecmp(filter, "none"))
|
||||||
|
iface->rxfilter = CNF_HWTS_RXFILTER_NONE;
|
||||||
|
else if (!strcasecmp(filter, "ntp"))
|
||||||
|
iface->rxfilter = CNF_HWTS_RXFILTER_NTP;
|
||||||
|
else if (!strcasecmp(filter, "all"))
|
||||||
|
iface->rxfilter = CNF_HWTS_RXFILTER_ALL;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} else if (!strcasecmp(p, "nocrossts")) {
|
||||||
|
n = 0;
|
||||||
|
iface->nocrossts = 1;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1293,11 +1381,19 @@ parse_include(char *line)
|
|||||||
{
|
{
|
||||||
glob_t gl;
|
glob_t gl;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
int r;
|
||||||
|
|
||||||
check_number_of_args(line, 1);
|
check_number_of_args(line, 1);
|
||||||
|
|
||||||
if (glob(line, 0, NULL, &gl)) {
|
if ((r = glob(line,
|
||||||
DEBUG_LOG(LOGF_Configure, "glob of %s failed", line);
|
#ifdef GLOB_NOMAGIC
|
||||||
|
GLOB_NOMAGIC |
|
||||||
|
#endif
|
||||||
|
GLOB_ERR, NULL, &gl)) != 0) {
|
||||||
|
if (r != GLOB_NOMATCH)
|
||||||
|
LOG_FATAL("Could not search for files matching %s", line);
|
||||||
|
|
||||||
|
DEBUG_LOG("glob of %s failed", line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1323,7 +1419,7 @@ CNF_CreateDirs(uid_t uid, gid_t gid)
|
|||||||
existed. It MUST NOT be accessible by others as permissions on Unix
|
existed. It MUST NOT be accessible by others as permissions on Unix
|
||||||
domain sockets are ignored on some systems (e.g. Solaris). */
|
domain sockets are ignored on some systems (e.g. Solaris). */
|
||||||
if (!UTI_CheckDirPermissions(dir, 0770, uid, gid)) {
|
if (!UTI_CheckDirPermissions(dir, 0770, uid, gid)) {
|
||||||
LOG(LOGS_WARN, LOGF_Configure, "Disabled command socket %s", bind_cmd_path);
|
LOG(LOGS_WARN, "Disabled command socket %s", bind_cmd_path);
|
||||||
bind_cmd_path[0] = '\0';
|
bind_cmd_path[0] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1354,7 +1450,7 @@ CNF_AddInitSources(void)
|
|||||||
ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i);
|
ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i);
|
||||||
ntp_addr.port = cps_source.port;
|
ntp_addr.port = cps_source.port;
|
||||||
cps_source.params.iburst = 1;
|
cps_source.params.iburst = 1;
|
||||||
cps_source.params.online = 0;
|
cps_source.params.connectivity = SRC_OFFLINE;
|
||||||
|
|
||||||
NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params);
|
NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params);
|
||||||
}
|
}
|
||||||
@@ -1462,8 +1558,9 @@ CNF_GetDumpDir(void)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
CNF_GetLogMeasurements(void)
|
CNF_GetLogMeasurements(int *raw)
|
||||||
{
|
{
|
||||||
|
*raw = raw_measurements;
|
||||||
return do_log_measurements;
|
return do_log_measurements;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1541,14 +1638,6 @@ CNF_GetRtcDevice(void)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
|
||||||
CNF_GetDumpOnExit(void)
|
|
||||||
{
|
|
||||||
return do_dump_on_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ================================================== */
|
|
||||||
|
|
||||||
double
|
double
|
||||||
CNF_GetMaxUpdateSkew(void)
|
CNF_GetMaxUpdateSkew(void)
|
||||||
{
|
{
|
||||||
@@ -1729,7 +1818,7 @@ CNF_SetupAccessRestrictions(void)
|
|||||||
node = ARR_GetElement(ntp_restrictions, i);
|
node = ARR_GetElement(ntp_restrictions, i);
|
||||||
status = NCR_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
|
status = NCR_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
LOG_FATAL(LOGF_Configure, "Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
|
LOG_FATAL("Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1737,7 +1826,7 @@ CNF_SetupAccessRestrictions(void)
|
|||||||
node = ARR_GetElement(cmd_restrictions, i);
|
node = ARR_GetElement(cmd_restrictions, i);
|
||||||
status = CAM_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
|
status = CAM_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
LOG_FATAL(LOGF_Configure, "Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
|
LOG_FATAL("Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1968,17 +2057,91 @@ CNF_GetInitStepThreshold(void)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
CNF_GetHwTsInterface(unsigned int index, char **name, double *tx_comp, double *rx_comp)
|
CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface)
|
||||||
{
|
{
|
||||||
HwTs_Interface *iface;
|
|
||||||
|
|
||||||
if (index >= ARR_GetSize(hwts_interfaces))
|
if (index >= ARR_GetSize(hwts_interfaces))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
iface = ARR_GetElement(hwts_interfaces, index);
|
*iface = (CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, index);
|
||||||
*name = iface->name;
|
|
||||||
*tx_comp = iface->tx_comp;
|
|
||||||
*rx_comp = iface->rx_comp;
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
char *
|
||||||
|
CNF_GetNtsCacheDir(void)
|
||||||
|
{
|
||||||
|
return nts_cachedir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
char *
|
||||||
|
CNF_GetNtsServerCertFile(void)
|
||||||
|
{
|
||||||
|
return nts_server_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
char *
|
||||||
|
CNF_GetNtsServerKeyFile(void)
|
||||||
|
{
|
||||||
|
return nts_server_key_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNtsServerPort(void)
|
||||||
|
{
|
||||||
|
return nts_server_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNtsServerProcesses(void)
|
||||||
|
{
|
||||||
|
return nts_server_processes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNtsServerConnections(void)
|
||||||
|
{
|
||||||
|
return nts_server_connections;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNtsRefresh(void)
|
||||||
|
{
|
||||||
|
return nts_refresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNtsRotate(void)
|
||||||
|
{
|
||||||
|
return nts_rotate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
char *
|
||||||
|
CNF_GetNtsTrustedCertFile(void)
|
||||||
|
{
|
||||||
|
return nts_trusted_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
CNF_GetNoSystemCert(void)
|
||||||
|
{
|
||||||
|
return no_system_cert;
|
||||||
|
}
|
||||||
|
|||||||
37
conf.h
37
conf.h
@@ -31,7 +31,7 @@
|
|||||||
#include "addressing.h"
|
#include "addressing.h"
|
||||||
#include "reference.h"
|
#include "reference.h"
|
||||||
|
|
||||||
extern void CNF_Initialise(int restarted);
|
extern void CNF_Initialise(int restarted, int client_only);
|
||||||
extern void CNF_Finalise(void);
|
extern void CNF_Finalise(void);
|
||||||
|
|
||||||
extern char *CNF_GetRtcDevice(void);
|
extern char *CNF_GetRtcDevice(void);
|
||||||
@@ -52,7 +52,7 @@ extern char *CNF_GetDriftFile(void);
|
|||||||
extern char *CNF_GetLogDir(void);
|
extern char *CNF_GetLogDir(void);
|
||||||
extern char *CNF_GetDumpDir(void);
|
extern char *CNF_GetDumpDir(void);
|
||||||
extern int CNF_GetLogBanner(void);
|
extern int CNF_GetLogBanner(void);
|
||||||
extern int CNF_GetLogMeasurements(void);
|
extern int CNF_GetLogMeasurements(int *raw);
|
||||||
extern int CNF_GetLogStatistics(void);
|
extern int CNF_GetLogStatistics(void);
|
||||||
extern int CNF_GetLogTracking(void);
|
extern int CNF_GetLogTracking(void);
|
||||||
extern int CNF_GetLogRtc(void);
|
extern int CNF_GetLogRtc(void);
|
||||||
@@ -60,7 +60,6 @@ extern int CNF_GetLogRefclocks(void);
|
|||||||
extern int CNF_GetLogTempComp(void);
|
extern int CNF_GetLogTempComp(void);
|
||||||
extern char *CNF_GetKeysFile(void);
|
extern char *CNF_GetKeysFile(void);
|
||||||
extern char *CNF_GetRtcFile(void);
|
extern char *CNF_GetRtcFile(void);
|
||||||
extern int CNF_GetDumpOnExit(void);
|
|
||||||
extern int CNF_GetManualEnabled(void);
|
extern int CNF_GetManualEnabled(void);
|
||||||
extern int CNF_GetCommandPort(void);
|
extern int CNF_GetCommandPort(void);
|
||||||
extern int CNF_GetRtcOnUtc(void);
|
extern int CNF_GetRtcOnUtc(void);
|
||||||
@@ -119,6 +118,36 @@ extern char *CNF_GetHwclockFile(void);
|
|||||||
extern int CNF_GetInitSources(void);
|
extern int CNF_GetInitSources(void);
|
||||||
extern double CNF_GetInitStepThreshold(void);
|
extern double CNF_GetInitStepThreshold(void);
|
||||||
|
|
||||||
extern int CNF_GetHwTsInterface(unsigned int index, char **name, double *tx_comp, double *rx_comp);
|
typedef enum {
|
||||||
|
CNF_HWTS_RXFILTER_ANY,
|
||||||
|
CNF_HWTS_RXFILTER_NONE,
|
||||||
|
CNF_HWTS_RXFILTER_NTP,
|
||||||
|
CNF_HWTS_RXFILTER_ALL,
|
||||||
|
} CNF_HwTs_RxFilter;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *name;
|
||||||
|
int minpoll;
|
||||||
|
int min_samples;
|
||||||
|
int max_samples;
|
||||||
|
int nocrossts;
|
||||||
|
CNF_HwTs_RxFilter rxfilter;
|
||||||
|
double precision;
|
||||||
|
double tx_comp;
|
||||||
|
double rx_comp;
|
||||||
|
} CNF_HwTsInterface;
|
||||||
|
|
||||||
|
extern int CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface);
|
||||||
|
|
||||||
|
extern char *CNF_GetNtsCacheDir(void);
|
||||||
|
extern char *CNF_GetNtsServerCertFile(void);
|
||||||
|
extern char *CNF_GetNtsServerKeyFile(void);
|
||||||
|
extern int CNF_GetNtsServerPort(void);
|
||||||
|
extern int CNF_GetNtsServerProcesses(void);
|
||||||
|
extern int CNF_GetNtsServerConnections(void);
|
||||||
|
extern int CNF_GetNtsRefresh(void);
|
||||||
|
extern int CNF_GetNtsRotate(void);
|
||||||
|
extern char *CNF_GetNtsTrustedCertFile(void);
|
||||||
|
extern int CNF_GetNoSystemCert(void);
|
||||||
|
|
||||||
#endif /* GOT_CONF_H */
|
#endif /* GOT_CONF_H */
|
||||||
|
|||||||
299
configure
vendored
299
configure
vendored
@@ -5,7 +5,8 @@
|
|||||||
#
|
#
|
||||||
# Copyright (C) Richard P. Curnow 1997-2003
|
# Copyright (C) Richard P. Curnow 1997-2003
|
||||||
# Copyright (C) Bryan Christianson 2016
|
# Copyright (C) Bryan Christianson 2016
|
||||||
# Copyright (C) Miroslav Lichvar 2009, 2012-2016
|
# Copyright (C) Miroslav Lichvar 2009, 2012-2018
|
||||||
|
# Copyright (C) Stefan R. Filipek 2019
|
||||||
#
|
#
|
||||||
# =======================================================================
|
# =======================================================================
|
||||||
|
|
||||||
@@ -54,6 +55,34 @@ test_code () {
|
|||||||
return $result
|
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
|
||||||
usage () {
|
usage () {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
@@ -85,8 +114,11 @@ For better control, use the options below.
|
|||||||
--with-readline-library=DIR Specify where readline lib directory is
|
--with-readline-library=DIR Specify where readline lib directory is
|
||||||
--with-ncurses-library=DIR Specify where ncurses lib directory is
|
--with-ncurses-library=DIR Specify where ncurses lib directory is
|
||||||
--disable-sechash Disable support for hashes other than MD5
|
--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-nss Don't use NSS even if it is available
|
||||||
--without-tomcrypt Don't use libtomcrypt 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-cmdmon Disable command and monitoring support
|
||||||
--disable-ntp Disable NTP support
|
--disable-ntp Disable NTP support
|
||||||
--disable-refclock Disable reference clock support
|
--disable-refclock Disable reference clock support
|
||||||
@@ -107,7 +139,7 @@ For better control, use the options below.
|
|||||||
since 1970-01-01 [50*365 days ago]
|
since 1970-01-01 [50*365 days ago]
|
||||||
--with-user=USER Specify default chronyd user [root]
|
--with-user=USER Specify default chronyd user [root]
|
||||||
--with-hwclockfile=PATH Specify default path to hwclock(8) adjtime file
|
--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-rtcdevice=PATH Specify default path to RTC device [/dev/rtc]
|
||||||
--with-sendmail=PATH Path to sendmail binary [/usr/lib/sendmail]
|
--with-sendmail=PATH Path to sendmail binary [/usr/lib/sendmail]
|
||||||
--enable-debug Enable debugging support
|
--enable-debug Enable debugging support
|
||||||
@@ -135,6 +167,11 @@ Some influential environment variables:
|
|||||||
headers in a nonstandard directory <include dir>
|
headers in a nonstandard directory <include dir>
|
||||||
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
|
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
|
||||||
nonstandard directory <lib dir>
|
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
|
Use these variables to override the choices made by \`configure' or to help
|
||||||
it to find libraries and programs with nonstandard names/locations.
|
it to find libraries and programs with nonstandard names/locations.
|
||||||
@@ -152,13 +189,6 @@ add_def () {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
#}}}
|
#}}}
|
||||||
#{{{ pkg_config
|
|
||||||
pkg_config () {
|
|
||||||
type pkg-config > /dev/null 2> /dev/null || return 1
|
|
||||||
|
|
||||||
pkg-config $@ 2> /dev/null
|
|
||||||
}
|
|
||||||
#}}}
|
|
||||||
#{{{ get_features
|
#{{{ get_features
|
||||||
get_features () {
|
get_features () {
|
||||||
ff=1
|
ff=1
|
||||||
@@ -184,11 +214,11 @@ OPERATINGSYSTEM=`uname -s`
|
|||||||
VERSION=`uname -r`
|
VERSION=`uname -r`
|
||||||
MACHINE=`uname -m`
|
MACHINE=`uname -m`
|
||||||
|
|
||||||
|
LIBS=""
|
||||||
EXTRA_LIBS=""
|
EXTRA_LIBS=""
|
||||||
EXTRA_CLI_LIBS=""
|
EXTRA_CLI_LIBS=""
|
||||||
EXTRA_OBJECTS=""
|
EXTRA_OBJECTS=""
|
||||||
EXTRA_DEFS=""
|
EXTRA_CLI_OBJECTS=""
|
||||||
SYSDEFS=""
|
|
||||||
|
|
||||||
feat_debug=0
|
feat_debug=0
|
||||||
feat_cmdmon=1
|
feat_cmdmon=1
|
||||||
@@ -198,8 +228,11 @@ feat_readline=1
|
|||||||
try_readline=1
|
try_readline=1
|
||||||
try_editline=1
|
try_editline=1
|
||||||
feat_sechash=1
|
feat_sechash=1
|
||||||
|
try_nettle=1
|
||||||
try_nss=1
|
try_nss=1
|
||||||
try_tomcrypt=1
|
try_tomcrypt=1
|
||||||
|
feat_nts=1
|
||||||
|
try_gnutls=1
|
||||||
feat_rtc=1
|
feat_rtc=1
|
||||||
try_rtc=0
|
try_rtc=0
|
||||||
feat_droproot=1
|
feat_droproot=1
|
||||||
@@ -225,9 +258,10 @@ feat_timestamping=1
|
|||||||
try_timestamping=0
|
try_timestamping=0
|
||||||
feat_ntp_signd=0
|
feat_ntp_signd=0
|
||||||
ntp_era_split=""
|
ntp_era_split=""
|
||||||
|
use_pthread=0
|
||||||
default_user="root"
|
default_user="root"
|
||||||
default_hwclockfile=""
|
default_hwclockfile=""
|
||||||
default_pidfile="/var/run/chronyd.pid"
|
default_pidfile="/var/run/chrony/chronyd.pid"
|
||||||
default_rtcdevice="/dev/rtc"
|
default_rtcdevice="/dev/rtc"
|
||||||
mail_program="/usr/lib/sendmail"
|
mail_program="/usr/lib/sendmail"
|
||||||
|
|
||||||
@@ -360,12 +394,21 @@ do
|
|||||||
--disable-sechash )
|
--disable-sechash )
|
||||||
feat_sechash=0
|
feat_sechash=0
|
||||||
;;
|
;;
|
||||||
|
--without-nettle )
|
||||||
|
try_nettle=0
|
||||||
|
;;
|
||||||
--without-nss )
|
--without-nss )
|
||||||
try_nss=0
|
try_nss=0
|
||||||
;;
|
;;
|
||||||
--without-tomcrypt )
|
--without-tomcrypt )
|
||||||
try_tomcrypt=0
|
try_tomcrypt=0
|
||||||
;;
|
;;
|
||||||
|
--disable-nts )
|
||||||
|
feat_nts=0
|
||||||
|
;;
|
||||||
|
--without-gnutls )
|
||||||
|
try_gnutls=0
|
||||||
|
;;
|
||||||
--host-system=* )
|
--host-system=* )
|
||||||
OPERATINGSYSTEM=`echo $option | sed -e 's/^.*=//;'`
|
OPERATINGSYSTEM=`echo $option | sed -e 's/^.*=//;'`
|
||||||
;;
|
;;
|
||||||
@@ -390,7 +433,7 @@ SYSTEM=${OPERATINGSYSTEM}-${MACHINE}
|
|||||||
|
|
||||||
case $OPERATINGSYSTEM in
|
case $OPERATINGSYSTEM in
|
||||||
Linux)
|
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_libcap != "0" ] && try_libcap=1
|
||||||
try_rtc=1
|
try_rtc=1
|
||||||
[ $try_seccomp != "0" ] && try_seccomp=1
|
[ $try_seccomp != "0" ] && try_seccomp=1
|
||||||
@@ -405,7 +448,9 @@ case $OPERATINGSYSTEM in
|
|||||||
# recvmmsg() seems to be broken on FreeBSD 11.0 and it's just
|
# recvmmsg() seems to be broken on FreeBSD 11.0 and it's just
|
||||||
# a wrapper around recvmsg()
|
# a wrapper around recvmsg()
|
||||||
try_recvmmsg=0
|
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
|
add_def FREEBSD
|
||||||
if [ $feat_droproot = "1" ]; then
|
if [ $feat_droproot = "1" ]; then
|
||||||
add_def FEAT_PRIVDROP
|
add_def FEAT_PRIVDROP
|
||||||
@@ -414,26 +459,37 @@ case $OPERATINGSYSTEM in
|
|||||||
echo "Configuring for $SYSTEM"
|
echo "Configuring for $SYSTEM"
|
||||||
;;
|
;;
|
||||||
NetBSD)
|
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_clockctl=1
|
||||||
|
try_setsched=1
|
||||||
|
try_lockmem=1
|
||||||
add_def NETBSD
|
add_def NETBSD
|
||||||
echo "Configuring for $SYSTEM"
|
echo "Configuring for $SYSTEM"
|
||||||
;;
|
;;
|
||||||
Darwin)
|
Darwin)
|
||||||
EXTRA_OBJECTS="sys_macosx.o"
|
EXTRA_OBJECTS="sys_macosx.o"
|
||||||
EXTRA_LIBS="-lresolv"
|
LIBS="$LIBS -lresolv"
|
||||||
EXTRA_CLI_LIBS="-lresolv"
|
|
||||||
add_def MACOSX
|
add_def MACOSX
|
||||||
if [ $feat_droproot = "1" ]; then
|
if [ $feat_droproot = "1" ]; then
|
||||||
add_def FEAT_PRIVDROP
|
add_def FEAT_PRIVDROP
|
||||||
priv_ops="ADJUSTTIME SETTIME BINDSOCKET"
|
priv_ops="ADJUSTTIME SETTIME BINDSOCKET"
|
||||||
fi
|
fi
|
||||||
|
major=`echo $VERSION | cut -d. -f1`
|
||||||
|
# ntp_adjtime is not available in macOS 10.12 (Darwin 16.x.x) and earlier
|
||||||
|
if [ $major -gt "16" ]; then
|
||||||
|
add_def HAVE_MACOS_SYS_TIMEX
|
||||||
|
EXTRA_OBJECTS="$EXTRA_OBJECTS sys_generic.o sys_netbsd.o sys_timex.o"
|
||||||
|
if [ $feat_droproot = "1" ]; then
|
||||||
|
priv_ops="$priv_ops ADJUSTTIMEX"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
echo "Configuring for macOS (" $SYSTEM "macOS version" $VERSION ")"
|
echo "Configuring for macOS (" $SYSTEM "macOS version" $VERSION ")"
|
||||||
;;
|
;;
|
||||||
SunOS)
|
SunOS)
|
||||||
EXTRA_OBJECTS="sys_generic.o sys_solaris.o sys_timex.o"
|
EXTRA_OBJECTS="sys_generic.o sys_solaris.o sys_timex.o sys_posix.o"
|
||||||
EXTRA_LIBS="-lsocket -lnsl -lresolv"
|
LIBS="$LIBS -lsocket -lnsl -lresolv"
|
||||||
EXTRA_CLI_LIBS="-lsocket -lnsl -lresolv"
|
try_setsched=1
|
||||||
|
try_lockmem=1
|
||||||
add_def SOLARIS
|
add_def SOLARIS
|
||||||
# These are needed to have msg_control in struct msghdr
|
# These are needed to have msg_control in struct msghdr
|
||||||
add_def __EXTENSIONS__
|
add_def __EXTENSIONS__
|
||||||
@@ -463,7 +519,7 @@ fi
|
|||||||
|
|
||||||
if [ $feat_ntp = "1" ]; then
|
if [ $feat_ntp = "1" ]; then
|
||||||
add_def FEAT_NTP
|
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
|
if [ $feat_ntp_signd = "1" ]; then
|
||||||
add_def FEAT_SIGND
|
add_def FEAT_SIGND
|
||||||
EXTRA_OBJECTS="$EXTRA_OBJECTS ntp_signd.o"
|
EXTRA_OBJECTS="$EXTRA_OBJECTS ntp_signd.o"
|
||||||
@@ -490,14 +546,16 @@ MYCPPFLAGS="$CPPFLAGS"
|
|||||||
MYLDFLAGS="$LDFLAGS"
|
MYLDFLAGS="$LDFLAGS"
|
||||||
|
|
||||||
if [ "x$MYCC" = "x" ]; then
|
if [ "x$MYCC" = "x" ]; then
|
||||||
MYCC=gcc
|
for cc in gcc clang cc ""; do
|
||||||
if ! test_code "$MYCC" '' '' '' ''; then
|
if [ "x$cc" = "x" ]; then
|
||||||
MYCC=cc
|
|
||||||
if ! test_code "$MYCC" '' '' '' ''; then
|
|
||||||
echo "error: no C compiler found"
|
echo "error: no C compiler found"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
MYCC=$cc
|
||||||
|
if test_code "$MYCC" '' '' '' ''; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
else
|
else
|
||||||
if ! test_code "$MYCC" '' '' '' ''; then
|
if ! test_code "$MYCC" '' '' '' ''; then
|
||||||
echo "error: C compiler $MYCC cannot create executables"
|
echo "error: C compiler $MYCC cannot create executables"
|
||||||
@@ -507,12 +565,38 @@ fi
|
|||||||
|
|
||||||
if [ "x$MYCFLAGS" = "x" ]; then
|
if [ "x$MYCFLAGS" = "x" ]; then
|
||||||
MYCFLAGS="-O2 -g"
|
MYCFLAGS="-O2 -g"
|
||||||
|
|
||||||
|
TESTCFLAGS="-D_FORTIFY_SOURCE=2 -fPIE"
|
||||||
|
TESTLDFLAGS="-pie -Wl,-z,relro,-z,now"
|
||||||
|
if test_code 'hardening compiler options' '' "$TESTCFLAGS" "$TESTLDFLAGS" ''; then
|
||||||
|
MYCFLAGS="$MYCFLAGS $TESTCFLAGS"
|
||||||
|
MYLDFLAGS="$MYLDFLAGS $TESTLDFLAGS"
|
||||||
|
fi
|
||||||
|
TESTCFLAGS="-fstack-protector-strong --param=ssp-buffer-size=4"
|
||||||
|
if test_code '-fstack-protector-strong' '' "$TESTCFLAGS" '' ''; then
|
||||||
|
MYCFLAGS="$MYCFLAGS $TESTCFLAGS"
|
||||||
|
else
|
||||||
|
TESTCFLAGS="-fstack-protector --param=ssp-buffer-size=4"
|
||||||
|
if test_code '-fstack-protector' '' "$TESTCFLAGS" '' ''; then
|
||||||
|
MYCFLAGS="$MYCFLAGS $TESTCFLAGS"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "x$MYCC" = "xgcc" ]; then
|
if [ "x$MYCC" = "xgcc" ] || [ "x$MYCC" = "xclang" ]; then
|
||||||
MYCFLAGS="$MYCFLAGS -Wmissing-prototypes -Wall"
|
MYCFLAGS="$MYCFLAGS -Wmissing-prototypes -Wall"
|
||||||
fi
|
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' '' '' '
|
if test_code '64-bit time_t' 'time.h' '' '' '
|
||||||
char x[sizeof(time_t) > 4 ? 1 : -1] = {0};
|
char x[sizeof(time_t) > 4 ? 1 : -1] = {0};
|
||||||
return x[0];'
|
return x[0];'
|
||||||
@@ -523,7 +607,11 @@ then
|
|||||||
split_seconds=$ntp_era_split
|
split_seconds=$ntp_era_split
|
||||||
split_days=0
|
split_days=0
|
||||||
else
|
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
|
if [ "x$split_seconds" = "x" ]; then
|
||||||
echo "error: could not get current time, --with-ntp-era option is needed"
|
echo "error: could not get current time, --with-ntp-era option is needed"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -548,25 +636,15 @@ then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
MATHCODE='return (int) pow(2.0, log(sqrt((double)argc)));'
|
MATHCODE='return (int) pow(2.0, log(sqrt((double)argc)));'
|
||||||
if test_code 'math' 'math.h' '' '' "$MATHCODE"; then
|
if ! test_code 'math' 'math.h' '' '' "$MATHCODE"; then
|
||||||
LIBS=""
|
|
||||||
else
|
|
||||||
if test_code 'math in -lm' 'math.h' '' '-lm' "$MATHCODE"; then
|
if test_code 'math in -lm' 'math.h' '' '-lm' "$MATHCODE"; then
|
||||||
LIBS="-lm"
|
LIBS="$LIBS -lm"
|
||||||
else
|
else
|
||||||
echo "error: could not compile/link a program which uses sqrt(), log(), pow()"
|
echo "error: could not compile/link a program which uses sqrt(), log(), pow()"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
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' '' '' '
|
if test_code 'struct in_pktinfo' 'sys/socket.h netinet/in.h' '' '' '
|
||||||
struct in_pktinfo ipi;
|
struct in_pktinfo ipi;
|
||||||
return sizeof (ipi.ipi_spec_dst.s_addr) + IP_PKTINFO;'
|
return sizeof (ipi.ipi_spec_dst.s_addr) + IP_PKTINFO;'
|
||||||
@@ -575,7 +653,7 @@ then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $feat_ipv6 = "1" ] && \
|
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;
|
struct sockaddr_in6 n;
|
||||||
char p[100];
|
char p[100];
|
||||||
n.sin6_addr = in6addr_any;
|
n.sin6_addr = in6addr_any;
|
||||||
@@ -611,35 +689,41 @@ if [ $try_clock_gettime = "1" ]; then
|
|||||||
fi
|
fi
|
||||||
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);'
|
'return getaddrinfo(0, 0, 0, 0);'
|
||||||
then
|
then
|
||||||
add_def HAVE_GETADDRINFO
|
add_def HAVE_GETADDRINFO
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $feat_asyncdns = "1" ] && \
|
if [ $feat_asyncdns = "1" ] && \
|
||||||
test_code 'pthread' 'pthread.h' '-pthread' '' \
|
test_code 'pthread' 'pthread.h' '-pthread' '' '
|
||||||
'return pthread_create((void *)1, NULL, (void *)1, NULL);'
|
pthread_t thread;
|
||||||
|
return (int)pthread_create(&thread, NULL, (void *)1, NULL);'
|
||||||
then
|
then
|
||||||
add_def FEAT_ASYNCDNS
|
add_def FEAT_ASYNCDNS
|
||||||
add_def USE_PTHREAD_ASYNCDNS
|
add_def USE_PTHREAD_ASYNCDNS
|
||||||
EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o"
|
EXTRA_OBJECTS="$EXTRA_OBJECTS nameserv_async.o"
|
||||||
MYCFLAGS="$MYCFLAGS -pthread"
|
use_pthread=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
|
if test_code 'arc4random_buf()' 'stdlib.h' '' '' 'arc4random_buf(NULL, 0);'; then
|
||||||
add_def HAVE_ARC4RANDOM
|
add_def HAVE_ARC4RANDOM
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if test_code 'getrandom()' 'stdlib.h sys/random.h' '' '' \
|
||||||
|
'return getrandom(NULL, 256, 0);'; then
|
||||||
|
add_def HAVE_GETRANDOM
|
||||||
|
fi
|
||||||
|
|
||||||
RECVMMSG_CODE='
|
RECVMMSG_CODE='
|
||||||
struct mmsghdr hdr;
|
struct mmsghdr hdr;
|
||||||
return !recvmmsg(0, &hdr, 1, MSG_DONTWAIT, 0);'
|
return !recvmmsg(0, &hdr, 1, MSG_DONTWAIT, 0);'
|
||||||
if [ $try_recvmmsg = "1" ]; then
|
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
|
add_def HAVE_RECVMMSG
|
||||||
else
|
else
|
||||||
if test_code 'recvmmsg() with _GNU_SOURCE' 'sys/socket.h' '-D_GNU_SOURCE' \
|
if test_code 'recvmmsg() with _GNU_SOURCE' 'sys/socket.h' '-D_GNU_SOURCE' \
|
||||||
"$EXTRA_LIBS" "$RECVMMSG_CODE"
|
"$LIBS" "$RECVMMSG_CODE"
|
||||||
then
|
then
|
||||||
add_def _GNU_SOURCE
|
add_def _GNU_SOURCE
|
||||||
add_def HAVE_RECVMMSG
|
add_def HAVE_RECVMMSG
|
||||||
@@ -658,15 +742,27 @@ if [ $feat_timestamping = "1" ] && [ $try_timestamping = "1" ] &&
|
|||||||
then
|
then
|
||||||
add_def HAVE_LINUX_TIMESTAMPING
|
add_def HAVE_LINUX_TIMESTAMPING
|
||||||
EXTRA_OBJECTS="$EXTRA_OBJECTS hwclock.o ntp_io_linux.o"
|
EXTRA_OBJECTS="$EXTRA_OBJECTS hwclock.o ntp_io_linux.o"
|
||||||
|
|
||||||
|
if test_code 'other timestamping options' \
|
||||||
|
'sys/types.h sys/socket.h linux/net_tstamp.h' '' '' '
|
||||||
|
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
|
||||||
|
add_def HAVE_LINUX_TIMESTAMPING_OPT_TX_SWHW 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
timepps_h=""
|
timepps_h=""
|
||||||
if [ $feat_refclock = "1" ] && [ $feat_pps = "1" ]; then
|
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"
|
timepps_h="sys/timepps.h"
|
||||||
add_def HAVE_SYS_TIMEPPS_H
|
add_def HAVE_SYS_TIMEPPS_H
|
||||||
else
|
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"
|
timepps_h="timepps.h"
|
||||||
add_def HAVE_TIMEPPS_H
|
add_def HAVE_TIMEPPS_H
|
||||||
fi
|
fi
|
||||||
@@ -674,10 +770,11 @@ if [ $feat_refclock = "1" ] && [ $feat_pps = "1" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "x$timepps_h" != "x" ] && \
|
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_handle_t h = 0;
|
||||||
pps_info_t i;
|
pps_info_t i;
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
ts.tv_sec = ts.tv_nsec = 0;
|
||||||
return time_pps_fetch(h, PPS_TSFMT_TSPEC, &i, &ts);'
|
return time_pps_fetch(h, PPS_TSFMT_TSPEC, &i, &ts);'
|
||||||
then
|
then
|
||||||
add_def FEAT_PPS
|
add_def FEAT_PPS
|
||||||
@@ -709,7 +806,7 @@ then
|
|||||||
# NAME2IPADDRESS shouldn't be enabled with other operations as the helper
|
# NAME2IPADDRESS shouldn't be enabled with other operations as the helper
|
||||||
# process works on one request at the time and the async resolver could
|
# process works on one request at the time and the async resolver could
|
||||||
# block the main thread
|
# block the main thread
|
||||||
priv_ops="NAME2IPADDRESS"
|
priv_ops="NAME2IPADDRESS RELOADDNS"
|
||||||
EXTRA_LIBS="$EXTRA_LIBS -lseccomp"
|
EXTRA_LIBS="$EXTRA_LIBS -lseccomp"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -732,32 +829,43 @@ fi
|
|||||||
if [ $feat_refclock = "1" ] && [ $feat_phc = "1" ] && [ $try_phc = "1" ] && \
|
if [ $feat_refclock = "1" ] && [ $feat_phc = "1" ] && [ $try_phc = "1" ] && \
|
||||||
grep '#define HAVE_CLOCK_GETTIME' config.h > /dev/null && \
|
grep '#define HAVE_CLOCK_GETTIME' config.h > /dev/null && \
|
||||||
test_code '<linux/ptp_clock.h>' 'sys/ioctl.h linux/ptp_clock.h' '' '' \
|
test_code '<linux/ptp_clock.h>' 'sys/ioctl.h linux/ptp_clock.h' '' '' \
|
||||||
'ioctl(1, PTP_CLOCK_GETCAPS, 0);'
|
'ioctl(1, PTP_CLOCK_GETCAPS + PTP_SYS_OFFSET, 0);'
|
||||||
then
|
then
|
||||||
|
grep 'HAVE_LINUX_TIMESTAMPING' config.h > /dev/null ||
|
||||||
|
EXTRA_OBJECTS="$EXTRA_OBJECTS hwclock.o"
|
||||||
add_def FEAT_PHC
|
add_def FEAT_PHC
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $try_setsched = "1" ] && \
|
if [ $try_setsched = "1" ] && \
|
||||||
test_code \
|
test_code \
|
||||||
'sched_setscheduler()' \
|
'pthread_setschedparam()' \
|
||||||
'sched.h' '' '' '
|
'pthread.h sched.h' '-pthread' '' '
|
||||||
struct sched_param sched;
|
struct sched_param sched;
|
||||||
sched_get_priority_max(SCHED_FIFO);
|
sched_get_priority_max(SCHED_FIFO);
|
||||||
sched_setscheduler(0, SCHED_FIFO, &sched);'
|
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);'
|
||||||
then
|
then
|
||||||
add_def HAVE_SCHED_SETSCHEDULER
|
add_def HAVE_PTHREAD_SETSCHEDPARAM
|
||||||
|
use_pthread=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $try_lockmem = "1" ] && \
|
if [ $try_lockmem = "1" ] && \
|
||||||
test_code \
|
test_code \
|
||||||
'mlockall()' \
|
'mlockall()' \
|
||||||
'sys/mman.h sys/resource.h' '' '' '
|
'sys/mman.h' '' '' '
|
||||||
struct rlimit rlim;
|
|
||||||
setrlimit(RLIMIT_MEMLOCK, &rlim);
|
|
||||||
mlockall(MCL_CURRENT|MCL_FUTURE);'
|
mlockall(MCL_CURRENT|MCL_FUTURE);'
|
||||||
then
|
then
|
||||||
add_def HAVE_MLOCKALL
|
add_def HAVE_MLOCKALL
|
||||||
fi
|
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" ]
|
if [ $feat_forcednsretry = "1" ]
|
||||||
then
|
then
|
||||||
@@ -807,7 +915,29 @@ fi
|
|||||||
HASH_OBJ="hash_intmd5.o"
|
HASH_OBJ="hash_intmd5.o"
|
||||||
HASH_LINK=""
|
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_cflags="`pkg_config --cflags nss`"
|
||||||
test_link="`pkg_config --libs-only-L nss` -lfreebl3"
|
test_link="`pkg_config --libs-only-L nss` -lfreebl3"
|
||||||
if test_code 'NSS' 'nss.h hasht.h nsslowhash.h' \
|
if test_code 'NSS' 'nss.h hasht.h nsslowhash.h' \
|
||||||
@@ -816,7 +946,6 @@ if [ $feat_sechash = "1" ] && [ $try_nss = "1" ]; then
|
|||||||
then
|
then
|
||||||
HASH_OBJ="hash_nss.o"
|
HASH_OBJ="hash_nss.o"
|
||||||
HASH_LINK="$test_link"
|
HASH_LINK="$test_link"
|
||||||
LIBS="$LIBS $HASH_LINK"
|
|
||||||
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||||
add_def FEAT_SECHASH
|
add_def FEAT_SECHASH
|
||||||
fi
|
fi
|
||||||
@@ -828,12 +957,48 @@ if [ $feat_sechash = "1" ] && [ "x$HASH_LINK" = "x" ] && [ $try_tomcrypt = "1" ]
|
|||||||
then
|
then
|
||||||
HASH_OBJ="hash_tomcrypt.o"
|
HASH_OBJ="hash_tomcrypt.o"
|
||||||
HASH_LINK="-ltomcrypt"
|
HASH_LINK="-ltomcrypt"
|
||||||
LIBS="$LIBS $HASH_LINK"
|
|
||||||
MYCPPFLAGS="$MYCPPFLAGS -I/usr/include/tomcrypt"
|
MYCPPFLAGS="$MYCPPFLAGS -I/usr/include/tomcrypt"
|
||||||
add_def FEAT_SECHASH
|
add_def FEAT_SECHASH
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
EXTRA_OBJECTS="$EXTRA_OBJECTS $HASH_OBJ"
|
||||||
|
EXTRA_CLI_OBJECTS="$EXTRA_CLI_OBJECTS $HASH_OBJ"
|
||||||
|
LIBS="$LIBS $HASH_LINK"
|
||||||
|
|
||||||
|
if [ $feat_ntp = "1" ] && [ $feat_nts = "1" ] && [ $try_gnutls = "1" ] && \
|
||||||
|
echo "$HASH_LINK" | grep 'nettle' > /dev/null; 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_priority_init2(NULL, "", NULL, GNUTLS_PRIORITY_INIT_DEF_APPEND) +
|
||||||
|
gnutls_prf_rfc5705(NULL, 0, "", 0, "", 16, NULL);' &&
|
||||||
|
test_code 'AES128 in nettle' 'nettle/aes.h' '' "$LIBS" \
|
||||||
|
'aes128_set_encrypt_key(NULL, 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"
|
||||||
|
EXTRA_OBJECTS="$EXTRA_OBJECTS siv_nettle.o"
|
||||||
|
LIBS="$LIBS $test_link"
|
||||||
|
MYCPPFLAGS="$MYCPPFLAGS $test_cflags"
|
||||||
|
add_def FEAT_NTS
|
||||||
|
|
||||||
|
add_def HAVE_SIV
|
||||||
|
if test_code 'SIV in nettle' \
|
||||||
|
'nettle/siv-cmac.h' "" "$LIBS" \
|
||||||
|
'siv_cmac_aes128_set_key(NULL, NULL);'
|
||||||
|
then
|
||||||
|
add_def HAVE_NETTLE_SIV_CMAC
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $use_pthread = "1" ]; then
|
||||||
|
MYCFLAGS="$MYCFLAGS -pthread"
|
||||||
|
fi
|
||||||
|
|
||||||
SYSCONFDIR=/etc
|
SYSCONFDIR=/etc
|
||||||
if [ "x$SETSYSCONFDIR" != "x" ]; then
|
if [ "x$SETSYSCONFDIR" != "x" ]; then
|
||||||
SYSCONFDIR=$SETSYSCONFDIR
|
SYSCONFDIR=$SETSYSCONFDIR
|
||||||
@@ -897,9 +1062,9 @@ add_def DEFAULT_USER "\"$default_user\""
|
|||||||
add_def DEFAULT_COMMAND_SOCKET "\"$CHRONYRUNDIR/chronyd.sock\""
|
add_def DEFAULT_COMMAND_SOCKET "\"$CHRONYRUNDIR/chronyd.sock\""
|
||||||
add_def MAIL_PROGRAM "\"$mail_program\""
|
add_def MAIL_PROGRAM "\"$mail_program\""
|
||||||
|
|
||||||
common_features="`get_features IPV6 DEBUG`"
|
common_features="`get_features SECHASH IPV6 DEBUG`"
|
||||||
chronyc_features="`get_features READLINE`"
|
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 CHRONYC_FEATURES "\"$chronyc_features $common_features\""
|
||||||
add_def CHRONYD_FEATURES "\"$chronyd_features $common_features\""
|
add_def CHRONYD_FEATURES "\"$chronyd_features $common_features\""
|
||||||
echo "Features : $chronyd_features $chronyc_features $common_features"
|
echo "Features : $chronyd_features $chronyc_features $common_features"
|
||||||
@@ -915,15 +1080,15 @@ add_def CHRONY_VERSION "\"${CHRONY_VERSION}\""
|
|||||||
for f in Makefile doc/Makefile test/unit/Makefile
|
for f in Makefile doc/Makefile test/unit/Makefile
|
||||||
do
|
do
|
||||||
echo Creating $f
|
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%@CC@%${MYCC}%;\
|
||||||
s%@CFLAGS@%${MYCFLAGS}%;\
|
s%@CFLAGS@%${MYCFLAGS}%;\
|
||||||
s%@CPPFLAGS@%${MYCPPFLAGS}%;\
|
s%@CPPFLAGS@%${MYCPPFLAGS}%;\
|
||||||
s%@LIBS@%${LIBS}%;\
|
|
||||||
s%@LDFLAGS@%${MYLDFLAGS}%;\
|
s%@LDFLAGS@%${MYLDFLAGS}%;\
|
||||||
|
s%@LIBS@%${LIBS}%;\
|
||||||
s%@EXTRA_LIBS@%${EXTRA_LIBS}%;\
|
s%@EXTRA_LIBS@%${EXTRA_LIBS}%;\
|
||||||
s%@EXTRA_CLI_LIBS@%${EXTRA_CLI_LIBS}%;\
|
s%@EXTRA_CLI_LIBS@%${EXTRA_CLI_LIBS}%;\
|
||||||
s%@HASH_OBJ@%${HASH_OBJ}%;\
|
|
||||||
s%@SYSCONFDIR@%${SYSCONFDIR}%;\
|
s%@SYSCONFDIR@%${SYSCONFDIR}%;\
|
||||||
s%@BINDIR@%${BINDIR}%;\
|
s%@BINDIR@%${BINDIR}%;\
|
||||||
s%@SBINDIR@%${SBINDIR}%;\
|
s%@SBINDIR@%${SBINDIR}%;\
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
151
doc/chronyc.adoc
151
doc/chronyc.adoc
@@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// Copyright (C) Richard P. Curnow 1997-2003
|
// Copyright (C) Richard P. Curnow 1997-2003
|
||||||
// Copyright (C) Stephen Wadeley 2016
|
// Copyright (C) Stephen Wadeley 2016
|
||||||
// Copyright (C) Miroslav Lichvar 2009-2016
|
// Copyright (C) Miroslav Lichvar 2009-2017
|
||||||
//
|
//
|
||||||
// This program is free software; you can redistribute it and/or modify
|
// 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
|
// it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -78,11 +78,18 @@ 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
|
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.
|
DNS lookups. Long addresses will not be truncated to fit into the column.
|
||||||
|
|
||||||
|
*-N*::
|
||||||
|
This option enables printing of the original names of NTP sources that were
|
||||||
|
specified in the configuration file, or *chronyc* commands, and are internally
|
||||||
|
used by *chronyd*. Without the *-n* and *-N* option, the names of NTP sources
|
||||||
|
are obtained from reverse DNS lookups and can be different from the original
|
||||||
|
names.
|
||||||
|
|
||||||
*-c*::
|
*-c*::
|
||||||
This option enables printing of reports in a comma-separated values (CSV)
|
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
|
format. Reverse DNS lookups will be disabled, time will be printed as number of
|
||||||
number of seconds since the epoch and values in seconds will not be converted
|
seconds since the epoch, and values in seconds will not be converted to other
|
||||||
to other units.
|
units.
|
||||||
|
|
||||||
*-d*::
|
*-d*::
|
||||||
This option enables printing of debugging messages if *chronyc* was compiled
|
This option enables printing of debugging messages if *chronyc* was compiled
|
||||||
@@ -130,15 +137,15 @@ performance. An example of the output is shown below.
|
|||||||
----
|
----
|
||||||
Reference ID : CB00710F (foo.example.net)
|
Reference ID : CB00710F (foo.example.net)
|
||||||
Stratum : 3
|
Stratum : 3
|
||||||
Ref time (UTC) : Fri Feb 3 15:00:29 2012
|
Ref time (UTC) : Fri Jan 27 09:49:17 2017
|
||||||
System time : 0.000001501 seconds slow of NTP time
|
System time : 0.000006523 seconds slow of NTP time
|
||||||
Last offset : -0.000001632 seconds
|
Last offset : -0.000006747 seconds
|
||||||
RMS offset : 0.000002360 seconds
|
RMS offset : 0.000035822 seconds
|
||||||
Frequency : 331.898 ppm fast
|
Frequency : 3.225 ppm slow
|
||||||
Residual freq : 0.004 ppm
|
Residual freq : -0.000 ppm
|
||||||
Skew : 0.154 ppm
|
Skew : 0.129 ppm
|
||||||
Root delay : 0.373169 seconds
|
Root delay : 0.013639022 seconds
|
||||||
Root dispersion : 0.024780 seconds
|
Root dispersion : 0.001100737 seconds
|
||||||
Update interval : 64.2 seconds
|
Update interval : 64.2 seconds
|
||||||
Leap status : Normal
|
Leap status : Normal
|
||||||
----
|
----
|
||||||
@@ -187,9 +194,6 @@ The '`frequency`' is the rate by which the system's clock would be wrong if
|
|||||||
For example, a value of 1 ppm would mean that when the system's clock thinks it
|
For example, a value of 1 ppm would mean that when the system's clock thinks it
|
||||||
has advanced 1 second, it has actually advanced by 1.000001 seconds relative to
|
has advanced 1 second, it has actually advanced by 1.000001 seconds relative to
|
||||||
true time.
|
true time.
|
||||||
+
|
|
||||||
As you can see in the example, the clock in the computer is not a very
|
|
||||||
good one; it would gain about 30 seconds per day if it was not corrected!
|
|
||||||
*Residual freq*:::
|
*Residual freq*:::
|
||||||
This shows the '`residual frequency`' for the currently selected reference
|
This shows the '`residual frequency`' for the currently selected reference
|
||||||
source. This reflects any difference between what the measurements from the
|
source. This reflects any difference between what the measurements from the
|
||||||
@@ -219,7 +223,7 @@ An absolute bound on the computer's clock accuracy (assuming the stratum-1
|
|||||||
computer is correct) is given by:
|
computer is correct) is given by:
|
||||||
+
|
+
|
||||||
----
|
----
|
||||||
clock_error <= root_dispersion + (0.5 * |root_delay|)
|
clock_error <= |system_time_offset| + root_dispersion + (0.5 * root_delay)
|
||||||
----
|
----
|
||||||
*Update interval*:::
|
*Update interval*:::
|
||||||
This is the interval between the last two clock updates.
|
This is the interval between the last two clock updates.
|
||||||
@@ -287,15 +291,18 @@ milliseconds.
|
|||||||
|
|
||||||
=== Time sources
|
=== Time sources
|
||||||
|
|
||||||
[[sources]]*sources* [*-v*]::
|
[[sources]]*sources* [*-a*] [*-v*]::
|
||||||
This command displays information about the current time sources that *chronyd*
|
This command displays information about the current time sources that *chronyd*
|
||||||
is accessing.
|
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.
|
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
|
MS Name/IP address Stratum Poll Reach LastRx Last sample
|
||||||
===============================================================================
|
===============================================================================
|
||||||
#* GPS0 0 4 377 11 -479ns[ -621ns] +/- 134ns
|
#* GPS0 0 4 377 11 -479ns[ -621ns] +/- 134ns
|
||||||
@@ -341,8 +348,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
|
the source. A value of 377 indicates that a valid reply was received for all
|
||||||
from the last eight transmissions.
|
from the last eight transmissions.
|
||||||
*LastRx*:::
|
*LastRx*:::
|
||||||
This column shows how long ago the last sample was received from the source.
|
This column shows how long ago the last good sample (which is shown in the next
|
||||||
This is normally in seconds. The letters _m_, _h_, _d_ or _y_ indicate
|
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.
|
minutes, hours, days, or years.
|
||||||
*Last sample*:::
|
*Last sample*:::
|
||||||
This column shows the offset between the local clock and the source at the
|
This column shows the offset between the local clock and the source at the
|
||||||
@@ -355,18 +363,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 measurement. Positive offsets indicate that the local clock is ahead of
|
||||||
the source.
|
the source.
|
||||||
|
|
||||||
[[sourcestats]]*sourcestats* [*-v*]::
|
[[sourcestats]]*sourcestats* [*-a*] [*-v*]::
|
||||||
The *sourcestats* command displays information about the drift rate and offset
|
The *sourcestats* command displays information about the drift rate and offset
|
||||||
estimation process for each of the sources currently being examined by
|
estimation process for each of the sources currently being examined by
|
||||||
*chronyd*.
|
*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.
|
extra caption lines are shown as a reminder of the meanings of the columns.
|
||||||
+
|
+
|
||||||
An example report is:
|
An example report is:
|
||||||
+
|
+
|
||||||
----
|
----
|
||||||
210 Number of sources = 1
|
|
||||||
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
|
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
|
||||||
===============================================================================
|
===============================================================================
|
||||||
foo.example.net 11 5 46m -0.001 0.045 1us 25us
|
foo.example.net 11 5 46m -0.001 0.045 1us 25us
|
||||||
@@ -444,8 +455,9 @@ not visible in the *sources* and *sourcestats* reports.
|
|||||||
|
|
||||||
[[ntpdata]]*ntpdata* [_address_]::
|
[[ntpdata]]*ntpdata* [_address_]::
|
||||||
The *ntpdata* command displays the last valid measurement and other
|
The *ntpdata* command displays the last valid measurement and other
|
||||||
NTP-specific information about the specified NTP source, or all NTP sources if
|
NTP-specific information about the specified NTP source, or all NTP sources
|
||||||
no address was specified. An example of the output is shown below.
|
(with a known address) if no address was specified. An example of the output is
|
||||||
|
shown below.
|
||||||
+
|
+
|
||||||
----
|
----
|
||||||
Remote address : 203.0.113.15 (CB00710F)
|
Remote address : 203.0.113.15 (CB00710F)
|
||||||
@@ -528,15 +540,13 @@ The number of all packets received from the source.
|
|||||||
*Total valid RX*:::
|
*Total valid RX*:::
|
||||||
The number of valid packets received from the source.
|
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
|
The *add peer* command allows a new NTP peer to be added whilst
|
||||||
*chronyd* is running.
|
*chronyd* is running.
|
||||||
+
|
+
|
||||||
Following the words *add peer*, the syntax of the following
|
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.
|
<<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.
|
An example of using this command is shown below.
|
||||||
+
|
+
|
||||||
@@ -544,15 +554,27 @@ An example of using this command is shown below.
|
|||||||
add peer foo.example.net minpoll 6 maxpoll 10 key 25
|
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
|
The *add server* command allows a new NTP server to be added whilst
|
||||||
*chronyd* is running.
|
*chronyd* is running.
|
||||||
+
|
+
|
||||||
Following the words *add server*, the syntax of the following parameters and
|
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.
|
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:
|
An example of using this command is shown below:
|
||||||
+
|
+
|
||||||
@@ -690,7 +712,8 @@ the loaded periods. The *offline* and *online* commands can be used to achieve
|
|||||||
this.
|
this.
|
||||||
+
|
+
|
||||||
There are four forms of the *offline* command. The first form is a wildcard,
|
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
|
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.
|
uses an IP address or a hostname. These forms are illustrated below.
|
||||||
+
|
+
|
||||||
@@ -725,6 +748,14 @@ particular source or sources has been restored.
|
|||||||
+
|
+
|
||||||
The syntax is identical to that of the <<offline,*offline*>> command.
|
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_::
|
[[polltarget]]*polltarget* _address_ _polltarget_::
|
||||||
The *polltarget* command is used to modify the poll target for one of the
|
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
|
current set of sources. It is equivalent to the *polltarget* option in the
|
||||||
@@ -739,6 +770,15 @@ Sources that stop responding will be replaced with newly resolved addresses
|
|||||||
automatically after 8 polling intervals, but this command can still be useful
|
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.
|
to replace them immediately and not wait until they are marked as unreachable.
|
||||||
|
|
||||||
|
[[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 time input
|
||||||
|
|
||||||
[[manual]]
|
[[manual]]
|
||||||
@@ -1121,17 +1161,26 @@ purged. An example of how to do this is shown below.
|
|||||||
|
|
||||||
[[dump]]*dump*::
|
[[dump]]*dump*::
|
||||||
The *dump* command causes *chronyd* to write its current history of
|
The *dump* command causes *chronyd* to write its current history of
|
||||||
measurements for each of its sources to dump files, either for inspection or to
|
measurements for each of its sources to dump files in the directory specified
|
||||||
support the *-r* option when *chronyd* is restarted.
|
in the configuration file by the <<chrony.conf.adoc#dumpdir,*dumpdir*>>
|
||||||
+
|
directive. Note that *chronyd* does this automatically when it exits. This
|
||||||
The *dump* command is somewhat equivalent to the
|
command is mainly useful for inspection of the history whilst *chronyd* is
|
||||||
<<chrony.conf.adoc#dumponexit,*dumponexit*>> directive in the configuration
|
running.
|
||||||
file.
|
|
||||||
+
|
[[rekey]]*rekey*::
|
||||||
To use the *dump* command, you might want to configure the name of the
|
The *rekey* command causes *chronyd* to re-read the key file specified in the
|
||||||
directory into which the dump files will be written. This can only be
|
configuration file by the <<chrony.conf.adoc#keyfile,*keyfile*>> directive.
|
||||||
done in the configuration file with the <<chrony.conf.adoc#dumpdir,*dumpdir*>>
|
|
||||||
directive.
|
[[reset]]*reset*::
|
||||||
|
The *reset* 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).
|
||||||
|
|
||||||
|
[[shutdown]]*shutdown*::
|
||||||
|
The *shutdown* command causes *chronyd* to exit. This is equivalent to sending
|
||||||
|
the process the SIGTERM signal.
|
||||||
|
|
||||||
=== Client commands
|
=== Client commands
|
||||||
|
|
||||||
@@ -1181,10 +1230,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
|
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*
|
(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
|
or *peer* directives in the configuration file. The second argument is the name
|
||||||
function (by default SHA1 or MD5 if SHA1 is not available) and the third
|
of the hash function or cipher (by default SHA1, or MD5 if SHA1 is not
|
||||||
argument is the number of bits the key should have, between 80 and 4096 bits
|
available). The third argument is the length of the key in bits if a hash
|
||||||
(by default 160 bits).
|
function was selected, between 80 and 4096 bits (by default 160 bits).
|
||||||
+
|
+
|
||||||
An example is:
|
An example is:
|
||||||
+
|
+
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// This file is part of chrony
|
// This file is part of chrony
|
||||||
//
|
//
|
||||||
// Copyright (C) Richard P. Curnow 1997-2003
|
// Copyright (C) Richard P. Curnow 1997-2003
|
||||||
// Copyright (C) Miroslav Lichvar 2009-2016
|
// Copyright (C) Miroslav Lichvar 2009-2017
|
||||||
//
|
//
|
||||||
// This program is free software; you can redistribute it and/or modify
|
// 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
|
// it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -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
|
will read them from a configuration file. The compiled-in default location of
|
||||||
the file is _@SYSCONFDIR@/chrony.conf_.
|
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
|
== OPTIONS
|
||||||
|
|
||||||
@@ -55,35 +55,46 @@ IPv6 sockets will be created.
|
|||||||
|
|
||||||
*-f* _file_::
|
*-f* _file_::
|
||||||
This option can be used to specify an alternate location for the configuration
|
This option can be used to specify an alternate location for the configuration
|
||||||
file (default _@SYSCONFDIR@/chrony.conf_).
|
file. The default value is _@SYSCONFDIR@/chrony.conf_.
|
||||||
|
|
||||||
*-n*::
|
*-n*::
|
||||||
When run in this mode, the program will not detach itself from the terminal.
|
When run in this mode, the program will not detach itself from the terminal.
|
||||||
|
|
||||||
*-d*::
|
*-d*::
|
||||||
When run in this mode, the program will not detach itself from the terminal,
|
When run in this mode, the program will not detach itself from the terminal,
|
||||||
and all messages will be sent to the terminal instead of to syslog. When
|
and all messages will be written to the terminal instead of syslog. If
|
||||||
*chronyd* was compiled with debugging support, this option can be used twice to
|
*chronyd* was compiled with enabled support for debugging, this option can be
|
||||||
print also debugging messages.
|
used twice to enable debug messages.
|
||||||
|
|
||||||
|
*-l* _file_::
|
||||||
|
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.
|
||||||
|
|
||||||
*-q*::
|
*-q*::
|
||||||
When run in this mode, *chronyd* will set the system clock once and exit. It
|
When run in this mode, *chronyd* will set the system clock once and exit. It
|
||||||
will not detach from the terminal.
|
will not detach from the terminal.
|
||||||
|
|
||||||
*-Q*::
|
*-Q*::
|
||||||
This option is similar to *-q*, but it will only print the offset without any
|
This option is similar to the *-q* option, except it only prints the offset
|
||||||
corrections of the clock.
|
without making any corrections of the clock and it allows *chronyd* to be
|
||||||
|
started without root privileges.
|
||||||
|
|
||||||
*-r*::
|
*-r*::
|
||||||
This option will try to reload and then delete files containing sample
|
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 for each of the servers and reference clocks being used. The
|
||||||
histories are created by using the <<chronyc.adoc#dump,*dump*>> command in
|
files are expected to be in the directory specified by the
|
||||||
*chronyc*, or by setting the <<chrony.conf.adoc#dumponexit,*dumponexit*>>
|
<<chrony.conf.adoc#dumpdir,*dumpdir*>>
|
||||||
directive in the configuration file. This option is useful if you want to stop
|
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.
|
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
|
However, it should be used only on systems where the kernel can maintain clock
|
||||||
compensation whilst not under *chronyd*'s control (i.e. Linux, FreeBSD, NetBSD
|
compensation whilst not under *chronyd*'s control (i.e. Linux, FreeBSD, NetBSD,
|
||||||
and Solaris).
|
Solaris, and macOS 10.13 or later).
|
||||||
|
|
||||||
*-R*::
|
*-R*::
|
||||||
When this option is used, the <<chrony.conf.adoc#initstepslew,*initstepslew*>>
|
When this option is used, the <<chrony.conf.adoc#initstepslew,*initstepslew*>>
|
||||||
@@ -120,7 +131,8 @@ running, but still allow it to adjust the frequency of the system clock.
|
|||||||
*-u* _user_::
|
*-u* _user_::
|
||||||
This option sets the name of the system user to which *chronyd* will switch
|
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
|
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 default value is
|
||||||
|
_@DEFAULT_USER@_.
|
||||||
+
|
+
|
||||||
On Linux, *chronyd* needs to be compiled with support for the *libcap* library.
|
On Linux, *chronyd* needs to be compiled with support for the *libcap* library.
|
||||||
On macOS, FreeBSD, NetBSD and Solaris *chronyd* forks into two processes.
|
On macOS, FreeBSD, NetBSD and Solaris *chronyd* forks into two processes.
|
||||||
@@ -130,8 +142,9 @@ range of privileged system calls on behalf of the parent.
|
|||||||
*-F* _level_::
|
*-F* _level_::
|
||||||
This option configures a system call filter when *chronyd* is compiled with
|
This option configures a system call filter when *chronyd* is compiled with
|
||||||
support for the Linux secure computing (seccomp) facility. In level 1 the
|
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
|
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 (default 0).
|
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's recommended to enable the filter only when it's known to work on the
|
||||||
version of the system where *chrony* is installed as the filter needs to allow
|
version of the system where *chrony* is installed as the filter needs to allow
|
||||||
@@ -143,14 +156,23 @@ killed even in normal operation.
|
|||||||
*-P* _priority_::
|
*-P* _priority_::
|
||||||
On Linux, this option will select the SCHED_FIFO real-time scheduler at the
|
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
|
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
|
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*::
|
*-m*::
|
||||||
This option will lock *chronyd* into RAM so that it will never be paged out.
|
This option will lock *chronyd* into RAM so that it will never be paged out.
|
||||||
This mode is only supported on Linux.
|
This mode is only supported on Linux.
|
||||||
|
|
||||||
|
*-x*::
|
||||||
|
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 run without the capability to adjust or set the
|
||||||
|
system clock (e.g. in some containers) in order to operate as an NTP server. It
|
||||||
|
is not recommended to run *chronyd* (with or without *-x*) when another process
|
||||||
|
is controlling the system clock.
|
||||||
|
|
||||||
*-v*::
|
*-v*::
|
||||||
With this option *chronyd* will print version number to the terminal and exit.
|
With this option *chronyd* will print version number to the terminal and exit.
|
||||||
|
|
||||||
|
|||||||
210
doc/faq.adoc
210
doc/faq.adoc
@@ -50,7 +50,7 @@ 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
|
reliability the client should have at least three servers. The `iburst` option
|
||||||
speeds up the initial synchronisation.
|
speeds up the initial synchronisation.
|
||||||
|
|
||||||
To stabilize the initial synchronisation on the next start, the estimated drift
|
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.
|
of the system clock is saved to a file specified by the `driftfile` directive.
|
||||||
|
|
||||||
If the system clock can be far from the true time after boot for any reason,
|
If the system clock can be far from the true time after boot for any reason,
|
||||||
@@ -59,7 +59,7 @@ slewing, which would take a very long time. The `makestep` directive does
|
|||||||
that.
|
that.
|
||||||
|
|
||||||
In order to keep the real-time clock (RTC) close to the true time, so the
|
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 initialized on the
|
system time is reasonably close to the true time when it's initialised on the
|
||||||
next boot from the RTC, the `rtcsync` directive enables a mode in which 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
|
system time is periodically copied to the RTC. It is supported on Linux and
|
||||||
macOS.
|
macOS.
|
||||||
@@ -79,7 +79,7 @@ rtcsync
|
|||||||
|
|
||||||
You need to add an `allow` directive to the _chrony.conf_ file in order to open
|
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
|
the NTP port and allow `chronyd` to reply to client requests. `allow` with no
|
||||||
specified subnet allows all IPv4 and IPv6 addresses.
|
specified subnet allows access from all IPv4 and IPv6 addresses.
|
||||||
|
|
||||||
=== I have several computers on a LAN. Should be all clients of an external server?
|
=== I have several computers on a LAN. Should be all clients of an external server?
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ _chrony.conf_ file. This configuration will be better because
|
|||||||
No. Starting from version 1.25, `chronyd` will keep trying to resolve
|
No. Starting from version 1.25, `chronyd` will keep trying to resolve
|
||||||
the names specified by the `server`, `pool`, and `peer` directives in an
|
the names specified by the `server`, `pool`, and `peer` directives in an
|
||||||
increasing interval until it succeeds. The `online` command can be issued from
|
increasing interval until it succeeds. The `online` command can be issued from
|
||||||
`chronyc` to try to resolve them immediately.
|
`chronyc` to force `chronyd` to try to resolve the names immediately.
|
||||||
|
|
||||||
=== How can I make `chronyd` more secure?
|
=== How can I make `chronyd` more secure?
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ 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
|
and how should be the actual interval adjusted in the specified range. Their
|
||||||
default values are 6 (64 seconds) for `minpoll`, 10 (1024 seconds) for
|
default values are 6 (64 seconds) for `minpoll`, 10 (1024 seconds) for
|
||||||
`maxpoll` and 8 (samples) for `polltarget`. The default values should be used
|
`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 have
|
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
|
permission to poll some servers more frequently, setting these options for
|
||||||
shorter polling intervals may significantly improve the accuracy of the system
|
shorter polling intervals may significantly improve the accuracy of the system
|
||||||
clock.
|
clock.
|
||||||
@@ -171,6 +171,11 @@ 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 sensitivity of the crystal oscillator and the maximum rate of the
|
||||||
temperature change).
|
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 may work better.
|
||||||
|
|
||||||
An example of the directive for an NTP server on the Internet that you are
|
An example of the directive for an NTP server on the Internet that you are
|
||||||
allowed to poll frequently could be
|
allowed to poll frequently could be
|
||||||
|
|
||||||
@@ -178,15 +183,15 @@ allowed to poll frequently could be
|
|||||||
server foo.example.net minpoll 4 maxpoll 6 polltarget 16
|
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
|
LAN could be
|
||||||
|
|
||||||
----
|
----
|
||||||
server ntp.local minpoll 2 maxpoll 4 polltarget 30
|
server ntp.local minpoll 2 maxpoll 4 polltarget 30
|
||||||
----
|
----
|
||||||
|
|
||||||
The maxdelay options are useful to ignore measurements with larger delay (e.g.
|
The maxdelay options are useful to ignore measurements with an unusally large
|
||||||
due to congestion in the network) and improve the stability of the
|
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
|
synchronisation. The `maxdelaydevratio` option could be added to the example
|
||||||
with local NTP server
|
with local NTP server
|
||||||
|
|
||||||
@@ -194,16 +199,90 @@ with local NTP server
|
|||||||
server ntp.local minpoll 2 maxpoll 4 polltarget 30 maxdelaydevratio 2
|
server ntp.local minpoll 2 maxpoll 4 polltarget 30 maxdelaydevratio 2
|
||||||
----
|
----
|
||||||
|
|
||||||
If your server supports the interleaved mode, the `xleave` option should be
|
If your server supports the interleaved mode (e.g. it is running `chronyd`),
|
||||||
added to the `server` directive in order to receive server's more accurate
|
the `xleave` option should be added to the `server` directive in order to allow
|
||||||
hardware or kernel transmit timestamps. When combined with local hardware
|
the server to send the client more accurate transmit timestamps (kernel or
|
||||||
timestamping, a sub-microsecond accuracy may be possible. An example could be
|
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 may be possible. For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
server ntp.local minpoll 0 maxpoll 0 xleave
|
||||||
hwtimestamp eth0
|
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.
|
||||||
|
|
||||||
|
If it is acceptable for NTP clients in the network to send requests at an
|
||||||
|
excessive rate, a sub-second polling interval may 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
|
||||||
|
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, and a timeout can be specified with the `-t` option.
|
||||||
|
The following command would take only up to about 1 second.
|
||||||
|
|
||||||
|
----
|
||||||
|
# chronyd -q -t 1 'server pool.ntp.org iburst maxsamples 1'
|
||||||
|
----
|
||||||
|
|
||||||
|
=== 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`.
|
||||||
|
|
||||||
|
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` may cause the offsets in the `tracking` and
|
||||||
|
`sourcestats` reports/logs to be significantly smaller than the actual offsets
|
||||||
|
and be unsuitable for monitoring.
|
||||||
|
|
||||||
=== What happened to the `commandkey` and `generatecommandkey` directives?
|
=== What happened to the `commandkey` and `generatecommandkey` directives?
|
||||||
|
|
||||||
They were removed in version 2.2. Authentication is no longer supported in the
|
They were removed in version 2.2. Authentication is no longer supported in the
|
||||||
@@ -242,8 +321,17 @@ MS Name/IP address Stratum Poll Reach LastRx Last sample
|
|||||||
=== Are NTP servers specified with the `offline` option?
|
=== Are NTP servers specified with the `offline` option?
|
||||||
|
|
||||||
Check that you're using ``chronyc``'s `online` and `offline` commands
|
Check that you're using ``chronyc``'s `online` and `offline` commands
|
||||||
appropriately. Again, check in _measurements.log_ to see if you're getting any
|
appropriately. The `activity` command prints the number of sources that are
|
||||||
data back from the server.
|
currently online and offline. For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
200 OK
|
||||||
|
3 sources online
|
||||||
|
0 sources offline
|
||||||
|
0 sources doing burst (return to online)
|
||||||
|
0 sources doing burst (return to offline)
|
||||||
|
0 sources with unknown address
|
||||||
|
----
|
||||||
|
|
||||||
=== Is `chronyd` allowed to step the system clock?
|
=== Is `chronyd` allowed to step the system clock?
|
||||||
|
|
||||||
@@ -271,6 +359,49 @@ to
|
|||||||
makestep 1 -1
|
makestep 1 -1
|
||||||
----
|
----
|
||||||
|
|
||||||
|
=== 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 may 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
|
||||||
|
----
|
||||||
|
|
||||||
|
=== 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`
|
== Issues with `chronyc`
|
||||||
|
|
||||||
=== I keep getting the error `506 Cannot talk to daemon`
|
=== I keep getting the error `506 Cannot talk to daemon`
|
||||||
@@ -326,14 +457,14 @@ Only by the source code. See _cmdmon.c_ (`chronyd` side) and _client.c_
|
|||||||
=== What is the real-time clock (RTC)?
|
=== What is the real-time clock (RTC)?
|
||||||
|
|
||||||
This is the clock which keeps the time even when your computer is turned off.
|
This is the clock which keeps the time even when your computer is turned off.
|
||||||
It is used to initialize the system clock on boot. It normally doesn't drift
|
It is used to initialise the system clock on boot. It normally doesn't drift
|
||||||
more than few seconds per day.
|
more than few seconds per day.
|
||||||
|
|
||||||
There are two approaches how `chronyd` can work with it. One is to use the
|
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
|
`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 won't touch
|
||||||
the RTC. If the computer is not turned off for a long time, the RTC should
|
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 initialized from
|
still be close to the true time when the system clock will be initialised from
|
||||||
it on the next boot.
|
it on the next boot.
|
||||||
|
|
||||||
The other option is to use the `rtcfile` directive, which tells `chronyd` to
|
The other option is to use the `rtcfile` directive, which tells `chronyd` to
|
||||||
@@ -372,16 +503,31 @@ things
|
|||||||
|
|
||||||
Some other program running on the system may be using the device.
|
Some other program running on the system may be using the device.
|
||||||
|
|
||||||
|
=== 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
|
== 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
|
No, the broadcast/multicast client mode is not supported and there is currently
|
||||||
to implement it. The broadcast and multicast modes are inherently less
|
no plan to implement it. While the mode may be useful to simplify configuration
|
||||||
accurate and less secure (even with authentication) than the ordinary
|
of clients in large networks, it is inherently less accurate and less secure
|
||||||
server/client mode and they are not as useful as they used to be. Even with
|
(even with authentication) than the ordinary client/server mode.
|
||||||
very modest hardware a single NTP server can serve time to hundreds of
|
|
||||||
thousands of clients using the ordinary 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?
|
=== Can `chronyd` transmit broadcast NTP packets?
|
||||||
|
|
||||||
@@ -396,15 +542,15 @@ option for all time sources in the _chrony.conf_ file.
|
|||||||
|
|
||||||
=== What happens if the network connection is dropped without using ``chronyc``'s `offline` command first?
|
=== What happens if the network connection is dropped without using ``chronyc``'s `offline` command first?
|
||||||
|
|
||||||
`chronyd` will keep trying to access the server(s) that it thinks are online.
|
`chronyd` will keep trying to access the sources that it thinks are online, and
|
||||||
When the network is connected again, it will take some time (on average half of
|
it will take longer before new measurements are actually made and the clock is
|
||||||
the maximum polling interval) before new measurements are made and the clock is
|
corrected when the network is connected again. If the sources were set to
|
||||||
corrected. If the servers were set to offline and the `online` command was
|
offline, `chronyd` would make new measurements immediately after issuing the
|
||||||
issued when the network was connected, `chronyd` would make new measurements
|
`online` command.
|
||||||
immediately.
|
|
||||||
|
|
||||||
The `auto_offline` option to the `server` entry in the _chrony.conf_ file may
|
Unless the network connection lasts only few minutes (less than the maximum
|
||||||
be useful to switch the servers to the offline state automatically.
|
polling interval), the delay is usually not a problem, and it may be acceptable
|
||||||
|
to keep all sources online all the time.
|
||||||
|
|
||||||
== Operating systems
|
== Operating systems
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
code is supplied in the form of a gzipped tar file, which unpacks to a
|
||||||
subdirectory identifying the name and version of the program.
|
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
|
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
|
This is a shell script that automatically determines the system type. There is
|
||||||
a single optional parameter, `--prefix` which indicates the directory tree
|
an optional parameter `--prefix`, which indicates the directory tree where the
|
||||||
where the software should be installed. For example,
|
software should be installed. For example,
|
||||||
|
|
||||||
----
|
----
|
||||||
./configure --prefix=/opt/free
|
./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
|
control program into `/opt/free/bin`. The default value for the prefix is
|
||||||
`/usr/local`.
|
`/usr/local`.
|
||||||
|
|
||||||
The configure script assumes you want to use gcc as your compiler. If you want
|
The `configure` script assumes you want to use `gcc` as your compiler. If you
|
||||||
to use a different compiler, you can configure this way:
|
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
|
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`
|
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
|
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
|
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 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
|
||||||
|
http://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 or readline library are available,
|
If development files for the editline or readline library are available,
|
||||||
`chronyc` will be built with line editing support. If you don't want this,
|
`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
|
If a `timepps.h` header is available (e.g. from the
|
||||||
http://linuxpps.org[LinuxPPS project]), `chronyd` will be built with PPS API
|
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
|
normally searched by the compiler, you can add it to the searched locations by
|
||||||
setting the `CPPFLAGS` variable to `-I/path/to/timepps`.
|
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
|
Now type
|
||||||
|
|
||||||
----
|
----
|
||||||
@@ -122,6 +160,16 @@ 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
|
option or the `user` directive in the configuration file, or set the default
|
||||||
user with the `--with-user` configure option before building.
|
user with the `--with-user` configure option before building.
|
||||||
|
|
||||||
|
== Support for system call filtering
|
||||||
|
|
||||||
|
`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.
|
||||||
|
|
||||||
== Support for line editing libraries
|
== Support for line editing libraries
|
||||||
|
|
||||||
`chronyc` can be built with support for line editing, this allows you to use
|
`chronyc` can be built with support for line editing, this allows you to use
|
||||||
@@ -132,12 +180,12 @@ 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
|
incompatible with chrony's license GPLv2. You should use editline instead if
|
||||||
you don't want to use older readline versions.
|
you don't want to use older readline versions.
|
||||||
|
|
||||||
The configure script will automatically enable the line editing support if one
|
The `configure` script will automatically enable the line editing support if
|
||||||
of the supported libraries is available. If they are both available, the
|
one of the supported libraries is available. If they are both available, the
|
||||||
editline library will be used.
|
editline library will be used.
|
||||||
|
|
||||||
If you don't want to use it (in which case chronyc will use a minimal command
|
If you don't want to use it (in which case `chronyc` will use a minimal command
|
||||||
line interface), invoke configure like this:
|
line interface), invoke `configure` like this:
|
||||||
|
|
||||||
----
|
----
|
||||||
./configure --disable-readline other-options...
|
./configure --disable-readline other-options...
|
||||||
@@ -161,12 +209,12 @@ normally searched by the compiler and linker, you need to use extra options:
|
|||||||
|
|
||||||
== Extra options for package builders
|
== Extra options for package builders
|
||||||
|
|
||||||
The configure and make procedures have some extra options that may be useful if
|
The `configure` and `make` procedures have some extra options that may be
|
||||||
you are building a distribution package for chrony.
|
useful if you are building a distribution package for `chrony`.
|
||||||
|
|
||||||
The `--mandir=DIR` option to configure specifies an install directory for the
|
The `--mandir=DIR` option to `configure` specifies an installation directory
|
||||||
man pages. This overrides the `man` subdirectory of the argument to the
|
for the man pages. This overrides the `man` subdirectory of the argument to the
|
||||||
--prefix option.
|
`--prefix` option.
|
||||||
|
|
||||||
----
|
----
|
||||||
./configure --prefix=/usr --mandir=/usr/share/man
|
./configure --prefix=/usr --mandir=/usr/share/man
|
||||||
@@ -174,8 +222,8 @@ man pages. This overrides the `man` subdirectory of the argument to the
|
|||||||
|
|
||||||
to set both options together.
|
to set both options together.
|
||||||
|
|
||||||
The final option is the `DESTDIR` option to the make command. For example, you
|
The final option is the `DESTDIR` option to the `make` command. For example,
|
||||||
could use the commands
|
you could use the commands
|
||||||
|
|
||||||
----
|
----
|
||||||
./configure --prefix=/usr --mandir=/usr/share/man
|
./configure --prefix=/usr --mandir=/usr/share/man
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Wait for chrony to synchronize system clock
|
Description=Wait for chrony to synchronize system clock
|
||||||
|
Documentation=man:chronyc(1)
|
||||||
After=chronyd.service
|
After=chronyd.service
|
||||||
Requires=chronyd.service
|
Requires=chronyd.service
|
||||||
Before=time-sync.target
|
Before=time-sync.target
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ pool pool.ntp.org iburst
|
|||||||
# Record the rate at which the system clock gains/losses time.
|
# Record the rate at which the system clock gains/losses time.
|
||||||
driftfile /var/lib/chrony/drift
|
driftfile /var/lib/chrony/drift
|
||||||
|
|
||||||
# In first three updates step the system clock instead of slew
|
# Allow the system clock to be stepped in the first three updates
|
||||||
# if the adjustment is larger than 1 second.
|
# if its offset is larger than 1 second.
|
||||||
makestep 1.0 3
|
makestep 1.0 3
|
||||||
|
|
||||||
# Enable kernel synchronization of the real-time clock (RTC).
|
# Enable kernel synchronization of the real-time clock (RTC).
|
||||||
|
|||||||
@@ -5,22 +5,32 @@ pool pool.ntp.org iburst
|
|||||||
# Record the rate at which the system clock gains/losses time.
|
# Record the rate at which the system clock gains/losses time.
|
||||||
driftfile /var/lib/chrony/drift
|
driftfile /var/lib/chrony/drift
|
||||||
|
|
||||||
# In first three updates step the system clock instead of slew
|
# Allow the system clock to be stepped in the first three updates
|
||||||
# if the adjustment is larger than 1 second.
|
# if its offset is larger than 1 second.
|
||||||
makestep 1.0 3
|
makestep 1.0 3
|
||||||
|
|
||||||
# Enable kernel synchronization of the real-time clock (RTC).
|
# Enable kernel synchronization of the real-time clock (RTC).
|
||||||
rtcsync
|
rtcsync
|
||||||
|
|
||||||
# Allow NTP client access from local network.
|
# Enable hardware timestamping on all interfaces that support it.
|
||||||
#allow 192.168/16
|
#hwtimestamp *
|
||||||
|
|
||||||
# Serve time even if not synchronized to any NTP server.
|
# Increase the minimum number of selectable sources required to adjust
|
||||||
|
# the system clock.
|
||||||
|
#minsources 2
|
||||||
|
|
||||||
|
# Allow NTP client access from local network.
|
||||||
|
#allow 192.168.0.0/16
|
||||||
|
|
||||||
|
# Serve time even if not synchronized to a time source.
|
||||||
#local stratum 10
|
#local stratum 10
|
||||||
|
|
||||||
# Specify file containing keys for NTP authentication.
|
# Specify file containing keys for NTP authentication.
|
||||||
#keyfile /etc/chrony.keys
|
#keyfile /etc/chrony.keys
|
||||||
|
|
||||||
|
# Get TAI-UTC offset and leap seconds from the system tz database.
|
||||||
|
#leapsectz right/UTC
|
||||||
|
|
||||||
# Specify directory for log files.
|
# Specify directory for log files.
|
||||||
logdir /var/log/chrony
|
logdir /var/log/chrony
|
||||||
|
|
||||||
|
|||||||
@@ -33,42 +33,30 @@
|
|||||||
|
|
||||||
! pool pool.ntp.org iburst
|
! pool pool.ntp.org iburst
|
||||||
|
|
||||||
# However, for dial-up use you probably want these instead. The word
|
|
||||||
# 'offline' means that the server is not visible at boot time. Use
|
|
||||||
# chronyc's 'online' command to tell chronyd that these servers have
|
|
||||||
# become visible after you go on-line.
|
|
||||||
|
|
||||||
! server foo.example.net offline
|
|
||||||
! server bar.example.net offline
|
|
||||||
! server baz.example.net offline
|
|
||||||
|
|
||||||
! pool pool.ntp.org offline
|
|
||||||
|
|
||||||
# You may want to specify NTP 'peers' instead. If you run a network
|
|
||||||
# with a lot of computers and want several computers running chrony to
|
|
||||||
# have the 'front-line' interface to the public NTP servers, you can
|
|
||||||
# 'peer' these machines together to increase robustness.
|
|
||||||
|
|
||||||
! peer foo.example.net
|
|
||||||
|
|
||||||
# There are other options to the 'server' and 'peer' directives that you
|
|
||||||
# might want to use. For example, you can ignore measurements whose
|
|
||||||
# round-trip-time is too large (indicating that the measurement is
|
|
||||||
# probably useless, because you don't know which way the measurement
|
|
||||||
# message got held up.) Consult the full documentation for details.
|
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### AVOIDING POTENTIALLY BOGUS CHANGES TO YOUR CLOCK
|
### AVOIDING POTENTIALLY BOGUS CHANGES TO YOUR CLOCK
|
||||||
#
|
#
|
||||||
# To avoid changes being made to your computer's gain/loss compensation
|
# To avoid changes being made to your computer's gain/loss compensation
|
||||||
# when the measurement history is too erratic, you might want to enable
|
# when the measurement history is too erratic, you might want to enable
|
||||||
# one of the following lines. The first seems good for dial-up (or
|
# one of the following lines. The first seems good with servers on the
|
||||||
# other high-latency connections like slow leased lines), the second
|
# Internet, the second seems OK for a LAN environment.
|
||||||
# seems OK for a LAN environment.
|
|
||||||
|
|
||||||
! maxupdateskew 100
|
! maxupdateskew 100
|
||||||
! maxupdateskew 5
|
! maxupdateskew 5
|
||||||
|
|
||||||
|
# If you want to increase the minimum number of selectable sources
|
||||||
|
# required to update the system clock in order to make the
|
||||||
|
# synchronisation more reliable, uncomment (and edit) the following
|
||||||
|
# line.
|
||||||
|
|
||||||
|
! minsources 2
|
||||||
|
|
||||||
|
# If your computer has a good stable clock (e.g. it is not a virtual
|
||||||
|
# machine), you might also want to reduce the maximum assumed drift
|
||||||
|
# (frequency error) of the clock (the value is specified in ppm).
|
||||||
|
|
||||||
|
! maxdrift 100
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### FILENAMES ETC
|
### FILENAMES ETC
|
||||||
# Chrony likes to keep information about your computer's clock in files.
|
# Chrony likes to keep information about your computer's clock in files.
|
||||||
@@ -107,7 +95,13 @@ driftfile /var/lib/chrony/drift
|
|||||||
# still running and bail out. If you want to change the path to the PID
|
# 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.
|
# 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
|
||||||
|
# TAI-UTC offset and when will the next leap second occur.
|
||||||
|
|
||||||
|
! leapsectz right/UTC
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### INITIAL CLOCK CORRECTION
|
### INITIAL CLOCK CORRECTION
|
||||||
@@ -181,13 +175,12 @@ driftfile /var/lib/chrony/drift
|
|||||||
# machine accesses it. The information can be accessed by the 'clients'
|
# machine accesses it. The information can be accessed by the 'clients'
|
||||||
# command of chronyc. You can disable this facility by uncommenting the
|
# command of chronyc. You can disable this facility by uncommenting the
|
||||||
# following line. This will save a bit of memory if you have many
|
# following line. This will save a bit of memory if you have many
|
||||||
# clients.
|
# clients and it will also disable support for the interleaved mode.
|
||||||
|
|
||||||
! noclientlog
|
! noclientlog
|
||||||
|
|
||||||
# The clientlog size is limited to 512KB by default. If you have many
|
# The clientlog size is limited to 512KB by default. If you have many
|
||||||
# clients, especially in many different subnets, you might want to
|
# clients, you might want to increase the limit.
|
||||||
# increase the limit.
|
|
||||||
|
|
||||||
! clientloglimit 4194304
|
! clientloglimit 4194304
|
||||||
|
|
||||||
@@ -196,7 +189,7 @@ driftfile /var/lib/chrony/drift
|
|||||||
# clients that are sending requests too frequently, uncomment and edit
|
# clients that are sending requests too frequently, uncomment and edit
|
||||||
# the following line.
|
# the following line.
|
||||||
|
|
||||||
! limitrate interval 3 burst 8
|
! ratelimit interval 3 burst 8
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### REPORTING BIG CLOCK CHANGES
|
### REPORTING BIG CLOCK CHANGES
|
||||||
@@ -243,7 +236,17 @@ driftfile /var/lib/chrony/drift
|
|||||||
# Rate limiting can be enabled also for command packets. (Note,
|
# Rate limiting can be enabled also for command packets. (Note,
|
||||||
# commands from localhost are never limited.)
|
# commands from localhost are never limited.)
|
||||||
|
|
||||||
! cmdratelimit interval 1 burst 16
|
! cmdratelimit interval -4 burst 16
|
||||||
|
|
||||||
|
#######################################################################
|
||||||
|
### HARDWARE TIMESTAMPING
|
||||||
|
# On Linux, if the network interface controller and its driver support
|
||||||
|
# hardware timestamping, it can significantly improve the accuracy of
|
||||||
|
# synchronisation. It can be enabled on specified interfaces only, or it
|
||||||
|
# can be enabled on all interfaces that support it.
|
||||||
|
|
||||||
|
! hwtimestamp eth0
|
||||||
|
! hwtimestamp *
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### REAL TIME CLOCK
|
### REAL TIME CLOCK
|
||||||
@@ -274,6 +277,12 @@ driftfile /var/lib/chrony/drift
|
|||||||
|
|
||||||
! rtcdevice /dev/misc/rtc
|
! rtcdevice /dev/misc/rtc
|
||||||
|
|
||||||
|
# Alternatively, if not using the -s option, this directive can be used
|
||||||
|
# to enable a mode in which the RTC is periodically set to the system
|
||||||
|
# time, with no tracking of its drift.
|
||||||
|
|
||||||
|
! rtcsync
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
### REAL TIME SCHEDULER
|
### REAL TIME SCHEDULER
|
||||||
# This directive tells chronyd to use the real-time FIFO scheduler with the
|
# This directive tells chronyd to use the real-time FIFO scheduler with the
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# This is an example chrony keys file. It is used for NTP authentication with
|
# This is an example chrony keys file. It enables authentication of NTP
|
||||||
# symmetric keys. It should be readable only by root or the user to which
|
# packets with symmetric keys when its location is specified by the keyfile
|
||||||
# chronyd is configured to switch to after start.
|
# 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
|
# Don't use the example keys! It's recommended to generate random keys using
|
||||||
# the chronyc keygen command.
|
# the chronyc keygen command.
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# This is a NetworkManager dispatcher script for chronyd to set its NTP sources
|
# This is a NetworkManager dispatcher / networkd-dispatcher script for
|
||||||
# online/offline when a default route is configured/removed on the system.
|
# chronyd to set its NTP sources online or offline when a network interface
|
||||||
|
# is configured or removed
|
||||||
|
|
||||||
export LC_ALL=C
|
export LC_ALL=C
|
||||||
|
|
||||||
if [ "$2" = "up" ]; then
|
# For NetworkManager consider only up/down events
|
||||||
/sbin/ip route list dev "$1" | grep -q '^default' &&
|
[ $# -ge 2 ] && [ "$2" != "up" ] && [ "$2" != "down" ] && exit 0
|
||||||
/usr/bin/chronyc online > /dev/null 2>&1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$2" = "down" ]; then
|
# Note: for networkd-dispatcher routable.d ~= on and off.d ~= off
|
||||||
/sbin/ip route list | grep -q '^default' ||
|
|
||||||
/usr/bin/chronyc offline > /dev/null 2>&1
|
chronyc onoffline > /dev/null 2>&1
|
||||||
fi
|
|
||||||
|
|
||||||
exit 0
|
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
|
|
||||||
|
|
||||||
@@ -1,13 +1,18 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=NTP client/server
|
Description=NTP client/server
|
||||||
|
Documentation=man:chronyd(8) man:chrony.conf(5)
|
||||||
After=ntpdate.service sntp.service ntpd.service
|
After=ntpdate.service sntp.service ntpd.service
|
||||||
Conflicts=ntpd.service systemd-timesyncd.service
|
Conflicts=ntpd.service systemd-timesyncd.service
|
||||||
|
ConditionCapability=CAP_SYS_TIME
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=forking
|
Type=forking
|
||||||
PIDFile=/var/run/chronyd.pid
|
PIDFile=/run/chrony/chronyd.pid
|
||||||
EnvironmentFile=-/etc/sysconfig/chronyd
|
EnvironmentFile=-/etc/sysconfig/chronyd
|
||||||
ExecStart=/usr/sbin/chronyd $OPTIONS
|
ExecStart=/usr/sbin/chronyd $OPTIONS
|
||||||
|
PrivateTmp=yes
|
||||||
|
ProtectHome=yes
|
||||||
|
ProtectSystem=full
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#include "md5.c"
|
#include "md5.c"
|
||||||
|
|
||||||
@@ -49,18 +50,17 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
|||||||
const unsigned char *in2, unsigned int in2_len,
|
const unsigned char *in2, unsigned int in2_len,
|
||||||
unsigned char *out, unsigned int out_len)
|
unsigned char *out, unsigned int out_len)
|
||||||
{
|
{
|
||||||
if (out_len < 16)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
MD5Init(&ctx);
|
MD5Init(&ctx);
|
||||||
MD5Update(&ctx, in1, in1_len);
|
MD5Update(&ctx, in1, in1_len);
|
||||||
if (in2)
|
if (in2)
|
||||||
MD5Update(&ctx, in2, in2_len);
|
MD5Update(&ctx, in2, in2_len);
|
||||||
MD5Final(&ctx);
|
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
|
void
|
||||||
|
|||||||
119
hash_nettle.c
Normal file
119
hash_nettle.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
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 char *name;
|
||||||
|
const char *int_name;
|
||||||
|
const struct nettle_hash *nettle_hash;
|
||||||
|
void *context;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct hash hashes[] = {
|
||||||
|
{ "MD5", "md5", NULL, NULL },
|
||||||
|
{ "SHA1", "sha1", NULL, NULL },
|
||||||
|
{ "SHA256", "sha256", NULL, NULL },
|
||||||
|
{ "SHA384", "sha384", NULL, NULL },
|
||||||
|
{ "SHA512", "sha512", NULL, NULL },
|
||||||
|
{ "SHA3-224", "sha3_224", NULL, NULL },
|
||||||
|
{ "SHA3-256", "sha3_256", NULL, NULL },
|
||||||
|
{ "SHA3-384", "sha3_384", NULL, NULL },
|
||||||
|
{ "SHA3-512", "sha3_512", NULL, NULL },
|
||||||
|
{ NULL, NULL, NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
HSH_GetHashId(const char *name)
|
||||||
|
{
|
||||||
|
int id, nid;
|
||||||
|
|
||||||
|
for (id = 0; hashes[id].name; id++) {
|
||||||
|
if (!strcmp(name, hashes[id].name))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hashes[id].name)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
const struct nettle_hash *hash;
|
||||||
|
void *context;
|
||||||
|
|
||||||
|
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].name; i++) {
|
||||||
|
if (hashes[i].context)
|
||||||
|
Free(hashes[i].context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
#include <nsslowhash.h>
|
#include <nsslowhash.h>
|
||||||
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
static NSSLOWInitContext *ictx;
|
static NSSLOWInitContext *ictx;
|
||||||
|
|
||||||
@@ -78,13 +79,17 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
|||||||
const unsigned char *in2, unsigned int in2_len,
|
const unsigned char *in2, unsigned int in2_len,
|
||||||
unsigned char *out, unsigned int out_len)
|
unsigned char *out, unsigned int out_len)
|
||||||
{
|
{
|
||||||
unsigned int ret;
|
unsigned char buf[MAX_HASH_LENGTH];
|
||||||
|
unsigned int ret = 0;
|
||||||
|
|
||||||
NSSLOWHASH_Begin(hashes[id].context);
|
NSSLOWHASH_Begin(hashes[id].context);
|
||||||
NSSLOWHASH_Update(hashes[id].context, in1, in1_len);
|
NSSLOWHASH_Update(hashes[id].context, in1, in1_len);
|
||||||
if (in2)
|
if (in2)
|
||||||
NSSLOWHASH_Update(hashes[id].context, in2, in2_len);
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
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
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
struct hash {
|
struct hash {
|
||||||
const char *name;
|
const char *name;
|
||||||
@@ -38,18 +39,6 @@ struct hash {
|
|||||||
|
|
||||||
static const struct hash hashes[] = {
|
static const struct hash hashes[] = {
|
||||||
{ "MD5", "md5", &md5_desc },
|
{ "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
|
|
||||||
#ifdef LTC_SHA1
|
#ifdef LTC_SHA1
|
||||||
{ "SHA1", "sha1", &sha1_desc },
|
{ "SHA1", "sha1", &sha1_desc },
|
||||||
#endif
|
#endif
|
||||||
@@ -62,6 +51,12 @@ static const struct hash hashes[] = {
|
|||||||
#ifdef LTC_SHA512
|
#ifdef LTC_SHA512
|
||||||
{ "SHA512", "sha512", &sha512_desc },
|
{ "SHA512", "sha512", &sha512_desc },
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef LTC_SHA3
|
||||||
|
{ "SHA3-224", "sha3-224", &sha3_224_desc },
|
||||||
|
{ "SHA3-256", "sha3-256", &sha3_256_desc },
|
||||||
|
{ "SHA3-384", "sha3-384", &sha3_384_desc },
|
||||||
|
{ "SHA3-512", "sha3-512", &sha3_512_desc },
|
||||||
|
#endif
|
||||||
#ifdef LTC_TIGER
|
#ifdef LTC_TIGER
|
||||||
{ "TIGER", "tiger", &tiger_desc },
|
{ "TIGER", "tiger", &tiger_desc },
|
||||||
#endif
|
#endif
|
||||||
@@ -99,19 +94,24 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
|
|||||||
const unsigned char *in2, unsigned int in2_len,
|
const unsigned char *in2, unsigned int in2_len,
|
||||||
unsigned char *out, unsigned int out_len)
|
unsigned char *out, unsigned int out_len)
|
||||||
{
|
{
|
||||||
|
unsigned char buf[MAX_HASH_LENGTH];
|
||||||
unsigned long len;
|
unsigned long len;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
len = out_len;
|
len = sizeof (buf);
|
||||||
if (in2)
|
if (in2)
|
||||||
r = hash_memory_multi(id, out, &len,
|
r = hash_memory_multi(id, buf, &len,
|
||||||
in1, (unsigned long)in1_len, in2, (unsigned long)in2_len, NULL, 0);
|
in1, (unsigned long)in1_len,
|
||||||
|
in2, (unsigned long)in2_len, NULL, 0);
|
||||||
else
|
else
|
||||||
r = hash_memory(id, in1, in1_len, out, &len);
|
r = hash_memory(id, in1, in1_len, buf, &len);
|
||||||
|
|
||||||
if (r != CRYPT_OK)
|
if (r != CRYPT_OK)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
len = MIN(len, out_len);
|
||||||
|
memcpy(out, buf, len);
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
79
hwclock.c
79
hwclock.c
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Miroslav Lichvar 2016
|
* Copyright (C) Miroslav Lichvar 2016-2018
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -36,11 +36,12 @@
|
|||||||
#include "regress.h"
|
#include "regress.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/* Maximum number of samples per clock */
|
/* Minimum and maximum number of samples per clock */
|
||||||
#define MAX_SAMPLES 16
|
#define MIN_SAMPLES 2
|
||||||
|
#define MAX_SAMPLES 64
|
||||||
|
|
||||||
/* Minimum interval between samples (in seconds) */
|
/* Maximum acceptable frequency offset of the clock */
|
||||||
#define MIN_SAMPLE_SEPARATION 1.0
|
#define MAX_FREQ_OFFSET (2.0 / 3.0)
|
||||||
|
|
||||||
struct HCL_Instance_Record {
|
struct HCL_Instance_Record {
|
||||||
/* HW and local reference timestamp */
|
/* HW and local reference timestamp */
|
||||||
@@ -49,15 +50,20 @@ struct HCL_Instance_Record {
|
|||||||
|
|
||||||
/* Samples stored as intervals (uncorrected for frequency error)
|
/* Samples stored as intervals (uncorrected for frequency error)
|
||||||
relative to local_ref and hw_ref */
|
relative to local_ref and hw_ref */
|
||||||
double x_data[MAX_SAMPLES];
|
double *x_data;
|
||||||
double y_data[MAX_SAMPLES];
|
double *y_data;
|
||||||
|
|
||||||
/* Number of samples */
|
/* Minimum, maximum and current number of samples */
|
||||||
|
int min_samples;
|
||||||
|
int max_samples;
|
||||||
int n_samples;
|
int n_samples;
|
||||||
|
|
||||||
/* Maximum error of the last sample */
|
/* Maximum error of the last sample */
|
||||||
double last_err;
|
double last_err;
|
||||||
|
|
||||||
|
/* Minimum interval between samples */
|
||||||
|
double min_separation;
|
||||||
|
|
||||||
/* Flag indicating the offset and frequency values are valid */
|
/* Flag indicating the offset and frequency values are valid */
|
||||||
int valid_coefs;
|
int valid_coefs;
|
||||||
|
|
||||||
@@ -86,15 +92,24 @@ handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
HCL_Instance
|
HCL_Instance
|
||||||
HCL_CreateInstance(void)
|
HCL_CreateInstance(int min_samples, int max_samples, double min_separation)
|
||||||
{
|
{
|
||||||
HCL_Instance clock;
|
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 = MallocNew(struct HCL_Instance_Record);
|
||||||
clock->x_data[MAX_SAMPLES - 1] = 0.0;
|
clock->x_data = MallocArray(double, max_samples);
|
||||||
clock->y_data[MAX_SAMPLES - 1] = 0.0;
|
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->n_samples = 0;
|
||||||
clock->valid_coefs = 0;
|
clock->valid_coefs = 0;
|
||||||
|
clock->min_separation = min_separation;
|
||||||
|
|
||||||
LCL_AddParameterChangeHandler(handle_slew, clock);
|
LCL_AddParameterChangeHandler(handle_slew, clock);
|
||||||
|
|
||||||
@@ -106,6 +121,8 @@ HCL_CreateInstance(void)
|
|||||||
void HCL_DestroyInstance(HCL_Instance clock)
|
void HCL_DestroyInstance(HCL_Instance clock)
|
||||||
{
|
{
|
||||||
LCL_RemoveParameterChangeHandler(handle_slew, clock);
|
LCL_RemoveParameterChangeHandler(handle_slew, clock);
|
||||||
|
Free(clock->y_data);
|
||||||
|
Free(clock->x_data);
|
||||||
Free(clock);
|
Free(clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +132,7 @@ int
|
|||||||
HCL_NeedsNewSample(HCL_Instance clock, struct timespec *now)
|
HCL_NeedsNewSample(HCL_Instance clock, struct timespec *now)
|
||||||
{
|
{
|
||||||
if (!clock->n_samples ||
|
if (!clock->n_samples ||
|
||||||
fabs(UTI_DiffTimespecsToDouble(now, &clock->local_ref)) >= MIN_SAMPLE_SEPARATION)
|
fabs(UTI_DiffTimespecsToDouble(now, &clock->local_ref)) >= clock->min_separation)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -134,18 +151,18 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
|||||||
|
|
||||||
/* Shift old samples */
|
/* Shift old samples */
|
||||||
if (clock->n_samples) {
|
if (clock->n_samples) {
|
||||||
if (clock->n_samples >= MAX_SAMPLES)
|
if (clock->n_samples >= clock->max_samples)
|
||||||
clock->n_samples--;
|
clock->n_samples--;
|
||||||
|
|
||||||
hw_delta = UTI_DiffTimespecsToDouble(hw_ts, &clock->hw_ref);
|
hw_delta = UTI_DiffTimespecsToDouble(hw_ts, &clock->hw_ref);
|
||||||
local_delta = UTI_DiffTimespecsToDouble(local_ts, &clock->local_ref) / local_freq;
|
local_delta = UTI_DiffTimespecsToDouble(local_ts, &clock->local_ref) / local_freq;
|
||||||
|
|
||||||
if (hw_delta <= 0.0 || local_delta < MIN_SAMPLE_SEPARATION / 2.0) {
|
if (hw_delta <= 0.0 || local_delta < clock->min_separation / 2.0) {
|
||||||
clock->n_samples = 0;
|
clock->n_samples = 0;
|
||||||
DEBUG_LOG(LOGF_HwClocks, "HW clock reset interval=%f", local_delta);
|
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->y_data[i - 1] = clock->y_data[i] - hw_delta;
|
||||||
clock->x_data[i - 1] = clock->x_data[i] - local_delta;
|
clock->x_data[i - 1] = clock->x_data[i] - local_delta;
|
||||||
}
|
}
|
||||||
@@ -158,31 +175,33 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts,
|
|||||||
|
|
||||||
/* Get new coefficients */
|
/* Get new coefficients */
|
||||||
clock->valid_coefs =
|
clock->valid_coefs =
|
||||||
RGR_FindBestRobustRegression(clock->x_data + MAX_SAMPLES - clock->n_samples,
|
RGR_FindBestRobustRegression(clock->x_data + clock->max_samples - clock->n_samples,
|
||||||
clock->y_data + MAX_SAMPLES - clock->n_samples,
|
clock->y_data + clock->max_samples - clock->n_samples,
|
||||||
clock->n_samples, 1.0e-9, &clock->offset, &raw_freq,
|
clock->n_samples, 1.0e-10, &clock->offset, &raw_freq,
|
||||||
&n_runs, &best_start);
|
&n_runs, &best_start);
|
||||||
|
|
||||||
if (!clock->valid_coefs) {
|
if (!clock->valid_coefs) {
|
||||||
DEBUG_LOG(LOGF_HwClocks, "HW clock needs more samples");
|
DEBUG_LOG("HW clock needs more samples");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
clock->frequency = raw_freq / local_freq;
|
clock->frequency = raw_freq / local_freq;
|
||||||
|
|
||||||
/* Drop unneeded samples */
|
/* 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, throw away
|
/* If the fit doesn't cross the error interval of the last sample,
|
||||||
all previous samples and keep only the frequency estimate */
|
or the frequency is not sane, drop all samples and start again */
|
||||||
if (fabs(clock->offset) > err) {
|
if (fabs(clock->offset) > err ||
|
||||||
DEBUG_LOG(LOGF_HwClocks, "HW clock reset offset=%e", clock->offset);
|
fabs(clock->frequency - 1.0) > MAX_FREQ_OFFSET) {
|
||||||
clock->offset = 0.0;
|
DEBUG_LOG("HW clock reset");
|
||||||
clock->n_samples = 1;
|
clock->n_samples = 0;
|
||||||
|
clock->valid_coefs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_HwClocks, "HW clock samples=%d offset=%e freq=%.9e raw_freq=%.9e err=%e ref_diff=%e",
|
DEBUG_LOG("HW clock samples=%d offset=%e freq=%e raw_freq=%e err=%e ref_diff=%e",
|
||||||
clock->n_samples, clock->offset, clock->frequency, raw_freq, err,
|
clock->n_samples, clock->offset, clock->frequency - 1.0, raw_freq - 1.0, err,
|
||||||
UTI_DiffTimespecsToDouble(&clock->hw_ref, &clock->local_ref));
|
UTI_DiffTimespecsToDouble(&clock->hw_ref, &clock->local_ref));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +216,7 @@ HCL_CookTime(HCL_Instance clock, struct timespec *raw, struct timespec *cooked,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
elapsed = UTI_DiffTimespecsToDouble(raw, &clock->hw_ref);
|
elapsed = UTI_DiffTimespecsToDouble(raw, &clock->hw_ref);
|
||||||
offset = clock->offset + elapsed / clock->frequency;
|
offset = elapsed / clock->frequency - clock->offset;
|
||||||
UTI_AddDoubleToTimespec(&clock->local_ref, offset, cooked);
|
UTI_AddDoubleToTimespec(&clock->local_ref, offset, cooked);
|
||||||
|
|
||||||
/* Fow now, just return the error of the last sample */
|
/* Fow now, just return the error of the last sample */
|
||||||
|
|||||||
@@ -29,7 +29,8 @@
|
|||||||
typedef struct HCL_Instance_Record *HCL_Instance;
|
typedef struct HCL_Instance_Record *HCL_Instance;
|
||||||
|
|
||||||
/* Create a new HW clock instance */
|
/* Create a new HW clock instance */
|
||||||
extern HCL_Instance HCL_CreateInstance(void);
|
extern HCL_Instance HCL_CreateInstance(int min_samples, int max_samples,
|
||||||
|
double min_separation);
|
||||||
|
|
||||||
/* Destroy a HW clock instance */
|
/* Destroy a HW clock instance */
|
||||||
extern void HCL_DestroyInstance(HCL_Instance clock);
|
extern void HCL_DestroyInstance(HCL_Instance clock);
|
||||||
|
|||||||
166
keys.c
166
keys.c
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
#include "array.h"
|
#include "array.h"
|
||||||
#include "keys.h"
|
#include "keys.h"
|
||||||
|
#include "cmac.h"
|
||||||
#include "cmdparse.h"
|
#include "cmdparse.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
@@ -42,11 +43,22 @@
|
|||||||
/* Consider 80 bits as the absolute minimum for a secure key */
|
/* Consider 80 bits as the absolute minimum for a secure key */
|
||||||
#define MIN_SECURE_KEY_LENGTH 10
|
#define MIN_SECURE_KEY_LENGTH 10
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NTP_MAC,
|
||||||
|
CMAC,
|
||||||
|
} KeyClass;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
char *val;
|
KeyClass class;
|
||||||
int len;
|
union {
|
||||||
int hash_id;
|
struct {
|
||||||
|
unsigned char *value;
|
||||||
|
int length;
|
||||||
|
int hash_id;
|
||||||
|
} ntp_mac;
|
||||||
|
CMC_Instance cmac;
|
||||||
|
} data;
|
||||||
int auth_delay;
|
int auth_delay;
|
||||||
} Key;
|
} Key;
|
||||||
|
|
||||||
@@ -62,9 +74,21 @@ static void
|
|||||||
free_keys(void)
|
free_keys(void)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
Key *key;
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(keys); i++)
|
for (i = 0; i < ARR_GetSize(keys); i++) {
|
||||||
Free(((Key *)ARR_GetElement(keys, i))->val);
|
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);
|
ARR_SetSize(keys, 0);
|
||||||
cache_valid = 0;
|
cache_valid = 0;
|
||||||
@@ -107,10 +131,13 @@ determine_hash_delay(uint32_t key_id)
|
|||||||
double diff, min_diff;
|
double diff, min_diff;
|
||||||
int i, nsecs;
|
int i, nsecs;
|
||||||
|
|
||||||
|
memset(&pkt, 0, sizeof (pkt));
|
||||||
|
|
||||||
for (i = 0; i < 10; i++) {
|
for (i = 0; i < 10; i++) {
|
||||||
LCL_ReadRawTime(&before);
|
LCL_ReadRawTime(&before);
|
||||||
KEY_GenerateAuth(key_id, (unsigned char *)&pkt, NTP_NORMAL_PACKET_LENGTH,
|
KEY_GenerateAuth(key_id, (unsigned char *)&pkt, NTP_HEADER_LENGTH,
|
||||||
(unsigned char *)&pkt.auth_data, sizeof (pkt.auth_data));
|
(unsigned char *)&pkt + NTP_HEADER_LENGTH,
|
||||||
|
sizeof (pkt) - NTP_HEADER_LENGTH);
|
||||||
LCL_ReadRawTime(&after);
|
LCL_ReadRawTime(&after);
|
||||||
|
|
||||||
diff = UTI_DiffTimespecsToDouble(&after, &before);
|
diff = UTI_DiffTimespecsToDouble(&after, &before);
|
||||||
@@ -119,39 +146,26 @@ determine_hash_delay(uint32_t key_id)
|
|||||||
min_diff = diff;
|
min_diff = diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add on a bit extra to allow for copying, conversions etc */
|
nsecs = 1.0e9 * min_diff;
|
||||||
nsecs = 1.0625e9 * min_diff;
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Keys, "authentication delay for key %"PRIu32": %d nsecs", key_id, nsecs);
|
DEBUG_LOG("authentication delay for key %"PRIu32": %d nsecs", key_id, nsecs);
|
||||||
|
|
||||||
return nsecs;
|
return nsecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Decode password encoded in ASCII or HEX */
|
/* Decode key encoded in ASCII or HEX */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
decode_password(char *key)
|
decode_key(char *key)
|
||||||
{
|
{
|
||||||
int i, j, len = strlen(key);
|
int len = strlen(key);
|
||||||
char buf[3], *p;
|
|
||||||
|
|
||||||
if (!strncmp(key, "ASCII:", 6)) {
|
if (!strncmp(key, "ASCII:", 6)) {
|
||||||
memmove(key, key + 6, len - 6);
|
memmove(key, key + 6, len - 6);
|
||||||
return len - 6;
|
return len - 6;
|
||||||
} else if (!strncmp(key, "HEX:", 4)) {
|
} else if (!strncmp(key, "HEX:", 4)) {
|
||||||
if ((len - 4) % 2)
|
return UTI_HexToBytes(key + 4, key, len);
|
||||||
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;
|
|
||||||
} else {
|
} else {
|
||||||
/* assume ASCII */
|
/* assume ASCII */
|
||||||
return len;
|
return len;
|
||||||
@@ -183,11 +197,11 @@ compare_keys_by_id(const void *a, const void *b)
|
|||||||
void
|
void
|
||||||
KEY_Reload(void)
|
KEY_Reload(void)
|
||||||
{
|
{
|
||||||
unsigned int i, line_number;
|
unsigned int i, line_number, key_length, cmac_key_length;
|
||||||
FILE *in;
|
FILE *in;
|
||||||
uint32_t key_id;
|
char line[2048], *key_file, *key_value;
|
||||||
char line[2048], *keyval, *key_file;
|
const char *key_type;
|
||||||
const char *hashname;
|
int hash_id;
|
||||||
Key key;
|
Key key;
|
||||||
|
|
||||||
free_keys();
|
free_keys();
|
||||||
@@ -198,9 +212,9 @@ KEY_Reload(void)
|
|||||||
if (!key_file)
|
if (!key_file)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
in = fopen(key_file, "r");
|
in = UTI_OpenFile(NULL, key_file, NULL, 'r', 0);
|
||||||
if (!in) {
|
if (!in) {
|
||||||
LOG(LOGS_WARN, LOGF_Keys, "Could not open keyfile %s", key_file);
|
LOG(LOGS_WARN, "Could not open keyfile %s", key_file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,26 +225,43 @@ KEY_Reload(void)
|
|||||||
if (!*line)
|
if (!*line)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!CPS_ParseKey(line, &key_id, &hashname, &keyval)) {
|
memset(&key, 0, sizeof (key));
|
||||||
LOG(LOGS_WARN, LOGF_Keys, "Could not parse key at line %d in file %s", line_number, key_file);
|
|
||||||
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.hash_id = HSH_GetHashId(hashname);
|
key_length = decode_key(key_value);
|
||||||
if (key.hash_id < 0) {
|
if (key_length == 0) {
|
||||||
LOG(LOGS_WARN, LOGF_Keys, "Unknown hash function in key %"PRIu32, key_id);
|
LOG(LOGS_WARN, "Could not decode key %"PRIu32, key.id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.len = decode_password(keyval);
|
hash_id = HSH_GetHashId(key_type);
|
||||||
if (!key.len) {
|
cmac_key_length = CMC_GetKeyLength(key_type);
|
||||||
LOG(LOGS_WARN, LOGF_Keys, "Could not decode password in key %"PRIu32, key_id);
|
|
||||||
|
if (hash_id >= 0) {
|
||||||
|
key.class = NTP_MAC;
|
||||||
|
key.data.ntp_mac.value = MallocArray(unsigned char, key_length);
|
||||||
|
memcpy(key.data.ntp_mac.value, key_value, key_length);
|
||||||
|
key.data.ntp_mac.length = key_length;
|
||||||
|
key.data.ntp_mac.hash_id = hash_id;
|
||||||
|
} else if (cmac_key_length > 0) {
|
||||||
|
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.data.cmac = CMC_CreateInstance(key_type, (unsigned char *)key_value, key_length);
|
||||||
|
assert(key.data.cmac);
|
||||||
|
} else {
|
||||||
|
LOG(LOGS_WARN, "Unknown hash function or cipher in key %"PRIu32, key.id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.id = key_id;
|
|
||||||
key.val = MallocArray(char, key.len);
|
|
||||||
memcpy(key.val, keyval, key.len);
|
|
||||||
ARR_AppendElement(keys, &key);
|
ARR_AppendElement(keys, &key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +275,7 @@ KEY_Reload(void)
|
|||||||
/* Check for duplicates */
|
/* Check for duplicates */
|
||||||
for (i = 1; i < ARR_GetSize(keys); i++) {
|
for (i = 1; i < ARR_GetSize(keys); i++) {
|
||||||
if (get_key(i - 1)->id == get_key(i)->id)
|
if (get_key(i - 1)->id == get_key(i)->id)
|
||||||
LOG(LOGS_WARN, LOGF_Keys, "Detected duplicate key %"PRIu32, get_key(i - 1)->id);
|
LOG(LOGS_WARN, "Detected duplicate key %"PRIu32, get_key(i - 1)->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Erase any passwords from stack */
|
/* Erase any passwords from stack */
|
||||||
@@ -333,7 +364,15 @@ KEY_GetAuthLength(uint32_t key_id)
|
|||||||
if (!key)
|
if (!key)
|
||||||
return 0;
|
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,30 +387,41 @@ KEY_CheckKeyLength(uint32_t key_id)
|
|||||||
if (!key)
|
if (!key)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return key->len >= MIN_SECURE_KEY_LENGTH;
|
switch (key->class) {
|
||||||
|
case NTP_MAC:
|
||||||
|
return key->data.ntp_mac.length >= MIN_SECURE_KEY_LENGTH;
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
generate_ntp_auth(int hash_id, const unsigned char *key, int key_len,
|
generate_auth(Key *key, const unsigned char *data, int data_len,
|
||||||
const unsigned char *data, int data_len,
|
unsigned char *auth, int auth_len)
|
||||||
unsigned char *auth, int auth_len)
|
|
||||||
{
|
{
|
||||||
return HSH_Hash(hash_id, key, key_len, data, data_len, auth, auth_len);
|
switch (key->class) {
|
||||||
|
case NTP_MAC:
|
||||||
|
return HSH_Hash(key->data.ntp_mac.hash_id, key->data.ntp_mac.value,
|
||||||
|
key->data.ntp_mac.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
|
static int
|
||||||
check_ntp_auth(int hash_id, const unsigned char *key, int key_len,
|
check_auth(Key *key, const unsigned char *data, int data_len,
|
||||||
const unsigned char *data, int data_len,
|
const unsigned char *auth, int auth_len, int trunc_len)
|
||||||
const unsigned char *auth, int auth_len, int trunc_len)
|
|
||||||
{
|
{
|
||||||
unsigned char buf[MAX_HASH_LENGTH];
|
unsigned char buf[MAX_HASH_LENGTH];
|
||||||
int hash_len;
|
int hash_len;
|
||||||
|
|
||||||
hash_len = generate_ntp_auth(hash_id, key, key_len, data, data_len, buf, sizeof (buf));
|
hash_len = generate_auth(key, data, data_len, buf, sizeof (buf));
|
||||||
|
|
||||||
return MIN(hash_len, trunc_len) == auth_len && !memcmp(buf, auth, auth_len);
|
return MIN(hash_len, trunc_len) == auth_len && !memcmp(buf, auth, auth_len);
|
||||||
}
|
}
|
||||||
@@ -380,7 +430,7 @@ check_ntp_auth(int hash_id, const unsigned char *key, int key_len,
|
|||||||
|
|
||||||
int
|
int
|
||||||
KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
||||||
unsigned char *auth, int auth_len)
|
unsigned char *auth, int auth_len)
|
||||||
{
|
{
|
||||||
Key *key;
|
Key *key;
|
||||||
|
|
||||||
@@ -389,8 +439,7 @@ KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
|||||||
if (!key)
|
if (!key)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return generate_ntp_auth(key->hash_id, (unsigned char *)key->val, key->len,
|
return generate_auth(key, data, data_len, auth, auth_len);
|
||||||
data, data_len, auth, auth_len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -406,6 +455,5 @@ KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
|
|||||||
if (!key)
|
if (!key)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return check_ntp_auth(key->hash_id, (unsigned char *)key->val, key->len,
|
return check_auth(key, data, data_len, auth, auth_len, trunc_len);
|
||||||
data, data_len, auth, auth_len, trunc_len);
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
local.c
22
local.c
@@ -146,7 +146,7 @@ calculate_sys_precision(void)
|
|||||||
|
|
||||||
assert(precision_log >= -30);
|
assert(precision_log >= -30);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Local, "Clock precision %.9f (%d)", precision_quantum, precision_log);
|
DEBUG_LOG("Clock precision %.9f (%d)", precision_quantum, precision_log);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -358,12 +358,12 @@ LCL_ReadRawTime(struct timespec *ts)
|
|||||||
{
|
{
|
||||||
#if HAVE_CLOCK_GETTIME
|
#if HAVE_CLOCK_GETTIME
|
||||||
if (clock_gettime(CLOCK_REALTIME, ts) < 0)
|
if (clock_gettime(CLOCK_REALTIME, ts) < 0)
|
||||||
LOG_FATAL(LOGF_Local, "clock_gettime() failed : %s", strerror(errno));
|
LOG_FATAL("clock_gettime() failed : %s", strerror(errno));
|
||||||
#else
|
#else
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
if (gettimeofday(&tv, NULL) < 0)
|
if (gettimeofday(&tv, NULL) < 0)
|
||||||
LOG_FATAL(LOGF_Local, "gettimeofday() failed : %s", strerror(errno));
|
LOG_FATAL("gettimeofday() failed : %s", strerror(errno));
|
||||||
|
|
||||||
UTI_TimevalToTimespec(&tv, ts);
|
UTI_TimevalToTimespec(&tv, ts);
|
||||||
#endif
|
#endif
|
||||||
@@ -426,7 +426,7 @@ clamp_freq(double freq)
|
|||||||
if (freq <= max_freq_ppm && freq >= -max_freq_ppm)
|
if (freq <= max_freq_ppm && freq >= -max_freq_ppm)
|
||||||
return freq;
|
return freq;
|
||||||
|
|
||||||
LOG(LOGS_WARN, LOGF_Local, "Frequency %.1f ppm exceeds allowed maximum", freq);
|
LOG(LOGS_WARN, "Frequency %.1f ppm exceeds allowed maximum", freq);
|
||||||
|
|
||||||
return CLAMP(-max_freq_ppm, freq, max_freq_ppm);
|
return CLAMP(-max_freq_ppm, freq, max_freq_ppm);
|
||||||
}
|
}
|
||||||
@@ -440,7 +440,7 @@ check_offset(struct timespec *now, double offset)
|
|||||||
if (UTI_IsTimeOffsetSane(now, -offset))
|
if (UTI_IsTimeOffsetSane(now, -offset))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
LOG(LOGS_WARN, LOGF_Local, "Adjustment of %.1f seconds is invalid", -offset);
|
LOG(LOGS_WARN, "Adjustment of %.1f seconds is invalid", -offset);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,7 +546,7 @@ LCL_ApplyStepOffset(double offset)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!(*drv_apply_step_offset)(offset)) {
|
if (!(*drv_apply_step_offset)(offset)) {
|
||||||
LOG(LOGS_ERR, LOGF_Local, "Could not step clock");
|
LOG(LOGS_ERR, "Could not step system clock");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -613,7 +613,7 @@ LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset, double corr_rate)
|
|||||||
|
|
||||||
current_freq_ppm = clamp_freq(current_freq_ppm);
|
current_freq_ppm = clamp_freq(current_freq_ppm);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Local, "old_freq=%.3fppm new_freq=%.3fppm offset=%.6fsec",
|
DEBUG_LOG("old_freq=%.3fppm new_freq=%.3fppm offset=%.6fsec",
|
||||||
old_freq_ppm, current_freq_ppm, doffset);
|
old_freq_ppm, current_freq_ppm, doffset);
|
||||||
|
|
||||||
/* Call the system-specific driver for setting the frequency */
|
/* Call the system-specific driver for setting the frequency */
|
||||||
@@ -660,7 +660,7 @@ lcl_RegisterSystemDrivers(lcl_ReadFrequencyDriver read_freq,
|
|||||||
|
|
||||||
current_freq_ppm = (*drv_read_freq)();
|
current_freq_ppm = (*drv_read_freq)();
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Local, "Local freq=%.3fppm", current_freq_ppm);
|
DEBUG_LOG("Local freq=%.3fppm", current_freq_ppm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -684,7 +684,7 @@ LCL_MakeStep(void)
|
|||||||
if (!LCL_ApplyStepOffset(-correction))
|
if (!LCL_ApplyStepOffset(-correction))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
LOG(LOGS_WARN, LOGF_Local, "System clock was stepped by %.6f seconds", correction);
|
LOG(LOGS_WARN, "System clock was stepped by %.6f seconds", correction);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -700,10 +700,10 @@ LCL_CanSystemLeap(void)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
LCL_SetSystemLeap(int leap)
|
LCL_SetSystemLeap(int leap, int tai_offset)
|
||||||
{
|
{
|
||||||
if (drv_set_leap) {
|
if (drv_set_leap) {
|
||||||
(drv_set_leap)(leap);
|
(drv_set_leap)(leap, tai_offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
local.h
9
local.h
@@ -201,10 +201,11 @@ extern int LCL_MakeStep(void);
|
|||||||
does something */
|
does something */
|
||||||
extern int LCL_CanSystemLeap(void);
|
extern int LCL_CanSystemLeap(void);
|
||||||
|
|
||||||
/* Routine to set the system clock to correct itself for a leap second if
|
/* Routine to set the system clock to correct itself for a leap second and also
|
||||||
supported. Leap second will be inserted at the end of the day if the
|
set its TAI-UTC offset. If supported, leap second will be inserted at the
|
||||||
argument is positive, deleted if negative, and zero resets the setting. */
|
end of the day if the argument is positive, deleted if negative, and zero
|
||||||
extern void LCL_SetSystemLeap(int leap);
|
resets the setting. */
|
||||||
|
extern void LCL_SetSystemLeap(int leap, int tai_offset);
|
||||||
|
|
||||||
/* Routine to set a frequency correction (in ppm) that should be applied
|
/* Routine to set a frequency correction (in ppm) that should be applied
|
||||||
to local clock to compensate for temperature changes. A positive
|
to local clock to compensate for temperature changes. A positive
|
||||||
|
|||||||
4
localp.h
4
localp.h
@@ -54,8 +54,8 @@ typedef int (*lcl_ApplyStepOffsetDriver)(double offset);
|
|||||||
raw time to get the corrected time */
|
raw time to get the corrected time */
|
||||||
typedef void (*lcl_OffsetCorrectionDriver)(struct timespec *raw, double *corr, double *err);
|
typedef void (*lcl_OffsetCorrectionDriver)(struct timespec *raw, double *corr, double *err);
|
||||||
|
|
||||||
/* System driver to schedule leap second */
|
/* System driver to schedule leap seconds and set TAI-UTC offset */
|
||||||
typedef void (*lcl_SetLeapDriver)(int leap);
|
typedef void (*lcl_SetLeapDriver)(int leap, int tai_offset);
|
||||||
|
|
||||||
/* System driver to set the synchronisation status */
|
/* System driver to set the synchronisation status */
|
||||||
typedef void (*lcl_SetSyncStatusDriver)(int synchronised, double est_error, double max_error);
|
typedef void (*lcl_SetSyncStatusDriver)(int synchronised, double est_error, double max_error);
|
||||||
|
|||||||
115
logging.c
115
logging.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Miroslav Lichvar 2011-2014
|
* Copyright (C) Miroslav Lichvar 2011-2014, 2018
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -29,25 +29,24 @@
|
|||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/* This is used by DEBUG_LOG macro */
|
/* This is used by DEBUG_LOG macro */
|
||||||
int log_debug_enabled = 0;
|
LOG_Severity log_min_severity = LOGS_INFO;
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Flag indicating we have initialised */
|
/* Flag indicating we have initialised */
|
||||||
static int initialised = 0;
|
static int initialised = 0;
|
||||||
|
|
||||||
|
static FILE *file_log = NULL;
|
||||||
static int system_log = 0;
|
static int system_log = 0;
|
||||||
|
|
||||||
static int parent_fd = 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 {
|
struct LogFile {
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *banner;
|
const char *banner;
|
||||||
@@ -69,6 +68,7 @@ void
|
|||||||
LOG_Initialise(void)
|
LOG_Initialise(void)
|
||||||
{
|
{
|
||||||
initialised = 1;
|
initialised = 1;
|
||||||
|
LOG_OpenFileLog(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -77,9 +77,11 @@ LOG_Initialise(void)
|
|||||||
void
|
void
|
||||||
LOG_Finalise(void)
|
LOG_Finalise(void)
|
||||||
{
|
{
|
||||||
if (system_log) {
|
if (system_log)
|
||||||
closelog();
|
closelog();
|
||||||
}
|
|
||||||
|
if (file_log)
|
||||||
|
fclose(file_log);
|
||||||
|
|
||||||
LOG_CycleLogFiles();
|
LOG_CycleLogFiles();
|
||||||
|
|
||||||
@@ -112,8 +114,8 @@ static void log_message(int fatal, LOG_Severity severity, const char *message)
|
|||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
|
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
|
||||||
} else {
|
} else if (file_log) {
|
||||||
fprintf(stderr, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
fprintf(file_log, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,25 +123,26 @@ static void log_message(int fatal, LOG_Severity severity, const char *message)
|
|||||||
|
|
||||||
void LOG_Message(LOG_Severity severity,
|
void LOG_Message(LOG_Severity severity,
|
||||||
#if DEBUG > 0
|
#if DEBUG > 0
|
||||||
LOG_Facility facility, int line_number,
|
int line_number, const char *filename, const char *function_name,
|
||||||
const char *filename, const char *function_name,
|
|
||||||
#endif
|
#endif
|
||||||
const char *format, ...)
|
const char *format, ...)
|
||||||
{
|
{
|
||||||
char buf[2048];
|
char buf[2048];
|
||||||
va_list other_args;
|
va_list other_args;
|
||||||
time_t t;
|
time_t t;
|
||||||
struct tm stm;
|
struct tm *tm;
|
||||||
|
|
||||||
if (!system_log) {
|
if (!system_log && file_log && severity >= log_min_severity) {
|
||||||
/* Don't clutter up syslog with timestamps and internal debugging info */
|
/* Don't clutter up syslog with timestamps and internal debugging info */
|
||||||
time(&t);
|
time(&t);
|
||||||
stm = *gmtime(&t);
|
tm = gmtime(&t);
|
||||||
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
|
if (tm) {
|
||||||
fprintf(stderr, "%s ", buf);
|
strftime(buf, sizeof (buf), "%Y-%m-%dT%H:%M:%SZ", tm);
|
||||||
|
fprintf(file_log, "%s ", buf);
|
||||||
|
}
|
||||||
#if DEBUG > 0
|
#if DEBUG > 0
|
||||||
if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
|
if (log_min_severity <= LOGS_DEBUG)
|
||||||
fprintf(stderr, "%s:%d:(%s) ", filename, line_number, function_name);
|
fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,28 +155,52 @@ void LOG_Message(LOG_Severity severity,
|
|||||||
case LOGS_INFO:
|
case LOGS_INFO:
|
||||||
case LOGS_WARN:
|
case LOGS_WARN:
|
||||||
case LOGS_ERR:
|
case LOGS_ERR:
|
||||||
log_message(0, severity, buf);
|
if (severity >= log_min_severity)
|
||||||
|
log_message(0, severity, buf);
|
||||||
break;
|
break;
|
||||||
case LOGS_FATAL:
|
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
|
/* Send the message also to the foreground process if it is
|
||||||
process or write it to stderr if not detached */
|
still running, or stderr if it is still open */
|
||||||
if (system_log) {
|
if (parent_fd > 0) {
|
||||||
if (parent_fd > 0) {
|
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
||||||
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
; /* Not much we can do here */
|
||||||
; /* Not much we can do here */
|
} else if (system_log && parent_fd == 0) {
|
||||||
} else if (parent_fd == 0) {
|
system_log = 0;
|
||||||
system_log = 0;
|
log_message(1, severity, buf);
|
||||||
log_message(1, severity, buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
exit(1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
LOG_OpenFileLog(const char *log_file)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -185,12 +212,10 @@ LOG_OpenSystemLog(void)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void LOG_SetDebugLevel(int level)
|
void LOG_SetMinSeverity(LOG_Severity severity)
|
||||||
{
|
{
|
||||||
debug_level = level;
|
/* Don't print any debug messages in a non-debug build */
|
||||||
if (level >= DEBUG_LEVEL_PRINT_DEBUG) {
|
log_min_severity = CLAMP(DEBUG > 0 ? LOGS_DEBUG : LOGS_INFO, severity, LOGS_FATAL);
|
||||||
log_debug_enabled = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -199,6 +224,8 @@ void
|
|||||||
LOG_SetParentFd(int fd)
|
LOG_SetParentFd(int fd)
|
||||||
{
|
{
|
||||||
parent_fd = fd;
|
parent_fd = fd;
|
||||||
|
if (file_log == stderr)
|
||||||
|
file_log = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -238,24 +265,20 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (!logfiles[id].file) {
|
if (!logfiles[id].file) {
|
||||||
char filename[512], *logdir = CNF_GetLogDir();
|
char *logdir = CNF_GetLogDir();
|
||||||
|
|
||||||
if (logdir[0] == '\0') {
|
if (logdir[0] == '\0') {
|
||||||
LOG(LOGS_WARN, LOGF_Logging, "logdir not specified");
|
LOG(LOGS_WARN, "logdir not specified");
|
||||||
logfiles[id].name = NULL;
|
logfiles[id].name = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snprintf(filename, sizeof(filename), "%s/%s.log",
|
logfiles[id].file = UTI_OpenFile(logdir, logfiles[id].name, ".log", 'a', 0644);
|
||||||
logdir, logfiles[id].name) >= sizeof (filename) ||
|
if (!logfiles[id].file) {
|
||||||
!(logfiles[id].file = fopen(filename, "a"))) {
|
/* Disable the log */
|
||||||
LOG(LOGS_WARN, LOGF_Logging, "Could not open log file %s", filename);
|
|
||||||
logfiles[id].name = NULL;
|
logfiles[id].name = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close on exec */
|
|
||||||
UTI_FdSetCloexec(fileno(logfiles[id].file));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
banner = CNF_GetLogBanner();
|
banner = CNF_GetLogBanner();
|
||||||
@@ -263,7 +286,7 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
|
|||||||
char bannerline[256];
|
char bannerline[256];
|
||||||
int i, bannerlen;
|
int i, bannerlen;
|
||||||
|
|
||||||
bannerlen = strlen(logfiles[id].banner);
|
bannerlen = MIN(strlen(logfiles[id].banner), sizeof (bannerline) - 1);
|
||||||
|
|
||||||
for (i = 0; i < bannerlen; i++)
|
for (i = 0; i < bannerlen; i++)
|
||||||
bannerline[i] = '=';
|
bannerline[i] = '=';
|
||||||
|
|||||||
90
logging.h
90
logging.h
@@ -31,9 +31,6 @@
|
|||||||
|
|
||||||
#include "sysincl.h"
|
#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
|
/* Line logging macros. If the compiler is GNU C, we take advantage of
|
||||||
being able to get the function name also. */
|
being able to get the function name also. */
|
||||||
|
|
||||||
@@ -46,79 +43,38 @@ extern int log_debug_enabled;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if DEBUG > 0
|
#if DEBUG > 0
|
||||||
#define LOG_MESSAGE(severity, facility, ...) \
|
#define LOG_MESSAGE(severity, ...) \
|
||||||
LOG_Message(severity, facility, __LINE__, __FILE__, FUNCTION_NAME, __VA_ARGS__)
|
LOG_Message(severity, __LINE__, __FILE__, FUNCTION_NAME, __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define LOG_MESSAGE(severity, facility, ...) \
|
#define LOG_MESSAGE(severity, ...) \
|
||||||
LOG_Message(severity, __VA_ARGS__)
|
LOG_Message(severity, __VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DEBUG_LOG(facility, ...) \
|
#define DEBUG_LOG(...) \
|
||||||
do { \
|
do { \
|
||||||
if (DEBUG && log_debug_enabled) \
|
if (DEBUG && log_min_severity == LOGS_DEBUG) \
|
||||||
LOG_MESSAGE(LOGS_DEBUG, facility, __VA_ARGS__); \
|
LOG_MESSAGE(LOGS_DEBUG, __VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define LOG_FATAL(facility, ...) \
|
#define LOG_FATAL(...) \
|
||||||
do { \
|
do { \
|
||||||
LOG_MESSAGE(LOGS_FATAL, facility, __VA_ARGS__); \
|
LOG_MESSAGE(LOGS_FATAL, __VA_ARGS__); \
|
||||||
exit(1); \
|
exit(1); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define LOG(severity, facility, ...) LOG_MESSAGE(severity, facility, __VA_ARGS__)
|
#define LOG(severity, ...) LOG_MESSAGE(severity, __VA_ARGS__)
|
||||||
|
|
||||||
/* Definition of severity */
|
/* Definition of severity */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LOGS_INFO,
|
LOGS_DEBUG = -1,
|
||||||
|
LOGS_INFO = 0,
|
||||||
LOGS_WARN,
|
LOGS_WARN,
|
||||||
LOGS_ERR,
|
LOGS_ERR,
|
||||||
LOGS_FATAL,
|
LOGS_FATAL,
|
||||||
LOGS_DEBUG
|
|
||||||
} LOG_Severity;
|
} LOG_Severity;
|
||||||
|
|
||||||
/* Definition of facility. Each message is tagged with who generated
|
/* Minimum severity of messages to be logged */
|
||||||
it, so that the user can customise what level of reporting he gets
|
extern LOG_Severity log_min_severity;
|
||||||
for each area of the software */
|
|
||||||
typedef enum {
|
|
||||||
LOGF_Reference,
|
|
||||||
LOGF_NtpIO,
|
|
||||||
LOGF_NtpIOLinux,
|
|
||||||
LOGF_NtpCore,
|
|
||||||
LOGF_NtpSignd,
|
|
||||||
LOGF_NtpSources,
|
|
||||||
LOGF_Scheduler,
|
|
||||||
LOGF_SourceStats,
|
|
||||||
LOGF_Sources,
|
|
||||||
LOGF_Local,
|
|
||||||
LOGF_Util,
|
|
||||||
LOGF_Main,
|
|
||||||
LOGF_Memory,
|
|
||||||
LOGF_Client,
|
|
||||||
LOGF_ClientLog,
|
|
||||||
LOGF_Configure,
|
|
||||||
LOGF_CmdMon,
|
|
||||||
LOGF_Acquire,
|
|
||||||
LOGF_Manual,
|
|
||||||
LOGF_Keys,
|
|
||||||
LOGF_Logging,
|
|
||||||
LOGF_Nameserv,
|
|
||||||
LOGF_PrivOps,
|
|
||||||
LOGF_Rtc,
|
|
||||||
LOGF_Regress,
|
|
||||||
LOGF_Sys,
|
|
||||||
LOGF_SysGeneric,
|
|
||||||
LOGF_SysLinux,
|
|
||||||
LOGF_SysMacOSX,
|
|
||||||
LOGF_SysNetBSD,
|
|
||||||
LOGF_SysSolaris,
|
|
||||||
LOGF_SysTimex,
|
|
||||||
LOGF_SysWinnt,
|
|
||||||
LOGF_TempComp,
|
|
||||||
LOGF_RtcLinux,
|
|
||||||
LOGF_Refclock,
|
|
||||||
LOGF_HwClocks,
|
|
||||||
LOGF_Smooth,
|
|
||||||
} LOG_Facility;
|
|
||||||
|
|
||||||
/* Init function */
|
/* Init function */
|
||||||
extern void LOG_Initialise(void);
|
extern void LOG_Initialise(void);
|
||||||
@@ -128,26 +84,26 @@ extern void LOG_Finalise(void);
|
|||||||
|
|
||||||
/* Line logging function */
|
/* Line logging function */
|
||||||
#if DEBUG > 0
|
#if DEBUG > 0
|
||||||
FORMAT_ATTRIBUTE_PRINTF(6, 7)
|
FORMAT_ATTRIBUTE_PRINTF(5, 6)
|
||||||
extern void LOG_Message(LOG_Severity severity, LOG_Facility facility,
|
extern void LOG_Message(LOG_Severity severity, int line_number, const char *filename,
|
||||||
int line_number, const char *filename,
|
|
||||||
const char *function_name, const char *format, ...);
|
const char *function_name, const char *format, ...);
|
||||||
#else
|
#else
|
||||||
FORMAT_ATTRIBUTE_PRINTF(2, 3)
|
FORMAT_ATTRIBUTE_PRINTF(2, 3)
|
||||||
extern void LOG_Message(LOG_Severity severity, const char *format, ...);
|
extern void LOG_Message(LOG_Severity severity, const char *format, ...);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Set debug level:
|
/* Set the minimum severity of a message to be logged or printed to terminal.
|
||||||
0, 1 - only non-debug messages are logged
|
If the severity is LOGS_DEBUG and DEBUG is enabled, all messages will be
|
||||||
2 - debug messages are logged too, all messages are prefixed with
|
prefixed with the filename, line number, and function name. */
|
||||||
filename, line, and function name
|
extern void LOG_SetMinSeverity(LOG_Severity severity);
|
||||||
*/
|
|
||||||
extern void LOG_SetDebugLevel(int level);
|
/* 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 */
|
/* Log messages to syslog instead of stderr */
|
||||||
extern void LOG_OpenSystemLog(void);
|
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);
|
extern void LOG_SetParentFd(int fd);
|
||||||
|
|
||||||
/* Close the pipe to the foreground process so it can exit */
|
/* Close the pipe to the foreground process so it can exit */
|
||||||
|
|||||||
350
main.c
350
main.c
@@ -4,7 +4,7 @@
|
|||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) John G. Hasler 2009
|
* Copyright (C) John G. Hasler 2009
|
||||||
* Copyright (C) Miroslav Lichvar 2012-2015
|
* Copyright (C) Miroslav Lichvar 2012-2018
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -38,6 +38,10 @@
|
|||||||
#include "ntp_signd.h"
|
#include "ntp_signd.h"
|
||||||
#include "ntp_sources.h"
|
#include "ntp_sources.h"
|
||||||
#include "ntp_core.h"
|
#include "ntp_core.h"
|
||||||
|
#include "nts_ke_client.h"
|
||||||
|
#include "nts_ke_server.h"
|
||||||
|
#include "nts_ntp_server.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "sources.h"
|
#include "sources.h"
|
||||||
#include "sourcestats.h"
|
#include "sourcestats.h"
|
||||||
#include "reference.h"
|
#include "reference.h"
|
||||||
@@ -86,8 +90,12 @@ static void
|
|||||||
delete_pidfile(void)
|
delete_pidfile(void)
|
||||||
{
|
{
|
||||||
const char *pidfile = CNF_GetPidFile();
|
const char *pidfile = CNF_GetPidFile();
|
||||||
/* Don't care if this fails, there's not a lot we can do */
|
|
||||||
unlink(pidfile);
|
if (!pidfile[0])
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!UTI_RemoveFile(NULL, pidfile, NULL))
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -97,7 +105,7 @@ MAI_CleanupAndExit(void)
|
|||||||
{
|
{
|
||||||
if (!initialised) exit(exit_status);
|
if (!initialised) exit(exit_status);
|
||||||
|
|
||||||
if (CNF_GetDumpOnExit()) {
|
if (CNF_GetDumpDir()[0] != '\0') {
|
||||||
SRC_DumpSources();
|
SRC_DumpSources();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,12 +116,16 @@ MAI_CleanupAndExit(void)
|
|||||||
TMC_Finalise();
|
TMC_Finalise();
|
||||||
MNL_Finalise();
|
MNL_Finalise();
|
||||||
CLG_Finalise();
|
CLG_Finalise();
|
||||||
|
NKC_Finalise();
|
||||||
|
NKS_Finalise();
|
||||||
|
NNS_Finalise();
|
||||||
NSD_Finalise();
|
NSD_Finalise();
|
||||||
NSR_Finalise();
|
NSR_Finalise();
|
||||||
SST_Finalise();
|
SST_Finalise();
|
||||||
NCR_Finalise();
|
NCR_Finalise();
|
||||||
NIO_Finalise();
|
NIO_Finalise();
|
||||||
CAM_Finalise();
|
CAM_Finalise();
|
||||||
|
SCK_Finalise();
|
||||||
KEY_Finalise();
|
KEY_Finalise();
|
||||||
RCL_Finalise();
|
RCL_Finalise();
|
||||||
SRC_Finalise();
|
SRC_Finalise();
|
||||||
@@ -127,9 +139,8 @@ MAI_CleanupAndExit(void)
|
|||||||
delete_pidfile();
|
delete_pidfile();
|
||||||
|
|
||||||
CNF_Finalise();
|
CNF_Finalise();
|
||||||
LOG_Finalise();
|
|
||||||
|
|
||||||
HSH_Finalise();
|
HSH_Finalise();
|
||||||
|
LOG_Finalise();
|
||||||
|
|
||||||
exit(exit_status);
|
exit(exit_status);
|
||||||
}
|
}
|
||||||
@@ -242,63 +253,54 @@ post_init_rtc_hook(void *anything)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Return 1 if the process exists on the system. */
|
|
||||||
|
|
||||||
static int
|
static void
|
||||||
does_process_exist(int pid)
|
check_pidfile(void)
|
||||||
{
|
|
||||||
int status;
|
|
||||||
status = getsid(pid);
|
|
||||||
if (status >= 0) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ================================================== */
|
|
||||||
|
|
||||||
static int
|
|
||||||
maybe_another_chronyd_running(int *other_pid)
|
|
||||||
{
|
{
|
||||||
const char *pidfile = CNF_GetPidFile();
|
const char *pidfile = CNF_GetPidFile();
|
||||||
FILE *in;
|
FILE *in;
|
||||||
int pid, count;
|
int pid, count;
|
||||||
|
|
||||||
*other_pid = 0;
|
if (!pidfile[0])
|
||||||
|
return;
|
||||||
|
|
||||||
in = fopen(pidfile, "r");
|
in = UTI_OpenFile(NULL, pidfile, NULL, 'r', 0);
|
||||||
if (!in) return 0;
|
if (!in)
|
||||||
|
return;
|
||||||
|
|
||||||
count = fscanf(in, "%d", &pid);
|
count = fscanf(in, "%d", &pid);
|
||||||
fclose(in);
|
fclose(in);
|
||||||
|
|
||||||
if (count != 1) return 0;
|
if (count != 1)
|
||||||
|
return;
|
||||||
|
|
||||||
*other_pid = pid;
|
if (getsid(pid) < 0)
|
||||||
return does_process_exist(pid);
|
return;
|
||||||
|
|
||||||
|
LOG_FATAL("Another chronyd may already be running (pid=%d), check %s",
|
||||||
|
pid, pidfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
write_lockfile(void)
|
write_pidfile(void)
|
||||||
{
|
{
|
||||||
const char *pidfile = CNF_GetPidFile();
|
const char *pidfile = CNF_GetPidFile();
|
||||||
FILE *out;
|
FILE *out;
|
||||||
|
|
||||||
out = fopen(pidfile, "w");
|
if (!pidfile[0])
|
||||||
if (!out) {
|
return;
|
||||||
LOG_FATAL(LOGF_Main, "could not open lockfile %s for writing", pidfile);
|
|
||||||
} else {
|
out = UTI_OpenFile(NULL, pidfile, NULL, 'W', 0644);
|
||||||
fprintf(out, "%d\n", (int)getpid());
|
fprintf(out, "%d\n", (int)getpid());
|
||||||
fclose(out);
|
fclose(out);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
#define DEV_NULL "/dev/null"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
go_daemon(void)
|
go_daemon(void)
|
||||||
{
|
{
|
||||||
@@ -307,14 +309,14 @@ go_daemon(void)
|
|||||||
/* Create pipe which will the daemon use to notify the grandparent
|
/* Create pipe which will the daemon use to notify the grandparent
|
||||||
when it's initialised or send an error message */
|
when it's initialised or send an error message */
|
||||||
if (pipe(pipefd)) {
|
if (pipe(pipefd)) {
|
||||||
LOG_FATAL(LOGF_Main, "Could not detach, pipe failed : %s", strerror(errno));
|
LOG_FATAL("pipe() failed : %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Does this preserve existing signal handlers? */
|
/* Does this preserve existing signal handlers? */
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
LOG_FATAL(LOGF_Main, "Could not detach, fork failed : %s", strerror(errno));
|
LOG_FATAL("fork() failed : %s", strerror(errno));
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
/* In the 'grandparent' */
|
/* In the 'grandparent' */
|
||||||
char message[1024];
|
char message[1024];
|
||||||
@@ -340,7 +342,7 @@ go_daemon(void)
|
|||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
LOG_FATAL(LOGF_Main, "Could not detach, fork failed : %s", strerror(errno));
|
LOG_FATAL("fork() failed : %s", strerror(errno));
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
exit(0); /* In the 'parent' */
|
exit(0); /* In the 'parent' */
|
||||||
} else {
|
} else {
|
||||||
@@ -348,7 +350,7 @@ go_daemon(void)
|
|||||||
|
|
||||||
/* Change current directory to / */
|
/* Change current directory to / */
|
||||||
if (chdir("/") < 0) {
|
if (chdir("/") < 0) {
|
||||||
LOG_FATAL(LOGF_Main, "Could not chdir to / : %s", strerror(errno));
|
LOG_FATAL("chdir() failed : %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Don't keep stdin/out/err from before. But don't close
|
/* Don't keep stdin/out/err from before. But don't close
|
||||||
@@ -359,152 +361,207 @@ go_daemon(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG_SetParentFd(pipefd[1]);
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_version(void)
|
||||||
|
{
|
||||||
|
printf("chronyd (chrony) version %s (%s)\n", CHRONY_VERSION, CHRONYD_FEATURES);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
parse_int_arg(const char *arg)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (sscanf(arg, "%d", &i) != 1)
|
||||||
|
LOG_FATAL("Invalid argument %s", arg);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
int main
|
int main
|
||||||
(int argc, char **argv)
|
(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const char *conf_file = DEFAULT_CONF_FILE;
|
const char *conf_file = DEFAULT_CONF_FILE;
|
||||||
const char *progname = argv[0];
|
const char *progname = argv[0];
|
||||||
char *user = NULL;
|
char *user = NULL, *log_file = NULL;
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
int debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
|
int opt, debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
|
||||||
int do_init_rtc = 0, restarted = 0, timeout = 0;
|
int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = -1;
|
||||||
int other_pid;
|
|
||||||
int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
|
int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
|
||||||
int system_log = 1;
|
int clock_control = 1, system_log = 1, log_severity = LOGS_INFO;
|
||||||
int config_args = 0;
|
int config_args = 0;
|
||||||
|
|
||||||
do_platform_checks();
|
do_platform_checks();
|
||||||
|
|
||||||
LOG_Initialise();
|
LOG_Initialise();
|
||||||
|
|
||||||
/* Parse command line options */
|
/* Parse (undocumented) long command-line options */
|
||||||
while (++argv, (--argc)>0) {
|
for (optind = 1; optind < argc; optind++) {
|
||||||
|
if (!strcmp("--help", argv[optind])) {
|
||||||
if (!strcmp("-f", *argv)) {
|
print_help(progname);
|
||||||
++argv, --argc;
|
|
||||||
conf_file = *argv;
|
|
||||||
} else if (!strcmp("-P", *argv)) {
|
|
||||||
++argv, --argc;
|
|
||||||
if (argc == 0 || sscanf(*argv, "%d", &sched_priority) != 1) {
|
|
||||||
LOG_FATAL(LOGF_Main, "Bad scheduler priority");
|
|
||||||
}
|
|
||||||
} else if (!strcmp("-m", *argv)) {
|
|
||||||
lock_memory = 1;
|
|
||||||
} else if (!strcmp("-r", *argv)) {
|
|
||||||
reload = 1;
|
|
||||||
} else if (!strcmp("-R", *argv)) {
|
|
||||||
restarted = 1;
|
|
||||||
} else if (!strcmp("-u", *argv)) {
|
|
||||||
++argv, --argc;
|
|
||||||
if (argc == 0) {
|
|
||||||
LOG_FATAL(LOGF_Main, "Missing user name");
|
|
||||||
} else {
|
|
||||||
user = *argv;
|
|
||||||
}
|
|
||||||
} else if (!strcmp("-F", *argv)) {
|
|
||||||
++argv, --argc;
|
|
||||||
if (argc == 0 || sscanf(*argv, "%d", &scfilter_level) != 1)
|
|
||||||
LOG_FATAL(LOGF_Main, "Bad syscall filter level");
|
|
||||||
} else if (!strcmp("-s", *argv)) {
|
|
||||||
do_init_rtc = 1;
|
|
||||||
} else if (!strcmp("-v", *argv) || !strcmp("--version",*argv)) {
|
|
||||||
/* This write to the terminal is OK, it comes before we turn into a daemon */
|
|
||||||
printf("chronyd (chrony) version %s (%s)\n", CHRONY_VERSION, CHRONYD_FEATURES);
|
|
||||||
return 0;
|
return 0;
|
||||||
} else if (!strcmp("-n", *argv)) {
|
} else if (!strcmp("--version", argv[optind])) {
|
||||||
nofork = 1;
|
print_version();
|
||||||
} else if (!strcmp("-d", *argv)) {
|
|
||||||
debug++;
|
|
||||||
nofork = 1;
|
|
||||||
system_log = 0;
|
|
||||||
} else if (!strcmp("-q", *argv)) {
|
|
||||||
ref_mode = REF_ModeUpdateOnce;
|
|
||||||
nofork = 1;
|
|
||||||
system_log = 0;
|
|
||||||
} else if (!strcmp("-Q", *argv)) {
|
|
||||||
ref_mode = REF_ModePrintOnce;
|
|
||||||
nofork = 1;
|
|
||||||
system_log = 0;
|
|
||||||
} else if (!strcmp("-t", *argv)) {
|
|
||||||
++argv, --argc;
|
|
||||||
if (argc == 0 || sscanf(*argv, "%d", &timeout) != 1 || timeout <= 0)
|
|
||||||
LOG_FATAL(LOGF_Main, "Bad timeout");
|
|
||||||
} else if (!strcmp("-4", *argv)) {
|
|
||||||
address_family = IPADDR_INET4;
|
|
||||||
} else if (!strcmp("-6", *argv)) {
|
|
||||||
address_family = IPADDR_INET6;
|
|
||||||
} else if (!strcmp("-h", *argv) || !strcmp("--help", *argv)) {
|
|
||||||
printf("Usage: %s [-4|-6] [-n|-d] [-q|-Q] [-r] [-R] [-s] [-t TIMEOUT] [-f FILE|COMMAND...]\n",
|
|
||||||
progname);
|
|
||||||
return 0;
|
return 0;
|
||||||
} else if (*argv[0] == '-') {
|
|
||||||
LOG_FATAL(LOGF_Main, "Unrecognized command line option [%s]", *argv);
|
|
||||||
} else {
|
|
||||||
/* Process remaining arguments and configuration lines */
|
|
||||||
config_args = argc;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getuid() != 0) {
|
optind = 1;
|
||||||
/* This write to the terminal is OK, it comes before we turn into a daemon */
|
|
||||||
fprintf(stderr,"Not superuser\n");
|
/* Parse short command-line options */
|
||||||
return 1;
|
while ((opt = getopt(argc, argv, "46df:F:hl:L:mnP:qQrRst:u:vx")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case '4':
|
||||||
|
case '6':
|
||||||
|
address_family = opt == '4' ? IPADDR_INET4 : IPADDR_INET6;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
debug++;
|
||||||
|
nofork = 1;
|
||||||
|
system_log = 0;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
conf_file = optarg;
|
||||||
|
break;
|
||||||
|
case 'F':
|
||||||
|
scfilter_level = parse_int_arg(optarg);
|
||||||
|
break;
|
||||||
|
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':
|
||||||
|
sched_priority = parse_int_arg(optarg);
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
ref_mode = REF_ModeUpdateOnce;
|
||||||
|
nofork = 1;
|
||||||
|
client_only = 0;
|
||||||
|
system_log = 0;
|
||||||
|
break;
|
||||||
|
case 'Q':
|
||||||
|
ref_mode = REF_ModePrintOnce;
|
||||||
|
nofork = 1;
|
||||||
|
client_only = 1;
|
||||||
|
clock_control = 0;
|
||||||
|
system_log = 0;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
reload = 1;
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
restarted = 1;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
do_init_rtc = 1;
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
timeout = parse_int_arg(optarg);
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
user = optarg;
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
print_version();
|
||||||
|
return 0;
|
||||||
|
case 'x':
|
||||||
|
clock_control = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
print_help(progname);
|
||||||
|
return opt != 'h';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getuid() && !client_only)
|
||||||
|
LOG_FATAL("Not superuser");
|
||||||
|
|
||||||
/* Turn into a daemon */
|
/* Turn into a daemon */
|
||||||
if (!nofork) {
|
if (!nofork) {
|
||||||
go_daemon();
|
go_daemon();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (system_log) {
|
if (log_file) {
|
||||||
|
LOG_OpenFileLog(log_file);
|
||||||
|
} else if (system_log) {
|
||||||
LOG_OpenSystemLog();
|
LOG_OpenSystemLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_SetDebugLevel(debug);
|
LOG_SetMinSeverity(debug >= 2 ? LOGS_DEBUG : log_severity);
|
||||||
|
|
||||||
LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting (%s)",
|
LOG(LOGS_INFO, "chronyd version %s starting (%s)", CHRONY_VERSION, CHRONYD_FEATURES);
|
||||||
CHRONY_VERSION, CHRONYD_FEATURES);
|
|
||||||
|
|
||||||
DNS_SetAddressFamily(address_family);
|
DNS_SetAddressFamily(address_family);
|
||||||
|
|
||||||
CNF_Initialise(restarted);
|
CNF_Initialise(restarted, client_only);
|
||||||
|
|
||||||
/* Parse the config file or the remaining command line arguments */
|
/* Parse the config file or the remaining command line arguments */
|
||||||
|
config_args = argc - optind;
|
||||||
if (!config_args) {
|
if (!config_args) {
|
||||||
CNF_ReadFile(conf_file);
|
CNF_ReadFile(conf_file);
|
||||||
} else {
|
} else {
|
||||||
do {
|
for (; optind < argc; optind++)
|
||||||
CNF_ParseLine(NULL, config_args - argc + 1, *argv);
|
CNF_ParseLine(NULL, config_args + optind - argc + 1, argv[optind]);
|
||||||
} while (++argv, --argc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check whether another chronyd may already be running. Do this after
|
/* Check whether another chronyd may already be running */
|
||||||
* forking, so that message logging goes to the right place (i.e. syslog), in
|
check_pidfile();
|
||||||
* case this chronyd is being run from a boot script. */
|
|
||||||
if (maybe_another_chronyd_running(&other_pid)) {
|
|
||||||
LOG_FATAL(LOGF_Main, "Another chronyd may already be running (pid=%d), check lockfile (%s)",
|
|
||||||
other_pid, CNF_GetPidFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write our lockfile to prevent other chronyds running. This has *GOT* to
|
if (!user)
|
||||||
* be done *AFTER* the daemon-creation fork() */
|
user = CNF_GetUser();
|
||||||
write_lockfile();
|
|
||||||
|
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();
|
PRV_Initialise();
|
||||||
LCL_Initialise();
|
LCL_Initialise();
|
||||||
SCH_Initialise();
|
SCH_Initialise();
|
||||||
SYS_Initialise();
|
SYS_Initialise(clock_control);
|
||||||
RTC_Initialise(do_init_rtc);
|
RTC_Initialise(do_init_rtc);
|
||||||
SRC_Initialise();
|
SRC_Initialise();
|
||||||
RCL_Initialise();
|
RCL_Initialise();
|
||||||
KEY_Initialise();
|
KEY_Initialise();
|
||||||
|
SCK_Initialise();
|
||||||
|
|
||||||
/* Open privileged ports before dropping root */
|
/* Open privileged ports before dropping root */
|
||||||
CAM_Initialise(address_family);
|
CAM_Initialise(address_family);
|
||||||
@@ -524,24 +581,17 @@ int main
|
|||||||
SYS_LockMemory();
|
SYS_LockMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
/* Drop root privileges if the specified user has a non-zero UID */
|
||||||
user = CNF_GetUser();
|
if (!geteuid() && (pw->pw_uid || pw->pw_gid))
|
||||||
}
|
|
||||||
|
|
||||||
if ((pw = getpwnam(user)) == NULL)
|
|
||||||
LOG_FATAL(LOGF_Main, "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 user has non-zero uid or gid */
|
|
||||||
if (pw->pw_uid || pw->pw_gid)
|
|
||||||
SYS_DropRoot(pw->pw_uid, pw->pw_gid);
|
SYS_DropRoot(pw->pw_uid, pw->pw_gid);
|
||||||
|
|
||||||
REF_Initialise();
|
REF_Initialise();
|
||||||
SST_Initialise();
|
SST_Initialise();
|
||||||
NSR_Initialise();
|
NSR_Initialise();
|
||||||
NSD_Initialise();
|
NSD_Initialise();
|
||||||
|
NNS_Initialise();
|
||||||
|
NKS_Initialise(scfilter_level);
|
||||||
|
NKC_Initialise();
|
||||||
CLG_Initialise();
|
CLG_Initialise();
|
||||||
MNL_Initialise();
|
MNL_Initialise();
|
||||||
TMC_Initialise();
|
TMC_Initialise();
|
||||||
@@ -550,12 +600,12 @@ int main
|
|||||||
/* From now on, it is safe to do finalisation on exit */
|
/* From now on, it is safe to do finalisation on exit */
|
||||||
initialised = 1;
|
initialised = 1;
|
||||||
|
|
||||||
UTI_SetQuitSignalsHandler(signal_cleanup);
|
UTI_SetQuitSignalsHandler(signal_cleanup, 1);
|
||||||
|
|
||||||
CAM_OpenUnixSocket();
|
CAM_OpenUnixSocket();
|
||||||
|
|
||||||
if (scfilter_level)
|
if (scfilter_level)
|
||||||
SYS_EnableSystemCallFilter(scfilter_level);
|
SYS_EnableSystemCallFilter(scfilter_level, SYS_MAIN_PROCESS);
|
||||||
|
|
||||||
if (ref_mode == REF_ModeNormal && CNF_GetInitSources() > 0) {
|
if (ref_mode == REF_ModeNormal && CNF_GetInitSources() > 0) {
|
||||||
ref_mode = REF_ModeInitStepSlew;
|
ref_mode = REF_ModeInitStepSlew;
|
||||||
@@ -564,7 +614,7 @@ int main
|
|||||||
REF_SetModeEndHandler(reference_mode_end);
|
REF_SetModeEndHandler(reference_mode_end);
|
||||||
REF_SetMode(ref_mode);
|
REF_SetMode(ref_mode);
|
||||||
|
|
||||||
if (timeout)
|
if (timeout >= 0)
|
||||||
SCH_AddTimeoutByDelay(timeout, quit_timeout, NULL);
|
SCH_AddTimeoutByDelay(timeout, quit_timeout, NULL);
|
||||||
|
|
||||||
if (do_init_rtc) {
|
if (do_init_rtc) {
|
||||||
@@ -577,7 +627,7 @@ int main
|
|||||||
the scheduler. */
|
the scheduler. */
|
||||||
SCH_MainLoop();
|
SCH_MainLoop();
|
||||||
|
|
||||||
LOG(LOGS_INFO, LOGF_Main, "chronyd exiting");
|
LOG(LOGS_INFO, "chronyd exiting");
|
||||||
|
|
||||||
MAI_CleanupAndExit();
|
MAI_CleanupAndExit();
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,6 @@ cd RELEASES/$subdir || exit 1
|
|||||||
|
|
||||||
echo $version > version.txt
|
echo $version > version.txt
|
||||||
|
|
||||||
sed -i -e "s%@@VERSION@@%${version}%" examples/chrony.spec
|
|
||||||
|
|
||||||
./configure && make -C doc man txt || exit 1
|
./configure && make -C doc man txt || exit 1
|
||||||
|
|
||||||
iconv -f utf-8 -t ascii//TRANSLIT < doc/installation.txt > INSTALL
|
iconv -f utf-8 -t ascii//TRANSLIT < doc/installation.txt > INSTALL
|
||||||
|
|||||||
46
manual.c
46
manual.c
@@ -97,7 +97,8 @@ MNL_Finalise(void)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
estimate_and_set_system(struct timespec *now, int offset_provided, double offset, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
|
estimate_and_set_system(struct timespec *now, int offset_provided, double offset,
|
||||||
|
double *reg_offset, double *dfreq_ppm, double *new_afreq_ppm)
|
||||||
{
|
{
|
||||||
double agos[MAX_SAMPLES], offsets[MAX_SAMPLES];
|
double agos[MAX_SAMPLES], offsets[MAX_SAMPLES];
|
||||||
double b0, b1;
|
double b0, b1;
|
||||||
@@ -108,32 +109,26 @@ estimate_and_set_system(struct timespec *now, int offset_provided, double offset
|
|||||||
int found_freq;
|
int found_freq;
|
||||||
double slew_by;
|
double slew_by;
|
||||||
|
|
||||||
|
b0 = offset_provided ? offset : 0.0;
|
||||||
|
b1 = freq = 0.0;
|
||||||
|
found_freq = 0;
|
||||||
|
|
||||||
if (n_samples > 1) {
|
if (n_samples > 1) {
|
||||||
for (i=0; i<n_samples; i++) {
|
for (i=0; i<n_samples; i++) {
|
||||||
agos[i] = UTI_DiffTimespecsToDouble(&samples[n_samples - 1].when, &samples[i].when);
|
agos[i] = UTI_DiffTimespecsToDouble(&samples[n_samples - 1].when, &samples[i].when);
|
||||||
offsets[i] = samples[i].offset;
|
offsets[i] = samples[i].offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
RGR_FindBestRobustRegression(agos, offsets, n_samples,
|
if (RGR_FindBestRobustRegression(agos, offsets, n_samples, 1.0e-8,
|
||||||
1.0e-8, /* 0.01ppm easily good enough for this! */
|
&b0, &b1, &n_runs, &best_start)) {
|
||||||
&b0, &b1, &n_runs, &best_start);
|
/* Ignore b0 from regression; treat offset as being the most
|
||||||
|
recently entered value. (If the administrator knows he's put
|
||||||
|
an outlier in, he will rerun the settime operation.) However,
|
||||||
/* Ignore b0 from regression; treat offset as being the most
|
the frequency estimate comes from the regression. */
|
||||||
recently entered value. (If the administrator knows he's put
|
freq = -b1;
|
||||||
an outlier in, he will rerun the settime operation.) However,
|
found_freq = 1;
|
||||||
the frequency estimate comes from the regression. */
|
|
||||||
|
|
||||||
freq = -b1;
|
|
||||||
found_freq = 1;
|
|
||||||
} else {
|
|
||||||
if (offset_provided) {
|
|
||||||
b0 = offset;
|
|
||||||
} else {
|
|
||||||
b0 = 0.0;
|
|
||||||
}
|
}
|
||||||
b1 = freq = 0.0;
|
} else {
|
||||||
found_freq = 0;
|
|
||||||
agos[0] = 0.0;
|
agos[0] = 0.0;
|
||||||
offsets[0] = b0;
|
offsets[0] = b0;
|
||||||
}
|
}
|
||||||
@@ -145,21 +140,20 @@ estimate_and_set_system(struct timespec *now, int offset_provided, double offset
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (found_freq) {
|
if (found_freq) {
|
||||||
LOG(LOGS_INFO, LOGF_Manual,
|
LOG(LOGS_INFO, "Making a frequency change of %.3f ppm and a slew of %.6f",
|
||||||
"Making a frequency change of %.3f ppm and a slew of %.6f",
|
|
||||||
1.0e6 * freq, slew_by);
|
1.0e6 * freq, slew_by);
|
||||||
|
|
||||||
REF_SetManualReference(now,
|
REF_SetManualReference(now,
|
||||||
slew_by,
|
slew_by,
|
||||||
freq, skew);
|
freq, skew);
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_INFO, LOGF_Manual, "Making a slew of %.6f", slew_by);
|
LOG(LOGS_INFO, "Making a slew of %.6f", slew_by);
|
||||||
REF_SetManualReference(now,
|
REF_SetManualReference(now,
|
||||||
slew_by,
|
slew_by,
|
||||||
0.0, skew);
|
0.0, skew);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset_cs) *offset_cs = (long)(0.5 + 100.0 * b0);
|
if (reg_offset) *reg_offset = b0;
|
||||||
if (dfreq_ppm) *dfreq_ppm = 1.0e6 * freq;
|
if (dfreq_ppm) *dfreq_ppm = 1.0e6 * freq;
|
||||||
if (new_afreq_ppm) *new_afreq_ppm = LCL_ReadAbsoluteFrequency();
|
if (new_afreq_ppm) *new_afreq_ppm = LCL_ReadAbsoluteFrequency();
|
||||||
|
|
||||||
@@ -173,7 +167,7 @@ estimate_and_set_system(struct timespec *now, int offset_provided, double offset
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
MNL_AcceptTimestamp(struct timespec *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
|
MNL_AcceptTimestamp(struct timespec *ts, double *reg_offset, double *dfreq_ppm, double *new_afreq_ppm)
|
||||||
{
|
{
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
double offset, diff;
|
double offset, diff;
|
||||||
@@ -210,7 +204,7 @@ MNL_AcceptTimestamp(struct timespec *ts, long *offset_cs, double *dfreq_ppm, dou
|
|||||||
samples[n_samples].orig_offset = offset;
|
samples[n_samples].orig_offset = offset;
|
||||||
++n_samples;
|
++n_samples;
|
||||||
|
|
||||||
estimate_and_set_system(&now, 1, offset, offset_cs, dfreq_ppm, new_afreq_ppm);
|
estimate_and_set_system(&now, 1, offset, reg_offset, dfreq_ppm, new_afreq_ppm);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
|||||||
2
manual.h
2
manual.h
@@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
extern void MNL_Initialise(void);
|
extern void MNL_Initialise(void);
|
||||||
extern void MNL_Finalise(void);
|
extern void MNL_Finalise(void);
|
||||||
extern int MNL_AcceptTimestamp(struct timespec *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm);
|
extern int MNL_AcceptTimestamp(struct timespec *ts, double *reg_offset, double *dfreq_ppm, double *new_afreq_ppm);
|
||||||
|
|
||||||
extern void MNL_Enable(void);
|
extern void MNL_Enable(void);
|
||||||
extern void MNL_Disable(void);
|
extern void MNL_Disable(void);
|
||||||
|
|||||||
34
memory.c
34
memory.c
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Miroslav Lichvar 2014
|
* Copyright (C) Miroslav Lichvar 2014, 2017
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -37,7 +37,7 @@ Malloc(size_t size)
|
|||||||
|
|
||||||
r = malloc(size);
|
r = malloc(size);
|
||||||
if (!r && size)
|
if (!r && size)
|
||||||
LOG_FATAL(LOGF_Memory, "Could not allocate memory");
|
LOG_FATAL("Could not allocate memory");
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@@ -49,11 +49,37 @@ Realloc(void *ptr, size_t size)
|
|||||||
|
|
||||||
r = realloc(ptr, size);
|
r = realloc(ptr, size);
|
||||||
if (!r && size)
|
if (!r && size)
|
||||||
LOG_FATAL(LOGF_Memory, "Could not allocate memory");
|
LOG_FATAL("Could not allocate memory");
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
get_array_size(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
size_t array_size;
|
||||||
|
|
||||||
|
array_size = nmemb * size;
|
||||||
|
|
||||||
|
/* Check for overflow */
|
||||||
|
if (nmemb > 0 && array_size / nmemb != size)
|
||||||
|
LOG_FATAL("Could not allocate memory");
|
||||||
|
|
||||||
|
return array_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
Malloc2(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
return Malloc(get_array_size(nmemb, size));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
Realloc2(void *ptr, size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
return Realloc(ptr, get_array_size(nmemb, size));
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
Strdup(const char *s)
|
Strdup(const char *s)
|
||||||
{
|
{
|
||||||
@@ -61,7 +87,7 @@ Strdup(const char *s)
|
|||||||
|
|
||||||
r = strdup(s);
|
r = strdup(s);
|
||||||
if (!r)
|
if (!r)
|
||||||
LOG_FATAL(LOGF_Memory, "Could not allocate memory");
|
LOG_FATAL("Could not allocate memory");
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|||||||
8
memory.h
8
memory.h
@@ -27,15 +27,19 @@
|
|||||||
#ifndef GOT_MEMORY_H
|
#ifndef GOT_MEMORY_H
|
||||||
#define GOT_MEMORY_H
|
#define GOT_MEMORY_H
|
||||||
|
|
||||||
|
#include "sysincl.h"
|
||||||
|
|
||||||
/* Wrappers checking for errors */
|
/* Wrappers checking for errors */
|
||||||
extern void *Malloc(size_t size);
|
extern void *Malloc(size_t size);
|
||||||
extern void *Realloc(void *ptr, size_t size);
|
extern void *Realloc(void *ptr, size_t size);
|
||||||
|
extern void *Malloc2(size_t nmemb, size_t size);
|
||||||
|
extern void *Realloc2(void *ptr, size_t nmemb, size_t size);
|
||||||
extern char *Strdup(const char *s);
|
extern char *Strdup(const char *s);
|
||||||
|
|
||||||
/* Convenient macros */
|
/* Convenient macros */
|
||||||
#define MallocNew(T) ((T *) Malloc(sizeof(T)))
|
#define MallocNew(T) ((T *) Malloc(sizeof(T)))
|
||||||
#define MallocArray(T, n) ((T *) Malloc((n) * sizeof(T)))
|
#define MallocArray(T, n) ((T *) Malloc2(n, sizeof(T)))
|
||||||
#define ReallocArray(T,n,x) ((T *) Realloc((void *)(x), (n)*sizeof(T)))
|
#define ReallocArray(T, n, x) ((T *) Realloc2((void *)(x), n, sizeof(T)))
|
||||||
#define Free(x) free(x)
|
#define Free(x) free(x)
|
||||||
|
|
||||||
#endif /* GOT_MEMORY_H */
|
#endif /* GOT_MEMORY_H */
|
||||||
|
|||||||
26
nameserv.c
26
nameserv.c
@@ -30,7 +30,11 @@
|
|||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <resolv.h>
|
||||||
|
|
||||||
#include "nameserv.h"
|
#include "nameserv.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -53,8 +57,20 @@ DNS_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
|||||||
max_addrs = MIN(max_addrs, DNS_MAX_ADDRESSES);
|
max_addrs = MIN(max_addrs, DNS_MAX_ADDRESSES);
|
||||||
|
|
||||||
memset(&hints, 0, sizeof (hints));
|
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);
|
result = getaddrinfo(name, NULL, &hints, &res);
|
||||||
|
|
||||||
@@ -141,10 +157,14 @@ DNS_IPAddress2Name(IPAddr *ip_addr, char *name, int len)
|
|||||||
|
|
||||||
#ifdef FEAT_IPV6
|
#ifdef FEAT_IPV6
|
||||||
struct sockaddr_in6 in6;
|
struct sockaddr_in6 in6;
|
||||||
|
IPSockAddr ip_saddr;
|
||||||
socklen_t slen;
|
socklen_t slen;
|
||||||
char hbuf[NI_MAXHOST];
|
char hbuf[NI_MAXHOST];
|
||||||
|
|
||||||
slen = UTI_IPAndPortToSockaddr(ip_addr, 0, (struct sockaddr *)&in6);
|
ip_saddr.ip_addr = *ip_addr;
|
||||||
|
ip_saddr.port = 0;
|
||||||
|
|
||||||
|
slen = SCK_IPSockAddrToSockaddr(&ip_saddr, (struct sockaddr *)&in6, sizeof (in6));
|
||||||
if (!getnameinfo((struct sockaddr *)&in6, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
if (!getnameinfo((struct sockaddr *)&in6, slen, hbuf, sizeof (hbuf), NULL, 0, 0))
|
||||||
result = hbuf;
|
result = hbuf;
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ struct DNS_Async_Instance {
|
|||||||
int pipe[2];
|
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;
|
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);
|
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 */
|
/* Notify the main thread that the result is ready */
|
||||||
if (write(inst->pipe[1], "", 1) < 0)
|
if (write(inst->pipe[1], "", 1) < 0)
|
||||||
@@ -78,11 +80,9 @@ end_resolving(int fd, int event, void *anything)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (pthread_join(inst->thread, NULL)) {
|
if (pthread_join(inst->thread, NULL)) {
|
||||||
LOG_FATAL(LOGF_Nameserv, "pthread_join() failed");
|
LOG_FATAL("pthread_join() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
resolving_threads--;
|
|
||||||
|
|
||||||
SCH_RemoveFileHandler(inst->pipe[0]);
|
SCH_RemoveFileHandler(inst->pipe[0]);
|
||||||
close(inst->pipe[0]);
|
close(inst->pipe[0]);
|
||||||
close(inst->pipe[1]);
|
close(inst->pipe[1]);
|
||||||
@@ -110,17 +110,14 @@ DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *
|
|||||||
inst->status = DNS_Failure;
|
inst->status = DNS_Failure;
|
||||||
|
|
||||||
if (pipe(inst->pipe)) {
|
if (pipe(inst->pipe)) {
|
||||||
LOG_FATAL(LOGF_Nameserv, "pipe() failed");
|
LOG_FATAL("pipe() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
UTI_FdSetCloexec(inst->pipe[0]);
|
UTI_FdSetCloexec(inst->pipe[0]);
|
||||||
UTI_FdSetCloexec(inst->pipe[1]);
|
UTI_FdSetCloexec(inst->pipe[1]);
|
||||||
|
|
||||||
resolving_threads++;
|
|
||||||
assert(resolving_threads <= 1);
|
|
||||||
|
|
||||||
if (pthread_create(&inst->thread, NULL, start_resolving, inst)) {
|
if (pthread_create(&inst->thread, NULL, start_resolving, inst)) {
|
||||||
LOG_FATAL(LOGF_Nameserv, "pthread_create() failed");
|
LOG_FATAL("pthread_create() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
SCH_AddFileHandler(inst->pipe[0], SCH_FILE_INPUT, end_resolving, inst);
|
SCH_AddFileHandler(inst->pipe[0], SCH_FILE_INPUT, end_resolving, inst);
|
||||||
|
|||||||
76
ntp.h
76
ntp.h
@@ -38,24 +38,30 @@ typedef struct {
|
|||||||
|
|
||||||
typedef uint32_t NTP_int32;
|
typedef uint32_t NTP_int32;
|
||||||
|
|
||||||
|
/* The UDP port number used by NTP */
|
||||||
|
#define NTP_PORT 123
|
||||||
|
|
||||||
/* The NTP protocol version that we support */
|
/* The NTP protocol version that we support */
|
||||||
#define NTP_VERSION 4
|
#define NTP_VERSION 4
|
||||||
|
|
||||||
/* Maximum stratum number (infinity) */
|
/* Maximum stratum number (infinity) */
|
||||||
#define NTP_MAX_STRATUM 16
|
#define NTP_MAX_STRATUM 16
|
||||||
|
|
||||||
/* The minimum valid length of an extension field */
|
/* Invalid stratum number */
|
||||||
#define NTP_MIN_EXTENSION_LENGTH 16
|
#define NTP_INVALID_STRATUM 0
|
||||||
|
|
||||||
/* 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
|
|
||||||
|
|
||||||
/* The minimum and maximum supported length of MAC */
|
/* The minimum and maximum supported length of MAC */
|
||||||
#define NTP_MIN_MAC_LENGTH (4 + 16)
|
#define NTP_MIN_MAC_LENGTH (4 + 16)
|
||||||
#define NTP_MAX_MAC_LENGTH (4 + MAX_HASH_LENGTH)
|
#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
|
/* The maximum length of MAC in NTPv4 packets which allows deterministic
|
||||||
parsing of extension fields (RFC 7822) */
|
parsing of extension fields (RFC 7822) */
|
||||||
#define NTP_MAX_V4_MAC_LENGTH (4 + 20)
|
#define NTP_MAX_V4_MAC_LENGTH (4 + 20)
|
||||||
@@ -90,21 +96,10 @@ typedef struct {
|
|||||||
NTP_int64 receive_ts;
|
NTP_int64 receive_ts;
|
||||||
NTP_int64 transmit_ts;
|
NTP_int64 transmit_ts;
|
||||||
|
|
||||||
/* Optional extension fields, we don't send packets with them yet */
|
uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH];
|
||||||
/* uint8_t extensions[] */
|
|
||||||
|
|
||||||
/* Optional message authentication code (MAC) */
|
|
||||||
NTP_int32 auth_keyid;
|
|
||||||
uint8_t auth_data[NTP_MAX_MAC_LENGTH - 4];
|
|
||||||
} NTP_Packet;
|
} NTP_Packet;
|
||||||
|
|
||||||
#define NTP_NORMAL_PACKET_LENGTH (int)offsetof(NTP_Packet, auth_keyid)
|
#define NTP_HEADER_LENGTH (int)offsetof(NTP_Packet, extensions)
|
||||||
|
|
||||||
/* 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;
|
|
||||||
|
|
||||||
/* Macros to work with the lvm field */
|
/* Macros to work with the lvm field */
|
||||||
#define NTP_LVM_TO_LEAP(lvm) (((lvm) >> 6) & 0x3)
|
#define NTP_LVM_TO_LEAP(lvm) (((lvm) >> 6) & 0x3)
|
||||||
@@ -118,4 +113,45 @@ typedef struct {
|
|||||||
#define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
|
#define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
|
||||||
#define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */
|
#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, /* MAC using symmetric key (RFC 1305, RFC 5905) */
|
||||||
|
NTP_AUTH_MSSNTP, /* MS-SNTP authenticator field */
|
||||||
|
NTP_AUTH_MSSNTP_EXT, /* MS-SNTP extended authenticator field */
|
||||||
|
NTP_AUTH_NTS, /* Network Time Security (RFC ????) */
|
||||||
|
} 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;
|
||||||
|
int stratum;
|
||||||
|
} NTP_Sample;
|
||||||
|
|
||||||
#endif /* GOT_NTP_H */
|
#endif /* GOT_NTP_H */
|
||||||
|
|||||||
486
ntp_auth.c
Normal file
486
ntp_auth.c
Normal file
@@ -0,0 +1,486 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*
|
||||||
|
**********************************************************************
|
||||||
|
|
||||||
|
=======================================================================
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* 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 (NTP_Packet) - info->length - 4);
|
||||||
|
|
||||||
|
auth_len = KEY_GenerateAuth(key_id, (unsigned char *)packet, info->length,
|
||||||
|
(unsigned char *)packet + info->length + 4, max_auth_len);
|
||||||
|
if (!auth_len) {
|
||||||
|
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->length += 4 + auth_len;
|
||||||
|
|
||||||
|
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, (void *)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 void
|
||||||
|
adjust_timestamp(NTP_AuthMode mode, uint32_t key_id, struct timespec *ts)
|
||||||
|
{
|
||||||
|
switch (mode) {
|
||||||
|
case NTP_AUTH_SYMMETRIC:
|
||||||
|
ts->tv_nsec += KEY_GetAuthDelay(key_id);
|
||||||
|
UTI_NormaliseTimespec(ts);
|
||||||
|
break;
|
||||||
|
case NTP_AUTH_MSSNTP:
|
||||||
|
ts->tv_nsec += NSD_GetAuthDelay(key_id);
|
||||||
|
UTI_NormaliseTimespec(ts);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_zero_data(unsigned char *data, int length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < length; i++)
|
||||||
|
if (data[i])
|
||||||
|
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, const IPSockAddr *ntp_address)
|
||||||
|
{
|
||||||
|
NAU_Instance instance = create_instance(NTP_AUTH_NTS);
|
||||||
|
|
||||||
|
instance->nts = NNC_CreateInstance(nts_address, name, ntp_address);
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NAU_DestroyInstance(NAU_Instance instance)
|
||||||
|
{
|
||||||
|
if (instance->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NAU_AdjustRequestTimestamp(NAU_Instance instance, struct timespec *ts)
|
||||||
|
{
|
||||||
|
adjust_timestamp(instance->mode, instance->key_id, ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* 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) {
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* The NTPv4-specific limit for MAC length enables deterministic parsing of
|
||||||
|
packets with extension fields (RFC 7822), but we support longer MACs in
|
||||||
|
packets with no extension fields for compatibility with older chrony
|
||||||
|
clients. Check if the longer MAC would authenticate the packet before
|
||||||
|
trying to parse the data as an extension field. */
|
||||||
|
if (parsed == NTP_HEADER_LENGTH &&
|
||||||
|
remainder > NTP_MAX_V4_MAC_LENGTH && remainder <= NTP_MAX_MAC_LENGTH &&
|
||||||
|
KEY_CheckAuth(ntohl(*(uint32_t *)(data + parsed)), data, parsed,
|
||||||
|
(void *)(data + parsed + 4), remainder - 4, NTP_MAX_MAC_LENGTH - 4))
|
||||||
|
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)) {
|
||||||
|
/* Invalid MAC or format error */
|
||||||
|
DEBUG_LOG("Invalid format or MAC");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(ef_length > 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) {
|
||||||
|
/* This is not 100% reliable as a MAC could fail to authenticate and could
|
||||||
|
pass as an extension field, leaving reminder smaller than the minimum 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_NTS:
|
||||||
|
if (!NNS_CheckRequestAuth(request, info, kod))
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NAU_AdjustResponseTimestamp(NTP_Packet *request, NTP_PacketInfo *info, struct timespec *ts)
|
||||||
|
{
|
||||||
|
adjust_timestamp(info->auth.mode, info->auth.mac.key_id, ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
89
ntp_auth.h
Normal file
89
ntp_auth.h
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
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,
|
||||||
|
const IPSockAddr *ntp_address);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
/* Adjust a transmit timestamp for an estimated minimum time it takes to call
|
||||||
|
NAU_GenerateRequestAuth() */
|
||||||
|
extern void NAU_AdjustRequestTimestamp(NAU_Instance instance, struct timespec *ts);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
/* Adjust a transmit timestamp for an estimated minimum time it takes to call
|
||||||
|
NAU_GenerateResponseAuth() */
|
||||||
|
extern void NAU_AdjustResponseTimestamp(NTP_Packet *request, NTP_PacketInfo *info,
|
||||||
|
struct timespec *ts);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
#endif
|
||||||
1389
ntp_core.c
1389
ntp_core.c
File diff suppressed because it is too large
Load Diff
15
ntp_core.h
15
ntp_core.h
@@ -59,7 +59,8 @@ extern void NCR_Initialise(void);
|
|||||||
extern void NCR_Finalise(void);
|
extern void NCR_Finalise(void);
|
||||||
|
|
||||||
/* Get a new instance for a server or peer */
|
/* 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 */
|
/* Destroy an instance */
|
||||||
extern void NCR_DestroyInstance(NCR_Instance 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);
|
extern void NCR_ResetPoll(NCR_Instance instance);
|
||||||
|
|
||||||
/* Change the remote address of an 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,
|
/* 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 */
|
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 */
|
/* Slew receive and transmit times in instance records */
|
||||||
extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset);
|
extern void NCR_SlewTimes(NCR_Instance inst, struct timespec *when, double dfreq, double doffset);
|
||||||
|
|
||||||
/* Take a particular source online (i.e. start sampling it) */
|
/* Take a particular source online (i.e. start sampling it) or offline
|
||||||
extern void NCR_TakeSourceOnline(NCR_Instance inst);
|
(i.e. stop sampling it) */
|
||||||
|
extern void NCR_SetConnectivity(NCR_Instance inst, SRC_Connectivity connectivity);
|
||||||
/* 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);
|
|
||||||
|
|
||||||
extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll);
|
extern void NCR_ModifyMinpoll(NCR_Instance inst, int new_minpoll);
|
||||||
|
|
||||||
|
|||||||
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
|
||||||
|
*
|
||||||
|
* 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
|
||||||
631
ntp_io.c
631
ntp_io.c
@@ -4,7 +4,7 @@
|
|||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Timo Teras 2009
|
* Copyright (C) Timo Teras 2009
|
||||||
* Copyright (C) Miroslav Lichvar 2009, 2013-2015
|
* Copyright (C) Miroslav Lichvar 2009, 2013-2016, 2018
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -30,11 +30,11 @@
|
|||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
#include "array.h"
|
|
||||||
#include "ntp_io.h"
|
#include "ntp_io.h"
|
||||||
#include "ntp_core.h"
|
#include "ntp_core.h"
|
||||||
#include "ntp_sources.h"
|
#include "ntp_sources.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "local.h"
|
#include "local.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
@@ -46,54 +46,16 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define INVALID_SOCK_FD -1
|
#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 */
|
/* The server/peer and client sockets for IPv4 and IPv6 */
|
||||||
static int server_sock_fd4;
|
static int server_sock_fd4;
|
||||||
static int client_sock_fd4;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
static int server_sock_fd6;
|
static int server_sock_fd6;
|
||||||
|
static int client_sock_fd4;
|
||||||
static int client_sock_fd6;
|
static int client_sock_fd6;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Reference counters for server sockets to keep them open only when needed */
|
/* Reference counters for server sockets to keep them open only when needed */
|
||||||
static int server_sock_ref4;
|
static int server_sock_ref4;
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
static int server_sock_ref6;
|
static int server_sock_ref6;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Flag indicating we create a new connected client socket for each
|
/* Flag indicating we create a new connected client socket for each
|
||||||
server instead of sharing client_sock_fd4 and client_sock_fd6 */
|
server instead of sharing client_sock_fd4 and client_sock_fd6 */
|
||||||
@@ -105,6 +67,9 @@ static int separate_client_sockets;
|
|||||||
disabled */
|
disabled */
|
||||||
static int permanent_server_sockets;
|
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 */
|
/* Flag indicating that we have been initialised */
|
||||||
static int initialised=0;
|
static int initialised=0;
|
||||||
|
|
||||||
@@ -116,157 +81,44 @@ static void read_from_socket(int sock_fd, int event, void *anything);
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
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;
|
int sock_fd, sock_flags, events = SCH_FILE_INPUT;
|
||||||
socklen_t my_addr_len;
|
IPSockAddr local_addr;
|
||||||
int sock_fd;
|
|
||||||
IPAddr bind_address;
|
|
||||||
int events = SCH_FILE_INPUT, on_off = 1;
|
|
||||||
|
|
||||||
/* Open Internet domain UDP socket for NTP message transmissions */
|
if (!SCK_IsFamilySupported(family))
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
|
||||||
sock_fd = socket(family, SOCK_DGRAM, 0);
|
if (!client_only)
|
||||||
|
CNF_GetBindAddress(family, &local_addr.ip_addr);
|
||||||
|
else
|
||||||
|
CNF_GetBindAcquisitionAddress(family, &local_addr.ip_addr);
|
||||||
|
|
||||||
|
if (local_addr.ip_addr.family != family)
|
||||||
|
SCK_GetAnyLocalIPAddress(family, &local_addr.ip_addr);
|
||||||
|
|
||||||
|
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, sock_flags);
|
||||||
if (sock_fd < 0) {
|
if (sock_fd < 0) {
|
||||||
if (!client_only) {
|
if (!client_only)
|
||||||
LOG(LOGS_ERR, LOGF_NtpIO, "Could not open %s NTP socket : %s",
|
LOG(LOGS_ERR, "Could not open NTP socket on %s", UTI_IPSockAddrToString(&local_addr));
|
||||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
|
||||||
} else {
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "Could not open %s NTP socket : %s",
|
|
||||||
UTI_SockaddrFamilyToString(family), strerror(errno));
|
|
||||||
}
|
|
||||||
return INVALID_SOCK_FD;
|
return INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close on exec */
|
if (!client_only && family == IPADDR_INET4 && local_addr.port > 0)
|
||||||
UTI_FdSetCloexec(sock_fd);
|
bound_server_sock_fd4 = local_addr.ip_addr.addr.in4 != INADDR_ANY;
|
||||||
|
|
||||||
/* 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;
|
|
||||||
#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, LOGF_NtpIO, "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, LOGF_NtpIO, "Could not set %s socket option", "SO_BROADCAST");
|
|
||||||
/* Don't quit - we might survive anyway */
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef SO_TIMESTAMP
|
|
||||||
/* Enable receiving of timestamp control messages */
|
|
||||||
#ifdef SO_TIMESTAMPNS
|
|
||||||
/* Try nanosecond resolution first */
|
|
||||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPNS, (char *)&on_off, sizeof(on_off)) < 0)
|
|
||||||
#endif
|
|
||||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMP, (char *)&on_off, sizeof(on_off)) < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_NtpIO, "Could not set %s socket option", "SO_TIMESTAMP");
|
|
||||||
/* Don't quit - we might survive anyway */
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
/* Enable kernel/HW timestamping of packets */
|
||||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
NIO_Linux_SetTimestampSocketOptions(sock_fd, client_only, &events);
|
if (!NIO_Linux_SetTimestampSocketOptions(sock_fd, client_only, &events))
|
||||||
#endif
|
#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, LOGF_NtpIO, "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, LOGF_NtpIO, "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, LOGF_NtpIO, "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, LOGF_NtpIO, "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, LOGF_NtpIO, "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, LOGF_NtpIO, "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 */
|
/* Register handler for read and possibly exception events on the socket */
|
||||||
SCH_AddFileHandler(sock_fd, events, read_from_socket, NULL);
|
SCH_AddFileHandler(sock_fd, events, read_from_socket, NULL);
|
||||||
@@ -277,40 +129,9 @@ prepare_socket(int family, int port_number, int client_only)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
prepare_separate_client_socket(int family)
|
open_separate_client_socket(IPSockAddr *remote_addr)
|
||||||
{
|
{
|
||||||
switch (family) {
|
return open_socket(remote_addr->ip_addr.family, 0, 1, remote_addr);
|
||||||
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(LOGF_NtpIO, "Could not connect NTP socket to %s:%d : %s",
|
|
||||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
|
||||||
strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -321,34 +142,11 @@ close_socket(int sock_fd)
|
|||||||
if (sock_fd == INVALID_SOCK_FD)
|
if (sock_fd == INVALID_SOCK_FD)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
|
NIO_Linux_NotifySocketClosing(sock_fd);
|
||||||
|
#endif
|
||||||
SCH_RemoveFileHandler(sock_fd);
|
SCH_RemoveFileHandler(sock_fd);
|
||||||
close(sock_fd);
|
SCK_CloseSocket(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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -361,23 +159,20 @@ NIO_Initialise(int family)
|
|||||||
assert(!initialised);
|
assert(!initialised);
|
||||||
initialised = 1;
|
initialised = 1;
|
||||||
|
|
||||||
|
#ifdef PRIVOPS_BINDSOCKET
|
||||||
|
SCK_SetPrivBind(PRV_BindSocket);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
NIO_Linux_Initialise();
|
NIO_Linux_Initialise();
|
||||||
#else
|
#else
|
||||||
if (1) {
|
if (1) {
|
||||||
double tx_comp, rx_comp;
|
CNF_HwTsInterface *conf_iface;
|
||||||
char *name;
|
if (CNF_GetHwTsInterface(0, &conf_iface))
|
||||||
if (CNF_GetHwTsInterface(0, &name, &tx_comp, &rx_comp))
|
LOG_FATAL("HW timestamping not supported");
|
||||||
LOG_FATAL(LOGF_NtpIO, "HW timestamping not supported");
|
|
||||||
}
|
}
|
||||||
#endif
|
#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();
|
server_port = CNF_GetNTPPort();
|
||||||
client_port = CNF_GetAcquisitionPort();
|
client_port = CNF_GetAcquisitionPort();
|
||||||
|
|
||||||
@@ -390,48 +185,39 @@ NIO_Initialise(int family)
|
|||||||
client_port == server_port);
|
client_port == server_port);
|
||||||
|
|
||||||
server_sock_fd4 = INVALID_SOCK_FD;
|
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;
|
server_sock_fd6 = INVALID_SOCK_FD;
|
||||||
|
client_sock_fd4 = INVALID_SOCK_FD;
|
||||||
client_sock_fd6 = INVALID_SOCK_FD;
|
client_sock_fd6 = INVALID_SOCK_FD;
|
||||||
|
server_sock_ref4 = 0;
|
||||||
server_sock_ref6 = 0;
|
server_sock_ref6 = 0;
|
||||||
#endif
|
|
||||||
|
|
||||||
if (family == IPADDR_UNSPEC || family == IPADDR_INET4) {
|
if (family == IPADDR_UNSPEC || family == IPADDR_INET4) {
|
||||||
if (permanent_server_sockets && server_port)
|
if (permanent_server_sockets && server_port)
|
||||||
server_sock_fd4 = prepare_socket(AF_INET, server_port, 0);
|
server_sock_fd4 = open_socket(IPADDR_INET4, server_port, 0, NULL);
|
||||||
if (!separate_client_sockets) {
|
if (!separate_client_sockets) {
|
||||||
if (client_port != server_port || !server_port)
|
if (client_port != server_port || !server_port)
|
||||||
client_sock_fd4 = prepare_socket(AF_INET, client_port, 1);
|
client_sock_fd4 = open_socket(IPADDR_INET4, client_port, 1, NULL);
|
||||||
else
|
else
|
||||||
client_sock_fd4 = server_sock_fd4;
|
client_sock_fd4 = server_sock_fd4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
if (family == IPADDR_UNSPEC || family == IPADDR_INET6) {
|
if (family == IPADDR_UNSPEC || family == IPADDR_INET6) {
|
||||||
if (permanent_server_sockets && server_port)
|
if (permanent_server_sockets && server_port)
|
||||||
server_sock_fd6 = prepare_socket(AF_INET6, server_port, 0);
|
server_sock_fd6 = open_socket(IPADDR_INET6, server_port, 0, NULL);
|
||||||
if (!separate_client_sockets) {
|
if (!separate_client_sockets) {
|
||||||
if (client_port != server_port || !server_port)
|
if (client_port != server_port || !server_port)
|
||||||
client_sock_fd6 = prepare_socket(AF_INET6, client_port, 1);
|
client_sock_fd6 = open_socket(IPADDR_INET6, client_port, 1, NULL);
|
||||||
else
|
else
|
||||||
client_sock_fd6 = server_sock_fd6;
|
client_sock_fd6 = server_sock_fd6;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
if ((server_port && server_sock_fd4 == INVALID_SOCK_FD &&
|
if ((server_port && permanent_server_sockets &&
|
||||||
permanent_server_sockets
|
server_sock_fd4 == INVALID_SOCK_FD && server_sock_fd6 == INVALID_SOCK_FD) ||
|
||||||
#ifdef FEAT_IPV6
|
(!separate_client_sockets &&
|
||||||
&& server_sock_fd6 == INVALID_SOCK_FD
|
client_sock_fd4 == INVALID_SOCK_FD && client_sock_fd6 == INVALID_SOCK_FD)) {
|
||||||
#endif
|
LOG_FATAL("Could not open NTP sockets");
|
||||||
) || (!separate_client_sockets && client_sock_fd4 == INVALID_SOCK_FD
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
&& client_sock_fd6 == INVALID_SOCK_FD
|
|
||||||
#endif
|
|
||||||
)) {
|
|
||||||
LOG_FATAL(LOGF_NtpIO, "Could not open NTP sockets");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,14 +230,11 @@ NIO_Finalise(void)
|
|||||||
close_socket(client_sock_fd4);
|
close_socket(client_sock_fd4);
|
||||||
close_socket(server_sock_fd4);
|
close_socket(server_sock_fd4);
|
||||||
server_sock_fd4 = client_sock_fd4 = INVALID_SOCK_FD;
|
server_sock_fd4 = client_sock_fd4 = INVALID_SOCK_FD;
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
if (server_sock_fd6 != client_sock_fd6)
|
if (server_sock_fd6 != client_sock_fd6)
|
||||||
close_socket(client_sock_fd6);
|
close_socket(client_sock_fd6);
|
||||||
close_socket(server_sock_fd6);
|
close_socket(server_sock_fd6);
|
||||||
server_sock_fd6 = client_sock_fd6 = INVALID_SOCK_FD;
|
server_sock_fd6 = client_sock_fd6 = INVALID_SOCK_FD;
|
||||||
#endif
|
|
||||||
ARR_DestroyInstance(recv_headers);
|
|
||||||
ARR_DestroyInstance(recv_messages);
|
|
||||||
|
|
||||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
NIO_Linux_Finalise();
|
NIO_Linux_Finalise();
|
||||||
@@ -466,25 +249,13 @@ int
|
|||||||
NIO_OpenClientSocket(NTP_Remote_Address *remote_addr)
|
NIO_OpenClientSocket(NTP_Remote_Address *remote_addr)
|
||||||
{
|
{
|
||||||
if (separate_client_sockets) {
|
if (separate_client_sockets) {
|
||||||
int sock_fd = prepare_separate_client_socket(remote_addr->ip_addr.family);
|
return open_separate_client_socket(remote_addr);
|
||||||
|
|
||||||
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;
|
|
||||||
} else {
|
} else {
|
||||||
switch (remote_addr->ip_addr.family) {
|
switch (remote_addr->ip_addr.family) {
|
||||||
case IPADDR_INET4:
|
case IPADDR_INET4:
|
||||||
return client_sock_fd4;
|
return client_sock_fd4;
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
return client_sock_fd6;
|
return client_sock_fd6;
|
||||||
#endif
|
|
||||||
default:
|
default:
|
||||||
return INVALID_SOCK_FD;
|
return INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
@@ -501,20 +272,18 @@ NIO_OpenServerSocket(NTP_Remote_Address *remote_addr)
|
|||||||
if (permanent_server_sockets)
|
if (permanent_server_sockets)
|
||||||
return server_sock_fd4;
|
return server_sock_fd4;
|
||||||
if (server_sock_fd4 == INVALID_SOCK_FD)
|
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)
|
if (server_sock_fd4 != INVALID_SOCK_FD)
|
||||||
server_sock_ref4++;
|
server_sock_ref4++;
|
||||||
return server_sock_fd4;
|
return server_sock_fd4;
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
if (permanent_server_sockets)
|
if (permanent_server_sockets)
|
||||||
return server_sock_fd6;
|
return server_sock_fd6;
|
||||||
if (server_sock_fd6 == INVALID_SOCK_FD)
|
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)
|
if (server_sock_fd6 != INVALID_SOCK_FD)
|
||||||
server_sock_ref6++;
|
server_sock_ref6++;
|
||||||
return server_sock_fd6;
|
return server_sock_fd6;
|
||||||
#endif
|
|
||||||
default:
|
default:
|
||||||
return INVALID_SOCK_FD;
|
return INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
@@ -542,16 +311,12 @@ NIO_CloseServerSocket(int sock_fd)
|
|||||||
close_socket(server_sock_fd4);
|
close_socket(server_sock_fd4);
|
||||||
server_sock_fd4 = INVALID_SOCK_FD;
|
server_sock_fd4 = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
}
|
} else if (sock_fd == server_sock_fd6) {
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
else if (sock_fd == server_sock_fd6) {
|
|
||||||
if (--server_sock_ref6 <= 0) {
|
if (--server_sock_ref6 <= 0) {
|
||||||
close_socket(server_sock_fd6);
|
close_socket(server_sock_fd6);
|
||||||
server_sock_fd6 = INVALID_SOCK_FD;
|
server_sock_fd6 = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
#endif
|
|
||||||
else {
|
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -562,121 +327,76 @@ int
|
|||||||
NIO_IsServerSocket(int sock_fd)
|
NIO_IsServerSocket(int sock_fd)
|
||||||
{
|
{
|
||||||
return sock_fd != INVALID_SOCK_FD &&
|
return sock_fd != INVALID_SOCK_FD &&
|
||||||
(sock_fd == server_sock_fd4
|
(sock_fd == server_sock_fd4 || sock_fd == server_sock_fd6);
|
||||||
#ifdef FEAT_IPV6
|
}
|
||||||
|| sock_fd == server_sock_fd6
|
|
||||||
#endif
|
/* ================================================== */
|
||||||
);
|
|
||||||
|
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
|
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_Address local_addr;
|
||||||
NTP_Local_Timestamp local_ts;
|
NTP_Local_Timestamp local_ts;
|
||||||
struct timespec sched_ts;
|
struct timespec sched_ts;
|
||||||
struct cmsghdr *cmsg;
|
|
||||||
int if_index;
|
|
||||||
|
|
||||||
SCH_GetLastEventTime(&local_ts.ts, &local_ts.err, NULL);
|
SCH_GetLastEventTime(&local_ts.ts, &local_ts.err, NULL);
|
||||||
local_ts.source = NTP_TS_DAEMON;
|
local_ts.source = NTP_TS_DAEMON;
|
||||||
sched_ts = local_ts.ts;
|
sched_ts = local_ts.ts;
|
||||||
|
|
||||||
if (hdr->msg_namelen > sizeof (union sockaddr_in46)) {
|
if (message->addr_type != SCK_ADDR_IP) {
|
||||||
DEBUG_LOG(LOGF_NtpIO, "Truncated source address");
|
DEBUG_LOG("Unexpected address type");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hdr->msg_namelen >= sizeof (((struct sockaddr *)hdr->msg_name)->sa_family)) {
|
local_addr.ip_addr = message->local_addr.ip;
|
||||||
UTI_SockaddrToIPAndPort((struct sockaddr *)hdr->msg_name,
|
local_addr.if_index = message->if_index;;
|
||||||
&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.sock_fd = sock_fd;
|
local_addr.sock_fd = sock_fd;
|
||||||
if_index = -1;
|
|
||||||
|
|
||||||
if (hdr->msg_flags & MSG_TRUNC) {
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "Received truncated message from %s:%d",
|
|
||||||
UTI_IPToString(&remote_addr.ip_addr), remote_addr.port);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hdr->msg_flags & MSG_CTRUNC) {
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "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;
|
|
||||||
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;
|
|
||||||
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
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
if (NIO_Linux_ProcessMessage(&remote_addr, &local_addr, &local_ts,
|
if (NIO_Linux_ProcessMessage(message, &local_addr, &local_ts, event))
|
||||||
hdr, length, sock_fd, if_index))
|
|
||||||
return;
|
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
|
#endif
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "Received %d bytes from %s:%d to %s fd=%d if=%d tss=%d delay=%.9f",
|
if (local_ts.source != NTP_TS_DAEMON)
|
||||||
length, UTI_IPToString(&remote_addr.ip_addr), remote_addr.port,
|
DEBUG_LOG("Updated RX timestamp delay=%.9f tss=%u",
|
||||||
UTI_IPToString(&local_addr.ip_addr), local_addr.sock_fd, if_index,
|
UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts), local_ts.source);
|
||||||
local_ts.source, UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts));
|
|
||||||
|
|
||||||
/* Just ignore the packet if it's not of a recognized length */
|
/* 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;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
NSR_ProcessRx(&remote_addr, &local_addr, &local_ts,
|
NSR_ProcessRx(&message->remote_addr.ip, &local_addr, &local_ts, message->data, message->length);
|
||||||
(NTP_Packet *)hdr->msg_iov[0].iov_base, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -687,46 +407,28 @@ read_from_socket(int sock_fd, int event, void *anything)
|
|||||||
/* This should only be called when there is something
|
/* This should only be called when there is something
|
||||||
to read, otherwise it may block */
|
to read, otherwise it may block */
|
||||||
|
|
||||||
struct MessageHeader *hdr;
|
SCK_Message messages[SCK_MAX_RECV_MESSAGES];
|
||||||
unsigned int i, n;
|
int i, received, flags = 0;
|
||||||
int status, flags = 0;
|
|
||||||
|
|
||||||
hdr = ARR_GetElements(recv_headers);
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
n = ARR_GetSize(recv_headers);
|
if (NIO_Linux_ProcessEvent(sock_fd, event))
|
||||||
assert(n >= 1);
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (event == SCH_FILE_EXCEPTION) {
|
if (event == SCH_FILE_EXCEPTION) {
|
||||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
flags |= MSG_ERRQUEUE;
|
flags |= SCK_FLAG_MSG_ERRQUEUE;
|
||||||
#else
|
#else
|
||||||
assert(0);
|
assert(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_RECVMMSG
|
received = SCK_ReceiveMessages(sock_fd, messages, SCK_MAX_RECV_MESSAGES, flags);
|
||||||
status = recvmmsg(sock_fd, hdr, n, flags | MSG_DONTWAIT, NULL);
|
if (received <= 0)
|
||||||
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(LOGF_NtpIO, "Could not receive from fd %d : %s", sock_fd,
|
|
||||||
strerror(errno));
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < received; i++)
|
||||||
hdr = ARR_GetElement(recv_headers, i);
|
process_message(&messages[i], sock_fd, event);
|
||||||
process_message(&hdr->msg_hdr, hdr->msg_len, sock_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Restore the buffers to their original state */
|
|
||||||
prepare_buffers(n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -736,102 +438,43 @@ int
|
|||||||
NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||||
NTP_Local_Address *local_addr, int length, int process_tx)
|
NTP_Local_Address *local_addr, int length, int process_tx)
|
||||||
{
|
{
|
||||||
union sockaddr_in46 remote;
|
SCK_Message message;
|
||||||
struct msghdr msg;
|
|
||||||
struct iovec iov;
|
|
||||||
struct cmsghdr *cmsg, cmsgbuf[CMSGBUF_SIZE / sizeof (struct cmsghdr)];
|
|
||||||
int cmsglen;
|
|
||||||
socklen_t addrlen = 0;
|
|
||||||
|
|
||||||
assert(initialised);
|
assert(initialised);
|
||||||
|
|
||||||
if (local_addr->sock_fd == INVALID_SOCK_FD) {
|
if (local_addr->sock_fd == INVALID_SOCK_FD) {
|
||||||
DEBUG_LOG(LOGF_NtpIO, "No socket to send to %s:%d",
|
DEBUG_LOG("No socket to send to %s", UTI_IPSockAddrToString(remote_addr));
|
||||||
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port);
|
|
||||||
return 0;
|
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) {
|
if (NIO_IsServerSocket(local_addr->sock_fd) || !separate_client_sockets) {
|
||||||
addrlen = UTI_IPAndPortToSockaddr(&remote_addr->ip_addr, remote_addr->port,
|
message.remote_addr.ip.ip_addr = remote_addr->ip_addr;
|
||||||
&remote.u);
|
message.remote_addr.ip.port = remote_addr->port;
|
||||||
if (!addrlen)
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addrlen) {
|
message.if_index = local_addr->if_index;
|
||||||
msg.msg_name = &remote.u;
|
message.local_addr.ip = local_addr->ip_addr;
|
||||||
msg.msg_namelen = addrlen;
|
|
||||||
} else {
|
|
||||||
msg.msg_name = NULL;
|
|
||||||
msg.msg_namelen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
iov.iov_base = packet;
|
#if !defined(HAVE_IN_PKTINFO) && defined(IP_SENDSRCADDR)
|
||||||
iov.iov_len = length;
|
/* On FreeBSD a local IPv4 address cannot be specified on bound socket */
|
||||||
msg.msg_iov = &iov;
|
if (message.local_addr.ip.family == IPADDR_INET4 &&
|
||||||
msg.msg_iovlen = 1;
|
(local_addr->sock_fd != server_sock_fd4 || bound_server_sock_fd4))
|
||||||
msg.msg_control = cmsgbuf;
|
message.local_addr.ip.family = IPADDR_UNSPEC;
|
||||||
msg.msg_controllen = sizeof(cmsgbuf);
|
|
||||||
msg.msg_flags = 0;
|
|
||||||
cmsglen = 0;
|
|
||||||
|
|
||||||
#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));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_LINUX_TIMESTAMPING
|
#ifdef HAVE_LINUX_TIMESTAMPING
|
||||||
if (process_tx)
|
if (process_tx)
|
||||||
cmsglen = NIO_Linux_RequestTxTimestamp(&msg, cmsglen, local_addr->sock_fd);
|
NIO_Linux_RequestTxTimestamp(&message, local_addr->sock_fd);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
msg.msg_controllen = cmsglen;
|
if (!SCK_SendMessage(local_addr->sock_fd, &message, 0))
|
||||||
/* This is apparently required on some systems */
|
|
||||||
if (!cmsglen)
|
|
||||||
msg.msg_control = NULL;
|
|
||||||
|
|
||||||
if (sendmsg(local_addr->sock_fd, &msg, 0) < 0) {
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "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));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpIO, "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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
6
ntp_io.h
6
ntp_io.h
@@ -53,6 +53,12 @@ extern void NIO_CloseServerSocket(int sock_fd);
|
|||||||
/* Function to check if socket is a server socket */
|
/* Function to check if socket is a server socket */
|
||||||
extern int NIO_IsServerSocket(int sock_fd);
|
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 */
|
/* Function to transmit a packet */
|
||||||
extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
extern int NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||||
NTP_Local_Address *local_addr, int length, int process_tx);
|
NTP_Local_Address *local_addr, int length, int process_tx);
|
||||||
|
|||||||
607
ntp_io_linux.c
607
ntp_io_linux.c
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
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
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -29,10 +29,8 @@
|
|||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#include <linux/errqueue.h>
|
|
||||||
#include <linux/ethtool.h>
|
#include <linux/ethtool.h>
|
||||||
#include <linux/net_tstamp.h>
|
#include <linux/net_tstamp.h>
|
||||||
#include <linux/ptp_clock.h>
|
|
||||||
#include <linux/sockios.h>
|
#include <linux/sockios.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
|
||||||
@@ -46,26 +44,23 @@
|
|||||||
#include "ntp_io_linux.h"
|
#include "ntp_io_linux.h"
|
||||||
#include "ntp_sources.h"
|
#include "ntp_sources.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "sys_linux.h"
|
#include "sys_linux.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
union sockaddr_in46 {
|
|
||||||
struct sockaddr_in in4;
|
|
||||||
#ifdef FEAT_IPV6
|
|
||||||
struct sockaddr_in6 in6;
|
|
||||||
#endif
|
|
||||||
struct sockaddr u;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Interface {
|
struct Interface {
|
||||||
char name[IF_NAMESIZE];
|
char name[IF_NAMESIZE];
|
||||||
int if_index;
|
int if_index;
|
||||||
int phc_fd;
|
int phc_fd;
|
||||||
|
int phc_mode;
|
||||||
|
int phc_nocrossts;
|
||||||
/* Link speed in mbit/s */
|
/* Link speed in mbit/s */
|
||||||
int link_speed;
|
int link_speed;
|
||||||
/* Start of UDP data at layer 2 for IPv4 and IPv6 */
|
/* Start of UDP data at layer 2 for IPv4 and IPv6 */
|
||||||
int l2_udp4_ntp_start;
|
int l2_udp4_ntp_start;
|
||||||
int l2_udp6_ntp_start;
|
int l2_udp6_ntp_start;
|
||||||
|
/* Precision of PHC readings */
|
||||||
|
double precision;
|
||||||
/* Compensation of errors in TX and RX timestamping */
|
/* Compensation of errors in TX and RX timestamping */
|
||||||
double tx_comp;
|
double tx_comp;
|
||||||
double rx_comp;
|
double rx_comp;
|
||||||
@@ -75,6 +70,9 @@ struct Interface {
|
|||||||
/* Number of PHC readings per HW clock sample */
|
/* Number of PHC readings per HW clock sample */
|
||||||
#define PHC_READINGS 10
|
#define PHC_READINGS 10
|
||||||
|
|
||||||
|
/* Minimum interval between PHC readings */
|
||||||
|
#define MIN_PHC_POLL -6
|
||||||
|
|
||||||
/* Maximum acceptable offset between HW and daemon/kernel timestamp */
|
/* Maximum acceptable offset between HW and daemon/kernel timestamp */
|
||||||
#define MAX_TS_DELAY 1.0
|
#define MAX_TS_DELAY 1.0
|
||||||
|
|
||||||
@@ -88,40 +86,61 @@ static int ts_tx_flags;
|
|||||||
/* Flag indicating the socket options can't be changed in control messages */
|
/* Flag indicating the socket options can't be changed in control messages */
|
||||||
static int permanent_ts_options;
|
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
|
static int
|
||||||
add_interface(const char *name, double tx_comp, double rx_comp)
|
add_interface(CNF_HwTsInterface *conf_iface)
|
||||||
{
|
{
|
||||||
struct ethtool_ts_info ts_info;
|
struct ethtool_ts_info ts_info;
|
||||||
struct hwtstamp_config ts_config;
|
struct hwtstamp_config ts_config;
|
||||||
struct ifreq req;
|
struct ifreq req;
|
||||||
int sock_fd, if_index, phc_index, phc_fd;
|
int sock_fd, if_index, phc_fd, req_hwts_flags, rx_filter;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
struct Interface *iface;
|
struct Interface *iface;
|
||||||
char phc_path[64];
|
|
||||||
|
|
||||||
/* Check if the interface was not already added */
|
/* Check if the interface was not already added */
|
||||||
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
||||||
if (!strcmp(name, ((struct Interface *)ARR_GetElement(interfaces, i))->name))
|
if (!strcmp(conf_iface->name, ((struct Interface *)ARR_GetElement(interfaces, i))->name))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
sock_fd = SCK_OpenUdpSocket(NULL, NULL, 0);
|
||||||
if (sock_fd < 0)
|
if (sock_fd < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
memset(&req, 0, sizeof (req));
|
memset(&req, 0, sizeof (req));
|
||||||
memset(&ts_info, 0, sizeof (ts_info));
|
memset(&ts_info, 0, sizeof (ts_info));
|
||||||
|
|
||||||
if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", name) >= sizeof (req.ifr_name)) {
|
if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", conf_iface->name) >=
|
||||||
close(sock_fd);
|
sizeof (req.ifr_name)) {
|
||||||
|
SCK_CloseSocket(sock_fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(sock_fd, SIOCGIFINDEX, &req)) {
|
if (ioctl(sock_fd, SIOCGIFINDEX, &req)) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "ioctl(%s) failed : %s", "SIOCGIFINDEX", strerror(errno));
|
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCGIFINDEX", strerror(errno));
|
||||||
close(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,53 +150,100 @@ add_interface(const char *name, double tx_comp, double rx_comp)
|
|||||||
req.ifr_data = (char *)&ts_info;
|
req.ifr_data = (char *)&ts_info;
|
||||||
|
|
||||||
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
||||||
close(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req_hwts_flags = SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_TX_HARDWARE |
|
||||||
|
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);
|
||||||
|
SCK_CloseSocket(sock_fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
if (ts_info.rx_filters & (1 << HWTSTAMP_FILTER_ALL))
|
||||||
|
rx_filter = HWTSTAMP_FILTER_ALL;
|
||||||
|
else
|
||||||
|
rx_filter = HWTSTAMP_FILTER_NONE;
|
||||||
|
break;
|
||||||
|
case CNF_HWTS_RXFILTER_NONE:
|
||||||
|
rx_filter = HWTSTAMP_FILTER_NONE;
|
||||||
|
break;
|
||||||
|
#ifdef HAVE_LINUX_TIMESTAMPING_RXFILTER_NTP
|
||||||
|
case CNF_HWTS_RXFILTER_NTP:
|
||||||
|
rx_filter = HWTSTAMP_FILTER_NTP_ALL;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
rx_filter = HWTSTAMP_FILTER_ALL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ts_config.flags = 0;
|
ts_config.flags = 0;
|
||||||
ts_config.tx_type = HWTSTAMP_TX_ON;
|
ts_config.tx_type = HWTSTAMP_TX_ON;
|
||||||
ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
|
ts_config.rx_filter = rx_filter;
|
||||||
req.ifr_data = (char *)&ts_config;
|
req.ifr_data = (char *)&ts_config;
|
||||||
|
|
||||||
if (ioctl(sock_fd, SIOCSHWTSTAMP, &req)) {
|
if (ioctl(sock_fd, SIOCSHWTSTAMP, &req)) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "ioctl(%s) failed : %s", "SIOCSHWTSTAMP", strerror(errno));
|
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCSHWTSTAMP", strerror(errno));
|
||||||
close(sock_fd);
|
|
||||||
return 0;
|
/* 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_index = ts_info.phc_index;
|
|
||||||
|
|
||||||
if (snprintf(phc_path, sizeof (phc_path), "/dev/ptp%d", phc_index) >= sizeof (phc_path))
|
phc_fd = SYS_Linux_OpenPHC(NULL, ts_info.phc_index);
|
||||||
|
if (phc_fd < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
phc_fd = open(phc_path, O_RDONLY);
|
|
||||||
if (phc_fd < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_NtpIOLinux, "Could not open %s : %s", phc_path, strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
UTI_FdSetCloexec(phc_fd);
|
|
||||||
|
|
||||||
iface = ARR_GetNewElement(interfaces);
|
iface = ARR_GetNewElement(interfaces);
|
||||||
|
|
||||||
snprintf(iface->name, sizeof (iface->name), "%s", name);
|
snprintf(iface->name, sizeof (iface->name), "%s", conf_iface->name);
|
||||||
iface->if_index = if_index;
|
iface->if_index = if_index;
|
||||||
iface->phc_fd = phc_fd;
|
iface->phc_fd = phc_fd;
|
||||||
|
iface->phc_mode = 0;
|
||||||
|
iface->phc_nocrossts = conf_iface->nocrossts;
|
||||||
|
|
||||||
/* Start with 1 gbit and no VLANs or IPv4/IPv6 options */
|
/* Start with 1 gbit and no VLANs or IPv4/IPv6 options */
|
||||||
iface->link_speed = 1000;
|
iface->link_speed = 1000;
|
||||||
iface->l2_udp4_ntp_start = 42;
|
iface->l2_udp4_ntp_start = 42;
|
||||||
iface->l2_udp6_ntp_start = 62;
|
iface->l2_udp6_ntp_start = 62;
|
||||||
|
|
||||||
iface->tx_comp = tx_comp;
|
iface->precision = conf_iface->precision;
|
||||||
iface->rx_comp = rx_comp;
|
iface->tx_comp = conf_iface->tx_comp;
|
||||||
|
iface->rx_comp = conf_iface->rx_comp;
|
||||||
|
|
||||||
iface->clock = HCL_CreateInstance();
|
iface->clock = HCL_CreateInstance(conf_iface->min_samples, conf_iface->max_samples,
|
||||||
|
UTI_Log2ToDouble(MAX(conf_iface->minpoll, MIN_PHC_POLL)));
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "Enabled HW timestamping on %s", name);
|
LOG(LOGS_INFO, "Enabled HW timestamping %son %s",
|
||||||
|
ts_config.rx_filter == HWTSTAMP_FILTER_NONE ? "(TX only) " : "", iface->name);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -185,18 +251,22 @@ add_interface(const char *name, double tx_comp, double rx_comp)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
add_all_interfaces(double tx_comp, double rx_comp)
|
add_all_interfaces(CNF_HwTsInterface *conf_iface_all)
|
||||||
{
|
{
|
||||||
|
CNF_HwTsInterface conf_iface;
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
conf_iface = *conf_iface_all;
|
||||||
|
|
||||||
if (getifaddrs(&ifaddr)) {
|
if (getifaddrs(&ifaddr)) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "getifaddrs() failed : %s", strerror(errno));
|
DEBUG_LOG("getifaddrs() failed : %s", strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (r = 0, ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
for (r = 0, ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
||||||
if (add_interface(ifa->ifa_name, tx_comp, rx_comp))
|
conf_iface.name = ifa->ifa_name;
|
||||||
|
if (add_interface(&conf_iface))
|
||||||
r = 1;
|
r = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,9 +283,9 @@ update_interface_speed(struct Interface *iface)
|
|||||||
{
|
{
|
||||||
struct ethtool_cmd cmd;
|
struct ethtool_cmd cmd;
|
||||||
struct ifreq req;
|
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, 0);
|
||||||
if (sock_fd < 0)
|
if (sock_fd < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -227,14 +297,60 @@ update_interface_speed(struct Interface *iface)
|
|||||||
req.ifr_data = (char *)&cmd;
|
req.ifr_data = (char *)&cmd;
|
||||||
|
|
||||||
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
if (ioctl(sock_fd, SIOCETHTOOL, &req)) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
DEBUG_LOG("ioctl(%s) failed : %s", "SIOCETHTOOL", strerror(errno));
|
||||||
close(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
return;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
#if defined(HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO) || defined(HAVE_LINUX_TIMESTAMPING_OPT_TX_SWHW)
|
||||||
|
static int
|
||||||
|
check_timestamping_option(int option)
|
||||||
|
{
|
||||||
|
int sock_fd;
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenUdpSocket(NULL, NULL, 0);
|
||||||
|
if (sock_fd < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, option)) {
|
||||||
|
SCK_CloseSocket(sock_fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SCK_CloseSocket(sock_fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
open_dummy_socket(void)
|
||||||
|
{
|
||||||
|
int sock_fd, events = 0;
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenUdpSocket(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -242,8 +358,7 @@ update_interface_speed(struct Interface *iface)
|
|||||||
void
|
void
|
||||||
NIO_Linux_Initialise(void)
|
NIO_Linux_Initialise(void)
|
||||||
{
|
{
|
||||||
double tx_comp, rx_comp;
|
CNF_HwTsInterface *conf_iface;
|
||||||
char *name;
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int hwts;
|
int hwts;
|
||||||
|
|
||||||
@@ -252,28 +367,36 @@ NIO_Linux_Initialise(void)
|
|||||||
/* Enable HW timestamping on specified interfaces. If "*" was specified, try
|
/* Enable HW timestamping on specified interfaces. If "*" was specified, try
|
||||||
all interfaces. If no interface was specified, enable SW timestamping. */
|
all interfaces. If no interface was specified, enable SW timestamping. */
|
||||||
|
|
||||||
for (i = hwts = 0; CNF_GetHwTsInterface(i, &name, &tx_comp, &rx_comp); i++) {
|
for (i = hwts = 0; CNF_GetHwTsInterface(i, &conf_iface); i++) {
|
||||||
if (!strcmp("*", name))
|
if (!strcmp("*", conf_iface->name))
|
||||||
continue;
|
continue;
|
||||||
if (!add_interface(name, tx_comp, rx_comp))
|
if (!add_interface(conf_iface))
|
||||||
LOG_FATAL(LOGF_NtpIO, "Could not enable HW timestamping on %s", name);
|
LOG_FATAL("Could not enable HW timestamping on %s", conf_iface->name);
|
||||||
hwts = 1;
|
hwts = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; CNF_GetHwTsInterface(i, &name, &tx_comp, &rx_comp); i++) {
|
for (i = 0; CNF_GetHwTsInterface(i, &conf_iface); i++) {
|
||||||
if (strcmp("*", name))
|
if (strcmp("*", conf_iface->name))
|
||||||
continue;
|
continue;
|
||||||
if (add_all_interfaces(tx_comp, rx_comp))
|
if (add_all_interfaces(conf_iface))
|
||||||
hwts = 1;
|
hwts = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ts_flags = SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE;
|
||||||
|
ts_tx_flags = SOF_TIMESTAMPING_TX_SOFTWARE;
|
||||||
|
|
||||||
if (hwts) {
|
if (hwts) {
|
||||||
ts_flags = SOF_TIMESTAMPING_RAW_HARDWARE | SOF_TIMESTAMPING_RX_HARDWARE;
|
ts_flags |= SOF_TIMESTAMPING_RAW_HARDWARE | SOF_TIMESTAMPING_RX_HARDWARE;
|
||||||
ts_tx_flags = SOF_TIMESTAMPING_TX_HARDWARE;
|
ts_tx_flags |= SOF_TIMESTAMPING_TX_HARDWARE;
|
||||||
} else {
|
#ifdef HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO
|
||||||
ts_flags = SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE;
|
if (check_timestamping_option(SOF_TIMESTAMPING_OPT_PKTINFO))
|
||||||
ts_tx_flags = SOF_TIMESTAMPING_TX_SOFTWARE;
|
ts_flags |= SOF_TIMESTAMPING_OPT_PKTINFO;
|
||||||
|
#endif
|
||||||
|
#ifdef HAVE_LINUX_TIMESTAMPING_OPT_TX_SWHW
|
||||||
|
if (check_timestamping_option(SOF_TIMESTAMPING_OPT_TX_SWHW))
|
||||||
|
ts_flags |= SOF_TIMESTAMPING_OPT_TX_SWHW;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enable IP_PKTINFO in messages looped back to the error queue */
|
/* Enable IP_PKTINFO in messages looped back to the error queue */
|
||||||
@@ -281,6 +404,10 @@ NIO_Linux_Initialise(void)
|
|||||||
|
|
||||||
/* Kernels before 4.7 ignore timestamping flags set in control messages */
|
/* Kernels before 4.7 ignore timestamping flags set in control messages */
|
||||||
permanent_ts_options = !SYS_Linux_CheckKernelVersion(4, 7);
|
permanent_ts_options = !SYS_Linux_CheckKernelVersion(4, 7);
|
||||||
|
|
||||||
|
monitored_socket = INVALID_SOCK_FD;
|
||||||
|
suspended_socket = INVALID_SOCK_FD;
|
||||||
|
dummy_rxts_socket = INVALID_SOCK_FD;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -291,6 +418,9 @@ NIO_Linux_Finalise(void)
|
|||||||
struct Interface *iface;
|
struct Interface *iface;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
|
if (dummy_rxts_socket != INVALID_SOCK_FD)
|
||||||
|
SCK_CloseSocket(dummy_rxts_socket);
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
for (i = 0; i < ARR_GetSize(interfaces); i++) {
|
||||||
iface = ARR_GetElement(interfaces, i);
|
iface = ARR_GetElement(interfaces, i);
|
||||||
HCL_DestroyInstance(iface->clock);
|
HCL_DestroyInstance(iface->clock);
|
||||||
@@ -319,14 +449,12 @@ NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events)
|
|||||||
if (client_only || permanent_ts_options)
|
if (client_only || permanent_ts_options)
|
||||||
flags |= ts_tx_flags;
|
flags |= ts_tx_flags;
|
||||||
|
|
||||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_SELECT_ERR_QUEUE, &val, sizeof (val)) < 0) {
|
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_SELECT_ERR_QUEUE, val)) {
|
||||||
LOG(LOGS_ERR, LOGF_NtpIOLinux, "Could not set %s socket option", "SO_SELECT_ERR_QUEUE");
|
|
||||||
ts_flags = 0;
|
ts_flags = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof (flags)) < 0) {
|
if (!SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, flags)) {
|
||||||
LOG(LOGS_ERR, LOGF_NtpIOLinux, "Could not set %s socket option", "SO_TIMESTAMPING");
|
|
||||||
ts_flags = 0;
|
ts_flags = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -337,64 +465,69 @@ NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static void
|
||||||
get_phc_sample(int phc_fd, struct timespec *phc_ts, struct timespec *local_ts, double *p_delay)
|
resume_socket(int sock_fd)
|
||||||
{
|
{
|
||||||
struct ptp_sys_offset sys_off;
|
if (monitored_socket == sock_fd)
|
||||||
struct timespec ts1, ts2, ts3, phc_tss[PHC_READINGS], sys_tss[PHC_READINGS];
|
monitored_socket = INVALID_SOCK_FD;
|
||||||
double min_delay = 0.0, delays[PHC_READINGS], phc_sum, local_sum, local_prec;
|
|
||||||
int i, n;
|
|
||||||
|
|
||||||
/* Silence valgrind */
|
if (sock_fd == INVALID_SOCK_FD || sock_fd != suspended_socket)
|
||||||
memset(&sys_off, 0, sizeof (sys_off));
|
return;
|
||||||
|
|
||||||
sys_off.n_samples = PHC_READINGS;
|
suspended_socket = INVALID_SOCK_FD;
|
||||||
|
|
||||||
if (ioctl(phc_fd, PTP_SYS_OFFSET, &sys_off)) {
|
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_INPUT, 1);
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "ioctl(%s) failed : %s", "PTP_SYS_OFFSET", strerror(errno));
|
|
||||||
|
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;
|
return 0;
|
||||||
|
|
||||||
|
if (event == SCH_FILE_INPUT) {
|
||||||
|
suspend_socket(monitored_socket);
|
||||||
|
monitored_socket = INVALID_SOCK_FD;
|
||||||
|
|
||||||
|
/* Don't process the message yet */
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < PHC_READINGS; i++) {
|
return 0;
|
||||||
ts1.tv_sec = sys_off.ts[i * 2].sec;
|
|
||||||
ts1.tv_nsec = sys_off.ts[i * 2].nsec;
|
|
||||||
ts2.tv_sec = sys_off.ts[i * 2 + 1].sec;
|
|
||||||
ts2.tv_nsec = sys_off.ts[i * 2 + 1].nsec;
|
|
||||||
ts3.tv_sec = sys_off.ts[i * 2 + 2].sec;
|
|
||||||
ts3.tv_nsec = sys_off.ts[i * 2 + 2].nsec;
|
|
||||||
|
|
||||||
sys_tss[i] = ts1;
|
|
||||||
phc_tss[i] = ts2;
|
|
||||||
delays[i] = UTI_DiffTimespecsToDouble(&ts3, &ts1);
|
|
||||||
|
|
||||||
if (delays[i] <= 0.0)
|
|
||||||
/* Step in the middle of a PHC reading? */
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (!i || delays[i] < min_delay)
|
|
||||||
min_delay = delays[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
local_prec = LCL_GetSysPrecisionAsQuantum();
|
|
||||||
|
|
||||||
/* Combine best readings */
|
|
||||||
for (i = n = 0, phc_sum = local_sum = 0.0; i < PHC_READINGS; i++) {
|
|
||||||
if (delays[i] > min_delay + local_prec)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
phc_sum += UTI_DiffTimespecsToDouble(&phc_tss[i], &phc_tss[0]);
|
|
||||||
local_sum += UTI_DiffTimespecsToDouble(&sys_tss[i], &sys_tss[0]) + delays[i] / 2.0;
|
|
||||||
n++;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(n);
|
|
||||||
|
|
||||||
UTI_AddDoubleToTimespec(&phc_tss[0], phc_sum / n, phc_ts);
|
|
||||||
UTI_AddDoubleToTimespec(&sys_tss[0], local_sum / n, &ts1);
|
|
||||||
LCL_CookTime(&ts1, local_ts, NULL);
|
|
||||||
*p_delay = min_delay;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -420,52 +553,60 @@ get_interface(int if_index)
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
|
process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
|
||||||
NTP_Local_Timestamp *local_ts, int rx_ntp_length, int family)
|
NTP_Local_Timestamp *local_ts, int rx_ntp_length, int family,
|
||||||
|
int l2_length)
|
||||||
{
|
{
|
||||||
struct timespec sample_phc_ts, sample_local_ts, ts;
|
struct timespec sample_phc_ts, sample_sys_ts, sample_local_ts, ts;
|
||||||
double sample_delay, rx_correction, ts_delay, err;
|
double rx_correction, ts_delay, phc_err, local_err;
|
||||||
int l2_length;
|
|
||||||
|
|
||||||
if (HCL_NeedsNewSample(iface->clock, &local_ts->ts)) {
|
if (HCL_NeedsNewSample(iface->clock, &local_ts->ts)) {
|
||||||
if (!get_phc_sample(iface->phc_fd, &sample_phc_ts, &sample_local_ts, &sample_delay))
|
if (!SYS_Linux_GetPHCSample(iface->phc_fd, iface->phc_nocrossts, iface->precision,
|
||||||
|
&iface->phc_mode, &sample_phc_ts, &sample_sys_ts,
|
||||||
|
&phc_err))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
LCL_CookTime(&sample_sys_ts, &sample_local_ts, &local_err);
|
||||||
HCL_AccumulateSample(iface->clock, &sample_phc_ts, &sample_local_ts,
|
HCL_AccumulateSample(iface->clock, &sample_phc_ts, &sample_local_ts,
|
||||||
sample_delay / 2.0);
|
phc_err + local_err);
|
||||||
|
|
||||||
update_interface_speed(iface);
|
update_interface_speed(iface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We need to transpose RX timestamps as hardware timestamps are normally
|
/* We need to transpose RX timestamps as hardware timestamps are normally
|
||||||
preamble timestamps and RX timestamps in NTP are supposed to be trailer
|
preamble timestamps and RX timestamps in NTP are supposed to be trailer
|
||||||
timestamps. Without raw sockets we don't know the length of the packet
|
timestamps. If we don't know the length of the packet at layer 2, we
|
||||||
at layer 2, so we make an assumption that UDP data start at the same
|
make an assumption that UDP data start at the same position as in the
|
||||||
position as in the last transmitted packet which had a HW TX timestamp. */
|
last transmitted packet which had a HW TX timestamp. */
|
||||||
if (rx_ntp_length && iface->link_speed) {
|
if (rx_ntp_length && iface->link_speed) {
|
||||||
l2_length = (family == IPADDR_INET4 ? iface->l2_udp4_ntp_start :
|
if (!l2_length)
|
||||||
iface->l2_udp6_ntp_start) + rx_ntp_length + 4;
|
l2_length = (family == IPADDR_INET4 ? iface->l2_udp4_ntp_start :
|
||||||
|
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);
|
rx_correction = l2_length / (1.0e6 / 8 * iface->link_speed);
|
||||||
|
|
||||||
UTI_AddDoubleToTimespec(hw_ts, rx_correction, hw_ts);
|
UTI_AddDoubleToTimespec(hw_ts, rx_correction, hw_ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rx_ntp_length && iface->tx_comp)
|
if (!HCL_CookTime(iface->clock, hw_ts, &ts, &local_err))
|
||||||
UTI_AddDoubleToTimespec(hw_ts, iface->tx_comp, hw_ts);
|
|
||||||
else if (rx_ntp_length && iface->rx_comp)
|
|
||||||
UTI_AddDoubleToTimespec(hw_ts, -iface->rx_comp, hw_ts);
|
|
||||||
|
|
||||||
if (!HCL_CookTime(iface->clock, hw_ts, &ts, &err))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (!rx_ntp_length && iface->tx_comp)
|
||||||
|
UTI_AddDoubleToTimespec(&ts, iface->tx_comp, &ts);
|
||||||
|
else if (rx_ntp_length && iface->rx_comp)
|
||||||
|
UTI_AddDoubleToTimespec(&ts, -iface->rx_comp, &ts);
|
||||||
|
|
||||||
ts_delay = UTI_DiffTimespecsToDouble(&local_ts->ts, &ts);
|
ts_delay = UTI_DiffTimespecsToDouble(&local_ts->ts, &ts);
|
||||||
|
|
||||||
if (fabs(ts_delay) > MAX_TS_DELAY) {
|
if (fabs(ts_delay) > MAX_TS_DELAY) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "Unacceptable timestamp delay %.9f", ts_delay);
|
DEBUG_LOG("Unacceptable timestamp delay %.9f", ts_delay);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
local_ts->ts = ts;
|
local_ts->ts = ts;
|
||||||
local_ts->err = err;
|
local_ts->err = local_err;
|
||||||
local_ts->source = NTP_TS_HARDWARE;
|
local_ts->source = NTP_TS_HARDWARE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,7 +618,6 @@ static int
|
|||||||
extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
||||||
{
|
{
|
||||||
unsigned char *msg_start = msg;
|
unsigned char *msg_start = msg;
|
||||||
union sockaddr_in46 addr;
|
|
||||||
|
|
||||||
remote_addr->ip_addr.family = IPADDR_UNSPEC;
|
remote_addr->ip_addr.family = IPADDR_UNSPEC;
|
||||||
remote_addr->port = 0;
|
remote_addr->port = 0;
|
||||||
@@ -500,31 +640,60 @@ extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
|||||||
/* Parse destination address and port from IPv4/IPv6 and UDP headers */
|
/* Parse destination address and port from IPv4/IPv6 and UDP headers */
|
||||||
if (len >= 20 && msg[0] >> 4 == 4) {
|
if (len >= 20 && msg[0] >> 4 == 4) {
|
||||||
int ihl = (msg[0] & 0xf) * 4;
|
int ihl = (msg[0] & 0xf) * 4;
|
||||||
|
uint32_t addr;
|
||||||
|
|
||||||
if (len < ihl + 8 || msg[9] != 17)
|
if (len < ihl + 8 || msg[9] != 17)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
memcpy(&addr.in4.sin_addr.s_addr, msg + 16, sizeof (uint32_t));
|
memcpy(&addr, msg + 16, sizeof (addr));
|
||||||
addr.in4.sin_port = *(uint16_t *)(msg + ihl + 2);
|
remote_addr->ip_addr.addr.in4 = ntohl(addr);
|
||||||
addr.in4.sin_family = AF_INET;
|
remote_addr->port = ntohs(*(uint16_t *)(msg + ihl + 2));
|
||||||
|
remote_addr->ip_addr.family = IPADDR_INET4;
|
||||||
len -= ihl + 8, msg += ihl + 8;
|
len -= ihl + 8, msg += ihl + 8;
|
||||||
#ifdef FEAT_IPV6
|
#ifdef FEAT_IPV6
|
||||||
} else if (len >= 48 && msg[0] >> 4 == 6) {
|
} else if (len >= 48 && msg[0] >> 4 == 6) {
|
||||||
/* IPv6 extension headers are not supported */
|
int eh_len, next_header = msg[6];
|
||||||
if (msg[6] != 17)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
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));
|
||||||
addr.in6.sin6_port = *(uint16_t *)(msg + 40 + 2);
|
len -= 40, msg += 40;
|
||||||
addr.in6.sin6_family = AF_INET6;
|
|
||||||
len -= 48, msg += 48;
|
/* Skip IPv6 extension headers if present */
|
||||||
|
while (next_header != 17) {
|
||||||
|
switch (next_header) {
|
||||||
|
case 44: /* Fragment Header */
|
||||||
|
/* Process only the first fragment */
|
||||||
|
if (ntohs(*(uint16_t *)(msg + 2)) >> 3 != 0)
|
||||||
|
return 0;
|
||||||
|
eh_len = 8;
|
||||||
|
break;
|
||||||
|
case 0: /* Hop-by-Hop Options */
|
||||||
|
case 43: /* Routing Header */
|
||||||
|
case 60: /* Destination Options */
|
||||||
|
case 135: /* Mobility Header */
|
||||||
|
eh_len = 8 * (msg[1] + 1);
|
||||||
|
break;
|
||||||
|
case 51: /* Authentication Header */
|
||||||
|
eh_len = 4 * (msg[1] + 2);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eh_len < 8 || len < eh_len + 8)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
next_header = msg[0];
|
||||||
|
len -= eh_len, msg += eh_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
remote_addr->port = ntohs(*(uint16_t *)(msg + 2));
|
||||||
|
remote_addr->ip_addr.family = IPADDR_INET6;
|
||||||
|
len -= 8, msg += 8;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTI_SockaddrToIPAndPort(&addr.u, &remote_addr->ip_addr, &remote_addr->port);
|
|
||||||
|
|
||||||
/* Move the message to fix alignment of its fields */
|
/* Move the message to fix alignment of its fields */
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
memmove(msg_start, msg, len);
|
memmove(msg_start, msg, len);
|
||||||
@@ -535,50 +704,47 @@ extract_udp_data(unsigned char *msg, NTP_Remote_Address *remote_addr, int len)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr,
|
||||||
NTP_Local_Timestamp *local_ts, struct msghdr *hdr,
|
NTP_Local_Timestamp *local_ts, int event)
|
||||||
int length, int sock_fd, int if_index)
|
|
||||||
{
|
{
|
||||||
struct Interface *iface;
|
struct Interface *iface;
|
||||||
struct cmsghdr *cmsg;
|
int is_tx, ts_if_index, l2_length;
|
||||||
int is_tx, l2_length;
|
|
||||||
|
|
||||||
is_tx = hdr->msg_flags & MSG_ERRQUEUE;
|
is_tx = event == SCH_FILE_EXCEPTION;
|
||||||
iface = NULL;
|
iface = NULL;
|
||||||
|
|
||||||
for (cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) {
|
ts_if_index = message->timestamp.if_index;
|
||||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING) {
|
if (ts_if_index == INVALID_IF_INDEX)
|
||||||
struct scm_timestamping ts3;
|
ts_if_index = message->if_index;
|
||||||
|
l2_length = message->timestamp.l2_length;
|
||||||
|
|
||||||
memcpy(&ts3, CMSG_DATA(cmsg), sizeof (ts3));
|
if (!UTI_IsZeroTimespec(&message->timestamp.hw)) {
|
||||||
|
iface = get_interface(ts_if_index);
|
||||||
if (!UTI_IsZeroTimespec(&ts3.ts[0])) {
|
if (iface) {
|
||||||
LCL_CookTime(&ts3.ts[0], &local_ts->ts, &local_ts->err);
|
process_hw_timestamp(iface, &message->timestamp.hw, local_ts, !is_tx ? message->length : 0,
|
||||||
local_ts->source = NTP_TS_KERNEL;
|
message->remote_addr.ip.ip_addr.family, l2_length);
|
||||||
} else if (!UTI_IsZeroTimespec(&ts3.ts[2])) {
|
} else {
|
||||||
iface = get_interface(if_index);
|
DEBUG_LOG("HW clock not found for interface %d", ts_if_index);
|
||||||
if (iface) {
|
|
||||||
process_hw_timestamp(iface, &ts3.ts[2], local_ts, !is_tx ? length : 0,
|
|
||||||
remote_addr->ip_addr.family);
|
|
||||||
} else {
|
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "HW clock not found for interface %d", if_index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) ||
|
/* If a HW transmit timestamp was received, resume processing
|
||||||
(cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_RECVERR)) {
|
of non-error messages on this socket */
|
||||||
struct sock_extended_err err;
|
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 ||
|
/* If the kernel is slow with enabling RX timestamping, open a dummy
|
||||||
err.ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
|
socket to keep the kernel RX timestamping permanently enabled */
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "Unknown extended error");
|
if (!is_tx && local_ts->source == NTP_TS_DAEMON && ts_flags) {
|
||||||
/* Drop the message */
|
DEBUG_LOG("Missing kernel RX timestamp");
|
||||||
return 1;
|
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 */
|
/* Return the message if it's not received from the error queue */
|
||||||
@@ -588,59 +754,60 @@ 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
|
/* 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
|
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. */
|
currently doesn't seem to be a better way to get them both. */
|
||||||
l2_length = length;
|
l2_length = message->length;
|
||||||
length = extract_udp_data(hdr->msg_iov[0].iov_base, remote_addr, length);
|
message->length = extract_udp_data(message->data, &message->remote_addr.ip, message->length);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%d",
|
DEBUG_LOG("Extracted message for %s fd=%d len=%u",
|
||||||
l2_length, length, UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
|
UTI_IPSockAddrToString(&message->remote_addr.ip),
|
||||||
sock_fd, if_index, local_ts->source);
|
local_addr->sock_fd, message->length);
|
||||||
|
|
||||||
/* Update assumed position of UDP data at layer 2 for next received packet */
|
/* Update assumed position of UDP data at layer 2 for next received packet */
|
||||||
if (iface && length) {
|
if (iface && message->length) {
|
||||||
if (remote_addr->ip_addr.family == IPADDR_INET4)
|
if (message->remote_addr.ip.ip_addr.family == IPADDR_INET4)
|
||||||
iface->l2_udp4_ntp_start = l2_length - length;
|
iface->l2_udp4_ntp_start = l2_length - message->length;
|
||||||
else if (remote_addr->ip_addr.family == IPADDR_INET6)
|
else if (message->remote_addr.ip.ip_addr.family == IPADDR_INET6)
|
||||||
iface->l2_udp6_ntp_start = l2_length - length;
|
iface->l2_udp6_ntp_start = l2_length - message->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Drop the message if HW timestamp is missing or its processing failed */
|
/* Drop the message if it has no timestamp or its processing failed */
|
||||||
if ((ts_flags & SOF_TIMESTAMPING_RAW_HARDWARE) && local_ts->source != NTP_TS_HARDWARE) {
|
if (local_ts->source == NTP_TS_DAEMON) {
|
||||||
DEBUG_LOG(LOGF_NtpIOLinux, "Missing HW timestamp");
|
DEBUG_LOG("Missing TX timestamp");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length < NTP_NORMAL_PACKET_LENGTH)
|
if (message->length < NTP_HEADER_LENGTH)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
NSR_ProcessTx(remote_addr, local_addr, local_ts,
|
NSR_ProcessTx(&message->remote_addr.ip, local_addr, local_ts, message->data, message->length);
|
||||||
(NTP_Packet *)hdr->msg_iov[0].iov_base, length);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
void
|
||||||
NIO_Linux_RequestTxTimestamp(struct msghdr *msg, int cmsglen, int sock_fd)
|
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 */
|
/* Check if TX timestamping is disabled on this socket */
|
||||||
if (permanent_ts_options || !NIO_IsServerSocket(sock_fd))
|
if (permanent_ts_options || !NIO_IsServerSocket(sock_fd))
|
||||||
return cmsglen;
|
return;
|
||||||
|
|
||||||
/* Add control message that will enable TX timestamping for this message.
|
message->timestamp.tx_flags = ts_tx_flags;
|
||||||
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));
|
void
|
||||||
|
NIO_Linux_NotifySocketClosing(int sock_fd)
|
||||||
cmsg->cmsg_level = SOL_SOCKET;
|
{
|
||||||
cmsg->cmsg_type = SO_TIMESTAMPING;
|
resume_socket(sock_fd);
|
||||||
cmsg->cmsg_len = CMSG_LEN(sizeof (ts_tx_flags));
|
|
||||||
|
|
||||||
memcpy(CMSG_DATA(cmsg), &ts_tx_flags, sizeof (ts_tx_flags));
|
|
||||||
|
|
||||||
return cmsglen;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,14 +24,24 @@
|
|||||||
This is the header file for the Linux-specific NTP socket I/O bits.
|
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_Initialise(void);
|
||||||
|
|
||||||
extern void NIO_Linux_Finalise(void);
|
extern void NIO_Linux_Finalise(void);
|
||||||
|
|
||||||
extern int NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events);
|
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,
|
extern int NIO_Linux_ProcessEvent(int sock_fd, int event);
|
||||||
NTP_Local_Timestamp *local_ts, struct msghdr *hdr, int length,
|
|
||||||
int sock_fd, int if_index);
|
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
89
ntp_signd.c
89
ntp_signd.c
@@ -34,6 +34,7 @@
|
|||||||
#include "ntp_io.h"
|
#include "ntp_io.h"
|
||||||
#include "ntp_signd.h"
|
#include "ntp_signd.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/* Declarations per samba/source4/librpc/idl/ntp_signd.idl */
|
/* Declarations per samba/source4/librpc/idl/ntp_signd.idl */
|
||||||
@@ -90,7 +91,7 @@ static ARR_Instance queue;
|
|||||||
static unsigned int queue_head;
|
static unsigned int queue_head;
|
||||||
static unsigned int queue_tail;
|
static unsigned int queue_tail;
|
||||||
|
|
||||||
#define INVALID_SOCK_FD -1
|
#define INVALID_SOCK_FD (-6)
|
||||||
|
|
||||||
/* Unix domain socket connected to ntp_signd */
|
/* Unix domain socket connected to ntp_signd */
|
||||||
static int sock_fd;
|
static int sock_fd;
|
||||||
@@ -116,7 +117,7 @@ static void
|
|||||||
close_socket(void)
|
close_socket(void)
|
||||||
{
|
{
|
||||||
SCH_RemoveFileHandler(sock_fd);
|
SCH_RemoveFileHandler(sock_fd);
|
||||||
close(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
sock_fd = INVALID_SOCK_FD;
|
sock_fd = INVALID_SOCK_FD;
|
||||||
|
|
||||||
/* Empty the queue */
|
/* Empty the queue */
|
||||||
@@ -128,36 +129,24 @@ close_socket(void)
|
|||||||
static int
|
static int
|
||||||
open_socket(void)
|
open_socket(void)
|
||||||
{
|
{
|
||||||
struct sockaddr_un s;
|
char path[PATH_MAX];
|
||||||
|
|
||||||
if (sock_fd >= 0)
|
if (sock_fd != INVALID_SOCK_FD)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
if (snprintf(path, sizeof (path), "%s/socket", CNF_GetNtpSigndSocket()) >= sizeof (path)) {
|
||||||
|
DEBUG_LOG("signd socket path too long");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenUnixStreamSocket(path, NULL, 0);
|
||||||
if (sock_fd < 0) {
|
if (sock_fd < 0) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Could not open signd socket : %s", strerror(errno));
|
sock_fd = INVALID_SOCK_FD;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTI_FdSetCloexec(sock_fd);
|
|
||||||
SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_write_socket, NULL);
|
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)) {
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd socket path too long");
|
|
||||||
close_socket();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connect(sock_fd, (struct sockaddr *)&s, sizeof (s)) < 0) {
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Could not connect to signd : %s", strerror(errno));
|
|
||||||
close_socket();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Connected to signd");
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,25 +159,25 @@ process_response(SignInstance *inst)
|
|||||||
double delay;
|
double delay;
|
||||||
|
|
||||||
if (ntohs(inst->request.packet_id) != ntohl(inst->response.packet_id)) {
|
if (ntohs(inst->request.packet_id) != ntohl(inst->response.packet_id)) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Invalid response ID");
|
DEBUG_LOG("Invalid response ID");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ntohl(inst->response.op) != SIGNING_SUCCESS) {
|
if (ntohl(inst->response.op) != SIGNING_SUCCESS) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Signing failed");
|
DEBUG_LOG("Signing failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if the file descriptor is still valid */
|
/* Check if the file descriptor is still valid */
|
||||||
if (!NIO_IsServerSocket(inst->local_addr.sock_fd)) {
|
if (!NIO_IsServerSocket(inst->local_addr.sock_fd)) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Invalid NTP socket");
|
DEBUG_LOG("Invalid NTP socket");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SCH_GetLastEventTime(NULL, NULL, &ts);
|
SCH_GetLastEventTime(NULL, NULL, &ts);
|
||||||
delay = UTI_DiffTimespecsToDouble(&ts, &inst->request_ts);
|
delay = UTI_DiffTimespecsToDouble(&ts, &inst->request_ts);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Signing succeeded (delay %f)", delay);
|
DEBUG_LOG("Signing succeeded (delay %f)", delay);
|
||||||
|
|
||||||
/* Send the signed NTP packet */
|
/* Send the signed NTP packet */
|
||||||
NIO_SendPacket(&inst->response.signed_packet, &inst->remote_addr, &inst->local_addr,
|
NIO_SendPacket(&inst->response.signed_packet, &inst->remote_addr, &inst->local_addr,
|
||||||
@@ -218,16 +207,14 @@ read_write_socket(int sock_fd, int event, void *anything)
|
|||||||
if (!inst->sent)
|
if (!inst->sent)
|
||||||
SCH_GetLastEventTime(NULL, NULL, &inst->request_ts);
|
SCH_GetLastEventTime(NULL, NULL, &inst->request_ts);
|
||||||
|
|
||||||
s = send(sock_fd, (char *)&inst->request + inst->sent,
|
s = SCK_Send(sock_fd, (char *)&inst->request + inst->sent,
|
||||||
inst->request_length - inst->sent, 0);
|
inst->request_length - inst->sent, 0);
|
||||||
|
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd socket error: %s", strerror(errno));
|
|
||||||
close_socket();
|
close_socket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Sent %d bytes to signd", s);
|
|
||||||
inst->sent += s;
|
inst->sent += s;
|
||||||
|
|
||||||
/* Try again later if the request is not complete yet */
|
/* Try again later if the request is not complete yet */
|
||||||
@@ -235,31 +222,25 @@ read_write_socket(int sock_fd, int event, void *anything)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* Disable output and wait for a response */
|
/* 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) {
|
if (event == SCH_FILE_INPUT) {
|
||||||
if (IS_QUEUE_EMPTY()) {
|
if (IS_QUEUE_EMPTY()) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Unexpected signd response");
|
DEBUG_LOG("Unexpected signd response");
|
||||||
close_socket();
|
close_socket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(inst->received < sizeof (inst->response));
|
assert(inst->received < sizeof (inst->response));
|
||||||
s = recv(sock_fd, (char *)&inst->response + inst->received,
|
s = SCK_Receive(sock_fd, (char *)&inst->response + inst->received,
|
||||||
sizeof (inst->response) - inst->received, 0);
|
sizeof (inst->response) - inst->received, 0);
|
||||||
|
|
||||||
if (s <= 0) {
|
if (s <= 0) {
|
||||||
if (s < 0)
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd socket error: %s", strerror(errno));
|
|
||||||
else
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd socket closed");
|
|
||||||
|
|
||||||
close_socket();
|
close_socket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Received %d bytes from signd", s);
|
|
||||||
inst->received += s;
|
inst->received += s;
|
||||||
|
|
||||||
if (inst->received < sizeof (inst->response.length))
|
if (inst->received < sizeof (inst->response.length))
|
||||||
@@ -269,7 +250,7 @@ read_write_socket(int sock_fd, int event, void *anything)
|
|||||||
|
|
||||||
if (response_length < offsetof(SigndResponse, signed_packet) ||
|
if (response_length < offsetof(SigndResponse, signed_packet) ||
|
||||||
response_length > sizeof (SigndResponse)) {
|
response_length > sizeof (SigndResponse)) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Invalid response length");
|
DEBUG_LOG("Invalid response length");
|
||||||
close_socket();
|
close_socket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -283,7 +264,7 @@ read_write_socket(int sock_fd, int event, void *anything)
|
|||||||
/* Move the head and enable output for the next packet */
|
/* Move the head and enable output for the next packet */
|
||||||
queue_head = NEXT_QUEUE_INDEX(queue_head);
|
queue_head = NEXT_QUEUE_INDEX(queue_head);
|
||||||
if (!IS_QUEUE_EMPTY())
|
if (!IS_QUEUE_EMPTY())
|
||||||
SCH_SetFileHandlerEvents(sock_fd, SCH_FILE_INPUT | SCH_FILE_OUTPUT);
|
SCH_SetFileHandlerEvent(sock_fd, SCH_FILE_OUTPUT, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,7 +284,7 @@ NSD_Initialise()
|
|||||||
ARR_SetSize(queue, MAX_QUEUE_LENGTH);
|
ARR_SetSize(queue, MAX_QUEUE_LENGTH);
|
||||||
queue_head = queue_tail = 0;
|
queue_head = queue_tail = 0;
|
||||||
|
|
||||||
LOG(LOGS_INFO, LOGF_NtpSignd, "MS-SNTP authentication enabled");
|
LOG(LOGS_INFO, "MS-SNTP authentication enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -328,22 +309,23 @@ extern int NSD_GetAuthDelay(uint32_t key_id)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
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;
|
SignInstance *inst;
|
||||||
|
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd disabled");
|
DEBUG_LOG("signd disabled");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queue_head == NEXT_QUEUE_INDEX(queue_tail)) {
|
if (queue_head == NEXT_QUEUE_INDEX(queue_tail)) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "signd queue full");
|
DEBUG_LOG("signd queue full");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length != NTP_NORMAL_PACKET_LENGTH) {
|
if (info->length != NTP_HEADER_LENGTH) {
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Invalid packet length");
|
DEBUG_LOG("Invalid packet length");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +337,7 @@ NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *r
|
|||||||
inst->local_addr = *local_addr;
|
inst->local_addr = *local_addr;
|
||||||
inst->sent = 0;
|
inst->sent = 0;
|
||||||
inst->received = 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 */
|
/* The length field doesn't include itself */
|
||||||
inst->request.length = htonl(inst->request_length - sizeof (inst->request.length));
|
inst->request.length = htonl(inst->request_length - sizeof (inst->request.length));
|
||||||
@@ -365,16 +347,15 @@ NSD_SignAndSendPacket(uint32_t key_id, NTP_Packet *packet, NTP_Remote_Address *r
|
|||||||
inst->request._pad = 0;
|
inst->request._pad = 0;
|
||||||
inst->request.key_id = htonl(key_id);
|
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 */
|
/* Enable output if there was no pending request */
|
||||||
if (IS_QUEUE_EMPTY())
|
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);
|
queue_tail = NEXT_QUEUE_INDEX(queue_tail);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSignd, "Packet added to signd queue (%u:%u)",
|
DEBUG_LOG("Packet added to signd queue (%u:%u)", queue_head, queue_tail);
|
||||||
queue_head, queue_tail);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ extern void NSD_Finalise(void);
|
|||||||
extern int NSD_GetAuthDelay(uint32_t key_id);
|
extern int NSD_GetAuthDelay(uint32_t key_id);
|
||||||
|
|
||||||
/* Function to sign an NTP packet and send it */
|
/* 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
|
#endif
|
||||||
|
|||||||
486
ntp_sources.c
486
ntp_sources.c
@@ -34,11 +34,13 @@
|
|||||||
#include "array.h"
|
#include "array.h"
|
||||||
#include "ntp_sources.h"
|
#include "ntp_sources.h"
|
||||||
#include "ntp_core.h"
|
#include "ntp_core.h"
|
||||||
|
#include "ntp_io.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "local.h"
|
#include "local.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "nameserv_async.h"
|
#include "nameserv_async.h"
|
||||||
|
#include "privops.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -47,7 +49,9 @@
|
|||||||
particular sources */
|
particular sources */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
NTP_Remote_Address *remote_addr; /* The address of this source, non-NULL
|
NTP_Remote_Address *remote_addr; /* The address of this source, non-NULL
|
||||||
means this slot in table is in use */
|
means this slot in table is in use
|
||||||
|
(an IPADDR_ID address means the address
|
||||||
|
is not resolved yet) */
|
||||||
NCR_Instance data; /* Data for the protocol engine for this source */
|
NCR_Instance data; /* Data for the protocol engine for this source */
|
||||||
char *name; /* Name of the source, may be NULL */
|
char *name; /* Name of the source, may be NULL */
|
||||||
int pool; /* Number of the pool from which was this source
|
int pool; /* Number of the pool from which was this source
|
||||||
@@ -66,21 +70,21 @@ static int n_sources;
|
|||||||
/* Flag indicating new sources will be started automatically when added */
|
/* Flag indicating new sources will be started automatically when added */
|
||||||
static int auto_start_sources = 0;
|
static int auto_start_sources = 0;
|
||||||
|
|
||||||
/* Source with unknown address (which may be resolved later) */
|
/* Last assigned address ID */
|
||||||
|
static uint32_t last_address_id = 0;
|
||||||
|
|
||||||
|
/* Source scheduled for name resolving (first resolving or replacement) */
|
||||||
struct UnresolvedSource {
|
struct UnresolvedSource {
|
||||||
|
/* Current address of the source (IDADDR_ID is used for a single source
|
||||||
|
with unknown address and IPADDR_UNSPEC for a pool of sources */
|
||||||
|
NTP_Remote_Address address;
|
||||||
|
/* ID of the pool if not a single source */
|
||||||
|
int pool;
|
||||||
|
/* Name to be resolved */
|
||||||
char *name;
|
char *name;
|
||||||
int port;
|
/* Flag indicating addresses should be used in a random order */
|
||||||
int random_order;
|
int random_order;
|
||||||
int replacement;
|
/* Next unresolved source in the list */
|
||||||
union {
|
|
||||||
struct {
|
|
||||||
NTP_Source_Type type;
|
|
||||||
SourceParameters params;
|
|
||||||
int pool;
|
|
||||||
int max_new_sources;
|
|
||||||
} new_source;
|
|
||||||
NTP_Remote_Address replace_source;
|
|
||||||
};
|
|
||||||
struct UnresolvedSource *next;
|
struct UnresolvedSource *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -100,9 +104,13 @@ static NSR_SourceResolvingEndHandler resolving_end_handler = NULL;
|
|||||||
|
|
||||||
/* Pool of sources with the same name */
|
/* Pool of sources with the same name */
|
||||||
struct SourcePool {
|
struct SourcePool {
|
||||||
/* Number of sources added from this pool (ignoring tentative sources) */
|
/* Number of all sources from the pool */
|
||||||
int sources;
|
int sources;
|
||||||
/* Maximum number of sources */
|
/* Number of sources with unresolved address */
|
||||||
|
int unresolved_sources;
|
||||||
|
/* Number of non-tentative sources */
|
||||||
|
int confirmed_sources;
|
||||||
|
/* Maximum number of confirmed sources */
|
||||||
int max_sources;
|
int max_sources;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,9 +120,11 @@ static ARR_Instance pools;
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Forward prototypes */
|
/* Forward prototypes */
|
||||||
|
|
||||||
static void resolve_sources(void *arg);
|
static void resolve_sources(void);
|
||||||
static void rehash_records(void);
|
static void rehash_records(void);
|
||||||
static void clean_source_record(SourceRecord *record);
|
static void clean_source_record(SourceRecord *record);
|
||||||
|
static void remove_pool_sources(int pool, int tentative, int unresolved);
|
||||||
|
static void remove_unresolved_source(struct UnresolvedSource *us);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
slew_sources(struct timespec *raw,
|
slew_sources(struct timespec *raw,
|
||||||
@@ -139,6 +149,14 @@ get_record(unsigned index)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static struct SourcePool *
|
||||||
|
get_pool(unsigned index)
|
||||||
|
{
|
||||||
|
return (struct SourcePool *)ARR_GetElement(pools, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
NSR_Initialise(void)
|
NSR_Initialise(void)
|
||||||
{
|
{
|
||||||
@@ -159,11 +177,8 @@ void
|
|||||||
NSR_Finalise(void)
|
NSR_Finalise(void)
|
||||||
{
|
{
|
||||||
SourceRecord *record;
|
SourceRecord *record;
|
||||||
struct UnresolvedSource *us;
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
ARR_DestroyInstance(pools);
|
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||||
record = get_record(i);
|
record = get_record(i);
|
||||||
if (record->remote_addr)
|
if (record->remote_addr)
|
||||||
@@ -171,13 +186,10 @@ NSR_Finalise(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ARR_DestroyInstance(records);
|
ARR_DestroyInstance(records);
|
||||||
|
ARR_DestroyInstance(pools);
|
||||||
|
|
||||||
while (unresolved_sources) {
|
while (unresolved_sources)
|
||||||
us = unresolved_sources;
|
remove_unresolved_source(unresolved_sources);
|
||||||
unresolved_sources = us->next;
|
|
||||||
Free(us->name);
|
|
||||||
Free(us);
|
|
||||||
}
|
|
||||||
|
|
||||||
initialised = 0;
|
initialised = 0;
|
||||||
}
|
}
|
||||||
@@ -205,12 +217,14 @@ find_slot(NTP_Remote_Address *remote_addr, int *slot, int *found)
|
|||||||
unsigned short port;
|
unsigned short port;
|
||||||
|
|
||||||
size = ARR_GetSize(records);
|
size = ARR_GetSize(records);
|
||||||
|
|
||||||
|
*slot = 0;
|
||||||
|
*found = 0;
|
||||||
|
|
||||||
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
|
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
|
||||||
remote_addr->ip_addr.family != IPADDR_INET6) {
|
remote_addr->ip_addr.family != IPADDR_INET6 &&
|
||||||
*found = *slot = 0;
|
remote_addr->ip_addr.family != IPADDR_ID)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
hash = UTI_IPToHash(&remote_addr->ip_addr);
|
hash = UTI_IPToHash(&remote_addr->ip_addr);
|
||||||
port = remote_addr->port;
|
port = remote_addr->port;
|
||||||
@@ -229,8 +243,6 @@ find_slot(NTP_Remote_Address *remote_addr, int *slot, int *found)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*found = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -295,7 +307,8 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type, So
|
|||||||
return NSR_AlreadyInUse;
|
return NSR_AlreadyInUse;
|
||||||
} else {
|
} else {
|
||||||
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
|
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
|
||||||
remote_addr->ip_addr.family != IPADDR_INET6) {
|
remote_addr->ip_addr.family != IPADDR_INET6 &&
|
||||||
|
remote_addr->ip_addr.family != IPADDR_ID) {
|
||||||
return NSR_InvalidAF;
|
return NSR_InvalidAF;
|
||||||
} else {
|
} else {
|
||||||
n_sources++;
|
n_sources++;
|
||||||
@@ -307,13 +320,19 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type, So
|
|||||||
|
|
||||||
assert(!found);
|
assert(!found);
|
||||||
record = get_record(slot);
|
record = get_record(slot);
|
||||||
record->data = NCR_GetInstance(remote_addr, type, params);
|
record->data = NCR_CreateInstance(remote_addr, type, params, name);
|
||||||
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
||||||
record->name = name ? Strdup(name) : NULL;
|
record->name = name ? Strdup(name) : NULL;
|
||||||
record->pool = pool;
|
record->pool = pool;
|
||||||
record->tentative = 1;
|
record->tentative = 1;
|
||||||
|
|
||||||
if (auto_start_sources)
|
if (record->pool != INVALID_POOL) {
|
||||||
|
get_pool(record->pool)->sources++;
|
||||||
|
if (!UTI_IsIPReal(&remote_addr->ip_addr))
|
||||||
|
get_pool(record->pool)->unresolved_sources++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto_start_sources && UTI_IsIPReal(&remote_addr->ip_addr))
|
||||||
NCR_StartInstance(record->data);
|
NCR_StartInstance(record->data);
|
||||||
|
|
||||||
return NSR_Success;
|
return NSR_Success;
|
||||||
@@ -324,70 +343,109 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type, So
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static NSR_Status
|
static NSR_Status
|
||||||
replace_source(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
|
change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr,
|
||||||
|
int replacement)
|
||||||
{
|
{
|
||||||
int slot1, slot2, found;
|
int slot1, slot2, found;
|
||||||
SourceRecord *record;
|
SourceRecord *record;
|
||||||
struct SourcePool *pool;
|
LOG_Severity severity;
|
||||||
|
char *name;
|
||||||
|
|
||||||
find_slot(old_addr, &slot1, &found);
|
find_slot(old_addr, &slot1, &found);
|
||||||
if (!found)
|
if (!found)
|
||||||
return NSR_NoSuchSource;
|
return NSR_NoSuchSource;
|
||||||
|
|
||||||
|
/* Make sure there is no other source using the new address (with the same
|
||||||
|
or different port), but allow a source to have its port changed */
|
||||||
find_slot(new_addr, &slot2, &found);
|
find_slot(new_addr, &slot2, &found);
|
||||||
if (found)
|
if (found == 2 || (found != 0 && slot1 != slot2))
|
||||||
return NSR_AlreadyInUse;
|
return NSR_AlreadyInUse;
|
||||||
|
|
||||||
record = get_record(slot1);
|
record = get_record(slot1);
|
||||||
NCR_ChangeRemoteAddress(record->data, new_addr);
|
NCR_ChangeRemoteAddress(record->data, new_addr, !replacement);
|
||||||
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
record->remote_addr = NCR_GetRemoteAddress(record->data);
|
||||||
|
if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) {
|
||||||
|
if (auto_start_sources)
|
||||||
|
NCR_StartInstance(record->data);
|
||||||
|
if (record->pool != INVALID_POOL)
|
||||||
|
get_pool(record->pool)->unresolved_sources--;
|
||||||
|
}
|
||||||
|
|
||||||
if (!record->tentative) {
|
if (!record->tentative) {
|
||||||
record->tentative = 1;
|
record->tentative = 1;
|
||||||
|
|
||||||
if (record->pool != INVALID_POOL) {
|
if (record->pool != INVALID_POOL)
|
||||||
pool = ARR_GetElement(pools, record->pool);
|
get_pool(record->pool)->confirmed_sources--;
|
||||||
pool->sources--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The hash table must be rebuilt for the new address */
|
name = record->name;
|
||||||
rehash_records();
|
severity = UTI_IsIPReal(&old_addr->ip_addr) ? LOGS_INFO : LOGS_DEBUG;
|
||||||
|
|
||||||
LOG(LOGS_INFO, LOGF_NtpSources, "Source %s replaced with %s",
|
if (slot1 != slot2) {
|
||||||
UTI_IPToString(&old_addr->ip_addr),
|
/* The hash table must be rebuilt for the changed address */
|
||||||
UTI_IPToString(&new_addr->ip_addr));
|
rehash_records();
|
||||||
|
|
||||||
|
LOG(severity, "Source %s %s %s (%s)", UTI_IPToString(&old_addr->ip_addr),
|
||||||
|
replacement ? "replaced with" : "changed to",
|
||||||
|
UTI_IPToString(&new_addr->ip_addr), name ? name : "");
|
||||||
|
} else {
|
||||||
|
LOG(severity, "Source %s (%s) changed port to %d",
|
||||||
|
UTI_IPToString(&new_addr->ip_addr), name ? name : "", new_addr->port);
|
||||||
|
}
|
||||||
|
|
||||||
return NSR_Success;
|
return NSR_Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
replace_source_connectable(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
|
||||||
|
{
|
||||||
|
if (!NIO_IsServerConnectable(new_addr)) {
|
||||||
|
DEBUG_LOG("%s not connectable", UTI_IPToString(&new_addr->ip_addr));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (change_source_address(old_addr, new_addr, 1) == NSR_AlreadyInUse)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs)
|
process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs)
|
||||||
{
|
{
|
||||||
NTP_Remote_Address address;
|
NTP_Remote_Address old_addr, new_addr;
|
||||||
int i, added;
|
SourceRecord *record;
|
||||||
unsigned short first = 0;
|
unsigned short first = 0;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
if (us->random_order)
|
if (us->random_order)
|
||||||
UTI_GetRandomBytes(&first, sizeof (first));
|
UTI_GetRandomBytes(&first, sizeof (first));
|
||||||
|
|
||||||
for (i = added = 0; i < n_addrs; i++) {
|
for (i = 0; i < n_addrs; i++) {
|
||||||
address.ip_addr = ip_addrs[((unsigned int)i + first) % n_addrs];
|
new_addr.ip_addr = ip_addrs[((unsigned int)i + first) % n_addrs];
|
||||||
address.port = us->port;
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSources, "(%d) %s", i + 1, UTI_IPToString(&address.ip_addr));
|
DEBUG_LOG("(%d) %s", i + 1, UTI_IPToString(&new_addr.ip_addr));
|
||||||
|
|
||||||
if (us->replacement) {
|
if (us->pool != INVALID_POOL) {
|
||||||
if (replace_source(&us->replace_source, &address) != NSR_AlreadyInUse)
|
/* In the pool resolving mode, try to replace all sources from
|
||||||
break;
|
the pool which don't have a real address yet */
|
||||||
|
for (j = 0; j < ARR_GetSize(records); j++) {
|
||||||
|
record = get_record(j);
|
||||||
|
if (!record->remote_addr || record->pool != us->pool ||
|
||||||
|
UTI_IsIPReal(&record->remote_addr->ip_addr))
|
||||||
|
continue;
|
||||||
|
old_addr = *record->remote_addr;
|
||||||
|
new_addr.port = old_addr.port;
|
||||||
|
if (replace_source_connectable(&old_addr, &new_addr))
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (add_source(&address, us->name, us->new_source.type, &us->new_source.params,
|
new_addr.port = us->address.port;
|
||||||
us->new_source.pool) == NSR_Success)
|
if (replace_source_connectable(&us->address, &new_addr))
|
||||||
added++;
|
|
||||||
|
|
||||||
if (added >= us->new_source.max_new_sources)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -395,16 +453,43 @@ process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_resolved(struct UnresolvedSource *us)
|
||||||
|
{
|
||||||
|
int slot, found;
|
||||||
|
|
||||||
|
if (us->pool != INVALID_POOL) {
|
||||||
|
return get_pool(us->pool)->unresolved_sources <= 0;
|
||||||
|
} else {
|
||||||
|
/* If the address is no longer present, it was removed or replaced
|
||||||
|
(i.e. resolved) */
|
||||||
|
find_slot(&us->address, &slot, &found);
|
||||||
|
return !found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
resolve_sources_timeout(void *arg)
|
||||||
|
{
|
||||||
|
resolving_id = 0;
|
||||||
|
resolve_sources();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *anything)
|
name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *anything)
|
||||||
{
|
{
|
||||||
struct UnresolvedSource *us, **i, *next;
|
struct UnresolvedSource *us, *next;
|
||||||
|
|
||||||
us = (struct UnresolvedSource *)anything;
|
us = (struct UnresolvedSource *)anything;
|
||||||
|
|
||||||
assert(us == resolving_source);
|
assert(us == resolving_source);
|
||||||
|
assert(resolving_id == 0);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSources, "%s resolved to %d addrs", us->name, n_addrs);
|
DEBUG_LOG("%s resolved to %d addrs", us->name, n_addrs);
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case DNS_TryAgain:
|
case DNS_TryAgain:
|
||||||
@@ -413,7 +498,7 @@ name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *any
|
|||||||
process_resolved_name(us, ip_addrs, n_addrs);
|
process_resolved_name(us, ip_addrs, n_addrs);
|
||||||
break;
|
break;
|
||||||
case DNS_Failure:
|
case DNS_Failure:
|
||||||
LOG(LOGS_WARN, LOGF_NtpSources, "Invalid host %s", us->name);
|
LOG(LOGS_WARN, "Invalid host %s", us->name);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
@@ -421,35 +506,25 @@ name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *any
|
|||||||
|
|
||||||
next = us->next;
|
next = us->next;
|
||||||
|
|
||||||
/* Remove the source from the list on success or failure, replacements
|
/* Don't repeat the resolving if it (permanently) failed, it was a
|
||||||
are removed on any status */
|
replacement of a real address, or all addresses are already resolved */
|
||||||
if (us->replacement || status != DNS_TryAgain) {
|
if (status == DNS_Failure || UTI_IsIPReal(&us->address.ip_addr) || is_resolved(us))
|
||||||
for (i = &unresolved_sources; *i; i = &(*i)->next) {
|
remove_unresolved_source(us);
|
||||||
if (*i == us) {
|
|
||||||
*i = us->next;
|
|
||||||
Free(us->name);
|
|
||||||
Free(us);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resolving_source = next;
|
resolving_source = next;
|
||||||
|
|
||||||
if (next) {
|
if (next) {
|
||||||
/* Continue with the next source in the list */
|
/* Continue with the next source in the list */
|
||||||
DEBUG_LOG(LOGF_NtpSources, "resolving %s", next->name);
|
DEBUG_LOG("resolving %s", next->name);
|
||||||
DNS_Name2IPAddressAsync(next->name, name_resolve_handler, next);
|
DNS_Name2IPAddressAsync(next->name, name_resolve_handler, next);
|
||||||
} else {
|
} else {
|
||||||
/* This was the last source in the list. If some sources couldn't
|
/* This was the last source in the list. If some sources couldn't
|
||||||
be resolved, try again in exponentially increasing interval. */
|
be resolved, try again in exponentially increasing interval. */
|
||||||
if (unresolved_sources) {
|
if (unresolved_sources) {
|
||||||
if (resolving_interval < MIN_RESOLVE_INTERVAL)
|
resolving_interval = CLAMP(MIN_RESOLVE_INTERVAL, resolving_interval + 1,
|
||||||
resolving_interval = MIN_RESOLVE_INTERVAL;
|
MAX_RESOLVE_INTERVAL);
|
||||||
else if (resolving_interval < MAX_RESOLVE_INTERVAL)
|
resolving_id = SCH_AddTimeoutByDelay(RESOLVE_INTERVAL_UNIT * (1 << resolving_interval),
|
||||||
resolving_interval++;
|
resolve_sources_timeout, NULL);
|
||||||
resolving_id = SCH_AddTimeoutByDelay(RESOLVE_INTERVAL_UNIT *
|
|
||||||
(1 << resolving_interval), resolve_sources, NULL);
|
|
||||||
} else {
|
} else {
|
||||||
resolving_interval = 0;
|
resolving_interval = 0;
|
||||||
}
|
}
|
||||||
@@ -463,20 +538,30 @@ name_resolve_handler(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *any
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
resolve_sources(void *arg)
|
resolve_sources(void)
|
||||||
{
|
{
|
||||||
struct UnresolvedSource *us;
|
struct UnresolvedSource *us, *next, *i;
|
||||||
|
|
||||||
assert(!resolving_source);
|
assert(!resolving_source);
|
||||||
|
|
||||||
DNS_Reload();
|
/* Remove sources that don't need to be resolved anymore */
|
||||||
|
for (i = unresolved_sources; i; i = next) {
|
||||||
|
next = i->next;
|
||||||
|
if (is_resolved(i))
|
||||||
|
remove_unresolved_source(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!unresolved_sources)
|
||||||
|
return;
|
||||||
|
|
||||||
|
PRV_ReloadDNS();
|
||||||
|
|
||||||
/* Start with the first source in the list, name_resolve_handler
|
/* Start with the first source in the list, name_resolve_handler
|
||||||
will iterate over the rest */
|
will iterate over the rest */
|
||||||
us = unresolved_sources;
|
us = unresolved_sources;
|
||||||
|
|
||||||
resolving_source = us;
|
resolving_source = us;
|
||||||
DEBUG_LOG(LOGF_NtpSources, "resolving %s", us->name);
|
DEBUG_LOG("resolving %s", us->name);
|
||||||
DNS_Name2IPAddressAsync(us->name, name_resolve_handler, us);
|
DNS_Name2IPAddressAsync(us->name, name_resolve_handler, us);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,6 +580,23 @@ append_unresolved_source(struct UnresolvedSource *us)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
remove_unresolved_source(struct UnresolvedSource *us)
|
||||||
|
{
|
||||||
|
struct UnresolvedSource **i;
|
||||||
|
|
||||||
|
for (i = &unresolved_sources; *i; i = &(*i)->next) {
|
||||||
|
if (*i == us) {
|
||||||
|
*i = us->next;
|
||||||
|
Free(us->name);
|
||||||
|
Free(us);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
NSR_Status
|
NSR_Status
|
||||||
NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParameters *params)
|
NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParameters *params)
|
||||||
{
|
{
|
||||||
@@ -503,41 +605,60 @@ NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourceParam
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
NSR_Status
|
||||||
NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params)
|
NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params)
|
||||||
{
|
{
|
||||||
struct UnresolvedSource *us;
|
struct UnresolvedSource *us;
|
||||||
struct SourcePool *sp;
|
struct SourcePool *sp;
|
||||||
NTP_Remote_Address remote_addr;
|
NTP_Remote_Address remote_addr;
|
||||||
|
int i, new_sources;
|
||||||
|
|
||||||
/* If the name is an IP address, don't bother with full resolving now
|
/* If the name is an IP address, don't bother with full resolving now
|
||||||
or later when trying to replace the source */
|
or later when trying to replace the source */
|
||||||
if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
|
if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
|
||||||
remote_addr.port = port;
|
remote_addr.port = port;
|
||||||
NSR_AddSource(&remote_addr, type, params);
|
return NSR_AddSource(&remote_addr, type, params);
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
/* Make sure the name is at least printable and has no spaces */
|
||||||
|
for (i = 0; name[i] != '\0'; i++) {
|
||||||
|
if (!isgraph(name[i]))
|
||||||
|
return NSR_InvalidName;
|
||||||
}
|
}
|
||||||
|
|
||||||
us = MallocNew(struct UnresolvedSource);
|
us = MallocNew(struct UnresolvedSource);
|
||||||
us->name = Strdup(name);
|
us->name = Strdup(name);
|
||||||
us->port = port;
|
|
||||||
us->random_order = 0;
|
us->random_order = 0;
|
||||||
us->replacement = 0;
|
|
||||||
us->new_source.type = type;
|
remote_addr.ip_addr.family = IPADDR_ID;
|
||||||
us->new_source.params = *params;
|
remote_addr.ip_addr.addr.id = ++last_address_id;
|
||||||
|
remote_addr.port = port;
|
||||||
|
|
||||||
if (!pool) {
|
if (!pool) {
|
||||||
us->new_source.pool = INVALID_POOL;
|
us->pool = INVALID_POOL;
|
||||||
us->new_source.max_new_sources = 1;
|
us->address = remote_addr;
|
||||||
|
new_sources = 1;
|
||||||
} else {
|
} else {
|
||||||
sp = (struct SourcePool *)ARR_GetNewElement(pools);
|
sp = (struct SourcePool *)ARR_GetNewElement(pools);
|
||||||
sp->sources = 0;
|
sp->sources = 0;
|
||||||
sp->max_sources = params->max_sources;
|
sp->unresolved_sources = 0;
|
||||||
us->new_source.pool = ARR_GetSize(pools) - 1;
|
sp->confirmed_sources = 0;
|
||||||
us->new_source.max_new_sources = MAX_POOL_SOURCES;
|
sp->max_sources = CLAMP(1, params->max_sources, MAX_POOL_SOURCES);
|
||||||
|
us->pool = ARR_GetSize(pools) - 1;
|
||||||
|
us->address.ip_addr.family = IPADDR_UNSPEC;
|
||||||
|
new_sources = MIN(2 * sp->max_sources, MAX_POOL_SOURCES);
|
||||||
}
|
}
|
||||||
|
|
||||||
append_unresolved_source(us);
|
append_unresolved_source(us);
|
||||||
|
|
||||||
|
for (i = 0; i < new_sources; i++) {
|
||||||
|
if (i > 0)
|
||||||
|
remote_addr.ip_addr.addr.id = ++last_address_id;
|
||||||
|
if (add_source(&remote_addr, name, type, params, us->pool) != NSR_Success)
|
||||||
|
return NSR_TooManySources;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NSR_UnresolvedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -557,11 +678,12 @@ NSR_ResolveSources(void)
|
|||||||
if (unresolved_sources) {
|
if (unresolved_sources) {
|
||||||
/* Make sure no resolving is currently running */
|
/* Make sure no resolving is currently running */
|
||||||
if (!resolving_source) {
|
if (!resolving_source) {
|
||||||
if (resolving_interval) {
|
if (resolving_id != 0) {
|
||||||
SCH_RemoveTimeout(resolving_id);
|
SCH_RemoveTimeout(resolving_id);
|
||||||
|
resolving_id = 0;
|
||||||
resolving_interval--;
|
resolving_interval--;
|
||||||
}
|
}
|
||||||
resolve_sources(NULL);
|
resolve_sources();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* No unresolved sources, we are done */
|
/* No unresolved sources, we are done */
|
||||||
@@ -574,10 +696,12 @@ NSR_ResolveSources(void)
|
|||||||
|
|
||||||
void NSR_StartSources(void)
|
void NSR_StartSources(void)
|
||||||
{
|
{
|
||||||
|
NTP_Remote_Address *addr;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||||
if (!get_record(i)->remote_addr)
|
addr = get_record(i)->remote_addr;
|
||||||
|
if (!addr || !UTI_IsIPReal(&addr->ip_addr))
|
||||||
continue;
|
continue;
|
||||||
NCR_StartInstance(get_record(i)->data);
|
NCR_StartInstance(get_record(i)->data);
|
||||||
}
|
}
|
||||||
@@ -596,6 +720,19 @@ static void
|
|||||||
clean_source_record(SourceRecord *record)
|
clean_source_record(SourceRecord *record)
|
||||||
{
|
{
|
||||||
assert(record->remote_addr);
|
assert(record->remote_addr);
|
||||||
|
|
||||||
|
if (record->pool != INVALID_POOL) {
|
||||||
|
struct SourcePool *pool = get_pool(record->pool);
|
||||||
|
|
||||||
|
pool->sources--;
|
||||||
|
if (!UTI_IsIPReal(&record->remote_addr->ip_addr))
|
||||||
|
pool->unresolved_sources--;
|
||||||
|
if (!record->tentative)
|
||||||
|
pool->confirmed_sources--;
|
||||||
|
if (pool->max_sources > pool->sources)
|
||||||
|
pool->max_sources = pool->sources;
|
||||||
|
}
|
||||||
|
|
||||||
record->remote_addr = NULL;
|
record->remote_addr = NULL;
|
||||||
NCR_DestroyInstance(record->data);
|
NCR_DestroyInstance(record->data);
|
||||||
if (record->name)
|
if (record->name)
|
||||||
@@ -657,19 +794,17 @@ resolve_source_replacement(SourceRecord *record)
|
|||||||
{
|
{
|
||||||
struct UnresolvedSource *us;
|
struct UnresolvedSource *us;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSources, "trying to replace %s",
|
DEBUG_LOG("trying to replace %s", UTI_IPToString(&record->remote_addr->ip_addr));
|
||||||
UTI_IPToString(&record->remote_addr->ip_addr));
|
|
||||||
|
|
||||||
us = MallocNew(struct UnresolvedSource);
|
us = MallocNew(struct UnresolvedSource);
|
||||||
us->name = Strdup(record->name);
|
us->name = Strdup(record->name);
|
||||||
us->port = record->remote_addr->port;
|
|
||||||
/* If there never was a valid reply from this source (e.g. it was a bad
|
/* If there never was a valid reply from this source (e.g. it was a bad
|
||||||
replacement), ignore the order of addresses from the resolver to not get
|
replacement), ignore the order of addresses from the resolver to not get
|
||||||
stuck to a pair of addresses if the order doesn't change, or a group of
|
stuck to a pair of addresses if the order doesn't change, or a group of
|
||||||
IPv4/IPv6 addresses if the resolver prefers inaccessible IP family */
|
IPv4/IPv6 addresses if the resolver prefers inaccessible IP family */
|
||||||
us->random_order = record->tentative;
|
us->random_order = record->tentative;
|
||||||
us->replacement = 1;
|
us->pool = INVALID_POOL;
|
||||||
us->replace_source = *record->remote_addr;
|
us->address = *record->remote_addr;
|
||||||
|
|
||||||
append_unresolved_source(us);
|
append_unresolved_source(us);
|
||||||
NSR_ResolveSources();
|
NSR_ResolveSources();
|
||||||
@@ -704,7 +839,7 @@ NSR_HandleBadSource(IPAddr *address)
|
|||||||
SCH_GetLastEventTime(NULL, NULL, &now);
|
SCH_GetLastEventTime(NULL, NULL, &now);
|
||||||
diff = UTI_DiffTimespecsToDouble(&now, &last_replacement);
|
diff = UTI_DiffTimespecsToDouble(&now, &last_replacement);
|
||||||
if (fabs(diff) < RESOLVE_INTERVAL_UNIT * (1 << MIN_REPLACEMENT_INTERVAL)) {
|
if (fabs(diff) < RESOLVE_INTERVAL_UNIT * (1 << MIN_REPLACEMENT_INTERVAL)) {
|
||||||
DEBUG_LOG(LOGF_NtpSources, "replacement postponed");
|
DEBUG_LOG("replacement postponed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
last_replacement = now;
|
last_replacement = now;
|
||||||
@@ -731,7 +866,18 @@ NSR_RefreshAddresses(void)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static void remove_tentative_pool_sources(int pool)
|
NSR_Status
|
||||||
|
NSR_UpdateSourceNtpAddress(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
|
||||||
|
{
|
||||||
|
if (new_addr->ip_addr.family == IPADDR_UNSPEC)
|
||||||
|
return NSR_InvalidAF;
|
||||||
|
|
||||||
|
return change_source_address(old_addr, new_addr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void remove_pool_sources(int pool, int tentative, int unresolved)
|
||||||
{
|
{
|
||||||
SourceRecord *record;
|
SourceRecord *record;
|
||||||
unsigned int i, removed;
|
unsigned int i, removed;
|
||||||
@@ -739,10 +885,14 @@ static void remove_tentative_pool_sources(int pool)
|
|||||||
for (i = removed = 0; i < ARR_GetSize(records); i++) {
|
for (i = removed = 0; i < ARR_GetSize(records); i++) {
|
||||||
record = get_record(i);
|
record = get_record(i);
|
||||||
|
|
||||||
if (!record->remote_addr || record->pool != pool || !record->tentative)
|
if (!record->remote_addr || record->pool != pool)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSources, "removing tentative source %s",
|
if ((tentative && !record->tentative) ||
|
||||||
|
(unresolved && UTI_IsIPReal(&record->remote_addr->ip_addr)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
DEBUG_LOG("removing %ssource %s", tentative ? "tentative " : "",
|
||||||
UTI_IPToString(&record->remote_addr->ip_addr));
|
UTI_IPToString(&record->remote_addr->ip_addr));
|
||||||
|
|
||||||
clean_source_record(record);
|
clean_source_record(record);
|
||||||
@@ -773,6 +923,29 @@ NSR_GetLocalRefid(IPAddr *address)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
char *
|
||||||
|
NSR_GetName(IPAddr *address)
|
||||||
|
{
|
||||||
|
NTP_Remote_Address remote_addr;
|
||||||
|
int slot, found;
|
||||||
|
SourceRecord *record;
|
||||||
|
|
||||||
|
remote_addr.ip_addr = *address;
|
||||||
|
remote_addr.port = 0;
|
||||||
|
|
||||||
|
find_slot(&remote_addr, &slot, &found);
|
||||||
|
if (!found)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
record = get_record(slot);
|
||||||
|
if (record->name)
|
||||||
|
return record->name;
|
||||||
|
|
||||||
|
return UTI_IPToString(&record->remote_addr->ip_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
/* This routine is called by ntp_io when a new packet arrives off the network,
|
/* This routine is called by ntp_io when a new packet arrives off the network,
|
||||||
possibly with an authentication tail */
|
possibly with an authentication tail */
|
||||||
void
|
void
|
||||||
@@ -797,16 +970,15 @@ NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
|||||||
record->tentative = 0;
|
record->tentative = 0;
|
||||||
|
|
||||||
if (record->pool != INVALID_POOL) {
|
if (record->pool != INVALID_POOL) {
|
||||||
pool = ARR_GetElement(pools, record->pool);
|
pool = get_pool(record->pool);
|
||||||
pool->sources++;
|
pool->confirmed_sources++;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_NtpSources, "pool %s has %d confirmed sources",
|
DEBUG_LOG("pool %s has %d confirmed sources", record->name, pool->confirmed_sources);
|
||||||
record->name, pool->sources);
|
|
||||||
|
|
||||||
/* If the number of sources from the pool reached the configured
|
/* If the number of sources from the pool reached the configured
|
||||||
maximum, remove the remaining tentative sources */
|
maximum, remove the remaining tentative sources */
|
||||||
if (pool->sources >= pool->max_sources)
|
if (pool->confirmed_sources >= pool->max_sources)
|
||||||
remove_tentative_pool_sources(record->pool);
|
remove_pool_sources(record->pool, 1, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -862,80 +1034,37 @@ slew_sources(struct timespec *raw,
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
int
|
||||||
NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address)
|
NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity)
|
||||||
{
|
|
||||||
SourceRecord *record;
|
|
||||||
unsigned int i;
|
|
||||||
int any;
|
|
||||||
|
|
||||||
NSR_ResolveSources();
|
|
||||||
|
|
||||||
any = 0;
|
|
||||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
|
||||||
record = get_record(i);
|
|
||||||
if (record->remote_addr) {
|
|
||||||
if (address->family == IPADDR_UNSPEC ||
|
|
||||||
!UTI_CompareIPs(&record->remote_addr->ip_addr, address, mask)) {
|
|
||||||
any = 1;
|
|
||||||
NCR_TakeSourceOnline(record->data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (address->family == IPADDR_UNSPEC) {
|
|
||||||
struct UnresolvedSource *us;
|
|
||||||
|
|
||||||
for (us = unresolved_sources; us; us = us->next) {
|
|
||||||
if (us->replacement)
|
|
||||||
continue;
|
|
||||||
any = 1;
|
|
||||||
us->new_source.params.online = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return any;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ================================================== */
|
|
||||||
|
|
||||||
int
|
|
||||||
NSR_TakeSourcesOffline(IPAddr *mask, IPAddr *address)
|
|
||||||
{
|
{
|
||||||
SourceRecord *record, *syncpeer;
|
SourceRecord *record, *syncpeer;
|
||||||
unsigned int i, any;
|
unsigned int i, any;
|
||||||
|
|
||||||
|
if (connectivity != SRC_OFFLINE)
|
||||||
|
NSR_ResolveSources();
|
||||||
|
|
||||||
any = 0;
|
any = 0;
|
||||||
syncpeer = NULL;
|
syncpeer = NULL;
|
||||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||||
record = get_record(i);
|
record = get_record(i);
|
||||||
if (record->remote_addr) {
|
if (record->remote_addr) {
|
||||||
if (address->family == IPADDR_UNSPEC ||
|
/* Ignore SRC_MAYBE_ONLINE connectivity change for unspecified unresolved
|
||||||
|
sources as they would always end up in the offline state */
|
||||||
|
if ((address->family == IPADDR_UNSPEC &&
|
||||||
|
(connectivity != SRC_MAYBE_ONLINE || UTI_IsIPReal(&record->remote_addr->ip_addr))) ||
|
||||||
!UTI_CompareIPs(&record->remote_addr->ip_addr, address, mask)) {
|
!UTI_CompareIPs(&record->remote_addr->ip_addr, address, mask)) {
|
||||||
any = 1;
|
any = 1;
|
||||||
if (NCR_IsSyncPeer(record->data)) {
|
if (NCR_IsSyncPeer(record->data)) {
|
||||||
syncpeer = record;
|
syncpeer = record;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
NCR_TakeSourceOffline(record->data);
|
NCR_SetConnectivity(record->data, connectivity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take sync peer offline as last to avoid reference switching */
|
/* Set the sync peer last to avoid unnecessary reference switching */
|
||||||
if (syncpeer) {
|
if (syncpeer)
|
||||||
NCR_TakeSourceOffline(syncpeer->data);
|
NCR_SetConnectivity(syncpeer->data, connectivity);
|
||||||
}
|
|
||||||
|
|
||||||
if (address->family == IPADDR_UNSPEC) {
|
|
||||||
struct UnresolvedSource *us;
|
|
||||||
|
|
||||||
for (us = unresolved_sources; us; us = us->next) {
|
|
||||||
if (us->replacement)
|
|
||||||
continue;
|
|
||||||
any = 1;
|
|
||||||
us->new_source.params.online = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return any;
|
return any;
|
||||||
}
|
}
|
||||||
@@ -1147,26 +1276,25 @@ NSR_GetActivityReport(RPT_ActivityReport *report)
|
|||||||
{
|
{
|
||||||
SourceRecord *record;
|
SourceRecord *record;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
struct UnresolvedSource *us;
|
|
||||||
|
|
||||||
report->online = 0;
|
report->online = 0;
|
||||||
report->offline = 0;
|
report->offline = 0;
|
||||||
report->burst_online = 0;
|
report->burst_online = 0;
|
||||||
report->burst_offline = 0;
|
report->burst_offline = 0;
|
||||||
|
report->unresolved = 0;
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(records); i++) {
|
for (i = 0; i < ARR_GetSize(records); i++) {
|
||||||
record = get_record(i);
|
record = get_record(i);
|
||||||
if (record->remote_addr) {
|
if (!record->remote_addr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!UTI_IsIPReal(&record->remote_addr->ip_addr)) {
|
||||||
|
report->unresolved++;
|
||||||
|
} else {
|
||||||
NCR_IncrementActivityCounters(record->data, &report->online, &report->offline,
|
NCR_IncrementActivityCounters(record->data, &report->online, &report->offline,
|
||||||
&report->burst_online, &report->burst_offline);
|
&report->burst_online, &report->burst_offline);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
report->unresolved = 0;
|
|
||||||
|
|
||||||
for (us = unresolved_sources; us; us = us->next) {
|
|
||||||
report->unresolved++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ typedef enum {
|
|||||||
NSR_NoSuchSource, /* Remove - attempt to remove a source that is not known */
|
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_AlreadyInUse, /* AddSource - attempt to add a source that is already known */
|
||||||
NSR_TooManySources, /* AddSource - too many sources already present */
|
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;
|
} NSR_Status;
|
||||||
|
|
||||||
/* Procedure to add a new server or peer source. */
|
/* Procedure to add a new server or peer source. */
|
||||||
@@ -52,8 +54,10 @@ extern NSR_Status NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type
|
|||||||
|
|
||||||
/* Procedure to add a new server, peer source, or pool of servers specified by
|
/* 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
|
name instead of address. The name is resolved in exponentially increasing
|
||||||
intervals until it succeeds or fails with a non-temporary error. */
|
intervals until it succeeds or fails with a non-temporary error. If the
|
||||||
extern void NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type, SourceParameters *params);
|
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);
|
||||||
|
|
||||||
/* Function type for handlers to be called back when an attempt
|
/* Function type for handlers to be called back when an attempt
|
||||||
* (possibly unsuccessful) to resolve unresolved sources ends */
|
* (possibly unsuccessful) to resolve unresolved sources ends */
|
||||||
@@ -83,9 +87,17 @@ extern void NSR_HandleBadSource(IPAddr *address);
|
|||||||
/* Procedure to resolve all names again */
|
/* Procedure to resolve all names again */
|
||||||
extern void NSR_RefreshAddresses(void);
|
extern void NSR_RefreshAddresses(void);
|
||||||
|
|
||||||
|
/* Procedure to update the address of a source */
|
||||||
|
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 */
|
/* Procedure to get local reference ID corresponding to a source */
|
||||||
extern uint32_t NSR_GetLocalRefid(IPAddr *address);
|
extern uint32_t NSR_GetLocalRefid(IPAddr *address);
|
||||||
|
|
||||||
|
/* Procedure to get the name of a source. If the source doesn't have a name,
|
||||||
|
it returns a temporary string containing formatted address. */
|
||||||
|
extern char *NSR_GetName(IPAddr *address);
|
||||||
|
|
||||||
/* This routine is called by ntp_io when a new packet arrives off the network */
|
/* 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,
|
extern void NSR_ProcessRx(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr,
|
||||||
NTP_Local_Timestamp *rx_ts, NTP_Packet *message, int length);
|
NTP_Local_Timestamp *rx_ts, NTP_Packet *message, int length);
|
||||||
@@ -102,14 +114,9 @@ extern void NSR_Initialise(void);
|
|||||||
extern void NSR_Finalise(void);
|
extern void NSR_Finalise(void);
|
||||||
|
|
||||||
/* This routine is used to indicate that sources whose IP addresses
|
/* This routine is used to indicate that sources whose IP addresses
|
||||||
match a particular subnet should be set online again. Returns a
|
match a particular subnet should be set online or offline. It returns
|
||||||
flag indicating whether any hosts matched the address */
|
a flag indicating whether any hosts matched the address. */
|
||||||
extern int NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address);
|
extern int NSR_SetConnectivity(IPAddr *mask, IPAddr *address, SRC_Connectivity connectivity);
|
||||||
|
|
||||||
/* 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);
|
|
||||||
|
|
||||||
extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll);
|
extern int NSR_ModifyMinpoll(IPAddr *address, int new_minpoll);
|
||||||
|
|
||||||
|
|||||||
69
nts_ke.h
Normal file
69
nts_ke.h
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
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_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/1"
|
||||||
|
#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
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int length;
|
||||||
|
unsigned char key[NKE_MAX_KEY_LENGTH];
|
||||||
|
} NKE_Key;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int length;
|
||||||
|
unsigned char cookie[NKE_MAX_COOKIE_LENGTH];
|
||||||
|
} NKE_Cookie;
|
||||||
|
|
||||||
|
#endif
|
||||||
389
nts_ke_client.c
Normal file
389
nts_ke_client.c
Normal file
@@ -0,0 +1,389 @@
|
|||||||
|
/*
|
||||||
|
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_Instance session;
|
||||||
|
int destroying;
|
||||||
|
int got_response;
|
||||||
|
int resolving_name;
|
||||||
|
|
||||||
|
SIV_Algorithm siv_algorithm;
|
||||||
|
NKE_Key c2s, s2c;
|
||||||
|
NKE_Cookie cookies[NKE_MAX_COOKIES];
|
||||||
|
int num_cookies;
|
||||||
|
char server_name[NKE_MAX_RECORD_BODY_LENGTH + 1];
|
||||||
|
IPSockAddr ntp_address;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void *client_credentials;
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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) {
|
||||||
|
NKC_DestroyInstance(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 of the same family as NTS-KE */
|
||||||
|
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->siv_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 #%d length=%d", inst->num_cookies + 1, length);
|
||||||
|
assert(NKE_MAX_COOKIE_LENGTH == sizeof (inst->cookies[inst->num_cookies].cookie));
|
||||||
|
if (length <= NKE_MAX_COOKIE_LENGTH && inst->num_cookies < NKE_MAX_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(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->siv_algorithm, &inst->c2s, &inst->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)) {
|
||||||
|
DNS_Name2IPAddressAsync(inst->server_name, name_resolve_handler, inst);
|
||||||
|
inst->resolving_name = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inst->got_response = 1;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKC_Initialise(void)
|
||||||
|
{
|
||||||
|
client_credentials = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKC_Finalise(void)
|
||||||
|
{
|
||||||
|
if (client_credentials)
|
||||||
|
NKSN_DestroyCertCredentials(client_credentials);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
NKC_Instance
|
||||||
|
NKC_CreateInstance(IPSockAddr *address, const char *name)
|
||||||
|
{
|
||||||
|
NKC_Instance inst;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* Create the credentials with the first client instance and share them
|
||||||
|
with other instances */
|
||||||
|
if (!client_credentials)
|
||||||
|
client_credentials = NKSN_CreateCertCredentials(NULL, NULL, CNF_GetNtsTrustedCertFile());
|
||||||
|
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKC_DestroyInstance(NKC_Instance inst)
|
||||||
|
{
|
||||||
|
/* If the resolver is running, destroy the instance later when finished */
|
||||||
|
if (inst->resolving_name) {
|
||||||
|
inst->destroying = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NKSN_DestroyInstance(inst->session);
|
||||||
|
|
||||||
|
Free(inst->name);
|
||||||
|
Free(inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NKC_Start(NKC_Instance inst)
|
||||||
|
{
|
||||||
|
IPSockAddr local_addr;
|
||||||
|
int sock_fd;
|
||||||
|
|
||||||
|
assert(!NKC_IsActive(inst));
|
||||||
|
|
||||||
|
if (!client_credentials) {
|
||||||
|
DEBUG_LOG("Missing client credentials");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Follow the bindacqaddress setting */
|
||||||
|
CNF_GetBindAcquisitionAddress(inst->address.ip_addr.family, &local_addr.ip_addr);
|
||||||
|
if (local_addr.ip_addr.family != inst->address.ip_addr.family)
|
||||||
|
SCK_GetAnyLocalIPAddress(inst->address.ip_addr.family, &local_addr.ip_addr);
|
||||||
|
|
||||||
|
local_addr.port = 0;
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenTcpSocket(&inst->address, &local_addr, 0);
|
||||||
|
if (sock_fd < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Start a NTS-KE session */
|
||||||
|
if (!NKSN_StartSession(inst->session, sock_fd, client_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,
|
||||||
|
SIV_Algorithm *siv_algorithm, NKE_Key *c2s, NKE_Key *s2c,
|
||||||
|
NKE_Cookie *cookies, int *num_cookies, int max_cookies,
|
||||||
|
IPSockAddr *ntp_address)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!inst->got_response || inst->resolving_name)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
*siv_algorithm = inst->siv_algorithm;
|
||||||
|
*c2s = inst->c2s;
|
||||||
|
*s2c = inst->s2c;
|
||||||
|
|
||||||
|
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 i;
|
||||||
|
}
|
||||||
58
nts_ke_client.h
Normal file
58
nts_ke_client.h
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* Init and fini functions */
|
||||||
|
extern void NKC_Initialise(void);
|
||||||
|
extern void NKC_Finalise(void);
|
||||||
|
|
||||||
|
/* Create a client NTS-KE instance */
|
||||||
|
extern NKC_Instance NKC_CreateInstance(IPSockAddr *address, const char *name);
|
||||||
|
|
||||||
|
/* 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,
|
||||||
|
SIV_Algorithm *siv_algorithm, NKE_Key *c2s, NKE_Key *s2c,
|
||||||
|
NKE_Cookie *cookies, int *num_cookies, int max_cookies,
|
||||||
|
IPSockAddr *ntp_address);
|
||||||
|
|
||||||
|
#endif
|
||||||
785
nts_ke_server.c
Normal file
785
nts_ke_server.c
Normal file
@@ -0,0 +1,785 @@
|
|||||||
|
/*
|
||||||
|
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 "logging.h"
|
||||||
|
#include "memory.h"
|
||||||
|
#include "ntp_core.h"
|
||||||
|
#include "nts_ke_session.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 MIN_KEY_ROTATE_INTERVAL 1.0
|
||||||
|
|
||||||
|
#define INVALID_SOCK_FD (-7)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t key_id;
|
||||||
|
uint8_t 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 int server_sock_fd4;
|
||||||
|
static int server_sock_fd6;
|
||||||
|
|
||||||
|
static int helper_sock_fd;
|
||||||
|
|
||||||
|
static int initialised = 0;
|
||||||
|
|
||||||
|
/* Array of NKSN instances */
|
||||||
|
static ARR_Instance sessions;
|
||||||
|
static void *server_credentials;
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int handle_message(void *arg);
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
handle_client(int sock_fd, IPSockAddr *addr)
|
||||||
|
{
|
||||||
|
NKSN_Instance inst, *instp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (sock_fd > FD_SETSIZE / 2) {
|
||||||
|
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||||
|
UTI_IPSockAddrToString(addr), "too many descriptors");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find a slot which is free or has a 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, UTI_IPSockAddrToString(addr), 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!NKSN_StartSession(inst, sock_fd, 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;
|
||||||
|
|
||||||
|
if (!SCK_ReceiveMessage(fd, &message, SCK_FLAG_MSG_DESCRIPTOR))
|
||||||
|
return;
|
||||||
|
|
||||||
|
sock_fd = message.descriptor;
|
||||||
|
if (sock_fd < 0) {
|
||||||
|
/* Message with no descriptor is a shutdown command */
|
||||||
|
SCH_QuitProgram();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.length != sizeof (HelperRequest)) {
|
||||||
|
DEBUG_LOG("Unexpected message length");
|
||||||
|
SCK_CloseSocket(sock_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
req = message.data;
|
||||||
|
|
||||||
|
/* Extract the server key and client address from the request */
|
||||||
|
server_keys[current_server_key].id = ntohl(req->key_id);
|
||||||
|
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)))
|
||||||
|
assert(0);
|
||||||
|
|
||||||
|
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 server_fd, int event, void *arg)
|
||||||
|
{
|
||||||
|
SCK_Message message;
|
||||||
|
IPSockAddr addr;
|
||||||
|
int log_index, sock_fd;
|
||||||
|
struct timespec now;
|
||||||
|
|
||||||
|
sock_fd = SCK_AcceptConnection(server_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_LogNTPAccess(&addr.ip_addr, &now);
|
||||||
|
if (log_index >= 0 && CLG_LimitNTPResponseRate(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;
|
||||||
|
|
||||||
|
/* Include the current server key and client address in the request */
|
||||||
|
memset(&req, 0, sizeof (req));
|
||||||
|
req.key_id = htonl(server_keys[current_server_key].id);
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (!SCK_SendMessage(helper_sock_fd, &message, SCK_FLAG_MSG_DESCRIPTOR)) {
|
||||||
|
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, int port)
|
||||||
|
{
|
||||||
|
IPSockAddr local_addr;
|
||||||
|
int sock_fd;
|
||||||
|
|
||||||
|
if (!SCK_IsFamilySupported(family))
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
|
||||||
|
CNF_GetBindAddress(family, &local_addr.ip_addr);
|
||||||
|
|
||||||
|
if (local_addr.ip_addr.family != family)
|
||||||
|
SCK_GetAnyLocalIPAddress(family, &local_addr.ip_addr);
|
||||||
|
|
||||||
|
local_addr.port = port;
|
||||||
|
|
||||||
|
sock_fd = SCK_OpenTcpSocket(NULL, &local_addr, 0);
|
||||||
|
if (sock_fd < 0) {
|
||||||
|
LOG(LOGS_ERR, "Could not open NTS-KE socket on %s", UTI_IPSockAddrToString(&local_addr));
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SCK_ListenOnSocket(sock_fd, CNF_GetNtsServerConnections())) {
|
||||||
|
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_Cookie cookie;
|
||||||
|
NKE_Key c2s, s2c;
|
||||||
|
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 {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This should be configurable */
|
||||||
|
if (0) {
|
||||||
|
const char server[] = "::1";
|
||||||
|
if (!NKSN_AddRecord(session, 1, NKE_RECORD_NTPV4_SERVER_NEGOTIATION, server,
|
||||||
|
sizeof (server) - 1))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!NKSN_GetKeys(session, aead_algorithm, &c2s, &s2c))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for (i = 0; i < NKE_MAX_COOKIES; i++) {
|
||||||
|
if (!NKS_GenerateCookie(&c2s, &s2c, &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 = -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 == -1) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
for (i = 0; i < MIN(length, sizeof (data)) / 2; i++) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
for (i = 0; i < MIN(length, sizeof (data)) / 2; i++) {
|
||||||
|
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 (aead_algorithm < 0 || next_protocol < 0)
|
||||||
|
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;
|
||||||
|
|
||||||
|
assert(index < MAX_SERVER_KEYS);
|
||||||
|
|
||||||
|
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 (!SIV_SetKey(server_keys[index].siv, server_keys[index].key, key_length))
|
||||||
|
assert(0);
|
||||||
|
|
||||||
|
UTI_GetRandomBytes(&server_keys[index].id, sizeof (server_keys[index].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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
save_keys(void)
|
||||||
|
{
|
||||||
|
char hex_key[SIV_MAX_KEY_LENGTH * 2 + 1];
|
||||||
|
int i, index, key_length;
|
||||||
|
char *cachedir;
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
cachedir = CNF_GetNtsCacheDir();
|
||||||
|
if (!cachedir)
|
||||||
|
return;
|
||||||
|
|
||||||
|
f = UTI_OpenFile(cachedir, "ntskeys", ".tmp", 'w', 0600);
|
||||||
|
if (!f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||||
|
index = (current_server_key + i + 1) % MAX_SERVER_KEYS;
|
||||||
|
|
||||||
|
if (key_length > sizeof (server_keys[index].key) ||
|
||||||
|
!UTI_BytesToHex(server_keys[index].key, key_length, hex_key, sizeof (hex_key))) {
|
||||||
|
assert(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(f, "%08"PRIX32" %s\n", server_keys[index].id, hex_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
if (!UTI_RenameTempFile(cachedir, "ntskeys", ".tmp", NULL))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
load_keys(void)
|
||||||
|
{
|
||||||
|
int i, index, line_length, key_length, n;
|
||||||
|
char *cachedir, line[1024];
|
||||||
|
FILE *f;
|
||||||
|
uint32_t id;
|
||||||
|
|
||||||
|
cachedir = CNF_GetNtsCacheDir();
|
||||||
|
if (!cachedir)
|
||||||
|
return;
|
||||||
|
|
||||||
|
f = UTI_OpenFile(cachedir, "ntskeys", NULL, 'r', 0);
|
||||||
|
if (!f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
key_length = SIV_GetKeyLength(SERVER_COOKIE_SIV);
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||||
|
if (!fgets(line, sizeof (line), f))
|
||||||
|
break;
|
||||||
|
|
||||||
|
line_length = strlen(line);
|
||||||
|
if (line_length < 10)
|
||||||
|
break;
|
||||||
|
/* Drop '\n' */
|
||||||
|
line[line_length - 1] = '\0';
|
||||||
|
|
||||||
|
if (sscanf(line, "%"PRIX32"%n", &id, &n) != 1 || line[n] != ' ')
|
||||||
|
break;
|
||||||
|
|
||||||
|
index = id % MAX_SERVER_KEYS;
|
||||||
|
|
||||||
|
if (UTI_HexToBytes(line + n + 1, server_keys[index].key,
|
||||||
|
sizeof (server_keys[index].key)) != key_length)
|
||||||
|
break;
|
||||||
|
|
||||||
|
server_keys[index].id = id;
|
||||||
|
if (!SIV_SetKey(server_keys[index].siv, server_keys[index].key, key_length))
|
||||||
|
assert(0);
|
||||||
|
|
||||||
|
DEBUG_LOG("Loaded key %"PRIX32, id);
|
||||||
|
|
||||||
|
current_server_key = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
key_timeout(void *arg)
|
||||||
|
{
|
||||||
|
current_server_key = (current_server_key + 1) % MAX_SERVER_KEYS;
|
||||||
|
generate_key(current_server_key);
|
||||||
|
save_keys();
|
||||||
|
|
||||||
|
SCH_AddTimeoutByDelay(MAX(CNF_GetNtsRotate(), MIN_KEY_ROTATE_INTERVAL),
|
||||||
|
key_timeout, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
start_helper(int id, int scfilter_level, int main_fd, int helper_fd)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
|
||||||
|
if (pid < 0)
|
||||||
|
LOG_FATAL("fork() failed : %s", strerror(errno));
|
||||||
|
|
||||||
|
if (pid > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
SCK_CloseSocket(main_fd);
|
||||||
|
|
||||||
|
LOG_CloseParentFd();
|
||||||
|
SCH_Reset();
|
||||||
|
SCH_AddFileHandler(helper_fd, SCH_FILE_INPUT, handle_helper_request, NULL);
|
||||||
|
UTI_SetQuitSignalsHandler(helper_signal, 1);
|
||||||
|
if (scfilter_level != 0)
|
||||||
|
SYS_EnableSystemCallFilter(scfilter_level, SYS_NTSKE_HELPER);
|
||||||
|
|
||||||
|
initialised = 1;
|
||||||
|
|
||||||
|
DEBUG_LOG("NTS-KE helper #%d started", id);
|
||||||
|
|
||||||
|
SCH_MainLoop();
|
||||||
|
|
||||||
|
NKS_Finalise();
|
||||||
|
|
||||||
|
DEBUG_LOG("NTS-KE helper #%d exiting", id);
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKS_Initialise(int scfilter_level)
|
||||||
|
{
|
||||||
|
char *cert, *key;
|
||||||
|
int i, processes;
|
||||||
|
|
||||||
|
server_sock_fd4 = INVALID_SOCK_FD;
|
||||||
|
server_sock_fd6 = INVALID_SOCK_FD;
|
||||||
|
helper_sock_fd = INVALID_SOCK_FD;
|
||||||
|
|
||||||
|
cert = CNF_GetNtsServerCertFile();
|
||||||
|
key = CNF_GetNtsServerKeyFile();
|
||||||
|
|
||||||
|
if (!cert || !key)
|
||||||
|
return;
|
||||||
|
|
||||||
|
server_credentials = NKSN_CreateCertCredentials(cert, key, NULL);
|
||||||
|
if (!server_credentials)
|
||||||
|
return;
|
||||||
|
|
||||||
|
sessions = ARR_CreateInstance(sizeof (NKSN_Instance));
|
||||||
|
for (i = 0; i < CNF_GetNtsServerConnections(); i++)
|
||||||
|
*(NKSN_Instance *)ARR_GetNewElement(sessions) = NULL;
|
||||||
|
for (i = 0; i < MAX_SERVER_KEYS; i++)
|
||||||
|
server_keys[i].siv = NULL;
|
||||||
|
|
||||||
|
server_sock_fd4 = open_socket(IPADDR_INET4, CNF_GetNtsServerPort());
|
||||||
|
server_sock_fd6 = open_socket(IPADDR_INET6, CNF_GetNtsServerPort());
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
load_keys();
|
||||||
|
|
||||||
|
key_timeout(NULL);
|
||||||
|
|
||||||
|
processes = CNF_GetNtsServerProcesses();
|
||||||
|
|
||||||
|
if (processes > 0) {
|
||||||
|
int sock_fd1, sock_fd2;
|
||||||
|
|
||||||
|
sock_fd1 = SCK_OpenUnixSocketPair(0, &sock_fd2);
|
||||||
|
|
||||||
|
for (i = 0; i < processes; i++)
|
||||||
|
start_helper(i + 1, scfilter_level, sock_fd1, sock_fd2);
|
||||||
|
|
||||||
|
SCK_CloseSocket(sock_fd2);
|
||||||
|
helper_sock_fd = sock_fd1;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialised = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKS_Finalise(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!initialised)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (helper_sock_fd != INVALID_SOCK_FD) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
save_keys();
|
||||||
|
for (i = 0; i < MAX_SERVER_KEYS; i++) {
|
||||||
|
if (server_keys[i].siv != NULL)
|
||||||
|
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);
|
||||||
|
|
||||||
|
NKSN_DestroyCertCredentials(server_credentials);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
/* A server cookie consists of key ID, nonce, and encrypted C2S+S2C keys */
|
||||||
|
|
||||||
|
int
|
||||||
|
NKS_GenerateCookie(NKE_Key *c2s, NKE_Key *s2c, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c2s->length < 0 || c2s->length > NKE_MAX_KEY_LENGTH ||
|
||||||
|
s2c->length < 0 || s2c->length > NKE_MAX_KEY_LENGTH) {
|
||||||
|
DEBUG_LOG("Invalid key length");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
key = &server_keys[current_server_key];
|
||||||
|
|
||||||
|
header = (ServerCookieHeader *)cookie->cookie;
|
||||||
|
|
||||||
|
/* Keep the fields in the host byte order */
|
||||||
|
header->key_id = key->id;
|
||||||
|
UTI_GetRandomBytes(header->nonce, sizeof (header->nonce));
|
||||||
|
|
||||||
|
plaintext_length = c2s->length + s2c->length;
|
||||||
|
assert(plaintext_length <= sizeof (plaintext));
|
||||||
|
memcpy(plaintext, c2s->key, c2s->length);
|
||||||
|
memcpy(plaintext + c2s->length, s2c->key, 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_Key *c2s, NKE_Key *s2c)
|
||||||
|
{
|
||||||
|
unsigned char plaintext[2 * NKE_MAX_KEY_LENGTH], *ciphertext;
|
||||||
|
int ciphertext_length, plaintext_length, tag_length;
|
||||||
|
ServerCookieHeader *header;
|
||||||
|
ServerKey *key;
|
||||||
|
|
||||||
|
if (!initialised) {
|
||||||
|
DEBUG_LOG("NTS server disabled");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cookie->length <= 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 = &server_keys[header->key_id % MAX_SERVER_KEYS];
|
||||||
|
if (header->key_id != key->id) {
|
||||||
|
DEBUG_LOG("Unknown key %"PRIX32, header->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
c2s->length = plaintext_length / 2;
|
||||||
|
s2c->length = plaintext_length / 2;
|
||||||
|
assert(c2s->length <= sizeof (c2s->key));
|
||||||
|
|
||||||
|
memcpy(c2s->key, plaintext, c2s->length);
|
||||||
|
memcpy(s2c->key, plaintext + c2s->length, s2c->length);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
42
nts_ke_server.h
Normal file
42
nts_ke_server.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
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_Initialise(int scfilter_level);
|
||||||
|
extern void NKS_Finalise(void);
|
||||||
|
|
||||||
|
/* Generate a new NTS cookie containing the C2S and S2C keys */
|
||||||
|
extern int NKS_GenerateCookie(NKE_Key *c2s, NKE_Key *s2c, NKE_Cookie *cookie);
|
||||||
|
|
||||||
|
/* Validate a cookie and extract the C2S and S2C keys */
|
||||||
|
extern int NKS_DecodeCookie(NKE_Cookie *cookie, NKE_Key *c2s, NKE_Key *s2c);
|
||||||
|
|
||||||
|
#endif
|
||||||
779
nts_ke_session.c
Normal file
779
nts_ke_session.c
Normal file
@@ -0,0 +1,779 @@
|
|||||||
|
/*
|
||||||
|
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 "logging.h"
|
||||||
|
#include "memory.h"
|
||||||
|
#include "siv.h"
|
||||||
|
#include "socket.h"
|
||||||
|
#include "sched.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <gnutls/gnutls.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 *name;
|
||||||
|
NKSN_MessageHandler handler;
|
||||||
|
void *handler_arg;
|
||||||
|
|
||||||
|
KeState state;
|
||||||
|
int sock_fd;
|
||||||
|
gnutls_session_t tls_session;
|
||||||
|
SCH_TimeoutID timeout_id;
|
||||||
|
|
||||||
|
struct Message message;
|
||||||
|
int new_message;
|
||||||
|
int ended_message;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static gnutls_priority_t priority_cache;
|
||||||
|
|
||||||
|
static int credentials_counter = 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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = gnutls_init(&session, GNUTLS_NONBLOCK | (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) {
|
||||||
|
r = gnutls_server_name_set(session, GNUTLS_NAME_DNS, server_name, strlen(server_name));
|
||||||
|
if (r < 0)
|
||||||
|
goto error;
|
||||||
|
gnutls_session_set_verify_cert(session, server_name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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->name);
|
||||||
|
|
||||||
|
inst->timeout_id = 0;
|
||||||
|
stop_session(inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_socket_error(int sock_fd)
|
||||||
|
{
|
||||||
|
int optval;
|
||||||
|
socklen_t optlen = sizeof (optval);
|
||||||
|
|
||||||
|
if (getsockopt(sock_fd, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0) {
|
||||||
|
DEBUG_LOG("getsockopt() failed : %s", strerror(errno));
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return optval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
check_alpn(NKSN_Instance inst)
|
||||||
|
{
|
||||||
|
gnutls_datum_t alpn;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = gnutls_alpn_get_selected_protocol(inst->tls_session, &alpn);
|
||||||
|
if (r < 0 || alpn.size != sizeof (NKE_ALPN_NAME) - 1 ||
|
||||||
|
strncmp((const char *)alpn.data, NKE_ALPN_NAME, sizeof (NKE_ALPN_NAME) - 1))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
SCH_SetFileHandlerEvent(inst->sock_fd, SCH_FILE_OUTPUT, 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;
|
||||||
|
|
||||||
|
r = get_socket_error(inst->sock_fd);
|
||||||
|
|
||||||
|
if (r) {
|
||||||
|
LOG(LOGS_ERR, "Could not connect to %s : %s", inst->name, strerror(r));
|
||||||
|
stop_session(inst);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_LOG("Connected to %s", inst->name);
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR,
|
||||||
|
"TLS handshake with %s failed : %s", inst->name, gnutls_strerror(r));
|
||||||
|
stop_session(inst);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disable output when the handshake is trying to receive data */
|
||||||
|
SCH_SetFileHandlerEvent(inst->sock_fd, SCH_FILE_OUTPUT,
|
||||||
|
gnutls_record_get_direction(inst->tls_session));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
char *description = gnutls_session_get_desc(inst->tls_session);
|
||||||
|
DEBUG_LOG("Handshake with %s completed %s",
|
||||||
|
inst->name, description ? description : "");
|
||||||
|
gnutls_free(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!check_alpn(inst)) {
|
||||||
|
LOG(inst->server ? LOGS_DEBUG : LOGS_ERR, "NTS-KE not supported by %s", inst->name);
|
||||||
|
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);
|
||||||
|
|
||||||
|
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->name, gnutls_strerror(r));
|
||||||
|
stop_session(inst);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_LOG("Sent %d bytes to %s", r, inst->name);
|
||||||
|
|
||||||
|
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->name, gnutls_strerror(r));
|
||||||
|
stop_session(inst);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_LOG("Received %d bytes from %s", r, inst->name);
|
||||||
|
|
||||||
|
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->name);
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
|
||||||
|
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->name, gnutls_strerror(r));
|
||||||
|
stop_session(inst);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disable output when the TLS shutdown is trying to receive data */
|
||||||
|
SCH_SetFileHandlerEvent(inst->sock_fd, SCH_FILE_OUTPUT,
|
||||||
|
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 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
read_write_socket(int fd, int event, void *arg)
|
||||||
|
{
|
||||||
|
NKSN_Instance inst = arg;
|
||||||
|
|
||||||
|
if (!handle_event(inst, event))
|
||||||
|
return;
|
||||||
|
|
||||||
|
reset_message_parsing(&inst->message);
|
||||||
|
|
||||||
|
if (!(inst->handler)(inst->handler_arg)) {
|
||||||
|
stop_session(inst);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
/* NTS specification requires TLS1.2 or later */
|
||||||
|
r = gnutls_priority_init2(&priority_cache, "-VERS-SSL3.0:-VERS-TLS1.0:-VERS-TLS1.1",
|
||||||
|
NULL, GNUTLS_PRIORITY_INIT_DEF_APPEND);
|
||||||
|
if (r < 0)
|
||||||
|
LOG_FATAL("Could not initialise %s : %s", "priority cache", gnutls_strerror(r));
|
||||||
|
|
||||||
|
gnutls_initialised = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static void
|
||||||
|
deinit_gnutls(void)
|
||||||
|
{
|
||||||
|
assert(gnutls_initialised);
|
||||||
|
|
||||||
|
gnutls_priority_deinit(priority_cache);
|
||||||
|
gnutls_global_deinit();
|
||||||
|
gnutls_initialised = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void *
|
||||||
|
NKSN_CreateCertCredentials(char *cert, char *key, char *trusted_certs)
|
||||||
|
{
|
||||||
|
gnutls_certificate_credentials_t credentials = NULL;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
init_gnutls();
|
||||||
|
|
||||||
|
r = gnutls_certificate_allocate_credentials(&credentials);
|
||||||
|
if (r < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
if (cert && key) {
|
||||||
|
r = gnutls_certificate_set_x509_key_file(credentials, cert, key,
|
||||||
|
GNUTLS_X509_FMT_PEM);
|
||||||
|
if (r < 0)
|
||||||
|
goto error;
|
||||||
|
} else {
|
||||||
|
if (!CNF_GetNoSystemCert()) {
|
||||||
|
r = gnutls_certificate_set_x509_system_trust(credentials);
|
||||||
|
if (r < 0)
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trusted_certs) {
|
||||||
|
r = gnutls_certificate_set_x509_trust_file(credentials, trusted_certs,
|
||||||
|
GNUTLS_X509_FMT_PEM);
|
||||||
|
if (r < 0)
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
credentials_counter++;
|
||||||
|
|
||||||
|
return credentials;
|
||||||
|
|
||||||
|
error:
|
||||||
|
LOG(LOGS_ERR, "Could not set credentials : %s", gnutls_strerror(r));
|
||||||
|
if (credentials)
|
||||||
|
gnutls_certificate_free_credentials(credentials);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKSN_DestroyCertCredentials(void *credentials)
|
||||||
|
{
|
||||||
|
gnutls_certificate_free_credentials(credentials);
|
||||||
|
credentials_counter--;
|
||||||
|
if (credentials_counter != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
deinit_gnutls();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
NKSN_Instance
|
||||||
|
NKSN_CreateInstance(int server_mode, const char *name,
|
||||||
|
NKSN_MessageHandler handler, void *handler_arg)
|
||||||
|
{
|
||||||
|
NKSN_Instance inst;
|
||||||
|
|
||||||
|
inst = MallocNew(struct NKSN_Instance_Record);
|
||||||
|
|
||||||
|
inst->server = server_mode;
|
||||||
|
inst->name = Strdup(name);
|
||||||
|
inst->handler = handler;
|
||||||
|
inst->handler_arg = handler_arg;
|
||||||
|
/* Replace NULL arg with the session itself */
|
||||||
|
if (!inst->handler_arg)
|
||||||
|
inst->handler_arg = inst;
|
||||||
|
|
||||||
|
inst->state = KE_STOPPED;
|
||||||
|
inst->sock_fd = INVALID_SOCK_FD;
|
||||||
|
inst->tls_session = NULL;
|
||||||
|
inst->timeout_id = 0;
|
||||||
|
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKSN_DestroyInstance(NKSN_Instance inst)
|
||||||
|
{
|
||||||
|
stop_session(inst);
|
||||||
|
|
||||||
|
Free(inst->name);
|
||||||
|
Free(inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NKSN_StartSession(NKSN_Instance inst, int sock_fd, void *credentials, double timeout)
|
||||||
|
{
|
||||||
|
assert(inst->state == KE_STOPPED);
|
||||||
|
|
||||||
|
inst->tls_session = create_tls_session(inst->server, sock_fd,
|
||||||
|
inst->server ? NULL : inst->name,
|
||||||
|
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->timeout_id = SCH_AddTimeoutByDelay(timeout, session_timeout, inst);
|
||||||
|
|
||||||
|
reset_message(&inst->message);
|
||||||
|
inst->new_message = 0;
|
||||||
|
inst->ended_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);
|
||||||
|
|
||||||
|
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 (!get_record(&inst->message, critical, &type2, body_length, body, buffer_length))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
c2s->length = SIV_GetKeyLength(siv);
|
||||||
|
s2c->length = SIV_GetKeyLength(siv);
|
||||||
|
assert(c2s->length <= sizeof (c2s->key));
|
||||||
|
assert(s2c->length <= sizeof (s2c->key));
|
||||||
|
|
||||||
|
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,
|
||||||
|
c2s->length, (char *)c2s->key) < 0)
|
||||||
|
return 0;
|
||||||
|
if (gnutls_prf_rfc5705(inst->tls_session,
|
||||||
|
sizeof (NKE_EXPORTER_LABEL) - 1, NKE_EXPORTER_LABEL,
|
||||||
|
sizeof (NKE_EXPORTER_CONTEXT_S2C) - 1, NKE_EXPORTER_CONTEXT_S2C,
|
||||||
|
s2c->length, (char *)s2c->key) < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NKSN_IsStopped(NKSN_Instance inst)
|
||||||
|
{
|
||||||
|
return inst->state == KE_STOPPED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NKSN_StopSession(NKSN_Instance inst)
|
||||||
|
{
|
||||||
|
stop_session(inst);
|
||||||
|
}
|
||||||
83
nts_ke_session.h
Normal file
83
nts_ke_session.h
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
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_Instance_Record *NKSN_Instance;
|
||||||
|
|
||||||
|
/* Handler for received NTS-KE messages. A non-zero return code stops
|
||||||
|
the session. */
|
||||||
|
typedef int (*NKSN_MessageHandler)(void *arg);
|
||||||
|
|
||||||
|
/* Get client or server credentials using certificates of trusted CAs,
|
||||||
|
or a server certificate and key. The credentials may be shared between
|
||||||
|
different clients or servers. */
|
||||||
|
extern void *NKSN_CreateCertCredentials(char *cert, char *key, char *trusted_certs);
|
||||||
|
|
||||||
|
/* Destroy the credentials */
|
||||||
|
extern void NKSN_DestroyCertCredentials(void *credentials);
|
||||||
|
|
||||||
|
/* Create an instance */
|
||||||
|
extern NKSN_Instance NKSN_CreateInstance(int server_mode, const char *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, void *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);
|
||||||
|
|
||||||
|
#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
|
||||||
174
nts_ntp_auth.c
Normal file
174
nts_ntp_auth.c
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
/*
|
||||||
|
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;
|
||||||
|
|
||||||
|
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");
|
||||||
|
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 (!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)
|
||||||
|
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 = (unsigned char *)(header + 1) + 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;
|
||||||
|
|
||||||
|
if (!SIV_Decrypt(siv, nonce, nonce_length, packet, info->length - ef_body_length - 4,
|
||||||
|
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
|
||||||
441
nts_ntp_client.c
Normal file
441
nts_ntp_client.c
Normal file
@@ -0,0 +1,441 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
#define MAX_TOTAL_COOKIE_LENGTH (8 * 108)
|
||||||
|
#define MIN_NKE_RETRY_INTERVAL 1000
|
||||||
|
|
||||||
|
struct NNC_Instance_Record {
|
||||||
|
const IPSockAddr *ntp_address;
|
||||||
|
IPSockAddr nts_address;
|
||||||
|
char *name;
|
||||||
|
SIV_Instance siv_c2s;
|
||||||
|
SIV_Instance siv_s2c;
|
||||||
|
NKC_Instance nke;
|
||||||
|
|
||||||
|
double last_nke_attempt;
|
||||||
|
double last_nke_success;
|
||||||
|
NKE_Cookie cookies[NTS_MAX_COOKIES];
|
||||||
|
int num_cookies;
|
||||||
|
int cookie_index;
|
||||||
|
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
|
||||||
|
reset_instance(NNC_Instance inst)
|
||||||
|
{
|
||||||
|
inst->last_nke_attempt = -MIN_NKE_RETRY_INTERVAL;
|
||||||
|
inst->last_nke_success = 0.0;
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
inst->cookie_index = 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, const IPSockAddr *ntp_address)
|
||||||
|
{
|
||||||
|
NNC_Instance inst;
|
||||||
|
|
||||||
|
inst = MallocNew(struct NNC_Instance_Record);
|
||||||
|
|
||||||
|
inst->ntp_address = ntp_address;
|
||||||
|
inst->nts_address = *nts_address;
|
||||||
|
inst->name = name ? Strdup(name) : NULL;
|
||||||
|
inst->siv_c2s = NULL;
|
||||||
|
inst->siv_s2c = NULL;
|
||||||
|
inst->nke = NULL;
|
||||||
|
|
||||||
|
reset_instance(inst);
|
||||||
|
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NNC_DestroyInstance(NNC_Instance inst)
|
||||||
|
{
|
||||||
|
if (inst->nke)
|
||||||
|
NKC_DestroyInstance(inst->nke);
|
||||||
|
if (inst->siv_c2s)
|
||||||
|
SIV_DestroyInstance(inst->siv_c2s);
|
||||||
|
if (inst->siv_s2c)
|
||||||
|
SIV_DestroyInstance(inst->siv_s2c);
|
||||||
|
|
||||||
|
Free(inst->name);
|
||||||
|
Free(inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_nke_needed(NNC_Instance inst)
|
||||||
|
{
|
||||||
|
/* Force NKE if a NAK was received since last valid auth */
|
||||||
|
if (inst->nak_response && !inst->ok_response && inst->num_cookies > 0) {
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
DEBUG_LOG("Dropped cookies");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Force NKE if the keys encrypting the cookies are too old */
|
||||||
|
if (inst->num_cookies > 0 &&
|
||||||
|
SCH_GetLastEventMonoTime() - inst->last_nke_success > CNF_GetNtsRefresh())
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
|
||||||
|
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 = old_address.ip_addr;
|
||||||
|
if (new_address.port == 0)
|
||||||
|
new_address.port = old_address.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_nke_data(NNC_Instance inst)
|
||||||
|
{
|
||||||
|
NTP_Remote_Address ntp_address;
|
||||||
|
SIV_Algorithm siv;
|
||||||
|
NKE_Key c2s, s2c;
|
||||||
|
double now;
|
||||||
|
int got_data;
|
||||||
|
|
||||||
|
assert(is_nke_needed(inst));
|
||||||
|
|
||||||
|
now = SCH_GetLastEventMonoTime();
|
||||||
|
|
||||||
|
if (!inst->nke) {
|
||||||
|
if (now - inst->last_nke_attempt < MIN_NKE_RETRY_INTERVAL) {
|
||||||
|
DEBUG_LOG("Limiting NTS-KE request rate");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inst->name) {
|
||||||
|
LOG(LOGS_ERR, "Missing name of %s for NTS-KE",
|
||||||
|
UTI_IPToString(&inst->nts_address.ip_addr));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inst->nke = NKC_CreateInstance(&inst->nts_address, inst->name);
|
||||||
|
|
||||||
|
if (!NKC_Start(inst->nke))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
inst->last_nke_attempt = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NKC_IsActive(inst->nke))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
got_data = NKC_GetNtsData(inst->nke, &siv, &c2s, &s2c,
|
||||||
|
inst->cookies, &inst->num_cookies, NTS_MAX_COOKIES,
|
||||||
|
&ntp_address);
|
||||||
|
|
||||||
|
NKC_DestroyInstance(inst->nke);
|
||||||
|
inst->nke = NULL;
|
||||||
|
|
||||||
|
if (!got_data)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (!set_ntp_address(inst, &ntp_address)) {
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inst->cookie_index = 0;
|
||||||
|
|
||||||
|
if (inst->siv_c2s)
|
||||||
|
SIV_DestroyInstance(inst->siv_c2s);
|
||||||
|
if (inst->siv_s2c)
|
||||||
|
SIV_DestroyInstance(inst->siv_s2c);
|
||||||
|
|
||||||
|
inst->siv_c2s = SIV_CreateInstance(siv);
|
||||||
|
inst->siv_s2c = SIV_CreateInstance(siv);
|
||||||
|
|
||||||
|
if (!inst->siv_c2s || !inst->siv_s2c ||
|
||||||
|
!SIV_SetKey(inst->siv_c2s, c2s.key, c2s.length) ||
|
||||||
|
!SIV_SetKey(inst->siv_s2c, s2c.key, s2c.length)) {
|
||||||
|
DEBUG_LOG("Could not initialise SIV");
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inst->nak_response = 0;
|
||||||
|
|
||||||
|
inst->last_nke_success = now;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NNC_PrepareForAuth(NNC_Instance inst)
|
||||||
|
{
|
||||||
|
if (is_nke_needed(inst)) {
|
||||||
|
if (!get_nke_data(inst))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTI_GetRandomBytes(&inst->uniq_id, sizeof (inst->uniq_id));
|
||||||
|
UTI_GetRandomBytes(&inst->nonce, sizeof (inst->nonce));
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NNC_GenerateRequestAuth(NNC_Instance inst, NTP_Packet *packet,
|
||||||
|
NTP_PacketInfo *info)
|
||||||
|
{
|
||||||
|
NKE_Cookie *cookie;
|
||||||
|
int i, req_cookies;
|
||||||
|
|
||||||
|
if (inst->num_cookies == 0 || !inst->siv_c2s)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (info->mode != MODE_CLIENT)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
cookie = &inst->cookies[inst->cookie_index];
|
||||||
|
req_cookies = MIN(NTS_MAX_COOKIES - inst->num_cookies + 1,
|
||||||
|
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_AddField(packet, info, NTP_EF_NTS_COOKIE_PLACEHOLDER,
|
||||||
|
cookie->cookie, cookie->length))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!NNA_GenerateAuthEF(packet, info, inst->siv_c2s, inst->nonce, sizeof (inst->nonce),
|
||||||
|
(const unsigned char *)"", 0, NTP_MAX_V4_MAC_LENGTH + 4))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
inst->num_cookies--;
|
||||||
|
inst->cookie_index = (inst->cookie_index + 1) % NTS_MAX_COOKIES;
|
||||||
|
inst->ok_response = 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))
|
||||||
|
break;
|
||||||
|
|
||||||
|
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;
|
||||||
|
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 only one response per request */
|
||||||
|
if (inst->ok_response)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (!inst->siv_s2c)
|
||||||
|
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))
|
||||||
|
break;
|
||||||
|
|
||||||
|
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_s2c, parsed,
|
||||||
|
plaintext, sizeof (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->last_nke_attempt = -MIN_NKE_RETRY_INTERVAL;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
NNC_ChangeAddress(NNC_Instance inst, IPAddr *address)
|
||||||
|
{
|
||||||
|
if (inst->nke)
|
||||||
|
NKC_DestroyInstance(inst->nke);
|
||||||
|
|
||||||
|
inst->nke = NULL;
|
||||||
|
inst->num_cookies = 0;
|
||||||
|
inst->nts_address.ip_addr = *address;
|
||||||
|
|
||||||
|
reset_instance(inst);
|
||||||
|
|
||||||
|
DEBUG_LOG("NTS reset");
|
||||||
|
}
|
||||||
46
nts_ntp_client.h
Normal file
46
nts_ntp_client.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
typedef struct NNC_Instance_Record *NNC_Instance;
|
||||||
|
|
||||||
|
extern NNC_Instance NNC_CreateInstance(IPSockAddr *nts_address, const char *name,
|
||||||
|
const IPSockAddr *ntp_address);
|
||||||
|
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);
|
||||||
|
|
||||||
|
#endif
|
||||||
253
nts_ntp_server.c
Normal file
253
nts_ntp_server.c
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
/* Create an NTS-NTP server instance only if NTS-KE server is enabled */
|
||||||
|
if (!CNF_GetNtsServerCertFile() || !CNF_GetNtsServerKeyFile()) {
|
||||||
|
server = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
server = Malloc(sizeof (struct NtsServer));
|
||||||
|
server->siv = SIV_CreateInstance(AEAD_AES_SIV_CMAC_256);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
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_Cookie cookie;
|
||||||
|
NKE_Key c2s, s2c;
|
||||||
|
void *ef_body;
|
||||||
|
|
||||||
|
if (!server)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
*kod = 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))
|
||||||
|
break;
|
||||||
|
|
||||||
|
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))
|
||||||
|
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:
|
||||||
|
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, &c2s, &s2c)) {
|
||||||
|
*kod = NTP_KOD_NTS_NAK;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SIV_SetKey(server->siv, c2s.key, 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))
|
||||||
|
break;
|
||||||
|
|
||||||
|
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, s2c.key, s2c.length)) {
|
||||||
|
DEBUG_LOG("Could not set S2C key");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTI_GetRandomBytes(server->nonce, sizeof (server->nonce));
|
||||||
|
|
||||||
|
server->num_cookies = MIN(NTS_MAX_COOKIES, requested_cookies);
|
||||||
|
for (i = 0; i < server->num_cookies; i++)
|
||||||
|
if (!NKS_GenerateCookie(&c2s, &s2c, &server->cookies[i]))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
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 expected request */
|
||||||
|
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))
|
||||||
|
break;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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
|
||||||
34
pktlength.c
34
pktlength.c
@@ -114,8 +114,16 @@ static const struct request_length request_lengths[] = {
|
|||||||
client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
||||||
REQ_LENGTH_ENTRY(local, null), /* LOCAL2 */
|
REQ_LENGTH_ENTRY(local, null), /* LOCAL2 */
|
||||||
REQ_LENGTH_ENTRY(ntp_data, ntp_data), /* NTP_DATA */
|
REQ_LENGTH_ENTRY(ntp_data, ntp_data), /* NTP_DATA */
|
||||||
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_SERVER2 */
|
{ 0, 0 }, /* ADD_SERVER2 */
|
||||||
REQ_LENGTH_ENTRY(ntp_source, null), /* ADD_PEER2 */
|
{ 0, 0 }, /* ADD_PEER2 */
|
||||||
|
{ 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 */
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint16_t reply_lengths[] = {
|
static const uint16_t reply_lengths[] = {
|
||||||
@@ -123,19 +131,22 @@ static const uint16_t reply_lengths[] = {
|
|||||||
RPY_LENGTH_ENTRY(null), /* NULL */
|
RPY_LENGTH_ENTRY(null), /* NULL */
|
||||||
RPY_LENGTH_ENTRY(n_sources), /* N_SOURCES */
|
RPY_LENGTH_ENTRY(n_sources), /* N_SOURCES */
|
||||||
RPY_LENGTH_ENTRY(source_data), /* SOURCE_DATA */
|
RPY_LENGTH_ENTRY(source_data), /* SOURCE_DATA */
|
||||||
RPY_LENGTH_ENTRY(manual_timestamp), /* MANUAL_TIMESTAMP */
|
0, /* MANUAL_TIMESTAMP */
|
||||||
RPY_LENGTH_ENTRY(tracking), /* TRACKING */
|
RPY_LENGTH_ENTRY(tracking), /* TRACKING */
|
||||||
RPY_LENGTH_ENTRY(sourcestats), /* SOURCESTATS */
|
RPY_LENGTH_ENTRY(sourcestats), /* SOURCESTATS */
|
||||||
RPY_LENGTH_ENTRY(rtc), /* RTC */
|
RPY_LENGTH_ENTRY(rtc), /* RTC */
|
||||||
0, /* SUBNETS_ACCESSED - not supported */
|
0, /* SUBNETS_ACCESSED - not supported */
|
||||||
0, /* CLIENT_ACCESSES - not supported */
|
0, /* CLIENT_ACCESSES - not supported */
|
||||||
0, /* CLIENT_ACCESSES_BY_INDEX - 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(activity), /* ACTIVITY */
|
||||||
RPY_LENGTH_ENTRY(smoothing), /* SMOOTHING */
|
RPY_LENGTH_ENTRY(smoothing), /* SMOOTHING */
|
||||||
RPY_LENGTH_ENTRY(server_stats), /* SERVER_STATS */
|
RPY_LENGTH_ENTRY(server_stats), /* SERVER_STATS */
|
||||||
RPY_LENGTH_ENTRY(client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
RPY_LENGTH_ENTRY(client_accesses_by_index), /* CLIENT_ACCESSES_BY_INDEX2 */
|
||||||
RPY_LENGTH_ENTRY(ntp_data), /* NTP_DATA */
|
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 */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -192,21 +203,6 @@ PKL_ReplyLength(CMD_Reply *r)
|
|||||||
if (type < 1 || type >= N_REPLY_TYPES)
|
if (type < 1 || type >= N_REPLY_TYPES)
|
||||||
return 0;
|
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];
|
return reply_lengths[type];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
193
privops.c
193
privops.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Bryan Christianson 2015
|
* Copyright (C) Bryan Christianson 2015
|
||||||
* Copyright (C) Miroslav Lichvar 2016
|
* Copyright (C) Miroslav Lichvar 2017
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
#include "nameserv.h"
|
#include "nameserv.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "privops.h"
|
#include "privops.h"
|
||||||
|
#include "socket.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#define OP_ADJUSTTIME 1024
|
#define OP_ADJUSTTIME 1024
|
||||||
@@ -40,6 +41,7 @@
|
|||||||
#define OP_SETTIME 1026
|
#define OP_SETTIME 1026
|
||||||
#define OP_BINDSOCKET 1027
|
#define OP_BINDSOCKET 1027
|
||||||
#define OP_NAME2IPADDRESS 1028
|
#define OP_NAME2IPADDRESS 1028
|
||||||
|
#define OP_RELOADDNS 1029
|
||||||
#define OP_QUIT 1099
|
#define OP_QUIT 1099
|
||||||
|
|
||||||
union sockaddr_in46 {
|
union sockaddr_in46 {
|
||||||
@@ -157,7 +159,7 @@ res_fatal(PrvResponse *res, const char *fmt, ...)
|
|||||||
static int
|
static int
|
||||||
send_response(int fd, const PrvResponse *res)
|
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 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -169,37 +171,23 @@ send_response(int fd, const PrvResponse *res)
|
|||||||
static int
|
static int
|
||||||
receive_from_daemon(int fd, PrvRequest *req)
|
receive_from_daemon(int fd, PrvRequest *req)
|
||||||
{
|
{
|
||||||
struct msghdr msg;
|
SCK_Message message;
|
||||||
struct cmsghdr *cmsg;
|
|
||||||
struct iovec iov;
|
|
||||||
char cmsgbuf[256];
|
|
||||||
|
|
||||||
iov.iov_base = req;
|
if (!SCK_ReceiveMessage(fd, &message, SCK_FLAG_MSG_DESCRIPTOR) ||
|
||||||
iov.iov_len = sizeof (*req);
|
message.length != 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))
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
memcpy(req, message.data, sizeof (*req));
|
||||||
|
|
||||||
if (req->op == OP_BINDSOCKET) {
|
if (req->op == OP_BINDSOCKET) {
|
||||||
/* extract transferred descriptor */
|
req->data.bind_socket.sock = message.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));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return error if valid descriptor not found */
|
/* return error if valid descriptor not found */
|
||||||
if (req->data.bind_socket.sock < 0)
|
if (req->data.bind_socket.sock < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
} else if (message.descriptor >= 0) {
|
||||||
|
SCK_CloseSocket(message.descriptor);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -256,8 +244,7 @@ do_set_time(const ReqSetTime *req, PrvResponse *res)
|
|||||||
static void
|
static void
|
||||||
do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
||||||
{
|
{
|
||||||
unsigned short port;
|
IPSockAddr ip_saddr;
|
||||||
IPAddr ip;
|
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
struct sockaddr *sa;
|
struct sockaddr *sa;
|
||||||
socklen_t sa_len;
|
socklen_t sa_len;
|
||||||
@@ -266,10 +253,11 @@ do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
|||||||
sa_len = req->sa_len;
|
sa_len = req->sa_len;
|
||||||
sock_fd = req->sock;
|
sock_fd = req->sock;
|
||||||
|
|
||||||
UTI_SockaddrToIPAndPort(sa, &ip, &port);
|
SCK_SockaddrToIPSockAddr(sa, sa_len, &ip_saddr);
|
||||||
if (port && port != CNF_GetNTPPort()) {
|
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||||
close(sock_fd);
|
ip_saddr.port != CNF_GetAcquisitionPort()) {
|
||||||
res_fatal(res, "Invalid port %d", port);
|
SCK_CloseSocket(sock_fd);
|
||||||
|
res_fatal(res, "Invalid port %d", ip_saddr.port);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +266,7 @@ do_bind_socket(ReqBindSocket *req, PrvResponse *res)
|
|||||||
res->res_errno = errno;
|
res->res_errno = errno;
|
||||||
|
|
||||||
/* sock is still open on daemon side, but we're done with it in the helper */
|
/* sock is still open on daemon side, but we're done with it in the helper */
|
||||||
close(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -293,8 +281,6 @@ do_name_to_ipaddress(ReqName2IPAddress *req, PrvResponse *res)
|
|||||||
/* make sure the string is terminated */
|
/* make sure the string is terminated */
|
||||||
req->name[sizeof (req->name) - 1] = '\0';
|
req->name[sizeof (req->name) - 1] = '\0';
|
||||||
|
|
||||||
DNS_Reload();
|
|
||||||
|
|
||||||
res->rc = DNS_Name2IPAddress(req->name, res->data.name_to_ipaddress.addresses,
|
res->rc = DNS_Name2IPAddress(req->name, res->data.name_to_ipaddress.addresses,
|
||||||
DNS_MAX_ADDRESSES);
|
DNS_MAX_ADDRESSES);
|
||||||
}
|
}
|
||||||
@@ -302,6 +288,19 @@ do_name_to_ipaddress(ReqName2IPAddress *req, PrvResponse *res)
|
|||||||
|
|
||||||
/* ======================================================================= */
|
/* ======================================================================= */
|
||||||
|
|
||||||
|
/* HELPER - perform DNS_Reload() */
|
||||||
|
|
||||||
|
#ifdef PRIVOPS_RELOADDNS
|
||||||
|
static void
|
||||||
|
do_reload_dns(PrvResponse *res)
|
||||||
|
{
|
||||||
|
DNS_Reload();
|
||||||
|
res->rc = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ======================================================================= */
|
||||||
|
|
||||||
/* HELPER - main loop - action requests from the daemon */
|
/* HELPER - main loop - action requests from the daemon */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -343,6 +342,11 @@ helper_main(int fd)
|
|||||||
case OP_NAME2IPADDRESS:
|
case OP_NAME2IPADDRESS:
|
||||||
do_name_to_ipaddress(&req.data.name_to_ipaddress, &res);
|
do_name_to_ipaddress(&req.data.name_to_ipaddress, &res);
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
|
#ifdef PRIVOPS_RELOADDNS
|
||||||
|
case OP_RELOADDNS:
|
||||||
|
do_reload_dns(&res);
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
case OP_QUIT:
|
case OP_QUIT:
|
||||||
quit = 1;
|
quit = 1;
|
||||||
@@ -356,7 +360,7 @@ helper_main(int fd)
|
|||||||
send_response(fd, &res);
|
send_response(fd, &res);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
SCK_CloseSocket(fd);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,16 +373,16 @@ receive_response(PrvResponse *res)
|
|||||||
{
|
{
|
||||||
int resp_len;
|
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)
|
if (resp_len < 0)
|
||||||
LOG_FATAL(LOGF_PrivOps, "Could not read from helper : %s", strerror(errno));
|
LOG_FATAL("Could not read from helper : %s", strerror(errno));
|
||||||
if (resp_len != sizeof (*res))
|
if (resp_len != sizeof (*res))
|
||||||
LOG_FATAL(LOGF_PrivOps, "Invalid helper response");
|
LOG_FATAL("Invalid helper response");
|
||||||
|
|
||||||
if (res->fatal_error)
|
if (res->fatal_error)
|
||||||
LOG_FATAL(LOGF_PrivOps, "Error in helper : %s", res->data.fatal_msg.msg);
|
LOG_FATAL("Error in helper : %s", res->data.fatal_msg.msg);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_PrivOps, "Received response rc=%d", res->rc);
|
DEBUG_LOG("Received response rc=%d", res->rc);
|
||||||
|
|
||||||
/* if operation failed in the helper, set errno so daemon can print log message */
|
/* if operation failed in the helper, set errno so daemon can print log message */
|
||||||
if (res->res_errno)
|
if (res->res_errno)
|
||||||
@@ -392,47 +396,28 @@ receive_response(PrvResponse *res)
|
|||||||
static void
|
static void
|
||||||
send_request(PrvRequest *req)
|
send_request(PrvRequest *req)
|
||||||
{
|
{
|
||||||
struct msghdr msg;
|
SCK_Message message;
|
||||||
struct iovec iov;
|
int flags;
|
||||||
char cmsgbuf[256];
|
|
||||||
|
|
||||||
iov.iov_base = req;
|
SCK_InitMessage(&message, SCK_ADDR_UNSPEC);
|
||||||
iov.iov_len = sizeof (*req);
|
|
||||||
|
|
||||||
msg.msg_name = NULL;
|
message.data = req;
|
||||||
msg.msg_namelen = 0;
|
message.length = sizeof (*req);
|
||||||
msg.msg_iov = &iov;
|
flags = 0;
|
||||||
msg.msg_iovlen = 1;
|
|
||||||
msg.msg_control = NULL;
|
|
||||||
msg.msg_controllen = 0;
|
|
||||||
msg.msg_flags = 0;
|
|
||||||
|
|
||||||
if (req->op == OP_BINDSOCKET) {
|
if (req->op == OP_BINDSOCKET) {
|
||||||
/* send file descriptor as a control message */
|
/* send file descriptor as a control message */
|
||||||
struct cmsghdr *cmsg;
|
message.descriptor = req->data.bind_socket.sock;
|
||||||
int *ptr_send_fd;
|
flags |= SCK_FLAG_MSG_DESCRIPTOR;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sendmsg(helper_fd, &msg, 0) < 0) {
|
if (!SCK_SendMessage(helper_fd, &message, flags)) {
|
||||||
/* don't try to send another request from exit() */
|
/* don't try to send another request from exit() */
|
||||||
helper_fd = -1;
|
helper_fd = -1;
|
||||||
LOG_FATAL(LOGF_PrivOps, "Could not send to helper : %s", strerror(errno));
|
LOG_FATAL("Could not send to helper : %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_PrivOps, "Sent request op=%d", req->op);
|
DEBUG_LOG("Sent request op=%d", req->op);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ======================================================================= */
|
/* ======================================================================= */
|
||||||
@@ -556,13 +541,14 @@ PRV_SetTime(const struct timeval *tp, const struct timezone *tzp)
|
|||||||
int
|
int
|
||||||
PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
||||||
{
|
{
|
||||||
|
IPSockAddr ip_saddr;
|
||||||
PrvRequest req;
|
PrvRequest req;
|
||||||
PrvResponse res;
|
PrvResponse res;
|
||||||
IPAddr ip;
|
|
||||||
unsigned short port;
|
|
||||||
|
|
||||||
UTI_SockaddrToIPAndPort(address, &ip, &port);
|
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
|
||||||
assert(!port || port == CNF_GetNTPPort());
|
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
||||||
|
ip_saddr.port != CNF_GetAcquisitionPort())
|
||||||
|
assert(0);
|
||||||
|
|
||||||
if (!have_helper())
|
if (!have_helper())
|
||||||
return bind(sock, address, address_len);
|
return bind(sock, address, address_len);
|
||||||
@@ -571,6 +557,7 @@ PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
|||||||
req.op = OP_BINDSOCKET;
|
req.op = OP_BINDSOCKET;
|
||||||
req.data.bind_socket.sock = sock;
|
req.data.bind_socket.sock = sock;
|
||||||
req.data.bind_socket.sa_len = address_len;
|
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);
|
memcpy(&req.data.bind_socket.sa.u, address, address_len);
|
||||||
|
|
||||||
submit_request(&req, &res);
|
submit_request(&req, &res);
|
||||||
@@ -598,7 +585,6 @@ PRV_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
|||||||
req.op = OP_NAME2IPADDRESS;
|
req.op = OP_NAME2IPADDRESS;
|
||||||
if (snprintf(req.data.name_to_ipaddress.name, sizeof (req.data.name_to_ipaddress.name),
|
if (snprintf(req.data.name_to_ipaddress.name, sizeof (req.data.name_to_ipaddress.name),
|
||||||
"%s", name) >= sizeof (req.data.name_to_ipaddress.name)) {
|
"%s", name) >= sizeof (req.data.name_to_ipaddress.name)) {
|
||||||
DEBUG_LOG(LOGF_PrivOps, "Name too long");
|
|
||||||
return DNS_Failure;
|
return DNS_Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -613,6 +599,30 @@ PRV_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs)
|
|||||||
|
|
||||||
/* ======================================================================= */
|
/* ======================================================================= */
|
||||||
|
|
||||||
|
/* DAEMON - request res_init() */
|
||||||
|
|
||||||
|
#ifdef PRIVOPS_RELOADDNS
|
||||||
|
void
|
||||||
|
PRV_ReloadDNS(void)
|
||||||
|
{
|
||||||
|
PrvRequest req;
|
||||||
|
PrvResponse res;
|
||||||
|
|
||||||
|
if (!have_helper()) {
|
||||||
|
DNS_Reload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&req, 0, sizeof (req));
|
||||||
|
req.op = OP_RELOADDNS;
|
||||||
|
|
||||||
|
submit_request(&req, &res);
|
||||||
|
assert(!res.rc);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ======================================================================= */
|
||||||
|
|
||||||
void
|
void
|
||||||
PRV_Initialise(void)
|
PRV_Initialise(void)
|
||||||
{
|
{
|
||||||
@@ -628,44 +638,37 @@ void
|
|||||||
PRV_StartHelper(void)
|
PRV_StartHelper(void)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int fd, sock_pair[2];
|
int fd, sock_fd1, sock_fd2;
|
||||||
|
|
||||||
if (have_helper())
|
if (have_helper())
|
||||||
LOG_FATAL(LOGF_PrivOps, "Helper already running");
|
LOG_FATAL("Helper already running");
|
||||||
|
|
||||||
if (
|
sock_fd1 = SCK_OpenUnixSocketPair(SCK_FLAG_BLOCK, &sock_fd2);
|
||||||
#ifdef SOCK_SEQPACKET
|
|
||||||
socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock_pair) &&
|
|
||||||
#endif
|
|
||||||
socketpair(AF_UNIX, SOCK_DGRAM, 0, sock_pair))
|
|
||||||
LOG_FATAL(LOGF_PrivOps, "socketpair() failed : %s", strerror(errno));
|
|
||||||
|
|
||||||
UTI_FdSetCloexec(sock_pair[0]);
|
|
||||||
UTI_FdSetCloexec(sock_pair[1]);
|
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
LOG_FATAL(LOGF_PrivOps, "fork() failed : %s", strerror(errno));
|
LOG_FATAL("fork() failed : %s", strerror(errno));
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* child process */
|
/* child process */
|
||||||
close(sock_pair[0]);
|
SCK_CloseSocket(sock_fd1);
|
||||||
|
|
||||||
/* close other descriptors inherited from the parent process */
|
/* close other descriptors inherited from the parent process, except
|
||||||
for (fd = 0; fd < 1024; fd++) {
|
stdin, stdout, and stderr */
|
||||||
if (fd != sock_pair[1])
|
for (fd = STDERR_FILENO + 1; fd < 1024; fd++) {
|
||||||
|
if (fd != sock_fd2)
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ignore signals, the process will exit on OP_QUIT request */
|
/* 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 {
|
} else {
|
||||||
/* parent process */
|
/* parent process */
|
||||||
close(sock_pair[1]);
|
SCK_CloseSocket(sock_fd2);
|
||||||
helper_fd = sock_pair[0];
|
helper_fd = sock_fd1;
|
||||||
helper_pid = pid;
|
helper_pid = pid;
|
||||||
|
|
||||||
/* stop the helper even when not exiting cleanly from the main function */
|
/* stop the helper even when not exiting cleanly from the main function */
|
||||||
|
|||||||
@@ -58,6 +58,12 @@ int PRV_Name2IPAddress(const char *name, IPAddr *ip_addrs, int max_addrs);
|
|||||||
#define PRV_Name2IPAddress DNS_Name2IPAddress
|
#define PRV_Name2IPAddress DNS_Name2IPAddress
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef PRIVOPS_RELOADDNS
|
||||||
|
void PRV_ReloadDNS(void);
|
||||||
|
#else
|
||||||
|
#define PRV_ReloadDNS DNS_Reload
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef PRIVOPS_HELPER
|
#ifdef PRIVOPS_HELPER
|
||||||
void PRV_Initialise(void);
|
void PRV_Initialise(void);
|
||||||
void PRV_StartHelper(void);
|
void PRV_StartHelper(void);
|
||||||
|
|||||||
684
refclock.c
684
refclock.c
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014
|
* Copyright (C) Miroslav Lichvar 2009-2011, 2013-2014, 2016-2019
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -37,6 +37,7 @@
|
|||||||
#include "sources.h"
|
#include "sources.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "regress.h"
|
#include "regress.h"
|
||||||
|
#include "samplefilt.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
|
||||||
/* list of refclock drivers */
|
/* list of refclock drivers */
|
||||||
@@ -75,15 +76,19 @@ struct RCL_Instance_Record {
|
|||||||
int driver_polled;
|
int driver_polled;
|
||||||
int poll;
|
int poll;
|
||||||
int leap_status;
|
int leap_status;
|
||||||
|
int pps_forced;
|
||||||
int pps_rate;
|
int pps_rate;
|
||||||
int pps_active;
|
int pps_active;
|
||||||
int max_lock_age;
|
int max_lock_age;
|
||||||
struct MedianFilter filter;
|
int stratum;
|
||||||
|
int tai;
|
||||||
uint32_t ref_id;
|
uint32_t ref_id;
|
||||||
uint32_t lock_ref;
|
uint32_t lock_ref;
|
||||||
double offset;
|
double offset;
|
||||||
double delay;
|
double delay;
|
||||||
double precision;
|
double precision;
|
||||||
|
double pulse_width;
|
||||||
|
SPF_Instance filter;
|
||||||
SCH_TimeoutID timeout_id;
|
SCH_TimeoutID timeout_id;
|
||||||
SRC_Instance source;
|
SRC_Instance source;
|
||||||
};
|
};
|
||||||
@@ -93,7 +98,7 @@ static ARR_Instance refclocks;
|
|||||||
|
|
||||||
static LOG_FileID logfileid;
|
static LOG_FileID logfileid;
|
||||||
|
|
||||||
static int valid_sample_time(RCL_Instance instance, struct timespec *raw, struct timespec *cooked);
|
static int valid_sample_time(RCL_Instance instance, struct timespec *sample_time);
|
||||||
static int pps_stratum(RCL_Instance instance, struct timespec *ts);
|
static int pps_stratum(RCL_Instance instance, struct timespec *ts);
|
||||||
static void poll_timeout(void *arg);
|
static void poll_timeout(void *arg);
|
||||||
static void slew_samples(struct timespec *raw, struct timespec *cooked, double dfreq,
|
static void slew_samples(struct timespec *raw, struct timespec *cooked, double dfreq,
|
||||||
@@ -101,18 +106,6 @@ static void slew_samples(struct timespec *raw, struct timespec *cooked, double d
|
|||||||
static void add_dispersion(double dispersion, void *anything);
|
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 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
|
static RCL_Instance
|
||||||
get_refclock(unsigned int index)
|
get_refclock(unsigned int index)
|
||||||
{
|
{
|
||||||
@@ -147,7 +140,7 @@ RCL_Finalise(void)
|
|||||||
if (inst->driver->fini)
|
if (inst->driver->fini)
|
||||||
inst->driver->fini(inst);
|
inst->driver->fini(inst);
|
||||||
|
|
||||||
filter_fini(&inst->filter);
|
SPF_DestroyInstance(inst->filter);
|
||||||
Free(inst->driver_parameter);
|
Free(inst->driver_parameter);
|
||||||
SRC_DestroyInstance(inst->source);
|
SRC_DestroyInstance(inst->source);
|
||||||
Free(inst);
|
Free(inst);
|
||||||
@@ -164,7 +157,6 @@ RCL_Finalise(void)
|
|||||||
int
|
int
|
||||||
RCL_AddRefclock(RefclockParameters *params)
|
RCL_AddRefclock(RefclockParameters *params)
|
||||||
{
|
{
|
||||||
int pps_source = 0;
|
|
||||||
RCL_Instance inst;
|
RCL_Instance inst;
|
||||||
|
|
||||||
inst = MallocNew(struct RCL_Instance_Record);
|
inst = MallocNew(struct RCL_Instance_Record);
|
||||||
@@ -172,27 +164,21 @@ RCL_AddRefclock(RefclockParameters *params)
|
|||||||
|
|
||||||
if (strcmp(params->driver_name, "SHM") == 0) {
|
if (strcmp(params->driver_name, "SHM") == 0) {
|
||||||
inst->driver = &RCL_SHM_driver;
|
inst->driver = &RCL_SHM_driver;
|
||||||
inst->precision = 1e-6;
|
|
||||||
} else if (strcmp(params->driver_name, "SOCK") == 0) {
|
} else if (strcmp(params->driver_name, "SOCK") == 0) {
|
||||||
inst->driver = &RCL_SOCK_driver;
|
inst->driver = &RCL_SOCK_driver;
|
||||||
inst->precision = 1e-9;
|
|
||||||
pps_source = 1;
|
|
||||||
} else if (strcmp(params->driver_name, "PPS") == 0) {
|
} else if (strcmp(params->driver_name, "PPS") == 0) {
|
||||||
inst->driver = &RCL_PPS_driver;
|
inst->driver = &RCL_PPS_driver;
|
||||||
inst->precision = 1e-9;
|
|
||||||
pps_source = 1;
|
|
||||||
} else if (strcmp(params->driver_name, "PHC") == 0) {
|
} else if (strcmp(params->driver_name, "PHC") == 0) {
|
||||||
inst->driver = &RCL_PHC_driver;
|
inst->driver = &RCL_PHC_driver;
|
||||||
inst->precision = 1e-9;
|
|
||||||
} else {
|
} else {
|
||||||
LOG_FATAL(LOGF_Refclock, "unknown refclock driver %s", params->driver_name);
|
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(LOGF_Refclock, "refclock driver %s is not compiled in", params->driver_name);
|
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->data = NULL;
|
||||||
inst->driver_parameter = params->driver_parameter;
|
inst->driver_parameter = params->driver_parameter;
|
||||||
@@ -201,14 +187,18 @@ RCL_AddRefclock(RefclockParameters *params)
|
|||||||
inst->poll = params->poll;
|
inst->poll = params->poll;
|
||||||
inst->driver_polled = 0;
|
inst->driver_polled = 0;
|
||||||
inst->leap_status = LEAP_Normal;
|
inst->leap_status = LEAP_Normal;
|
||||||
|
inst->pps_forced = params->pps_forced;
|
||||||
inst->pps_rate = params->pps_rate;
|
inst->pps_rate = params->pps_rate;
|
||||||
inst->pps_active = 0;
|
inst->pps_active = 0;
|
||||||
inst->max_lock_age = params->max_lock_age;
|
inst->max_lock_age = params->max_lock_age;
|
||||||
|
inst->stratum = params->stratum;
|
||||||
|
inst->tai = params->tai;
|
||||||
inst->lock_ref = params->lock_ref_id;
|
inst->lock_ref = params->lock_ref_id;
|
||||||
inst->offset = params->offset;
|
inst->offset = params->offset;
|
||||||
inst->delay = params->delay;
|
inst->delay = params->delay;
|
||||||
if (params->precision > 0.0)
|
inst->precision = LCL_GetSysPrecisionAsQuantum();
|
||||||
inst->precision = params->precision;
|
inst->precision = MAX(inst->precision, params->precision);
|
||||||
|
inst->pulse_width = params->pulse_width;
|
||||||
inst->timeout_id = -1;
|
inst->timeout_id = -1;
|
||||||
inst->source = NULL;
|
inst->source = NULL;
|
||||||
|
|
||||||
@@ -221,12 +211,8 @@ RCL_AddRefclock(RefclockParameters *params)
|
|||||||
inst->driver_parameter[i] = '\0';
|
inst->driver_parameter[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pps_source) {
|
if (inst->pps_rate < 1)
|
||||||
if (inst->pps_rate < 1)
|
inst->pps_rate = 1;
|
||||||
inst->pps_rate = 1;
|
|
||||||
} else {
|
|
||||||
inst->pps_rate = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params->ref_id)
|
if (params->ref_id)
|
||||||
inst->ref_id = params->ref_id;
|
inst->ref_id = params->ref_id;
|
||||||
@@ -251,25 +237,26 @@ RCL_AddRefclock(RefclockParameters *params)
|
|||||||
max_samples = 1 << (inst->poll - inst->driver_poll);
|
max_samples = 1 << (inst->poll - inst->driver_poll);
|
||||||
if (max_samples < params->filter_length) {
|
if (max_samples < params->filter_length) {
|
||||||
if (max_samples < 4) {
|
if (max_samples < 4) {
|
||||||
LOG(LOGS_WARN, LOGF_Refclock, "Setting filter length for %s to %d",
|
LOG(LOGS_WARN, "Setting filter length for %s to %d",
|
||||||
UTI_RefidToString(inst->ref_id), max_samples);
|
UTI_RefidToString(inst->ref_id), max_samples);
|
||||||
}
|
}
|
||||||
params->filter_length = max_samples;
|
params->filter_length = max_samples;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inst->driver->init)
|
if (inst->driver->init && !inst->driver->init(inst))
|
||||||
if (!inst->driver->init(inst)) {
|
LOG_FATAL("refclock %s initialisation failed", params->driver_name);
|
||||||
LOG_FATAL(LOGF_Refclock, "refclock %s initialisation failed", params->driver_name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, params->sel_options, NULL,
|
||||||
params->min_samples, params->max_samples);
|
params->min_samples, params->max_samples, 0.0, 0.0);
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock %s refid=%s poll=%d dpoll=%d filter=%d",
|
DEBUG_LOG("refclock %s refid=%s poll=%d dpoll=%d filter=%d",
|
||||||
params->driver_name, UTI_RefidToString(inst->ref_id),
|
params->driver_name, UTI_RefidToString(inst->ref_id),
|
||||||
inst->poll, inst->driver_poll, params->filter_length);
|
inst->poll, inst->driver_poll, params->filter_length);
|
||||||
|
|
||||||
@@ -338,44 +325,122 @@ RCL_GetDriverParameter(RCL_Instance instance)
|
|||||||
return instance->driver_parameter;
|
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 *
|
char *
|
||||||
RCL_GetDriverOption(RCL_Instance instance, char *name)
|
RCL_GetDriverOption(RCL_Instance instance, char *name)
|
||||||
{
|
{
|
||||||
char *s, *e;
|
char *option;
|
||||||
int n;
|
int len;
|
||||||
|
|
||||||
s = instance->driver_parameter;
|
len = strlen(name);
|
||||||
e = s + instance->driver_parameter_length;
|
|
||||||
n = strlen(name);
|
|
||||||
|
|
||||||
while (1) {
|
for (option = get_next_driver_option(instance, NULL);
|
||||||
s += strlen(s) + 1;
|
option;
|
||||||
if (s >= e)
|
option = get_next_driver_option(instance, option)) {
|
||||||
break;
|
if (!strncmp(name, option, len)) {
|
||||||
if (!strncmp(name, s, n)) {
|
if (option[len] == '=')
|
||||||
if (s[n] == '=')
|
return option + len + 1;
|
||||||
return s + n + 1;
|
if (option[len] == '\0')
|
||||||
if (s[n] == '\0')
|
return option + len;
|
||||||
return s + n;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
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;
|
||||||
|
|
||||||
|
/* Handle special case when PPS is used with the local reference */
|
||||||
|
if (instance->pps_active && instance->lock_ref == -1)
|
||||||
|
sample.stratum = pps_stratum(instance, &sample.time);
|
||||||
|
else
|
||||||
|
sample.stratum = instance->stratum;
|
||||||
|
|
||||||
|
return SPF_AccumulateSample(instance->filter, &sample);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap)
|
RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap)
|
||||||
{
|
{
|
||||||
double correction, dispersion;
|
double correction, dispersion;
|
||||||
struct timespec cooked_time;
|
struct timespec cooked_time;
|
||||||
|
|
||||||
|
if (instance->pps_forced)
|
||||||
|
return RCL_AddPulse(instance, sample_time, -offset);
|
||||||
|
|
||||||
LCL_GetOffsetCorrection(sample_time, &correction, &dispersion);
|
LCL_GetOffsetCorrection(sample_time, &correction, &dispersion);
|
||||||
UTI_AddDoubleToTimespec(sample_time, correction, &cooked_time);
|
UTI_AddDoubleToTimespec(sample_time, correction, &cooked_time);
|
||||||
dispersion += instance->precision;
|
dispersion += instance->precision;
|
||||||
|
|
||||||
/* Make sure the timestamp and offset provided by the driver are sane */
|
/* Make sure the timestamp and offset provided by the driver are sane */
|
||||||
if (!UTI_IsTimeOffsetSane(sample_time, offset) ||
|
if (!UTI_IsTimeOffsetSane(sample_time, offset) ||
|
||||||
!valid_sample_time(instance, sample_time, &cooked_time))
|
!valid_sample_time(instance, &cooked_time))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
switch (leap) {
|
switch (leap) {
|
||||||
@@ -385,11 +450,19 @@ RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset
|
|||||||
instance->leap_status = leap;
|
instance->leap_status = leap;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock sample ignored bad leap %d", leap);
|
DEBUG_LOG("refclock sample ignored bad leap %d", leap);
|
||||||
return 0;
|
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;
|
instance->pps_active = 0;
|
||||||
|
|
||||||
log_sample(instance, &cooked_time, 0, 0, offset, offset - correction + instance->offset, dispersion);
|
log_sample(instance, &cooked_time, 0, 0, offset, offset - correction + instance->offset, dispersion);
|
||||||
@@ -404,24 +477,57 @@ RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset
|
|||||||
int
|
int
|
||||||
RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second)
|
RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second)
|
||||||
{
|
{
|
||||||
double correction, dispersion, offset;
|
double correction, dispersion;
|
||||||
struct timespec cooked_time;
|
struct timespec cooked_time;
|
||||||
|
|
||||||
|
LCL_GetOffsetCorrection(pulse_time, &correction, &dispersion);
|
||||||
|
UTI_AddDoubleToTimespec(pulse_time, correction, &cooked_time);
|
||||||
|
second += correction;
|
||||||
|
|
||||||
|
if (!UTI_IsTimeOffsetSane(pulse_time, 0.0))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return RCL_AddCookedPulse(instance, &cooked_time, second, dispersion, correction);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
check_pulse_edge(RCL_Instance instance, double offset, double distance)
|
||||||
|
{
|
||||||
|
double max_error;
|
||||||
|
|
||||||
|
if (instance->pulse_width <= 0.0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
max_error = 1.0 / instance->pps_rate - instance->pulse_width;
|
||||||
|
max_error = MIN(instance->pulse_width, max_error);
|
||||||
|
max_error *= 0.5;
|
||||||
|
|
||||||
|
if (fabs(offset) > max_error || distance > max_error) {
|
||||||
|
DEBUG_LOG("refclock pulse ignored offset=%.9f distance=%.9f max_error=%.9f",
|
||||||
|
offset, distance, max_error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||||
|
double second, double dispersion, double raw_correction)
|
||||||
|
{
|
||||||
|
double offset;
|
||||||
int rate;
|
int rate;
|
||||||
NTP_Leap leap;
|
NTP_Leap leap;
|
||||||
|
|
||||||
leap = LEAP_Normal;
|
if (!UTI_IsTimeOffsetSane(cooked_time, second) ||
|
||||||
LCL_GetOffsetCorrection(pulse_time, &correction, &dispersion);
|
!valid_sample_time(instance, cooked_time))
|
||||||
UTI_AddDoubleToTimespec(pulse_time, correction, &cooked_time);
|
|
||||||
dispersion += instance->precision;
|
|
||||||
|
|
||||||
if (!UTI_IsTimeOffsetSane(pulse_time, 0.0) ||
|
|
||||||
!valid_sample_time(instance, pulse_time, &cooked_time))
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
leap = LEAP_Normal;
|
||||||
|
dispersion += instance->precision;
|
||||||
rate = instance->pps_rate;
|
rate = instance->pps_rate;
|
||||||
assert(rate > 0);
|
|
||||||
|
|
||||||
offset = -second - correction + instance->offset;
|
offset = -second + instance->offset;
|
||||||
|
|
||||||
/* Adjust the offset to [-0.5/rate, 0.5/rate) interval */
|
/* Adjust the offset to [-0.5/rate, 0.5/rate) interval */
|
||||||
offset -= (long)(offset * rate) / (double)rate;
|
offset -= (long)(offset * rate) / (double)rate;
|
||||||
@@ -432,44 +538,47 @@ RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second)
|
|||||||
|
|
||||||
if (instance->lock_ref != -1) {
|
if (instance->lock_ref != -1) {
|
||||||
RCL_Instance lock_refclock;
|
RCL_Instance lock_refclock;
|
||||||
struct timespec ref_sample_time;
|
NTP_Sample ref_sample;
|
||||||
double sample_diff, ref_offset, ref_dispersion, shift;
|
double sample_diff, shift;
|
||||||
|
|
||||||
lock_refclock = get_refclock(instance->lock_ref);
|
lock_refclock = get_refclock(instance->lock_ref);
|
||||||
|
|
||||||
if (!filter_get_last_sample(&lock_refclock->filter,
|
if (!SPF_GetLastSample(lock_refclock->filter, &ref_sample)) {
|
||||||
&ref_sample_time, &ref_offset, &ref_dispersion)) {
|
DEBUG_LOG("refclock pulse ignored no ref sample");
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock pulse ignored no ref sample");
|
|
||||||
return 0;
|
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) {
|
if (fabs(sample_diff) >= (double)instance->max_lock_age / rate) {
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock pulse ignored samplediff=%.9f",
|
DEBUG_LOG("refclock pulse ignored samplediff=%.9f",
|
||||||
sample_diff);
|
sample_diff);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Align the offset to the reference sample */
|
/* Align the offset to the reference sample */
|
||||||
if ((ref_offset - offset) >= 0.0)
|
if ((ref_sample.offset - offset) >= 0.0)
|
||||||
shift = (long)((ref_offset - offset) * rate + 0.5) / (double)rate;
|
shift = (long)((ref_sample.offset - offset) * rate + 0.5) / (double)rate;
|
||||||
else
|
else
|
||||||
shift = (long)((ref_offset - offset) * rate - 0.5) / (double)rate;
|
shift = (long)((ref_sample.offset - offset) * rate - 0.5) / (double)rate;
|
||||||
|
|
||||||
offset += shift;
|
offset += shift;
|
||||||
|
|
||||||
if (fabs(ref_offset - offset) + ref_dispersion + dispersion >= 0.2 / rate) {
|
if (fabs(ref_sample.offset - offset) +
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock pulse ignored offdiff=%.9f refdisp=%.9f disp=%.9f",
|
ref_sample.root_dispersion + dispersion >= 0.2 / rate) {
|
||||||
ref_offset - offset, ref_dispersion, dispersion);
|
DEBUG_LOG("refclock pulse ignored offdiff=%.9f refdisp=%.9f disp=%.9f",
|
||||||
|
ref_sample.offset - offset, ref_sample.root_dispersion, dispersion);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!check_pulse_edge(instance, ref_sample.offset - offset, 0.0))
|
||||||
|
return 0;
|
||||||
|
|
||||||
leap = lock_refclock->leap_status;
|
leap = lock_refclock->leap_status;
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock pulse second=%.9f offset=%.9f offdiff=%.9f samplediff=%.9f",
|
DEBUG_LOG("refclock pulse offset=%.9f offdiff=%.9f samplediff=%.9f",
|
||||||
second, offset, ref_offset - offset, sample_diff);
|
offset, ref_sample.offset - offset, sample_diff);
|
||||||
} else {
|
} else {
|
||||||
struct timespec ref_time;
|
struct timespec ref_time;
|
||||||
int is_synchronised, stratum;
|
int is_synchronised, stratum;
|
||||||
@@ -479,24 +588,30 @@ RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second)
|
|||||||
/* Ignore the pulse if we are not well synchronized and the local
|
/* Ignore the pulse if we are not well synchronized and the local
|
||||||
reference is not active */
|
reference is not active */
|
||||||
|
|
||||||
REF_GetReferenceParams(&cooked_time, &is_synchronised, &leap, &stratum,
|
REF_GetReferenceParams(cooked_time, &is_synchronised, &leap, &stratum,
|
||||||
&ref_id, &ref_time, &root_delay, &root_dispersion);
|
&ref_id, &ref_time, &root_delay, &root_dispersion);
|
||||||
distance = fabs(root_delay) / 2 + root_dispersion;
|
distance = fabs(root_delay) / 2 + root_dispersion;
|
||||||
|
|
||||||
if (leap == LEAP_Unsynchronised || distance >= 0.5 / rate) {
|
if (leap == LEAP_Unsynchronised || distance >= 0.5 / rate) {
|
||||||
DEBUG_LOG(LOGF_Refclock, "refclock pulse ignored second=%.9f sync=%d dist=%.9f",
|
DEBUG_LOG("refclock pulse ignored offset=%.9f sync=%d dist=%.9f",
|
||||||
second, leap != LEAP_Unsynchronised, distance);
|
offset, leap != LEAP_Unsynchronised, distance);
|
||||||
/* Drop also all stored samples */
|
/* Drop also all stored samples */
|
||||||
filter_reset(&instance->filter);
|
SPF_DropSamples(instance->filter);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!check_pulse_edge(instance, offset, distance))
|
||||||
|
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->leap_status = leap;
|
||||||
instance->pps_active = 1;
|
instance->pps_active = 1;
|
||||||
|
|
||||||
log_sample(instance, &cooked_time, 0, 1, offset + correction - instance->offset, offset, dispersion);
|
log_sample(instance, cooked_time, 0, 1, offset + raw_correction - instance->offset,
|
||||||
|
offset, dispersion);
|
||||||
|
|
||||||
/* for logging purposes */
|
/* for logging purposes */
|
||||||
if (!instance->driver->poll)
|
if (!instance->driver->poll)
|
||||||
@@ -505,23 +620,31 @@ RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
double
|
||||||
valid_sample_time(RCL_Instance instance, struct timespec *raw, struct timespec *cooked)
|
RCL_GetPrecision(RCL_Instance instance)
|
||||||
{
|
{
|
||||||
struct timespec now_raw, last_sample_time;
|
return instance->precision;
|
||||||
double diff, last_offset, last_dispersion;
|
}
|
||||||
|
|
||||||
LCL_ReadRawTime(&now_raw);
|
int
|
||||||
diff = UTI_DiffTimespecsToDouble(&now_raw, raw);
|
RCL_GetDriverPoll(RCL_Instance instance)
|
||||||
|
{
|
||||||
|
return instance->driver_poll;
|
||||||
|
}
|
||||||
|
|
||||||
if (diff < 0.0 || diff > UTI_Log2ToDouble(instance->poll + 1) ||
|
static int
|
||||||
(filter_get_samples(&instance->filter) > 0 &&
|
valid_sample_time(RCL_Instance instance, struct timespec *sample_time)
|
||||||
filter_get_last_sample(&instance->filter, &last_sample_time,
|
{
|
||||||
&last_offset, &last_dispersion) &&
|
struct timespec now;
|
||||||
UTI_CompareTimespecs(&last_sample_time, cooked) >= 0)) {
|
double diff;
|
||||||
DEBUG_LOG(LOGF_Refclock, "%s refclock sample not valid age=%.6f raw=%s cooked=%s",
|
|
||||||
UTI_RefidToString(instance->ref_id), diff,
|
LCL_ReadCookedTime(&now, NULL);
|
||||||
UTI_TimespecToString(raw), UTI_TimespecToString(cooked));
|
diff = UTI_DiffTimespecsToDouble(&now, sample_time);
|
||||||
|
|
||||||
|
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);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -562,6 +685,7 @@ pps_stratum(RCL_Instance instance, struct timespec *ts)
|
|||||||
static void
|
static void
|
||||||
poll_timeout(void *arg)
|
poll_timeout(void *arg)
|
||||||
{
|
{
|
||||||
|
NTP_Sample sample;
|
||||||
int poll;
|
int poll;
|
||||||
|
|
||||||
RCL_Instance inst = (RCL_Instance)arg;
|
RCL_Instance inst = (RCL_Instance)arg;
|
||||||
@@ -575,26 +699,15 @@ poll_timeout(void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(inst->driver->poll && inst->driver_polled < (1 << (inst->poll - inst->driver_poll)))) {
|
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;
|
inst->driver_polled = 0;
|
||||||
|
|
||||||
if (sample_ok) {
|
if (SPF_GetFilteredSample(inst->filter, &sample)) {
|
||||||
if (inst->pps_active && inst->lock_ref == -1)
|
|
||||||
/* Handle special case when PPS is used with local stratum */
|
|
||||||
stratum = pps_stratum(inst, &sample_time);
|
|
||||||
else
|
|
||||||
stratum = 0;
|
|
||||||
|
|
||||||
SRC_UpdateReachability(inst->source, 1);
|
SRC_UpdateReachability(inst->source, 1);
|
||||||
SRC_AccumulateSample(inst->source, &sample_time, offset,
|
SRC_SetLeapStatus(inst->source, inst->leap_status);
|
||||||
inst->delay, dispersion, inst->delay, dispersion, stratum, inst->leap_status);
|
SRC_AccumulateSample(inst->source, &sample);
|
||||||
SRC_SelectSource(inst->source);
|
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 {
|
} else {
|
||||||
SRC_UpdateReachability(inst->source, 0);
|
SRC_UpdateReachability(inst->source, 0);
|
||||||
}
|
}
|
||||||
@@ -611,9 +724,9 @@ slew_samples(struct timespec *raw, struct timespec *cooked, double dfreq,
|
|||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(refclocks); i++) {
|
for (i = 0; i < ARR_GetSize(refclocks); i++) {
|
||||||
if (change_type == LCL_ChangeUnknownStep)
|
if (change_type == LCL_ChangeUnknownStep)
|
||||||
filter_reset(&get_refclock(i)->filter);
|
SPF_DropSamples(get_refclock(i)->filter);
|
||||||
else
|
else
|
||||||
filter_slew_samples(&get_refclock(i)->filter, cooked, dfreq, doffset);
|
SPF_SlewSamples(get_refclock(i)->filter, cooked, dfreq, doffset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -623,7 +736,7 @@ add_dispersion(double dispersion, void *anything)
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < ARR_GetSize(refclocks); 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
|
static void
|
||||||
@@ -655,320 +768,3 @@ log_sample(RCL_Instance instance, struct timespec *sample_time, int filtered, in
|
|||||||
dispersion);
|
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(LOGF_Refclock, "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(LOGF_Refclock, "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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -37,17 +37,21 @@ typedef struct {
|
|||||||
int driver_poll;
|
int driver_poll;
|
||||||
int poll;
|
int poll;
|
||||||
int filter_length;
|
int filter_length;
|
||||||
|
int pps_forced;
|
||||||
int pps_rate;
|
int pps_rate;
|
||||||
int min_samples;
|
int min_samples;
|
||||||
int max_samples;
|
int max_samples;
|
||||||
int sel_options;
|
int sel_options;
|
||||||
int max_lock_age;
|
int max_lock_age;
|
||||||
|
int stratum;
|
||||||
|
int tai;
|
||||||
uint32_t ref_id;
|
uint32_t ref_id;
|
||||||
uint32_t lock_ref_id;
|
uint32_t lock_ref_id;
|
||||||
double offset;
|
double offset;
|
||||||
double delay;
|
double delay;
|
||||||
double precision;
|
double precision;
|
||||||
double max_dispersion;
|
double max_dispersion;
|
||||||
|
double pulse_width;
|
||||||
} RefclockParameters;
|
} RefclockParameters;
|
||||||
|
|
||||||
typedef struct RCL_Instance_Record *RCL_Instance;
|
typedef struct RCL_Instance_Record *RCL_Instance;
|
||||||
@@ -68,8 +72,13 @@ extern void RCL_ReportSource(RPT_SourceReport *report, struct timespec *now);
|
|||||||
extern void RCL_SetDriverData(RCL_Instance instance, void *data);
|
extern void RCL_SetDriverData(RCL_Instance instance, void *data);
|
||||||
extern void *RCL_GetDriverData(RCL_Instance instance);
|
extern void *RCL_GetDriverData(RCL_Instance instance);
|
||||||
extern char *RCL_GetDriverParameter(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 char *RCL_GetDriverOption(RCL_Instance instance, char *name);
|
||||||
extern int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap);
|
extern int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap);
|
||||||
extern int RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second);
|
extern int RCL_AddPulse(RCL_Instance instance, struct timespec *pulse_time, double second);
|
||||||
|
extern int RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
|
||||||
|
double second, double dispersion, double raw_correction);
|
||||||
|
extern double RCL_GetPrecision(RCL_Instance instance);
|
||||||
|
extern int RCL_GetDriverPoll(RCL_Instance instance);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
207
refclock_phc.c
207
refclock_phc.c
@@ -2,7 +2,7 @@
|
|||||||
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
chronyd/chronyc - Programs for keeping computer clocks accurate.
|
||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Miroslav Lichvar 2013
|
* Copyright (C) Miroslav Lichvar 2013, 2017
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -33,144 +33,137 @@
|
|||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
#include <linux/ptp_clock.h>
|
|
||||||
|
|
||||||
#include "refclock.h"
|
#include "refclock.h"
|
||||||
|
#include "hwclock.h"
|
||||||
|
#include "local.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
#include "memory.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "sched.h"
|
||||||
|
#include "sys_linux.h"
|
||||||
|
|
||||||
/* From linux/include/linux/posix-timers.h */
|
struct phc_instance {
|
||||||
#define CPUCLOCK_MAX 3
|
int fd;
|
||||||
#define CLOCKFD CPUCLOCK_MAX
|
int mode;
|
||||||
#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
|
int nocrossts;
|
||||||
|
int extpps;
|
||||||
#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
|
int pin;
|
||||||
|
int channel;
|
||||||
#define NUM_READINGS 10
|
HCL_Instance clock;
|
||||||
|
|
||||||
static int no_sys_offset_ioctl = 0;
|
|
||||||
|
|
||||||
struct phc_reading {
|
|
||||||
struct timespec sys_ts1;
|
|
||||||
struct timespec phc_ts;;
|
|
||||||
struct timespec sys_ts2;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int read_phc_ioctl(struct phc_reading *readings, int phc_fd, int n)
|
static void read_ext_pulse(int sockfd, int event, void *anything);
|
||||||
{
|
|
||||||
#if defined(PTP_SYS_OFFSET) && NUM_READINGS <= PTP_MAX_SAMPLES
|
|
||||||
struct ptp_sys_offset sys_off;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Silence valgrind */
|
|
||||||
memset(&sys_off, 0, sizeof (sys_off));
|
|
||||||
|
|
||||||
sys_off.n_samples = n;
|
|
||||||
if (ioctl(phc_fd, PTP_SYS_OFFSET, &sys_off)) {
|
|
||||||
LOG(LOGS_ERR, LOGF_Refclock, "ioctl(PTP_SYS_OFFSET) failed : %s", strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
|
||||||
readings[i].sys_ts1.tv_sec = sys_off.ts[i * 2].sec;
|
|
||||||
readings[i].sys_ts1.tv_nsec = sys_off.ts[i * 2].nsec;
|
|
||||||
readings[i].phc_ts.tv_sec = sys_off.ts[i * 2 + 1].sec;
|
|
||||||
readings[i].phc_ts.tv_nsec = sys_off.ts[i * 2 + 1].nsec;
|
|
||||||
readings[i].sys_ts2.tv_sec = sys_off.ts[i * 2 + 2].sec;
|
|
||||||
readings[i].sys_ts2.tv_nsec = sys_off.ts[i * 2 + 2].nsec;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
#else
|
|
||||||
/* Not available */
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_phc_user(struct phc_reading *readings, int phc_fd, int n)
|
|
||||||
{
|
|
||||||
clockid_t phc_id;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
phc_id = FD_TO_CLOCKID(phc_fd);
|
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
|
||||||
if (clock_gettime(CLOCK_REALTIME, &readings[i].sys_ts1) ||
|
|
||||||
clock_gettime(phc_id, &readings[i].phc_ts) ||
|
|
||||||
clock_gettime(CLOCK_REALTIME, &readings[i].sys_ts2)) {
|
|
||||||
LOG(LOGS_ERR, LOGF_Refclock, "clock_gettime() failed : %s", strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int phc_initialise(RCL_Instance instance)
|
static int phc_initialise(RCL_Instance instance)
|
||||||
{
|
{
|
||||||
struct ptp_clock_caps caps;
|
const char *options[] = {"nocrossts", "extpps", "pin", "channel", "clear", NULL};
|
||||||
int phc_fd;
|
struct phc_instance *phc;
|
||||||
char *path;
|
int phc_fd, rising_edge;
|
||||||
|
char *path, *s;
|
||||||
|
|
||||||
|
RCL_CheckDriverOptions(instance, options);
|
||||||
|
|
||||||
path = RCL_GetDriverParameter(instance);
|
path = RCL_GetDriverParameter(instance);
|
||||||
|
|
||||||
phc_fd = open(path, O_RDONLY);
|
phc_fd = SYS_Linux_OpenPHC(path, 0);
|
||||||
if (phc_fd < 0) {
|
if (phc_fd < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "open() failed on %s", path);
|
LOG_FATAL("Could not open PHC");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure it is a PHC */
|
phc = MallocNew(struct phc_instance);
|
||||||
if (ioctl(phc_fd, PTP_CLOCK_GETCAPS, &caps)) {
|
phc->fd = phc_fd;
|
||||||
LOG_FATAL(LOGF_Refclock, "ioctl(PTP_CLOCK_GETCAPS) failed : %s", strerror(errno));
|
phc->mode = 0;
|
||||||
return 0;
|
phc->nocrossts = RCL_GetDriverOption(instance, "nocrossts") ? 1 : 0;
|
||||||
|
phc->extpps = RCL_GetDriverOption(instance, "extpps") ? 1 : 0;
|
||||||
|
|
||||||
|
if (phc->extpps) {
|
||||||
|
s = RCL_GetDriverOption(instance, "pin");
|
||||||
|
phc->pin = s ? atoi(s) : 0;
|
||||||
|
s = RCL_GetDriverOption(instance, "channel");
|
||||||
|
phc->channel = s ? atoi(s) : 0;
|
||||||
|
rising_edge = RCL_GetDriverOption(instance, "clear") ? 0 : 1;
|
||||||
|
phc->clock = HCL_CreateInstance(0, 16, UTI_Log2ToDouble(RCL_GetDriverPoll(instance)));
|
||||||
|
|
||||||
|
if (!SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel,
|
||||||
|
rising_edge, !rising_edge, 1))
|
||||||
|
LOG_FATAL("Could not enable external PHC timestamping");
|
||||||
|
|
||||||
|
SCH_AddFileHandler(phc->fd, SCH_FILE_INPUT, read_ext_pulse, instance);
|
||||||
|
} else {
|
||||||
|
phc->pin = phc->channel = 0;
|
||||||
|
phc->clock = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTI_FdSetCloexec(phc_fd);
|
RCL_SetDriverData(instance, phc);
|
||||||
|
|
||||||
RCL_SetDriverData(instance, (void *)(long)phc_fd);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void phc_finalise(RCL_Instance instance)
|
static void phc_finalise(RCL_Instance instance)
|
||||||
{
|
{
|
||||||
close((long)RCL_GetDriverData(instance));
|
struct phc_instance *phc;
|
||||||
|
|
||||||
|
phc = (struct phc_instance *)RCL_GetDriverData(instance);
|
||||||
|
|
||||||
|
if (phc->extpps) {
|
||||||
|
SCH_RemoveFileHandler(phc->fd);
|
||||||
|
SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel, 0, 0, 0);
|
||||||
|
HCL_DestroyInstance(phc->clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(phc->fd);
|
||||||
|
Free(phc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_ext_pulse(int fd, int event, void *anything)
|
||||||
|
{
|
||||||
|
RCL_Instance instance;
|
||||||
|
struct phc_instance *phc;
|
||||||
|
struct timespec phc_ts, local_ts;
|
||||||
|
double local_err;
|
||||||
|
int channel;
|
||||||
|
|
||||||
|
instance = anything;
|
||||||
|
phc = RCL_GetDriverData(instance);
|
||||||
|
|
||||||
|
if (!SYS_Linux_ReadPHCExtTimestamp(phc->fd, &phc_ts, &channel))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (channel != phc->channel) {
|
||||||
|
DEBUG_LOG("Unexpected extts channel %d\n", channel);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!HCL_CookTime(phc->clock, &phc_ts, &local_ts, &local_err))
|
||||||
|
return;
|
||||||
|
|
||||||
|
RCL_AddCookedPulse(instance, &local_ts, 1.0e-9 * local_ts.tv_nsec, local_err,
|
||||||
|
UTI_DiffTimespecsToDouble(&phc_ts, &local_ts));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int phc_poll(RCL_Instance instance)
|
static int phc_poll(RCL_Instance instance)
|
||||||
{
|
{
|
||||||
struct phc_reading readings[NUM_READINGS];
|
struct phc_instance *phc;
|
||||||
double offset = 0.0, delay, best_delay = 0.0;
|
struct timespec phc_ts, sys_ts, local_ts;
|
||||||
int i, phc_fd, best;
|
double offset, phc_err, local_err;
|
||||||
|
|
||||||
phc_fd = (long)RCL_GetDriverData(instance);
|
|
||||||
|
|
||||||
if (!no_sys_offset_ioctl) {
|
phc = (struct phc_instance *)RCL_GetDriverData(instance);
|
||||||
if (!read_phc_ioctl(readings, phc_fd, NUM_READINGS)) {
|
|
||||||
no_sys_offset_ioctl = 1;
|
if (!SYS_Linux_GetPHCSample(phc->fd, phc->nocrossts, RCL_GetPrecision(instance),
|
||||||
return 0;
|
&phc->mode, &phc_ts, &sys_ts, &phc_err))
|
||||||
}
|
return 0;
|
||||||
} else {
|
|
||||||
if (!read_phc_user(readings, phc_fd, NUM_READINGS))
|
if (phc->extpps) {
|
||||||
return 0;
|
LCL_CookTime(&sys_ts, &local_ts, &local_err);
|
||||||
|
HCL_AccumulateSample(phc->clock, &phc_ts, &local_ts, phc_err + local_err);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find the fastest reading */
|
offset = UTI_DiffTimespecsToDouble(&phc_ts, &sys_ts);
|
||||||
for (i = 0; i < NUM_READINGS; i++) {
|
|
||||||
delay = UTI_DiffTimespecsToDouble(&readings[i].sys_ts2, &readings[i].sys_ts1);
|
|
||||||
|
|
||||||
if (!i || best_delay > delay) {
|
DEBUG_LOG("PHC offset: %+.9f err: %.9f", offset, phc_err);
|
||||||
best = i;
|
|
||||||
best_delay = delay;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
offset = UTI_DiffTimespecsToDouble(&readings[best].phc_ts, &readings[best].sys_ts2) +
|
return RCL_AddSample(instance, &sys_ts, offset, LEAP_Normal);
|
||||||
best_delay / 2.0;
|
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_Refclock, "PHC offset: %+.9f delay: %.9f", offset, best_delay);
|
|
||||||
|
|
||||||
return RCL_AddSample(instance, &readings[best].sys_ts2, offset, LEAP_Normal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RefclockDriver RCL_PHC_driver = {
|
RefclockDriver RCL_PHC_driver = {
|
||||||
|
|||||||
@@ -48,48 +48,51 @@ struct pps_instance {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int pps_initialise(RCL_Instance instance) {
|
static int pps_initialise(RCL_Instance instance) {
|
||||||
|
const char *options[] = {"clear", NULL};
|
||||||
pps_handle_t handle;
|
pps_handle_t handle;
|
||||||
pps_params_t params;
|
pps_params_t params;
|
||||||
struct pps_instance *pps;
|
struct pps_instance *pps;
|
||||||
int fd, edge_clear, mode;
|
int fd, edge_clear, mode;
|
||||||
char *path;
|
char *path;
|
||||||
|
|
||||||
|
RCL_CheckDriverOptions(instance, options);
|
||||||
|
|
||||||
path = RCL_GetDriverParameter(instance);
|
path = RCL_GetDriverParameter(instance);
|
||||||
edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;
|
edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;
|
||||||
|
|
||||||
fd = open(path, O_RDWR);
|
fd = open(path, O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "open() failed on %s", path);
|
LOG_FATAL("Could not open %s : %s", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTI_FdSetCloexec(fd);
|
UTI_FdSetCloexec(fd);
|
||||||
|
|
||||||
if (time_pps_create(fd, &handle) < 0) {
|
if (time_pps_create(fd, &handle) < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "time_pps_create() failed on %s", path);
|
LOG_FATAL("time_pps_create() failed on %s : %s", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (time_pps_getcap(handle, &mode) < 0) {
|
if (time_pps_getcap(handle, &mode) < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "time_pps_getcap() failed on %s", path);
|
LOG_FATAL("time_pps_getcap() failed on %s : %s", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (time_pps_getparams(handle, ¶ms) < 0) {
|
if (time_pps_getparams(handle, ¶ms) < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "time_pps_getparams() failed on %s", path);
|
LOG_FATAL("time_pps_getparams() failed on %s : %s", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!edge_clear) {
|
if (!edge_clear) {
|
||||||
if (!(mode & PPS_CAPTUREASSERT)) {
|
if (!(mode & PPS_CAPTUREASSERT)) {
|
||||||
LOG_FATAL(LOGF_Refclock, "CAPTUREASSERT not supported on %s", path);
|
LOG_FATAL("CAPTUREASSERT not supported on %s", path);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
params.mode |= PPS_CAPTUREASSERT;
|
params.mode |= PPS_CAPTUREASSERT;
|
||||||
params.mode &= ~PPS_CAPTURECLEAR;
|
params.mode &= ~PPS_CAPTURECLEAR;
|
||||||
} else {
|
} else {
|
||||||
if (!(mode & PPS_CAPTURECLEAR)) {
|
if (!(mode & PPS_CAPTURECLEAR)) {
|
||||||
LOG_FATAL(LOGF_Refclock, "CAPTURECLEAR not supported on %s", path);
|
LOG_FATAL("CAPTURECLEAR not supported on %s", path);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
params.mode |= PPS_CAPTURECLEAR;
|
params.mode |= PPS_CAPTURECLEAR;
|
||||||
@@ -97,7 +100,7 @@ static int pps_initialise(RCL_Instance instance) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (time_pps_setparams(handle, ¶ms) < 0) {
|
if (time_pps_setparams(handle, ¶ms) < 0) {
|
||||||
LOG_FATAL(LOGF_Refclock, "time_pps_setparams() failed on %s", path);
|
LOG_FATAL("time_pps_setparams() failed on %s : %s", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +136,7 @@ static int pps_poll(RCL_Instance instance)
|
|||||||
ts.tv_nsec = 0;
|
ts.tv_nsec = 0;
|
||||||
|
|
||||||
if (time_pps_fetch(pps->handle, PPS_TSFMT_TSPEC, &pps_info, &ts) < 0) {
|
if (time_pps_fetch(pps->handle, PPS_TSFMT_TSPEC, &pps_info, &ts) < 0) {
|
||||||
LOG(LOGS_ERR, LOGF_Refclock, "time_pps_fetch() failed : %s", strerror(errno));
|
LOG(LOGS_ERR, "time_pps_fetch() failed : %s", strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,8 +149,8 @@ static int pps_poll(RCL_Instance instance)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (seq == pps->last_seq || UTI_IsZeroTimespec(&ts)) {
|
if (seq == pps->last_seq || UTI_IsZeroTimespec(&ts)) {
|
||||||
DEBUG_LOG(LOGF_Refclock, "PPS sample ignored seq=%lu ts=%s",
|
DEBUG_LOG("PPS sample ignored seq=%lu ts=%s",
|
||||||
seq, UTI_TimespecToString(&ts));
|
(unsigned long)seq, UTI_TimespecToString(&ts));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,23 +59,26 @@ struct shmTime {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int shm_initialise(RCL_Instance instance) {
|
static int shm_initialise(RCL_Instance instance) {
|
||||||
|
const char *options[] = {"perm", NULL};
|
||||||
int id, param, perm;
|
int id, param, perm;
|
||||||
char *s;
|
char *s;
|
||||||
struct shmTime *shm;
|
struct shmTime *shm;
|
||||||
|
|
||||||
|
RCL_CheckDriverOptions(instance, options);
|
||||||
|
|
||||||
param = atoi(RCL_GetDriverParameter(instance));
|
param = atoi(RCL_GetDriverParameter(instance));
|
||||||
s = RCL_GetDriverOption(instance, "perm");
|
s = RCL_GetDriverOption(instance, "perm");
|
||||||
perm = s ? strtol(s, NULL, 8) & 0777 : 0600;
|
perm = s ? strtol(s, NULL, 8) & 0777 : 0600;
|
||||||
|
|
||||||
id = shmget(SHMKEY + param, sizeof (struct shmTime), IPC_CREAT | perm);
|
id = shmget(SHMKEY + param, sizeof (struct shmTime), IPC_CREAT | perm);
|
||||||
if (id == -1) {
|
if (id == -1) {
|
||||||
LOG_FATAL(LOGF_Refclock, "shmget() failed");
|
LOG_FATAL("shmget() failed : %s", strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
shm = (struct shmTime *)shmat(id, 0, 0);
|
shm = (struct shmTime *)shmat(id, 0, 0);
|
||||||
if ((long)shm == -1) {
|
if ((long)shm == -1) {
|
||||||
LOG_FATAL(LOGF_Refclock, "shmat() failed");
|
LOG_FATAL("shmat() failed : %s", strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +103,7 @@ static int shm_poll(RCL_Instance instance)
|
|||||||
|
|
||||||
if ((t.mode == 1 && t.count != shm->count) ||
|
if ((t.mode == 1 && t.count != shm->count) ||
|
||||||
!(t.mode == 0 || t.mode == 1) || !t.valid) {
|
!(t.mode == 0 || t.mode == 1) || !t.valid) {
|
||||||
DEBUG_LOG(LOGF_Refclock, "SHM sample ignored mode=%d count=%d valid=%d",
|
DEBUG_LOG("SHM sample ignored mode=%d count=%d valid=%d",
|
||||||
t.mode, t.count, t.valid);
|
t.mode, t.count, t.valid);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "sched.h"
|
#include "sched.h"
|
||||||
|
#include "socket.h"
|
||||||
|
|
||||||
#define SOCK_MAGIC 0x534f434b
|
#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);
|
s = recv(sockfd, &sample, sizeof (sample), 0);
|
||||||
|
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
LOG(LOGS_ERR, LOGF_Refclock, "Could not read SOCK sample : %s",
|
DEBUG_LOG("Could not read SOCK sample : %s", strerror(errno));
|
||||||
strerror(errno));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s != sizeof (sample)) {
|
if (s != sizeof (sample)) {
|
||||||
LOG(LOGS_WARN, LOGF_Refclock, "Unexpected length of SOCK sample : %d != %ld",
|
DEBUG_LOG("Unexpected length of SOCK sample : %d != %ld",
|
||||||
s, (long)sizeof (sample));
|
s, (long)sizeof (sample));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sample.magic != SOCK_MAGIC) {
|
if (sample.magic != SOCK_MAGIC) {
|
||||||
LOG(LOGS_WARN, LOGF_Refclock, "Unexpected magic number in SOCK sample : %x != %x",
|
DEBUG_LOG("Unexpected magic number in SOCK sample : %x != %x",
|
||||||
sample.magic, SOCK_MAGIC);
|
(unsigned int)sample.magic, (unsigned int)SOCK_MAGIC);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,31 +98,16 @@ static void read_sample(int sockfd, int event, void *anything)
|
|||||||
|
|
||||||
static int sock_initialise(RCL_Instance instance)
|
static int sock_initialise(RCL_Instance instance)
|
||||||
{
|
{
|
||||||
struct sockaddr_un s;
|
|
||||||
int sockfd;
|
int sockfd;
|
||||||
char *path;
|
char *path;
|
||||||
|
|
||||||
|
RCL_CheckDriverOptions(instance, NULL);
|
||||||
|
|
||||||
path = RCL_GetDriverParameter(instance);
|
path = RCL_GetDriverParameter(instance);
|
||||||
|
|
||||||
s.sun_family = AF_UNIX;
|
sockfd = SCK_OpenUnixDatagramSocket(NULL, path, 0);
|
||||||
if (snprintf(s.sun_path, sizeof (s.sun_path), "%s", path) >= sizeof (s.sun_path)) {
|
if (sockfd < 0)
|
||||||
LOG_FATAL(LOGF_Refclock, "path %s is too long", path);
|
LOG_FATAL("Could not open socket %s", path);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
||||||
if (sockfd < 0) {
|
|
||||||
LOG_FATAL(LOGF_Refclock, "socket() failed");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
UTI_FdSetCloexec(sockfd);
|
|
||||||
|
|
||||||
unlink(path);
|
|
||||||
if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) {
|
|
||||||
LOG_FATAL(LOGF_Refclock, "bind() failed");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
RCL_SetDriverData(instance, (void *)(long)sockfd);
|
RCL_SetDriverData(instance, (void *)(long)sockfd);
|
||||||
SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
|
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);
|
sockfd = (long)RCL_GetDriverData(instance);
|
||||||
SCH_RemoveFileHandler(sockfd);
|
SCH_RemoveFileHandler(sockfd);
|
||||||
close(sockfd);
|
SCK_RemoveSocket(sockfd);
|
||||||
|
SCK_CloseSocket(sockfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefclockDriver RCL_SOCK_driver = {
|
RefclockDriver RCL_SOCK_driver = {
|
||||||
|
|||||||
625
reference.c
625
reference.c
File diff suppressed because it is too large
Load Diff
@@ -144,6 +144,7 @@ extern void REF_SetReference
|
|||||||
double offset,
|
double offset,
|
||||||
double offset_sd,
|
double offset_sd,
|
||||||
double frequency,
|
double frequency,
|
||||||
|
double frequency_sd,
|
||||||
double skew,
|
double skew,
|
||||||
double root_delay,
|
double root_delay,
|
||||||
double root_dispersion
|
double root_dispersion
|
||||||
@@ -161,6 +162,9 @@ extern void REF_SetManualReference
|
|||||||
extern void
|
extern void
|
||||||
REF_SetUnsynchronised(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
|
/* Return the current stratum of this host or 16 if the host is not
|
||||||
synchronised */
|
synchronised */
|
||||||
extern int REF_GetOurStratum(void);
|
extern int REF_GetOurStratum(void);
|
||||||
@@ -184,6 +188,9 @@ extern void REF_DisableLocal(void);
|
|||||||
and is better to discard any measurements */
|
and is better to discard any measurements */
|
||||||
extern int REF_IsLeapSecondClose(void);
|
extern int REF_IsLeapSecondClose(void);
|
||||||
|
|
||||||
|
/* 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);
|
extern void REF_GetTrackingReport(RPT_TrackingReport *rep);
|
||||||
|
|
||||||
#endif /* GOT_REFERENCE_H */
|
#endif /* GOT_REFERENCE_H */
|
||||||
|
|||||||
59
regress.c
59
regress.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Miroslav Lichvar 2011
|
* Copyright (C) Miroslav Lichvar 2011, 2016-2017
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#define MAX_POINTS 128
|
#define MAX_POINTS 64
|
||||||
|
|
||||||
void
|
void
|
||||||
RGR_WeightedRegression
|
RGR_WeightedRegression
|
||||||
@@ -285,7 +285,7 @@ RGR_FindBestRegression
|
|||||||
n - start <= min_samples) {
|
n - start <= min_samples) {
|
||||||
if (start != resid_start) {
|
if (start != resid_start) {
|
||||||
/* Ignore extra samples in returned nruns */
|
/* Ignore extra samples in returned nruns */
|
||||||
nruns = n_runs_from_residuals(resid - resid_start + start, n - start);
|
nruns = n_runs_from_residuals(resid + (start - resid_start), n - start);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
@@ -340,7 +340,7 @@ RGR_FindBestRegression
|
|||||||
0-521-43108-5). */
|
0-521-43108-5). */
|
||||||
|
|
||||||
static double
|
static double
|
||||||
find_ordered_entry_with_flags(double *x, int n, int index, int *flags)
|
find_ordered_entry_with_flags(double *x, int n, int index, char *flags)
|
||||||
{
|
{
|
||||||
int u, v, l, r;
|
int u, v, l, r;
|
||||||
double temp;
|
double temp;
|
||||||
@@ -403,9 +403,9 @@ find_ordered_entry_with_flags(double *x, int n, int index, int *flags)
|
|||||||
static double
|
static double
|
||||||
find_ordered_entry(double *x, int n, int index)
|
find_ordered_entry(double *x, int n, int index)
|
||||||
{
|
{
|
||||||
int flags[MAX_POINTS];
|
char flags[MAX_POINTS];
|
||||||
|
|
||||||
memset(flags, 0, n * sizeof(int));
|
memset(flags, 0, n * sizeof (flags[0]));
|
||||||
return find_ordered_entry_with_flags(x, n, index, flags);
|
return find_ordered_entry_with_flags(x, n, index, flags);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -417,9 +417,9 @@ static double
|
|||||||
find_median(double *x, int n)
|
find_median(double *x, int n)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
int flags[MAX_POINTS];
|
char flags[MAX_POINTS];
|
||||||
|
|
||||||
memset(flags, 0, n*sizeof(int));
|
memset(flags, 0, n * sizeof (flags[0]));
|
||||||
k = n>>1;
|
k = n>>1;
|
||||||
if (n&1) {
|
if (n&1) {
|
||||||
return find_ordered_entry_with_flags(x, n, k, flags);
|
return find_ordered_entry_with_flags(x, n, k, flags);
|
||||||
@@ -429,6 +429,19 @@ find_median(double *x, int n)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
double
|
||||||
|
RGR_FindMedian(double *x, int n)
|
||||||
|
{
|
||||||
|
double tmp[MAX_POINTS];
|
||||||
|
|
||||||
|
assert(n > 0 && n <= MAX_POINTS);
|
||||||
|
memcpy(tmp, x, n * sizeof (tmp[0]));
|
||||||
|
|
||||||
|
return find_median(tmp, n);
|
||||||
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* This function evaluates the equation
|
/* This function evaluates the equation
|
||||||
|
|
||||||
@@ -509,7 +522,7 @@ RGR_FindBestRobustRegression
|
|||||||
double mx, dx, my, dy;
|
double mx, dx, my, dy;
|
||||||
int nruns = 0;
|
int nruns = 0;
|
||||||
|
|
||||||
assert(n < MAX_POINTS);
|
assert(n <= MAX_POINTS);
|
||||||
|
|
||||||
if (n < 2) {
|
if (n < 2) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -566,24 +579,18 @@ RGR_FindBestRobustRegression
|
|||||||
Estimate standard deviation of b and expand range about b based
|
Estimate standard deviation of b and expand range about b based
|
||||||
on that. */
|
on that. */
|
||||||
sb = sqrt(s2 * W/V);
|
sb = sqrt(s2 * W/V);
|
||||||
if (sb > tol) {
|
incr = MAX(sb, tol);
|
||||||
incr = 3.0 * sb;
|
|
||||||
} else {
|
|
||||||
incr = 3.0 * tol;
|
|
||||||
}
|
|
||||||
|
|
||||||
blo = b;
|
|
||||||
bhi = b;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* Make sure incr is significant to blo and bhi */
|
incr *= 2.0;
|
||||||
while (bhi + incr == bhi || blo - incr == blo) {
|
|
||||||
incr *= 2;
|
/* Give up if the interval is too large */
|
||||||
}
|
if (incr > 100.0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
blo = b - incr;
|
||||||
|
bhi = b + incr;
|
||||||
|
|
||||||
blo -= incr;
|
|
||||||
bhi += incr;
|
|
||||||
|
|
||||||
/* We don't want 'a' yet */
|
/* We don't want 'a' yet */
|
||||||
eval_robust_residual(x + start, y + start, n_points, blo, &a, &rlo);
|
eval_robust_residual(x + start, y + start, n_points, blo, &a, &rlo);
|
||||||
eval_robust_residual(x + start, y + start, n_points, bhi, &a, &rhi);
|
eval_robust_residual(x + start, y + start, n_points, bhi, &a, &rhi);
|
||||||
@@ -594,6 +601,8 @@ RGR_FindBestRobustRegression
|
|||||||
/* OK, so the root for b lies in (blo, bhi). Start bisecting */
|
/* OK, so the root for b lies in (blo, bhi). Start bisecting */
|
||||||
do {
|
do {
|
||||||
bmid = 0.5 * (blo + bhi);
|
bmid = 0.5 * (blo + bhi);
|
||||||
|
if (!(blo < bmid && bmid < bhi))
|
||||||
|
break;
|
||||||
eval_robust_residual(x + start, y + start, n_points, bmid, &a, &rmid);
|
eval_robust_residual(x + start, y + start, n_points, bmid, &a, &rmid);
|
||||||
if (rmid == 0.0) {
|
if (rmid == 0.0) {
|
||||||
break;
|
break;
|
||||||
@@ -606,7 +615,7 @@ RGR_FindBestRobustRegression
|
|||||||
} else {
|
} else {
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
} while ((bhi - blo) > tol && (bmid - blo) * (bhi - bmid) > 0.0);
|
} while (bhi - blo > tol);
|
||||||
|
|
||||||
*b0 = a;
|
*b0 = a;
|
||||||
*b1 = bmid;
|
*b1 = bmid;
|
||||||
|
|||||||
@@ -131,4 +131,7 @@ RGR_MultipleRegress
|
|||||||
double *b2 /* estimated second slope */
|
double *b2 /* estimated second slope */
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/* Return the median value from an array */
|
||||||
|
extern double RGR_FindMedian(double *x, int n);
|
||||||
|
|
||||||
#endif /* GOT_REGRESS_H */
|
#endif /* GOT_REGRESS_H */
|
||||||
|
|||||||
8
rtc.c
8
rtc.c
@@ -104,7 +104,7 @@ apply_driftfile_time(time_t t)
|
|||||||
|
|
||||||
if (now.tv_sec < t) {
|
if (now.tv_sec < t) {
|
||||||
if (LCL_ApplyStepOffset(now.tv_sec - t))
|
if (LCL_ApplyStepOffset(now.tv_sec - t))
|
||||||
LOG(LOGS_INFO, LOGF_Rtc, "System time restored from driftfile");
|
LOG(LOGS_INFO, "System time restored from driftfile");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ RTC_Initialise(int initial_set)
|
|||||||
|
|
||||||
if (file_name) {
|
if (file_name) {
|
||||||
if (CNF_GetRtcSync()) {
|
if (CNF_GetRtcSync()) {
|
||||||
LOG_FATAL(LOGF_Rtc, "rtcfile directive cannot be used with rtcsync");
|
LOG_FATAL("rtcfile directive cannot be used with rtcsync");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (driver.init) {
|
if (driver.init) {
|
||||||
@@ -150,7 +150,7 @@ RTC_Initialise(int initial_set)
|
|||||||
driver_initialised = 1;
|
driver_initialised = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_ERR, LOGF_Rtc, "RTC not supported on this operating system");
|
LOG(LOGS_ERR, "RTC not supported on this operating system");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,7 +160,7 @@ RTC_Initialise(int initial_set)
|
|||||||
void
|
void
|
||||||
RTC_Finalise(void)
|
RTC_Finalise(void)
|
||||||
{
|
{
|
||||||
if (driver.fini) {
|
if (driver_initialised) {
|
||||||
(driver.fini)();
|
(driver.fini)();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
186
rtc_linux.c
186
rtc_linux.c
@@ -189,7 +189,7 @@ accumulate_sample(time_t rtc, struct timespec *sys)
|
|||||||
|
|
||||||
/* Discard all samples if the RTC was stepped back (not our trim) */
|
/* Discard all samples if the RTC was stepped back (not our trim) */
|
||||||
if (n_samples > 0 && rtc_sec[n_samples - 1] - rtc >= rtc_trim[n_samples - 1]) {
|
if (n_samples > 0 && rtc_sec[n_samples - 1] - rtc >= rtc_trim[n_samples - 1]) {
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "RTC samples discarded");
|
DEBUG_LOG("RTC samples discarded");
|
||||||
n_samples = 0;
|
n_samples = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,7 +294,7 @@ slew_samples
|
|||||||
coef_gain_rate += dfreq * (1.0 - coef_gain_rate);
|
coef_gain_rate += dfreq * (1.0 - coef_gain_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "dfreq=%.8f doffset=%.6f old_fast=%.6f old_rate=%.3f new_fast=%.6f new_rate=%.3f",
|
DEBUG_LOG("dfreq=%.8f doffset=%.6f old_fast=%.6f old_rate=%.3f new_fast=%.6f new_rate=%.3f",
|
||||||
dfreq, doffset,
|
dfreq, doffset,
|
||||||
old_seconds_fast, 1.0e6 * old_gain_rate,
|
old_seconds_fast, 1.0e6 * old_gain_rate,
|
||||||
coef_seconds_fast, 1.0e6 * coef_gain_rate);
|
coef_seconds_fast, 1.0e6 * coef_gain_rate);
|
||||||
@@ -352,7 +352,7 @@ rtc_from_t(const time_t *t)
|
|||||||
|
|
||||||
static time_t
|
static time_t
|
||||||
t_from_rtc(struct tm *stm) {
|
t_from_rtc(struct tm *stm) {
|
||||||
struct tm temp1, temp2;
|
struct tm temp1, temp2, *tm;
|
||||||
long diff;
|
long diff;
|
||||||
time_t t1, t2;
|
time_t t1, t2;
|
||||||
|
|
||||||
@@ -360,18 +360,20 @@ t_from_rtc(struct tm *stm) {
|
|||||||
temp1.tm_isdst = 0;
|
temp1.tm_isdst = 0;
|
||||||
|
|
||||||
t1 = mktime(&temp1);
|
t1 = mktime(&temp1);
|
||||||
if (rtc_on_utc) {
|
|
||||||
temp2 = *gmtime(&t1);
|
tm = rtc_on_utc ? gmtime(&t1) : localtime(&t1);
|
||||||
} else {
|
if (!tm) {
|
||||||
temp2 = *localtime(&t1);
|
DEBUG_LOG("gmtime()/localtime() failed");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temp2 = *tm;
|
||||||
temp2.tm_isdst = 0;
|
temp2.tm_isdst = 0;
|
||||||
t2 = mktime(&temp2);
|
t2 = mktime(&temp2);
|
||||||
diff = t2 - t1;
|
diff = t2 - t1;
|
||||||
|
|
||||||
if (t1 - diff == -1)
|
if (t1 - diff == -1)
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "Could not convert RTC time");
|
DEBUG_LOG("Could not convert RTC time");
|
||||||
|
|
||||||
return t1 - diff;
|
return t1 - diff;
|
||||||
}
|
}
|
||||||
@@ -388,12 +390,9 @@ read_hwclock_file(const char *hwclock_file)
|
|||||||
if (!hwclock_file || !hwclock_file[0])
|
if (!hwclock_file || !hwclock_file[0])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
in = fopen(hwclock_file, "r");
|
in = UTI_OpenFile(NULL, hwclock_file, NULL, 'r', 0);
|
||||||
if (!in) {
|
if (!in)
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "Could not open %s : %s",
|
|
||||||
hwclock_file, strerror(errno));
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
/* Read third line from the file. */
|
/* Read third line from the file. */
|
||||||
for (i = 0; i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
@@ -408,8 +407,7 @@ read_hwclock_file(const char *hwclock_file)
|
|||||||
} else if (i == 3 && !strncmp(line, "UTC", 3)) {
|
} else if (i == 3 && !strncmp(line, "UTC", 3)) {
|
||||||
rtc_on_utc = 1;
|
rtc_on_utc = 1;
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "Could not read RTC LOCAL/UTC setting from %s",
|
LOG(LOGS_WARN, "Could not read RTC LOCAL/UTC setting from %s", hwclock_file);
|
||||||
hwclock_file);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,15 +442,15 @@ read_coefs_from_file(void)
|
|||||||
|
|
||||||
tried_to_load_coefs = 1;
|
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",
|
if (fscanf(in, "%d%ld%lf%lf",
|
||||||
&valid_coefs_from_file,
|
&valid_coefs_from_file,
|
||||||
&file_ref_time,
|
&file_ref_time,
|
||||||
&file_ref_offset,
|
&file_ref_offset,
|
||||||
&file_rate_ppm) == 4) {
|
&file_rate_ppm) == 4) {
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "Could not read coefficients from %s",
|
LOG(LOGS_WARN, "Could not read coefficients from %s", coefs_file_name);
|
||||||
coefs_file_name);
|
|
||||||
}
|
}
|
||||||
fclose(in);
|
fclose(in);
|
||||||
}
|
}
|
||||||
@@ -466,67 +464,40 @@ read_coefs_from_file(void)
|
|||||||
static int
|
static int
|
||||||
write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
||||||
{
|
{
|
||||||
struct stat buf;
|
|
||||||
char *temp_coefs_file_name;
|
|
||||||
FILE *out;
|
FILE *out;
|
||||||
int r1, r2;
|
|
||||||
|
|
||||||
/* Create a temporary file with a '.tmp' extension. */
|
/* Create a temporary file with a '.tmp' extension. */
|
||||||
|
out = UTI_OpenFile(NULL, coefs_file_name, ".tmp", 'w', 0644);
|
||||||
temp_coefs_file_name = (char*) Malloc(strlen(coefs_file_name)+8);
|
if (!out)
|
||||||
|
|
||||||
if(!temp_coefs_file_name) {
|
|
||||||
return RTC_ST_BADFILE;
|
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, LOGF_RtcLinux, "Could not open temporary RTC file %s.tmp for writing",
|
|
||||||
coefs_file_name);
|
|
||||||
return RTC_ST_BADFILE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Gain rate is written out in ppm */
|
/* Gain rate is written out in ppm */
|
||||||
r1 = fprintf(out, "%1d %ld %.6f %.3f\n",
|
fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate);
|
||||||
valid, ref_time, offset, 1.0e6 * rate);
|
fclose(out);
|
||||||
r2 = fclose(out);
|
|
||||||
if (r1 < 0 || r2) {
|
/* Rename the temporary file to the correct location */
|
||||||
Free(temp_coefs_file_name);
|
if (!UTI_RenameTempFile(NULL, coefs_file_name, ".tmp", NULL))
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "Could not write to temporary RTC file %s.tmp",
|
|
||||||
coefs_file_name);
|
|
||||||
return RTC_ST_BADFILE;
|
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, LOGF_RtcLinux,
|
|
||||||
"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, LOGF_RtcLinux, "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;
|
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
|
/* file_name is the name of the file where we save the RTC params
|
||||||
@@ -536,6 +507,23 @@ write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
|
|||||||
int
|
int
|
||||||
RTC_Linux_Initialise(void)
|
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_sec = MallocArray(time_t, MAX_SAMPLES);
|
||||||
rtc_trim = MallocArray(double, MAX_SAMPLES);
|
rtc_trim = MallocArray(double, MAX_SAMPLES);
|
||||||
system_times = MallocArray(struct timespec, MAX_SAMPLES);
|
system_times = MallocArray(struct timespec, MAX_SAMPLES);
|
||||||
@@ -546,18 +534,6 @@ RTC_Linux_Initialise(void)
|
|||||||
/* In case it didn't get done by pre-init */
|
/* In case it didn't get done by pre-init */
|
||||||
coefs_file_name = CNF_GetRtcFile();
|
coefs_file_name = CNF_GetRtcFile();
|
||||||
|
|
||||||
/* Try to open device */
|
|
||||||
|
|
||||||
fd = open (CNF_GetRtcDevice(), O_RDWR);
|
|
||||||
if (fd < 0) {
|
|
||||||
LOG(LOGS_ERR, LOGF_RtcLinux, "Could not open RTC device %s : %s",
|
|
||||||
CNF_GetRtcDevice(), strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Close on exec */
|
|
||||||
UTI_FdSetCloexec(fd);
|
|
||||||
|
|
||||||
n_samples = 0;
|
n_samples = 0;
|
||||||
n_samples_since_regression = 0;
|
n_samples_since_regression = 0;
|
||||||
n_runs = 0;
|
n_runs = 0;
|
||||||
@@ -590,6 +566,7 @@ RTC_Linux_Finalise(void)
|
|||||||
/* Remove input file handler */
|
/* Remove input file handler */
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
SCH_RemoveFileHandler(fd);
|
SCH_RemoveFileHandler(fd);
|
||||||
|
switch_interrupts(0);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
/* Save the RTC data */
|
/* Save the RTC data */
|
||||||
@@ -603,31 +580,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, LOGF_RtcLinux, "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, LOGF_RtcLinux, "Could not %s RTC interrupt : %s",
|
|
||||||
"disable", strerror(errno));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ================================================== */
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
measurement_timeout(void *any)
|
measurement_timeout(void *any)
|
||||||
{
|
{
|
||||||
@@ -658,7 +610,7 @@ set_rtc(time_t new_rtc_time)
|
|||||||
|
|
||||||
status = ioctl(fd, RTC_SET_TIME, &rtc_raw);
|
status = ioctl(fd, RTC_SET_TIME, &rtc_raw);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG(LOGS_ERR, LOGF_RtcLinux, "Could not set RTC time");
|
LOG(LOGS_ERR, "Could not set RTC time");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -701,10 +653,10 @@ handle_initial_trim(void)
|
|||||||
sys_error_now = rtc_error_now - coef_seconds_fast;
|
sys_error_now = rtc_error_now - coef_seconds_fast;
|
||||||
|
|
||||||
LCL_AccumulateOffset(sys_error_now, 0.0);
|
LCL_AccumulateOffset(sys_error_now, 0.0);
|
||||||
LOG(LOGS_INFO, LOGF_RtcLinux, "System clock off from RTC by %f seconds (slew)",
|
LOG(LOGS_INFO, "System clock off from RTC by %f seconds (slew)",
|
||||||
sys_error_now);
|
sys_error_now);
|
||||||
} else {
|
} else {
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "No valid rtcfile coefficients");
|
LOG(LOGS_WARN, "No valid rtcfile coefficients");
|
||||||
}
|
}
|
||||||
|
|
||||||
coefs_valid = 0;
|
coefs_valid = 0;
|
||||||
@@ -729,7 +681,7 @@ handle_relock_after_trim(void)
|
|||||||
if (valid) {
|
if (valid) {
|
||||||
write_coefs_to_file(1,ref,fast,saved_coef_gain_rate);
|
write_coefs_to_file(1,ref,fast,saved_coef_gain_rate);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "Could not do regression after trim");
|
DEBUG_LOG("Could not do regression after trim");
|
||||||
}
|
}
|
||||||
|
|
||||||
coefs_valid = 0;
|
coefs_valid = 0;
|
||||||
@@ -824,7 +776,7 @@ read_from_device(int fd_, int event, void *any)
|
|||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
/* This looks like a bad error : the file descriptor was indicating it was
|
/* This looks like a bad error : the file descriptor was indicating it was
|
||||||
* ready to read but we couldn't read anything. Give up. */
|
* ready to read but we couldn't read anything. Give up. */
|
||||||
LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read flags %s : %s", CNF_GetRtcDevice(), strerror(errno));
|
LOG(LOGS_ERR, "Could not read flags %s : %s", CNF_GetRtcDevice(), strerror(errno));
|
||||||
SCH_RemoveFileHandler(fd);
|
SCH_RemoveFileHandler(fd);
|
||||||
switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
|
switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
|
||||||
close(fd);
|
close(fd);
|
||||||
@@ -848,7 +800,7 @@ read_from_device(int fd_, int event, void *any)
|
|||||||
|
|
||||||
status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
|
status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read time from %s : %s", CNF_GetRtcDevice(), strerror(errno));
|
LOG(LOGS_ERR, "Could not read time from %s : %s", CNF_GetRtcDevice(), strerror(errno));
|
||||||
error = 1;
|
error = 1;
|
||||||
goto turn_off_interrupt;
|
goto turn_off_interrupt;
|
||||||
}
|
}
|
||||||
@@ -889,7 +841,7 @@ turn_off_interrupt:
|
|||||||
switch (operating_mode) {
|
switch (operating_mode) {
|
||||||
case OM_INITIAL:
|
case OM_INITIAL:
|
||||||
if (error) {
|
if (error) {
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "Could not complete initial step due to errors");
|
DEBUG_LOG("Could not complete initial step due to errors");
|
||||||
operating_mode = OM_NORMAL;
|
operating_mode = OM_NORMAL;
|
||||||
(after_init_hook)(after_init_hook_arg);
|
(after_init_hook)(after_init_hook_arg);
|
||||||
|
|
||||||
@@ -902,7 +854,7 @@ turn_off_interrupt:
|
|||||||
|
|
||||||
case OM_AFTERTRIM:
|
case OM_AFTERTRIM:
|
||||||
if (error) {
|
if (error) {
|
||||||
DEBUG_LOG(LOGF_RtcLinux, "Could not complete after trim relock due to errors");
|
DEBUG_LOG("Could not complete after trim relock due to errors");
|
||||||
operating_mode = OM_NORMAL;
|
operating_mode = OM_NORMAL;
|
||||||
|
|
||||||
switch_interrupts(0);
|
switch_interrupts(0);
|
||||||
@@ -1041,7 +993,7 @@ RTC_Linux_TimePreInit(time_t driftfile_time)
|
|||||||
UTI_AddDoubleToTimespec(&new_sys_time, -accumulated_error, &new_sys_time);
|
UTI_AddDoubleToTimespec(&new_sys_time, -accumulated_error, &new_sys_time);
|
||||||
|
|
||||||
if (new_sys_time.tv_sec < driftfile_time) {
|
if (new_sys_time.tv_sec < driftfile_time) {
|
||||||
LOG(LOGS_WARN, LOGF_RtcLinux, "RTC time before last driftfile modification (ignored)");
|
LOG(LOGS_WARN, "RTC time before last driftfile modification (ignored)");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1050,7 +1002,7 @@ RTC_Linux_TimePreInit(time_t driftfile_time)
|
|||||||
/* Set system time only if the step is larger than 1 second */
|
/* Set system time only if the step is larger than 1 second */
|
||||||
if (fabs(sys_offset) >= 1.0) {
|
if (fabs(sys_offset) >= 1.0) {
|
||||||
if (LCL_ApplyStepOffset(sys_offset))
|
if (LCL_ApplyStepOffset(sys_offset))
|
||||||
LOG(LOGS_INFO, LOGF_RtcLinux, "System time set from RTC");
|
LOG(LOGS_INFO, "System time set from RTC");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1095,7 +1047,7 @@ RTC_Linux_Trim(void)
|
|||||||
|
|
||||||
if (fabs(coef_seconds_fast) > 1.0) {
|
if (fabs(coef_seconds_fast) > 1.0) {
|
||||||
|
|
||||||
LOG(LOGS_INFO, LOGF_RtcLinux, "RTC wrong by %.3f seconds (step)",
|
LOG(LOGS_INFO, "RTC wrong by %.3f seconds (step)",
|
||||||
coef_seconds_fast);
|
coef_seconds_fast);
|
||||||
|
|
||||||
/* Do processing to set clock. Let R be the value we set the
|
/* Do processing to set clock. Let R be the value we set the
|
||||||
|
|||||||
452
samplefilt.c
Normal file
452
samplefilt.c
Normal file
@@ -0,0 +1,452 @@
|
|||||||
|
/*
|
||||||
|
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, k = -1; 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;
|
||||||
|
result->stratum = last_sample->stratum;
|
||||||
|
|
||||||
|
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
|
||||||
93
sched.c
93
sched.c
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
* Copyright (C) Richard P. Curnow 1997-2003
|
* Copyright (C) Richard P. Curnow 1997-2003
|
||||||
* Copyright (C) Miroslav Lichvar 2011, 2013-2015
|
* Copyright (C) Miroslav Lichvar 2011, 2013-2016
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of version 2 of the GNU General Public License as
|
||||||
@@ -65,6 +65,12 @@ static ARR_Instance file_handlers;
|
|||||||
static struct timespec last_select_ts, last_select_ts_raw;
|
static struct timespec last_select_ts, last_select_ts_raw;
|
||||||
static double last_select_ts_err;
|
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 */
|
/* Variables to handler the timer queue */
|
||||||
@@ -136,6 +142,8 @@ SCH_Initialise(void)
|
|||||||
|
|
||||||
LCL_ReadRawTime(&last_select_ts_raw);
|
LCL_ReadRawTime(&last_select_ts_raw);
|
||||||
last_select_ts = 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;
|
initialised = 1;
|
||||||
}
|
}
|
||||||
@@ -147,6 +155,8 @@ void
|
|||||||
SCH_Finalise(void) {
|
SCH_Finalise(void) {
|
||||||
ARR_DestroyInstance(file_handlers);
|
ARR_DestroyInstance(file_handlers);
|
||||||
|
|
||||||
|
LCL_RemoveParameterChangeHandler(handle_slew, NULL);
|
||||||
|
|
||||||
initialised = 0;
|
initialised = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +173,7 @@ SCH_AddFileHandler
|
|||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
|
||||||
if (fd >= FD_SETSIZE)
|
if (fd >= FD_SETSIZE)
|
||||||
LOG_FATAL(LOGF_Scheduler, "Too many file descriptors");
|
LOG_FATAL("Too many file descriptors");
|
||||||
|
|
||||||
/* Resize the array if the descriptor is highest so far */
|
/* Resize the array if the descriptor is highest so far */
|
||||||
while (ARR_GetSize(file_handlers) <= fd) {
|
while (ARR_GetSize(file_handlers) <= fd) {
|
||||||
@@ -219,13 +229,16 @@ SCH_RemoveFileHandler(int fd)
|
|||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
SCH_SetFileHandlerEvents(int fd, int events)
|
SCH_SetFileHandlerEvent(int fd, int event, int enable)
|
||||||
{
|
{
|
||||||
FileHandlerEntry *ptr;
|
FileHandlerEntry *ptr;
|
||||||
|
|
||||||
assert(events);
|
|
||||||
ptr = ARR_GetElement(file_handlers, fd);
|
ptr = ARR_GetElement(file_handlers, fd);
|
||||||
ptr->events = events;
|
|
||||||
|
if (enable)
|
||||||
|
ptr->events |= event;
|
||||||
|
else
|
||||||
|
ptr->events &= ~event;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -244,6 +257,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
|
#define TQE_ALLOC_QUANTUM 32
|
||||||
|
|
||||||
static TimerQueueEntry *
|
static TimerQueueEntry *
|
||||||
@@ -350,7 +371,7 @@ SCH_AddTimeoutByDelay(double delay, SCH_TimeoutHandler handler, SCH_ArbitraryArg
|
|||||||
LCL_ReadRawTime(&now);
|
LCL_ReadRawTime(&now);
|
||||||
UTI_AddDoubleToTimespec(&now, delay, &then);
|
UTI_AddDoubleToTimespec(&now, delay, &then);
|
||||||
if (UTI_CompareTimespecs(&now, &then) > 0) {
|
if (UTI_CompareTimespecs(&now, &then) > 0) {
|
||||||
LOG_FATAL(LOGF_Scheduler, "Timeout overflow");
|
LOG_FATAL("Timeout overflow");
|
||||||
}
|
}
|
||||||
|
|
||||||
return SCH_AddTimeout(&then, handler, arg);
|
return SCH_AddTimeout(&then, handler, arg);
|
||||||
@@ -469,6 +490,20 @@ SCH_RemoveTimeout(SCH_TimeoutID id)
|
|||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
void
|
||||||
|
SCH_Reset(void)
|
||||||
|
{
|
||||||
|
while (n_timer_queue_entries > 0)
|
||||||
|
SCH_RemoveTimeout(timer_queue.next->id);
|
||||||
|
|
||||||
|
while (one_highest_fd > 0) {
|
||||||
|
close(one_highest_fd - 1);
|
||||||
|
SCH_RemoveFileHandler(one_highest_fd - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Try to dispatch any timeouts that have already gone by, and
|
/* Try to dispatch any timeouts that have already gone by, and
|
||||||
keep going until all are done. (The earlier ones may take so
|
keep going until all are done. (The earlier ones may take so
|
||||||
@@ -512,7 +547,7 @@ dispatch_timeouts(struct timespec *now) {
|
|||||||
more time than was delay of a scheduled timeout. */
|
more time than was delay of a scheduled timeout. */
|
||||||
if (n_done > n_timer_queue_entries * 4 &&
|
if (n_done > n_timer_queue_entries * 4 &&
|
||||||
n_done > n_entries_on_start * 4) {
|
n_done > n_entries_on_start * 4) {
|
||||||
LOG_FATAL(LOGF_Scheduler, "Possible infinite loop in scheduling");
|
LOG_FATAL("Possible infinite loop in scheduling");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -531,7 +566,8 @@ dispatch_filehandlers(int nfd, fd_set *read_fds, fd_set *write_fds, fd_set *exce
|
|||||||
if (except_fds && FD_ISSET(fd, except_fds)) {
|
if (except_fds && FD_ISSET(fd, except_fds)) {
|
||||||
/* This descriptor has an exception, dispatch its handler */
|
/* This descriptor has an exception, dispatch its handler */
|
||||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
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--;
|
nfd--;
|
||||||
|
|
||||||
/* Don't try to read from it now */
|
/* Don't try to read from it now */
|
||||||
@@ -544,14 +580,16 @@ dispatch_filehandlers(int nfd, fd_set *read_fds, fd_set *write_fds, fd_set *exce
|
|||||||
if (read_fds && FD_ISSET(fd, read_fds)) {
|
if (read_fds && FD_ISSET(fd, read_fds)) {
|
||||||
/* This descriptor can be read from, dispatch its handler */
|
/* This descriptor can be read from, dispatch its handler */
|
||||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
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--;
|
nfd--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (write_fds && FD_ISSET(fd, write_fds)) {
|
if (write_fds && FD_ISSET(fd, write_fds)) {
|
||||||
/* This descriptor can be written to, dispatch its handler */
|
/* This descriptor can be written to, dispatch its handler */
|
||||||
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fd);
|
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--;
|
nfd--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -680,10 +718,10 @@ check_current_time(struct timespec *prev_raw, struct timespec *raw, int timeout,
|
|||||||
|
|
||||||
if (last_select_ts_raw.tv_sec + elapsed_min.tv_sec >
|
if (last_select_ts_raw.tv_sec + elapsed_min.tv_sec >
|
||||||
raw->tv_sec + JUMP_DETECT_THRESHOLD) {
|
raw->tv_sec + JUMP_DETECT_THRESHOLD) {
|
||||||
LOG(LOGS_WARN, LOGF_Scheduler, "Backward time jump detected!");
|
LOG(LOGS_WARN, "Backward time jump detected!");
|
||||||
} else if (prev_raw->tv_sec + elapsed_max.tv_sec + JUMP_DETECT_THRESHOLD <
|
} else if (prev_raw->tv_sec + elapsed_max.tv_sec + JUMP_DETECT_THRESHOLD <
|
||||||
raw->tv_sec) {
|
raw->tv_sec) {
|
||||||
LOG(LOGS_WARN, LOGF_Scheduler, "Forward time jump detected!");
|
LOG(LOGS_WARN, "Forward time jump detected!");
|
||||||
} else {
|
} else {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -700,6 +738,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
|
void
|
||||||
SCH_MainLoop(void)
|
SCH_MainLoop(void)
|
||||||
{
|
{
|
||||||
@@ -742,7 +805,7 @@ SCH_MainLoop(void)
|
|||||||
/* if there are no file descriptors being waited on and no
|
/* if there are no file descriptors being waited on and no
|
||||||
timeout set, this is clearly ridiculous, so stop the run */
|
timeout set, this is clearly ridiculous, so stop the run */
|
||||||
if (!ptv && !p_read_fds && !p_write_fds)
|
if (!ptv && !p_read_fds && !p_write_fds)
|
||||||
LOG_FATAL(LOGF_Scheduler, "Nothing to do");
|
LOG_FATAL("Nothing to do");
|
||||||
|
|
||||||
status = select(one_highest_fd, p_read_fds, p_write_fds, p_except_fds, ptv);
|
status = select(one_highest_fd, p_read_fds, p_write_fds, p_except_fds, ptv);
|
||||||
errsv = errno;
|
errsv = errno;
|
||||||
@@ -756,13 +819,15 @@ SCH_MainLoop(void)
|
|||||||
LCL_CookTime(&now, &cooked, &err);
|
LCL_CookTime(&now, &cooked, &err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_monotonic_time(&cooked, &last_select_ts);
|
||||||
|
|
||||||
last_select_ts_raw = now;
|
last_select_ts_raw = now;
|
||||||
last_select_ts = cooked;
|
last_select_ts = cooked;
|
||||||
last_select_ts_err = err;
|
last_select_ts_err = err;
|
||||||
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
if (!need_to_exit && errsv != EINTR) {
|
if (!need_to_exit && errsv != EINTR) {
|
||||||
LOG_FATAL(LOGF_Scheduler, "select() failed : %s", strerror(errsv));
|
LOG_FATAL("select() failed : %s", strerror(errsv));
|
||||||
}
|
}
|
||||||
} else if (status > 0) {
|
} else if (status > 0) {
|
||||||
/* A file descriptor is ready for input or output */
|
/* A file descriptor is ready for input or output */
|
||||||
|
|||||||
11
sched.h
11
sched.h
@@ -34,7 +34,8 @@ typedef unsigned int SCH_TimeoutID;
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SCH_ReservedTimeoutValue = 0,
|
SCH_ReservedTimeoutValue = 0,
|
||||||
SCH_NtpSamplingClass,
|
SCH_NtpClientClass,
|
||||||
|
SCH_NtpPeerClass,
|
||||||
SCH_NtpBroadcastClass,
|
SCH_NtpBroadcastClass,
|
||||||
SCH_NumberOfClasses /* needs to be last */
|
SCH_NumberOfClasses /* needs to be last */
|
||||||
} SCH_TimeoutClass;
|
} SCH_TimeoutClass;
|
||||||
@@ -59,11 +60,14 @@ extern void SCH_Finalise(void);
|
|||||||
/* Register a handler for when select goes true on a file descriptor */
|
/* 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_AddFileHandler(int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg);
|
||||||
extern void SCH_RemoveFileHandler(int fd);
|
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 */
|
/* 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);
|
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 */
|
/* 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);
|
extern SCH_TimeoutID SCH_AddTimeout(struct timespec *ts, SCH_TimeoutHandler handler, SCH_ArbitraryArgument arg);
|
||||||
|
|
||||||
@@ -81,6 +85,9 @@ extern SCH_TimeoutID SCH_AddTimeoutInClass(double min_delay, double separation,
|
|||||||
/* The next one probably ought to return a status code */
|
/* The next one probably ought to return a status code */
|
||||||
extern void SCH_RemoveTimeout(SCH_TimeoutID);
|
extern void SCH_RemoveTimeout(SCH_TimeoutID);
|
||||||
|
|
||||||
|
/* Remove all timeouts and close all file descriptors */
|
||||||
|
extern void SCH_Reset(void);
|
||||||
|
|
||||||
extern void SCH_MainLoop(void);
|
extern void SCH_MainLoop(void);
|
||||||
|
|
||||||
extern void SCH_QuitProgram(void);
|
extern void SCH_QuitProgram(void);
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user