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"
41 #include "sd-daemon.h"
43 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
46 #define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
49 /* expected accuracy of time synchronization; used to adjust the poll interval */
50 #define NTP_ACCURACY_SEC 0.2
53 * "A client MUST NOT under any conditions use a poll interval less
56 #define NTP_POLL_INTERVAL_MIN_SEC 32
57 #define NTP_POLL_INTERVAL_MAX_SEC 2048
60 * Maximum delta in seconds which the system clock is gradually adjusted
61 * (slew) to approach the network time. Deltas larger that this are set by
62 * letting the system time jump. The kernel's limit for adjtime is 0.5s.
64 #define NTP_MAX_ADJUST 0.4
66 /* NTP protocol, packet header */
67 #define NTP_LEAP_PLUSSEC 1
68 #define NTP_LEAP_MINUSSEC 2
69 #define NTP_LEAP_NOTINSYNC 3
70 #define NTP_MODE_CLIENT 3
71 #define NTP_MODE_SERVER 4
72 #define NTP_FIELD_LEAP(f) (((f) >> 6) & 3)
73 #define NTP_FIELD_VERSION(f) (((f) >> 3) & 7)
74 #define NTP_FIELD_MODE(f) ((f) & 7)
75 #define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
78 * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
79 * in seconds relative to 0h on 1 January 1900."
81 #define OFFSET_1900_1970 2208988800UL
98 struct ntp_ts_short root_delay;
99 struct ntp_ts_short root_dispersion;
101 struct ntp_ts reference_time;
102 struct ntp_ts origin_time;
103 struct ntp_ts recv_time;
104 struct ntp_ts trans_time;
107 typedef struct Manager Manager;
112 sd_event_source *event_receive;
114 struct sockaddr_in server_addr;
116 uint64_t packet_count;
118 /* last sent packet */
119 struct timespec trans_time_mon;
120 struct timespec trans_time;
121 usec_t retry_interval;
125 sd_event_source *event_timer;
126 usec_t poll_interval_usec;
134 unsigned int samples_idx;
135 double samples_jitter;
140 /* watch for time changes */
141 sd_event_source *event_clock_watch;
145 static void manager_free(Manager *m);
146 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
147 #define _cleanup_manager_free_ _cleanup_(manager_freep)
149 static int sntp_arm_timer(Manager *m, usec_t next);
150 static int sntp_clock_watch_setup(Manager *m);
151 static void sntp_server_disconnect(Manager *m);
153 static double ntp_ts_to_d(const struct ntp_ts *ts) {
154 return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
157 static double tv_to_d(const struct timeval *tv) {
158 return tv->tv_sec + (1.0e-6 * tv->tv_usec);
161 static double ts_to_d(const struct timespec *ts) {
162 return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
165 static void d_to_tv(double d, struct timeval *tv) {
166 tv->tv_sec = (long)d;
167 tv->tv_usec = (d - tv->tv_sec) * 1000 * 1000;
169 /* the kernel expects -0.3s as {-1, 7000.000} */
170 if (tv->tv_usec < 0) {
172 tv->tv_usec += 1000 * 1000;
176 static double square(double d) {
180 static int sntp_send_request(Manager *m) {
181 struct ntp_msg ntpmsg = {};
182 struct sockaddr_in addr = {};
187 * "The client initializes the NTP message header, sends the request
188 * to the server, and strips the time of day from the Transmit
189 * Timestamp field of the reply. For this purpose, all the NTP
190 * header fields are set to 0, except the Mode, VN, and optional
191 * Transmit Timestamp fields."
193 ntpmsg.field = NTP_FIELD(0, 4, NTP_MODE_CLIENT);
196 * Set transmit timestamp, remember it; the server will send that back
197 * as the origin timestamp and we have an indication that this is the
198 * matching answer to our request.
200 * The actual value does not matter, We do not care about the correct
201 * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
203 clock_gettime(CLOCK_MONOTONIC, &m->trans_time_mon);
204 clock_gettime(CLOCK_REALTIME, &m->trans_time);
205 ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
206 ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
208 addr.sin_family = AF_INET;
209 addr.sin_port = htobe16(123);
210 addr.sin_addr.s_addr = inet_addr(m->server);
211 len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &addr, sizeof(addr));
212 if (len == sizeof(ntpmsg)) {
214 log_debug("Sent NTP request to: %s", m->server);
216 log_debug("Sending NTP request to %s failed: %m", m->server);
218 /* re-arm timer with incresing timeout, in case the packets never arrive back */
219 if (m->retry_interval > 0) {
220 if (m->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
221 m->retry_interval *= 2;
223 m->retry_interval = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
224 r = sntp_arm_timer(m, m->retry_interval);
231 static int sntp_timer(sd_event_source *source, usec_t usec, void *userdata) {
232 Manager *m = userdata;
236 sntp_send_request(m);
240 static int sntp_arm_timer(Manager *m, usec_t next) {
244 assert(m->event_receive);
247 m->event_timer = sd_event_source_unref(m->event_timer);
251 if (m->event_timer) {
252 r = sd_event_source_set_time(m->event_timer, now(CLOCK_MONOTONIC) + next);
256 return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT);
259 r = sd_event_add_time(
263 now(CLOCK_MONOTONIC) + next, 0,
271 static int sntp_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
272 Manager *m = userdata;
275 assert(m->event_receive);
278 sntp_clock_watch_setup(m);
280 /* skip our own jumps */
287 log_info("System time changed. Resyncing.");
288 m->poll_resync = true;
289 sntp_send_request(m);
294 /* wake up when the system time changes underneath us */
295 static int sntp_clock_watch_setup(Manager *m) {
296 struct itimerspec its = { .it_value.tv_sec = TIME_T_MAX };
297 _cleanup_close_ int fd = -1;
299 sd_event_source *source;
303 assert(m->event_receive);
305 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
307 log_error("Failed to create timerfd: %m");
311 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
312 log_error("Failed to set up timerfd: %m");
316 e = sd_event_source_get_event(m->event_receive);
317 r = sd_event_add_io(e, &source, fd, EPOLLIN, sntp_clock_watch, m);
319 log_error("Failed to create clock watch event source: %s", strerror(-r));
323 sd_event_source_unref(m->event_clock_watch);
324 m->event_clock_watch = source;
326 if (m->clock_watch_fd >= 0)
327 close(m->clock_watch_fd);
328 m->clock_watch_fd = fd;
334 static int sntp_adjust_clock(Manager *m, double offset, int leap_sec) {
335 struct timex tmx = {};
339 * For small deltas, tell the kernel to gradually adjust the system
340 * clock to the NTP time, larger deltas are just directly set.
342 * Clear STA_UNSYNC, it will enable the kernel's 11-minute mode, which
343 * syncs the system time periodically to the hardware clock.
345 if (fabs(offset) < NTP_MAX_ADJUST) {
346 tmx.modes = ADJ_STATUS | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
347 tmx.status = STA_PLL;
348 tmx.offset = offset * USEC_PER_SEC;
349 tmx.constant = log2i(m->poll_interval_usec / USEC_PER_SEC) - 6;
352 log_debug(" adjust (slew): %+f sec\n", (double)tmx.offset / USEC_PER_SEC);
354 tmx.modes = ADJ_SETOFFSET;
355 d_to_tv(offset, &tmx.time);
358 log_debug(" adjust (jump): %+f sec\n", tv_to_d(&tmx.time));
363 tmx.status |= STA_INS;
366 tmx.status |= STA_DEL;
370 //r = clock_adjtime(CLOCK_REALTIME, &tmx);
375 log_debug(" status : %04i %s\n"
376 " time now : %li.%06li\n"
378 " offset : %+f sec\n"
379 " freq offset : %+li (%+.3f ppm)\n",
380 tmx.status, tmx.status & STA_UNSYNC ? "" : "sync",
381 tmx.time.tv_sec, tmx.time.tv_usec,
383 (double)tmx.offset / USEC_PER_SEC,
384 tmx.freq, (double)tmx.freq / 65536);
389 static bool sntp_sample_spike_detection(Manager *m, double offset, double delay) {
390 unsigned int i, idx_cur, idx_new, idx_min;
396 /* ignore initial sample */
397 if (m->packet_count == 1)
400 /* store the current data in our samples array */
401 idx_cur = m->samples_idx;
402 idx_new = (idx_cur + 1) % ELEMENTSOF(m->samples);
403 m->samples_idx = idx_new;
404 m->samples[idx_new].offset = offset;
405 m->samples[idx_new].delay = delay;
407 /* calculate new jitter value from the RMS differences relative to the lowest delay sample */
408 jitter = m->samples_jitter;
409 for (idx_min = idx_cur, i = 0; i < ELEMENTSOF(m->samples); i++)
410 if (m->samples[i].delay > 0 && m->samples[i].delay < m->samples[idx_min].delay)
414 for (i = 0; i < ELEMENTSOF(m->samples); i++)
415 j += square(m->samples[i].offset - m->samples[idx_min].offset);
416 m->samples_jitter = sqrt(j / (ELEMENTSOF(m->samples) - 1));
418 /* ignore samples when resyncing */
422 /* always accept offset if we are farther off than the round-trip delay */
423 if (fabs(offset) > delay)
426 /* we need a few samples before looking at them */
427 if (m->packet_count < 4)
430 /* do not accept anything worse than the maximum possible error of the best sample */
431 if (fabs(offset) > m->samples[idx_min].delay)
434 /* compare the difference between the current offset to the previous offset and jitter */
435 return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
438 static void sntp_adjust_poll(Manager *m, double offset, bool spike) {
439 if (m->poll_resync) {
440 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
441 m->poll_resync = false;
445 /* set to minimal poll interval */
446 if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
447 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
451 /* increase polling interval */
452 if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
453 if (m->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
454 m->poll_interval_usec *= 2;
458 /* decrease polling interval */
459 if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
460 if (m->poll_interval_usec > NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC)
461 m->poll_interval_usec /= 2;
466 static int sntp_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
467 Manager *m = userdata;
468 unsigned char buf[sizeof(struct ntp_msg)];
471 .iov_len = sizeof(buf),
474 struct cmsghdr cmsghdr;
475 uint8_t buf[CMSG_SPACE(sizeof(struct timeval))];
477 struct sockaddr_in server_addr;
478 struct msghdr msghdr = {
481 .msg_control = &control,
482 .msg_controllen = sizeof(control),
483 .msg_name = &server_addr,
484 .msg_namelen = sizeof(server_addr),
486 struct cmsghdr *cmsg;
487 struct timespec now_ts;
488 struct timeval *recv_time;
490 struct ntp_msg *ntpmsg;
491 double origin, receive, trans, dest;
492 double delay, offset;
497 if (revents & (EPOLLHUP|EPOLLERR)) {
498 log_debug("Server connection returned error. Closing.");
499 sntp_server_disconnect(m);
503 len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
505 log_debug("Error receiving message. Disconnecting.");
509 if (iov.iov_len < sizeof(struct ntp_msg)) {
510 log_debug("Invalid response from server. Disconnecting.");
514 if (m->server_addr.sin_addr.s_addr != server_addr.sin_addr.s_addr) {
515 log_debug("Response from unknown server. Disconnecting.");
520 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
521 if (cmsg->cmsg_level != SOL_SOCKET)
524 switch (cmsg->cmsg_type) {
526 recv_time = (struct timeval *) CMSG_DATA(cmsg);
531 log_debug("Invalid packet timestamp. Disconnecting.");
535 ntpmsg = iov.iov_base;
537 log_debug("Unexpected reply. Ignoring.");
541 /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
542 if (be32toh(ntpmsg->origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
543 be32toh(ntpmsg->origin_time.frac) != m->trans_time.tv_nsec) {
544 log_debug("Invalid reply; not our transmit time. Ignoring.");
548 if (NTP_FIELD_LEAP(ntpmsg->field) == NTP_LEAP_NOTINSYNC) {
549 log_debug("Server is not synchronized. Disconnecting.");
553 if (NTP_FIELD_VERSION(ntpmsg->field) != 4) {
554 log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg->field));
558 if (NTP_FIELD_MODE(ntpmsg->field) != NTP_MODE_SERVER) {
559 log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg->field));
565 m->retry_interval = 0;
567 /* announce leap seconds */
568 if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_PLUSSEC)
570 else if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_MINUSSEC)
576 * "Timestamp Name ID When Generated
577 * ------------------------------------------------------------
578 * Originate Timestamp T1 time request sent by client
579 * Receive Timestamp T2 time request received by server
580 * Transmit Timestamp T3 time reply sent by server
581 * Destination Timestamp T4 time reply received by client
583 * The round-trip delay, d, and system clock offset, t, are defined as:
584 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
586 clock_gettime(CLOCK_MONOTONIC, &now_ts);
587 origin = tv_to_d(recv_time) - (ts_to_d(&now_ts) - ts_to_d(&m->trans_time_mon)) + OFFSET_1900_1970;
588 receive = ntp_ts_to_d(&ntpmsg->recv_time);
589 trans = ntp_ts_to_d(&ntpmsg->trans_time);
590 dest = tv_to_d(recv_time) + OFFSET_1900_1970;
592 offset = ((receive - origin) + (trans - dest)) / 2;
593 delay = (dest - origin) - (trans - receive);
595 spike = sntp_sample_spike_detection(m, offset, delay);
597 sntp_adjust_poll(m, offset, spike);
599 log_debug("NTP response:\n"
604 " precision : %f sec (%d)\n"
605 " reference : %.4s\n"
610 " offset : %+f sec\n"
612 " packet count : %"PRIu64"\n"
614 " poll interval: %llu\n",
615 NTP_FIELD_LEAP(ntpmsg->field),
616 NTP_FIELD_VERSION(ntpmsg->field),
617 NTP_FIELD_MODE(ntpmsg->field),
619 exp2(ntpmsg->precision), ntpmsg->precision,
620 ntpmsg->stratum == 1 ? ntpmsg->refid : "n/a",
621 origin - OFFSET_1900_1970,
622 receive - OFFSET_1900_1970,
623 trans - OFFSET_1900_1970,
624 dest - OFFSET_1900_1970,
627 m->samples_jitter, spike ? " spike" : "",
628 m->poll_interval_usec / USEC_PER_SEC);
630 log_info("%s: interval/offset/delay/jitter %llu/%+f/%f/%f",
631 m->server, m->poll_interval_usec / USEC_PER_SEC, offset, delay, m->samples_jitter);
634 r = sntp_adjust_clock(m, offset, leap_sec);
636 log_error("Failed to call clock_adjtime(): %m");
639 r = sntp_arm_timer(m, m->poll_interval_usec);
646 static int sntp_server_connect(Manager *m, const char *server) {
647 _cleanup_free_ char *s = NULL;
651 assert(m->server_socket >= 0);
661 zero(m->server_addr);
662 m->server_addr.sin_family = AF_INET;
663 m->server_addr.sin_addr.s_addr = inet_addr(server);
665 m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
667 return sntp_send_request(m);
670 static void sntp_server_disconnect(Manager *m) {
674 m->event_timer = sd_event_source_unref(m->event_timer);
676 m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
677 if (m->clock_watch_fd > 0)
678 close(m->clock_watch_fd);
679 m->clock_watch_fd = -1;
681 m->event_receive = sd_event_source_unref(m->event_receive);
682 if (m->server_socket > 0)
683 close(m->server_socket);
684 m->server_socket = -1;
686 zero(m->server_addr);
691 static int sntp_listen_setup(Manager *m) {
692 _cleanup_close_ int fd = -1;
693 struct sockaddr_in addr;
695 const int tos = IPTOS_LOWDELAY;
698 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
703 addr.sin_family = AF_INET;
704 r = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
708 r = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on));
712 r = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
716 r = sd_event_add_io(m->event, &m->event_receive, fd, EPOLLIN, sntp_receive_response, m);
720 m->server_socket = fd;
726 static int manager_new(Manager **ret) {
727 _cleanup_manager_free_ Manager *m = NULL;
730 m = new0(Manager, 1);
734 r = sd_event_default(&m->event);
738 r = sntp_listen_setup(m);
742 r = sntp_clock_watch_setup(m);
752 static void manager_free(Manager *m) {
757 sd_event_unref(m->event);
761 int main(int argc, char *argv[]) {
762 _cleanup_manager_free_ Manager *m = NULL;
766 log_set_target(LOG_TARGET_AUTO);
767 log_parse_environment();
774 server = "216.239.32.15"; /* time1.google.com */
778 "STATUS=Connecting to %s", server);
780 r = sntp_server_connect(m, server);
785 "STATUS=Connected to %s", server);
787 r = sd_event_loop(m->event);
792 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;