8ee42e62735db0d08e8082f6e612e2e9cd6361dc
[oweals/busybox.git] / networking / ntpd.c
1 /*
2  * NTP client/server, based on OpenNTPD 3.9p1
3  *
4  * Author: Adam Tkac <vonsch@gmail.com>
5  *
6  * Licensed under GPLv2, see file LICENSE in this tarball for details.
7  */
8
9 #include "libbb.h"
10 #include <netinet/ip.h> /* For IPTOS_LOWDELAY definition */
11
12 #ifndef IP_PKTINFO
13 # error "Sorry, your kernel has to support IP_PKTINFO"
14 #endif
15
16 #define INTERVAL_QUERY_NORMAL           30      /* sync to peers every n secs */
17 #define INTERVAL_QUERY_PATHETIC         60
18 #define INTERVAL_QUERY_AGRESSIVE        5
19
20 #define TRUSTLEVEL_BADPEER              6
21 #define TRUSTLEVEL_PATHETIC             2
22 #define TRUSTLEVEL_AGRESSIVE            8
23 #define TRUSTLEVEL_MAX                  10
24
25 #define QSCALE_OFF_MIN                  0.05
26 #define QSCALE_OFF_MAX                  0.50
27
28 #define QUERYTIME_MAX           15      /* single query might take n secs max */
29 #define OFFSET_ARRAY_SIZE       8
30 #define SETTIME_MIN_OFFSET      180     /* min offset for settime at start */
31 #define SETTIME_TIMEOUT         15      /* max seconds to wait with -s */
32
33 /* Style borrowed from NTP ref/tcpdump and updated for SNTPv4 (RFC2030). */
34
35 /*
36  * RFC Section 3
37  *
38  *    0                   1                   2                   3
39  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *   |                         Integer Part                          |
42  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  *   |                         Fraction Part                         |
44  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  *
46  *    0                   1                   2                   3
47  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
48  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49  *   |            Integer Part       |     Fraction Part             |
50  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 */
52 typedef struct {
53         uint32_t int_partl;
54         uint32_t fractionl;
55 } l_fixedpt_t;
56
57 typedef struct {
58         uint16_t int_parts;
59         uint16_t fractions;
60 } s_fixedpt_t;
61
62 #define NTP_DIGESTSIZE          16
63 #define NTP_MSGSIZE_NOAUTH      48
64 #define NTP_MSGSIZE             (NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE)
65
66 typedef struct {
67         uint8_t status; /* status of local clock and leap info */
68         uint8_t stratum;        /* Stratum level */
69         uint8_t ppoll;          /* poll value */
70         int8_t precision;
71         s_fixedpt_t rootdelay;
72         s_fixedpt_t dispersion;
73         uint32_t refid;
74         l_fixedpt_t reftime;
75         l_fixedpt_t orgtime;
76         l_fixedpt_t rectime;
77         l_fixedpt_t xmttime;
78         uint32_t keyid;
79         uint8_t digest[NTP_DIGESTSIZE];
80 } ntp_msg_t;
81
82 typedef struct {
83         int                     fd;
84         ntp_msg_t               msg;
85         double                  xmttime;
86 } ntp_query_t;
87
88 enum {
89         NTP_VERSION     = 4,
90         NTP_MAXSTRATUM  = 15,
91         /* Leap Second Codes (high order two bits) */
92         LI_NOWARNING    = (0 << 6),     /* no warning */
93         LI_PLUSSEC      = (1 << 6),     /* add a second (61 seconds) */
94         LI_MINUSSEC     = (2 << 6),     /* minus a second (59 seconds) */
95         LI_ALARM        = (3 << 6),     /* alarm condition */
96
97         /* Status Masks */
98         MODE_MASK       = (7 << 0),
99         VERSION_MASK    = (7 << 3),
100         LI_MASK         = (3 << 6),
101
102         /* Mode values */
103         MODE_RES0       = 0,    /* reserved */
104         MODE_SYM_ACT    = 1,    /* symmetric active */
105         MODE_SYM_PAS    = 2,    /* symmetric passive */
106         MODE_CLIENT     = 3,    /* client */
107         MODE_SERVER     = 4,    /* server */
108         MODE_BROADCAST  = 5,    /* broadcast */
109         MODE_RES1       = 6,    /* reserved for NTP control message */
110         MODE_RES2       = 7     /* reserved for private use */
111 };
112
113 #define JAN_1970        2208988800UL    /* 1970 - 1900 in seconds */
114
115 enum client_state {
116         STATE_NONE,
117         STATE_QUERY_SENT,
118         STATE_REPLY_RECEIVED
119 };
120
121 typedef struct {
122         double          rootdelay;
123         double          rootdispersion;
124         double          reftime;
125         uint32_t        refid;
126         uint32_t        refid4;
127         uint8_t         synced;
128         uint8_t         leap;
129         int8_t          precision;
130         uint8_t         poll;
131         uint8_t         stratum;
132 } ntp_status_t;
133
134 typedef struct {
135         ntp_status_t            status;
136         double                  offset;
137         double                  delay;
138         double                  error;
139         time_t                  rcvd;
140         uint8_t                 good;
141 } ntp_offset_t;
142
143 typedef struct {
144         len_and_sockaddr        *lsa;
145         ntp_query_t              query;
146         ntp_offset_t             reply[OFFSET_ARRAY_SIZE];
147         ntp_offset_t             update;
148         enum client_state        state;
149         time_t                   next;
150         time_t                   deadline;
151         uint8_t                  shift;
152         uint8_t                  trustlevel;
153 } ntp_peer_t;
154
155 enum {
156         OPT_n = (1 << 0),
157         OPT_g = (1 << 1),
158         OPT_q = (1 << 2),
159         /* Insert new options above this line. */
160         /* Non-compat options: */
161         OPT_p = (1 << 3),
162         OPT_l = (1 << 4),
163 };
164
165
166 struct globals {
167         unsigned        verbose;
168 #if ENABLE_FEATURE_NTPD_SERVER
169         int             listen_fd;
170 #endif
171         llist_t         *ntp_peers;
172         ntp_status_t    status;
173         uint32_t        scale;
174         uint8_t         settime;
175         uint8_t         firstadj;
176         smallint        peer_cnt;
177 };
178 #define G (*ptr_to_globals)
179
180
181 static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
182
183
184 static void
185 set_next(ntp_peer_t *p, time_t t)
186 {
187         p->next = time(NULL) + t;
188         p->deadline = 0;
189 }
190
191 static void
192 add_peers(const char *s)
193 {
194         ntp_peer_t *p;
195
196         p = xzalloc(sizeof(*p));
197 //TODO: big ntpd uses all IPs, not just 1st, do we need to mimic that?
198         p->lsa = xhost2sockaddr(s, 123);
199         p->query.fd = -1;
200         p->query.msg.status = MODE_CLIENT | (NTP_VERSION << 3);
201         if (STATE_NONE != 0)
202                 p->state = STATE_NONE;
203         p->trustlevel = TRUSTLEVEL_PATHETIC;
204         p->query.fd = -1;
205         set_next(p, 0);
206
207         llist_add_to(&G.ntp_peers, p);
208         G.peer_cnt++;
209 }
210
211 static double
212 gettime(void)
213 {
214         struct timeval  tv;
215
216         gettimeofday(&tv, NULL); /* never fails */
217         return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
218 }
219
220
221 static void
222 d_to_tv(double d, struct timeval *tv)
223 {
224         tv->tv_sec = (long)d;
225         tv->tv_usec = (d - tv->tv_sec) * 1000000;
226 }
227
228 static double
229 lfp_to_d(l_fixedpt_t lfp)
230 {
231         double  ret;
232
233         lfp.int_partl = ntohl(lfp.int_partl);
234         lfp.fractionl = ntohl(lfp.fractionl);
235         ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
236         return ret;
237 }
238
239 #if ENABLE_FEATURE_NTPD_SERVER
240 static l_fixedpt_t
241 d_to_lfp(double d)
242 {
243         l_fixedpt_t     lfp;
244
245         lfp.int_partl = htonl((uint32_t)d);
246         lfp.fractionl = htonl((uint32_t)((d - (u_int32_t)d) * UINT_MAX));
247         return lfp;
248 }
249 #endif
250
251 static double
252 sfp_to_d(s_fixedpt_t sfp)
253 {
254         double  ret;
255
256         sfp.int_parts = ntohs(sfp.int_parts);
257         sfp.fractions = ntohs(sfp.fractions);
258         ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
259         return ret;
260 }
261
262 #if ENABLE_FEATURE_NTPD_SERVER
263 static s_fixedpt_t
264 d_to_sfp(double d)
265 {
266         s_fixedpt_t     sfp;
267
268         sfp.int_parts = htons((uint16_t)d);
269         sfp.fractions = htons((uint16_t)((d - (u_int16_t)d) * USHRT_MAX));
270         return sfp;
271 }
272 #endif
273
274 static void
275 set_deadline(ntp_peer_t *p, time_t t)
276 {
277         p->deadline = time(NULL) + t;
278         p->next = 0;
279 }
280
281 static time_t
282 error_interval(void)
283 {
284         time_t interval, r;
285
286         interval = INTERVAL_QUERY_PATHETIC * QSCALE_OFF_MAX / QSCALE_OFF_MIN;
287         r = random() % (interval / 10);
288         return (interval + r);
289 }
290
291 static int
292 sendmsg_wrap(int fd,
293                 const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen,
294                 ntp_msg_t *msg, ssize_t len)
295 {
296         ssize_t ret;
297
298         errno = 0;
299         if (!from) {
300                 ret = sendto(fd, msg, len, 0, to, addrlen);
301         } else {
302                 ret = send_to_from(fd, msg, len, 0, to, from, addrlen);
303         }
304         if (ret != len) {
305                 bb_perror_msg("send failed");
306                 return -1;
307         }
308         return 0;
309 }
310
311 static int
312 client_query(ntp_peer_t *p)
313 {
314         if (p->query.fd == -1) {
315                 p->query.fd = xsocket(p->lsa->u.sa.sa_family, SOCK_DGRAM, 0);
316 #if ENABLE_FEATURE_IPV6
317                 if (p->lsa->u.sa.sa_family == AF_INET)
318 #endif
319                         setsockopt(p->query.fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
320         }
321
322         /*
323          * Send out a random 64-bit number as our transmit time.  The NTP
324          * server will copy said number into the originate field on the
325          * response that it sends us.  This is totally legal per the SNTP spec.
326          *
327          * The impact of this is two fold: we no longer send out the current
328          * system time for the world to see (which may aid an attacker), and
329          * it gives us a (not very secure) way of knowing that we're not
330          * getting spoofed by an attacker that can't capture our traffic
331          * but can spoof packets from the NTP server we're communicating with.
332          *
333          * Save the real transmit timestamp locally.
334          */
335
336         p->query.msg.xmttime.int_partl = random();
337         p->query.msg.xmttime.fractionl = random();
338         p->query.xmttime = gettime();
339
340         if (sendmsg_wrap(p->query.fd, /*from:*/ NULL, /*to:*/ &p->lsa->u.sa, /*addrlen:*/ p->lsa->len,
341                         &p->query.msg, NTP_MSGSIZE_NOAUTH) == -1) {
342                 set_next(p, INTERVAL_QUERY_PATHETIC);
343                 return -1;
344         }
345
346         p->state = STATE_QUERY_SENT;
347         set_deadline(p, QUERYTIME_MAX);
348
349         return 0;
350 }
351
352 static int
353 offset_compare(const void *aa, const void *bb)
354 {
355         const ntp_peer_t * const *a;
356         const ntp_peer_t * const *b;
357
358         a = aa;
359         b = bb;
360
361         if ((*a)->update.offset < (*b)->update.offset)
362                 return -1;
363         return ((*a)->update.offset > (*b)->update.offset);
364 }
365
366 static uint32_t
367 updated_scale(double offset)
368 {
369         if (offset < 0)
370                 offset = -offset;
371
372         if (offset > QSCALE_OFF_MAX)
373                 return 1;
374         if (offset < QSCALE_OFF_MIN)
375                 return QSCALE_OFF_MAX / QSCALE_OFF_MIN;
376         return QSCALE_OFF_MAX / offset;
377 }
378
379 static void
380 adjtime_wrap(void)
381 {
382         ntp_peer_t       *p;
383         unsigned          offset_cnt;
384         int               i = 0;
385         ntp_peer_t      **peers;
386         double            offset_median;
387         llist_t          *item;
388         len_and_sockaddr *lsa;
389         struct timeval    tv, olddelta;
390
391         offset_cnt = 0;
392         for (item = G.ntp_peers; item != NULL; item = item->link) {
393                 p = (ntp_peer_t *) item->data;
394                 if (p->trustlevel < TRUSTLEVEL_BADPEER)
395                         continue;
396                 if (!p->update.good)
397                         return;
398                 offset_cnt++;
399         }
400
401         peers = xzalloc(sizeof(ntp_peer_t *) * offset_cnt);
402         for (item = G.ntp_peers; item != NULL; item = item->link) {
403                 p = (ntp_peer_t *) item->data;
404                 if (p->trustlevel < TRUSTLEVEL_BADPEER)
405                         continue;
406                 peers[i++] = p;
407         }
408
409         qsort(peers, offset_cnt, sizeof(ntp_peer_t *), offset_compare);
410
411         if (offset_cnt != 0) {
412                 if ((offset_cnt & 1) == 0) {
413 //TODO: try offset_cnt /= 2...
414                         offset_median =
415                             (peers[offset_cnt / 2 - 1]->update.offset +
416                             peers[offset_cnt / 2]->update.offset) / 2;
417                         G.status.rootdelay =
418                             (peers[offset_cnt / 2 - 1]->update.delay +
419                             peers[offset_cnt / 2]->update.delay) / 2;
420                         G.status.stratum = MAX(
421                             peers[offset_cnt / 2 - 1]->update.status.stratum,
422                             peers[offset_cnt / 2]->update.status.stratum);
423                 } else {
424                         offset_median = peers[offset_cnt / 2]->update.offset;
425                         G.status.rootdelay = peers[offset_cnt / 2]->update.delay;
426                         G.status.stratum = peers[offset_cnt / 2]->update.status.stratum;
427                 }
428                 G.status.leap = peers[offset_cnt / 2]->update.status.leap;
429
430                 bb_info_msg("adjusting local clock by %fs", offset_median);
431
432                 d_to_tv(offset_median, &tv);
433                 if (adjtime(&tv, &olddelta) == -1)
434                         bb_error_msg("adjtime failed");
435                 else if (!G.firstadj
436                  && olddelta.tv_sec == 0
437                  && olddelta.tv_usec == 0
438                  && !G.status.synced
439                 ) {
440                         bb_info_msg("clock synced");
441                         G.status.synced = 1;
442                 } else if (G.status.synced) {
443                         bb_info_msg("clock unsynced");
444                         G.status.synced = 0;
445                 }
446
447                 G.firstadj = 0;
448                 G.status.reftime = gettime();
449                 G.status.stratum++;     /* one more than selected peer */
450                 G.scale = updated_scale(offset_median);
451
452                 G.status.refid4 = peers[offset_cnt / 2]->update.status.refid4;
453
454                 lsa = peers[offset_cnt / 2]->lsa;
455                 G.status.refid =
456 #if ENABLE_FEATURE_IPV6
457                         lsa->u.sa.sa_family != AF_INET ?
458                                 G.status.refid4 :
459 #endif
460                                 lsa->u.sin.sin_addr.s_addr;
461         }
462
463         free(peers);
464
465         for (item = G.ntp_peers; item != NULL; item = item->link) {
466                 p = (ntp_peer_t *) item->data;
467                 p->update.good = 0;
468         }
469 }
470
471 static void
472 settime(double offset)
473 {
474         ntp_peer_t *p;
475         llist_t         *item;
476         struct timeval  tv, curtime;
477         char            buf[80];
478         time_t          tval;
479
480         if (!G.settime)
481                 goto bail;
482
483         G.settime = 0;
484
485         /* if the offset is small, don't call settimeofday */
486         if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET)
487                 goto bail;
488
489         gettimeofday(&curtime, NULL); /* never fails */
490
491         d_to_tv(offset, &tv);
492         curtime.tv_usec += tv.tv_usec + 1000000;
493         curtime.tv_sec += tv.tv_sec - 1 + (curtime.tv_usec / 1000000);
494         curtime.tv_usec %= 1000000;
495
496         if (settimeofday(&curtime, NULL) == -1) {
497                 bb_error_msg("settimeofday");
498                 goto bail;
499         }
500
501         tval = curtime.tv_sec;
502         strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
503
504         /* Do we want to print message below to system log when daemonized? */
505         bb_info_msg("set local clock to %s (offset %fs)", buf, offset);
506
507         for (item = G.ntp_peers; item != NULL; item = item->link) {
508                 p = (ntp_peer_t *) item->data;
509                 if (p->next)
510                         p->next -= offset;
511                 if (p->deadline)
512                         p->deadline -= offset;
513         }
514
515  bail:
516         if (option_mask32 & OPT_q)
517                 exit(0);
518 }
519
520 static void
521 client_update(ntp_peer_t *p)
522 {
523         int i, best = 0, good = 0;
524
525         /*
526          * clock filter
527          * find the offset which arrived with the lowest delay
528          * use that as the peer update
529          * invalidate it and all older ones
530          */
531
532         for (i = 0; good == 0 && i < OFFSET_ARRAY_SIZE; i++) {
533                 if (p->reply[i].good) {
534                         good++;
535                         best = i;
536                 }
537         }
538
539         for (; i < OFFSET_ARRAY_SIZE; i++) {
540                 if (p->reply[i].good) {
541                         good++;
542                         if (p->reply[i].delay < p->reply[best].delay)
543                                 best = i;
544                 }
545         }
546
547         if (good < 8)
548                 return;
549
550         memcpy(&p->update, &p->reply[best], sizeof(p->update));
551         adjtime_wrap();
552
553         for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
554                 if (p->reply[i].rcvd <= p->reply[best].rcvd)
555                         p->reply[i].good = 0;
556 }
557
558 static time_t
559 scale_interval(time_t requested)
560 {
561         time_t interval, r;
562
563         interval = requested * G.scale;
564         r = random() % MAX(5, interval / 10);
565         return (interval + r);
566 }
567
568 static void
569 client_dispatch(ntp_peer_t *p)
570 {
571         char                     *addr;
572         ssize_t                  size;
573         ntp_msg_t                msg;
574         double                   T1, T2, T3, T4;
575         time_t                   interval;
576         ntp_offset_t            *offset;
577
578         addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa);
579
580         size = recvfrom(p->query.fd, &msg, sizeof(msg), 0, NULL, NULL);
581         if (size == -1) {
582                 bb_perror_msg("recvfrom(%s) error", addr);
583                 if (errno == EHOSTUNREACH || errno == EHOSTDOWN
584                  || errno == ENETUNREACH || errno == ENETDOWN
585                  || errno == ECONNREFUSED || errno == EADDRNOTAVAIL
586                 ) {
587 //TODO: always do this?
588                         set_next(p, error_interval());
589                         goto bail;
590                 }
591                 xfunc_die();
592         }
593
594         T4 = gettime();
595
596         if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
597                 bb_error_msg("malformed packet received from %s", addr);
598                 goto bail;
599         }
600
601         if (msg.orgtime.int_partl != p->query.msg.xmttime.int_partl
602          || msg.orgtime.fractionl != p->query.msg.xmttime.fractionl
603         ) {
604                 goto bail;
605         }
606
607         if ((msg.status & LI_ALARM) == LI_ALARM
608          || msg.stratum == 0
609          || msg.stratum > NTP_MAXSTRATUM
610         ) {
611                 interval = error_interval();
612                 bb_info_msg("reply from %s: not synced, next query %ds", addr, (int) interval);
613                 goto bail;
614         }
615
616         /*
617          * From RFC 2030 (with a correction to the delay math):
618          *
619          *     Timestamp Name          ID   When Generated
620          *     ------------------------------------------------------------
621          *     Originate Timestamp     T1   time request sent by client
622          *     Receive Timestamp       T2   time request received by server
623          *     Transmit Timestamp      T3   time reply sent by server
624          *     Destination Timestamp   T4   time reply received by client
625          *
626          *  The roundtrip delay d and local clock offset t are defined as
627          *
628          *    d = (T4 - T1) - (T3 - T2)     t = ((T2 - T1) + (T3 - T4)) / 2.
629          */
630
631         T1 = p->query.xmttime;
632         T2 = lfp_to_d(msg.rectime);
633         T3 = lfp_to_d(msg.xmttime);
634
635         offset = &p->reply[p->shift];
636
637         offset->offset = ((T2 - T1) + (T3 - T4)) / 2;
638         offset->delay = (T4 - T1) - (T3 - T2);
639         if (offset->delay < 0) {
640                 interval = error_interval();
641                 set_next(p, interval);
642                 bb_info_msg("reply from %s: negative delay %f", addr, p->reply[p->shift].delay);
643                 goto bail;
644         }
645         offset->error = (T2 - T1) - (T3 - T4);
646         offset->rcvd = time(NULL);
647         offset->good = 1;
648
649         offset->status.leap = (msg.status & LI_MASK);
650         offset->status.precision = msg.precision;
651         offset->status.rootdelay = sfp_to_d(msg.rootdelay);
652         offset->status.rootdispersion = sfp_to_d(msg.dispersion);
653         offset->status.refid = ntohl(msg.refid);
654         offset->status.refid4 = msg.xmttime.fractionl;
655         offset->status.reftime = lfp_to_d(msg.reftime);
656         offset->status.poll = msg.ppoll;
657         offset->status.stratum = msg.stratum;
658
659         if (p->trustlevel < TRUSTLEVEL_PATHETIC)
660                 interval = scale_interval(INTERVAL_QUERY_PATHETIC);
661         else if (p->trustlevel < TRUSTLEVEL_AGRESSIVE)
662                 interval = scale_interval(INTERVAL_QUERY_AGRESSIVE);
663         else
664                 interval = scale_interval(INTERVAL_QUERY_NORMAL);
665
666         set_next(p, interval);
667         p->state = STATE_REPLY_RECEIVED;
668
669         /* every received reply which we do not discard increases trust */
670         if (p->trustlevel < TRUSTLEVEL_MAX) {
671                 if (p->trustlevel < TRUSTLEVEL_BADPEER
672                  && p->trustlevel + 1 >= TRUSTLEVEL_BADPEER
673                 ) {
674                         bb_info_msg("peer %s now valid", addr);
675                 }
676                 p->trustlevel++;
677         }
678
679         bb_info_msg("reply from %s: offset %f delay %f, next query %ds", addr,
680                         offset->offset, offset->delay, (int) interval);
681
682         client_update(p);
683         settime(offset->offset);
684
685         if (++p->shift >= OFFSET_ARRAY_SIZE)
686                 p->shift = 0;
687
688  bail:
689         free(addr);
690 }
691
692 #if ENABLE_FEATURE_NTPD_SERVER
693 static void
694 server_dispatch(int fd)
695 {
696         ssize_t                 size;
697         uint8_t                 version;
698         double                  rectime;
699         len_and_sockaddr        *to;
700         struct sockaddr         *from;
701         ntp_msg_t               query, reply;
702
703         to = get_sock_lsa(G.listen_fd);
704         from = xzalloc(to->len);
705
706         size = recv_from_to(fd, &query, sizeof(query), 0, from, &to->u.sa, to->len);
707         if (size == -1)
708                 bb_error_msg_and_die("recv_from_to");
709         if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
710                 char *addr = xmalloc_sockaddr2dotted_noport(from);
711                 bb_error_msg("malformed packet received from %s", addr);
712                 free(addr);
713                 goto bail;
714         }
715
716         rectime = gettime();
717         version = (query.status & VERSION_MASK) >> 3;
718
719         memset(&reply, 0, sizeof(reply));
720         reply.status = G.status.synced ? G.status.leap : LI_ALARM;
721         reply.status |= (query.status & VERSION_MASK);
722         reply.status |= ((query.status & MODE_MASK) == MODE_CLIENT) ?
723                          MODE_SERVER : MODE_SYM_PAS;
724         reply.stratum = G.status.stratum;
725         reply.ppoll = query.ppoll;
726         reply.precision = G.status.precision;
727         reply.rectime = d_to_lfp(rectime);
728         reply.reftime = d_to_lfp(G.status.reftime);
729         reply.xmttime = d_to_lfp(gettime());
730         reply.orgtime = query.xmttime;
731         reply.rootdelay = d_to_sfp(G.status.rootdelay);
732         reply.refid = (version > 3) ? G.status.refid4 : G.status.refid;
733
734         /* We reply from the address packet was sent to,
735          * this makes to/from look swapped here: */
736         sendmsg_wrap(fd, /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len,
737                         &reply, size);
738
739  bail:
740         free(to);
741         free(from);
742 }
743 #endif
744
745 /* Upstream ntpd's options:
746  *
747  * -4   Force DNS resolution of host names to the IPv4 namespace.
748  * -6   Force DNS resolution of host names to the IPv6 namespace.
749  * -a   Require cryptographic authentication for broadcast client,
750  *      multicast client and symmetric passive associations.
751  *      This is the default.
752  * -A   Do not require cryptographic authentication for broadcast client,
753  *      multicast client and symmetric passive associations.
754  *      This is almost never a good idea.
755  * -b   Enable the client to synchronize to broadcast servers.
756  * -c conffile
757  *      Specify the name and path of the configuration file,
758  *      default /etc/ntp.conf
759  * -d   Specify debugging mode. This option may occur more than once,
760  *      with each occurrence indicating greater detail of display.
761  * -D level
762  *      Specify debugging level directly.
763  * -f driftfile
764  *      Specify the name and path of the frequency file.
765  *      This is the same operation as the "driftfile FILE"
766  *      configuration command.
767  * -g   Normally, ntpd exits with a message to the system log
768  *      if the offset exceeds the panic threshold, which is 1000 s
769  *      by default. This option allows the time to be set to any value
770  *      without restriction; however, this can happen only once.
771  *      If the threshold is exceeded after that, ntpd will exit
772  *      with a message to the system log. This option can be used
773  *      with the -q and -x options. See the tinker command for other options.
774  * -i jaildir
775  *      Chroot the server to the directory jaildir. This option also implies
776  *      that the server attempts to drop root privileges at startup
777  *      (otherwise, chroot gives very little additional security).
778  *      You may need to also specify a -u option.
779  * -k keyfile
780  *      Specify the name and path of the symmetric key file,
781  *      default /etc/ntp/keys. This is the same operation
782  *      as the "keys FILE" configuration command.
783  * -l logfile
784  *      Specify the name and path of the log file. The default
785  *      is the system log file. This is the same operation as
786  *      the "logfile FILE" configuration command.
787  * -L   Do not listen to virtual IPs. The default is to listen.
788  * -n   Don't fork.
789  * -N   To the extent permitted by the operating system,
790  *      run the ntpd at the highest priority.
791  * -p pidfile
792  *      Specify the name and path of the file used to record the ntpd
793  *      process ID. This is the same operation as the "pidfile FILE"
794  *      configuration command.
795  * -P priority
796  *      To the extent permitted by the operating system,
797  *      run the ntpd at the specified priority.
798  * -q   Exit the ntpd just after the first time the clock is set.
799  *      This behavior mimics that of the ntpdate program, which is
800  *      to be retired. The -g and -x options can be used with this option.
801  *      Note: The kernel time discipline is disabled with this option.
802  * -r broadcastdelay
803  *      Specify the default propagation delay from the broadcast/multicast
804  *      server to this client. This is necessary only if the delay
805  *      cannot be computed automatically by the protocol.
806  * -s statsdir
807  *      Specify the directory path for files created by the statistics
808  *      facility. This is the same operation as the "statsdir DIR"
809  *      configuration command.
810  * -t key
811  *      Add a key number to the trusted key list. This option can occur
812  *      more than once.
813  * -u user[:group]
814  *      Specify a user, and optionally a group, to switch to.
815  * -v variable
816  * -V variable
817  *      Add a system variable listed by default.
818  * -x   Normally, the time is slewed if the offset is less than the step
819  *      threshold, which is 128 ms by default, and stepped if above
820  *      the threshold. This option sets the threshold to 600 s, which is
821  *      well within the accuracy window to set the clock manually.
822  *      Note: since the slew rate of typical Unix kernels is limited
823  *      to 0.5 ms/s, each second of adjustment requires an amortization
824  *      interval of 2000 s. Thus, an adjustment as much as 600 s
825  *      will take almost 14 days to complete. This option can be used
826  *      with the -g and -q options. See the tinker command for other options.
827  *      Note: The kernel time discipline is disabled with this option.
828  */
829
830 /* By doing init in a separate function we decrease stack usage
831  * in main loop.
832  */
833 static NOINLINE void ntp_init(char **argv)
834 {
835         unsigned opts;
836         llist_t *peers;
837
838         tzset();
839
840         if (getuid())
841                 bb_error_msg_and_die("need root privileges");
842
843         peers = NULL;
844         opt_complementary = "dd:p::"; /* d: counter, p: list */
845         opts = getopt32(argv,
846                         "ngq" /* compat */
847                         "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
848                         "d" /* compat */
849                         "46aAbLNx", /* compat, ignored */
850                         &peers, &G.verbose);
851 #if ENABLE_FEATURE_NTPD_SERVER
852         G.listen_fd = -1;
853         if (opts & OPT_l) {
854                 G.listen_fd = create_and_bind_dgram_or_die(NULL, 123);
855                 socket_want_pktinfo(G.listen_fd);
856                 setsockopt(G.listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
857         }
858 #endif
859         if (opts & OPT_g)
860                 G.settime = 1;
861         while (peers)
862                 add_peers(llist_pop(&peers));
863         if (!(opts & OPT_n)) {
864                 logmode = LOGMODE_NONE;
865                 bb_daemonize(DAEMON_DEVNULL_STDIO);
866         }
867
868         /* Set some globals */
869         {
870                 int prec = 0;
871                 int b;
872 #if 0
873                 struct timespec tp;
874                 /* We can use sys_clock_getres but assuming 10ms tick should be fine */
875                 clock_getres(CLOCK_REALTIME, &tp);
876                 tp.tv_sec = 0;
877                 tp.tv_nsec = 10000000;
878                 b = 1000000000 / tp.tv_nsec;    /* convert to Hz */
879 #else
880                 b = 100; /* b = 1000000000/10000000 = 100 */
881 #endif
882                 while (b > 1)
883                         prec--, b >>= 1;
884                 G.status.precision = prec;
885         }
886         G.scale = 1;
887         G.firstadj = 1;
888
889         bb_signals((1 << SIGTERM) | (1 << SIGINT), record_signo);
890         bb_signals((1 << SIGPIPE) | (1 << SIGHUP), SIG_IGN);
891 }
892
893 int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
894 int ntpd_main(int argc UNUSED_PARAM, char **argv)
895 {
896         struct globals g;
897         struct pollfd *pfd;
898         ntp_peer_t **idx2peer;
899
900         memset(&g, 0, sizeof(g));
901         SET_PTR_TO_GLOBALS(&g);
902
903         ntp_init(argv);
904
905         {
906                 unsigned new_cnt = g.peer_cnt;
907                 idx2peer = xzalloc(sizeof(void *) * new_cnt);
908                 /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */
909                 pfd = xzalloc(sizeof(pfd[0]) * (new_cnt + ENABLE_FEATURE_NTPD_SERVER));
910         }
911
912         while (!bb_got_signal) {
913                 llist_t *item;
914                 unsigned i, j, idx_peers;
915                 unsigned sent_cnt, trial_cnt;
916                 int nfds, timeout;
917                 time_t nextaction;
918
919                 nextaction = time(NULL) + 3600;
920
921                 i = 0;
922 #if ENABLE_FEATURE_NTPD_SERVER
923                 if (g.listen_fd != -1) {
924                         pfd[0].fd = g.listen_fd;
925                         pfd[0].events = POLLIN;
926                         i++;
927                 }
928 #endif
929                 idx_peers = i;
930                 sent_cnt = trial_cnt = 0;
931                 for (item = g.ntp_peers; item != NULL; item = item->link) {
932                         ntp_peer_t *p = (ntp_peer_t *) item->data;
933
934                         if (p->next > 0 && p->next <= time(NULL)) {
935                                 trial_cnt++;
936                                 if (client_query(p) == 0)
937                                         sent_cnt++;
938                         }
939                         if (p->next > 0 && p->next < nextaction)
940                                 nextaction = p->next;
941                         if (p->deadline > 0 && p->deadline < nextaction)
942                                 nextaction = p->deadline;
943
944                         if (p->deadline > 0 && p->deadline <= time(NULL)) {
945                                 char *addr = xmalloc_sockaddr2dotted_noport(&p->lsa->u.sa);
946
947                                 timeout = error_interval();
948                                 bb_info_msg("no reply from %s received in time, "
949                                                 "next query %ds", addr, timeout);
950                                 if (p->trustlevel >= TRUSTLEVEL_BADPEER) {
951                                         p->trustlevel /= 2;
952                                         if (p->trustlevel < TRUSTLEVEL_BADPEER)
953                                                 bb_info_msg("peer %s now invalid", addr);
954                                 }
955                                 free(addr);
956
957                                 set_next(p, timeout);
958                         }
959
960                         if (p->state == STATE_QUERY_SENT) {
961                                 pfd[i].fd = p->query.fd;
962                                 pfd[i].events = POLLIN;
963                                 idx2peer[i - idx_peers] = p;
964                                 i++;
965                         }
966                 }
967
968                 if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
969                         settime(0);     /* no good peers, don't wait */
970
971                 timeout = nextaction - time(NULL);
972                 if (timeout < 0)
973                         timeout = 0;
974
975                 if (g.verbose)
976                         bb_error_msg("entering poll %u secs", timeout);
977                 nfds = poll(pfd, i, timeout * 1000);
978
979                 j = 0;
980 #if ENABLE_FEATURE_NTPD_SERVER
981                 for (; nfds > 0 && j < idx_peers; j++) {
982                         if (pfd[j].revents & (POLLIN|POLLERR)) {
983                                 nfds--;
984                                 server_dispatch(pfd[j].fd);
985                         }
986                 }
987 #endif
988                 for (; nfds > 0 && j < i; j++) {
989                         if (pfd[j].revents & (POLLIN|POLLERR)) {
990                                 nfds--;
991                                 client_dispatch(idx2peer[j - idx_peers]);
992                         }
993                 }
994         } /* while (!bb_got_signal) */
995
996         kill_myself_with_sig(bb_got_signal);
997 }