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/>.
23 * "Simple Network Time Protocol Version 4 (SNTPv4) is a subset of the
24 * Network Time Protocol (NTP) used to synchronize computer clocks in
25 * the Internet. SNTPv4 can be used when the ultimate performance of
26 * a full NTP implementation based on RFC 1305 is neither needed nor
29 * "Unlike most NTP clients, SNTP clients normally operate with only a
30 * single server at a time."
32 * http://tools.ietf.org/html/rfc4330
42 #include <arpa/inet.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
45 #include <sys/timerfd.h>
46 #include <sys/timex.h>
47 #include <sys/socket.h>
51 #include "sparse-endian.h"
54 #include "timedate-sntp.h"
56 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
59 #define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
62 /* expected accuracy of time synchronization; used to adjust the poll interval */
63 #define NTP_ACCURACY_SEC 0.2
66 * "A client MUST NOT under any conditions use a poll interval less
69 #define NTP_POLL_INTERVAL_MIN_SEC 32
70 #define NTP_POLL_INTERVAL_MAX_SEC 2048
73 * Maximum delta in seconds which the system clock is gradually adjusted
74 * (slew) to approach the network time. Deltas larger that this are set by
75 * letting the system time jump. The kernel's limit for adjtime is 0.5s.
77 #define NTP_MAX_ADJUST 0.4
79 /* NTP protocol, packet header */
80 #define NTP_LEAP_PLUSSEC 1
81 #define NTP_LEAP_MINUSSEC 2
82 #define NTP_LEAP_NOTINSYNC 3
83 #define NTP_MODE_CLIENT 3
84 #define NTP_MODE_SERVER 4
85 #define NTP_FIELD_LEAP(f) (((f) >> 6) & 3)
86 #define NTP_FIELD_VERSION(f) (((f) >> 3) & 7)
87 #define NTP_FIELD_MODE(f) ((f) & 7)
88 #define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
91 * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
92 * in seconds relative to 0h on 1 January 1900."
94 #define OFFSET_1900_1970 2208988800UL
101 struct ntp_ts_short {
111 struct ntp_ts_short root_delay;
112 struct ntp_ts_short root_dispersion;
114 struct ntp_ts reference_time;
115 struct ntp_ts origin_time;
116 struct ntp_ts recv_time;
117 struct ntp_ts trans_time;
121 void (*report)(usec_t poll, double offset, double delay, double jitter, bool spike);
124 sd_event_source *event_receive;
126 struct sockaddr_in server_addr;
128 uint64_t packet_count;
130 /* last sent packet */
131 struct timespec trans_time_mon;
132 struct timespec trans_time;
133 usec_t retry_interval;
137 sd_event_source *event_timer;
138 usec_t poll_interval_usec;
146 unsigned int samples_idx;
147 double samples_jitter;
152 /* watch for time changes */
153 sd_event_source *event_clock_watch;
157 static int sntp_arm_timer(SNTPContext *sntp, usec_t next);
158 static int sntp_clock_watch_setup(SNTPContext *sntp);
160 static double ntp_ts_to_d(const struct ntp_ts *ts) {
161 return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
164 static double tv_to_d(const struct timeval *tv) {
165 return tv->tv_sec + (1.0e-6 * tv->tv_usec);
168 static double ts_to_d(const struct timespec *ts) {
169 return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
172 static void d_to_tv(double d, struct timeval *tv) {
173 tv->tv_sec = (long)d;
174 tv->tv_usec = (d - tv->tv_sec) * 1000 * 1000;
176 /* the kernel expects -0.3s as {-1, 7000.000} */
177 if (tv->tv_usec < 0) {
179 tv->tv_usec += 1000 * 1000;
183 static double square(double d) {
187 static int sntp_send_request(SNTPContext *sntp) {
188 struct ntp_msg ntpmsg = {};
189 struct sockaddr_in addr = {};
194 * "The client initializes the NTP message header, sends the request
195 * to the server, and strips the time of day from the Transmit
196 * Timestamp field of the reply. For this purpose, all the NTP
197 * header fields are set to 0, except the Mode, VN, and optional
198 * Transmit Timestamp fields."
200 ntpmsg.field = NTP_FIELD(0, 4, NTP_MODE_CLIENT);
203 * Set transmit timestamp, remember it; the server will send that back
204 * as the origin timestamp and we have an indication that this is the
205 * matching answer to our request.
207 * The actual value does not matter, We do not care about the correct
208 * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
210 clock_gettime(CLOCK_MONOTONIC, &sntp->trans_time_mon);
211 clock_gettime(CLOCK_REALTIME, &sntp->trans_time);
212 ntpmsg.trans_time.sec = htobe32(sntp->trans_time.tv_sec + OFFSET_1900_1970);
213 ntpmsg.trans_time.frac = htobe32(sntp->trans_time.tv_nsec);
215 addr.sin_family = AF_INET;
216 addr.sin_port = htobe16(123);
217 addr.sin_addr.s_addr = inet_addr(sntp->server);
218 len = sendto(sntp->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &addr, sizeof(addr));
219 if (len == sizeof(ntpmsg)) {
220 sntp->pending = true;
221 log_debug("Sent NTP request to: %s", sntp->server);
223 log_debug("Sending NTP request to %s failed: %m", sntp->server);
225 /* re-arm timer with incresing timeout, in case the packets never arrive back */
226 if (sntp->retry_interval > 0) {
227 if (sntp->retry_interval < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
228 sntp->retry_interval *= 2;
230 sntp->retry_interval = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
231 r = sntp_arm_timer(sntp, sntp->retry_interval);
238 static int sntp_timer(sd_event_source *source, usec_t usec, void *userdata) {
239 SNTPContext *sntp = userdata;
243 sntp_send_request(sntp);
247 static int sntp_arm_timer(SNTPContext *sntp, usec_t next) {
252 assert(sntp->event_receive);
255 sntp->event_timer = sd_event_source_unref(sntp->event_timer);
259 if (sntp->event_timer) {
260 r = sd_event_source_set_time(sntp->event_timer, now(CLOCK_MONOTONIC) + next);
264 return sd_event_source_set_enabled(sntp->event_timer, SD_EVENT_ONESHOT);
267 e = sd_event_source_get_event(sntp->event_receive);
268 r = sd_event_add_time(
272 now(CLOCK_MONOTONIC) + next, 0,
280 static int sntp_clock_watch(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
281 SNTPContext *sntp = userdata;
284 assert(sntp->event_receive);
287 sntp_clock_watch_setup(sntp);
289 /* skip our own jumps */
291 sntp->jumped = false;
296 log_info("System time changed. Resyncing.");
297 sntp->poll_resync = true;
298 sntp_send_request(sntp);
303 /* wake up when the system time changes underneath us */
304 static int sntp_clock_watch_setup(SNTPContext *sntp) {
305 struct itimerspec its = { .it_value.tv_sec = TIME_T_MAX };
306 _cleanup_close_ int fd = -1;
308 sd_event_source *source;
312 assert(sntp->event_receive);
314 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
316 log_error("Failed to create timerfd: %m");
320 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
321 log_error("Failed to set up timerfd: %m");
325 e = sd_event_source_get_event(sntp->event_receive);
326 r = sd_event_add_io(e, &source, fd, EPOLLIN, sntp_clock_watch, sntp);
328 log_error("Failed to create clock watch event source: %s", strerror(-r));
332 sd_event_source_unref(sntp->event_clock_watch);
333 sntp->event_clock_watch = source;
335 if (sntp->clock_watch_fd >= 0)
336 close(sntp->clock_watch_fd);
337 sntp->clock_watch_fd = fd;
343 static int sntp_adjust_clock(SNTPContext *sntp, double offset, int leap_sec) {
344 struct timex tmx = {};
348 * For small deltas, tell the kernel to gradually adjust the system
349 * clock to the NTP time, larger deltas are just directly set.
351 * Clear STA_UNSYNC, it will enable the kernel's 11-minute mode, which
352 * syncs the system time periodically to the hardware clock.
354 if (offset < NTP_MAX_ADJUST && offset > -NTP_MAX_ADJUST) {
355 tmx.modes |= ADJ_STATUS | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
356 tmx.status = STA_PLL;
357 tmx.offset = offset * 1000 * 1000;
358 tmx.constant = log2i(sntp->poll_interval_usec / USEC_PER_SEC) - 6;
361 log_debug(" adjust (slew): %+f sec\n", (double)tmx.offset / USEC_PER_SEC);
363 tmx.modes = ADJ_SETOFFSET;
364 d_to_tv(offset, &tmx.time);
367 log_debug(" adjust (jump): %+f sec\n", tv_to_d(&tmx.time));
372 tmx.status |= STA_INS;
375 tmx.status |= STA_DEL;
379 r = clock_adjtime(CLOCK_REALTIME, &tmx);
383 log_debug(" status : %04i %s\n"
384 " time now : %li.%06li\n"
386 " offset : %+f sec\n"
387 " freq offset : %+li (%+.3f ppm)\n",
388 tmx.status, tmx.status & STA_UNSYNC ? "" : "sync",
389 tmx.time.tv_sec, tmx.time.tv_usec,
391 (double)tmx.offset / USEC_PER_SEC,
392 tmx.freq, (double)tmx.freq / 65536);
397 static bool sntp_sample_spike_detection(SNTPContext *sntp, double offset, double delay) {
398 unsigned int i, idx_cur, idx_new, idx_min;
402 sntp->packet_count++;
404 /* ignore initial sample */
405 if (sntp->packet_count == 1)
408 /* store the current data in our samples array */
409 idx_cur = sntp->samples_idx;
410 idx_new = (idx_cur + 1) % ELEMENTSOF(sntp->samples);
411 sntp->samples_idx = idx_new;
412 sntp->samples[idx_new].offset = offset;
413 sntp->samples[idx_new].delay = delay;
415 /* calculate new jitter value from the RMS differences relative to the lowest delay sample */
416 jitter = sntp->samples_jitter;
417 for (idx_min = idx_cur, i = 0; i < ELEMENTSOF(sntp->samples); i++)
418 if (sntp->samples[i].delay > 0 && sntp->samples[i].delay < sntp->samples[idx_min].delay)
422 for (i = 0; i < ELEMENTSOF(sntp->samples); i++)
423 j += square(sntp->samples[i].offset - sntp->samples[idx_min].offset);
424 sntp->samples_jitter = sqrt(j / (ELEMENTSOF(sntp->samples) - 1));
426 /* ignore samples when resyncing */
427 if (sntp->poll_resync)
430 /* always accept offset if we are farther off than the round-trip delay */
431 if (fabs(offset) > delay)
434 /* we need a few samples before looking at them */
435 if (sntp->packet_count < 4)
438 /* do not accept anything worse than the maximum possible error of the best sample */
439 if (fabs(offset) > sntp->samples[idx_min].delay)
442 /* compare the difference between the current offset to the previous offset and jitter */
443 return fabs(offset - sntp->samples[idx_cur].offset) > 3 * jitter;
446 static void sntp_adjust_poll(SNTPContext *sntp, double offset, bool spike) {
447 if (sntp->poll_resync) {
448 sntp->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
449 sntp->poll_resync = false;
453 /* set to minimal poll interval */
454 if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
455 sntp->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
459 /* increase polling interval */
460 if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
461 if (sntp->poll_interval_usec < NTP_POLL_INTERVAL_MAX_SEC * USEC_PER_SEC)
462 sntp->poll_interval_usec *= 2;
466 /* decrease polling interval */
467 if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
468 if (sntp->poll_interval_usec > NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC)
469 sntp->poll_interval_usec /= 2;
474 static int sntp_receive_response(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
475 SNTPContext *sntp = userdata;
476 unsigned char buf[sizeof(struct ntp_msg)];
479 .iov_len = sizeof(buf),
482 struct cmsghdr cmsghdr;
483 uint8_t buf[CMSG_SPACE(sizeof(struct timeval))];
485 struct sockaddr_in server_addr;
486 struct msghdr msghdr = {
489 .msg_control = &control,
490 .msg_controllen = sizeof(control),
491 .msg_name = &server_addr,
492 .msg_namelen = sizeof(server_addr),
494 struct cmsghdr *cmsg;
495 struct timespec now_ts;
496 struct timeval *recv_time;
498 struct ntp_msg *ntpmsg;
499 double origin, receive, trans, dest;
500 double delay, offset;
505 if (revents & (EPOLLHUP|EPOLLERR)) {
506 log_debug("Server connection returned error. Closing.");
507 sntp_server_disconnect(sntp);
511 len = recvmsg(fd, &msghdr, MSG_DONTWAIT);
513 log_debug("Error receiving message. Disconnecting.");
517 if (iov.iov_len < sizeof(struct ntp_msg)) {
518 log_debug("Invalid response from server. Disconnecting.");
522 if (sntp->server_addr.sin_addr.s_addr != server_addr.sin_addr.s_addr) {
523 log_debug("Response from unknown server. Disconnecting.");
528 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
529 if (cmsg->cmsg_level != SOL_SOCKET)
532 switch (cmsg->cmsg_type) {
534 recv_time = (struct timeval *) CMSG_DATA(cmsg);
539 log_debug("Invalid packet timestamp. Disconnecting.");
543 ntpmsg = iov.iov_base;
544 if (!sntp->pending) {
545 log_debug("Unexpected reply. Ignoring.");
549 /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
550 if (be32toh(ntpmsg->origin_time.sec) != sntp->trans_time.tv_sec + OFFSET_1900_1970 ||
551 be32toh(ntpmsg->origin_time.frac) != sntp->trans_time.tv_nsec) {
552 log_debug("Invalid reply; not our transmit time. Ignoring.");
556 if (NTP_FIELD_LEAP(ntpmsg->field) == NTP_LEAP_NOTINSYNC) {
557 log_debug("Server is not synchronized. Disconnecting.");
561 if (NTP_FIELD_VERSION(ntpmsg->field) != 4) {
562 log_debug("Response NTPv%d. Disconnecting.", NTP_FIELD_VERSION(ntpmsg->field));
566 if (NTP_FIELD_MODE(ntpmsg->field) != NTP_MODE_SERVER) {
567 log_debug("Unsupported mode %d. Disconnecting.", NTP_FIELD_MODE(ntpmsg->field));
572 sntp->pending = false;
573 sntp->retry_interval = 0;
575 /* announce leap seconds */
576 if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_PLUSSEC)
578 else if (NTP_FIELD_LEAP(ntpmsg->field) & NTP_LEAP_MINUSSEC)
584 * "Timestamp Name ID When Generated
585 * ------------------------------------------------------------
586 * Originate Timestamp T1 time request sent by client
587 * Receive Timestamp T2 time request received by server
588 * Transmit Timestamp T3 time reply sent by server
589 * Destination Timestamp T4 time reply received by client
591 * The round-trip delay, d, and system clock offset, t, are defined as:
592 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2"
594 clock_gettime(CLOCK_MONOTONIC, &now_ts);
595 origin = tv_to_d(recv_time) - (ts_to_d(&now_ts) - ts_to_d(&sntp->trans_time_mon)) + OFFSET_1900_1970;
596 receive = ntp_ts_to_d(&ntpmsg->recv_time);
597 trans = ntp_ts_to_d(&ntpmsg->trans_time);
598 dest = tv_to_d(recv_time) + OFFSET_1900_1970;
600 offset = ((receive - origin) + (trans - dest)) / 2;
601 delay = (dest - origin) - (trans - receive);
603 spike = sntp_sample_spike_detection(sntp, offset, delay);
605 sntp_adjust_poll(sntp, offset, spike);
607 log_debug("NTP response:\n"
612 " precision : %f sec (%d)\n"
613 " reference : %.4s\n"
618 " offset : %+f sec\n"
620 " packet count : %"PRIu64"\n"
622 " poll interval: %llu\n",
623 NTP_FIELD_LEAP(ntpmsg->field),
624 NTP_FIELD_VERSION(ntpmsg->field),
625 NTP_FIELD_MODE(ntpmsg->field),
627 exp2(ntpmsg->precision), ntpmsg->precision,
628 ntpmsg->stratum == 1 ? ntpmsg->refid : "n/a",
629 origin - OFFSET_1900_1970,
630 receive - OFFSET_1900_1970,
631 trans - OFFSET_1900_1970,
632 dest - OFFSET_1900_1970,
635 sntp->samples_jitter, spike ? " spike" : "",
636 sntp->poll_interval_usec / USEC_PER_SEC);
639 sntp->report(sntp->poll_interval_usec, offset, delay, sntp->samples_jitter, spike);
642 r = sntp_adjust_clock(sntp, offset, leap_sec);
644 log_error("Failed to call clock_adjtime(): %m");
647 r = sntp_arm_timer(sntp, sntp->poll_interval_usec);
654 int sntp_server_connect(SNTPContext *sntp, const char *server) {
655 _cleanup_free_ char *s = NULL;
659 assert(sntp->server_socket >= 0);
669 zero(sntp->server_addr);
670 sntp->server_addr.sin_family = AF_INET;
671 sntp->server_addr.sin_addr.s_addr = inet_addr(server);
673 sntp->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
675 return sntp_send_request(sntp);
678 void sntp_server_disconnect(SNTPContext *sntp) {
682 sntp->event_timer = sd_event_source_unref(sntp->event_timer);
684 sntp->event_clock_watch = sd_event_source_unref(sntp->event_clock_watch);
685 if (sntp->clock_watch_fd > 0)
686 close(sntp->clock_watch_fd);
687 sntp->clock_watch_fd = -1;
689 sntp->event_receive = sd_event_source_unref(sntp->event_receive);
690 if (sntp->server_socket > 0)
691 close(sntp->server_socket);
692 sntp->server_socket = -1;
694 zero(sntp->server_addr);
699 static int sntp_listen_setup(SNTPContext *sntp, sd_event *e) {
700 _cleanup_close_ int fd = -1;
701 struct sockaddr_in addr;
703 const int tos = IPTOS_LOWDELAY;
706 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
711 addr.sin_family = AF_INET;
712 r = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
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(e, &sntp->event_receive, fd, EPOLLIN, sntp_receive_response, sntp);
728 sntp->server_socket = fd;
734 void sntp_report_register(SNTPContext *sntp, void (*report)(usec_t poll_usec, double offset, double delay, double jitter, bool spike)) {
735 sntp->report = report;
738 int sntp_new(SNTPContext **sntp, sd_event *e) {
739 _cleanup_free_ SNTPContext *c;
742 c = new0(SNTPContext, 1);
746 r = sntp_listen_setup(c, e);
750 r = sntp_clock_watch_setup(c);
760 SNTPContext *sntp_unref(SNTPContext *sntp) {
761 sntp_server_disconnect(sntp);