1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <netinet/ip.h>
32 #include <sys/timerfd.h>
33 #include <sys/timex.h>
34 #include <sys/socket.h>
38 #include "sparse-endian.h"
40 #include "socket-util.h"
42 #include "sd-daemon.h"
44 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
47 #define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
50 /* expected accuracy of time synchronization; used to adjust the poll interval */
51 #define NTP_ACCURACY_SEC 0.2
54 * "A client MUST NOT under any conditions use a poll interval less
57 #define NTP_POLL_INTERVAL_MIN_SEC 32
58 #define NTP_POLL_INTERVAL_MAX_SEC 2048
61 * Maximum delta in seconds which the system clock is gradually adjusted
62 * (slew) to approach the network time. Deltas larger that this are set by
63 * letting the system time jump. The kernel's limit for adjtime is 0.5s.
65 #define NTP_MAX_ADJUST 0.4
67 /* NTP protocol, packet header */
68 #define NTP_LEAP_PLUSSEC 1
69 #define NTP_LEAP_MINUSSEC 2
70 #define NTP_LEAP_NOTINSYNC 3
71 #define NTP_MODE_CLIENT 3
72 #define NTP_MODE_SERVER 4
73 #define NTP_FIELD_LEAP(f) (((f) >> 6) & 3)
74 #define NTP_FIELD_VERSION(f) (((f) >> 3) & 7)
75 #define NTP_FIELD_MODE(f) ((f) & 7)
76 #define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
79 * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
80 * in seconds relative to 0h on 1 January 1900."
82 #define OFFSET_1900_1970 2208988800UL
99 struct ntp_ts_short root_delay;
100 struct ntp_ts_short root_dispersion;
102 struct ntp_ts reference_time;
103 struct ntp_ts origin_time;
104 struct ntp_ts recv_time;
105 struct ntp_ts trans_time;
108 typedef struct Manager Manager;
113 sd_event_source *event_receive;
115 union sockaddr_union server_addr;
117 uint64_t packet_count;
119 /* last sent packet */
120 struct timespec trans_time_mon;
121 struct timespec trans_time;
122 usec_t retry_interval;
126 sd_event_source *event_timer;
127 usec_t poll_interval_usec;
135 unsigned int samples_idx;
136 double samples_jitter;
142 /* watch for time changes */
143 sd_event_source *event_clock_watch;
147 static void manager_free(Manager *m);
148 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
149 #define _cleanup_manager_free_ _cleanup_(manager_freep)
151 static int sntp_arm_timer(Manager *m, usec_t next);
152 static int sntp_clock_watch_setup(Manager *m);
153 static void sntp_server_disconnect(Manager *m);
155 static double ntp_ts_to_d(const struct ntp_ts *ts) {
156 return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
159 static double ts_to_d(const struct timespec *ts) {
160 return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
163 static double tv_to_d(const struct timeval *tv) {
164 return tv->tv_sec + (1.0e-6 * tv->tv_usec);
167 static double square(double d) {
171 static int sntp_send_request(Manager *m) {
172 struct ntp_msg ntpmsg = {
174 * "The client initializes the NTP message header, sends the request
175 * to the server, and strips the time of day from the Transmit
176 * Timestamp field of the reply. For this purpose, all the NTP
177 * header fields are set to 0, except the Mode, VN, and optional
178 * Transmit Timestamp fields."
180 .field = NTP_FIELD(0, 4, NTP_MODE_CLIENT),
183 union sockaddr_union addr = {
184 .in.sin_family = AF_INET,
185 .in.sin_port = htobe16(123),
192 * Set transmit timestamp, remember it; the server will send that back
193 * as the origin timestamp and we have an indication that this is the
194 * matching answer to our request.
196 * The actual value does not matter, We do not care about the correct
197 * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
199 clock_gettime(CLOCK_MONOTONIC, &m->trans_time_mon);
200 clock_gettime(CLOCK_REALTIME, &m->trans_time);
201 ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
202 ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
204 addr.in.sin_addr.s_addr = inet_addr(m->server);
205 len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &addr.sa, sizeof(addr.in));
206 if (len == sizeof(ntpmsg)) {
208 log_debug("Sent NTP request to: %s", m->server);
210 log_debug("Sending NTP request to %s failed: %m", m->server);
212 /* re-arm timer with incresing timeout, in case the packets never arrive back */
213 if (m->retry_interval > 0) {
214 if (m->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
215 m->retry_interval *= 2;
217 m->retry_interval = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
218 r = sntp_arm_timer(m, m->retry_interval);
225 static int sntp_timer(sd_event_source *source, usec_t usec, void *userdata) {
226 Manager *m = userdata;
230 sntp_send_request(m);
234 static int sntp_arm_timer(Manager *m, usec_t next) {
238 assert(m->event_receive);
241 m->event_timer = sd_event_source_unref(m->event_timer);
245 if (m->event_timer) {
246 r = sd_event_source_set_time(m->event_timer, now(CLOCK_MONOTONIC) + next);
250 return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT);
253 r = sd_event_add_time(
257 now(CLOCK_MONOTONIC) + next, 0,
265 static int sntp_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
266 Manager *m = userdata;
269 assert(m->event_receive);
272 sntp_clock_watch_setup(m);
274 /* skip our own jumps */
281 log_info("System time changed. Resyncing.");
282 m->poll_resync = true;
283 sntp_send_request(m);
288 /* wake up when the system time changes underneath us */
289 static int sntp_clock_watch_setup(Manager *m) {
291 struct itimerspec its = {
292 .it_value.tv_sec = TIME_T_MAX
295 _cleanup_close_ int fd = -1;
296 sd_event_source *source;
300 assert(m->event_receive);
302 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
304 log_error("Failed to create timerfd: %m");
308 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
309 log_error("Failed to set up timerfd: %m");
313 r = sd_event_add_io(m->event, &source, fd, EPOLLIN, sntp_clock_watch, m);
315 log_error("Failed to create clock watch event source: %s", strerror(-r));
319 sd_event_source_unref(m->event_clock_watch);
320 m->event_clock_watch = source;
322 if (m->clock_watch_fd >= 0)
323 close(m->clock_watch_fd);
324 m->clock_watch_fd = fd;
330 static int sntp_adjust_clock(Manager *m, double offset, int leap_sec) {
331 struct timex tmx = {};
335 * For small deltas, tell the kernel to gradually adjust the system
336 * clock to the NTP time, larger deltas are just directly set.
338 * Clear STA_UNSYNC, it will enable the kernel's 11-minute mode, which
339 * syncs the system time periodically to the hardware clock.
341 if (fabs(offset) < NTP_MAX_ADJUST) {
342 tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
343 tmx.status = STA_PLL;
344 tmx.offset = offset * NSEC_PER_SEC;
345 tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 4;
348 log_debug(" adjust (slew): %+.3f sec\n", offset);
350 tmx.modes = ADJ_SETOFFSET | ADJ_NANO;
352 /* ADJ_NANO uses nanoseconds in the microseconds field */
353 tmx.time.tv_sec = (long)offset;
354 tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
356 /* the kernel expects -0.3s as {-1, 7000.000.000} */
357 if (tmx.time.tv_usec < 0) {
358 tmx.time.tv_sec -= 1;
359 tmx.time.tv_usec += NSEC_PER_SEC;
363 log_debug(" adjust (jump): %+.3f sec\n", offset);
368 tmx.status |= STA_INS;
371 tmx.status |= STA_DEL;
375 r = clock_adjtime(CLOCK_REALTIME, &tmx);
379 m->drift_ppm = tmx.freq / 65536;
381 log_debug(" status : %04i %s\n"
382 " time now : %li.%03lli\n"
384 " offset : %+.3f sec\n"
385 " freq offset : %+li (%i ppm)\n",
386 tmx.status, tmx.status & STA_UNSYNC ? "" : "sync",
387 tmx.time.tv_sec, tmx.time.tv_usec / NSEC_PER_MSEC,
389 (double)tmx.offset / NSEC_PER_SEC,
390 tmx.freq, m->drift_ppm);
395 static bool sntp_sample_spike_detection(Manager *m, double offset, double delay) {
396 unsigned int i, idx_cur, idx_new, idx_min;
402 /* ignore initial sample */
403 if (m->packet_count == 1)
406 /* store the current data in our samples array */
407 idx_cur = m->samples_idx;
408 idx_new = (idx_cur + 1) % ELEMENTSOF(m->samples);
409 m->samples_idx = idx_new;
410 m->samples[idx_new].offset = offset;
411 m->samples[idx_new].delay = delay;
413 /* calculate new jitter value from the RMS differences relative to the lowest delay sample */
414 jitter = m->samples_jitter;
415 for (idx_min = idx_cur, i = 0; i < ELEMENTSOF(m->samples); i++)
416 if (m->samples[i].delay > 0 && m->samples[i].delay < m->samples[idx_min].delay)
420 for (i = 0; i < ELEMENTSOF(m->samples); i++)
421 j += square(m->samples[i].offset - m->samples[idx_min].offset);
422 m->samples_jitter = sqrt(j / (ELEMENTSOF(m->samples) - 1));
424 /* ignore samples when resyncing */
428 /* always accept offset if we are farther off than the round-trip delay */
429 if (fabs(offset) > delay)
432 /* we need a few samples before looking at them */
433 if (m->packet_count < 4)
436 /* do not accept anything worse than the maximum possible error of the best sample */
437 if (fabs(offset) > m->samples[idx_min].delay)
440 /* compare the difference between the current offset to the previous offset and jitter */
441 return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
444 static void sntp_adjust_poll(Manager *m, double offset, bool spike) {
445 if (m->poll_resync) {
446 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
447 m->poll_resync = false;
451 /* set to minimal poll interval */
452 if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
453 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
457 /* increase polling interval */
458 if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
459 if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
460 m->poll_interval_usec *= 2;
464 /* decrease polling interval */
465 if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
466 if (m->poll_interval_usec > NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC)
467 m->poll_interval_usec /= 2;
472 static int sntp_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
473 Manager *m = userdata;
474 unsigned char buf[sizeof(struct ntp_msg)];
477 .iov_len = sizeof(buf),
480 struct cmsghdr cmsghdr;
481 uint8_t buf[CMSG_SPACE(sizeof(struct timeval))];
483 union sockaddr_union server_addr;
484 struct msghdr msghdr = {
487 .msg_control = &control,
488 .msg_controllen = sizeof(control),
489 .msg_name = &server_addr,
490 .msg_namelen = sizeof(server_addr),
492 struct cmsghdr *cmsg;
493 struct timespec now_ts;
494 struct timeval *recv_time;
496 struct ntp_msg *ntpmsg;
497 double origin, receive, trans, dest;
498 double delay, offset;
503 if (revents & (EPOLLHUP|EPOLLERR)) {
504 log_debug("Server connection returned error. Closing.");
505 sntp_server_disconnect(m);
509 len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
511 log_debug("Error receiving message. Disconnecting.");
515 if (iov.iov_len < sizeof(struct ntp_msg)) {
516 log_debug("Invalid response from server. Disconnecting.");
520 if (m->server_addr.in.sin_addr.s_addr != server_addr.in.sin_addr.s_addr) {
521 log_debug("Response from unknown server. Disconnecting.");
526 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
527 if (cmsg->cmsg_level != SOL_SOCKET)
530 switch (cmsg->cmsg_type) {
532 recv_time = (struct timeval *) CMSG_DATA(cmsg);
537 log_debug("Invalid packet timestamp. Disconnecting.");
541 ntpmsg = iov.iov_base;
543 log_debug("Unexpected reply. Ignoring.");
547 /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
548 if (be32toh(ntpmsg->origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
549 be32toh(ntpmsg->origin_time.frac) != m->trans_time.tv_nsec) {
550 log_debug("Invalid reply; not our transmit time. Ignoring.");
554 if (NTP_FIELD_LEAP(ntpmsg->field) == NTP_LEAP_NOTINSYNC) {
555 log_debug("Server is not synchronized. Disconnecting.");
559 if (NTP_FIELD_VERSION(ntpmsg->field) != 4) {
560 log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg->field));
564 if (NTP_FIELD_MODE(ntpmsg->field) != NTP_MODE_SERVER) {
565 log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg->field));
571 m->retry_interval = 0;
573 /* announce leap seconds */
574 if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_PLUSSEC)
576 else if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_MINUSSEC)
582 * "Timestamp Name ID When Generated
583 * ------------------------------------------------------------
584 * Originate Timestamp T1 time request sent by client
585 * Receive Timestamp T2 time request received by server
586 * Transmit Timestamp T3 time reply sent by server
587 * Destination Timestamp T4 time reply received by client
589 * The round-trip delay, d, and system clock offset, t, are defined as:
590 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
592 clock_gettime(CLOCK_MONOTONIC, &now_ts);
593 origin = tv_to_d(recv_time) - (ts_to_d(&now_ts) - ts_to_d(&m->trans_time_mon)) + OFFSET_1900_1970;
594 receive = ntp_ts_to_d(&ntpmsg->recv_time);
595 trans = ntp_ts_to_d(&ntpmsg->trans_time);
596 dest = tv_to_d(recv_time) + OFFSET_1900_1970;
598 offset = ((receive - origin) + (trans - dest)) / 2;
599 delay = (dest - origin) - (trans - receive);
601 spike = sntp_sample_spike_detection(m, offset, delay);
603 sntp_adjust_poll(m, offset, spike);
605 log_debug("NTP response:\n"
610 " precision : %.6f sec (%d)\n"
611 " reference : %.4s\n"
616 " offset : %+.3f sec\n"
617 " delay : %+.3f sec\n"
618 " packet count : %"PRIu64"\n"
620 " poll interval: %llu\n",
621 NTP_FIELD_LEAP(ntpmsg->field),
622 NTP_FIELD_VERSION(ntpmsg->field),
623 NTP_FIELD_MODE(ntpmsg->field),
625 exp2(ntpmsg->precision), ntpmsg->precision,
626 ntpmsg->stratum == 1 ? ntpmsg->refid : "n/a",
627 origin - OFFSET_1900_1970,
628 receive - OFFSET_1900_1970,
629 trans - OFFSET_1900_1970,
630 dest - OFFSET_1900_1970,
633 m->samples_jitter, spike ? " spike" : "",
634 m->poll_interval_usec / USEC_PER_SEC);
637 r = sntp_adjust_clock(m, offset, leap_sec);
639 log_error("Failed to call clock_adjtime(): %m");
642 log_info("%s: interval/delta/delay/jitter/drift %llus/%+.3fs/%.3fs/%.3fs/%+ippm%s",
643 m->server, m->poll_interval_usec / USEC_PER_SEC, offset, delay, m->samples_jitter, m->drift_ppm,
644 spike ? " (ignored)" : "");
645 r = sntp_arm_timer(m, m->poll_interval_usec);
652 static int sntp_server_connect(Manager *m, const char *server) {
653 _cleanup_free_ char *s = NULL;
657 assert(m->server_socket >= 0);
667 zero(m->server_addr);
668 m->server_addr.in.sin_family = AF_INET;
669 m->server_addr.in.sin_addr.s_addr = inet_addr(server);
671 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
673 return sntp_send_request(m);
676 static void sntp_server_disconnect(Manager *m) {
680 m->event_timer = sd_event_source_unref(m->event_timer);
682 m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
683 if (m->clock_watch_fd > 0)
684 close(m->clock_watch_fd);
685 m->clock_watch_fd = -1;
687 m->event_receive = sd_event_source_unref(m->event_receive);
688 if (m->server_socket > 0)
689 close(m->server_socket);
690 m->server_socket = -1;
692 zero(m->server_addr);
697 static int sntp_listen_setup(Manager *m) {
699 union sockaddr_union addr = {
700 .in.sin_family = AF_INET,
703 _cleanup_close_ int fd = -1;
705 const int tos = IPTOS_LOWDELAY;
708 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
712 r = bind(fd, &addr.sa, sizeof(addr.in));
716 r = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on));
720 r = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
724 r = sd_event_add_io(m->event, &m->event_receive, fd, EPOLLIN, sntp_receive_response, m);
728 m->server_socket = fd;
734 static int manager_new(Manager **ret) {
735 _cleanup_manager_free_ Manager *m = NULL;
738 m = new0(Manager, 1);
742 r = sd_event_default(&m->event);
746 r = sntp_listen_setup(m);
750 r = sntp_clock_watch_setup(m);
760 static void manager_free(Manager *m) {
765 sd_event_unref(m->event);
769 int main(int argc, char *argv[]) {
770 _cleanup_manager_free_ Manager *m = NULL;
774 log_set_target(LOG_TARGET_AUTO);
775 log_parse_environment();
782 server = "216.239.32.15"; /* time1.google.com */
784 sd_notify(false, "READY=1");
786 r = sntp_server_connect(m, server);
790 sd_notifyf(false, "STATUS=Using Time Server: %s", server);
792 r = sd_event_loop(m->event);
797 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;