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