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.

268 lines
7.3 KiB

20 years ago
20 years ago
20 years ago
20 years ago
  1. /* $OpenBSD: client.c,v 1.28 2004/07/20 16:47:55 henning 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. int
  27. client_peer_init(struct ntp_peer *p)
  28. {
  29. struct sockaddr_in *sa_in;
  30. struct sockaddr_in6 *sa_in6;
  31. struct ntp_addr *h;
  32. if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL)
  33. fatal("client_query calloc");
  34. for (h = p->addr; h != NULL; h = h->next) {
  35. switch (h->ss.ss_family) {
  36. case AF_INET:
  37. sa_in = (struct sockaddr_in *)&h->ss;
  38. if (ntohs(sa_in->sin_port) == 0)
  39. sa_in->sin_port = htons(123);
  40. break;
  41. case AF_INET6:
  42. sa_in6 = (struct sockaddr_in6 *)&h->ss;
  43. if (ntohs(sa_in6->sin6_port) == 0)
  44. sa_in6->sin6_port = htons(123);
  45. break;
  46. default:
  47. fatal("king bula sez: wrong AF in client_peer_init");
  48. /* not reached */
  49. }
  50. }
  51. if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1)
  52. fatal("client_query socket");
  53. p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3);
  54. p->state = STATE_NONE;
  55. p->next = time(NULL);
  56. p->shift = 0;
  57. p->trustlevel = TRUSTLEVEL_PATHETIC;
  58. return (0);
  59. }
  60. int
  61. client_nextaddr(struct ntp_peer *p)
  62. {
  63. close(p->query->fd);
  64. if ((p->addr = p->addr->next) == NULL)
  65. p->addr = p->addr_head.a;
  66. if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1)
  67. fatal("client_query socket");
  68. p->shift = 0;
  69. p->trustlevel = TRUSTLEVEL_PATHETIC;
  70. return (0);
  71. }
  72. int
  73. client_query(struct ntp_peer *p)
  74. {
  75. /*
  76. * Send out a random 64-bit number as our transmit time. The NTP
  77. * server will copy said number into the originate field on the
  78. * response that it sends us. This is totally legal per the SNTP spec.
  79. *
  80. * The impact of this is two fold: we no longer send out the current
  81. * system time for the world to see (which may aid an attacker), and
  82. * it gives us a (not very secure) way of knowing that we're not
  83. * getting spoofed by an attacker that can't capture our traffic
  84. * but can spoof packets from the NTP server we're communicating with.
  85. *
  86. * Save the real transmit timestamp locally.
  87. */
  88. p->query->msg.xmttime.int_part = arc4random();
  89. p->query->msg.xmttime.fraction = arc4random();
  90. p->query->xmttime = gettime();
  91. if (ntp_sendmsg(p->query->fd, (struct sockaddr *)&p->addr->ss,
  92. &p->query->msg, NTP_MSGSIZE_NOAUTH, 0) == -1) {
  93. p->next = time(NULL) + INTERVAL_QUERY_PATHETIC;
  94. return (-1);
  95. }
  96. p->state = STATE_QUERY_SENT;
  97. p->next = 0;
  98. p->deadline = time(NULL) + QUERYTIME_MAX;
  99. return (0);
  100. }
  101. int
  102. client_dispatch(struct ntp_peer *p)
  103. {
  104. struct sockaddr_storage fsa;
  105. socklen_t fsa_len;
  106. char buf[NTP_MSGSIZE];
  107. ssize_t size;
  108. struct ntp_msg msg;
  109. double T1, T2, T3, T4;
  110. double abs_offset;
  111. time_t interval;
  112. fsa_len = sizeof(fsa);
  113. if ((size = recvfrom(p->query->fd, &buf, sizeof(buf), 0,
  114. (struct sockaddr *)&fsa, &fsa_len)) == -1) {
  115. if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
  116. errno == ENETDOWN) {
  117. log_warn("recvfrom %s",
  118. log_sockaddr((struct sockaddr *)&fsa));
  119. return (0);
  120. } else
  121. fatal("recvfrom");
  122. }
  123. T4 = gettime();
  124. ntp_getmsg(buf, size, &msg);
  125. if (msg.orgtime.int_part != p->query->msg.xmttime.int_part ||
  126. msg.orgtime.fraction != p->query->msg.xmttime.fraction)
  127. return (0);
  128. /*
  129. * From RFC 2030 (with a correction to the delay math):
  130. *
  131. * Timestamp Name ID When Generated
  132. * ------------------------------------------------------------
  133. * Originate Timestamp T1 time request sent by client
  134. * Receive Timestamp T2 time request received by server
  135. * Transmit Timestamp T3 time reply sent by server
  136. * Destination Timestamp T4 time reply received by client
  137. *
  138. * The roundtrip delay d and local clock offset t are defined as
  139. *
  140. * d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2.
  141. */
  142. T1 = p->query->xmttime;
  143. T2 = lfp_to_d(msg.rectime);
  144. T3 = lfp_to_d(msg.xmttime);
  145. p->reply[p->shift].offset = ((T2 - T1) + (T3 - T4)) / 2;
  146. p->reply[p->shift].delay = (T4 - T1) - (T3 - T2);
  147. p->reply[p->shift].error = (T2 - T1) - (T3 - T4);
  148. p->reply[p->shift].rcvd = time(NULL);
  149. p->reply[p->shift].good = 1;
  150. p->reply[p->shift].status.leap = (msg.status & LIMASK) >> 6;
  151. p->reply[p->shift].status.precision = msg.precision;
  152. p->reply[p->shift].status.rootdelay = sfp_to_d(msg.distance);
  153. p->reply[p->shift].status.rootdispersion = sfp_to_d(msg.dispersion);
  154. p->reply[p->shift].status.refid = htonl(msg.refid);
  155. p->reply[p->shift].status.reftime = lfp_to_d(msg.reftime);
  156. p->reply[p->shift].status.poll = msg.ppoll;
  157. if (p->trustlevel < TRUSTLEVEL_PATHETIC)
  158. interval = INTERVAL_QUERY_PATHETIC;
  159. else if (p->trustlevel < TRUSTLEVEL_AGRESSIVE)
  160. interval = INTERVAL_QUERY_AGRESSIVE;
  161. else {
  162. if (p->reply[p->shift].offset < 0)
  163. abs_offset = -p->reply[p->shift].offset;
  164. else
  165. abs_offset = p->reply[p->shift].offset;
  166. if (abs_offset > QSCALE_OFF_MAX)
  167. interval = INTERVAL_QUERY_NORMAL;
  168. else if (abs_offset < QSCALE_OFF_MIN)
  169. interval = INTERVAL_QUERY_NORMAL *
  170. (QSCALE_OFF_MAX / QSCALE_OFF_MIN);
  171. else
  172. interval = INTERVAL_QUERY_NORMAL *
  173. (QSCALE_OFF_MAX / abs_offset);
  174. }
  175. p->next = time(NULL) + interval;
  176. p->deadline = 0;
  177. p->state = STATE_REPLY_RECEIVED;
  178. /* every received reply which we do not discard increases trust */
  179. if (p->trustlevel < 10) {
  180. if (p->trustlevel < TRUSTLEVEL_BADPEER &&
  181. p->trustlevel + 1 >= TRUSTLEVEL_BADPEER)
  182. log_info("peer %s now valid",
  183. log_sockaddr((struct sockaddr *)&fsa));
  184. p->trustlevel++;
  185. }
  186. client_update(p);
  187. log_debug("reply from %s: offset %f delay %f, "
  188. "next query %ds", log_sockaddr((struct sockaddr *)&fsa),
  189. p->reply[p->shift].offset, p->reply[p->shift].delay, interval);
  190. if (++p->shift >= OFFSET_ARRAY_SIZE)
  191. p->shift = 0;
  192. return (0);
  193. }
  194. int
  195. client_update(struct ntp_peer *p)
  196. {
  197. int i, best = 0, good = 0;
  198. /*
  199. * clock filter
  200. * find the offset which arrived with the lowest delay
  201. * use that as the peer update
  202. * invalidate it and all older ones
  203. */
  204. for (i = 0; good == 0 && i < OFFSET_ARRAY_SIZE; i++)
  205. if (p->reply[i].good) {
  206. good++;
  207. best = i;
  208. }
  209. for (; i < OFFSET_ARRAY_SIZE; i++)
  210. if (p->reply[i].good) {
  211. good++;
  212. if (p->reply[i].delay < p->reply[best].delay)
  213. best = i;
  214. }
  215. if (good < 8)
  216. return (-1);
  217. memcpy(&p->update, &p->reply[best], sizeof(p->update));
  218. ntp_adjtime();
  219. for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
  220. if (p->reply[i].rcvd <= p->reply[best].rcvd)
  221. p->reply[i].good = 0;
  222. return (0);
  223. }