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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <time.h>
  28. #include <ipxe/malloc.h>
  29. #include <ipxe/refcnt.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/xfer.h>
  32. #include <ipxe/open.h>
  33. #include <ipxe/retry.h>
  34. #include <ipxe/timer.h>
  35. #include <ipxe/time.h>
  36. #include <ipxe/tcpip.h>
  37. #include <ipxe/ntp.h>
  38. /** @file
  39. *
  40. * Network Time Protocol
  41. *
  42. */
  43. /** An NTP client */
  44. struct ntp_client {
  45. /** Reference count */
  46. struct refcnt refcnt;
  47. /** Job control interface */
  48. struct interface job;
  49. /** Data transfer interface */
  50. struct interface xfer;
  51. /** Retransmission timer */
  52. struct retry_timer timer;
  53. };
  54. /**
  55. * Close NTP client
  56. *
  57. * @v ntp NTP client
  58. * @v rc Reason for close
  59. */
  60. static void ntp_close ( struct ntp_client *ntp, int rc ) {
  61. /* Stop timer */
  62. stop_timer ( &ntp->timer );
  63. /* Shut down interfaces */
  64. intf_shutdown ( &ntp->xfer, rc );
  65. intf_shutdown ( &ntp->job, rc );
  66. }
  67. /**
  68. * Send NTP request
  69. *
  70. * @v ntp NTP client
  71. * @ret rc Return status code
  72. */
  73. static int ntp_request ( struct ntp_client *ntp ) {
  74. struct ntp_header hdr;
  75. int rc;
  76. DBGC ( ntp, "NTP %p sending request\n", ntp );
  77. /* Construct header */
  78. memset ( &hdr, 0, sizeof ( hdr ) );
  79. hdr.flags = ( NTP_FL_LI_UNKNOWN | NTP_FL_VN_1 | NTP_FL_MODE_CLIENT );
  80. hdr.transmit.seconds = htonl ( time ( NULL ) + NTP_EPOCH );
  81. hdr.transmit.fraction = htonl ( NTP_FRACTION_MAGIC );
  82. /* Send request */
  83. if ( ( rc = xfer_deliver_raw ( &ntp->xfer, &hdr,
  84. sizeof ( hdr ) ) ) != 0 ) {
  85. DBGC ( ntp, "NTP %p could not send request: %s\n",
  86. ntp, strerror ( rc ) );
  87. return rc;
  88. }
  89. return 0;
  90. }
  91. /**
  92. * Handle NTP response
  93. *
  94. * @v ntp NTP client
  95. * @v iobuf I/O buffer
  96. * @v meta Data transfer metadata
  97. * @ret rc Return status code
  98. */
  99. static int ntp_deliver ( struct ntp_client *ntp, struct io_buffer *iobuf,
  100. struct xfer_metadata *meta ) {
  101. struct ntp_header *hdr;
  102. struct sockaddr_tcpip *st_src;
  103. int32_t delta;
  104. int rc;
  105. /* Check source port */
  106. st_src = ( ( struct sockaddr_tcpip * ) meta->src );
  107. if ( st_src->st_port != htons ( NTP_PORT ) ) {
  108. DBGC ( ntp, "NTP %p received non-NTP packet:\n", ntp );
  109. DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
  110. goto ignore;
  111. }
  112. /* Check packet length */
  113. if ( iob_len ( iobuf ) < sizeof ( *hdr ) ) {
  114. DBGC ( ntp, "NTP %p received malformed packet:\n", ntp );
  115. DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
  116. goto ignore;
  117. }
  118. hdr = iobuf->data;
  119. /* Check mode */
  120. if ( ( hdr->flags & NTP_FL_MODE_MASK ) != NTP_FL_MODE_SERVER ) {
  121. DBGC ( ntp, "NTP %p received non-server packet:\n", ntp );
  122. DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
  123. goto ignore;
  124. }
  125. /* Check magic value */
  126. if ( hdr->originate.fraction != htonl ( NTP_FRACTION_MAGIC ) ) {
  127. DBGC ( ntp, "NTP %p received unrecognised packet:\n", ntp );
  128. DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
  129. goto ignore;
  130. }
  131. /* Check for Kiss-o'-Death packets */
  132. if ( ! hdr->stratum ) {
  133. DBGC ( ntp, "NTP %p received kiss-o'-death:\n", ntp );
  134. DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
  135. rc = -EPROTO;
  136. goto close;
  137. }
  138. /* Calculate clock delta */
  139. delta = ( ntohl ( hdr->receive.seconds ) -
  140. ntohl ( hdr->originate.seconds ) );
  141. DBGC ( ntp, "NTP %p delta %d seconds\n", ntp, delta );
  142. /* Adjust system clock */
  143. time_adjust ( delta );
  144. /* Success */
  145. rc = 0;
  146. close:
  147. ntp_close ( ntp, rc );
  148. ignore:
  149. free_iob ( iobuf );
  150. return 0;
  151. }
  152. /**
  153. * Handle data transfer window change
  154. *
  155. * @v ntp NTP client
  156. */
  157. static void ntp_window_changed ( struct ntp_client *ntp ) {
  158. /* Start timer to send initial request */
  159. start_timer_nodelay ( &ntp->timer );
  160. }
  161. /** Data transfer interface operations */
  162. static struct interface_operation ntp_xfer_op[] = {
  163. INTF_OP ( xfer_deliver, struct ntp_client *, ntp_deliver ),
  164. INTF_OP ( xfer_window_changed, struct ntp_client *,
  165. ntp_window_changed ),
  166. INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
  167. };
  168. /** Data transfer interface descriptor */
  169. static struct interface_descriptor ntp_xfer_desc =
  170. INTF_DESC_PASSTHRU ( struct ntp_client, xfer, ntp_xfer_op, job );
  171. /** Job control interface operations */
  172. static struct interface_operation ntp_job_op[] = {
  173. INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
  174. };
  175. /** Job control interface descriptor */
  176. static struct interface_descriptor ntp_job_desc =
  177. INTF_DESC_PASSTHRU ( struct ntp_client, job, ntp_job_op, xfer );
  178. /**
  179. * Handle NTP timer expiry
  180. *
  181. * @v timer Retransmission timer
  182. * @v fail Failure indicator
  183. */
  184. static void ntp_expired ( struct retry_timer *timer, int fail ) {
  185. struct ntp_client *ntp =
  186. container_of ( timer, struct ntp_client, timer );
  187. /* Shut down client if we have failed */
  188. if ( fail ) {
  189. ntp_close ( ntp, -ETIMEDOUT );
  190. return;
  191. }
  192. /* Otherwise, restart timer and (re)transmit request */
  193. start_timer ( &ntp->timer );
  194. ntp_request ( ntp );
  195. }
  196. /**
  197. * Start NTP client
  198. *
  199. * @v job Job control interface
  200. * @v hostname NTP server
  201. * @ret rc Return status code
  202. */
  203. int start_ntp ( struct interface *job, const char *hostname ) {
  204. struct ntp_client *ntp;
  205. union {
  206. struct sockaddr_tcpip st;
  207. struct sockaddr sa;
  208. } server;
  209. int rc;
  210. /* Allocate and initialise structure*/
  211. ntp = zalloc ( sizeof ( *ntp ) );
  212. if ( ! ntp ) {
  213. rc = -ENOMEM;
  214. goto err_alloc;
  215. }
  216. ref_init ( &ntp->refcnt, NULL );
  217. intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
  218. intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
  219. timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
  220. set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );
  221. /* Open socket */
  222. memset ( &server, 0, sizeof ( server ) );
  223. server.st.st_port = htons ( NTP_PORT );
  224. if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
  225. hostname, NULL ) ) != 0 ) {
  226. DBGC ( ntp, "NTP %p could not open socket: %s\n",
  227. ntp, strerror ( rc ) );
  228. goto err_open;
  229. }
  230. /* Attach parent interface, mortalise self, and return */
  231. intf_plug_plug ( &ntp->job, job );
  232. ref_put ( &ntp->refcnt );
  233. return 0;
  234. err_open:
  235. ntp_close ( ntp, rc );
  236. ref_put ( &ntp->refcnt );
  237. err_alloc:
  238. return rc;
  239. }