summaryrefslogtreecommitdiffstats
path: root/src/net/udp/tftp.c
blob: 69650ab0ff4de6fde3e03ac64ebabe29b532482a (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Copyright (C) 2006 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
#include <vsprintf.h>
#include <gpxe/async.h>
#include <gpxe/tftp.h>

/** @file
 *
 * TFTP protocol
 *
 */

/** A TFTP option */
struct tftp_option {
	/** Option name */
	const char *name;
	/** Option processor
	 *
	 * @v tftp	TFTP connection
	 * @v value	Option value
	 * @ret rc	Return status code
	 */
	int ( * process ) ( struct tftp_session *tftp, const char *value );
};

/**
 * Process TFTP "blksize" option
 *
 * @v tftp		TFTP connection
 * @v value		Option value
 * @ret rc		Return status code
 */
static int tftp_process_blksize ( struct tftp_session *tftp,
				  const char *value ) {
	char *end;

	tftp->blksize = strtoul ( value, &end, 10 );
	if ( *end ) {
		DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
		       tftp, value );
		return -EINVAL;
	}
	DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );

	return 0;
}

/**
 * Process TFTP "tsize" option
 *
 * @v tftp		TFTP connection
 * @v value		Option value
 * @ret rc		Return status code
 */
static int tftp_process_tsize ( struct tftp_session *tftp,
				const char *value ) {
	char *end;

	tftp->tsize = strtoul ( value, &end, 10 );
	if ( *end ) {
		DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
		       tftp, value );
		return -EINVAL;
	}
	DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );

	return 0;
}

/** Recognised TFTP options */
static struct tftp_option tftp_options[] = {
	{ "blksize", tftp_process_blksize },
	{ "tsize", tftp_process_tsize },
	{ NULL, NULL }
};

/**
 * Process TFTP option
 *
 * @v tftp		TFTP connection
 * @v name		Option name
 * @v value		Option value
 * @ret rc		Return status code
 */
static int tftp_process_option ( struct tftp_session *tftp,
				 const char *name, const char *value ) {
	struct tftp_option *option;

	for ( option = tftp_options ; option->name ; option++ ) {
		if ( strcasecmp ( name, option->name ) == 0 )
			return option->process ( tftp, value );
	}

	DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
	       tftp, name, value );

	return -EINVAL;
}

/** Translation between TFTP errors and internal error numbers */
static const uint8_t tftp_errors[] = {
	[TFTP_ERR_FILE_NOT_FOUND]	= PXENV_STATUS_TFTP_FILE_NOT_FOUND,
	[TFTP_ERR_ACCESS_DENIED]	= PXENV_STATUS_TFTP_ACCESS_VIOLATION,
	[TFTP_ERR_ILLEGAL_OP]		= PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
};

/**
 * Mark TFTP session as complete
 *
 * @v tftp		TFTP connection
 * @v rc		Return status code
 */
static void tftp_done ( struct tftp_session *tftp, int rc ) {

	/* Stop the retry timer */
	stop_timer ( &tftp->timer );

	/* Close UDP connection */
	udp_close ( &tftp->udp );

	/* Mark async operation as complete */
	async_done ( &tftp->async, rc );
}

/**
 * Send next packet in TFTP session
 *
 * @v tftp		TFTP connection
 */
static void tftp_send_packet ( struct tftp_session *tftp ) {
	start_timer ( &tftp->timer );
	udp_senddata ( &tftp->udp );
}

/**
 * Handle TFTP retransmission timer expiry
 *
 * @v timer		Retry timer
 * @v fail		Failure indicator
 */
static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
	struct tftp_session *tftp =
		container_of ( timer, struct tftp_session, timer );

	if ( fail ) {
		tftp_done ( tftp, -ETIMEDOUT );
	} else {
		tftp_send_packet ( tftp );
	}
}

/**
 * Mark TFTP block as received
 *
 * @v tftp		TFTP connection
 * @v block		Block number
 */
static void tftp_received ( struct tftp_session *tftp, unsigned int block ) {

	/* Stop the retry timer */
	stop_timer ( &tftp->timer );

	/* Update state to indicate which block we're now waiting for */
	tftp->state = block;

	/* Send next packet */
	tftp_send_packet ( tftp );
}

/**
 * Transmit RRQ
 *
 * @v tftp		TFTP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_send_rrq ( struct tftp_session *tftp, void *buf, size_t len ) {
	struct tftp_rrq *rrq = buf;
	void *data;
	void *end;

	DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, tftp->filename );

	data = rrq->data;
	end = ( buf + len );
	if ( data > end )
		goto overflow;
	data += ( snprintf ( data, ( end - data ),
			     "%s%coctet%cblksize%c%d%ctsize%c0",
			     tftp->filename, 0, 0, 0,
			     tftp->request_blksize, 0, 0 ) + 1 );
	if ( data > end )
		goto overflow;
	rrq->opcode = htons ( TFTP_RRQ );

	return udp_send ( &tftp->udp, buf, ( data - buf ) );

 overflow:
	DBGC ( tftp, "TFTP %p RRQ out of space\n", tftp );
	return -ENOBUFS;
}

/**
 * Receive OACK
 *
 * @v tftp		TFTP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_rx_oack ( struct tftp_session *tftp, void *buf, size_t len ) {
	struct tftp_oack *oack = buf;
	char *end = buf + len;
	char *name;
	char *value;
	int rc;

	/* Sanity check */
	if ( len < sizeof ( *oack ) ) {
		DBGC ( tftp, "TFTP %p received underlength OACK packet "
		       "length %d\n", tftp, len );
		return -EINVAL;
	}
	if ( end[-1] != '\0' ) {
		DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
		       tftp );
		return -EINVAL;
	}

	/* Process each option in turn */
	name = oack->data;
	while ( name < end ) {
		value = ( name + strlen ( name ) + 1 );
		if ( value == end ) {
			DBGC ( tftp, "TFTP %p received OACK missing value "
			       "for option \"%s\"\n", tftp, name );
			return -EINVAL;
		}
		if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
			return rc;
		name = ( value + strlen ( value ) + 1 );
	}

	/* Mark as received block 0 (the OACK) */
	tftp_received ( tftp, 0 );

	return 0;
}

/**
 * Receive DATA
 *
 * @v tftp		TFTP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_rx_data ( struct tftp_session *tftp, void *buf, size_t len ) {
	struct tftp_data *data = buf;
	unsigned int block;
	size_t data_offset;
	size_t data_len;
	int rc;

	/* Sanity check */
	if ( len < sizeof ( *data ) ) {
		DBGC ( tftp, "TFTP %p received underlength DATA packet "
		       "length %d\n", tftp, len );
		return -EINVAL;
	}

	/* Fill data buffer */
	block = ntohs ( data->block );
	data_offset = ( ( block - 1 ) * tftp->blksize );
	data_len = ( len - offsetof ( typeof ( *data ), data ) );
	if ( ( rc = fill_buffer ( tftp->buffer, data->data, data_offset,
				  data_len ) ) != 0 ) {
		DBGC ( tftp, "TFTP %p could not fill data buffer: %s\n",
		       tftp, strerror ( rc ) );
		tftp_done ( tftp, rc );
		return rc;
	}

	/* Mark block as received */
	tftp_received ( tftp, block );

	/* Finish when final block received */
	if ( data_len < tftp->blksize )
		tftp_done ( tftp, 0 );

	return 0;
}

/**
 * Transmit ACK
 *
 * @v tftp		TFTP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_send_ack ( struct tftp_session *tftp ) {
	struct tftp_ack ack;

	ack.opcode = htons ( TFTP_ACK );
	ack.block = htons ( tftp->state );
	return udp_send ( &tftp->udp, &ack, sizeof ( ack ) );
}

/**
 * Receive ERROR
 *
 * @v tftp		TFTP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_rx_error ( struct tftp_session *tftp, void *buf, size_t len ) {
	struct tftp_error *error = buf;
	unsigned int err;
	int rc = 0;

	/* Sanity check */
	if ( len < sizeof ( *error ) ) {
		DBGC ( tftp, "TFTP %p received underlength ERROR packet "
		       "length %d\n", tftp, len );
		return -EINVAL;
	}

	DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
	       "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
	
	/* Determine final operation result */
	err = ntohs ( error->errcode );
	if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
		rc = -tftp_errors[err];
	if ( ! rc )
		rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;

	/* Close TFTP session */
	tftp_done ( tftp, rc );

	return 0;
}

/**
 * Transmit data
 *
 * @v conn		UDP connection
 * @v buf		Temporary data buffer
 * @v len		Length of temporary data buffer
 * @ret rc		Return status code
 */
static int tftp_senddata ( struct udp_connection *conn,
			   void *buf, size_t len ) {
	struct tftp_session *tftp = 
		container_of ( conn, struct tftp_session, udp );

	if ( tftp->state < 0 ) {
		return tftp_send_rrq ( tftp, buf, len );
	} else {
		return tftp_send_ack ( tftp );
	}
}

/**
 * Receive new data
 *
 * @v udp		UDP connection
 * @v data		Received data
 * @v len		Length of received data
 * @v st_src		Partially-filled source address
 * @v st_dest		Partially-filled destination address
 */
static int tftp_newdata ( struct udp_connection *conn, void *data, size_t len,
			  struct sockaddr_tcpip *st_src __unused,
			  struct sockaddr_tcpip *st_dest __unused ) {
	struct tftp_session *tftp = 
		container_of ( conn, struct tftp_session, udp );
	struct tftp_common *common = data;
	
	if ( len < sizeof ( *common ) ) {
		DBGC ( tftp, "TFTP %p received underlength packet length %d\n",
		       tftp, len );
		return -EINVAL;
	}

	/* Filter by TID.  Set TID on first response received */
	if ( tftp->tid ) {
		if ( tftp->tid != st_src->st_port ) {
			DBGC ( tftp, "TFTP %p received packet from wrong port "
			       "(got %d, wanted %d)\n", tftp,
			       ntohs ( st_src->st_port ), ntohs ( tftp->tid ));
			return -EINVAL;
		}
	} else {
		tftp->tid = st_src->st_port;
		DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
		       ntohs ( tftp->tid ) );
		udp_connect_port ( &tftp->udp, tftp->tid );
	}

	/* Filter by source address */
	if ( memcmp ( st_src, udp_peer ( &tftp->udp ),
		      sizeof ( *st_src ) ) != 0 ) {
		DBGC ( tftp, "TFTP %p received packet from foreign source\n",
		       tftp );
		return -EINVAL;
	}

	switch ( common->opcode ) {
	case htons ( TFTP_OACK ):
		return tftp_rx_oack ( tftp, data, len );
	case htons ( TFTP_DATA ):
		return tftp_rx_data ( tftp, data, len );
	case htons ( TFTP_ERROR ):
		return tftp_rx_error ( tftp, data, len );
	default:
		DBGC ( tftp, "TFTP %p received strange packet type %d\n", tftp,
		       ntohs ( common->opcode ) );
		return -EINVAL;
	};
}

/** TFTP UDP operations */
static struct udp_operations tftp_udp_operations = {
	.senddata = tftp_senddata,
	.newdata = tftp_newdata,
};

/**
 * Initiate TFTP download
 *
 * @v tftp		TFTP session
 * @ret aop		Asynchronous operation
 */
struct async_operation * tftp_get ( struct tftp_session *tftp ) {
	int rc;

	assert ( tftp->filename != NULL );
	assert ( tftp->buffer != NULL );
	assert ( tftp->udp.peer.st_family != 0 );

	/* Initialise TFTP session */
	if ( ! tftp->request_blksize )
		tftp->request_blksize = TFTP_MAX_BLKSIZE;
	tftp->blksize = TFTP_DEFAULT_BLKSIZE;
	tftp->tsize = 0;
	tftp->tid = 0;
	tftp->state = -1;
	tftp->udp.udp_op = &tftp_udp_operations;
	tftp->timer.expired = tftp_timer_expired;

	/* Open UDP connection */
	if ( ( rc = udp_open ( &tftp->udp, 0 ) ) != 0 ) {
		async_done ( &tftp->async, rc );
		goto out;
	}

	/* Transmit initial RRQ */
	tftp_send_packet ( tftp );

 out:
	return &tftp->async;
}