Merge from vendor branch NTPD:
[dragonfly.git] / contrib / ntpd / client.c
CommitLineData
1c93c89f 1/* $OpenBSD: client.c,v 1.56 2005/02/03 10:53:33 dtucker Exp $ */
03798098
JS
2
3/*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/param.h>
21#include <errno.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25#include <unistd.h>
26
27#include "ntpd.h"
28
29int client_update(struct ntp_peer *);
03798098
JS
30void set_deadline(struct ntp_peer *, time_t);
31
32void
33set_next(struct ntp_peer *p, time_t t)
34{
35 p->next = time(NULL) + t;
36 p->deadline = 0;
37}
38
39void
40set_deadline(struct ntp_peer *p, time_t t)
41{
42 p->deadline = time(NULL) + t;
43 p->next = 0;
44}
45
46int
47client_peer_init(struct ntp_peer *p)
48{
49 if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL)
1c93c89f 50 fatal("client_peer_init calloc");
03798098
JS
51 p->query->fd = -1;
52 p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3);
53 p->state = STATE_NONE;
54 p->shift = 0;
55 p->trustlevel = TRUSTLEVEL_PATHETIC;
1c93c89f 56 p->lasterror = 0;
03798098
JS
57
58 return (client_addr_init(p));
59}
60
61int
62client_addr_init(struct ntp_peer *p)
63{
64 struct sockaddr_in *sa_in;
65 struct sockaddr_in6 *sa_in6;
66 struct ntp_addr *h;
67
68 for (h = p->addr; h != NULL; h = h->next) {
69 switch (h->ss.ss_family) {
70 case AF_INET:
71 sa_in = (struct sockaddr_in *)&h->ss;
72 if (ntohs(sa_in->sin_port) == 0)
73 sa_in->sin_port = htons(123);
74 break;
75 case AF_INET6:
76 sa_in6 = (struct sockaddr_in6 *)&h->ss;
77 if (ntohs(sa_in6->sin6_port) == 0)
78 sa_in6->sin6_port = htons(123);
79 break;
80 default:
81 fatal("king bula sez: wrong AF in client_addr_init");
82 /* not reached */
83 }
84 }
85
39f5a752 86 p->query->fd = -1;
03798098
JS
87 set_next(p, 0);
88
89 return (0);
90}
91
92int
93client_nextaddr(struct ntp_peer *p)
94{
95 close(p->query->fd);
96 p->query->fd = -1;
97
98 if (p->addr_head.a == NULL) {
08255113 99 priv_host_dns(p->addr_head.name, p->id);
03798098
JS
100 return (-1);
101 }
102
103 if ((p->addr = p->addr->next) == NULL)
104 p->addr = p->addr_head.a;
105
03798098
JS
106 p->shift = 0;
107 p->trustlevel = TRUSTLEVEL_PATHETIC;
108
109 return (0);
110}
111
112int
113client_query(struct ntp_peer *p)
114{
39f5a752
JS
115 int tos = IPTOS_LOWDELAY;
116
03798098 117 if (p->addr == NULL && client_nextaddr(p) == -1) {
1c93c89f 118 set_next(p, error_interval());
03798098
JS
119 return (-1);
120 }
121
39f5a752
JS
122 if (p->query->fd == -1) {
123 struct sockaddr *sa = (struct sockaddr *)&p->addr->ss;
124
125 if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM,
126 0)) == -1)
127 fatal("client_query socket");
128 if (connect(p->query->fd, sa, SA_LEN(sa)) == -1) {
129 if (errno == ECONNREFUSED || errno == ENETUNREACH ||
130 errno == EHOSTUNREACH) {
131 client_nextaddr(p);
1c93c89f 132 set_next(p, error_interval());
39f5a752
JS
133 return (-1);
134 } else
135 fatal("client_query connect");
136 }
137 if (p->addr->ss.ss_family == AF_INET && setsockopt(p->query->fd,
138 IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1)
139 log_warn("setsockopt IPTOS_LOWDELAY");
140 }
141
03798098
JS
142 /*
143 * Send out a random 64-bit number as our transmit time. The NTP
144 * server will copy said number into the originate field on the
145 * response that it sends us. This is totally legal per the SNTP spec.
146 *
147 * The impact of this is two fold: we no longer send out the current
148 * system time for the world to see (which may aid an attacker), and
149 * it gives us a (not very secure) way of knowing that we're not
150 * getting spoofed by an attacker that can't capture our traffic
151 * but can spoof packets from the NTP server we're communicating with.
152 *
153 * Save the real transmit timestamp locally.
154 */
155
c1dd3b46
JS
156 p->query->msg.xmttime.int_partl = arc4random();
157 p->query->msg.xmttime.fractionl = arc4random();
03798098
JS
158 p->query->xmttime = gettime();
159
39f5a752
JS
160 if (ntp_sendmsg(p->query->fd, NULL, &p->query->msg,
161 NTP_MSGSIZE_NOAUTH, 0) == -1) {
03798098
JS
162 set_next(p, INTERVAL_QUERY_PATHETIC);
163 return (-1);
164 }
165
166 p->state = STATE_QUERY_SENT;
167 set_deadline(p, QUERYTIME_MAX);
168
169 return (0);
170}
171
172int
39f5a752 173client_dispatch(struct ntp_peer *p, u_int8_t settime)
03798098 174{
03798098
JS
175 char buf[NTP_MSGSIZE];
176 ssize_t size;
177 struct ntp_msg msg;
178 double T1, T2, T3, T4;
03798098
JS
179 time_t interval;
180
03798098 181 if ((size = recvfrom(p->query->fd, &buf, sizeof(buf), 0,
39f5a752 182 NULL, NULL)) == -1) {
03798098 183 if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
1c93c89f
JS
184 errno == ENETUNREACH || errno == ENETDOWN ||
185 errno == ECONNREFUSED) {
186 client_log_error(p, "recvfrom", errno);
187 set_next(p, error_interval());
03798098
JS
188 return (0);
189 } else
190 fatal("recvfrom");
191 }
192
193 T4 = gettime();
194
195 ntp_getmsg(buf, size, &msg);
196
c1dd3b46
JS
197 if (msg.orgtime.int_partl != p->query->msg.xmttime.int_partl ||
198 msg.orgtime.fractionl != p->query->msg.xmttime.fractionl)
199 return (0);
200
201 if ((msg.status & LI_ALARM) == LI_ALARM || msg.stratum == 0 ||
1c93c89f
JS
202 msg.stratum > NTP_MAXSTRATUM) {
203 interval = error_interval();
204 set_next(p, interval);
205 log_info("reply from %s: not synced, next query %ds",
206 log_sockaddr((struct sockaddr *)&p->addr->ss), interval);
03798098 207 return (0);
1c93c89f 208 }
03798098
JS
209
210 /*
211 * From RFC 2030 (with a correction to the delay math):
212 *
39f5a752 213 * Timestamp Name ID When Generated
03798098
JS
214 * ------------------------------------------------------------
215 * Originate Timestamp T1 time request sent by client
216 * Receive Timestamp T2 time request received by server
217 * Transmit Timestamp T3 time reply sent by server
218 * Destination Timestamp T4 time reply received by client
219 *
220 * The roundtrip delay d and local clock offset t are defined as
221 *
222 * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2.
223 */
224
225 T1 = p->query->xmttime;
226 T2 = lfp_to_d(msg.rectime);
227 T3 = lfp_to_d(msg.xmttime);
228
229 p->reply[p->shift].offset = ((T2 - T1) + (T3 - T4)) / 2;
230 p->reply[p->shift].delay = (T4 - T1) - (T3 - T2);
231 p->reply[p->shift].error = (T2 - T1) - (T3 - T4);
232 p->reply[p->shift].rcvd = time(NULL);
233 p->reply[p->shift].good = 1;
234
235 p->reply[p->shift].status.leap = (msg.status & LIMASK) >> 6;
236 p->reply[p->shift].status.precision = msg.precision;
39f5a752 237 p->reply[p->shift].status.rootdelay = sfp_to_d(msg.rootdelay);
03798098 238 p->reply[p->shift].status.rootdispersion = sfp_to_d(msg.dispersion);
39f5a752 239 p->reply[p->shift].status.refid = ntohl(msg.refid);
03798098
JS
240 p->reply[p->shift].status.reftime = lfp_to_d(msg.reftime);
241 p->reply[p->shift].status.poll = msg.ppoll;
39f5a752 242 p->reply[p->shift].status.stratum = msg.stratum;
03798098
JS
243
244 if (p->trustlevel < TRUSTLEVEL_PATHETIC)
1c93c89f 245 interval = scale_interval(INTERVAL_QUERY_PATHETIC);
03798098 246 else if (p->trustlevel < TRUSTLEVEL_AGRESSIVE)
1c93c89f
JS
247 interval = scale_interval(INTERVAL_QUERY_AGRESSIVE);
248 else
249 interval = scale_interval(INTERVAL_QUERY_NORMAL);
03798098
JS
250
251 set_next(p, interval);
252 p->state = STATE_REPLY_RECEIVED;
253
254 /* every received reply which we do not discard increases trust */
c1dd3b46 255 if (p->trustlevel < TRUSTLEVEL_MAX) {
03798098
JS
256 if (p->trustlevel < TRUSTLEVEL_BADPEER &&
257 p->trustlevel + 1 >= TRUSTLEVEL_BADPEER)
258 log_info("peer %s now valid",
39f5a752 259 log_sockaddr((struct sockaddr *)&p->addr->ss));
03798098
JS
260 p->trustlevel++;
261 }
262
263 client_update(p);
39f5a752 264 if (settime)
08255113 265 priv_settime(p->reply[p->shift].offset);
03798098
JS
266
267 log_debug("reply from %s: offset %f delay %f, "
39f5a752 268 "next query %ds", log_sockaddr((struct sockaddr *)&p->addr->ss),
03798098
JS
269 p->reply[p->shift].offset, p->reply[p->shift].delay, interval);
270
271 if (++p->shift >= OFFSET_ARRAY_SIZE)
272 p->shift = 0;
273
274 return (0);
275}
276
277int
278client_update(struct ntp_peer *p)
279{
280 int i, best = 0, good = 0;
281
282 /*
283 * clock filter
284 * find the offset which arrived with the lowest delay
285 * use that as the peer update
286 * invalidate it and all older ones
287 */
288
289 for (i = 0; good == 0 && i < OFFSET_ARRAY_SIZE; i++)
290 if (p->reply[i].good) {
291 good++;
292 best = i;
293 }
294
295 for (; i < OFFSET_ARRAY_SIZE; i++)
296 if (p->reply[i].good) {
297 good++;
298 if (p->reply[i].delay < p->reply[best].delay)
299 best = i;
300 }
301
302 if (good < 8)
303 return (-1);
304
305 memcpy(&p->update, &p->reply[best], sizeof(p->update));
08255113 306 priv_adjtime();
03798098
JS
307
308 for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
309 if (p->reply[i].rcvd <= p->reply[best].rcvd)
310 p->reply[i].good = 0;
311
312 return (0);
313}
1c93c89f
JS
314
315void
316client_log_error(struct ntp_peer *peer, const char *operation, int error)
317{
318 const char *address;
319
320 address = log_sockaddr((struct sockaddr *)&peer->addr->ss);
321 if (peer->lasterror == error) {
322 log_debug("%s %s", operation, address);
323 return;
324 }
325 peer->lasterror = error;
326 log_warn("%s %s", operation, address);
327}