Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NTP client/server, based on OpenNTPD 3.9p1 |
| 3 | * |
Adam Tkac | 4bf88d9 | 2015-01-04 17:46:08 +0100 | [diff] [blame] | 4 | * Busybox port author: Adam Tkac (C) 2009 <vonsch@gmail.com> |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 5 | * |
Adam Tkac | 4bf88d9 | 2015-01-04 17:46:08 +0100 | [diff] [blame] | 6 | * OpenNTPd 3.9p1 copyright holders: |
| 7 | * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
| 8 | * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org> |
| 9 | * |
| 10 | * OpenNTPd code is licensed under ISC-style licence: |
| 11 | * |
| 12 | * Permission to use, copy, modify, and distribute this software for any |
| 13 | * purpose with or without fee is hereby granted, provided that the above |
| 14 | * copyright notice and this permission notice appear in all copies. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 17 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 19 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 20 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
| 21 | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 22 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 23 | *********************************************************************** |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 24 | * |
| 25 | * Parts of OpenNTPD clock syncronization code is replaced by |
Adam Tkac | 4bf88d9 | 2015-01-04 17:46:08 +0100 | [diff] [blame] | 26 | * code which is based on ntp-4.2.6, which carries the following |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 27 | * copyright notice: |
| 28 | * |
Adam Tkac | 4bf88d9 | 2015-01-04 17:46:08 +0100 | [diff] [blame] | 29 | * Copyright (c) University of Delaware 1992-2009 |
| 30 | * |
| 31 | * Permission to use, copy, modify, and distribute this software and |
| 32 | * its documentation for any purpose with or without fee is hereby |
| 33 | * granted, provided that the above copyright notice appears in all |
| 34 | * copies and that both the copyright notice and this permission |
| 35 | * notice appear in supporting documentation, and that the name |
| 36 | * University of Delaware not be used in advertising or publicity |
| 37 | * pertaining to distribution of the software without specific, |
| 38 | * written prior permission. The University of Delaware makes no |
| 39 | * representations about the suitability this software for any |
| 40 | * purpose. It is provided "as is" without express or implied warranty. |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 41 | *********************************************************************** |
| 42 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 43 | //config:config NTPD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 44 | //config: bool "ntpd (22 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 45 | //config: default y |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 46 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 47 | //config: The NTP client/server daemon. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 48 | //config: |
| 49 | //config:config FEATURE_NTPD_SERVER |
| 50 | //config: bool "Make ntpd usable as a NTP server" |
| 51 | //config: default y |
| 52 | //config: depends on NTPD |
| 53 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 54 | //config: Make ntpd usable as a NTP server. If you disable this option |
| 55 | //config: ntpd will be usable only as a NTP client. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 56 | //config: |
| 57 | //config:config FEATURE_NTPD_CONF |
| 58 | //config: bool "Make ntpd understand /etc/ntp.conf" |
| 59 | //config: default y |
| 60 | //config: depends on NTPD |
| 61 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 62 | //config: Make ntpd look in /etc/ntp.conf for peers. Only "server address" |
| 63 | //config: is supported. |
Denys Vlasenko | 63d053d | 2018-10-30 23:07:26 +0100 | [diff] [blame] | 64 | //config: |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 65 | //config:config FEATURE_NTP_AUTH |
| 66 | //config: bool "Support md5/sha1 message authentication codes" |
Denys Vlasenko | 63d053d | 2018-10-30 23:07:26 +0100 | [diff] [blame] | 67 | //config: default y |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 68 | //config: depends on NTPD |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 69 | |
| 70 | //applet:IF_NTPD(APPLET(ntpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 71 | |
| 72 | //kbuild:lib-$(CONFIG_NTPD) += ntpd.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 73 | |
| 74 | //usage:#define ntpd_trivial_usage |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 75 | //usage: "[-dnqNw"IF_FEATURE_NTPD_SERVER("l] [-I IFACE")"] [-S PROG]" |
| 76 | //usage: IF_NOT_FEATURE_NTP_AUTH(" [-p PEER]...") |
| 77 | //usage: IF_FEATURE_NTP_AUTH(" [-k KEYFILE] [-p [keyno:N:]PEER]...") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 78 | //usage:#define ntpd_full_usage "\n\n" |
| 79 | //usage: "NTP client/server\n" |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 80 | //usage: "\n -d[d] Verbose" |
Denys Vlasenko | 50c5b36 | 2021-10-08 15:02:53 +0200 | [diff] [blame] | 81 | //usage: "\n -n Run in foreground" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 82 | //usage: "\n -q Quit after clock is set" |
| 83 | //usage: "\n -N Run at high priority" |
| 84 | //usage: "\n -w Do not set time (only query peers), implies -n" |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 85 | //usage: "\n -S PROG Run PROG after stepping time, stratum change, and every 11 min" |
| 86 | //usage: IF_NOT_FEATURE_NTP_AUTH( |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 87 | //usage: "\n -p PEER Obtain time from PEER (may be repeated)" |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 88 | //usage: ) |
| 89 | //usage: IF_FEATURE_NTP_AUTH( |
| 90 | //usage: "\n -k FILE Key file (ntp.keys compatible)" |
| 91 | //usage: "\n -p [keyno:NUM:]PEER" |
| 92 | //usage: "\n Obtain time from PEER (may be repeated)" |
| 93 | //usage: "\n Use key NUM for authentication" |
| 94 | //usage: ) |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 95 | //usage: IF_FEATURE_NTPD_CONF( |
Denys Vlasenko | 3c31b09 | 2015-03-05 14:04:44 +0100 | [diff] [blame] | 96 | //usage: "\n If -p is not given, 'server HOST' lines" |
| 97 | //usage: "\n from /etc/ntp.conf are used" |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 98 | //usage: ) |
Denys Vlasenko | 3aef814 | 2015-03-02 20:59:13 +0100 | [diff] [blame] | 99 | //usage: IF_FEATURE_NTPD_SERVER( |
| 100 | //usage: "\n -l Also run as server on port 123" |
| 101 | //usage: "\n -I IFACE Bind server to IFACE, implies -l" |
| 102 | //usage: ) |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 103 | |
| 104 | // -l and -p options are not compatible with "standard" ntpd: |
| 105 | // it has them as "-l logfile" and "-p pidfile". |
| 106 | // -S and -w are not compat either, "standard" ntpd has no such opts. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 107 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 108 | #include "libbb.h" |
| 109 | #include <math.h> |
Codarren Velvindron | 7c43d43 | 2018-04-15 20:37:50 +0400 | [diff] [blame] | 110 | #include <netinet/ip.h> /* For IPTOS_DSCP_AF21 definition */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 111 | #include <sys/timex.h> |
Codarren Velvindron | 7c43d43 | 2018-04-15 20:37:50 +0400 | [diff] [blame] | 112 | #ifndef IPTOS_DSCP_AF21 |
| 113 | # define IPTOS_DSCP_AF21 0x48 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 114 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 115 | |
Alex Samorukov | f1baa4a | 2021-01-04 01:28:39 +0100 | [diff] [blame] | 116 | #if defined(__FreeBSD__) |
| 117 | /* see sys/timex.h */ |
| 118 | # define adjtimex ntp_adjtime |
| 119 | # define ADJ_OFFSET MOD_OFFSET |
| 120 | # define ADJ_STATUS MOD_STATUS |
| 121 | # define ADJ_TIMECONST MOD_TIMECONST |
| 122 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 123 | |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 124 | /* Verbosity control (max level of -dddd options accepted). |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 125 | * max 6 is very talkative (and bloated). 3 is non-bloated, |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 126 | * production level setting. |
| 127 | */ |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 128 | #define MAX_VERBOSE 3 |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 129 | |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 130 | /* High-level description of the algorithm: |
| 131 | * |
| 132 | * We start running with very small poll_exp, BURSTPOLL, |
Leonid Lisovskiy | 894ef60 | 2010-10-20 22:36:51 +0200 | [diff] [blame] | 133 | * in order to quickly accumulate INITIAL_SAMPLES datapoints |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 134 | * for each peer. Then, time is stepped if the offset is larger |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 135 | * than STEP_THRESHOLD, otherwise it isn't stepped. |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 136 | * |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 137 | * Then poll_exp is set to MINPOLL, and we enter "steady state": we collect |
| 138 | * a datapoint, we select the best peer, if this datapoint is not a new one |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 139 | * (IOW: if this datapoint isn't for selected peer), sleep |
| 140 | * and collect another one; otherwise, use its offset to update |
| 141 | * frequency drift, if offset is somewhat large, reduce poll_exp, |
| 142 | * otherwise increase poll_exp. |
| 143 | * |
| 144 | * If offset is larger than STEP_THRESHOLD, which shouldn't normally |
| 145 | * happen, we assume that something "bad" happened (computer |
| 146 | * was hibernated, someone set totally wrong date, etc), |
| 147 | * then the time is stepped, all datapoints are discarded, |
| 148 | * and we go back to steady state. |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 149 | * |
| 150 | * Made some changes to speed up re-syncing after our clock goes bad |
| 151 | * (tested with suspending my laptop): |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 152 | * - if largish offset (>= STEP_THRESHOLD == 1 sec) is seen |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 153 | * from a peer, schedule next query for this peer soon |
| 154 | * without drastically lowering poll interval for everybody. |
| 155 | * This makes us collect enough data for step much faster: |
| 156 | * e.g. at poll = 10 (1024 secs), step was done within 5 minutes |
| 157 | * after first reply which indicated that our clock is 14 seconds off. |
| 158 | * - on step, do not discard d_dispersion data of the existing datapoints, |
| 159 | * do not clear reachable_bits. This prevents discarding first ~8 |
| 160 | * datapoints after the step. |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 161 | */ |
| 162 | |
Denys Vlasenko | 2d6c175 | 2021-03-02 12:07:14 +0100 | [diff] [blame] | 163 | #define INITIAL_SAMPLES 3 /* how many samples do we want for init */ |
Denys Vlasenko | ea096d6 | 2019-10-30 12:13:46 +0100 | [diff] [blame] | 164 | #define MIN_FREQHOLD 10 /* adjust offset, but not freq in this many first adjustments */ |
Denys Vlasenko | 9b1c8bf | 2018-08-03 11:03:55 +0200 | [diff] [blame] | 165 | #define BAD_DELAY_GROWTH 4 /* drop packet if its delay grew by more than this factor */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 166 | |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 167 | #define RETRY_INTERVAL 32 /* on send/recv error, retry in N secs (need to be power of 2) */ |
| 168 | #define NOREPLY_INTERVAL 512 /* sent, but got no reply: cap next query by this many seconds */ |
| 169 | #define RESPONSE_INTERVAL 16 /* wait for reply up to N secs */ |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 170 | #define HOSTNAME_INTERVAL 4 /* hostname lookup failed. Wait N * peer->dns_errors secs for next try */ |
| 171 | #define DNS_ERRORS_CAP 0x3f /* peer->dns_errors is in [0..63] */ |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 172 | |
| 173 | /* Step threshold (sec). std ntpd uses 0.128. |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 174 | */ |
| 175 | #define STEP_THRESHOLD 1 |
| 176 | /* Slew threshold (sec): adjtimex() won't accept offsets larger than this. |
Denys Vlasenko | 8502fa8 | 2019-02-15 14:32:08 +0100 | [diff] [blame] | 177 | * Using exact power of 2 (1/8, 1/2 etc) results in smaller code |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 178 | */ |
Denys Vlasenko | 8502fa8 | 2019-02-15 14:32:08 +0100 | [diff] [blame] | 179 | #define SLEW_THRESHOLD 0.5 |
| 180 | // ^^^^ used to be 0.125. |
| 181 | // Since Linux 2.6.26 (circa 2006), kernel accepts (-0.5s, +0.5s) range |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 182 | |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 183 | |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 184 | // #define PANIC_THRESHOLD 1000 /* panic threshold (sec) */ |
| 185 | |
| 186 | /* If we got |offset| > BIGOFF from a peer, cap next query interval |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 187 | * for this peer by this many seconds: |
| 188 | */ |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 189 | #define BIGOFF STEP_THRESHOLD |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 190 | #define BIGOFF_INTERVAL (1 << 7) /* 128 s */ |
| 191 | |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 192 | #define FREQ_TOLERANCE 0.000015 /* frequency tolerance (15 PPM) */ |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 193 | #define BURSTPOLL 0 /* initial poll */ |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 194 | #define MINPOLL 5 /* minimum poll interval. std ntpd uses 6 (6: 64 sec) */ |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 195 | /* If offset > discipline_jitter * POLLADJ_GATE, and poll interval is > 2^BIGPOLL, |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 196 | * then it is decreased _at once_. (If <= 2^BIGPOLL, it will be decreased _eventually_). |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 197 | */ |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 198 | #define BIGPOLL 9 /* 2^9 sec ~= 8.5 min */ |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 199 | #define MAXPOLL 12 /* maximum poll interval (12: 1.1h, 17: 36.4h). std ntpd uses 17 */ |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 200 | /* Actively lower poll when we see such big offsets. |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 201 | * With SLEW_THRESHOLD = 0.125, it means we try to sync more aggressively |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 202 | * if offset increases over ~0.04 sec |
| 203 | */ |
Denys Vlasenko | 2d48d9b | 2021-03-02 19:54:09 +0100 | [diff] [blame] | 204 | // #define POLLDOWN_OFFSET (SLEW_THRESHOLD / 3) |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 205 | #define MINDISP 0.01 /* minimum dispersion (sec) */ |
| 206 | #define MAXDISP 16 /* maximum dispersion (sec) */ |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 207 | #define MAXSTRAT 16 /* maximum stratum (infinity metric) */ |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 208 | #define MAXDIST 1 /* distance threshold (sec) */ |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 209 | #define MIN_SELECTED 1 /* minimum intersection survivors */ |
| 210 | #define MIN_CLUSTERED 3 /* minimum cluster survivors */ |
| 211 | |
Denys Vlasenko | 2d6c175 | 2021-03-02 12:07:14 +0100 | [diff] [blame] | 212 | /* Correct frequency ourself (0) or let kernel do it (1)? */ |
| 213 | #define USING_KERNEL_PLL_LOOP 1 |
| 214 | // /* frequency drift we can correct (500 PPM) */ |
| 215 | // #define MAXDRIFT 0.000500 |
| 216 | // /* Compromise Allan intercept (sec). doc uses 1500, std ntpd uses 512 */ |
| 217 | // #define ALLAN 512 |
| 218 | // /* PLL loop gain */ |
| 219 | // #define PLL 65536 |
| 220 | // /* FLL loop gain [why it depends on MAXPOLL??] */ |
| 221 | // #define FLL (MAXPOLL + 1) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 222 | |
| 223 | /* Poll-adjust threshold. |
| 224 | * When we see that offset is small enough compared to discipline jitter, |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 225 | * we grow a counter: += MINPOLL. When counter goes over POLLADJ_LIMIT, |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 226 | * we poll_exp++. If offset isn't small, counter -= poll_exp*2, |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 227 | * and when it goes below -POLLADJ_LIMIT, we poll_exp--. |
| 228 | * (Bumped from 30 to 40 since otherwise I often see poll_exp going *2* steps down) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 229 | */ |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 230 | #define POLLADJ_LIMIT 40 |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 231 | /* If offset < discipline_jitter * POLLADJ_GATE, then we decide to increase |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 232 | * poll interval (we think we can't improve timekeeping |
| 233 | * by staying at smaller poll). |
| 234 | */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 235 | #define POLLADJ_GATE 4 |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 236 | #define TIMECONST_HACK_GATE 2 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 237 | /* Parameter averaging constant */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 238 | #define AVG 4 |
| 239 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 240 | #define MAX_KEY_NUMBER 65535 |
| 241 | #define KEYID_SIZE sizeof(uint32_t) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 242 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 243 | enum { |
| 244 | NTP_VERSION = 4, |
| 245 | NTP_MAXSTRATUM = 15, |
| 246 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 247 | NTP_MD5_DIGESTSIZE = 16, |
| 248 | NTP_MSGSIZE_NOAUTH = 48, |
| 249 | NTP_MSGSIZE_MD5_AUTH = NTP_MSGSIZE_NOAUTH + KEYID_SIZE + NTP_MD5_DIGESTSIZE, |
| 250 | NTP_SHA1_DIGESTSIZE = 20, |
| 251 | NTP_MSGSIZE_SHA1_AUTH = NTP_MSGSIZE_NOAUTH + KEYID_SIZE + NTP_SHA1_DIGESTSIZE, |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 252 | |
| 253 | /* Status Masks */ |
| 254 | MODE_MASK = (7 << 0), |
| 255 | VERSION_MASK = (7 << 3), |
| 256 | VERSION_SHIFT = 3, |
| 257 | LI_MASK = (3 << 6), |
| 258 | |
| 259 | /* Leap Second Codes (high order two bits of m_status) */ |
| 260 | LI_NOWARNING = (0 << 6), /* no warning */ |
| 261 | LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */ |
| 262 | LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */ |
| 263 | LI_ALARM = (3 << 6), /* alarm condition */ |
| 264 | |
| 265 | /* Mode values */ |
| 266 | MODE_RES0 = 0, /* reserved */ |
| 267 | MODE_SYM_ACT = 1, /* symmetric active */ |
| 268 | MODE_SYM_PAS = 2, /* symmetric passive */ |
| 269 | MODE_CLIENT = 3, /* client */ |
| 270 | MODE_SERVER = 4, /* server */ |
| 271 | MODE_BROADCAST = 5, /* broadcast */ |
| 272 | MODE_RES1 = 6, /* reserved for NTP control message */ |
| 273 | MODE_RES2 = 7, /* reserved for private use */ |
| 274 | }; |
| 275 | |
| 276 | //TODO: better base selection |
| 277 | #define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */ |
| 278 | |
| 279 | #define NUM_DATAPOINTS 8 |
| 280 | |
| 281 | typedef struct { |
| 282 | uint32_t int_partl; |
| 283 | uint32_t fractionl; |
| 284 | } l_fixedpt_t; |
| 285 | |
| 286 | typedef struct { |
| 287 | uint16_t int_parts; |
| 288 | uint16_t fractions; |
| 289 | } s_fixedpt_t; |
| 290 | |
| 291 | typedef struct { |
| 292 | uint8_t m_status; /* status of local clock and leap info */ |
| 293 | uint8_t m_stratum; |
| 294 | uint8_t m_ppoll; /* poll value */ |
| 295 | int8_t m_precision_exp; |
| 296 | s_fixedpt_t m_rootdelay; |
| 297 | s_fixedpt_t m_rootdisp; |
| 298 | uint32_t m_refid; |
| 299 | l_fixedpt_t m_reftime; |
| 300 | l_fixedpt_t m_orgtime; |
| 301 | l_fixedpt_t m_rectime; |
| 302 | l_fixedpt_t m_xmttime; |
| 303 | uint32_t m_keyid; |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 304 | uint8_t m_digest[ENABLE_FEATURE_NTP_AUTH ? NTP_SHA1_DIGESTSIZE : NTP_MD5_DIGESTSIZE]; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 305 | } msg_t; |
| 306 | |
| 307 | typedef struct { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 308 | double d_offset; |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 309 | double d_recv_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 310 | double d_dispersion; |
| 311 | } datapoint_t; |
| 312 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 313 | #if ENABLE_FEATURE_NTP_AUTH |
| 314 | enum { |
| 315 | HASH_MD5, |
| 316 | HASH_SHA1, |
| 317 | }; |
| 318 | typedef struct { |
| 319 | unsigned id; //try uint16_t? |
| 320 | smalluint type; |
| 321 | smalluint msg_size; |
| 322 | smalluint key_length; |
| 323 | char key[0]; |
| 324 | } key_entry_t; |
| 325 | #endif |
| 326 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 327 | typedef struct { |
| 328 | len_and_sockaddr *p_lsa; |
| 329 | char *p_dotted; |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 330 | #if ENABLE_FEATURE_NTP_AUTH |
| 331 | key_entry_t *key_entry; |
| 332 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 333 | int p_fd; |
| 334 | int datapoint_idx; |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 335 | #if ENABLE_FEATURE_NTPD_SERVER |
| 336 | uint32_t p_refid; |
| 337 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 338 | uint32_t lastpkt_refid; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 339 | uint8_t lastpkt_status; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 340 | uint8_t lastpkt_stratum; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 341 | uint8_t reachable_bits; |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 342 | uint8_t dns_errors; |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 343 | /* when to send new query (if p_fd == -1) |
| 344 | * or when receive times out (if p_fd >= 0): */ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 345 | double next_action_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 346 | double p_xmttime; |
Denys Vlasenko | d531f93 | 2014-04-19 19:00:16 +0200 | [diff] [blame] | 347 | double p_raw_delay; |
| 348 | /* p_raw_delay is set even by "high delay" packets */ |
| 349 | /* lastpkt_delay isn't */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 350 | double lastpkt_recv_time; |
| 351 | double lastpkt_delay; |
| 352 | double lastpkt_rootdelay; |
| 353 | double lastpkt_rootdisp; |
| 354 | /* produced by filter algorithm: */ |
| 355 | double filter_offset; |
| 356 | double filter_dispersion; |
| 357 | double filter_jitter; |
| 358 | datapoint_t filter_datapoint[NUM_DATAPOINTS]; |
| 359 | /* last sent packet: */ |
| 360 | msg_t p_xmt_msg; |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 361 | char p_hostname[1]; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 362 | } peer_t; |
| 363 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 364 | enum { |
| 365 | OPT_n = (1 << 0), |
| 366 | OPT_q = (1 << 1), |
| 367 | OPT_N = (1 << 2), |
| 368 | OPT_x = (1 << 3), |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 369 | OPT_k = (1 << 4) * ENABLE_FEATURE_NTP_AUTH, |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 370 | /* Insert new options above this line. */ |
| 371 | /* Non-compat options: */ |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 372 | OPT_w = (1 << (4+ENABLE_FEATURE_NTP_AUTH)), |
| 373 | OPT_p = (1 << (5+ENABLE_FEATURE_NTP_AUTH)), |
| 374 | OPT_S = (1 << (6+ENABLE_FEATURE_NTP_AUTH)), |
| 375 | OPT_l = (1 << (7+ENABLE_FEATURE_NTP_AUTH)) * ENABLE_FEATURE_NTPD_SERVER, |
| 376 | OPT_I = (1 << (8+ENABLE_FEATURE_NTP_AUTH)) * ENABLE_FEATURE_NTPD_SERVER, |
Denys Vlasenko | 8e23faf | 2011-04-07 01:45:20 +0200 | [diff] [blame] | 377 | /* We hijack some bits for other purposes */ |
Denys Vlasenko | 16c52a5 | 2012-02-23 14:28:47 +0100 | [diff] [blame] | 378 | OPT_qq = (1 << 31), |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | struct globals { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 382 | double cur_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 383 | /* total round trip delay to currently selected reference clock */ |
| 384 | double rootdelay; |
| 385 | /* reference timestamp: time when the system clock was last set or corrected */ |
| 386 | double reftime; |
| 387 | /* total dispersion to currently selected reference clock */ |
| 388 | double rootdisp; |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 389 | |
| 390 | double last_script_run; |
| 391 | char *script_name; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 392 | llist_t *ntp_peers; |
| 393 | #if ENABLE_FEATURE_NTPD_SERVER |
| 394 | int listen_fd; |
Denys Vlasenko | 278842d | 2014-07-15 15:06:54 +0200 | [diff] [blame] | 395 | char *if_name; |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 396 | # define G_listen_fd (G.listen_fd) |
| 397 | #else |
| 398 | # define G_listen_fd (-1) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 399 | #endif |
| 400 | unsigned verbose; |
| 401 | unsigned peer_cnt; |
| 402 | /* refid: 32-bit code identifying the particular server or reference clock |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 403 | * in stratum 0 packets this is a four-character ASCII string, |
| 404 | * called the kiss code, used for debugging and monitoring |
| 405 | * in stratum 1 packets this is a four-character ASCII string |
| 406 | * assigned to the reference clock by IANA. Example: "GPS " |
| 407 | * in stratum 2+ packets, it's IPv4 address or 4 first bytes |
| 408 | * of MD5 hash of IPv6 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 409 | */ |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 410 | #if ENABLE_FEATURE_NTPD_SERVER |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 411 | uint32_t refid; |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 412 | #endif |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 413 | uint8_t ntp_status; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 414 | /* precision is defined as the larger of the resolution and time to |
| 415 | * read the clock, in log2 units. For instance, the precision of a |
| 416 | * mains-frequency clock incrementing at 60 Hz is 16 ms, even when the |
| 417 | * system clock hardware representation is to the nanosecond. |
| 418 | * |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 419 | * Delays, jitters of various kinds are clamped down to precision. |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 420 | * |
| 421 | * If precision_sec is too large, discipline_jitter gets clamped to it |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 422 | * and if offset is smaller than discipline_jitter * POLLADJ_GATE, poll |
| 423 | * interval grows even though we really can benefit from staying at |
| 424 | * smaller one, collecting non-lagged datapoits and correcting offset. |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 425 | * (Lagged datapoits exist when poll_exp is large but we still have |
| 426 | * systematic offset error - the time distance between datapoints |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 427 | * is significant and older datapoints have smaller offsets. |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 428 | * This makes our offset estimation a bit smaller than reality) |
| 429 | * Due to this effect, setting G_precision_sec close to |
| 430 | * STEP_THRESHOLD isn't such a good idea - offsets may grow |
| 431 | * too big and we will step. I observed it with -6. |
| 432 | * |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 433 | * OTOH, setting precision_sec far too small would result in futile |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 434 | * attempts to synchronize to an unachievable precision. |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 435 | * |
| 436 | * -6 is 1/64 sec, -7 is 1/128 sec and so on. |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 437 | * -8 is 1/256 ~= 0.003906 (worked well for me --vda) |
| 438 | * -9 is 1/512 ~= 0.001953 (let's try this for some time) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 439 | */ |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 440 | #define G_precision_exp -9 |
| 441 | /* |
Denys Vlasenko | 2d6c175 | 2021-03-02 12:07:14 +0100 | [diff] [blame] | 442 | * G_precision_exp is used only for constructing outgoing packets. |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 443 | * It's ok to set G_precision_sec to a slightly different value |
| 444 | * (One which is "nicer looking" in logs). |
| 445 | * Exact value would be (1.0 / (1 << (- G_precision_exp))): |
| 446 | */ |
| 447 | #define G_precision_sec 0.002 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 448 | uint8_t stratum; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 449 | |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 450 | //uint8_t discipline_state; // doc calls it c.state |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 451 | uint8_t poll_exp; // s.poll |
| 452 | int polladj_count; // c.count |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 453 | int FREQHOLD_cnt; |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 454 | long kernel_freq_drift; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 455 | peer_t *last_update_peer; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 456 | double last_update_offset; // c.last |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 457 | double last_update_recv_time; // s.t |
| 458 | double discipline_jitter; // c.jitter |
Denys Vlasenko | 547ee79 | 2012-03-05 10:18:00 +0100 | [diff] [blame] | 459 | /* Since we only compare it with ints, can simplify code |
| 460 | * by not making this variable floating point: |
| 461 | */ |
| 462 | unsigned offset_to_jitter_ratio; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 463 | //double cluster_offset; // s.offset |
| 464 | //double cluster_jitter; // s.jitter |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 465 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 466 | double discipline_freq_drift; // c.freq |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 467 | /* Maybe conditionally calculate wander? it's used only for logging */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 468 | double discipline_wander; // c.wander |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 469 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 470 | }; |
| 471 | #define G (*ptr_to_globals) |
| 472 | |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 473 | #define VERB1 if (MAX_VERBOSE && G.verbose) |
| 474 | #define VERB2 if (MAX_VERBOSE >= 2 && G.verbose >= 2) |
| 475 | #define VERB3 if (MAX_VERBOSE >= 3 && G.verbose >= 3) |
| 476 | #define VERB4 if (MAX_VERBOSE >= 4 && G.verbose >= 4) |
| 477 | #define VERB5 if (MAX_VERBOSE >= 5 && G.verbose >= 5) |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 478 | #define VERB6 if (MAX_VERBOSE >= 6 && G.verbose >= 6) |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 479 | |
| 480 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 481 | static double LOG2D(int a) |
| 482 | { |
| 483 | if (a < 0) |
| 484 | return 1.0 / (1UL << -a); |
| 485 | return 1UL << a; |
| 486 | } |
| 487 | static ALWAYS_INLINE double SQUARE(double x) |
| 488 | { |
| 489 | return x * x; |
| 490 | } |
| 491 | static ALWAYS_INLINE double MAXD(double a, double b) |
| 492 | { |
| 493 | if (a > b) |
| 494 | return a; |
| 495 | return b; |
| 496 | } |
Denys Vlasenko | e5897d0 | 2019-10-25 13:05:15 +0200 | [diff] [blame] | 497 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 498 | static ALWAYS_INLINE double MIND(double a, double b) |
| 499 | { |
| 500 | if (a < b) |
| 501 | return a; |
| 502 | return b; |
| 503 | } |
Denys Vlasenko | e5897d0 | 2019-10-25 13:05:15 +0200 | [diff] [blame] | 504 | #endif |
Denys Vlasenko | d498ff0 | 2010-01-03 21:06:27 +0100 | [diff] [blame] | 505 | static NOINLINE double my_SQRT(double X) |
| 506 | { |
| 507 | union { |
| 508 | float f; |
| 509 | int32_t i; |
| 510 | } v; |
| 511 | double invsqrt; |
| 512 | double Xhalf = X * 0.5; |
| 513 | |
| 514 | /* Fast and good approximation to 1/sqrt(X), black magic */ |
| 515 | v.f = X; |
| 516 | /*v.i = 0x5f3759df - (v.i >> 1);*/ |
| 517 | v.i = 0x5f375a86 - (v.i >> 1); /* - this constant is slightly better */ |
| 518 | invsqrt = v.f; /* better than 0.2% accuracy */ |
| 519 | |
| 520 | /* Refining it using Newton's method: x1 = x0 - f(x0)/f'(x0) |
| 521 | * f(x) = 1/(x*x) - X (f==0 when x = 1/sqrt(X)) |
| 522 | * f'(x) = -2/(x*x*x) |
| 523 | * f(x)/f'(x) = (X - 1/(x*x)) / (2/(x*x*x)) = X*x*x*x/2 - x/2 |
| 524 | * x1 = x0 - (X*x0*x0*x0/2 - x0/2) = 1.5*x0 - X*x0*x0*x0/2 = x0*(1.5 - (X/2)*x0*x0) |
| 525 | */ |
| 526 | invsqrt = invsqrt * (1.5 - Xhalf * invsqrt * invsqrt); /* ~0.05% accuracy */ |
| 527 | /* invsqrt = invsqrt * (1.5 - Xhalf * invsqrt * invsqrt); 2nd iter: ~0.0001% accuracy */ |
| 528 | /* With 4 iterations, more than half results will be exact, |
| 529 | * at 6th iterations result stabilizes with about 72% results exact. |
| 530 | * We are well satisfied with 0.05% accuracy. |
| 531 | */ |
| 532 | |
| 533 | return X * invsqrt; /* X * 1/sqrt(X) ~= sqrt(X) */ |
| 534 | } |
| 535 | static ALWAYS_INLINE double SQRT(double X) |
| 536 | { |
| 537 | /* If this arch doesn't use IEEE 754 floats, fall back to using libm */ |
| 538 | if (sizeof(float) != 4) |
| 539 | return sqrt(X); |
| 540 | |
Denys Vlasenko | 2d3253d | 2010-01-03 21:52:46 +0100 | [diff] [blame] | 541 | /* This avoids needing libm, saves about 0.5k on x86-32 */ |
Denys Vlasenko | d498ff0 | 2010-01-03 21:06:27 +0100 | [diff] [blame] | 542 | return my_SQRT(X); |
| 543 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 544 | |
| 545 | static double |
| 546 | gettime1900d(void) |
| 547 | { |
| 548 | struct timeval tv; |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 549 | xgettimeofday(&tv); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 550 | G.cur_time = tv.tv_sec + (1.0e-6 * tv.tv_usec) + OFFSET_1900_1970; |
| 551 | return G.cur_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static void |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 555 | d_to_tv(struct timeval *tv, double d) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 556 | { |
| 557 | tv->tv_sec = (long)d; |
| 558 | tv->tv_usec = (d - tv->tv_sec) * 1000000; |
| 559 | } |
| 560 | |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 561 | static NOINLINE double |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 562 | lfp_to_d(l_fixedpt_t lfp) |
| 563 | { |
| 564 | double ret; |
| 565 | lfp.int_partl = ntohl(lfp.int_partl); |
| 566 | lfp.fractionl = ntohl(lfp.fractionl); |
| 567 | ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX); |
| 568 | return ret; |
| 569 | } |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 570 | static NOINLINE double |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 571 | sfp_to_d(s_fixedpt_t sfp) |
| 572 | { |
| 573 | double ret; |
| 574 | sfp.int_parts = ntohs(sfp.int_parts); |
| 575 | sfp.fractions = ntohs(sfp.fractions); |
| 576 | ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX); |
| 577 | return ret; |
| 578 | } |
| 579 | #if ENABLE_FEATURE_NTPD_SERVER |
Denys Vlasenko | d005c9f | 2021-04-24 11:54:50 +0200 | [diff] [blame] | 580 | static NOINLINE void |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 581 | d_to_lfp(l_fixedpt_t *lfp, double d) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 582 | { |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 583 | uint32_t intl; |
| 584 | uint32_t frac; |
| 585 | intl = (uint32_t)d; |
| 586 | frac = (uint32_t)((d - intl) * UINT_MAX); |
| 587 | lfp->int_partl = htonl(intl); |
| 588 | lfp->fractionl = htonl(frac); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 589 | } |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 590 | static NOINLINE void |
| 591 | d_to_sfp(s_fixedpt_t *sfp, double d) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 592 | { |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 593 | uint16_t ints; |
| 594 | uint16_t frac; |
| 595 | ints = (uint16_t)d; |
| 596 | frac = (uint16_t)((d - ints) * USHRT_MAX); |
| 597 | sfp->int_parts = htons(ints); |
| 598 | sfp->fractions = htons(frac); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 599 | } |
| 600 | #endif |
| 601 | |
| 602 | static double |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 603 | dispersion(const datapoint_t *dp) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 604 | { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 605 | return dp->d_dispersion + FREQ_TOLERANCE * (G.cur_time - dp->d_recv_time); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static double |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 609 | root_distance(peer_t *p) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 610 | { |
| 611 | /* The root synchronization distance is the maximum error due to |
| 612 | * all causes of the local clock relative to the primary server. |
| 613 | * It is defined as half the total delay plus total dispersion |
| 614 | * plus peer jitter. |
| 615 | */ |
| 616 | return MAXD(MINDISP, p->lastpkt_rootdelay + p->lastpkt_delay) / 2 |
| 617 | + p->lastpkt_rootdisp |
| 618 | + p->filter_dispersion |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 619 | + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 620 | + p->filter_jitter; |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | set_next(peer_t *p, unsigned t) |
| 625 | { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 626 | p->next_action_time = G.cur_time + t; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Peer clock filter and its helpers |
| 631 | */ |
| 632 | static void |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 633 | filter_datapoints(peer_t *p) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 634 | { |
| 635 | int i, idx; |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 636 | double sum, wavg; |
| 637 | datapoint_t *fdp; |
| 638 | |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 639 | /* Simulations have shown that use of *averaged* offset for p->filter_offset |
| 640 | * is in fact worse than simply using last received one: with large poll intervals |
| 641 | * (>= 2048) averaging code uses offset values which are outdated by hours, |
| 642 | * and time/frequency correction goes totally wrong when fed essentially bogus offsets. |
| 643 | */ |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 644 | fdp = p->filter_datapoint; |
| 645 | idx = p->datapoint_idx; /* most recent datapoint's index */ |
| 646 | |
| 647 | /* filter_offset: simply use the most recent value */ |
| 648 | p->filter_offset = fdp[idx].d_offset; |
| 649 | |
| 650 | /* n-1 |
| 651 | * --- dispersion(i) |
| 652 | * filter_dispersion = \ ------------- |
| 653 | * / (i+1) |
| 654 | * --- 2 |
| 655 | * i=0 |
| 656 | */ |
| 657 | wavg = 0; |
| 658 | sum = 0; |
| 659 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
| 660 | sum += dispersion(&fdp[idx]) / (2 << i); |
| 661 | wavg += fdp[idx].d_offset; |
| 662 | idx = (idx - 1) & (NUM_DATAPOINTS - 1); |
| 663 | } |
| 664 | wavg /= NUM_DATAPOINTS; |
| 665 | p->filter_dispersion = sum; |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 666 | |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 667 | /* +----- -----+ ^ 1/2 |
| 668 | * | n-1 | |
| 669 | * | --- | |
| 670 | * | 1 \ 2 | |
| 671 | * filter_jitter = | --- * / (avg-offset_j) | |
| 672 | * | n --- | |
| 673 | * | j=0 | |
| 674 | * +----- -----+ |
| 675 | * where n is the number of valid datapoints in the filter (n > 1); |
| 676 | * if filter_jitter < precision then filter_jitter = precision |
| 677 | */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 678 | sum = 0; |
| 679 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 680 | sum += SQUARE(wavg - fdp[i].d_offset); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 681 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 682 | sum = SQRT(sum / NUM_DATAPOINTS); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 683 | p->filter_jitter = sum > G_precision_sec ? sum : G_precision_sec; |
| 684 | |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 685 | VERB4 bb_error_msg("filter offset:%+f disp:%f jitter:%f", |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 686 | p->filter_offset, |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 687 | p->filter_dispersion, |
| 688 | p->filter_jitter); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static void |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 692 | reset_peer_stats(peer_t *p, double offset) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 693 | { |
| 694 | int i; |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 695 | bool small_ofs = fabs(offset) < STEP_THRESHOLD; |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 696 | |
Denys Vlasenko | 777be10 | 2013-12-07 17:29:03 +0100 | [diff] [blame] | 697 | /* Used to set p->filter_datapoint[i].d_dispersion = MAXDISP |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 698 | * and clear reachable bits, but this proved to be too aggressive: |
Denys Vlasenko | f37f281 | 2016-03-04 07:06:53 +0100 | [diff] [blame] | 699 | * after step (tested with suspending laptop for ~30 secs), |
Denys Vlasenko | 777be10 | 2013-12-07 17:29:03 +0100 | [diff] [blame] | 700 | * this caused all previous data to be considered invalid, |
Denys Vlasenko | 1bfc4b8 | 2017-01-19 14:42:34 +0100 | [diff] [blame] | 701 | * making us needing to collect full ~8 datapoints per peer |
Denys Vlasenko | 777be10 | 2013-12-07 17:29:03 +0100 | [diff] [blame] | 702 | * after step in order to start trusting them. |
| 703 | * In turn, this was making poll interval decrease even after |
| 704 | * step was done. (Poll interval decreases already before step |
| 705 | * in this scenario, because we see large offsets and end up with |
| 706 | * no good peer to select). |
| 707 | */ |
| 708 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 709 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 710 | if (small_ofs) { |
Denys Vlasenko | eff6d59 | 2010-06-24 20:23:40 +0200 | [diff] [blame] | 711 | p->filter_datapoint[i].d_recv_time += offset; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 712 | if (p->filter_datapoint[i].d_offset != 0) { |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 713 | p->filter_datapoint[i].d_offset -= offset; |
| 714 | //bb_error_msg("p->filter_datapoint[%d].d_offset %f -> %f", |
| 715 | // i, |
| 716 | // p->filter_datapoint[i].d_offset + offset, |
| 717 | // p->filter_datapoint[i].d_offset); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 718 | } |
| 719 | } else { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 720 | p->filter_datapoint[i].d_recv_time = G.cur_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 721 | p->filter_datapoint[i].d_offset = 0; |
Denys Vlasenko | 777be10 | 2013-12-07 17:29:03 +0100 | [diff] [blame] | 722 | /*p->filter_datapoint[i].d_dispersion = MAXDISP;*/ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 725 | if (small_ofs) { |
Denys Vlasenko | eff6d59 | 2010-06-24 20:23:40 +0200 | [diff] [blame] | 726 | p->lastpkt_recv_time += offset; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 727 | } else { |
Denys Vlasenko | 777be10 | 2013-12-07 17:29:03 +0100 | [diff] [blame] | 728 | /*p->reachable_bits = 0;*/ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 729 | p->lastpkt_recv_time = G.cur_time; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 730 | } |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 731 | filter_datapoints(p); /* recalc p->filter_xxx */ |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 732 | VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 735 | #if ENABLE_FEATURE_NTPD_SERVER |
| 736 | static uint32_t calculate_refid(len_and_sockaddr *lsa) |
| 737 | { |
| 738 | # if ENABLE_FEATURE_IPV6 |
| 739 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 740 | md5_ctx_t md5; |
| 741 | uint32_t res[MD5_OUTSIZE / 4]; |
| 742 | |
| 743 | md5_begin(&md5); |
| 744 | md5_hash(&md5, &lsa->u.sin6.sin6_addr, sizeof(lsa->u.sin6.sin6_addr)); |
| 745 | md5_end(&md5, res); |
| 746 | return res[0]; |
| 747 | } |
| 748 | # endif |
| 749 | return lsa->u.sin.sin_addr.s_addr; |
| 750 | } |
| 751 | #endif |
| 752 | |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 753 | static len_and_sockaddr* |
| 754 | resolve_peer_hostname(peer_t *p) |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 755 | { |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 756 | len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123); |
| 757 | if (lsa) { |
| 758 | free(p->p_lsa); |
| 759 | free(p->p_dotted); |
| 760 | p->p_lsa = lsa; |
| 761 | p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); |
Denys Vlasenko | 1bfc4b8 | 2017-01-19 14:42:34 +0100 | [diff] [blame] | 762 | VERB1 if (strcmp(p->p_hostname, p->p_dotted) != 0) |
| 763 | bb_error_msg("'%s' is %s", p->p_hostname, p->p_dotted); |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 764 | #if ENABLE_FEATURE_NTPD_SERVER |
| 765 | p->p_refid = calculate_refid(p->p_lsa); |
| 766 | #endif |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 767 | p->dns_errors = 0; |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 768 | return lsa; |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 769 | } |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 770 | p->dns_errors = ((p->dns_errors << 1) | 1) & DNS_ERRORS_CAP; |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 771 | return lsa; |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 774 | #if !ENABLE_FEATURE_NTP_AUTH |
| 775 | #define add_peers(s, key_entry) \ |
| 776 | add_peers(s) |
| 777 | #endif |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 778 | static void |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 779 | add_peers(const char *s, key_entry_t *key_entry) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 780 | { |
Denys Vlasenko | f37f281 | 2016-03-04 07:06:53 +0100 | [diff] [blame] | 781 | llist_t *item; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 782 | peer_t *p; |
| 783 | |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 784 | p = xzalloc(sizeof(*p) + strlen(s)); |
| 785 | strcpy(p->p_hostname, s); |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 786 | p->p_fd = -1; |
| 787 | p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); |
| 788 | p->next_action_time = G.cur_time; /* = set_next(p, 0); */ |
| 789 | reset_peer_stats(p, STEP_THRESHOLD); |
Denys Vlasenko | f37f281 | 2016-03-04 07:06:53 +0100 | [diff] [blame] | 790 | |
| 791 | /* Names like N.<country2chars>.pool.ntp.org are randomly resolved |
| 792 | * to a pool of machines. Sometimes different N's resolve to the same IP. |
| 793 | * It is not useful to have two peers with same IP. We skip duplicates. |
| 794 | */ |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 795 | if (resolve_peer_hostname(p)) { |
| 796 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 797 | peer_t *pp = (peer_t *) item->data; |
| 798 | if (pp->p_dotted && strcmp(p->p_dotted, pp->p_dotted) == 0) { |
| 799 | bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted); |
| 800 | free(p->p_lsa); |
| 801 | free(p->p_dotted); |
| 802 | free(p); |
| 803 | return; |
| 804 | } |
Denys Vlasenko | f37f281 | 2016-03-04 07:06:53 +0100 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 808 | IF_FEATURE_NTP_AUTH(p->key_entry = key_entry;) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 809 | llist_add_to(&G.ntp_peers, p); |
| 810 | G.peer_cnt++; |
| 811 | } |
| 812 | |
| 813 | static int |
| 814 | do_sendto(int fd, |
| 815 | const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen, |
| 816 | msg_t *msg, ssize_t len) |
| 817 | { |
| 818 | ssize_t ret; |
| 819 | |
| 820 | errno = 0; |
| 821 | if (!from) { |
| 822 | ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen); |
| 823 | } else { |
| 824 | ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen); |
| 825 | } |
| 826 | if (ret != len) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 827 | bb_simple_perror_msg("send failed"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 828 | return -1; |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 833 | #if ENABLE_FEATURE_NTP_AUTH |
| 834 | static void |
| 835 | hash(key_entry_t *key_entry, const msg_t *msg, uint8_t *output) |
| 836 | { |
| 837 | union { |
| 838 | md5_ctx_t m; |
| 839 | sha1_ctx_t s; |
| 840 | } ctx; |
| 841 | unsigned hash_size = sizeof(*msg) - sizeof(msg->m_keyid) - sizeof(msg->m_digest); |
| 842 | |
| 843 | switch (key_entry->type) { |
| 844 | case HASH_MD5: |
| 845 | md5_begin(&ctx.m); |
| 846 | md5_hash(&ctx.m, key_entry->key, key_entry->key_length); |
| 847 | md5_hash(&ctx.m, msg, hash_size); |
| 848 | md5_end(&ctx.m, output); |
| 849 | break; |
| 850 | default: /* it's HASH_SHA1 */ |
| 851 | sha1_begin(&ctx.s); |
| 852 | sha1_hash(&ctx.s, key_entry->key, key_entry->key_length); |
| 853 | sha1_hash(&ctx.s, msg, hash_size); |
| 854 | sha1_end(&ctx.s, output); |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | static void |
| 860 | hash_peer(peer_t *p) |
| 861 | { |
| 862 | p->p_xmt_msg.m_keyid = htonl(p->key_entry->id); |
| 863 | hash(p->key_entry, &p->p_xmt_msg, p->p_xmt_msg.m_digest); |
| 864 | } |
| 865 | |
| 866 | static int |
| 867 | hashes_differ(peer_t *p, const msg_t *msg) |
| 868 | { |
| 869 | uint8_t digest[NTP_SHA1_DIGESTSIZE]; |
| 870 | hash(p->key_entry, msg, digest); |
| 871 | return memcmp(digest, msg->m_digest, p->key_entry->msg_size - NTP_MSGSIZE_NOAUTH - KEYID_SIZE); |
| 872 | } |
| 873 | #endif |
| 874 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 875 | static void |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 876 | send_query_to_peer(peer_t *p) |
| 877 | { |
Denys Vlasenko | a1e9bc6 | 2017-10-28 17:54:24 +0200 | [diff] [blame] | 878 | if (!p->p_lsa) |
| 879 | return; |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 880 | |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 881 | /* Why do we need to bind()? |
| 882 | * See what happens when we don't bind: |
| 883 | * |
| 884 | * socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3 |
| 885 | * setsockopt(3, SOL_IP, IP_TOS, [16], 4) = 0 |
| 886 | * gettimeofday({1259071266, 327885}, NULL) = 0 |
| 887 | * sendto(3, "xxx", 48, MSG_DONTWAIT, {sa_family=AF_INET, sin_port=htons(123), sin_addr=inet_addr("10.34.32.125")}, 16) = 48 |
| 888 | * ^^^ we sent it from some source port picked by kernel. |
| 889 | * time(NULL) = 1259071266 |
| 890 | * write(2, "ntpd: entering poll 15 secs\n", 28) = 28 |
| 891 | * poll([{fd=3, events=POLLIN}], 1, 15000) = 1 ([{fd=3, revents=POLLIN}]) |
| 892 | * recv(3, "yyy", 68, MSG_DONTWAIT) = 48 |
| 893 | * ^^^ this recv will receive packets to any local port! |
| 894 | * |
| 895 | * Uncomment this and use strace to see it in action: |
| 896 | */ |
| 897 | #define PROBE_LOCAL_ADDR /* { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); } */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 898 | |
| 899 | if (p->p_fd == -1) { |
| 900 | int fd, family; |
| 901 | len_and_sockaddr *local_lsa; |
| 902 | |
| 903 | family = p->p_lsa->u.sa.sa_family; |
| 904 | p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM); |
| 905 | /* local_lsa has "null" address and port 0 now. |
| 906 | * bind() ensures we have a *particular port* selected by kernel |
| 907 | * and remembered in p->p_fd, thus later recv(p->p_fd) |
| 908 | * receives only packets sent to this port. |
| 909 | */ |
| 910 | PROBE_LOCAL_ADDR |
| 911 | xbind(fd, &local_lsa->u.sa, local_lsa->len); |
| 912 | PROBE_LOCAL_ADDR |
| 913 | #if ENABLE_FEATURE_IPV6 |
| 914 | if (family == AF_INET) |
| 915 | #endif |
Codarren Velvindron | 7c43d43 | 2018-04-15 20:37:50 +0400 | [diff] [blame] | 916 | setsockopt_int(fd, IPPROTO_IP, IP_TOS, IPTOS_DSCP_AF21); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 917 | free(local_lsa); |
| 918 | } |
| 919 | |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 920 | /* Emit message _before_ attempted send. Think of a very short |
| 921 | * roundtrip networks: we need to go back to recv loop ASAP, |
| 922 | * to reduce delay. Printing messages after send works against that. |
| 923 | */ |
| 924 | VERB1 bb_error_msg("sending query to %s", p->p_dotted); |
| 925 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 926 | /* |
| 927 | * Send out a random 64-bit number as our transmit time. The NTP |
| 928 | * server will copy said number into the originate field on the |
| 929 | * response that it sends us. This is totally legal per the SNTP spec. |
| 930 | * |
| 931 | * The impact of this is two fold: we no longer send out the current |
| 932 | * system time for the world to see (which may aid an attacker), and |
| 933 | * it gives us a (not very secure) way of knowing that we're not |
| 934 | * getting spoofed by an attacker that can't capture our traffic |
| 935 | * but can spoof packets from the NTP server we're communicating with. |
| 936 | * |
| 937 | * Save the real transmit timestamp locally. |
| 938 | */ |
Denys Vlasenko | 0ed5f7a | 2014-03-05 18:58:15 +0100 | [diff] [blame] | 939 | p->p_xmt_msg.m_xmttime.int_partl = rand(); |
| 940 | p->p_xmt_msg.m_xmttime.fractionl = rand(); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 941 | p->p_xmttime = gettime1900d(); |
| 942 | |
Denys Vlasenko | 5a7e337 | 2013-05-23 16:06:59 +0200 | [diff] [blame] | 943 | /* Were doing it only if sendto worked, but |
Denys Vlasenko | 5ffdd1d | 2013-05-22 18:16:34 +0200 | [diff] [blame] | 944 | * loss of sync detection needs reachable_bits updated |
| 945 | * even if sending fails *locally*: |
| 946 | * "network is unreachable" because cable was pulled? |
| 947 | * We still need to declare "unsync" if this condition persists. |
| 948 | */ |
| 949 | p->reachable_bits <<= 1; |
| 950 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 951 | #if ENABLE_FEATURE_NTP_AUTH |
| 952 | if (p->key_entry) |
| 953 | hash_peer(p); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 954 | if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len, |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 955 | &p->p_xmt_msg, !p->key_entry ? NTP_MSGSIZE_NOAUTH : p->key_entry->msg_size) == -1 |
| 956 | ) |
| 957 | #else |
| 958 | if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len, |
| 959 | &p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1 |
| 960 | ) |
| 961 | #endif |
| 962 | { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 963 | close(p->p_fd); |
| 964 | p->p_fd = -1; |
Denys Vlasenko | 5a7e337 | 2013-05-23 16:06:59 +0200 | [diff] [blame] | 965 | /* |
| 966 | * We know that we sent nothing. |
| 967 | * We can retry *soon* without fearing |
| 968 | * that we are flooding the peer. |
| 969 | */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 970 | set_next(p, RETRY_INTERVAL); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 971 | return; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 974 | set_next(p, RESPONSE_INTERVAL); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 977 | /* Note that there is no provision to prevent several run_scripts |
Denys Vlasenko | 5a7e337 | 2013-05-23 16:06:59 +0200 | [diff] [blame] | 978 | * to be started in quick succession. In fact, it happens rather often |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 979 | * if initial syncronization results in a step. |
| 980 | * You will see "step" and then "stratum" script runs, sometimes |
| 981 | * as close as only 0.002 seconds apart. |
| 982 | * Script should be ready to deal with this. |
| 983 | */ |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 984 | static void run_script(const char *action, double offset) |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 985 | { |
| 986 | char *argv[3]; |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 987 | char *env1, *env2, *env3, *env4; |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 988 | |
Denys Vlasenko | 07c5987 | 2013-05-22 18:18:51 +0200 | [diff] [blame] | 989 | G.last_script_run = G.cur_time; |
| 990 | |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 991 | if (!G.script_name) |
| 992 | return; |
| 993 | |
| 994 | argv[0] = (char*) G.script_name; |
| 995 | argv[1] = (char*) action; |
| 996 | argv[2] = NULL; |
| 997 | |
| 998 | VERB1 bb_error_msg("executing '%s %s'", G.script_name, action); |
| 999 | |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1000 | env1 = xasprintf("%s=%u", "stratum", G.stratum); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1001 | putenv(env1); |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1002 | env2 = xasprintf("%s=%ld", "freq_drift_ppm", G.kernel_freq_drift); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1003 | putenv(env2); |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1004 | env3 = xasprintf("%s=%u", "poll_interval", 1 << G.poll_exp); |
| 1005 | putenv(env3); |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1006 | env4 = xasprintf("%s=%f", "offset", offset); |
| 1007 | putenv(env4); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1008 | /* Other items of potential interest: selected peer, |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1009 | * rootdelay, reftime, rootdisp, refid, ntp_status, |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1010 | * last_update_offset, last_update_recv_time, discipline_jitter, |
| 1011 | * how many peers have reachable_bits = 0? |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1012 | */ |
| 1013 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 1014 | /* Don't want to wait: it may run hwclock --systohc, and that |
| 1015 | * may take some time (seconds): */ |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 1016 | /*spawn_and_wait(argv);*/ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 1017 | spawn(argv); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1018 | |
| 1019 | unsetenv("stratum"); |
| 1020 | unsetenv("freq_drift_ppm"); |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1021 | unsetenv("poll_interval"); |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1022 | unsetenv("offset"); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1023 | free(env1); |
| 1024 | free(env2); |
Denys Vlasenko | ae47335 | 2010-01-07 11:51:13 +0100 | [diff] [blame] | 1025 | free(env3); |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1026 | free(env4); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1029 | static NOINLINE void |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1030 | step_time(double offset) |
| 1031 | { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1032 | llist_t *item; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1033 | double dtime; |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 1034 | struct timeval tvc, tvn; |
| 1035 | char buf[sizeof("yyyy-mm-dd hh:mm:ss") + /*paranoia:*/ 4]; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1036 | time_t tval; |
| 1037 | |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 1038 | xgettimeofday(&tvc); |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 1039 | dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset; |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 1040 | d_to_tv(&tvn, dtime); |
Denys Vlasenko | eb0c2e2 | 2020-12-16 21:36:36 +0100 | [diff] [blame] | 1041 | xsettimeofday(&tvn); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1042 | |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 1043 | VERB2 { |
| 1044 | tval = tvc.tv_sec; |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 1045 | strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); |
Denys Vlasenko | fc4ebd0 | 2012-02-28 02:45:00 +0100 | [diff] [blame] | 1046 | bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec); |
| 1047 | } |
| 1048 | tval = tvn.tv_sec; |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 1049 | strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1050 | bb_info_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 1051 | //maybe? G.FREQHOLD_cnt = 0; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1052 | |
| 1053 | /* Correct various fields which contain time-relative values: */ |
| 1054 | |
Denys Vlasenko | 4125a6b | 2012-06-11 11:41:46 +0200 | [diff] [blame] | 1055 | /* Globals: */ |
| 1056 | G.cur_time += offset; |
| 1057 | G.last_update_recv_time += offset; |
| 1058 | G.last_script_run += offset; |
| 1059 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1060 | /* p->lastpkt_recv_time, p->next_action_time and such: */ |
| 1061 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 1062 | peer_t *pp = (peer_t *) item->data; |
| 1063 | reset_peer_stats(pp, offset); |
Denys Vlasenko | 16c52a5 | 2012-02-23 14:28:47 +0100 | [diff] [blame] | 1064 | //bb_error_msg("offset:%+f pp->next_action_time:%f -> %f", |
Denys Vlasenko | eff6d59 | 2010-06-24 20:23:40 +0200 | [diff] [blame] | 1065 | // offset, pp->next_action_time, pp->next_action_time + offset); |
| 1066 | pp->next_action_time += offset; |
Denys Vlasenko | 4125a6b | 2012-06-11 11:41:46 +0200 | [diff] [blame] | 1067 | if (pp->p_fd >= 0) { |
| 1068 | /* We wait for reply from this peer too. |
| 1069 | * But due to step we are doing, reply's data is no longer |
| 1070 | * useful (in fact, it'll be bogus). Stop waiting for it. |
| 1071 | */ |
| 1072 | close(pp->p_fd); |
| 1073 | pp->p_fd = -1; |
| 1074 | set_next(pp, RETRY_INTERVAL); |
| 1075 | } |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1076 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1079 | static void clamp_pollexp_and_set_MAXSTRAT(void) |
| 1080 | { |
| 1081 | if (G.poll_exp < MINPOLL) |
| 1082 | G.poll_exp = MINPOLL; |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1083 | if (G.poll_exp > BIGPOLL) |
| 1084 | G.poll_exp = BIGPOLL; |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1085 | G.polladj_count = 0; |
| 1086 | G.stratum = MAXSTRAT; |
| 1087 | } |
| 1088 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * Selection and clustering, and their helpers |
| 1092 | */ |
| 1093 | typedef struct { |
| 1094 | peer_t *p; |
| 1095 | int type; |
| 1096 | double edge; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1097 | double opt_rd; /* optimization */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1098 | } point_t; |
| 1099 | static int |
| 1100 | compare_point_edge(const void *aa, const void *bb) |
| 1101 | { |
| 1102 | const point_t *a = aa; |
| 1103 | const point_t *b = bb; |
| 1104 | if (a->edge < b->edge) { |
| 1105 | return -1; |
| 1106 | } |
| 1107 | return (a->edge > b->edge); |
| 1108 | } |
| 1109 | typedef struct { |
| 1110 | peer_t *p; |
| 1111 | double metric; |
| 1112 | } survivor_t; |
| 1113 | static int |
| 1114 | compare_survivor_metric(const void *aa, const void *bb) |
| 1115 | { |
| 1116 | const survivor_t *a = aa; |
| 1117 | const survivor_t *b = bb; |
Denys Vlasenko | 510f56a | 2010-01-03 12:00:26 +0100 | [diff] [blame] | 1118 | if (a->metric < b->metric) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1119 | return -1; |
Denys Vlasenko | 510f56a | 2010-01-03 12:00:26 +0100 | [diff] [blame] | 1120 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1121 | return (a->metric > b->metric); |
| 1122 | } |
| 1123 | static int |
| 1124 | fit(peer_t *p, double rd) |
| 1125 | { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1126 | if ((p->reachable_bits & (p->reachable_bits-1)) == 0) { |
| 1127 | /* One or zero bits in reachable_bits */ |
Denys Vlasenko | 1303962 | 2018-09-13 12:15:36 +0200 | [diff] [blame] | 1128 | VERB4 bb_error_msg("peer %s unfit for selection: " |
| 1129 | "unreachable", p->p_dotted); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 1132 | #if 0 /* we filter out such packets earlier */ |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1133 | if ((p->lastpkt_status & LI_ALARM) == LI_ALARM |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1134 | || p->lastpkt_stratum >= MAXSTRAT |
| 1135 | ) { |
Denys Vlasenko | 1303962 | 2018-09-13 12:15:36 +0200 | [diff] [blame] | 1136 | VERB4 bb_error_msg("peer %s unfit for selection: " |
| 1137 | "bad status/stratum", p->p_dotted); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1140 | #endif |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1141 | /* rd is root_distance(p) */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1142 | if (rd > MAXDIST + FREQ_TOLERANCE * (1 << G.poll_exp)) { |
Denys Vlasenko | 1303962 | 2018-09-13 12:15:36 +0200 | [diff] [blame] | 1143 | VERB3 bb_error_msg("peer %s unfit for selection: " |
| 1144 | "root distance %f too high, jitter:%f", |
| 1145 | p->p_dotted, rd, p->filter_jitter |
| 1146 | ); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | //TODO |
| 1150 | // /* Do we have a loop? */ |
| 1151 | // if (p->refid == p->dstaddr || p->refid == s.refid) |
| 1152 | // return 0; |
Denys Vlasenko | b7c9fb2 | 2011-02-03 00:05:48 +0100 | [diff] [blame] | 1153 | return 1; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1154 | } |
Denys Vlasenko | 53b2fdc | 2021-10-10 13:50:53 +0200 | [diff] [blame] | 1155 | static NOINLINE peer_t* |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1156 | select_and_cluster(void) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1157 | { |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1158 | peer_t *p; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1159 | llist_t *item; |
| 1160 | int i, j; |
| 1161 | int size = 3 * G.peer_cnt; |
| 1162 | /* for selection algorithm */ |
| 1163 | point_t point[size]; |
| 1164 | unsigned num_points, num_candidates; |
| 1165 | double low, high; |
| 1166 | unsigned num_falsetickers; |
| 1167 | /* for cluster algorithm */ |
| 1168 | survivor_t survivor[size]; |
| 1169 | unsigned num_survivors; |
| 1170 | |
| 1171 | /* Selection */ |
| 1172 | |
| 1173 | num_points = 0; |
| 1174 | item = G.ntp_peers; |
Denys Vlasenko | ff3f3ac | 2015-01-29 16:31:36 +0100 | [diff] [blame] | 1175 | while (item != NULL) { |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1176 | double rd, offset; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1177 | |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1178 | p = (peer_t *) item->data; |
| 1179 | rd = root_distance(p); |
| 1180 | offset = p->filter_offset; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1181 | if (!fit(p, rd)) { |
| 1182 | item = item->link; |
| 1183 | continue; |
| 1184 | } |
| 1185 | |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1186 | VERB5 bb_error_msg("interval: [%f %f %f] %s", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1187 | offset - rd, |
| 1188 | offset, |
| 1189 | offset + rd, |
| 1190 | p->p_dotted |
| 1191 | ); |
| 1192 | point[num_points].p = p; |
| 1193 | point[num_points].type = -1; |
| 1194 | point[num_points].edge = offset - rd; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1195 | point[num_points].opt_rd = rd; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1196 | num_points++; |
| 1197 | point[num_points].p = p; |
| 1198 | point[num_points].type = 0; |
| 1199 | point[num_points].edge = offset; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1200 | point[num_points].opt_rd = rd; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1201 | num_points++; |
| 1202 | point[num_points].p = p; |
| 1203 | point[num_points].type = 1; |
| 1204 | point[num_points].edge = offset + rd; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1205 | point[num_points].opt_rd = rd; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1206 | num_points++; |
| 1207 | item = item->link; |
| 1208 | } |
| 1209 | num_candidates = num_points / 3; |
| 1210 | if (num_candidates == 0) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1211 | VERB3 bb_error_msg("no valid datapoints%s", ", no peer selected"); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1212 | return NULL; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1213 | } |
| 1214 | //TODO: sorting does not seem to be done in reference code |
| 1215 | qsort(point, num_points, sizeof(point[0]), compare_point_edge); |
| 1216 | |
| 1217 | /* Start with the assumption that there are no falsetickers. |
| 1218 | * Attempt to find a nonempty intersection interval containing |
| 1219 | * the midpoints of all truechimers. |
| 1220 | * If a nonempty interval cannot be found, increase the number |
| 1221 | * of assumed falsetickers by one and try again. |
| 1222 | * If a nonempty interval is found and the number of falsetickers |
| 1223 | * is less than the number of truechimers, a majority has been found |
| 1224 | * and the midpoint of each truechimer represents |
| 1225 | * the candidates available to the cluster algorithm. |
| 1226 | */ |
| 1227 | num_falsetickers = 0; |
| 1228 | while (1) { |
| 1229 | int c; |
| 1230 | unsigned num_midpoints = 0; |
| 1231 | |
| 1232 | low = 1 << 9; |
| 1233 | high = - (1 << 9); |
| 1234 | c = 0; |
| 1235 | for (i = 0; i < num_points; i++) { |
| 1236 | /* We want to do: |
| 1237 | * if (point[i].type == -1) c++; |
| 1238 | * if (point[i].type == 1) c--; |
| 1239 | * and it's simpler to do it this way: |
| 1240 | */ |
| 1241 | c -= point[i].type; |
| 1242 | if (c >= num_candidates - num_falsetickers) { |
| 1243 | /* If it was c++ and it got big enough... */ |
| 1244 | low = point[i].edge; |
| 1245 | break; |
| 1246 | } |
| 1247 | if (point[i].type == 0) |
| 1248 | num_midpoints++; |
| 1249 | } |
| 1250 | c = 0; |
| 1251 | for (i = num_points-1; i >= 0; i--) { |
| 1252 | c += point[i].type; |
| 1253 | if (c >= num_candidates - num_falsetickers) { |
| 1254 | high = point[i].edge; |
| 1255 | break; |
| 1256 | } |
| 1257 | if (point[i].type == 0) |
| 1258 | num_midpoints++; |
| 1259 | } |
| 1260 | /* If the number of midpoints is greater than the number |
| 1261 | * of allowed falsetickers, the intersection contains at |
| 1262 | * least one truechimer with no midpoint - bad. |
| 1263 | * Also, interval should be nonempty. |
| 1264 | */ |
| 1265 | if (num_midpoints <= num_falsetickers && low < high) |
| 1266 | break; |
| 1267 | num_falsetickers++; |
| 1268 | if (num_falsetickers * 2 >= num_candidates) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1269 | VERB3 bb_error_msg("falsetickers:%d, candidates:%d%s", |
| 1270 | num_falsetickers, num_candidates, |
| 1271 | ", no peer selected"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1272 | return NULL; |
| 1273 | } |
| 1274 | } |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1275 | VERB4 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1276 | low, high, num_candidates, num_falsetickers); |
| 1277 | |
| 1278 | /* Clustering */ |
| 1279 | |
| 1280 | /* Construct a list of survivors (p, metric) |
| 1281 | * from the chime list, where metric is dominated |
| 1282 | * first by stratum and then by root distance. |
| 1283 | * All other things being equal, this is the order of preference. |
| 1284 | */ |
| 1285 | num_survivors = 0; |
| 1286 | for (i = 0; i < num_points; i++) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1287 | if (point[i].edge < low || point[i].edge > high) |
| 1288 | continue; |
| 1289 | p = point[i].p; |
| 1290 | survivor[num_survivors].p = p; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1291 | /* x.opt_rd == root_distance(p); */ |
| 1292 | survivor[num_survivors].metric = MAXDIST * p->lastpkt_stratum + point[i].opt_rd; |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1293 | VERB5 bb_error_msg("survivor[%d] metric:%f peer:%s", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1294 | num_survivors, survivor[num_survivors].metric, p->p_dotted); |
| 1295 | num_survivors++; |
| 1296 | } |
| 1297 | /* There must be at least MIN_SELECTED survivors to satisfy the |
| 1298 | * correctness assertions. Ordinarily, the Byzantine criteria |
| 1299 | * require four survivors, but for the demonstration here, one |
| 1300 | * is acceptable. |
| 1301 | */ |
| 1302 | if (num_survivors < MIN_SELECTED) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1303 | VERB3 bb_error_msg("survivors:%d%s", |
| 1304 | num_survivors, |
| 1305 | ", no peer selected"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1306 | return NULL; |
| 1307 | } |
| 1308 | |
| 1309 | //looks like this is ONLY used by the fact that later we pick survivor[0]. |
| 1310 | //we can avoid sorting then, just find the minimum once! |
| 1311 | qsort(survivor, num_survivors, sizeof(survivor[0]), compare_survivor_metric); |
| 1312 | |
| 1313 | /* For each association p in turn, calculate the selection |
| 1314 | * jitter p->sjitter as the square root of the sum of squares |
| 1315 | * (p->offset - q->offset) over all q associations. The idea is |
| 1316 | * to repeatedly discard the survivor with maximum selection |
| 1317 | * jitter until a termination condition is met. |
| 1318 | */ |
| 1319 | while (1) { |
| 1320 | unsigned max_idx = max_idx; |
| 1321 | double max_selection_jitter = max_selection_jitter; |
| 1322 | double min_jitter = min_jitter; |
| 1323 | |
| 1324 | if (num_survivors <= MIN_CLUSTERED) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1325 | VERB4 bb_error_msg("num_survivors %d <= %d, not discarding more", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1326 | num_survivors, MIN_CLUSTERED); |
| 1327 | break; |
| 1328 | } |
| 1329 | |
| 1330 | /* To make sure a few survivors are left |
| 1331 | * for the clustering algorithm to chew on, |
| 1332 | * we stop if the number of survivors |
| 1333 | * is less than or equal to MIN_CLUSTERED (3). |
| 1334 | */ |
| 1335 | for (i = 0; i < num_survivors; i++) { |
| 1336 | double selection_jitter_sq; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1337 | |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1338 | p = survivor[i].p; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1339 | if (i == 0 || p->filter_jitter < min_jitter) |
| 1340 | min_jitter = p->filter_jitter; |
| 1341 | |
| 1342 | selection_jitter_sq = 0; |
| 1343 | for (j = 0; j < num_survivors; j++) { |
| 1344 | peer_t *q = survivor[j].p; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1345 | selection_jitter_sq += SQUARE(p->filter_offset - q->filter_offset); |
| 1346 | } |
| 1347 | if (i == 0 || selection_jitter_sq > max_selection_jitter) { |
| 1348 | max_selection_jitter = selection_jitter_sq; |
| 1349 | max_idx = i; |
| 1350 | } |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1351 | VERB6 bb_error_msg("survivor %d selection_jitter^2:%f", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1352 | i, selection_jitter_sq); |
| 1353 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1354 | max_selection_jitter = SQRT(max_selection_jitter / num_survivors); |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1355 | VERB5 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1356 | max_idx, max_selection_jitter, min_jitter); |
| 1357 | |
| 1358 | /* If the maximum selection jitter is less than the |
| 1359 | * minimum peer jitter, then tossing out more survivors |
| 1360 | * will not lower the minimum peer jitter, so we might |
| 1361 | * as well stop. |
| 1362 | */ |
| 1363 | if (max_selection_jitter < min_jitter) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1364 | VERB4 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1365 | max_selection_jitter, min_jitter, num_survivors); |
| 1366 | break; |
| 1367 | } |
| 1368 | |
| 1369 | /* Delete survivor[max_idx] from the list |
| 1370 | * and go around again. |
| 1371 | */ |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1372 | VERB6 bb_error_msg("dropping survivor %d", max_idx); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1373 | num_survivors--; |
| 1374 | while (max_idx < num_survivors) { |
| 1375 | survivor[max_idx] = survivor[max_idx + 1]; |
| 1376 | max_idx++; |
| 1377 | } |
| 1378 | } |
| 1379 | |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1380 | if (0) { |
| 1381 | /* Combine the offsets of the clustering algorithm survivors |
| 1382 | * using a weighted average with weight determined by the root |
| 1383 | * distance. Compute the selection jitter as the weighted RMS |
| 1384 | * difference between the first survivor and the remaining |
| 1385 | * survivors. In some cases the inherent clock jitter can be |
| 1386 | * reduced by not using this algorithm, especially when frequent |
| 1387 | * clockhopping is involved. bbox: thus we don't do it. |
| 1388 | */ |
| 1389 | double x, y, z, w; |
| 1390 | y = z = w = 0; |
| 1391 | for (i = 0; i < num_survivors; i++) { |
| 1392 | p = survivor[i].p; |
| 1393 | x = root_distance(p); |
| 1394 | y += 1 / x; |
| 1395 | z += p->filter_offset / x; |
| 1396 | w += SQUARE(p->filter_offset - survivor[0].p->filter_offset) / x; |
| 1397 | } |
| 1398 | //G.cluster_offset = z / y; |
| 1399 | //G.cluster_jitter = SQRT(w / y); |
| 1400 | } |
| 1401 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1402 | /* Pick the best clock. If the old system peer is on the list |
| 1403 | * and at the same stratum as the first survivor on the list, |
| 1404 | * then don't do a clock hop. Otherwise, select the first |
| 1405 | * survivor on the list as the new system peer. |
| 1406 | */ |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1407 | p = survivor[0].p; |
| 1408 | if (G.last_update_peer |
| 1409 | && G.last_update_peer->lastpkt_stratum <= p->lastpkt_stratum |
| 1410 | ) { |
| 1411 | /* Starting from 1 is ok here */ |
| 1412 | for (i = 1; i < num_survivors; i++) { |
| 1413 | if (G.last_update_peer == survivor[i].p) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1414 | VERB5 bb_simple_error_msg("keeping old synced peer"); |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1415 | p = G.last_update_peer; |
| 1416 | goto keep_old; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | G.last_update_peer = p; |
| 1421 | keep_old: |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1422 | VERB4 bb_error_msg("selected peer %s filter_offset:%+f age:%f", |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1423 | p->p_dotted, |
| 1424 | p->filter_offset, |
| 1425 | G.cur_time - p->lastpkt_recv_time |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1426 | ); |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1427 | return p; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | /* |
| 1432 | * Local clock discipline and its helpers |
| 1433 | */ |
| 1434 | static void |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1435 | set_new_values(double offset, double recv_time) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1436 | { |
| 1437 | /* Enter new state and set state variables. Note we use the time |
| 1438 | * of the last clock filter sample, which must be earlier than |
| 1439 | * the current time. |
| 1440 | */ |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1441 | VERB4 bb_error_msg("last update offset=%f recv_time=%f", |
| 1442 | offset, recv_time); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1443 | G.last_update_offset = offset; |
| 1444 | G.last_update_recv_time = recv_time; |
| 1445 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1446 | /* Return: -1: decrease poll interval, 0: leave as is, 1: increase */ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1447 | static NOINLINE int |
| 1448 | update_local_clock(peer_t *p) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1449 | { |
| 1450 | int rc; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1451 | struct timex tmx; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1452 | /* Note: can use G.cluster_offset instead: */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1453 | double offset = p->filter_offset; |
| 1454 | double recv_time = p->lastpkt_recv_time; |
| 1455 | double abs_offset; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1456 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1457 | double freq_drift; |
| 1458 | double since_last_update; |
Bartosz Golaszewski | 76ad748 | 2014-01-18 15:36:27 +0100 | [diff] [blame] | 1459 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1460 | double etemp, dtemp; |
| 1461 | |
| 1462 | abs_offset = fabs(offset); |
| 1463 | |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1464 | #if 0 |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 1465 | /* If needed, -S script can do it by looking at $offset |
| 1466 | * env var and killing parent */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1467 | /* If the offset is too large, give up and go home */ |
| 1468 | if (abs_offset > PANIC_THRESHOLD) { |
| 1469 | bb_error_msg_and_die("offset %f far too big, exiting", offset); |
| 1470 | } |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1471 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1472 | |
| 1473 | /* If this is an old update, for instance as the result |
| 1474 | * of a system peer change, avoid it. We never use |
| 1475 | * an old sample or the same sample twice. |
| 1476 | */ |
| 1477 | if (recv_time <= G.last_update_recv_time) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1478 | VERB3 bb_error_msg("update from %s: same or older datapoint, not using it", |
| 1479 | p->p_dotted); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1480 | return 0; /* "leave poll interval as is" */ |
| 1481 | } |
| 1482 | |
| 1483 | /* Clock state machine transition function. This is where the |
| 1484 | * action is and defines how the system reacts to large time |
| 1485 | * and frequency errors. |
| 1486 | */ |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1487 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | 423c4c2 | 2021-02-21 09:05:48 +0100 | [diff] [blame] | 1488 | since_last_update = recv_time - G.reftime; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1489 | freq_drift = 0; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1490 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1491 | |
| 1492 | /* There are two main regimes: when the |
| 1493 | * offset exceeds the step threshold and when it does not. |
| 1494 | */ |
| 1495 | if (abs_offset > STEP_THRESHOLD) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1496 | /* Step the time and clamp down the poll interval. |
| 1497 | * |
| 1498 | * In NSET state an initial frequency correction is |
| 1499 | * not available, usually because the frequency file has |
| 1500 | * not yet been written. Since the time is outside the |
| 1501 | * capture range, the clock is stepped. The frequency |
| 1502 | * will be set directly following the stepout interval. |
| 1503 | * |
| 1504 | * In FSET state the initial frequency has been set |
| 1505 | * from the frequency file. Since the time is outside |
| 1506 | * the capture range, the clock is stepped immediately, |
| 1507 | * rather than after the stepout interval. Guys get |
| 1508 | * nervous if it takes 17 minutes to set the clock for |
| 1509 | * the first time. |
| 1510 | * |
| 1511 | * In SPIK state the stepout threshold has expired and |
| 1512 | * the phase is still above the step threshold. Note |
| 1513 | * that a single spike greater than the step threshold |
| 1514 | * is always suppressed, even at the longer poll |
| 1515 | * intervals. |
| 1516 | */ |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1517 | VERB4 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1518 | step_time(offset); |
| 1519 | if (option_mask32 & OPT_q) { |
| 1520 | /* We were only asked to set time once. Done. */ |
| 1521 | exit(0); |
| 1522 | } |
| 1523 | |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1524 | clamp_pollexp_and_set_MAXSTRAT(); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1525 | |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1526 | run_script("step", offset); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1527 | |
Denys Vlasenko | cb76113 | 2014-01-08 17:17:52 +0100 | [diff] [blame] | 1528 | recv_time += offset; |
| 1529 | |
Denys Vlasenko | 547ee79 | 2012-03-05 10:18:00 +0100 | [diff] [blame] | 1530 | abs_offset = offset = 0; |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1531 | set_new_values(offset, recv_time); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1532 | } else { /* abs_offset <= STEP_THRESHOLD */ |
| 1533 | |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1534 | if (option_mask32 & OPT_q) { |
| 1535 | /* We were only asked to set time once. |
| 1536 | * The clock is precise enough, no need to step. |
| 1537 | */ |
| 1538 | exit(0); |
| 1539 | } |
| 1540 | |
Miroslav Lichvar | 760d035 | 2014-10-05 03:10:15 +0200 | [diff] [blame] | 1541 | /* The ratio is calculated before jitter is updated to make |
| 1542 | * poll adjust code more sensitive to large offsets. |
| 1543 | */ |
| 1544 | G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter; |
| 1545 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1546 | /* Compute the clock jitter as the RMS of exponentially |
| 1547 | * weighted offset differences. Used by the poll adjust code. |
| 1548 | */ |
| 1549 | etemp = SQUARE(G.discipline_jitter); |
Denys Vlasenko | 74584b8 | 2012-03-02 01:22:40 +0100 | [diff] [blame] | 1550 | dtemp = SQUARE(offset - G.last_update_offset); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1551 | G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG); |
Miroslav Lichvar | 760d035 | 2014-10-05 03:10:15 +0200 | [diff] [blame] | 1552 | if (G.discipline_jitter < G_precision_sec) |
| 1553 | G.discipline_jitter = G_precision_sec; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1554 | |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1555 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1556 | /* Compute freq_drift due to PLL and FLL contributions. |
| 1557 | * |
| 1558 | * The FLL and PLL frequency gain constants |
| 1559 | * depend on the poll interval and Allan |
| 1560 | * intercept. The FLL is not used below one-half |
| 1561 | * the Allan intercept. Above that the loop gain |
| 1562 | * increases in steps to 1 / AVG. |
| 1563 | */ |
| 1564 | if ((1 << G.poll_exp) > ALLAN / 2) { |
| 1565 | etemp = FLL - G.poll_exp; |
| 1566 | if (etemp < AVG) |
| 1567 | etemp = AVG; |
| 1568 | freq_drift += (offset - G.last_update_offset) / (MAXD(since_last_update, ALLAN) * etemp); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1569 | } |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1570 | /* For the PLL the integration interval |
| 1571 | * (numerator) is the minimum of the update |
| 1572 | * interval and poll interval. This allows |
| 1573 | * oversampling, but not undersampling. |
| 1574 | */ |
| 1575 | etemp = MIND(since_last_update, (1 << G.poll_exp)); |
| 1576 | dtemp = (4 * PLL) << G.poll_exp; |
| 1577 | freq_drift += offset * etemp / SQUARE(dtemp); |
| 1578 | #endif |
| 1579 | set_new_values(offset, recv_time); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1580 | if (G.stratum != p->lastpkt_stratum + 1) { |
| 1581 | G.stratum = p->lastpkt_stratum + 1; |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1582 | run_script("stratum", offset); |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 1583 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1586 | G.reftime = G.cur_time; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame] | 1587 | G.ntp_status = p->lastpkt_status; |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 1588 | #if ENABLE_FEATURE_NTPD_SERVER |
| 1589 | /* Our current refid is the IPv4 (or md5-hashed IPv6) address of the peer we took time from: */ |
| 1590 | G.refid = p->p_refid; |
| 1591 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1592 | G.rootdelay = p->lastpkt_rootdelay + p->lastpkt_delay; |
Denys Vlasenko | 9b20adc | 2010-01-17 02:51:33 +0100 | [diff] [blame] | 1593 | dtemp = p->filter_jitter; // SQRT(SQUARE(p->filter_jitter) + SQUARE(G.cluster_jitter)); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1594 | dtemp += MAXD(p->filter_dispersion + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time) + abs_offset, MINDISP); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1595 | G.rootdisp = p->lastpkt_rootdisp + dtemp; |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1596 | VERB4 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1597 | |
Denys Vlasenko | 2620d38 | 2021-02-21 09:13:05 +0100 | [diff] [blame] | 1598 | /* By this time, freq_drift and offset are set |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1599 | * to values suitable for adjtimex. |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1600 | */ |
| 1601 | #if !USING_KERNEL_PLL_LOOP |
| 1602 | /* Calculate the new frequency drift and frequency stability (wander). |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1603 | * Compute the clock wander as the RMS of exponentially weighted |
| 1604 | * frequency differences. This is not used directly, but can, |
| 1605 | * along with the jitter, be a highly useful monitoring and |
| 1606 | * debugging tool. |
| 1607 | */ |
| 1608 | dtemp = G.discipline_freq_drift + freq_drift; |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1609 | G.discipline_freq_drift = MAXD(MIND(MAXDRIFT, dtemp), -MAXDRIFT); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1610 | etemp = SQUARE(G.discipline_wander); |
| 1611 | dtemp = SQUARE(dtemp); |
| 1612 | G.discipline_wander = SQRT(etemp + (dtemp - etemp) / AVG); |
| 1613 | |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1614 | VERB4 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f", |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1615 | G.discipline_freq_drift, |
| 1616 | (long)(G.discipline_freq_drift * 65536e6), |
| 1617 | freq_drift, |
| 1618 | G.discipline_wander); |
| 1619 | #endif |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1620 | VERB4 { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1621 | memset(&tmx, 0, sizeof(tmx)); |
| 1622 | if (adjtimex(&tmx) < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1623 | bb_simple_perror_msg_and_die("adjtimex"); |
Denys Vlasenko | 8be49c3 | 2012-03-06 19:16:50 +0100 | [diff] [blame] | 1624 | bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld", |
| 1625 | tmx.freq, tmx.offset, tmx.status, tmx.constant); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1628 | memset(&tmx, 0, sizeof(tmx)); |
| 1629 | #if 0 |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1630 | //doesn't work, offset remains 0 (!) in kernel: |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1631 | //ntpd: set adjtimex freq:1786097 tmx.offset:77487 |
| 1632 | //ntpd: prev adjtimex freq:1786097 tmx.offset:0 |
| 1633 | //ntpd: cur adjtimex freq:1786097 tmx.offset:0 |
| 1634 | tmx.modes = ADJ_FREQUENCY | ADJ_OFFSET; |
| 1635 | /* 65536 is one ppm */ |
| 1636 | tmx.freq = G.discipline_freq_drift * 65536e6; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1637 | #endif |
| 1638 | tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR; |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 1639 | |
| 1640 | tmx.offset = (long)(offset * 1000000); /* usec */ |
| 1641 | if (SLEW_THRESHOLD < STEP_THRESHOLD) { |
| 1642 | if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) { |
| 1643 | tmx.offset = (long)(SLEW_THRESHOLD * 1000000); |
| 1644 | } |
| 1645 | if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) { |
| 1646 | tmx.offset = -(long)(SLEW_THRESHOLD * 1000000); |
| 1647 | } |
| 1648 | } |
| 1649 | |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 1650 | tmx.status = STA_PLL; |
| 1651 | if (G.FREQHOLD_cnt != 0) { |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 1652 | /* man adjtimex on STA_FREQHOLD: |
| 1653 | * "Normally adjustments made via ADJ_OFFSET result in dampened |
| 1654 | * frequency adjustments also being made. |
| 1655 | * This flag prevents the small frequency adjustment from being |
| 1656 | * made when correcting for an ADJ_OFFSET value." |
| 1657 | * |
| 1658 | * Use this flag for a few first adjustments at the beginning |
| 1659 | * of ntpd execution, otherwise even relatively small initial |
| 1660 | * offset tend to cause largish changes to in-kernel tmx.freq. |
| 1661 | * If ntpd was restarted due to e.g. switch to another network, |
| 1662 | * this destroys already well-established tmx.freq value. |
| 1663 | */ |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 1664 | if (G.FREQHOLD_cnt < 0) { |
| 1665 | /* Initialize it */ |
| 1666 | // Example: a laptop whose clock runs slower when hibernated, |
| 1667 | // after wake up it still has good tmx.freq, but accumulated ~0.5 sec offset: |
| 1668 | // Run with code where initial G.FREQHOLD_cnt was always 8: |
| 1669 | //15:17:52.947 no valid datapoints, no peer selected |
| 1670 | //15:17:56.515 update from:<IP> offset:+0.485133 delay:0.157762 jitter:0.209310 clock drift:-1.393ppm tc:4 |
| 1671 | //15:17:57.719 update from:<IP> offset:+0.483825 delay:0.158070 jitter:0.181159 clock drift:-1.393ppm tc:4 |
| 1672 | //15:17:59.925 update from:<IP> offset:+0.479504 delay:0.158147 jitter:0.156657 clock drift:-1.393ppm tc:4 |
| 1673 | //15:18:33.322 update from:<IP> offset:+0.428119 delay:0.158317 jitter:0.138071 clock drift:-1.393ppm tc:4 |
| 1674 | //15:19:06.718 update from:<IP> offset:+0.376932 delay:0.158276 jitter:0.122075 clock drift:-1.393ppm tc:4 |
| 1675 | //15:19:39.114 update from:<IP> offset:+0.327022 delay:0.158384 jitter:0.108538 clock drift:-1.393ppm tc:4 |
| 1676 | //15:20:12.715 update from:<IP> offset:+0.275596 delay:0.158297 jitter:0.097292 clock drift:-1.393ppm tc:4 |
| 1677 | //15:20:45.111 update from:<IP> offset:+0.225715 delay:0.158271 jitter:0.087841 clock drift:-1.393ppm tc:4 |
Denys Vlasenko | 029bd18 | 2018-07-02 16:17:07 +0200 | [diff] [blame] | 1678 | // If allowed to continue, it would start increasing tmx.freq now. |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 1679 | // Instead, it was ^Ced, and started anew: |
| 1680 | //15:21:15.043 no valid datapoints, no peer selected |
| 1681 | //15:21:17.408 update from:<IP> offset:+0.175910 delay:0.158314 jitter:0.076683 clock drift:-1.393ppm tc:4 |
| 1682 | //15:21:19.774 update from:<IP> offset:+0.171784 delay:0.158401 jitter:0.066436 clock drift:-1.393ppm tc:4 |
| 1683 | //15:21:22.140 update from:<IP> offset:+0.171660 delay:0.158592 jitter:0.057536 clock drift:-1.393ppm tc:4 |
| 1684 | //15:21:22.140 update from:<IP> offset:+0.167126 delay:0.158507 jitter:0.049792 clock drift:-1.393ppm tc:4 |
| 1685 | //15:21:55.696 update from:<IP> offset:+0.115223 delay:0.158277 jitter:0.050240 clock drift:-1.393ppm tc:4 |
| 1686 | //15:22:29.093 update from:<IP> offset:+0.068051 delay:0.158243 jitter:0.049405 clock drift:-1.393ppm tc:5 |
| 1687 | //15:23:02.490 update from:<IP> offset:+0.051632 delay:0.158215 jitter:0.043545 clock drift:-1.393ppm tc:5 |
| 1688 | //15:23:34.726 update from:<IP> offset:+0.039984 delay:0.158157 jitter:0.038106 clock drift:-1.393ppm tc:5 |
| 1689 | // STA_FREQHOLD no longer set, started increasing tmx.freq now: |
| 1690 | //15:24:06.961 update from:<IP> offset:+0.030968 delay:0.158190 jitter:0.033306 clock drift:+2.387ppm tc:5 |
| 1691 | //15:24:40.357 update from:<IP> offset:+0.023648 delay:0.158211 jitter:0.029072 clock drift:+5.454ppm tc:5 |
| 1692 | //15:25:13.774 update from:<IP> offset:+0.018068 delay:0.157660 jitter:0.025288 clock drift:+7.728ppm tc:5 |
| 1693 | //15:26:19.173 update from:<IP> offset:+0.010057 delay:0.157969 jitter:0.022255 clock drift:+8.361ppm tc:6 |
| 1694 | //15:27:26.602 update from:<IP> offset:+0.006737 delay:0.158103 jitter:0.019316 clock drift:+8.792ppm tc:6 |
| 1695 | //15:28:33.030 update from:<IP> offset:+0.004513 delay:0.158294 jitter:0.016765 clock drift:+9.080ppm tc:6 |
| 1696 | //15:29:40.617 update from:<IP> offset:+0.002787 delay:0.157745 jitter:0.014543 clock drift:+9.258ppm tc:6 |
| 1697 | //15:30:47.045 update from:<IP> offset:+0.001324 delay:0.157709 jitter:0.012594 clock drift:+9.342ppm tc:6 |
| 1698 | //15:31:53.473 update from:<IP> offset:+0.000007 delay:0.158142 jitter:0.010922 clock drift:+9.343ppm tc:6 |
| 1699 | //15:32:58.902 update from:<IP> offset:-0.000728 delay:0.158222 jitter:0.009454 clock drift:+9.298ppm tc:6 |
| 1700 | /* |
Denys Vlasenko | ea096d6 | 2019-10-30 12:13:46 +0100 | [diff] [blame] | 1701 | * This expression would choose MIN_FREQHOLD + 14 in the above example |
| 1702 | * (off_032 is +1 for each 0.032768 seconds of offset). |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 1703 | */ |
Denys Vlasenko | ea096d6 | 2019-10-30 12:13:46 +0100 | [diff] [blame] | 1704 | unsigned off_032 = abs((int)(tmx.offset >> 15)); |
| 1705 | G.FREQHOLD_cnt = 1 + MIN_FREQHOLD + off_032; |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 1706 | } |
| 1707 | G.FREQHOLD_cnt--; |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 1708 | tmx.status |= STA_FREQHOLD; |
| 1709 | } |
| 1710 | if (G.ntp_status & LI_PLUSSEC) |
| 1711 | tmx.status |= STA_INS; |
| 1712 | if (G.ntp_status & LI_MINUSSEC) |
| 1713 | tmx.status |= STA_DEL; |
| 1714 | |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 1715 | tmx.constant = (int)G.poll_exp - 4; |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1716 | /* EXPERIMENTAL. |
| 1717 | * The below if statement should be unnecessary, but... |
| 1718 | * It looks like Linux kernel's PLL is far too gentle in changing |
| 1719 | * tmx.freq in response to clock offset. Offset keeps growing |
| 1720 | * and eventually we fall back to smaller poll intervals. |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 1721 | * We can make correction more aggressive (about x2) by supplying |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1722 | * PLL time constant which is one less than the real one. |
| 1723 | * To be on a safe side, let's do it only if offset is significantly |
| 1724 | * larger than jitter. |
| 1725 | */ |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 1726 | if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE) |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1727 | tmx.constant--; |
Denys Vlasenko | 39dfb4d | 2018-03-10 21:25:53 +0100 | [diff] [blame] | 1728 | if (tmx.constant < 0) |
| 1729 | tmx.constant = 0; |
| 1730 | |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1731 | //tmx.esterror = (uint32_t)(clock_jitter * 1e6); |
| 1732 | //tmx.maxerror = (uint32_t)((sys_rootdelay / 2 + sys_rootdisp) * 1e6); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1733 | rc = adjtimex(&tmx); |
| 1734 | if (rc < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1735 | bb_simple_perror_msg_and_die("adjtimex"); |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 1736 | /* NB: here kernel returns constant == G.poll_exp, not == G.poll_exp - 4. |
| 1737 | * Not sure why. Perhaps it is normal. |
| 1738 | */ |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 1739 | VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x", |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1740 | rc, tmx.freq, tmx.offset, tmx.status); |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1741 | G.kernel_freq_drift = tmx.freq / 65536; |
Denys Vlasenko | 03718bb | 2016-02-24 01:22:45 +0100 | [diff] [blame] | 1742 | VERB2 bb_error_msg("update from:%s offset:%+f delay:%f jitter:%f clock drift:%+.3fppm tc:%d", |
| 1743 | p->p_dotted, |
| 1744 | offset, |
Denys Vlasenko | 9b1c8bf | 2018-08-03 11:03:55 +0200 | [diff] [blame] | 1745 | p->p_raw_delay, |
Denys Vlasenko | 03718bb | 2016-02-24 01:22:45 +0100 | [diff] [blame] | 1746 | G.discipline_jitter, |
| 1747 | (double)tmx.freq / 65536, |
| 1748 | (int)tmx.constant |
| 1749 | ); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1750 | |
| 1751 | return 1; /* "ok to increase poll interval" */ |
| 1752 | } |
| 1753 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1754 | /* |
| 1755 | * We've got a new reply packet from a peer, process it |
| 1756 | * (helpers first) |
| 1757 | */ |
| 1758 | static unsigned |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1759 | poll_interval(int upper_bound) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1760 | { |
Denys Vlasenko | 3e78f6f | 2014-02-09 15:35:04 +0100 | [diff] [blame] | 1761 | unsigned interval, r, mask; |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1762 | interval = 1 << G.poll_exp; |
| 1763 | if (interval > upper_bound) |
| 1764 | interval = upper_bound; |
Denys Vlasenko | 3e78f6f | 2014-02-09 15:35:04 +0100 | [diff] [blame] | 1765 | mask = ((interval-1) >> 4) | 1; |
Denys Vlasenko | 0ed5f7a | 2014-03-05 18:58:15 +0100 | [diff] [blame] | 1766 | r = rand(); |
Denys Vlasenko | 3e78f6f | 2014-02-09 15:35:04 +0100 | [diff] [blame] | 1767 | interval += r & mask; /* ~ random(0..1) * interval/16 */ |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 1768 | VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d)", interval, G.poll_exp); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1769 | return interval; |
| 1770 | } |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1771 | static void |
Miroslav Lichvar | 590a22c | 2014-09-18 16:19:05 +0200 | [diff] [blame] | 1772 | adjust_poll(int count) |
| 1773 | { |
| 1774 | G.polladj_count += count; |
| 1775 | if (G.polladj_count > POLLADJ_LIMIT) { |
| 1776 | G.polladj_count = 0; |
| 1777 | if (G.poll_exp < MAXPOLL) { |
| 1778 | G.poll_exp++; |
| 1779 | VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d", |
| 1780 | G.discipline_jitter, G.poll_exp); |
| 1781 | } |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1782 | } else if (G.polladj_count < -POLLADJ_LIMIT || (count < 0 && G.poll_exp > BIGPOLL)) { |
Miroslav Lichvar | 590a22c | 2014-09-18 16:19:05 +0200 | [diff] [blame] | 1783 | G.polladj_count = 0; |
| 1784 | if (G.poll_exp > MINPOLL) { |
| 1785 | llist_t *item; |
| 1786 | |
| 1787 | G.poll_exp--; |
| 1788 | /* Correct p->next_action_time in each peer |
| 1789 | * which waits for sending, so that they send earlier. |
| 1790 | * Old pp->next_action_time are on the order |
| 1791 | * of t + (1 << old_poll_exp) + small_random, |
| 1792 | * we simply need to subtract ~half of that. |
| 1793 | */ |
| 1794 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 1795 | peer_t *pp = (peer_t *) item->data; |
| 1796 | if (pp->p_fd < 0) |
| 1797 | pp->next_action_time -= (1 << G.poll_exp); |
| 1798 | } |
| 1799 | VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d", |
| 1800 | G.discipline_jitter, G.poll_exp); |
| 1801 | } |
| 1802 | } else { |
| 1803 | VERB4 bb_error_msg("polladj: count:%d", G.polladj_count); |
| 1804 | } |
| 1805 | } |
| 1806 | static NOINLINE void |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1807 | recv_and_process_peer_pkt(peer_t *p) |
| 1808 | { |
| 1809 | int rc; |
| 1810 | ssize_t size; |
| 1811 | msg_t msg; |
| 1812 | double T1, T2, T3, T4; |
Denys Vlasenko | d531f93 | 2014-04-19 19:00:16 +0200 | [diff] [blame] | 1813 | double offset; |
| 1814 | double prev_delay, delay; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1815 | unsigned interval; |
| 1816 | datapoint_t *datapoint; |
| 1817 | peer_t *q; |
| 1818 | |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 1819 | offset = 0; |
| 1820 | |
Denys Vlasenko | afb5d8b | 2020-09-29 14:19:17 +0200 | [diff] [blame] | 1821 | /* The below can happen as follows: |
| 1822 | * = we receive two peer rsponses at once. |
| 1823 | * = recv_and_process_peer_pkt(PEER1) -> update_local_clock() |
| 1824 | * -> step_time() and it closes all other fds, sets all ->fd to -1. |
| 1825 | * = recv_and_process_peer_pkt(PEER2) sees PEER2->fd == -1 |
| 1826 | */ |
| 1827 | if (p->p_fd < 0) |
| 1828 | return; |
| 1829 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1830 | /* We can recvfrom here and check from.IP, but some multihomed |
| 1831 | * ntp servers reply from their *other IP*. |
| 1832 | * TODO: maybe we should check at least what we can: from.port == 123? |
| 1833 | */ |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1834 | recv_again: |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1835 | size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT); |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1836 | if (size < 0) { |
| 1837 | if (errno == EINTR) |
| 1838 | /* Signal caught */ |
| 1839 | goto recv_again; |
| 1840 | if (errno == EAGAIN) |
| 1841 | /* There was no packet after all |
| 1842 | * (poll() returning POLLIN for a fd |
| 1843 | * is not a ironclad guarantee that data is there) |
| 1844 | */ |
| 1845 | return; |
| 1846 | /* |
| 1847 | * If you need a different handling for a specific |
| 1848 | * errno, always explain it in comment. |
| 1849 | */ |
| 1850 | bb_perror_msg_and_die("recv(%s) error", p->p_dotted); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1851 | } |
| 1852 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 1853 | #if ENABLE_FEATURE_NTP_AUTH |
| 1854 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH && size != NTP_MSGSIZE_SHA1_AUTH) { |
Denys Vlasenko | c0961e0 | 2019-07-02 13:10:19 +0200 | [diff] [blame] | 1855 | bb_error_msg("malformed packet received from %s: size %u", p->p_dotted, (int)size); |
Denys Vlasenko | 4125a6b | 2012-06-11 11:41:46 +0200 | [diff] [blame] | 1856 | return; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1857 | } |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 1858 | if (p->key_entry && hashes_differ(p, &msg)) { |
| 1859 | bb_error_msg("invalid cryptographic hash received from %s", p->p_dotted); |
| 1860 | return; |
| 1861 | } |
| 1862 | #else |
| 1863 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH) { |
Denys Vlasenko | c0961e0 | 2019-07-02 13:10:19 +0200 | [diff] [blame] | 1864 | bb_error_msg("malformed packet received from %s: size %u", p->p_dotted, (int)size); |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 1865 | return; |
| 1866 | } |
| 1867 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1868 | |
| 1869 | if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl |
| 1870 | || msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl |
| 1871 | ) { |
Denys Vlasenko | 4125a6b | 2012-06-11 11:41:46 +0200 | [diff] [blame] | 1872 | /* Somebody else's packet */ |
| 1873 | return; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
Denys Vlasenko | 4125a6b | 2012-06-11 11:41:46 +0200 | [diff] [blame] | 1876 | /* We do not expect any more packets from this peer for now. |
| 1877 | * Closing the socket informs kernel about it. |
| 1878 | * We open a new socket when we send a new query. |
| 1879 | */ |
| 1880 | close(p->p_fd); |
| 1881 | p->p_fd = -1; |
| 1882 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1883 | if ((msg.m_status & LI_ALARM) == LI_ALARM |
| 1884 | || msg.m_stratum == 0 |
| 1885 | || msg.m_stratum > NTP_MAXSTRATUM |
| 1886 | ) { |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 1887 | bb_error_msg("reply from %s: peer is unsynced", p->p_dotted); |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1888 | /* |
| 1889 | * Stratum 0 responses may have commands in 32-bit m_refid field: |
| 1890 | * "DENY", "RSTR" - peer does not like us at all, |
| 1891 | * "RATE" - peer is overloaded, reduce polling freq. |
| 1892 | * If poll interval is small, increase it. |
| 1893 | */ |
| 1894 | if (G.poll_exp < BIGPOLL) |
| 1895 | goto increase_interval; |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 1896 | goto pick_normal_interval; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1899 | // /* Verify valid root distance */ |
| 1900 | // if (msg.m_rootdelay / 2 + msg.m_rootdisp >= MAXDISP || p->lastpkt_reftime > msg.m_xmt) |
| 1901 | // return; /* invalid header values */ |
| 1902 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1903 | /* |
| 1904 | * From RFC 2030 (with a correction to the delay math): |
| 1905 | * |
| 1906 | * Timestamp Name ID When Generated |
| 1907 | * ------------------------------------------------------------ |
| 1908 | * Originate Timestamp T1 time request sent by client |
| 1909 | * Receive Timestamp T2 time request received by server |
| 1910 | * Transmit Timestamp T3 time reply sent by server |
| 1911 | * Destination Timestamp T4 time reply received by client |
| 1912 | * |
| 1913 | * The roundtrip delay and local clock offset are defined as |
| 1914 | * |
| 1915 | * delay = (T4 - T1) - (T3 - T2); offset = ((T2 - T1) + (T3 - T4)) / 2 |
| 1916 | */ |
| 1917 | T1 = p->p_xmttime; |
| 1918 | T2 = lfp_to_d(msg.m_rectime); |
| 1919 | T3 = lfp_to_d(msg.m_xmttime); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1920 | T4 = G.cur_time; |
Denys Vlasenko | 9b1c8bf | 2018-08-03 11:03:55 +0200 | [diff] [blame] | 1921 | delay = (T4 - T1) - (T3 - T2); |
| 1922 | |
| 1923 | /* |
| 1924 | * If this packet's delay is much bigger than the last one, |
| 1925 | * it's better to just ignore it than use its much less precise value. |
| 1926 | */ |
| 1927 | prev_delay = p->p_raw_delay; |
| 1928 | p->p_raw_delay = (delay < 0 ? 0.0 : delay); |
| 1929 | if (p->reachable_bits |
| 1930 | && delay > prev_delay * BAD_DELAY_GROWTH |
| 1931 | && delay > 1.0 / (8 * 1024) /* larger than ~0.000122 */ |
| 1932 | ) { |
| 1933 | bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, delay); |
| 1934 | goto pick_normal_interval; |
| 1935 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1936 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1937 | /* The delay calculation is a special case. In cases where the |
| 1938 | * server and client clocks are running at different rates and |
| 1939 | * with very fast networks, the delay can appear negative. In |
| 1940 | * order to avoid violating the Principle of Least Astonishment, |
| 1941 | * the delay is clamped not less than the system precision. |
| 1942 | */ |
Denys Vlasenko | d531f93 | 2014-04-19 19:00:16 +0200 | [diff] [blame] | 1943 | if (delay < G_precision_sec) |
| 1944 | delay = G_precision_sec; |
Denys Vlasenko | d531f93 | 2014-04-19 19:00:16 +0200 | [diff] [blame] | 1945 | p->lastpkt_delay = delay; |
| 1946 | p->lastpkt_recv_time = T4; |
| 1947 | VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time); |
| 1948 | p->lastpkt_status = msg.m_status; |
| 1949 | p->lastpkt_stratum = msg.m_stratum; |
| 1950 | p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay); |
| 1951 | p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp); |
| 1952 | p->lastpkt_refid = msg.m_refid; |
| 1953 | |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 1954 | p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0; |
| 1955 | datapoint = &p->filter_datapoint[p->datapoint_idx]; |
| 1956 | datapoint->d_recv_time = T4; |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 1957 | datapoint->d_offset = offset = ((T2 - T1) + (T3 - T4)) / 2; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1958 | datapoint->d_dispersion = LOG2D(msg.m_precision_exp) + G_precision_sec; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1959 | if (!p->reachable_bits) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1960 | /* 1st datapoint ever - replicate offset in every element */ |
| 1961 | int i; |
Denys Vlasenko | 132b044 | 2012-03-05 00:51:48 +0100 | [diff] [blame] | 1962 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 1963 | p->filter_datapoint[i].d_offset = offset; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1967 | p->reachable_bits |= 1; |
Denys Vlasenko | 074e8dc | 2010-01-04 23:58:13 +0100 | [diff] [blame] | 1968 | if ((MAX_VERBOSE && G.verbose) || (option_mask32 & OPT_w)) { |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1969 | bb_info_msg("reply from %s: offset:%+f delay:%f status:0x%02x strat:%d refid:0x%08x rootdelay:%f reach:0x%02x", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1970 | p->p_dotted, |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 1971 | offset, |
Denys Vlasenko | 9b1c8bf | 2018-08-03 11:03:55 +0200 | [diff] [blame] | 1972 | p->p_raw_delay, |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 1973 | p->lastpkt_status, |
| 1974 | p->lastpkt_stratum, |
| 1975 | p->lastpkt_refid, |
Denys Vlasenko | d98dc92 | 2012-03-08 03:27:49 +0100 | [diff] [blame] | 1976 | p->lastpkt_rootdelay, |
| 1977 | p->reachable_bits |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 1978 | /* not shown: m_ppoll, m_precision_exp, m_rootdisp, |
| 1979 | * m_reftime, m_orgtime, m_rectime, m_xmttime |
| 1980 | */ |
| 1981 | ); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* Muck with statictics and update the clock */ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 1985 | filter_datapoints(p); |
| 1986 | q = select_and_cluster(); |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1987 | rc = 0; |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 1988 | if (q) { |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 1989 | if (!(option_mask32 & OPT_w)) { |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 1990 | rc = update_local_clock(q); |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 1991 | #if 0 |
| 1992 | //Disabled this because there is a case where largish offsets |
| 1993 | //are unavoidable: if network round-trip delay is, say, ~0.6s, |
| 1994 | //error in offset estimation would be ~delay/2 ~= 0.3s. |
| 1995 | //Thus, offsets will be usually in -0.3...0.3s range. |
| 1996 | //In this case, this code would keep poll interval small, |
| 1997 | //but it won't be helping. |
| 1998 | //BIGOFF check below deals with a case of seeing multi-second offsets. |
| 1999 | |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 2000 | /* If drift is dangerously large, immediately |
| 2001 | * drop poll interval one step down. |
| 2002 | */ |
Denys Vlasenko | 5b9a910 | 2010-01-17 01:05:58 +0100 | [diff] [blame] | 2003 | if (fabs(q->filter_offset) >= POLLDOWN_OFFSET) { |
Denys Vlasenko | a14958c | 2013-12-04 16:32:09 +0100 | [diff] [blame] | 2004 | VERB4 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset); |
Miroslav Lichvar | 590a22c | 2014-09-18 16:19:05 +0200 | [diff] [blame] | 2005 | adjust_poll(-POLLADJ_LIMIT * 3); |
| 2006 | rc = 0; |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 2007 | } |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 2008 | #endif |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 2009 | } |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 2010 | } else { |
| 2011 | /* No peer selected. |
| 2012 | * If poll interval is small, increase it. |
| 2013 | */ |
| 2014 | if (G.poll_exp < BIGPOLL) |
| 2015 | goto increase_interval; |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 2016 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2017 | |
| 2018 | if (rc != 0) { |
| 2019 | /* Adjust the poll interval by comparing the current offset |
| 2020 | * with the clock jitter. If the offset is less than |
| 2021 | * the clock jitter times a constant, then the averaging interval |
| 2022 | * is increased, otherwise it is decreased. A bit of hysteresis |
| 2023 | * helps calm the dance. Works best using burst mode. |
| 2024 | */ |
Denys Vlasenko | 547ee79 | 2012-03-05 10:18:00 +0100 | [diff] [blame] | 2025 | if (rc > 0 && G.offset_to_jitter_ratio <= POLLADJ_GATE) { |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 2026 | /* was += G.poll_exp but it is a bit |
| 2027 | * too optimistic for my taste at high poll_exp's */ |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 2028 | increase_interval: |
Miroslav Lichvar | 590a22c | 2014-09-18 16:19:05 +0200 | [diff] [blame] | 2029 | adjust_poll(MINPOLL); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2030 | } else { |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 2031 | VERB3 if (rc > 0) |
Denys Vlasenko | 4c48a64 | 2016-03-03 22:01:23 +0100 | [diff] [blame] | 2032 | bb_error_msg("want smaller interval: offset/jitter = %u", |
| 2033 | G.offset_to_jitter_ratio); |
Miroslav Lichvar | 590a22c | 2014-09-18 16:19:05 +0200 | [diff] [blame] | 2034 | adjust_poll(-G.poll_exp * 2); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | /* Decide when to send new query for this peer */ |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 2039 | pick_normal_interval: |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 2040 | interval = poll_interval(INT_MAX); |
| 2041 | if (fabs(offset) >= BIGOFF && interval > BIGOFF_INTERVAL) { |
Denys Vlasenko | fc47fce | 2016-02-10 06:55:07 +0100 | [diff] [blame] | 2042 | /* If we are synced, offsets are less than SLEW_THRESHOLD, |
Denys Vlasenko | 0b3a38b | 2013-12-08 16:11:04 +0100 | [diff] [blame] | 2043 | * or at the very least not much larger than it. |
| 2044 | * Now we see a largish one. |
| 2045 | * Either this peer is feeling bad, or packet got corrupted, |
| 2046 | * or _our_ clock is wrong now and _all_ peers will show similar |
| 2047 | * largish offsets too. |
| 2048 | * I observed this with laptop suspend stopping clock. |
| 2049 | * In any case, it makes sense to make next request soonish: |
| 2050 | * cases 1 and 2: get a better datapoint, |
| 2051 | * case 3: allows to resync faster. |
| 2052 | */ |
| 2053 | interval = BIGOFF_INTERVAL; |
| 2054 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2055 | |
Denys Vlasenko | 4168fdd | 2010-01-04 00:19:13 +0100 | [diff] [blame] | 2056 | set_next(p, interval); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | #if ENABLE_FEATURE_NTPD_SERVER |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2060 | static NOINLINE void |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2061 | recv_and_process_client_pkt(void /*int fd*/) |
| 2062 | { |
| 2063 | ssize_t size; |
Cristian Ionescu-Idbohrn | 662972a | 2011-05-16 03:53:00 +0200 | [diff] [blame] | 2064 | //uint8_t version; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2065 | len_and_sockaddr *to; |
| 2066 | struct sockaddr *from; |
| 2067 | msg_t msg; |
| 2068 | uint8_t query_status; |
| 2069 | l_fixedpt_t query_xmttime; |
| 2070 | |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 2071 | to = get_sock_lsa(G_listen_fd); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2072 | from = xzalloc(to->len); |
| 2073 | |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 2074 | size = recv_from_to(G_listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len); |
Denys Vlasenko | b694608 | 2019-07-02 15:03:47 +0200 | [diff] [blame] | 2075 | |
| 2076 | /* "ntpq -p" (4.2.8p13) sends a 12-byte NTPv2 request: |
| 2077 | * m_status is 0x16: leap:0 version:2 mode:6(reserved1) |
| 2078 | * https://docs.ntpsec.org/latest/mode6.html |
| 2079 | * We don't support this. |
| 2080 | */ |
| 2081 | |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 2082 | # if ENABLE_FEATURE_NTP_AUTH |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2083 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH && size != NTP_MSGSIZE_SHA1_AUTH) |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 2084 | # else |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2085 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH) |
Denys Vlasenko | 9a2d899 | 2020-07-20 00:04:33 +0200 | [diff] [blame] | 2086 | # endif |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2087 | { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2088 | char *addr; |
| 2089 | if (size < 0) { |
| 2090 | if (errno == EAGAIN) |
| 2091 | goto bail; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2092 | bb_simple_perror_msg_and_die("recv"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2093 | } |
| 2094 | addr = xmalloc_sockaddr2dotted_noport(from); |
| 2095 | bb_error_msg("malformed packet received from %s: size %u", addr, (int)size); |
| 2096 | free(addr); |
| 2097 | goto bail; |
| 2098 | } |
| 2099 | |
Miroslav Lichvar | 150dc7a | 2016-08-01 20:24:24 +0200 | [diff] [blame] | 2100 | /* Respond only to client and symmetric active packets */ |
| 2101 | if ((msg.m_status & MODE_MASK) != MODE_CLIENT |
| 2102 | && (msg.m_status & MODE_MASK) != MODE_SYM_ACT |
| 2103 | ) { |
| 2104 | goto bail; |
| 2105 | } |
| 2106 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2107 | query_status = msg.m_status; |
| 2108 | query_xmttime = msg.m_xmttime; |
| 2109 | |
| 2110 | /* Build a reply packet */ |
| 2111 | memset(&msg, 0, sizeof(msg)); |
Paul Marks | b7841cf | 2013-01-14 02:39:10 +0100 | [diff] [blame] | 2112 | msg.m_status = G.stratum < MAXSTRAT ? (G.ntp_status & LI_MASK) : LI_ALARM; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2113 | msg.m_status |= (query_status & VERSION_MASK); |
| 2114 | msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ? |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 2115 | MODE_SERVER : MODE_SYM_PAS; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2116 | msg.m_stratum = G.stratum; |
| 2117 | msg.m_ppoll = G.poll_exp; |
| 2118 | msg.m_precision_exp = G_precision_exp; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2119 | /* this time was obtained between poll() and recv() */ |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 2120 | d_to_lfp(&msg.m_rectime, G.cur_time); |
| 2121 | d_to_lfp(&msg.m_xmttime, gettime1900d()); /* this instant */ |
Denys Vlasenko | d678257 | 2010-10-04 01:20:44 +0200 | [diff] [blame] | 2122 | if (G.peer_cnt == 0) { |
| 2123 | /* we have no peers: "stratum 1 server" mode. reftime = our own time */ |
| 2124 | G.reftime = G.cur_time; |
| 2125 | } |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 2126 | d_to_lfp(&msg.m_reftime, G.reftime); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2127 | msg.m_orgtime = query_xmttime; |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 2128 | d_to_sfp(&msg.m_rootdelay, G.rootdelay); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2129 | //simple code does not do this, fix simple code! |
Denys Vlasenko | 1195782 | 2021-03-26 12:02:08 +0100 | [diff] [blame] | 2130 | d_to_sfp(&msg.m_rootdisp, G.rootdisp); |
Cristian Ionescu-Idbohrn | 662972a | 2011-05-16 03:53:00 +0200 | [diff] [blame] | 2131 | //version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2132 | msg.m_refid = G.refid; // (version > (3 << VERSION_SHIFT)) ? G.refid : G.refid3; |
| 2133 | |
| 2134 | /* We reply from the local address packet was sent to, |
| 2135 | * this makes to/from look swapped here: */ |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 2136 | do_sendto(G_listen_fd, |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2137 | /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len, |
| 2138 | &msg, size); |
Denys Vlasenko | 855aeac | 2021-02-21 09:47:34 +0100 | [diff] [blame] | 2139 | VERB3 { |
| 2140 | char *addr; |
| 2141 | addr = xmalloc_sockaddr2dotted_noport(from); |
| 2142 | bb_error_msg("responded to query from %s", addr); |
| 2143 | free(addr); |
| 2144 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2145 | |
| 2146 | bail: |
| 2147 | free(to); |
| 2148 | free(from); |
| 2149 | } |
| 2150 | #endif |
| 2151 | |
| 2152 | /* Upstream ntpd's options: |
| 2153 | * |
| 2154 | * -4 Force DNS resolution of host names to the IPv4 namespace. |
| 2155 | * -6 Force DNS resolution of host names to the IPv6 namespace. |
| 2156 | * -a Require cryptographic authentication for broadcast client, |
| 2157 | * multicast client and symmetric passive associations. |
| 2158 | * This is the default. |
| 2159 | * -A Do not require cryptographic authentication for broadcast client, |
| 2160 | * multicast client and symmetric passive associations. |
| 2161 | * This is almost never a good idea. |
| 2162 | * -b Enable the client to synchronize to broadcast servers. |
| 2163 | * -c conffile |
| 2164 | * Specify the name and path of the configuration file, |
| 2165 | * default /etc/ntp.conf |
| 2166 | * -d Specify debugging mode. This option may occur more than once, |
| 2167 | * with each occurrence indicating greater detail of display. |
| 2168 | * -D level |
| 2169 | * Specify debugging level directly. |
| 2170 | * -f driftfile |
| 2171 | * Specify the name and path of the frequency file. |
| 2172 | * This is the same operation as the "driftfile FILE" |
| 2173 | * configuration command. |
| 2174 | * -g Normally, ntpd exits with a message to the system log |
| 2175 | * if the offset exceeds the panic threshold, which is 1000 s |
| 2176 | * by default. This option allows the time to be set to any value |
| 2177 | * without restriction; however, this can happen only once. |
| 2178 | * If the threshold is exceeded after that, ntpd will exit |
| 2179 | * with a message to the system log. This option can be used |
| 2180 | * with the -q and -x options. See the tinker command for other options. |
| 2181 | * -i jaildir |
| 2182 | * Chroot the server to the directory jaildir. This option also implies |
| 2183 | * that the server attempts to drop root privileges at startup |
| 2184 | * (otherwise, chroot gives very little additional security). |
| 2185 | * You may need to also specify a -u option. |
| 2186 | * -k keyfile |
| 2187 | * Specify the name and path of the symmetric key file, |
| 2188 | * default /etc/ntp/keys. This is the same operation |
| 2189 | * as the "keys FILE" configuration command. |
| 2190 | * -l logfile |
| 2191 | * Specify the name and path of the log file. The default |
| 2192 | * is the system log file. This is the same operation as |
| 2193 | * the "logfile FILE" configuration command. |
| 2194 | * -L Do not listen to virtual IPs. The default is to listen. |
| 2195 | * -n Don't fork. |
| 2196 | * -N To the extent permitted by the operating system, |
| 2197 | * run the ntpd at the highest priority. |
| 2198 | * -p pidfile |
| 2199 | * Specify the name and path of the file used to record the ntpd |
| 2200 | * process ID. This is the same operation as the "pidfile FILE" |
| 2201 | * configuration command. |
| 2202 | * -P priority |
| 2203 | * To the extent permitted by the operating system, |
| 2204 | * run the ntpd at the specified priority. |
| 2205 | * -q Exit the ntpd just after the first time the clock is set. |
| 2206 | * This behavior mimics that of the ntpdate program, which is |
| 2207 | * to be retired. The -g and -x options can be used with this option. |
| 2208 | * Note: The kernel time discipline is disabled with this option. |
| 2209 | * -r broadcastdelay |
| 2210 | * Specify the default propagation delay from the broadcast/multicast |
| 2211 | * server to this client. This is necessary only if the delay |
| 2212 | * cannot be computed automatically by the protocol. |
| 2213 | * -s statsdir |
| 2214 | * Specify the directory path for files created by the statistics |
| 2215 | * facility. This is the same operation as the "statsdir DIR" |
| 2216 | * configuration command. |
| 2217 | * -t key |
| 2218 | * Add a key number to the trusted key list. This option can occur |
| 2219 | * more than once. |
| 2220 | * -u user[:group] |
| 2221 | * Specify a user, and optionally a group, to switch to. |
| 2222 | * -v variable |
| 2223 | * -V variable |
| 2224 | * Add a system variable listed by default. |
| 2225 | * -x Normally, the time is slewed if the offset is less than the step |
| 2226 | * threshold, which is 128 ms by default, and stepped if above |
| 2227 | * the threshold. This option sets the threshold to 600 s, which is |
| 2228 | * well within the accuracy window to set the clock manually. |
| 2229 | * Note: since the slew rate of typical Unix kernels is limited |
| 2230 | * to 0.5 ms/s, each second of adjustment requires an amortization |
| 2231 | * interval of 2000 s. Thus, an adjustment as much as 600 s |
| 2232 | * will take almost 14 days to complete. This option can be used |
| 2233 | * with the -g and -q options. See the tinker command for other options. |
| 2234 | * Note: The kernel time discipline is disabled with this option. |
| 2235 | */ |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2236 | #if ENABLE_FEATURE_NTP_AUTH |
| 2237 | static key_entry_t * |
| 2238 | find_key_entry(llist_t *key_entries, unsigned id) |
| 2239 | { |
| 2240 | while (key_entries) { |
| 2241 | key_entry_t *cur = (key_entry_t*) key_entries->data; |
| 2242 | if (cur->id == id) |
| 2243 | return cur; |
| 2244 | key_entries = key_entries->link; |
| 2245 | } |
| 2246 | bb_error_msg_and_die("key %u is not defined", id); |
| 2247 | } |
| 2248 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2249 | |
| 2250 | /* By doing init in a separate function we decrease stack usage |
| 2251 | * in main loop. |
| 2252 | */ |
| 2253 | static NOINLINE void ntp_init(char **argv) |
| 2254 | { |
| 2255 | unsigned opts; |
| 2256 | llist_t *peers; |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2257 | #if ENABLE_FEATURE_NTP_AUTH |
| 2258 | llist_t *key_entries; |
| 2259 | char *key_file_path; |
| 2260 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2261 | |
Denys Vlasenko | 0ed5f7a | 2014-03-05 18:58:15 +0100 | [diff] [blame] | 2262 | srand(getpid()); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2263 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2264 | /* Set some globals */ |
Miroslav Lichvar | 760d035 | 2014-10-05 03:10:15 +0200 | [diff] [blame] | 2265 | G.discipline_jitter = G_precision_sec; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2266 | G.stratum = MAXSTRAT; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2267 | if (BURSTPOLL != 0) |
| 2268 | G.poll_exp = BURSTPOLL; /* speeds up initial sync */ |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 2269 | G.last_script_run = G.reftime = G.last_update_recv_time = gettime1900d(); /* sets G.cur_time too */ |
Denys Vlasenko | 448fdcc | 2018-03-11 17:08:02 +0100 | [diff] [blame] | 2270 | G.FREQHOLD_cnt = -1; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2271 | |
| 2272 | /* Parse options */ |
| 2273 | peers = NULL; |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2274 | IF_FEATURE_NTP_AUTH(key_entries = NULL;) |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 2275 | opts = getopt32(argv, "^" |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2276 | "nqNx" /* compat */ |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2277 | IF_FEATURE_NTP_AUTH("k:") /* compat */ |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 2278 | "wp:*S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ |
Denys Vlasenko | 278842d | 2014-07-15 15:06:54 +0200 | [diff] [blame] | 2279 | IF_FEATURE_NTPD_SERVER("I:") /* compat */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2280 | "d" /* compat */ |
Denys Vlasenko | 8b77a9e | 2017-08-23 15:08:25 +0200 | [diff] [blame] | 2281 | "46aAbgL" /* compat, ignored */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 2282 | "\0" |
Denys Vlasenko | caecfdc | 2019-07-02 11:28:18 +0200 | [diff] [blame] | 2283 | "=0" /* should have no arguments */ |
| 2284 | ":dd:wn" /* -d: counter; -p: list; -w implies -n */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 2285 | IF_FEATURE_NTPD_SERVER(":Il") /* -I implies -l */ |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2286 | IF_FEATURE_NTP_AUTH(, &key_file_path) |
| 2287 | , &peers, &G.script_name |
| 2288 | IF_FEATURE_NTPD_SERVER(, &G.if_name) |
| 2289 | , &G.verbose |
| 2290 | ); |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 2291 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2292 | // if (opts & OPT_x) /* disable stepping, only slew is allowed */ |
| 2293 | // G.time_was_stepped = 1; |
Denys Vlasenko | aabb0a9 | 2016-07-03 17:58:54 +0200 | [diff] [blame] | 2294 | |
| 2295 | #if ENABLE_FEATURE_NTPD_SERVER |
| 2296 | G_listen_fd = -1; |
| 2297 | if (opts & OPT_l) { |
| 2298 | G_listen_fd = create_and_bind_dgram_or_die(NULL, 123); |
| 2299 | if (G.if_name) { |
| 2300 | if (setsockopt_bindtodevice(G_listen_fd, G.if_name)) |
| 2301 | xfunc_die(); |
| 2302 | } |
| 2303 | socket_want_pktinfo(G_listen_fd); |
Codarren Velvindron | 7c43d43 | 2018-04-15 20:37:50 +0400 | [diff] [blame] | 2304 | setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_DSCP_AF21); |
Denys Vlasenko | aabb0a9 | 2016-07-03 17:58:54 +0200 | [diff] [blame] | 2305 | } |
| 2306 | #endif |
| 2307 | /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */ |
| 2308 | if (opts & OPT_N) |
| 2309 | setpriority(PRIO_PROCESS, 0, -15); |
| 2310 | |
Denys Vlasenko | aabb0a9 | 2016-07-03 17:58:54 +0200 | [diff] [blame] | 2311 | if (!(opts & OPT_n)) { |
| 2312 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv); |
| 2313 | logmode = LOGMODE_NONE; |
| 2314 | } |
| 2315 | |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2316 | #if ENABLE_FEATURE_NTP_AUTH |
| 2317 | if (opts & OPT_k) { |
| 2318 | char *tokens[4]; |
| 2319 | parser_t *parser; |
| 2320 | |
| 2321 | parser = config_open(key_file_path); |
| 2322 | while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL | PARSE_MIN_DIE) == 3) { |
| 2323 | key_entry_t *key_entry; |
| 2324 | char buffer[40]; |
| 2325 | smalluint hash_type; |
| 2326 | smalluint msg_size; |
| 2327 | smalluint key_length; |
| 2328 | char *key; |
| 2329 | |
| 2330 | if ((tokens[1][0] | 0x20) == 'm') |
| 2331 | /* supports 'M' and 'md5' formats */ |
| 2332 | hash_type = HASH_MD5; |
| 2333 | else |
| 2334 | if (strncasecmp(tokens[1], "sha", 3) == 0) |
| 2335 | /* supports 'sha' and 'sha1' formats */ |
| 2336 | hash_type = HASH_SHA1; |
| 2337 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2338 | bb_simple_error_msg_and_die("only MD5 and SHA1 keys supported"); |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2339 | /* man ntp.keys: |
| 2340 | * MD5 The key is 1 to 16 printable characters terminated by an EOL, |
| 2341 | * whitespace, or a # (which is the "start of comment" character). |
| 2342 | * SHA |
| 2343 | * SHA1 |
| 2344 | * RMD160 The key is a hex-encoded ASCII string of 40 characters, which |
| 2345 | * is truncated as necessary. |
| 2346 | */ |
| 2347 | key_length = strnlen(tokens[2], sizeof(buffer)+1); |
| 2348 | if (key_length >= sizeof(buffer)+1) { |
| 2349 | err: |
| 2350 | bb_error_msg_and_die("malformed key at line %u", parser->lineno); |
| 2351 | } |
| 2352 | if (hash_type == HASH_MD5) { |
| 2353 | key = tokens[2]; |
| 2354 | msg_size = NTP_MSGSIZE_MD5_AUTH; |
| 2355 | } else /* it's hash_type == HASH_SHA1 */ |
| 2356 | if (!(key_length & 1)) { |
| 2357 | key_length >>= 1; |
| 2358 | if (!hex2bin(buffer, tokens[2], key_length)) |
| 2359 | goto err; |
| 2360 | key = buffer; |
| 2361 | msg_size = NTP_MSGSIZE_SHA1_AUTH; |
| 2362 | } else { |
| 2363 | goto err; |
| 2364 | } |
| 2365 | key_entry = xzalloc(sizeof(*key_entry) + key_length); |
| 2366 | key_entry->type = hash_type; |
| 2367 | key_entry->msg_size = msg_size; |
| 2368 | key_entry->key_length = key_length; |
| 2369 | memcpy(key_entry->key, key, key_length); |
| 2370 | key_entry->id = xatou_range(tokens[0], 1, MAX_KEY_NUMBER); |
| 2371 | llist_add_to(&key_entries, key_entry); |
| 2372 | } |
| 2373 | config_close(parser); |
| 2374 | } |
| 2375 | #endif |
Denys Vlasenko | d678257 | 2010-10-04 01:20:44 +0200 | [diff] [blame] | 2376 | if (peers) { |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2377 | #if ENABLE_FEATURE_NTP_AUTH |
| 2378 | while (peers) { |
| 2379 | char *peer = llist_pop(&peers); |
| 2380 | key_entry_t *key_entry = NULL; |
| 2381 | if (strncmp(peer, "keyno:", 6) == 0) { |
| 2382 | char *end; |
| 2383 | int key_id; |
| 2384 | peer += 6; |
| 2385 | end = strchr(peer, ':'); |
Denys Vlasenko | 0d18e5c | 2018-11-25 00:42:56 +0100 | [diff] [blame] | 2386 | if (!end) bb_show_usage(); |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2387 | *end = '\0'; |
| 2388 | key_id = xatou_range(peer, 1, MAX_KEY_NUMBER); |
| 2389 | *end = ':'; |
| 2390 | key_entry = find_key_entry(key_entries, key_id); |
| 2391 | peer = end + 1; |
| 2392 | } |
| 2393 | add_peers(peer, key_entry); |
| 2394 | } |
| 2395 | #else |
Denys Vlasenko | d678257 | 2010-10-04 01:20:44 +0200 | [diff] [blame] | 2396 | while (peers) |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2397 | add_peers(llist_pop(&peers), NULL); |
| 2398 | #endif |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 2399 | } |
| 2400 | #if ENABLE_FEATURE_NTPD_CONF |
| 2401 | else { |
| 2402 | parser_t *parser; |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2403 | char *token[3 + 2*ENABLE_FEATURE_NTP_AUTH]; |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 2404 | |
| 2405 | parser = config_open("/etc/ntp.conf"); |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2406 | while (config_read(parser, token, 3 + 2*ENABLE_FEATURE_NTP_AUTH, 1, "# \t", PARSE_NORMAL)) { |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 2407 | if (strcmp(token[0], "server") == 0 && token[1]) { |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2408 | # if ENABLE_FEATURE_NTP_AUTH |
| 2409 | key_entry_t *key_entry = NULL; |
| 2410 | if (token[2] && token[3] && strcmp(token[2], "key") == 0) { |
| 2411 | unsigned key_id = xatou_range(token[3], 1, MAX_KEY_NUMBER); |
| 2412 | key_entry = find_key_entry(key_entries, key_id); |
| 2413 | } |
| 2414 | add_peers(token[1], key_entry); |
| 2415 | # else |
| 2416 | add_peers(token[1], NULL); |
| 2417 | # endif |
Denys Vlasenko | 504fe45 | 2014-03-23 15:06:38 +0100 | [diff] [blame] | 2418 | continue; |
| 2419 | } |
| 2420 | bb_error_msg("skipping %s:%u: unimplemented command '%s'", |
| 2421 | "/etc/ntp.conf", parser->lineno, token[0] |
| 2422 | ); |
| 2423 | } |
| 2424 | config_close(parser); |
| 2425 | } |
| 2426 | #endif |
| 2427 | if (G.peer_cnt == 0) { |
| 2428 | if (!(opts & OPT_l)) |
| 2429 | bb_show_usage(); |
Denys Vlasenko | d678257 | 2010-10-04 01:20:44 +0200 | [diff] [blame] | 2430 | /* -l but no peers: "stratum 1 server" mode */ |
| 2431 | G.stratum = 1; |
| 2432 | } |
Denys Vlasenko | da996c5 | 2019-03-27 13:37:40 +0100 | [diff] [blame] | 2433 | |
| 2434 | if (!(opts & OPT_n)) /* only if backgrounded: */ |
| 2435 | write_pidfile_std_path_and_ext("ntpd"); |
| 2436 | |
Denys Vlasenko | 74c992a | 2010-08-27 02:15:01 +0200 | [diff] [blame] | 2437 | /* If network is up, syncronization occurs in ~10 seconds. |
Denys Vlasenko | 8e23faf | 2011-04-07 01:45:20 +0200 | [diff] [blame] | 2438 | * We give "ntpd -q" 10 seconds to get first reply, |
| 2439 | * then another 50 seconds to finish syncing. |
Denys Vlasenko | 74c992a | 2010-08-27 02:15:01 +0200 | [diff] [blame] | 2440 | * |
| 2441 | * I tested ntpd 4.2.6p1 and apparently it never exits |
| 2442 | * (will try forever), but it does not feel right. |
| 2443 | * The goal of -q is to act like ntpdate: set time |
| 2444 | * after a reasonably small period of polling, or fail. |
| 2445 | */ |
Denys Vlasenko | 8e23faf | 2011-04-07 01:45:20 +0200 | [diff] [blame] | 2446 | if (opts & OPT_q) { |
| 2447 | option_mask32 |= OPT_qq; |
| 2448 | alarm(10); |
| 2449 | } |
Denys Vlasenko | 74c992a | 2010-08-27 02:15:01 +0200 | [diff] [blame] | 2450 | |
| 2451 | bb_signals(0 |
| 2452 | | (1 << SIGTERM) |
| 2453 | | (1 << SIGINT) |
| 2454 | | (1 << SIGALRM) |
| 2455 | , record_signo |
| 2456 | ); |
| 2457 | bb_signals(0 |
| 2458 | | (1 << SIGPIPE) |
| 2459 | | (1 << SIGCHLD) |
| 2460 | , SIG_IGN |
| 2461 | ); |
Brandon P. Enochs | a541314 | 2018-10-27 18:55:59 +0200 | [diff] [blame] | 2462 | //TODO: free unused elements of key_entries? |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 2466 | int ntpd_main(int argc UNUSED_PARAM, char **argv) |
| 2467 | { |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2468 | #undef G |
| 2469 | struct globals G; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2470 | struct pollfd *pfd; |
| 2471 | peer_t **idx2peer; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2472 | unsigned cnt; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2473 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2474 | memset(&G, 0, sizeof(G)); |
| 2475 | SET_PTR_TO_GLOBALS(&G); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2476 | |
| 2477 | ntp_init(argv); |
| 2478 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2479 | /* If ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */ |
| 2480 | cnt = G.peer_cnt + ENABLE_FEATURE_NTPD_SERVER; |
| 2481 | idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt); |
| 2482 | pfd = xzalloc(sizeof(pfd[0]) * cnt); |
| 2483 | |
Leonid Lisovskiy | 894ef60 | 2010-10-20 22:36:51 +0200 | [diff] [blame] | 2484 | /* Countdown: we never sync before we sent INITIAL_SAMPLES+1 |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 2485 | * packets to each peer. |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2486 | * NB: if some peer is not responding, we may end up sending |
| 2487 | * fewer packets to it and more to other peers. |
Leonid Lisovskiy | 894ef60 | 2010-10-20 22:36:51 +0200 | [diff] [blame] | 2488 | * NB2: sync usually happens using INITIAL_SAMPLES packets, |
Denys Vlasenko | 65d722b | 2010-01-11 02:14:04 +0100 | [diff] [blame] | 2489 | * since last reply does not come back instantaneously. |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2490 | */ |
Leonid Lisovskiy | 894ef60 | 2010-10-20 22:36:51 +0200 | [diff] [blame] | 2491 | cnt = G.peer_cnt * (INITIAL_SAMPLES + 1); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2492 | |
| 2493 | while (!bb_got_signal) { |
| 2494 | llist_t *item; |
| 2495 | unsigned i, j; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2496 | int nfds, timeout; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2497 | double nextaction; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2498 | |
| 2499 | /* Nothing between here and poll() blocks for any significant time */ |
| 2500 | |
Denys Vlasenko | 36acc46 | 2017-12-26 20:19:37 +0100 | [diff] [blame] | 2501 | nextaction = G.last_script_run + (11*60); |
| 2502 | if (nextaction < G.cur_time + 1) |
| 2503 | nextaction = G.cur_time + 1; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2504 | |
| 2505 | i = 0; |
| 2506 | #if ENABLE_FEATURE_NTPD_SERVER |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 2507 | if (G_listen_fd != -1) { |
| 2508 | pfd[0].fd = G_listen_fd; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2509 | pfd[0].events = POLLIN; |
| 2510 | i++; |
| 2511 | } |
| 2512 | #endif |
| 2513 | /* Pass over peer list, send requests, time out on receives */ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2514 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2515 | peer_t *p = (peer_t *) item->data; |
| 2516 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2517 | if (p->next_action_time <= G.cur_time) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2518 | if (p->p_fd == -1) { |
| 2519 | /* Time to send new req */ |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2520 | if (--cnt == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2521 | VERB4 bb_simple_error_msg("disabling burst mode"); |
Miroslav Lichvar | fb143f7 | 2014-09-18 16:19:03 +0200 | [diff] [blame] | 2522 | G.polladj_count = 0; |
| 2523 | G.poll_exp = MINPOLL; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2524 | } |
| 2525 | send_query_to_peer(p); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2526 | } else { |
| 2527 | /* Timed out waiting for reply */ |
| 2528 | close(p->p_fd); |
| 2529 | p->p_fd = -1; |
Miroslav Lichvar | b434ce7 | 2014-10-02 17:18:43 +0200 | [diff] [blame] | 2530 | /* If poll interval is small, increase it */ |
| 2531 | if (G.poll_exp < BIGPOLL) |
| 2532 | adjust_poll(MINPOLL); |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 2533 | timeout = poll_interval(NOREPLY_INTERVAL); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2534 | bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us", |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2535 | p->p_dotted, p->reachable_bits, timeout); |
Denys Vlasenko | c864196 | 2016-03-04 07:26:08 +0100 | [diff] [blame] | 2536 | |
| 2537 | /* What if don't see it because it changed its IP? */ |
Denys Vlasenko | e4caf1d | 2016-06-06 02:26:49 +0200 | [diff] [blame] | 2538 | if (p->reachable_bits == 0) |
Natanael Copa | b62ea34 | 2017-01-06 16:18:45 +0100 | [diff] [blame] | 2539 | resolve_peer_hostname(p); |
Denys Vlasenko | c864196 | 2016-03-04 07:26:08 +0100 | [diff] [blame] | 2540 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2541 | set_next(p, timeout); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (p->next_action_time < nextaction) |
| 2546 | nextaction = p->next_action_time; |
| 2547 | |
| 2548 | if (p->p_fd >= 0) { |
| 2549 | /* Wait for reply from this peer */ |
| 2550 | pfd[i].fd = p->p_fd; |
| 2551 | pfd[i].events = POLLIN; |
| 2552 | idx2peer[i] = p; |
| 2553 | i++; |
| 2554 | } |
| 2555 | } |
| 2556 | |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2557 | timeout = nextaction - G.cur_time; |
| 2558 | if (timeout < 0) |
| 2559 | timeout = 0; |
| 2560 | timeout++; /* (nextaction - G.cur_time) rounds down, compensating */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2561 | |
| 2562 | /* Here we may block */ |
Denys Vlasenko | 5024d86 | 2021-02-21 08:54:08 +0100 | [diff] [blame] | 2563 | VERB3 { |
Denys Vlasenko | 3e3a8d5 | 2012-04-01 16:31:04 +0200 | [diff] [blame] | 2564 | if (i > (ENABLE_FEATURE_NTPD_SERVER && G_listen_fd != -1)) { |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 2565 | /* We wait for at least one reply. |
| 2566 | * Poll for it, without wasting time for message. |
| 2567 | * Since replies often come under 1 second, this also |
| 2568 | * reduces clutter in logs. |
| 2569 | */ |
| 2570 | nfds = poll(pfd, i, 1000); |
| 2571 | if (nfds != 0) |
| 2572 | goto did_poll; |
| 2573 | if (--timeout <= 0) |
| 2574 | goto did_poll; |
| 2575 | } |
Denys Vlasenko | 8be49c3 | 2012-03-06 19:16:50 +0100 | [diff] [blame] | 2576 | bb_error_msg("poll:%us sockets:%u interval:%us", timeout, i, 1 << G.poll_exp); |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 2577 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2578 | nfds = poll(pfd, i, timeout * 1000); |
Denys Vlasenko | e8ce285 | 2012-03-03 12:15:46 +0100 | [diff] [blame] | 2579 | did_poll: |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2580 | gettime1900d(); /* sets G.cur_time */ |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 2581 | if (nfds <= 0) { |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2582 | double ct; |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 2583 | int dns_error; |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2584 | |
| 2585 | if (bb_got_signal) |
| 2586 | break; /* poll was interrupted by a signal */ |
| 2587 | |
| 2588 | if (G.cur_time - G.last_script_run > 11*60) { |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 2589 | /* Useful for updating battery-backed RTC and such */ |
Denys Vlasenko | 12628b7 | 2010-01-11 01:31:59 +0100 | [diff] [blame] | 2590 | run_script("periodic", G.last_update_offset); |
Denys Vlasenko | 06667f2 | 2010-01-06 13:05:08 +0100 | [diff] [blame] | 2591 | gettime1900d(); /* sets G.cur_time */ |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 2592 | } |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* Resolve peer names to IPs, if not resolved yet. |
| 2595 | * We do it only when poll timed out: |
| 2596 | * this way, we almost never overlap DNS resolution with |
| 2597 | * "request-reply" packet round trip. |
| 2598 | */ |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 2599 | dns_error = 0; |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2600 | ct = G.cur_time; |
| 2601 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 2602 | peer_t *p = (peer_t *) item->data; |
| 2603 | if (p->next_action_time <= ct && !p->p_lsa) { |
| 2604 | /* This can take up to ~10 sec per each DNS query */ |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 2605 | dns_error |= (!resolve_peer_hostname(p)); |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2606 | } |
| 2607 | } |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 2608 | if (!dns_error) |
| 2609 | goto check_unsync; |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2610 | /* Set next time for those which are still not resolved */ |
Denys Vlasenko | d5c1482 | 2017-10-31 16:53:23 +0100 | [diff] [blame] | 2611 | gettime1900d(); /* sets G.cur_time (needed for set_next()) */ |
Denys Vlasenko | 6a4f223 | 2017-10-31 12:44:37 +0100 | [diff] [blame] | 2612 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 2613 | peer_t *p = (peer_t *) item->data; |
| 2614 | if (p->next_action_time <= ct && !p->p_lsa) { |
| 2615 | set_next(p, HOSTNAME_INTERVAL * p->dns_errors); |
| 2616 | } |
| 2617 | } |
Denys Vlasenko | 5ffdd1d | 2013-05-22 18:16:34 +0200 | [diff] [blame] | 2618 | goto check_unsync; |
Denys Vlasenko | ede737b | 2010-01-06 12:27:47 +0100 | [diff] [blame] | 2619 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* Process any received packets */ |
| 2622 | j = 0; |
| 2623 | #if ENABLE_FEATURE_NTPD_SERVER |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2624 | if (G.listen_fd != -1) { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2625 | if (pfd[0].revents /* & (POLLIN|POLLERR)*/) { |
| 2626 | nfds--; |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2627 | recv_and_process_client_pkt(/*G.listen_fd*/); |
| 2628 | gettime1900d(); /* sets G.cur_time */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2629 | } |
| 2630 | j = 1; |
| 2631 | } |
| 2632 | #endif |
| 2633 | for (; nfds != 0 && j < i; j++) { |
| 2634 | if (pfd[j].revents /* & (POLLIN|POLLERR)*/) { |
Denys Vlasenko | 8e23faf | 2011-04-07 01:45:20 +0200 | [diff] [blame] | 2635 | /* |
| 2636 | * At init, alarm was set to 10 sec. |
| 2637 | * Now we did get a reply. |
| 2638 | * Increase timeout to 50 seconds to finish syncing. |
| 2639 | */ |
| 2640 | if (option_mask32 & OPT_qq) { |
| 2641 | option_mask32 &= ~OPT_qq; |
| 2642 | alarm(50); |
| 2643 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2644 | nfds--; |
| 2645 | recv_and_process_peer_pkt(idx2peer[j]); |
Denys Vlasenko | 0b00281 | 2010-01-03 08:59:59 +0100 | [diff] [blame] | 2646 | gettime1900d(); /* sets G.cur_time */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2647 | } |
| 2648 | } |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 2649 | |
Denys Vlasenko | 5ffdd1d | 2013-05-22 18:16:34 +0200 | [diff] [blame] | 2650 | check_unsync: |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 2651 | if (G.ntp_peers && G.stratum != MAXSTRAT) { |
| 2652 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 2653 | peer_t *p = (peer_t *) item->data; |
| 2654 | if (p->reachable_bits) |
| 2655 | goto have_reachable_peer; |
| 2656 | } |
| 2657 | /* No peer responded for last 8 packets, panic */ |
Denys Vlasenko | d3fe960 | 2014-09-27 22:56:09 +0200 | [diff] [blame] | 2658 | clamp_pollexp_and_set_MAXSTRAT(); |
Denys Vlasenko | 5a7e337 | 2013-05-23 16:06:59 +0200 | [diff] [blame] | 2659 | run_script("unsync", 0.0); |
Denys Vlasenko | d99ef63 | 2013-05-22 17:48:19 +0200 | [diff] [blame] | 2660 | have_reachable_peer: ; |
| 2661 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2662 | } /* while (!bb_got_signal) */ |
| 2663 | |
Denys Vlasenko | 5059653 | 2019-03-17 19:47:52 +0100 | [diff] [blame] | 2664 | remove_pidfile_std_path_and_ext("ntpd"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2665 | kill_myself_with_sig(bb_got_signal); |
| 2666 | } |
| 2667 | |
| 2668 | |
| 2669 | |
| 2670 | |
| 2671 | |
| 2672 | |
| 2673 | /*** openntpd-4.6 uses only adjtime, not adjtimex ***/ |
| 2674 | |
| 2675 | /*** ntp-4.2.6/ntpd/ntp_loopfilter.c - adjtimex usage ***/ |
| 2676 | |
| 2677 | #if 0 |
| 2678 | static double |
| 2679 | direct_freq(double fp_offset) |
| 2680 | { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2681 | #ifdef KERNEL_PLL |
| 2682 | /* |
| 2683 | * If the kernel is enabled, we need the residual offset to |
| 2684 | * calculate the frequency correction. |
| 2685 | */ |
| 2686 | if (pll_control && kern_enable) { |
| 2687 | memset(&ntv, 0, sizeof(ntv)); |
| 2688 | ntp_adjtime(&ntv); |
| 2689 | #ifdef STA_NANO |
| 2690 | clock_offset = ntv.offset / 1e9; |
| 2691 | #else /* STA_NANO */ |
| 2692 | clock_offset = ntv.offset / 1e6; |
| 2693 | #endif /* STA_NANO */ |
| 2694 | drift_comp = FREQTOD(ntv.freq); |
| 2695 | } |
| 2696 | #endif /* KERNEL_PLL */ |
| 2697 | set_freq((fp_offset - clock_offset) / (current_time - clock_epoch) + drift_comp); |
| 2698 | wander_resid = 0; |
| 2699 | return drift_comp; |
| 2700 | } |
| 2701 | |
| 2702 | static void |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 2703 | set_freq(double freq) /* frequency update */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2704 | { |
| 2705 | char tbuf[80]; |
| 2706 | |
| 2707 | drift_comp = freq; |
| 2708 | |
| 2709 | #ifdef KERNEL_PLL |
| 2710 | /* |
| 2711 | * If the kernel is enabled, update the kernel frequency. |
| 2712 | */ |
| 2713 | if (pll_control && kern_enable) { |
| 2714 | memset(&ntv, 0, sizeof(ntv)); |
| 2715 | ntv.modes = MOD_FREQUENCY; |
| 2716 | ntv.freq = DTOFREQ(drift_comp); |
| 2717 | ntp_adjtime(&ntv); |
| 2718 | snprintf(tbuf, sizeof(tbuf), "kernel %.3f PPM", drift_comp * 1e6); |
| 2719 | report_event(EVNT_FSET, NULL, tbuf); |
| 2720 | } else { |
| 2721 | snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6); |
| 2722 | report_event(EVNT_FSET, NULL, tbuf); |
| 2723 | } |
| 2724 | #else /* KERNEL_PLL */ |
| 2725 | snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6); |
| 2726 | report_event(EVNT_FSET, NULL, tbuf); |
| 2727 | #endif /* KERNEL_PLL */ |
| 2728 | } |
| 2729 | |
| 2730 | ... |
| 2731 | ... |
| 2732 | ... |
| 2733 | |
| 2734 | #ifdef KERNEL_PLL |
| 2735 | /* |
| 2736 | * This code segment works when clock adjustments are made using |
| 2737 | * precision time kernel support and the ntp_adjtime() system |
| 2738 | * call. This support is available in Solaris 2.6 and later, |
| 2739 | * Digital Unix 4.0 and later, FreeBSD, Linux and specially |
| 2740 | * modified kernels for HP-UX 9 and Ultrix 4. In the case of the |
| 2741 | * DECstation 5000/240 and Alpha AXP, additional kernel |
| 2742 | * modifications provide a true microsecond clock and nanosecond |
| 2743 | * clock, respectively. |
| 2744 | * |
| 2745 | * Important note: The kernel discipline is used only if the |
| 2746 | * step threshold is less than 0.5 s, as anything higher can |
| 2747 | * lead to overflow problems. This might occur if some misguided |
| 2748 | * lad set the step threshold to something ridiculous. |
| 2749 | */ |
| 2750 | if (pll_control && kern_enable) { |
| 2751 | |
| 2752 | #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | MOD_STATUS | MOD_TIMECONST) |
| 2753 | |
| 2754 | /* |
| 2755 | * We initialize the structure for the ntp_adjtime() |
| 2756 | * system call. We have to convert everything to |
| 2757 | * microseconds or nanoseconds first. Do not update the |
| 2758 | * system variables if the ext_enable flag is set. In |
| 2759 | * this case, the external clock driver will update the |
| 2760 | * variables, which will be read later by the local |
| 2761 | * clock driver. Afterwards, remember the time and |
| 2762 | * frequency offsets for jitter and stability values and |
| 2763 | * to update the frequency file. |
| 2764 | */ |
| 2765 | memset(&ntv, 0, sizeof(ntv)); |
| 2766 | if (ext_enable) { |
| 2767 | ntv.modes = MOD_STATUS; |
| 2768 | } else { |
| 2769 | #ifdef STA_NANO |
| 2770 | ntv.modes = MOD_BITS | MOD_NANO; |
| 2771 | #else /* STA_NANO */ |
| 2772 | ntv.modes = MOD_BITS; |
| 2773 | #endif /* STA_NANO */ |
| 2774 | if (clock_offset < 0) |
| 2775 | dtemp = -.5; |
| 2776 | else |
| 2777 | dtemp = .5; |
| 2778 | #ifdef STA_NANO |
| 2779 | ntv.offset = (int32)(clock_offset * 1e9 + dtemp); |
| 2780 | ntv.constant = sys_poll; |
| 2781 | #else /* STA_NANO */ |
| 2782 | ntv.offset = (int32)(clock_offset * 1e6 + dtemp); |
| 2783 | ntv.constant = sys_poll - 4; |
| 2784 | #endif /* STA_NANO */ |
| 2785 | ntv.esterror = (u_int32)(clock_jitter * 1e6); |
| 2786 | ntv.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6); |
| 2787 | ntv.status = STA_PLL; |
| 2788 | |
| 2789 | /* |
| 2790 | * Enable/disable the PPS if requested. |
| 2791 | */ |
| 2792 | if (pps_enable) { |
| 2793 | if (!(pll_status & STA_PPSTIME)) |
| 2794 | report_event(EVNT_KERN, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 2795 | NULL, "PPS enabled"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2796 | ntv.status |= STA_PPSTIME | STA_PPSFREQ; |
| 2797 | } else { |
| 2798 | if (pll_status & STA_PPSTIME) |
| 2799 | report_event(EVNT_KERN, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 2800 | NULL, "PPS disabled"); |
| 2801 | ntv.status &= ~(STA_PPSTIME | STA_PPSFREQ); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2802 | } |
| 2803 | if (sys_leap == LEAP_ADDSECOND) |
| 2804 | ntv.status |= STA_INS; |
| 2805 | else if (sys_leap == LEAP_DELSECOND) |
| 2806 | ntv.status |= STA_DEL; |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * Pass the stuff to the kernel. If it squeals, turn off |
| 2811 | * the pps. In any case, fetch the kernel offset, |
| 2812 | * frequency and jitter. |
| 2813 | */ |
| 2814 | if (ntp_adjtime(&ntv) == TIME_ERROR) { |
| 2815 | if (!(ntv.status & STA_PPSSIGNAL)) |
| 2816 | report_event(EVNT_KERN, NULL, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 2817 | "PPS no signal"); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 2818 | } |
| 2819 | pll_status = ntv.status; |
| 2820 | #ifdef STA_NANO |
| 2821 | clock_offset = ntv.offset / 1e9; |
| 2822 | #else /* STA_NANO */ |
| 2823 | clock_offset = ntv.offset / 1e6; |
| 2824 | #endif /* STA_NANO */ |
| 2825 | clock_frequency = FREQTOD(ntv.freq); |
| 2826 | |
| 2827 | /* |
| 2828 | * If the kernel PPS is lit, monitor its performance. |
| 2829 | */ |
| 2830 | if (ntv.status & STA_PPSTIME) { |
| 2831 | #ifdef STA_NANO |
| 2832 | clock_jitter = ntv.jitter / 1e9; |
| 2833 | #else /* STA_NANO */ |
| 2834 | clock_jitter = ntv.jitter / 1e6; |
| 2835 | #endif /* STA_NANO */ |
| 2836 | } |
| 2837 | |
| 2838 | #if defined(STA_NANO) && NTP_API == 4 |
| 2839 | /* |
| 2840 | * If the TAI changes, update the kernel TAI. |
| 2841 | */ |
| 2842 | if (loop_tai != sys_tai) { |
| 2843 | loop_tai = sys_tai; |
| 2844 | ntv.modes = MOD_TAI; |
| 2845 | ntv.constant = sys_tai; |
| 2846 | ntp_adjtime(&ntv); |
| 2847 | } |
| 2848 | #endif /* STA_NANO */ |
| 2849 | } |
| 2850 | #endif /* KERNEL_PLL */ |
| 2851 | #endif |