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