blob: 55bded8ffe38b05335cb572fb245eae16e364bb3 [file] [log] [blame]
Adam Tkacb1585062009-11-22 03:43:55 +01001/*
2 * NTP client/server, based on OpenNTPD 3.9p1
3 *
4 * Author: Adam Tkac <vonsch@gmail.com>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Adam Tkacb1585062009-11-22 03:43:55 +01007 */
Adam Tkacb1585062009-11-22 03:43:55 +01008#include "libbb.h"
9#include <netinet/ip.h> /* For IPTOS_LOWDELAY definition */
Mike Frysingerc5fe9f72012-07-05 23:19:09 -040010#include <sys/resource.h> /* setpriority */
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +010011#ifndef IPTOS_LOWDELAY
12# define IPTOS_LOWDELAY 0x10
13#endif
Adam Tkacb1585062009-11-22 03:43:55 +010014#ifndef IP_PKTINFO
15# error "Sorry, your kernel has to support IP_PKTINFO"
16#endif
17
Denys Vlasenko907647f2009-12-02 23:17:45 +010018
19/* Sync to peers every N secs */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010020#define INTERVAL_QUERY_NORMAL 30
21#define INTERVAL_QUERY_PATHETIC 60
22#define INTERVAL_QUERY_AGRESSIVE 5
Adam Tkacb1585062009-11-22 03:43:55 +010023
Denys Vlasenko907647f2009-12-02 23:17:45 +010024/* Bad if *less than* TRUSTLEVEL_BADPEER */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010025#define TRUSTLEVEL_BADPEER 6
26#define TRUSTLEVEL_PATHETIC 2
27#define TRUSTLEVEL_AGRESSIVE 8
28#define TRUSTLEVEL_MAX 10
Adam Tkacb1585062009-11-22 03:43:55 +010029
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010030#define QSCALE_OFF_MIN 0.05
31#define QSCALE_OFF_MAX 0.50
Adam Tkacb1585062009-11-22 03:43:55 +010032
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +010033/* Single query might take N secs max */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010034#define QUERYTIME_MAX 15
Denys Vlasenko907647f2009-12-02 23:17:45 +010035/* Min offset for settime at start. "man ntpd" says it's 128 ms */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010036#define STEPTIME_MIN_OFFSET 0.128
Adam Tkacb1585062009-11-22 03:43:55 +010037
Adam Tkacb1585062009-11-22 03:43:55 +010038typedef struct {
39 uint32_t int_partl;
40 uint32_t fractionl;
41} l_fixedpt_t;
42
43typedef struct {
44 uint16_t int_parts;
45 uint16_t fractions;
46} s_fixedpt_t;
47
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +010048enum {
49 NTP_DIGESTSIZE = 16,
50 NTP_MSGSIZE_NOAUTH = 48,
51 NTP_MSGSIZE = (NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE),
52};
Adam Tkacb1585062009-11-22 03:43:55 +010053
54typedef struct {
Denys Vlasenko386960a2009-12-02 01:51:24 +010055 uint8_t m_status; /* status of local clock and leap info */
56 uint8_t m_stratum; /* stratum level */
57 uint8_t m_ppoll; /* poll value */
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +010058 int8_t m_precision_exp;
Denys Vlasenko386960a2009-12-02 01:51:24 +010059 s_fixedpt_t m_rootdelay;
60 s_fixedpt_t m_dispersion;
61 uint32_t m_refid;
62 l_fixedpt_t m_reftime;
63 l_fixedpt_t m_orgtime;
64 l_fixedpt_t m_rectime;
65 l_fixedpt_t m_xmttime;
66 uint32_t m_keyid;
67 uint8_t m_digest[NTP_DIGESTSIZE];
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +010068} msg_t;
Adam Tkacb1585062009-11-22 03:43:55 +010069
Adam Tkacb1585062009-11-22 03:43:55 +010070enum {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010071 NTP_VERSION = 4,
72 NTP_MAXSTRATUM = 15,
Adam Tkacb1585062009-11-22 03:43:55 +010073
74 /* Status Masks */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010075 MODE_MASK = (7 << 0),
76 VERSION_MASK = (7 << 3),
77 VERSION_SHIFT = 3,
78 LI_MASK = (3 << 6),
Adam Tkacb1585062009-11-22 03:43:55 +010079
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +010080 /* Leap Second Codes (high order two bits of m_status) */
81 LI_NOWARNING = (0 << 6), /* no warning */
82 LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */
83 LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */
84 LI_ALARM = (3 << 6), /* alarm condition */
85
Adam Tkacb1585062009-11-22 03:43:55 +010086 /* Mode values */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +010087 MODE_RES0 = 0, /* reserved */
88 MODE_SYM_ACT = 1, /* symmetric active */
89 MODE_SYM_PAS = 2, /* symmetric passive */
90 MODE_CLIENT = 3, /* client */
91 MODE_SERVER = 4, /* server */
92 MODE_BROADCAST = 5, /* broadcast */
93 MODE_RES1 = 6, /* reserved for NTP control message */
94 MODE_RES2 = 7, /* reserved for private use */
Adam Tkacb1585062009-11-22 03:43:55 +010095};
96
Denys Vlasenkob1278a32009-11-24 16:03:47 +010097#define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */
Adam Tkacb1585062009-11-22 03:43:55 +010098
Denys Vlasenko386960a2009-12-02 01:51:24 +010099typedef struct {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100100 double d_offset;
101 double d_delay;
102 //UNUSED: double d_error;
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100103 time_t d_rcv_time;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100104 uint32_t d_refid4;
105 uint8_t d_leap;
106 uint8_t d_stratum;
107 uint8_t d_good;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100108} datapoint_t;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100109
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100110#define NUM_DATAPOINTS 8
Denys Vlasenko386960a2009-12-02 01:51:24 +0100111typedef struct {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100112 len_and_sockaddr *p_lsa;
113 char *p_dotted;
114 /* When to send new query (if p_fd == -1)
115 * or when receive times out (if p_fd >= 0): */
116 time_t next_action_time;
117 int p_fd;
118 uint8_t p_datapoint_idx;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100119 uint8_t p_trustlevel;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100120 double p_xmttime;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100121 datapoint_t update;
122 datapoint_t p_datapoint[NUM_DATAPOINTS];
123 msg_t p_xmt_msg;
124} peer_t;
Adam Tkacb1585062009-11-22 03:43:55 +0100125
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100126enum {
127 OPT_n = (1 << 0),
Denys Vlasenko907647f2009-12-02 23:17:45 +0100128 OPT_q = (1 << 1),
129 OPT_N = (1 << 2),
130 OPT_x = (1 << 3),
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100131 /* Insert new options above this line. */
132 /* Non-compat options: */
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100133 OPT_p = (1 << 4),
134 OPT_l = (1 << 5) * ENABLE_FEATURE_NTPD_SERVER,
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100135};
136
Adam Tkacb1585062009-11-22 03:43:55 +0100137
138struct globals {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100139 /* total round trip delay to currently selected reference clock */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100140 double rootdelay;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100141 /* reference timestamp: time when the system clock was last set or corrected */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100142 double reftime;
143 llist_t *ntp_peers;
Adam Tkacb1585062009-11-22 03:43:55 +0100144#if ENABLE_FEATURE_NTPD_SERVER
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100145 int listen_fd;
Adam Tkacb1585062009-11-22 03:43:55 +0100146#endif
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100147 unsigned verbose;
148 unsigned peer_cnt;
149 unsigned scale;
150 uint32_t refid;
151 uint32_t refid4;
152 uint8_t synced;
153 uint8_t leap;
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100154#define G_precision_exp -6
155// int8_t precision_exp;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100156 uint8_t stratum;
157 uint8_t time_was_stepped;
158 uint8_t first_adj_done;
Adam Tkacb1585062009-11-22 03:43:55 +0100159};
160#define G (*ptr_to_globals)
161
Adam Tkacb1585062009-11-22 03:43:55 +0100162static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
163
164
165static void
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100166set_next(peer_t *p, unsigned t)
Adam Tkacb1585062009-11-22 03:43:55 +0100167{
Denys Vlasenko907647f2009-12-02 23:17:45 +0100168 p->next_action_time = time(NULL) + t;
Adam Tkacb1585062009-11-22 03:43:55 +0100169}
170
171static void
Denys Vlasenko907647f2009-12-02 23:17:45 +0100172add_peers(char *s)
Adam Tkacb1585062009-11-22 03:43:55 +0100173{
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100174 peer_t *p;
Adam Tkacb1585062009-11-22 03:43:55 +0100175
176 p = xzalloc(sizeof(*p));
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100177 p->p_lsa = xhost2sockaddr(s, 123);
178 p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa);
179 p->p_fd = -1;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100180 p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
181 p->p_trustlevel = TRUSTLEVEL_PATHETIC;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100182 p->next_action_time = time(NULL); /* = set_next(p, 0); */
Adam Tkacb1585062009-11-22 03:43:55 +0100183
184 llist_add_to(&G.ntp_peers, p);
185 G.peer_cnt++;
186}
187
188static double
Denys Vlasenko907647f2009-12-02 23:17:45 +0100189gettime1900d(void)
Adam Tkacb1585062009-11-22 03:43:55 +0100190{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100191 struct timeval tv;
Adam Tkacb1585062009-11-22 03:43:55 +0100192 gettimeofday(&tv, NULL); /* never fails */
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100193 return (tv.tv_sec + 1.0e-6 * tv.tv_usec + OFFSET_1900_1970);
Adam Tkacb1585062009-11-22 03:43:55 +0100194}
195
Adam Tkacb1585062009-11-22 03:43:55 +0100196static void
197d_to_tv(double d, struct timeval *tv)
198{
199 tv->tv_sec = (long)d;
200 tv->tv_usec = (d - tv->tv_sec) * 1000000;
201}
202
203static double
204lfp_to_d(l_fixedpt_t lfp)
205{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100206 double ret;
Adam Tkacb1585062009-11-22 03:43:55 +0100207 lfp.int_partl = ntohl(lfp.int_partl);
208 lfp.fractionl = ntohl(lfp.fractionl);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100209 ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX);
210 return ret;
211}
212
Denys Vlasenko386960a2009-12-02 01:51:24 +0100213#if 0 //UNUSED
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100214static double
215sfp_to_d(s_fixedpt_t sfp)
216{
217 double ret;
218 sfp.int_parts = ntohs(sfp.int_parts);
219 sfp.fractions = ntohs(sfp.fractions);
220 ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX);
Adam Tkacb1585062009-11-22 03:43:55 +0100221 return ret;
222}
Denys Vlasenko386960a2009-12-02 01:51:24 +0100223#endif
Adam Tkacb1585062009-11-22 03:43:55 +0100224
225#if ENABLE_FEATURE_NTPD_SERVER
226static l_fixedpt_t
227d_to_lfp(double d)
228{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100229 l_fixedpt_t lfp;
230 lfp.int_partl = (uint32_t)d;
231 lfp.fractionl = (uint32_t)((d - lfp.int_partl) * UINT_MAX);
232 lfp.int_partl = htonl(lfp.int_partl);
233 lfp.fractionl = htonl(lfp.fractionl);
Adam Tkacb1585062009-11-22 03:43:55 +0100234 return lfp;
235}
Adam Tkacb1585062009-11-22 03:43:55 +0100236
Adam Tkacb1585062009-11-22 03:43:55 +0100237static s_fixedpt_t
238d_to_sfp(double d)
239{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100240 s_fixedpt_t sfp;
241 sfp.int_parts = (uint16_t)d;
242 sfp.fractions = (uint16_t)((d - sfp.int_parts) * USHRT_MAX);
243 sfp.int_parts = htons(sfp.int_parts);
244 sfp.fractions = htons(sfp.fractions);
Adam Tkacb1585062009-11-22 03:43:55 +0100245 return sfp;
246}
247#endif
248
Denys Vlasenko386960a2009-12-02 01:51:24 +0100249static unsigned
Adam Tkacb1585062009-11-22 03:43:55 +0100250error_interval(void)
251{
Denys Vlasenko386960a2009-12-02 01:51:24 +0100252 unsigned interval, r;
Adam Tkacb1585062009-11-22 03:43:55 +0100253 interval = INTERVAL_QUERY_PATHETIC * QSCALE_OFF_MAX / QSCALE_OFF_MIN;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100254 r = (unsigned)random() % (unsigned)(interval / 10);
Adam Tkacb1585062009-11-22 03:43:55 +0100255 return (interval + r);
256}
257
258static int
Denys Vlasenko386960a2009-12-02 01:51:24 +0100259do_sendto(int fd,
Adam Tkacb1585062009-11-22 03:43:55 +0100260 const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen,
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100261 msg_t *msg, ssize_t len)
Adam Tkacb1585062009-11-22 03:43:55 +0100262{
263 ssize_t ret;
264
265 errno = 0;
266 if (!from) {
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100267 ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen);
Adam Tkacb1585062009-11-22 03:43:55 +0100268 } else {
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100269 ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen);
Adam Tkacb1585062009-11-22 03:43:55 +0100270 }
271 if (ret != len) {
272 bb_perror_msg("send failed");
273 return -1;
274 }
275 return 0;
276}
277
278static int
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100279send_query_to_peer(peer_t *p)
Adam Tkacb1585062009-11-22 03:43:55 +0100280{
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100281 // Why do we need to bind()?
282 // See what happens when we don't bind:
283 //
284 // socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
285 // setsockopt(3, SOL_IP, IP_TOS, [16], 4) = 0
286 // gettimeofday({1259071266, 327885}, NULL) = 0
287 // sendto(3, "xxx", 48, MSG_DONTWAIT, {sa_family=AF_INET, sin_port=htons(123), sin_addr=inet_addr("10.34.32.125")}, 16) = 48
288 // ^^^ we sent it from some source port picked by kernel.
289 // time(NULL) = 1259071266
290 // write(2, "ntpd: entering poll 15 secs\n", 28) = 28
291 // poll([{fd=3, events=POLLIN}], 1, 15000) = 1 ([{fd=3, revents=POLLIN}])
292 // recv(3, "yyy", 68, MSG_DONTWAIT) = 48
293 // ^^^ this recv will receive packets to any local port!
294 //
295 // Uncomment this and use strace to see it in action:
296#define PROBE_LOCAL_ADDR // { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); }
297
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100298 if (p->p_fd == -1) {
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100299 int fd, family;
300 len_and_sockaddr *local_lsa;
301
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100302 family = p->p_lsa->u.sa.sa_family;
303 p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM);
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100304 /* local_lsa has "null" address and port 0 now.
305 * bind() ensures we have a *particular port* selected by kernel
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100306 * and remembered in p->p_fd, thus later recv(p->p_fd)
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100307 * receives only packets sent to this port.
308 */
309 PROBE_LOCAL_ADDR
310 xbind(fd, &local_lsa->u.sa, local_lsa->len);
311 PROBE_LOCAL_ADDR
Adam Tkacb1585062009-11-22 03:43:55 +0100312#if ENABLE_FEATURE_IPV6
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100313 if (family == AF_INET)
Adam Tkacb1585062009-11-22 03:43:55 +0100314#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100315 setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
316 free(local_lsa);
Adam Tkacb1585062009-11-22 03:43:55 +0100317 }
318
319 /*
320 * Send out a random 64-bit number as our transmit time. The NTP
321 * server will copy said number into the originate field on the
322 * response that it sends us. This is totally legal per the SNTP spec.
323 *
324 * The impact of this is two fold: we no longer send out the current
325 * system time for the world to see (which may aid an attacker), and
326 * it gives us a (not very secure) way of knowing that we're not
327 * getting spoofed by an attacker that can't capture our traffic
328 * but can spoof packets from the NTP server we're communicating with.
329 *
330 * Save the real transmit timestamp locally.
331 */
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100332 p->p_xmt_msg.m_xmttime.int_partl = random();
333 p->p_xmt_msg.m_xmttime.fractionl = random();
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100334 p->p_xmttime = gettime1900d();
Adam Tkacb1585062009-11-22 03:43:55 +0100335
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100336 if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len,
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100337 &p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1
Denys Vlasenko386960a2009-12-02 01:51:24 +0100338 ) {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100339 close(p->p_fd);
340 p->p_fd = -1;
Adam Tkacb1585062009-11-22 03:43:55 +0100341 set_next(p, INTERVAL_QUERY_PATHETIC);
342 return -1;
343 }
344
Denys Vlasenko386960a2009-12-02 01:51:24 +0100345 if (G.verbose)
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100346 bb_error_msg("sent query to %s", p->p_dotted);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100347 set_next(p, QUERYTIME_MAX);
Adam Tkacb1585062009-11-22 03:43:55 +0100348
349 return 0;
350}
351
Denys Vlasenko907647f2009-12-02 23:17:45 +0100352
353/* Time is stepped only once, when the first packet from a peer is received.
354 */
355static void
356step_time_once(double offset)
357{
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100358 double dtime;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100359 llist_t *item;
360 struct timeval tv;
361 char buf[80];
362 time_t tval;
363
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100364 if (G.time_was_stepped)
Denys Vlasenko907647f2009-12-02 23:17:45 +0100365 goto bail;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100366 G.time_was_stepped = 1;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100367
368 /* if the offset is small, don't step, slew (later) */
369 if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET)
370 goto bail;
371
372 gettimeofday(&tv, NULL); /* never fails */
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100373 dtime = offset + tv.tv_sec;
374 dtime += 1.0e-6 * tv.tv_usec;
375 d_to_tv(dtime, &tv);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100376
377 if (settimeofday(&tv, NULL) == -1)
378 bb_perror_msg_and_die("settimeofday");
379
380 tval = tv.tv_sec;
381 strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
382
383 bb_error_msg("setting clock to %s (offset %fs)", buf, offset);
384
385 for (item = G.ntp_peers; item != NULL; item = item->link) {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100386 peer_t *p = (peer_t *) item->data;
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100387 p->next_action_time -= (time_t)offset;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100388 }
389
390 bail:
391 if (option_mask32 & OPT_q)
392 exit(0);
393}
394
395
396/* Time is periodically slewed when we collect enough
397 * good data points.
398 */
Adam Tkacb1585062009-11-22 03:43:55 +0100399static int
Denys Vlasenko386960a2009-12-02 01:51:24 +0100400compare_offsets(const void *aa, const void *bb)
Adam Tkacb1585062009-11-22 03:43:55 +0100401{
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100402 const peer_t *const *a = aa;
403 const peer_t *const *b = bb;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100404 if ((*a)->update.d_offset < (*b)->update.d_offset)
Adam Tkacb1585062009-11-22 03:43:55 +0100405 return -1;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100406 return ((*a)->update.d_offset > (*b)->update.d_offset);
Adam Tkacb1585062009-11-22 03:43:55 +0100407}
Denys Vlasenko907647f2009-12-02 23:17:45 +0100408static unsigned
Adam Tkacb1585062009-11-22 03:43:55 +0100409updated_scale(double offset)
410{
411 if (offset < 0)
412 offset = -offset;
Adam Tkacb1585062009-11-22 03:43:55 +0100413 if (offset > QSCALE_OFF_MAX)
414 return 1;
415 if (offset < QSCALE_OFF_MIN)
416 return QSCALE_OFF_MAX / QSCALE_OFF_MIN;
417 return QSCALE_OFF_MAX / offset;
418}
Adam Tkacb1585062009-11-22 03:43:55 +0100419static void
Denys Vlasenko386960a2009-12-02 01:51:24 +0100420slew_time(void)
Adam Tkacb1585062009-11-22 03:43:55 +0100421{
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100422 llist_t *item;
423 double offset_median;
424 struct timeval tv;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100425
Denys Vlasenko386960a2009-12-02 01:51:24 +0100426 {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100427 peer_t **peers = xzalloc(sizeof(peers[0]) * G.peer_cnt);
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100428 unsigned goodpeer_cnt = 0;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100429 unsigned middle;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100430
431 for (item = G.ntp_peers; item != NULL; item = item->link) {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100432 peer_t *p = (peer_t *) item->data;
433 if (p->p_trustlevel < TRUSTLEVEL_BADPEER)
Denys Vlasenko386960a2009-12-02 01:51:24 +0100434 continue;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100435 if (!p->update.d_good) {
Denys Vlasenko907647f2009-12-02 23:17:45 +0100436 free(peers);
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100437 return;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100438 }
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100439 peers[goodpeer_cnt++] = p;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100440 }
441
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100442 if (goodpeer_cnt == 0) {
443 free(peers);
444 goto clear_good;
445 }
Denys Vlasenko386960a2009-12-02 01:51:24 +0100446
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100447 qsort(peers, goodpeer_cnt, sizeof(peers[0]), compare_offsets);
448
449 middle = goodpeer_cnt / 2;
450 if (middle != 0 && (goodpeer_cnt & 1) == 0) {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100451 offset_median = (peers[middle-1]->update.d_offset + peers[middle]->update.d_offset) / 2;
452 G.rootdelay = (peers[middle-1]->update.d_delay + peers[middle]->update.d_delay) / 2;
453 G.stratum = 1 + MAX(peers[middle-1]->update.d_stratum, peers[middle]->update.d_stratum);
Denys Vlasenko386960a2009-12-02 01:51:24 +0100454 } else {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100455 offset_median = peers[middle]->update.d_offset;
456 G.rootdelay = peers[middle]->update.d_delay;
457 G.stratum = 1 + peers[middle]->update.d_stratum;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100458 }
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100459 G.leap = peers[middle]->update.d_leap;
460 G.refid4 = peers[middle]->update.d_refid4;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100461 G.refid =
462#if ENABLE_FEATURE_IPV6
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100463 peers[middle]->p_lsa->u.sa.sa_family != AF_INET ?
Denys Vlasenko386960a2009-12-02 01:51:24 +0100464 G.refid4 :
465#endif
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100466 peers[middle]->p_lsa->u.sin.sin_addr.s_addr;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100467 free(peers);
Adam Tkacb1585062009-11-22 03:43:55 +0100468 }
Denys Vlasenko907647f2009-12-02 23:17:45 +0100469//TODO: if (offset_median > BIG) step_time(offset_median)?
Adam Tkacb1585062009-11-22 03:43:55 +0100470
Denys Vlasenko907647f2009-12-02 23:17:45 +0100471 G.scale = updated_scale(offset_median);
472
473 bb_error_msg("adjusting clock by %fs, our stratum is %u, time scale %u",
474 offset_median, G.stratum, G.scale);
Adam Tkacb1585062009-11-22 03:43:55 +0100475
Denys Vlasenkofae9f492009-12-01 02:32:01 +0100476 errno = 0;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100477 d_to_tv(offset_median, &tv);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100478 if (adjtime(&tv, &tv) == -1)
479 bb_perror_msg_and_die("adjtime failed");
480 if (G.verbose >= 2)
481 bb_error_msg("old adjust: %d.%06u", (int)tv.tv_sec, (unsigned)tv.tv_usec);
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100482
Denys Vlasenko907647f2009-12-02 23:17:45 +0100483 if (G.first_adj_done) {
484 uint8_t synced = (tv.tv_sec == 0 && tv.tv_usec == 0);
485 if (synced != G.synced) {
486 G.synced = synced;
487 bb_error_msg("clock is %ssynced", synced ? "" : "un");
488 }
489 }
490 G.first_adj_done = 1;
491
492 G.reftime = gettime1900d();
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100493
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100494 clear_good:
Adam Tkacb1585062009-11-22 03:43:55 +0100495 for (item = G.ntp_peers; item != NULL; item = item->link) {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100496 peer_t *p = (peer_t *) item->data;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100497 p->update.d_good = 0;
Adam Tkacb1585062009-11-22 03:43:55 +0100498 }
499}
500
501static void
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100502update_peer_data(peer_t *p)
Adam Tkacb1585062009-11-22 03:43:55 +0100503{
Denys Vlasenko386960a2009-12-02 01:51:24 +0100504 /* Clock filter.
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100505 * Find the datapoint with the lowest delay.
Denys Vlasenko386960a2009-12-02 01:51:24 +0100506 * Use that as the peer update.
507 * Invalidate it and all older ones.
Adam Tkacb1585062009-11-22 03:43:55 +0100508 */
Denys Vlasenko386960a2009-12-02 01:51:24 +0100509 int i;
Denys Vlasenko4bd51892009-12-02 13:43:06 +0100510 int best = -1;
511 int good = 0;
Adam Tkacb1585062009-11-22 03:43:55 +0100512
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100513 for (i = 0; i < NUM_DATAPOINTS; i++) {
514 if (p->p_datapoint[i].d_good) {
Adam Tkacb1585062009-11-22 03:43:55 +0100515 good++;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100516 if (best < 0 || p->p_datapoint[i].d_delay < p->p_datapoint[best].d_delay)
Adam Tkacb1585062009-11-22 03:43:55 +0100517 best = i;
518 }
519 }
520
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100521 if (good < 8) //FIXME: was it meant to be NUM_DATAPOINTS, not 8?
Adam Tkacb1585062009-11-22 03:43:55 +0100522 return;
523
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100524 p->update = p->p_datapoint[best]; /* struct copy */
Denys Vlasenko386960a2009-12-02 01:51:24 +0100525 slew_time();
Adam Tkacb1585062009-11-22 03:43:55 +0100526
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100527 for (i = 0; i < NUM_DATAPOINTS; i++)
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100528 if (p->p_datapoint[i].d_rcv_time <= p->p_datapoint[best].d_rcv_time)
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100529 p->p_datapoint[i].d_good = 0;
Adam Tkacb1585062009-11-22 03:43:55 +0100530}
531
Denys Vlasenko386960a2009-12-02 01:51:24 +0100532static unsigned
533scale_interval(unsigned requested)
Adam Tkacb1585062009-11-22 03:43:55 +0100534{
Denys Vlasenko386960a2009-12-02 01:51:24 +0100535 unsigned interval, r;
Adam Tkacb1585062009-11-22 03:43:55 +0100536 interval = requested * G.scale;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100537 r = (unsigned)random() % (unsigned)(MAX(5, interval / 10));
Adam Tkacb1585062009-11-22 03:43:55 +0100538 return (interval + r);
539}
Adam Tkacb1585062009-11-22 03:43:55 +0100540static void
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100541recv_and_process_peer_pkt(peer_t *p)
Adam Tkacb1585062009-11-22 03:43:55 +0100542{
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100543 ssize_t size;
544 msg_t msg;
545 double T1, T2, T3, T4;
546 unsigned interval;
547 datapoint_t *datapoint;
Adam Tkacb1585062009-11-22 03:43:55 +0100548
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100549 /* We can recvfrom here and check from.IP, but some multihomed
550 * ntp servers reply from their *other IP*.
551 * TODO: maybe we should check at least what we can: from.port == 123?
552 */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100553 size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
Adam Tkacb1585062009-11-22 03:43:55 +0100554 if (size == -1) {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100555 bb_perror_msg("recv(%s) error", p->p_dotted);
Adam Tkacb1585062009-11-22 03:43:55 +0100556 if (errno == EHOSTUNREACH || errno == EHOSTDOWN
557 || errno == ENETUNREACH || errno == ENETDOWN
558 || errno == ECONNREFUSED || errno == EADDRNOTAVAIL
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100559 || errno == EAGAIN
Adam Tkacb1585062009-11-22 03:43:55 +0100560 ) {
561//TODO: always do this?
562 set_next(p, error_interval());
Denys Vlasenko386960a2009-12-02 01:51:24 +0100563 goto close_sock;
Adam Tkacb1585062009-11-22 03:43:55 +0100564 }
565 xfunc_die();
566 }
567
Adam Tkacb1585062009-11-22 03:43:55 +0100568 if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100569 bb_error_msg("malformed packet received from %s", p->p_dotted);
Adam Tkacb1585062009-11-22 03:43:55 +0100570 goto bail;
571 }
572
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100573 if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl
574 || msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl
Adam Tkacb1585062009-11-22 03:43:55 +0100575 ) {
576 goto bail;
577 }
578
Denys Vlasenko386960a2009-12-02 01:51:24 +0100579 if ((msg.m_status & LI_ALARM) == LI_ALARM
580 || msg.m_stratum == 0
581 || msg.m_stratum > NTP_MAXSTRATUM
Adam Tkacb1585062009-11-22 03:43:55 +0100582 ) {
Denys Vlasenkoe99c8d22009-12-17 12:17:41 +0100583// TODO: stratum 0 responses may have commands in 32-bit m_refid field:
584// "DENY", "RSTR" - peer does not like us at all
585// "RATE" - peer is overloaded, reduce polling freq
Adam Tkacb1585062009-11-22 03:43:55 +0100586 interval = error_interval();
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100587 bb_error_msg("reply from %s: not synced, next query in %us", p->p_dotted, interval);
Denys Vlasenko386960a2009-12-02 01:51:24 +0100588 goto close_sock;
Adam Tkacb1585062009-11-22 03:43:55 +0100589 }
590
591 /*
592 * From RFC 2030 (with a correction to the delay math):
593 *
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100594 * Timestamp Name ID When Generated
595 * ------------------------------------------------------------
596 * Originate Timestamp T1 time request sent by client
597 * Receive Timestamp T2 time request received by server
598 * Transmit Timestamp T3 time reply sent by server
599 * Destination Timestamp T4 time reply received by client
Adam Tkacb1585062009-11-22 03:43:55 +0100600 *
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100601 * The roundtrip delay and local clock offset are defined as
Adam Tkacb1585062009-11-22 03:43:55 +0100602 *
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100603 * delay = (T4 - T1) - (T3 - T2); offset = ((T2 - T1) + (T3 - T4)) / 2
Adam Tkacb1585062009-11-22 03:43:55 +0100604 */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100605 T1 = p->p_xmttime;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100606 T2 = lfp_to_d(msg.m_rectime);
607 T3 = lfp_to_d(msg.m_xmttime);
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100608 T4 = gettime1900d();
Adam Tkacb1585062009-11-22 03:43:55 +0100609
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100610 datapoint = &p->p_datapoint[p->p_datapoint_idx];
Adam Tkacb1585062009-11-22 03:43:55 +0100611
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100612 datapoint->d_offset = ((T2 - T1) + (T3 - T4)) / 2;
613 datapoint->d_delay = (T4 - T1) - (T3 - T2);
614 if (datapoint->d_delay < 0) {
615 bb_error_msg("reply from %s: negative delay %f", p->p_dotted, datapoint->d_delay);
Adam Tkacb1585062009-11-22 03:43:55 +0100616 interval = error_interval();
617 set_next(p, interval);
Denys Vlasenko386960a2009-12-02 01:51:24 +0100618 goto close_sock;
Adam Tkacb1585062009-11-22 03:43:55 +0100619 }
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100620 //UNUSED: datapoint->d_error = (T2 - T1) - (T3 - T4);
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100621 datapoint->d_rcv_time = (time_t)(T4 - OFFSET_1900_1970); /* = time(NULL); */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100622 datapoint->d_good = 1;
Adam Tkacb1585062009-11-22 03:43:55 +0100623
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100624 datapoint->d_leap = (msg.m_status & LI_MASK);
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100625 //UNUSED: datapoint->o_precision = msg.m_precision_exp;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100626 //UNUSED: datapoint->o_rootdelay = sfp_to_d(msg.m_rootdelay);
627 //UNUSED: datapoint->o_rootdispersion = sfp_to_d(msg.m_dispersion);
628 //UNUSED: datapoint->d_refid = ntohl(msg.m_refid);
629 datapoint->d_refid4 = msg.m_xmttime.fractionl;
630 //UNUSED: datapoint->o_reftime = lfp_to_d(msg.m_reftime);
631 //UNUSED: datapoint->o_poll = msg.m_ppoll;
632 datapoint->d_stratum = msg.m_stratum;
Adam Tkacb1585062009-11-22 03:43:55 +0100633
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100634 if (p->p_trustlevel < TRUSTLEVEL_PATHETIC)
Adam Tkacb1585062009-11-22 03:43:55 +0100635 interval = scale_interval(INTERVAL_QUERY_PATHETIC);
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100636 else if (p->p_trustlevel < TRUSTLEVEL_AGRESSIVE)
Adam Tkacb1585062009-11-22 03:43:55 +0100637 interval = scale_interval(INTERVAL_QUERY_AGRESSIVE);
638 else
639 interval = scale_interval(INTERVAL_QUERY_NORMAL);
640
641 set_next(p, interval);
Adam Tkacb1585062009-11-22 03:43:55 +0100642
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100643 /* Every received reply which we do not discard increases trust */
644 if (p->p_trustlevel < TRUSTLEVEL_MAX) {
645 p->p_trustlevel++;
646 if (p->p_trustlevel == TRUSTLEVEL_BADPEER)
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100647 bb_error_msg("peer %s now valid", p->p_dotted);
Adam Tkacb1585062009-11-22 03:43:55 +0100648 }
649
Denys Vlasenko386960a2009-12-02 01:51:24 +0100650 if (G.verbose)
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100651 bb_error_msg("reply from %s: offset %f delay %f, next query in %us", p->p_dotted,
652 datapoint->d_offset, datapoint->d_delay, interval);
Adam Tkacb1585062009-11-22 03:43:55 +0100653
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100654 update_peer_data(p);
Denys Vlasenkof91e63c2009-12-02 02:30:31 +0100655//TODO: do it after all peers had a chance to return at least one reply?
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100656 step_time_once(datapoint->d_offset);
Adam Tkacb1585062009-11-22 03:43:55 +0100657
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100658 p->p_datapoint_idx++;
659 if (p->p_datapoint_idx >= NUM_DATAPOINTS)
660 p->p_datapoint_idx = 0;
Adam Tkacb1585062009-11-22 03:43:55 +0100661
Denys Vlasenko386960a2009-12-02 01:51:24 +0100662 close_sock:
Denys Vlasenko907647f2009-12-02 23:17:45 +0100663 /* We do not expect any more packets from this peer for now.
Denys Vlasenko386960a2009-12-02 01:51:24 +0100664 * Closing the socket informs kernel about it.
665 * We open a new socket when we send a new query.
666 */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100667 close(p->p_fd);
668 p->p_fd = -1;
Adam Tkacb1585062009-11-22 03:43:55 +0100669 bail:
Denys Vlasenko386960a2009-12-02 01:51:24 +0100670 return;
Adam Tkacb1585062009-11-22 03:43:55 +0100671}
672
673#if ENABLE_FEATURE_NTPD_SERVER
674static void
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100675recv_and_process_client_pkt(void /*int fd*/)
Adam Tkacb1585062009-11-22 03:43:55 +0100676{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100677 ssize_t size;
678 uint8_t version;
679 double rectime;
680 len_and_sockaddr *to;
681 struct sockaddr *from;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100682 msg_t msg;
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100683 uint8_t query_status;
684 uint8_t query_ppoll;
685 l_fixedpt_t query_xmttime;
Adam Tkacb1585062009-11-22 03:43:55 +0100686
687 to = get_sock_lsa(G.listen_fd);
688 from = xzalloc(to->len);
689
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100690 size = recv_from_to(G.listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len);
Adam Tkacb1585062009-11-22 03:43:55 +0100691 if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100692 char *addr;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100693 if (size < 0) {
694 if (errno == EAGAIN)
695 goto bail;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100696 bb_perror_msg_and_die("recv");
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100697 }
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100698 addr = xmalloc_sockaddr2dotted_noport(from);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100699 bb_error_msg("malformed packet received from %s: size %u", addr, (int)size);
Adam Tkacb1585062009-11-22 03:43:55 +0100700 free(addr);
701 goto bail;
702 }
703
Denys Vlasenko386960a2009-12-02 01:51:24 +0100704 query_status = msg.m_status;
705 query_ppoll = msg.m_ppoll;
706 query_xmttime = msg.m_xmttime;
Adam Tkacb1585062009-11-22 03:43:55 +0100707
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100708 /* Build a reply packet */
709 memset(&msg, 0, sizeof(msg));
Denys Vlasenko386960a2009-12-02 01:51:24 +0100710 msg.m_status = G.synced ? G.leap : LI_ALARM;
711 msg.m_status |= (query_status & VERSION_MASK);
712 msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ?
Denys Vlasenko69675782013-01-14 01:34:48 +0100713 MODE_SERVER : MODE_SYM_PAS;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100714 msg.m_stratum = G.stratum;
715 msg.m_ppoll = query_ppoll;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100716 msg.m_precision_exp = G_precision_exp;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100717 rectime = gettime1900d();
Denys Vlasenko386960a2009-12-02 01:51:24 +0100718 msg.m_xmttime = msg.m_rectime = d_to_lfp(rectime);
719 msg.m_reftime = d_to_lfp(G.reftime);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100720 //msg.m_xmttime = d_to_lfp(gettime1900d()); // = msg.m_rectime
Denys Vlasenko386960a2009-12-02 01:51:24 +0100721 msg.m_orgtime = query_xmttime;
722 msg.m_rootdelay = d_to_sfp(G.rootdelay);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100723 version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */
Denys Vlasenko386960a2009-12-02 01:51:24 +0100724 msg.m_refid = (version > (3 << VERSION_SHIFT)) ? G.refid4 : G.refid;
Adam Tkacb1585062009-11-22 03:43:55 +0100725
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100726 /* We reply from the local address packet was sent to,
Adam Tkacb1585062009-11-22 03:43:55 +0100727 * this makes to/from look swapped here: */
Denys Vlasenko386960a2009-12-02 01:51:24 +0100728 do_sendto(G.listen_fd,
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100729 /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len,
730 &msg, size);
Adam Tkacb1585062009-11-22 03:43:55 +0100731
732 bail:
733 free(to);
734 free(from);
735}
736#endif
737
738/* Upstream ntpd's options:
739 *
740 * -4 Force DNS resolution of host names to the IPv4 namespace.
741 * -6 Force DNS resolution of host names to the IPv6 namespace.
742 * -a Require cryptographic authentication for broadcast client,
743 * multicast client and symmetric passive associations.
744 * This is the default.
745 * -A Do not require cryptographic authentication for broadcast client,
746 * multicast client and symmetric passive associations.
747 * This is almost never a good idea.
748 * -b Enable the client to synchronize to broadcast servers.
749 * -c conffile
750 * Specify the name and path of the configuration file,
751 * default /etc/ntp.conf
752 * -d Specify debugging mode. This option may occur more than once,
753 * with each occurrence indicating greater detail of display.
754 * -D level
755 * Specify debugging level directly.
756 * -f driftfile
757 * Specify the name and path of the frequency file.
758 * This is the same operation as the "driftfile FILE"
759 * configuration command.
760 * -g Normally, ntpd exits with a message to the system log
761 * if the offset exceeds the panic threshold, which is 1000 s
762 * by default. This option allows the time to be set to any value
763 * without restriction; however, this can happen only once.
764 * If the threshold is exceeded after that, ntpd will exit
765 * with a message to the system log. This option can be used
766 * with the -q and -x options. See the tinker command for other options.
767 * -i jaildir
768 * Chroot the server to the directory jaildir. This option also implies
769 * that the server attempts to drop root privileges at startup
770 * (otherwise, chroot gives very little additional security).
771 * You may need to also specify a -u option.
772 * -k keyfile
773 * Specify the name and path of the symmetric key file,
774 * default /etc/ntp/keys. This is the same operation
775 * as the "keys FILE" configuration command.
776 * -l logfile
777 * Specify the name and path of the log file. The default
778 * is the system log file. This is the same operation as
779 * the "logfile FILE" configuration command.
780 * -L Do not listen to virtual IPs. The default is to listen.
781 * -n Don't fork.
782 * -N To the extent permitted by the operating system,
783 * run the ntpd at the highest priority.
784 * -p pidfile
785 * Specify the name and path of the file used to record the ntpd
786 * process ID. This is the same operation as the "pidfile FILE"
787 * configuration command.
788 * -P priority
789 * To the extent permitted by the operating system,
790 * run the ntpd at the specified priority.
791 * -q Exit the ntpd just after the first time the clock is set.
792 * This behavior mimics that of the ntpdate program, which is
793 * to be retired. The -g and -x options can be used with this option.
794 * Note: The kernel time discipline is disabled with this option.
795 * -r broadcastdelay
796 * Specify the default propagation delay from the broadcast/multicast
797 * server to this client. This is necessary only if the delay
798 * cannot be computed automatically by the protocol.
799 * -s statsdir
800 * Specify the directory path for files created by the statistics
801 * facility. This is the same operation as the "statsdir DIR"
802 * configuration command.
803 * -t key
804 * Add a key number to the trusted key list. This option can occur
805 * more than once.
806 * -u user[:group]
807 * Specify a user, and optionally a group, to switch to.
808 * -v variable
809 * -V variable
810 * Add a system variable listed by default.
811 * -x Normally, the time is slewed if the offset is less than the step
812 * threshold, which is 128 ms by default, and stepped if above
813 * the threshold. This option sets the threshold to 600 s, which is
814 * well within the accuracy window to set the clock manually.
815 * Note: since the slew rate of typical Unix kernels is limited
816 * to 0.5 ms/s, each second of adjustment requires an amortization
817 * interval of 2000 s. Thus, an adjustment as much as 600 s
818 * will take almost 14 days to complete. This option can be used
819 * with the -g and -q options. See the tinker command for other options.
820 * Note: The kernel time discipline is disabled with this option.
821 */
822
Adam Tkacb1585062009-11-22 03:43:55 +0100823/* By doing init in a separate function we decrease stack usage
824 * in main loop.
825 */
826static NOINLINE void ntp_init(char **argv)
827{
828 unsigned opts;
829 llist_t *peers;
830
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100831 srandom(getpid());
Adam Tkacb1585062009-11-22 03:43:55 +0100832
833 if (getuid())
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100834 bb_error_msg_and_die(bb_msg_you_must_be_root);
Adam Tkacb1585062009-11-22 03:43:55 +0100835
836 peers = NULL;
837 opt_complementary = "dd:p::"; /* d: counter, p: list */
838 opts = getopt32(argv,
Denys Vlasenko907647f2009-12-02 23:17:45 +0100839 "nqNx" /* compat */
Adam Tkacb1585062009-11-22 03:43:55 +0100840 "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
841 "d" /* compat */
Denys Vlasenko907647f2009-12-02 23:17:45 +0100842 "46aAbgL", /* compat, ignored */
Adam Tkacb1585062009-11-22 03:43:55 +0100843 &peers, &G.verbose);
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100844 if (!(opts & (OPT_p|OPT_l)))
845 bb_show_usage();
Denys Vlasenko907647f2009-12-02 23:17:45 +0100846 if (opts & OPT_x) /* disable stepping, only slew is allowed */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100847 G.time_was_stepped = 1;
Denys Vlasenko160b9ca2009-11-27 02:35:15 +0100848 while (peers)
849 add_peers(llist_pop(&peers));
850 if (!(opts & OPT_n)) {
851 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
852 logmode = LOGMODE_NONE;
853 }
Adam Tkacb1585062009-11-22 03:43:55 +0100854#if ENABLE_FEATURE_NTPD_SERVER
855 G.listen_fd = -1;
856 if (opts & OPT_l) {
857 G.listen_fd = create_and_bind_dgram_or_die(NULL, 123);
858 socket_want_pktinfo(G.listen_fd);
859 setsockopt(G.listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
860 }
861#endif
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100862 /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */
863 if (opts & OPT_N)
864 setpriority(PRIO_PROCESS, 0, -15);
Adam Tkacb1585062009-11-22 03:43:55 +0100865
866 /* Set some globals */
Denys Vlasenko907647f2009-12-02 23:17:45 +0100867#if 0
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100868 /* With constant b = 100, G.precision_exp is also constant -6.
Denys Vlasenko907647f2009-12-02 23:17:45 +0100869 * Uncomment this and you'll see */
Adam Tkacb1585062009-11-22 03:43:55 +0100870 {
871 int prec = 0;
872 int b;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100873# if 0
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200874 struct timespec tp;
Adam Tkacb1585062009-11-22 03:43:55 +0100875 /* We can use sys_clock_getres but assuming 10ms tick should be fine */
876 clock_getres(CLOCK_REALTIME, &tp);
877 tp.tv_sec = 0;
878 tp.tv_nsec = 10000000;
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100879 b = 1000000000 / tp.tv_nsec; /* convert to Hz */
Denys Vlasenko907647f2009-12-02 23:17:45 +0100880# else
Adam Tkacb1585062009-11-22 03:43:55 +0100881 b = 100; /* b = 1000000000/10000000 = 100 */
Denys Vlasenko907647f2009-12-02 23:17:45 +0100882# endif
Adam Tkacb1585062009-11-22 03:43:55 +0100883 while (b > 1)
884 prec--, b >>= 1;
Denys Vlasenko6879a7a2009-12-18 18:50:29 +0100885 //G.precision_exp = prec;
886 bb_error_msg("G.precision_exp:%d", prec); /* -6 */
Adam Tkacb1585062009-11-22 03:43:55 +0100887 }
Denys Vlasenko907647f2009-12-02 23:17:45 +0100888#endif
Adam Tkacb1585062009-11-22 03:43:55 +0100889 G.scale = 1;
Adam Tkacb1585062009-11-22 03:43:55 +0100890
891 bb_signals((1 << SIGTERM) | (1 << SIGINT), record_signo);
892 bb_signals((1 << SIGPIPE) | (1 << SIGHUP), SIG_IGN);
893}
894
895int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
896int ntpd_main(int argc UNUSED_PARAM, char **argv)
897{
898 struct globals g;
Adam Tkacb1585062009-11-22 03:43:55 +0100899 struct pollfd *pfd;
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100900 peer_t **idx2peer;
Adam Tkacb1585062009-11-22 03:43:55 +0100901
902 memset(&g, 0, sizeof(g));
903 SET_PTR_TO_GLOBALS(&g);
904
905 ntp_init(argv);
906
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100907 {
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100908 /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100909 unsigned cnt = g.peer_cnt + ENABLE_FEATURE_NTPD_SERVER;
910 idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt);
911 pfd = xzalloc(sizeof(pfd[0]) * cnt);
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100912 }
Adam Tkacb1585062009-11-22 03:43:55 +0100913
914 while (!bb_got_signal) {
915 llist_t *item;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100916 unsigned i, j;
Adam Tkacb1585062009-11-22 03:43:55 +0100917 unsigned sent_cnt, trial_cnt;
918 int nfds, timeout;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100919 time_t cur_time, nextaction;
Adam Tkacb1585062009-11-22 03:43:55 +0100920
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100921 /* Nothing between here and poll() blocks for any significant time */
922
923 cur_time = time(NULL);
924 nextaction = cur_time + 3600;
Adam Tkacb1585062009-11-22 03:43:55 +0100925
926 i = 0;
927#if ENABLE_FEATURE_NTPD_SERVER
928 if (g.listen_fd != -1) {
929 pfd[0].fd = g.listen_fd;
930 pfd[0].events = POLLIN;
931 i++;
932 }
933#endif
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100934 /* Pass over peer list, send requests, time out on receives */
Adam Tkacb1585062009-11-22 03:43:55 +0100935 sent_cnt = trial_cnt = 0;
936 for (item = g.ntp_peers; item != NULL; item = item->link) {
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100937 peer_t *p = (peer_t *) item->data;
Adam Tkacb1585062009-11-22 03:43:55 +0100938
Denys Vlasenko907647f2009-12-02 23:17:45 +0100939 /* Overflow-safe "if (p->next_action_time <= cur_time) ..." */
940 if ((int)(cur_time - p->next_action_time) >= 0) {
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100941 if (p->p_fd == -1) {
Denys Vlasenko907647f2009-12-02 23:17:45 +0100942 /* Time to send new req */
943 trial_cnt++;
944 if (send_query_to_peer(p) == 0)
945 sent_cnt++;
946 } else {
947 /* Timed out waiting for reply */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100948 close(p->p_fd);
949 p->p_fd = -1;
Denys Vlasenko907647f2009-12-02 23:17:45 +0100950 timeout = error_interval();
951 bb_error_msg("timed out waiting for %s, "
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100952 "next query in %us", p->p_dotted, timeout);
Denys Vlasenkod2fe69f2009-12-30 18:38:05 +0100953 if (p->p_trustlevel >= TRUSTLEVEL_BADPEER) {
954 p->p_trustlevel /= 2;
955 if (p->p_trustlevel < TRUSTLEVEL_BADPEER)
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100956 bb_error_msg("peer %s now invalid", p->p_dotted);
Denys Vlasenko907647f2009-12-02 23:17:45 +0100957 }
958 set_next(p, timeout);
Adam Tkacb1585062009-11-22 03:43:55 +0100959 }
Adam Tkacb1585062009-11-22 03:43:55 +0100960 }
961
Denys Vlasenko907647f2009-12-02 23:17:45 +0100962 if (p->next_action_time < nextaction)
963 nextaction = p->next_action_time;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100964
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100965 if (p->p_fd >= 0) {
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100966 /* Wait for reply from this peer */
Denys Vlasenkoafa2d332009-12-17 23:29:33 +0100967 pfd[i].fd = p->p_fd;
Adam Tkacb1585062009-11-22 03:43:55 +0100968 pfd[i].events = POLLIN;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100969 idx2peer[i] = p;
Adam Tkacb1585062009-11-22 03:43:55 +0100970 i++;
971 }
972 }
973
Denys Vlasenko8d580c72009-11-23 16:27:16 +0100974 if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
Denys Vlasenkofae9f492009-12-01 02:32:01 +0100975 step_time_once(0); /* no good peers, don't wait */
Adam Tkacb1585062009-11-22 03:43:55 +0100976
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100977 timeout = nextaction - cur_time;
Denys Vlasenko386960a2009-12-02 01:51:24 +0100978 if (timeout < 1)
979 timeout = 1;
Adam Tkacb1585062009-11-22 03:43:55 +0100980
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100981 /* Here we may block */
Denys Vlasenko386960a2009-12-02 01:51:24 +0100982 if (g.verbose >= 2)
Denys Vlasenko907647f2009-12-02 23:17:45 +0100983 bb_error_msg("poll %us, sockets:%u", timeout, i);
Adam Tkacb1585062009-11-22 03:43:55 +0100984 nfds = poll(pfd, i, timeout * 1000);
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100985 if (nfds <= 0)
986 continue;
Adam Tkacb1585062009-11-22 03:43:55 +0100987
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100988 /* Process any received packets */
Adam Tkacb1585062009-11-22 03:43:55 +0100989 j = 0;
990#if ENABLE_FEATURE_NTPD_SERVER
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100991 if (g.listen_fd != -1) {
992 if (pfd[0].revents /* & (POLLIN|POLLERR)*/) {
Adam Tkacb1585062009-11-22 03:43:55 +0100993 nfds--;
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100994 recv_and_process_client_pkt(/*g.listen_fd*/);
Adam Tkacb1585062009-11-22 03:43:55 +0100995 }
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100996 j = 1;
Adam Tkacb1585062009-11-22 03:43:55 +0100997 }
998#endif
Denys Vlasenko363e89b2009-11-24 14:04:15 +0100999 for (; nfds != 0 && j < i; j++) {
1000 if (pfd[j].revents /* & (POLLIN|POLLERR)*/) {
Adam Tkacb1585062009-11-22 03:43:55 +01001001 nfds--;
Denys Vlasenko363e89b2009-11-24 14:04:15 +01001002 recv_and_process_peer_pkt(idx2peer[j]);
Adam Tkacb1585062009-11-22 03:43:55 +01001003 }
1004 }
1005 } /* while (!bb_got_signal) */
1006
1007 kill_myself_with_sig(bb_got_signal);
1008}