iPXE
ntp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <time.h>
00030 #include <ipxe/malloc.h>
00031 #include <ipxe/refcnt.h>
00032 #include <ipxe/iobuf.h>
00033 #include <ipxe/xfer.h>
00034 #include <ipxe/open.h>
00035 #include <ipxe/retry.h>
00036 #include <ipxe/timer.h>
00037 #include <ipxe/time.h>
00038 #include <ipxe/tcpip.h>
00039 #include <ipxe/ntp.h>
00040 
00041 /** @file
00042  *
00043  * Network Time Protocol
00044  *
00045  */
00046 
00047 /** An NTP client */
00048 struct ntp_client {
00049         /** Reference count */
00050         struct refcnt refcnt;
00051         /** Job control interface */
00052         struct interface job;
00053         /** Data transfer interface */
00054         struct interface xfer;
00055         /** Retransmission timer */
00056         struct retry_timer timer;
00057 };
00058 
00059 /**
00060  * Close NTP client
00061  *
00062  * @v ntp               NTP client
00063  * @v rc                Reason for close
00064  */
00065 static void ntp_close ( struct ntp_client *ntp, int rc ) {
00066 
00067         /* Stop timer */
00068         stop_timer ( &ntp->timer );
00069 
00070         /* Shut down interfaces */
00071         intf_shutdown ( &ntp->xfer, rc );
00072         intf_shutdown ( &ntp->job, rc );
00073 }
00074 
00075 /**
00076  * Send NTP request
00077  *
00078  * @v ntp               NTP client
00079  * @ret rc              Return status code
00080  */
00081 static int ntp_request ( struct ntp_client *ntp ) {
00082         struct ntp_header hdr;
00083         int rc;
00084 
00085         DBGC ( ntp, "NTP %p sending request\n", ntp );
00086 
00087         /* Construct header */
00088         memset ( &hdr, 0, sizeof ( hdr ) );
00089         hdr.flags = ( NTP_FL_LI_UNKNOWN | NTP_FL_VN_1 | NTP_FL_MODE_CLIENT );
00090         hdr.transmit.seconds = htonl ( time ( NULL ) + NTP_EPOCH );
00091         hdr.transmit.fraction = htonl ( NTP_FRACTION_MAGIC );
00092 
00093         /* Send request */
00094         if ( ( rc = xfer_deliver_raw ( &ntp->xfer, &hdr,
00095                                        sizeof ( hdr ) ) ) != 0 ) {
00096                 DBGC ( ntp, "NTP %p could not send request: %s\n",
00097                        ntp, strerror ( rc ) );
00098                 return rc;
00099         }
00100 
00101         return 0;
00102 }
00103 
00104 /**
00105  * Handle NTP response
00106  *
00107  * @v ntp               NTP client
00108  * @v iobuf             I/O buffer
00109  * @v meta              Data transfer metadata
00110  * @ret rc              Return status code
00111  */
00112 static int ntp_deliver ( struct ntp_client *ntp, struct io_buffer *iobuf,
00113                          struct xfer_metadata *meta ) {
00114         struct ntp_header *hdr;
00115         struct sockaddr_tcpip *st_src;
00116         int32_t delta;
00117         int rc;
00118 
00119         /* Check source port */
00120         st_src = ( ( struct sockaddr_tcpip * ) meta->src );
00121         if ( st_src->st_port != htons ( NTP_PORT ) ) {
00122                 DBGC ( ntp, "NTP %p received non-NTP packet:\n", ntp );
00123                 DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
00124                 goto ignore;
00125         }
00126 
00127         /* Check packet length */
00128         if ( iob_len ( iobuf ) < sizeof ( *hdr ) ) {
00129                 DBGC ( ntp, "NTP %p received malformed packet:\n", ntp );
00130                 DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
00131                 goto ignore;
00132         }
00133         hdr = iobuf->data;
00134 
00135         /* Check mode */
00136         if ( ( hdr->flags & NTP_FL_MODE_MASK ) != NTP_FL_MODE_SERVER ) {
00137                 DBGC ( ntp, "NTP %p received non-server packet:\n", ntp );
00138                 DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
00139                 goto ignore;
00140         }
00141 
00142         /* Check magic value */
00143         if ( hdr->originate.fraction != htonl ( NTP_FRACTION_MAGIC ) ) {
00144                 DBGC ( ntp, "NTP %p received unrecognised packet:\n", ntp );
00145                 DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
00146                 goto ignore;
00147         }
00148 
00149         /* Check for Kiss-o'-Death packets */
00150         if ( ! hdr->stratum ) {
00151                 DBGC ( ntp, "NTP %p received kiss-o'-death:\n", ntp );
00152                 DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
00153                 rc = -EPROTO;
00154                 goto close;
00155         }
00156 
00157         /* Calculate clock delta */
00158         delta = ( ntohl ( hdr->receive.seconds ) -
00159                   ntohl ( hdr->originate.seconds ) );
00160         DBGC ( ntp, "NTP %p delta %d seconds\n", ntp, delta );
00161 
00162         /* Adjust system clock */
00163         time_adjust ( delta );
00164 
00165         /* Success */
00166         rc = 0;
00167 
00168  close:
00169         ntp_close ( ntp, rc );
00170  ignore:
00171         free_iob ( iobuf );
00172         return 0;
00173 }
00174 
00175 /**
00176  * Handle data transfer window change
00177  *
00178  * @v ntp               NTP client
00179  */
00180 static void ntp_window_changed ( struct ntp_client *ntp ) {
00181 
00182         /* Start timer to send initial request */
00183         start_timer_nodelay ( &ntp->timer );
00184 }
00185 
00186 /** Data transfer interface operations */
00187 static struct interface_operation ntp_xfer_op[] = {
00188         INTF_OP ( xfer_deliver, struct ntp_client *, ntp_deliver ),
00189         INTF_OP ( xfer_window_changed, struct ntp_client *,
00190                   ntp_window_changed ),
00191         INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
00192 };
00193 
00194 /** Data transfer interface descriptor */
00195 static struct interface_descriptor ntp_xfer_desc =
00196         INTF_DESC_PASSTHRU ( struct ntp_client, xfer, ntp_xfer_op, job );
00197 
00198 /** Job control interface operations */
00199 static struct interface_operation ntp_job_op[] = {
00200         INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
00201 };
00202 
00203 /** Job control interface descriptor */
00204 static struct interface_descriptor ntp_job_desc =
00205         INTF_DESC_PASSTHRU ( struct ntp_client, job, ntp_job_op, xfer );
00206 
00207 /**
00208  * Handle NTP timer expiry
00209  *
00210  * @v timer             Retransmission timer
00211  * @v fail              Failure indicator
00212  */
00213 static void ntp_expired ( struct retry_timer *timer, int fail ) {
00214         struct ntp_client *ntp =
00215                 container_of ( timer, struct ntp_client, timer );
00216 
00217         /* Shut down client if we have failed */
00218         if ( fail ) {
00219                 ntp_close ( ntp, -ETIMEDOUT );
00220                 return;
00221         }
00222 
00223         /* Otherwise, restart timer and (re)transmit request */
00224         start_timer ( &ntp->timer );
00225         ntp_request ( ntp );
00226 }
00227 
00228 /**
00229  * Start NTP client
00230  *
00231  * @v job               Job control interface
00232  * @v hostname          NTP server
00233  * @ret rc              Return status code
00234  */
00235 int start_ntp ( struct interface *job, const char *hostname ) {
00236         struct ntp_client *ntp;
00237         union {
00238                 struct sockaddr_tcpip st;
00239                 struct sockaddr sa;
00240         } server;
00241         int rc;
00242 
00243         /* Allocate and initialise structure*/
00244         ntp = zalloc ( sizeof ( *ntp ) );
00245         if ( ! ntp ) {
00246                 rc = -ENOMEM;
00247                 goto err_alloc;
00248         }
00249         ref_init ( &ntp->refcnt, NULL );
00250         intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
00251         intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
00252         timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
00253         set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );
00254 
00255         /* Open socket */
00256         memset ( &server, 0, sizeof ( server ) );
00257         server.st.st_port = htons ( NTP_PORT );
00258         if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
00259                                              hostname, NULL ) ) != 0 ) {
00260                 DBGC ( ntp, "NTP %p could not open socket: %s\n",
00261                        ntp, strerror ( rc ) );
00262                 goto err_open;
00263         }
00264 
00265         /* Attach parent interface, mortalise self, and return */
00266         intf_plug_plug ( &ntp->job, job );
00267         ref_put ( &ntp->refcnt );
00268         return 0;
00269 
00270  err_open:
00271         ntp_close ( ntp, rc );
00272         ref_put ( &ntp->refcnt );
00273  err_alloc:
00274         return rc;
00275 }