summaryrefslogtreecommitdiffstats
path: root/src/net/udp/ntp.c
blob: 5592335755a94f88fc4c436a0027a840005962d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <ipxe/malloc.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/time.h>
#include <ipxe/tcpip.h>
#include <ipxe/dhcp.h>
#include <ipxe/settings.h>
#include <ipxe/ntp.h>

/** @file
 *
 * Network Time Protocol
 *
 */

/** An NTP client */
struct ntp_client {
	/** Reference count */
	struct refcnt refcnt;
	/** Job control interface */
	struct interface job;
	/** Data transfer interface */
	struct interface xfer;
	/** Retransmission timer */
	struct retry_timer timer;
};

/**
 * Close NTP client
 *
 * @v ntp		NTP client
 * @v rc		Reason for close
 */
static void ntp_close ( struct ntp_client *ntp, int rc ) {

	/* Stop timer */
	stop_timer ( &ntp->timer );

	/* Shut down interfaces */
	intf_shutdown ( &ntp->xfer, rc );
	intf_shutdown ( &ntp->job, rc );
}

/**
 * Send NTP request
 *
 * @v ntp		NTP client
 * @ret rc		Return status code
 */
static int ntp_request ( struct ntp_client *ntp ) {
	struct ntp_header hdr;
	int rc;

	DBGC ( ntp, "NTP %p sending request\n", ntp );

	/* Construct header */
	memset ( &hdr, 0, sizeof ( hdr ) );
	hdr.flags = ( NTP_FL_LI_UNKNOWN | NTP_FL_VN_1 | NTP_FL_MODE_CLIENT );
	hdr.transmit.seconds = htonl ( time ( NULL ) + NTP_EPOCH );
	hdr.transmit.fraction = htonl ( NTP_FRACTION_MAGIC );

	/* Send request */
	if ( ( rc = xfer_deliver_raw ( &ntp->xfer, &hdr,
				       sizeof ( hdr ) ) ) != 0 ) {
		DBGC ( ntp, "NTP %p could not send request: %s\n",
		       ntp, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Handle NTP response
 *
 * @v ntp		NTP client
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int ntp_deliver ( struct ntp_client *ntp, struct io_buffer *iobuf,
			 struct xfer_metadata *meta ) {
	struct ntp_header *hdr;
	struct sockaddr_tcpip *st_src;
	int32_t delta;
	int rc;

	/* Check source port */
	st_src = ( ( struct sockaddr_tcpip * ) meta->src );
	if ( st_src->st_port != htons ( NTP_PORT ) ) {
		DBGC ( ntp, "NTP %p received non-NTP packet:\n", ntp );
		DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
		goto ignore;
	}

	/* Check packet length */
	if ( iob_len ( iobuf ) < sizeof ( *hdr ) ) {
		DBGC ( ntp, "NTP %p received malformed packet:\n", ntp );
		DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
		goto ignore;
	}
	hdr = iobuf->data;

	/* Check mode */
	if ( ( hdr->flags & NTP_FL_MODE_MASK ) != NTP_FL_MODE_SERVER ) {
		DBGC ( ntp, "NTP %p received non-server packet:\n", ntp );
		DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
		goto ignore;
	}

	/* Check magic value */
	if ( hdr->originate.fraction != htonl ( NTP_FRACTION_MAGIC ) ) {
		DBGC ( ntp, "NTP %p received unrecognised packet:\n", ntp );
		DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
		goto ignore;
	}

	/* Check for Kiss-o'-Death packets */
	if ( ! hdr->stratum ) {
		DBGC ( ntp, "NTP %p received kiss-o'-death:\n", ntp );
		DBGC_HDA ( ntp, 0, iobuf->data, iob_len ( iobuf ) );
		rc = -EPROTO;
		goto close;
	}

	/* Calculate clock delta */
	delta = ( ntohl ( hdr->receive.seconds ) -
		  ntohl ( hdr->originate.seconds ) );
	DBGC ( ntp, "NTP %p delta %d seconds\n", ntp, delta );

	/* Adjust system clock */
	time_adjust ( delta );

	/* Success */
	rc = 0;

 close:
	ntp_close ( ntp, rc );
 ignore:
	free_iob ( iobuf );
	return 0;
}

/**
 * Handle data transfer window change
 *
 * @v ntp		NTP client
 */
static void ntp_window_changed ( struct ntp_client *ntp ) {

	/* Start timer to send initial request */
	start_timer_nodelay ( &ntp->timer );
}

/** Data transfer interface operations */
static struct interface_operation ntp_xfer_op[] = {
	INTF_OP ( xfer_deliver, struct ntp_client *, ntp_deliver ),
	INTF_OP ( xfer_window_changed, struct ntp_client *,
		  ntp_window_changed ),
	INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
};

/** Data transfer interface descriptor */
static struct interface_descriptor ntp_xfer_desc =
	INTF_DESC_PASSTHRU ( struct ntp_client, xfer, ntp_xfer_op, job );

/** Job control interface operations */
static struct interface_operation ntp_job_op[] = {
	INTF_OP ( intf_close, struct ntp_client *, ntp_close ),
};

/** Job control interface descriptor */
static struct interface_descriptor ntp_job_desc =
	INTF_DESC_PASSTHRU ( struct ntp_client, job, ntp_job_op, xfer );

/**
 * Handle NTP timer expiry
 *
 * @v timer		Retransmission timer
 * @v fail		Failure indicator
 */
static void ntp_expired ( struct retry_timer *timer, int fail ) {
	struct ntp_client *ntp =
		container_of ( timer, struct ntp_client, timer );

	/* Shut down client if we have failed */
	if ( fail ) {
		ntp_close ( ntp, -ETIMEDOUT );
		return;
	}

	/* Otherwise, restart timer and (re)transmit request */
	start_timer ( &ntp->timer );
	ntp_request ( ntp );
}

/**
 * Start NTP client
 *
 * @v job		Job control interface
 * @v hostname		NTP server
 * @ret rc		Return status code
 */
int start_ntp ( struct interface *job, const char *hostname ) {
	struct ntp_client *ntp;
	union {
		struct sockaddr_tcpip st;
		struct sockaddr sa;
	} server;
	int rc;

	/* Allocate and initialise structure*/
	ntp = zalloc ( sizeof ( *ntp ) );
	if ( ! ntp ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	ref_init ( &ntp->refcnt, NULL );
	intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
	intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
	timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
	set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );

	/* Open socket */
	memset ( &server, 0, sizeof ( server ) );
	server.st.st_port = htons ( NTP_PORT );
	if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
					     hostname, NULL ) ) != 0 ) {
		DBGC ( ntp, "NTP %p could not open socket: %s\n",
		       ntp, strerror ( rc ) );
		goto err_open;
	}

	/* Attach parent interface, mortalise self, and return */
	intf_plug_plug ( &ntp->job, job );
	ref_put ( &ntp->refcnt );
	return 0;

 err_open:
	ntp_close ( ntp, rc );
	ref_put ( &ntp->refcnt );
 err_alloc:
	return rc;
}

/** IPv4 NTP server setting */
const struct setting ntp_setting __setting ( SETTING_IP4_EXTRA, ntp ) = {
	.name = "ntp",
	.description = "NTP server",
	.tag = DHCP_NTP_SERVERS,
	.type = &setting_type_ipv4,
};