Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
8.2 KiB

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