]> git.proxmox.com Git - grub2.git/blob - debian/grub-extras/disabled/gpxe/src/net/udp/tftp.c
grub2 (2.02+dfsg1-20) unstable; urgency=medium
[grub2.git] / debian / grub-extras / disabled / gpxe / src / net / udp / tftp.c
1 /*
2 * Copyright (C) 2006 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 FILE_LICENCE ( GPL2_OR_LATER );
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <byteswap.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <gpxe/refcnt.h>
30 #include <gpxe/xfer.h>
31 #include <gpxe/open.h>
32 #include <gpxe/uri.h>
33 #include <gpxe/tcpip.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/features.h>
36 #include <gpxe/bitmap.h>
37 #include <gpxe/settings.h>
38 #include <gpxe/dhcp.h>
39 #include <gpxe/uri.h>
40 #include <gpxe/tftp.h>
41
42 /** @file
43 *
44 * TFTP protocol
45 *
46 */
47
48 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
49
50 /* TFTP-specific error codes */
51 #define ETFTP_INVALID_BLKSIZE EUNIQ_01
52 #define ETFTP_INVALID_TSIZE EUNIQ_02
53 #define ETFTP_MC_NO_PORT EUNIQ_03
54 #define ETFTP_MC_NO_MC EUNIQ_04
55 #define ETFTP_MC_INVALID_MC EUNIQ_05
56 #define ETFTP_MC_INVALID_IP EUNIQ_06
57 #define ETFTP_MC_INVALID_PORT EUNIQ_07
58
59 /**
60 * A TFTP request
61 *
62 * This data structure holds the state for an ongoing TFTP transfer.
63 */
64 struct tftp_request {
65 /** Reference count */
66 struct refcnt refcnt;
67 /** Data transfer interface */
68 struct xfer_interface xfer;
69
70 /** URI being fetched */
71 struct uri *uri;
72 /** Transport layer interface */
73 struct xfer_interface socket;
74 /** Multicast transport layer interface */
75 struct xfer_interface mc_socket;
76
77 /** Data block size
78 *
79 * This is the "blksize" option negotiated with the TFTP
80 * server. (If the TFTP server does not support TFTP options,
81 * this will default to 512).
82 */
83 unsigned int blksize;
84 /** File size
85 *
86 * This is the value returned in the "tsize" option from the
87 * TFTP server. If the TFTP server does not support the
88 * "tsize" option, this value will be zero.
89 */
90 unsigned long tsize;
91
92 /** Server port
93 *
94 * This is the port to which RRQ packets are sent.
95 */
96 unsigned int port;
97 /** Peer address
98 *
99 * The peer address is determined by the first response
100 * received to the TFTP RRQ.
101 */
102 struct sockaddr_tcpip peer;
103 /** Request flags */
104 unsigned int flags;
105 /** MTFTP timeout count */
106 unsigned int mtftp_timeouts;
107
108 /** Block bitmap */
109 struct bitmap bitmap;
110 /** Maximum known length
111 *
112 * We don't always know the file length in advance. In
113 * particular, if the TFTP server doesn't support the tsize
114 * option, or we are using MTFTP, then we don't know the file
115 * length until we see the end-of-file block (which, in the
116 * case of MTFTP, may not be the last block we see).
117 *
118 * This value is updated whenever we obtain information about
119 * the file length.
120 */
121 size_t filesize;
122 /** Retransmission timer */
123 struct retry_timer timer;
124 };
125
126 /** TFTP request flags */
127 enum {
128 /** Send ACK packets */
129 TFTP_FL_SEND_ACK = 0x0001,
130 /** Request blksize and tsize options */
131 TFTP_FL_RRQ_SIZES = 0x0002,
132 /** Request multicast option */
133 TFTP_FL_RRQ_MULTICAST = 0x0004,
134 /** Perform MTFTP recovery on timeout */
135 TFTP_FL_MTFTP_RECOVERY = 0x0008,
136 };
137
138 /** Maximum number of MTFTP open requests before falling back to TFTP */
139 #define MTFTP_MAX_TIMEOUTS 3
140
141 /**
142 * Free TFTP request
143 *
144 * @v refcnt Reference counter
145 */
146 static void tftp_free ( struct refcnt *refcnt ) {
147 struct tftp_request *tftp =
148 container_of ( refcnt, struct tftp_request, refcnt );
149
150 uri_put ( tftp->uri );
151 bitmap_free ( &tftp->bitmap );
152 free ( tftp );
153 }
154
155 /**
156 * Mark TFTP request as complete
157 *
158 * @v tftp TFTP connection
159 * @v rc Return status code
160 */
161 static void tftp_done ( struct tftp_request *tftp, int rc ) {
162
163 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
164 tftp, rc, strerror ( rc ) );
165
166 /* Stop the retry timer */
167 stop_timer ( &tftp->timer );
168
169 /* Close all data transfer interfaces */
170 xfer_nullify ( &tftp->socket );
171 xfer_close ( &tftp->socket, rc );
172 xfer_nullify ( &tftp->mc_socket );
173 xfer_close ( &tftp->mc_socket, rc );
174 xfer_nullify ( &tftp->xfer );
175 xfer_close ( &tftp->xfer, rc );
176 }
177
178 /**
179 * Reopen TFTP socket
180 *
181 * @v tftp TFTP connection
182 * @ret rc Return status code
183 */
184 static int tftp_reopen ( struct tftp_request *tftp ) {
185 struct sockaddr_tcpip server;
186 int rc;
187
188 /* Close socket */
189 xfer_close ( &tftp->socket, 0 );
190
191 /* Disable ACK sending. */
192 tftp->flags &= ~TFTP_FL_SEND_ACK;
193
194 /* Reset peer address */
195 memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
196
197 /* Open socket */
198 memset ( &server, 0, sizeof ( server ) );
199 server.st_port = htons ( tftp->port );
200 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
201 ( struct sockaddr * ) &server,
202 tftp->uri->host, NULL ) ) != 0 ) {
203 DBGC ( tftp, "TFTP %p could not open socket: %s\n",
204 tftp, strerror ( rc ) );
205 return rc;
206 }
207
208 return 0;
209 }
210
211 /**
212 * Reopen TFTP multicast socket
213 *
214 * @v tftp TFTP connection
215 * @v local Local socket address
216 * @ret rc Return status code
217 */
218 static int tftp_reopen_mc ( struct tftp_request *tftp,
219 struct sockaddr *local ) {
220 int rc;
221
222 /* Close multicast socket */
223 xfer_close ( &tftp->mc_socket, 0 );
224
225 /* Open multicast socket. We never send via this socket, so
226 * use the local address as the peer address (since the peer
227 * address cannot be NULL).
228 */
229 if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
230 local, local ) ) != 0 ) {
231 DBGC ( tftp, "TFTP %p could not open multicast "
232 "socket: %s\n", tftp, strerror ( rc ) );
233 return rc;
234 }
235
236 return 0;
237 }
238
239 /**
240 * Presize TFTP receive buffers and block bitmap
241 *
242 * @v tftp TFTP connection
243 * @v filesize Known minimum file size
244 * @ret rc Return status code
245 */
246 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
247 unsigned int num_blocks;
248 int rc;
249
250 /* Do nothing if we are already large enough */
251 if ( filesize <= tftp->filesize )
252 return 0;
253
254 /* Record filesize */
255 tftp->filesize = filesize;
256
257 /* Notify recipient of file size */
258 xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
259 xfer_seek ( &tftp->xfer, 0, SEEK_SET );
260
261 /* Calculate expected number of blocks. Note that files whose
262 * length is an exact multiple of the blocksize will have a
263 * trailing zero-length block, which must be included.
264 */
265 num_blocks = ( ( filesize / tftp->blksize ) + 1 );
266 if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
267 DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
268 "%s\n", tftp, num_blocks, strerror ( rc ) );
269 return rc;
270 }
271
272 return 0;
273 }
274
275 /**
276 * TFTP requested blocksize
277 *
278 * This is treated as a global configuration parameter.
279 */
280 static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
281
282 /**
283 * Set TFTP request blocksize
284 *
285 * @v blksize Requested block size
286 */
287 void tftp_set_request_blksize ( unsigned int blksize ) {
288 if ( blksize < TFTP_DEFAULT_BLKSIZE )
289 blksize = TFTP_DEFAULT_BLKSIZE;
290 tftp_request_blksize = blksize;
291 }
292
293 /**
294 * MTFTP multicast receive address
295 *
296 * This is treated as a global configuration parameter.
297 */
298 static struct sockaddr_in tftp_mtftp_socket = {
299 .sin_family = AF_INET,
300 .sin_addr.s_addr = htonl ( 0xefff0101 ),
301 .sin_port = htons ( 3001 ),
302 };
303
304 /**
305 * Set MTFTP multicast address
306 *
307 * @v address Multicast IPv4 address
308 */
309 void tftp_set_mtftp_address ( struct in_addr address ) {
310 tftp_mtftp_socket.sin_addr = address;
311 }
312
313 /**
314 * Set MTFTP multicast port
315 *
316 * @v port Multicast port
317 */
318 void tftp_set_mtftp_port ( unsigned int port ) {
319 tftp_mtftp_socket.sin_port = htons ( port );
320 }
321
322 /**
323 * Transmit RRQ
324 *
325 * @v tftp TFTP connection
326 * @ret rc Return status code
327 */
328 static int tftp_send_rrq ( struct tftp_request *tftp ) {
329 struct tftp_rrq *rrq;
330 const char *path;
331 size_t len;
332 struct io_buffer *iobuf;
333
334 /* Strip initial '/' if present. If we were opened via the
335 * URI interface, then there will be an initial '/', since a
336 * full tftp:// URI provides no way to specify a non-absolute
337 * path. However, many TFTP servers (particularly Windows
338 * TFTP servers) complain about having an initial '/', and it
339 * violates user expectations to have a '/' silently added to
340 * the DHCP-specified filename.
341 */
342 path = tftp->uri->path;
343 if ( *path == '/' )
344 path++;
345
346 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
347
348 /* Allocate buffer */
349 len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
350 + 5 + 1 /* "octet" + NUL */
351 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
352 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
353 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
354 iobuf = xfer_alloc_iob ( &tftp->socket, len );
355 if ( ! iobuf )
356 return -ENOMEM;
357
358 /* Build request */
359 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
360 rrq->opcode = htons ( TFTP_RRQ );
361 iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
362 "%s%coctet", path, 0 ) + 1 );
363 if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
364 iob_put ( iobuf, snprintf ( iobuf->tail,
365 iob_tailroom ( iobuf ),
366 "blksize%c%d%ctsize%c0", 0,
367 tftp_request_blksize, 0, 0 ) + 1 );
368 }
369 if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
370 iob_put ( iobuf, snprintf ( iobuf->tail,
371 iob_tailroom ( iobuf ),
372 "multicast%c", 0 ) + 1 );
373 }
374
375 /* RRQ always goes to the address specified in the initial
376 * xfer_open() call
377 */
378 return xfer_deliver_iob ( &tftp->socket, iobuf );
379 }
380
381 /**
382 * Transmit ACK
383 *
384 * @v tftp TFTP connection
385 * @ret rc Return status code
386 */
387 static int tftp_send_ack ( struct tftp_request *tftp ) {
388 struct tftp_ack *ack;
389 struct io_buffer *iobuf;
390 struct xfer_metadata meta = {
391 .dest = ( struct sockaddr * ) &tftp->peer,
392 };
393 unsigned int block;
394
395 /* Determine next required block number */
396 block = bitmap_first_gap ( &tftp->bitmap );
397 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
398
399 /* Allocate buffer */
400 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
401 if ( ! iobuf )
402 return -ENOMEM;
403
404 /* Build ACK */
405 ack = iob_put ( iobuf, sizeof ( *ack ) );
406 ack->opcode = htons ( TFTP_ACK );
407 ack->block = htons ( block );
408
409 /* ACK always goes to the peer recorded from the RRQ response */
410 return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
411 }
412
413 /**
414 * Transmit next relevant packet
415 *
416 * @v tftp TFTP connection
417 * @ret rc Return status code
418 */
419 static int tftp_send_packet ( struct tftp_request *tftp ) {
420
421 /* Update retransmission timer */
422 stop_timer ( &tftp->timer );
423 start_timer ( &tftp->timer );
424
425 /* Send RRQ or ACK as appropriate */
426 if ( ! tftp->peer.st_family ) {
427 return tftp_send_rrq ( tftp );
428 } else {
429 if ( tftp->flags & TFTP_FL_SEND_ACK ) {
430 return tftp_send_ack ( tftp );
431 } else {
432 return 0;
433 }
434 }
435 }
436
437 /**
438 * Handle TFTP retransmission timer expiry
439 *
440 * @v timer Retry timer
441 * @v fail Failure indicator
442 */
443 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
444 struct tftp_request *tftp =
445 container_of ( timer, struct tftp_request, timer );
446 int rc;
447
448 /* If we are doing MTFTP, attempt the various recovery strategies */
449 if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
450 if ( tftp->peer.st_family ) {
451 /* If we have received any response from the server,
452 * try resending the RRQ to restart the download.
453 */
454 DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
455 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
456 goto err;
457 } else {
458 /* Fall back to plain TFTP after several attempts */
459 tftp->mtftp_timeouts++;
460 DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
461 "open\n", tftp, tftp->mtftp_timeouts );
462
463 if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
464 DBGC ( tftp, "TFTP %p falling back to plain "
465 "TFTP\n", tftp );
466 tftp->flags = TFTP_FL_RRQ_SIZES;
467
468 /* Close multicast socket */
469 xfer_close ( &tftp->mc_socket, 0 );
470
471 /* Reset retry timer */
472 start_timer_nodelay ( &tftp->timer );
473
474 /* The blocksize may change: discard
475 * the block bitmap
476 */
477 bitmap_free ( &tftp->bitmap );
478 memset ( &tftp->bitmap, 0,
479 sizeof ( tftp->bitmap ) );
480
481 /* Reopen on standard TFTP port */
482 tftp->port = TFTP_PORT;
483 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
484 goto err;
485 }
486 }
487 } else {
488 /* Not doing MTFTP (or have fallen back to plain
489 * TFTP); fail as per normal.
490 */
491 if ( fail ) {
492 rc = -ETIMEDOUT;
493 goto err;
494 }
495 }
496 tftp_send_packet ( tftp );
497 return;
498
499 err:
500 tftp_done ( tftp, rc );
501 }
502
503 /**
504 * Process TFTP "blksize" option
505 *
506 * @v tftp TFTP connection
507 * @v value Option value
508 * @ret rc Return status code
509 */
510 static int tftp_process_blksize ( struct tftp_request *tftp,
511 const char *value ) {
512 char *end;
513
514 tftp->blksize = strtoul ( value, &end, 10 );
515 if ( *end ) {
516 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
517 tftp, value );
518 return -( EINVAL | ETFTP_INVALID_BLKSIZE );
519 }
520 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
521
522 return 0;
523 }
524
525 /**
526 * Process TFTP "tsize" option
527 *
528 * @v tftp TFTP connection
529 * @v value Option value
530 * @ret rc Return status code
531 */
532 static int tftp_process_tsize ( struct tftp_request *tftp,
533 const char *value ) {
534 char *end;
535
536 tftp->tsize = strtoul ( value, &end, 10 );
537 if ( *end ) {
538 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
539 tftp, value );
540 return -( EINVAL | ETFTP_INVALID_TSIZE );
541 }
542 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
543
544 return 0;
545 }
546
547 /**
548 * Process TFTP "multicast" option
549 *
550 * @v tftp TFTP connection
551 * @v value Option value
552 * @ret rc Return status code
553 */
554 static int tftp_process_multicast ( struct tftp_request *tftp,
555 const char *value ) {
556 union {
557 struct sockaddr sa;
558 struct sockaddr_in sin;
559 } socket;
560 char buf[ strlen ( value ) + 1 ];
561 char *addr;
562 char *port;
563 char *port_end;
564 char *mc;
565 char *mc_end;
566 int rc;
567
568 /* Split value into "addr,port,mc" fields */
569 memcpy ( buf, value, sizeof ( buf ) );
570 addr = buf;
571 port = strchr ( addr, ',' );
572 if ( ! port ) {
573 DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
574 return -( EINVAL | ETFTP_MC_NO_PORT );
575 }
576 *(port++) = '\0';
577 mc = strchr ( port, ',' );
578 if ( ! mc ) {
579 DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
580 return -( EINVAL | ETFTP_MC_NO_MC );
581 }
582 *(mc++) = '\0';
583
584 /* Parse parameters */
585 if ( strtoul ( mc, &mc_end, 0 ) == 0 )
586 tftp->flags &= ~TFTP_FL_SEND_ACK;
587 if ( *mc_end ) {
588 DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
589 return -( EINVAL | ETFTP_MC_INVALID_MC );
590 }
591 DBGC ( tftp, "TFTP %p is%s the master client\n",
592 tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
593 if ( *addr && *port ) {
594 socket.sin.sin_family = AF_INET;
595 if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
596 DBGC ( tftp, "TFTP %p multicast invalid IP address "
597 "%s\n", tftp, addr );
598 return -( EINVAL | ETFTP_MC_INVALID_IP );
599 }
600 DBGC ( tftp, "TFTP %p multicast IP address %s\n",
601 tftp, inet_ntoa ( socket.sin.sin_addr ) );
602 socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
603 if ( *port_end ) {
604 DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
605 tftp, port );
606 return -( EINVAL | ETFTP_MC_INVALID_PORT );
607 }
608 DBGC ( tftp, "TFTP %p multicast port %d\n",
609 tftp, ntohs ( socket.sin.sin_port ) );
610 if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
611 return rc;
612 }
613
614 return 0;
615 }
616
617 /** A TFTP option */
618 struct tftp_option {
619 /** Option name */
620 const char *name;
621 /** Option processor
622 *
623 * @v tftp TFTP connection
624 * @v value Option value
625 * @ret rc Return status code
626 */
627 int ( * process ) ( struct tftp_request *tftp, const char *value );
628 };
629
630 /** Recognised TFTP options */
631 static struct tftp_option tftp_options[] = {
632 { "blksize", tftp_process_blksize },
633 { "tsize", tftp_process_tsize },
634 { "multicast", tftp_process_multicast },
635 { NULL, NULL }
636 };
637
638 /**
639 * Process TFTP option
640 *
641 * @v tftp TFTP connection
642 * @v name Option name
643 * @v value Option value
644 * @ret rc Return status code
645 */
646 static int tftp_process_option ( struct tftp_request *tftp,
647 const char *name, const char *value ) {
648 struct tftp_option *option;
649
650 for ( option = tftp_options ; option->name ; option++ ) {
651 if ( strcasecmp ( name, option->name ) == 0 )
652 return option->process ( tftp, value );
653 }
654
655 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
656 tftp, name, value );
657
658 /* Unknown options should be silently ignored */
659 return 0;
660 }
661
662 /**
663 * Receive OACK
664 *
665 * @v tftp TFTP connection
666 * @v buf Temporary data buffer
667 * @v len Length of temporary data buffer
668 * @ret rc Return status code
669 */
670 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
671 struct tftp_oack *oack = buf;
672 char *end = buf + len;
673 char *name;
674 char *value;
675 char *next;
676 int rc = 0;
677
678 /* Sanity check */
679 if ( len < sizeof ( *oack ) ) {
680 DBGC ( tftp, "TFTP %p received underlength OACK packet "
681 "length %zd\n", tftp, len );
682 rc = -EINVAL;
683 goto done;
684 }
685
686 /* Process each option in turn */
687 for ( name = oack->data ; name < end ; name = next ) {
688
689 /* Parse option name and value
690 *
691 * We treat parsing errors as non-fatal, because there
692 * exists at least one TFTP server (IBM Tivoli PXE
693 * Server 5.1.0.3) that has been observed to send
694 * malformed OACKs containing trailing garbage bytes.
695 */
696 value = ( name + strnlen ( name, ( end - name ) ) + 1 );
697 if ( value > end ) {
698 DBGC ( tftp, "TFTP %p received OACK with malformed "
699 "option name:\n", tftp );
700 DBGC_HD ( tftp, oack, len );
701 break;
702 }
703 if ( value == end ) {
704 DBGC ( tftp, "TFTP %p received OACK missing value "
705 "for option \"%s\"\n", tftp, name );
706 DBGC_HD ( tftp, oack, len );
707 break;
708 }
709 next = ( value + strnlen ( value, ( end - value ) ) + 1 );
710 if ( next > end ) {
711 DBGC ( tftp, "TFTP %p received OACK with malformed "
712 "value for option \"%s\":\n", tftp, name );
713 DBGC_HD ( tftp, oack, len );
714 break;
715 }
716
717 /* Process option */
718 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
719 goto done;
720 }
721
722 /* Process tsize information, if available */
723 if ( tftp->tsize ) {
724 if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
725 goto done;
726 }
727
728 /* Request next data block */
729 tftp_send_packet ( tftp );
730
731 done:
732 if ( rc )
733 tftp_done ( tftp, rc );
734 return rc;
735 }
736
737 /**
738 * Receive DATA
739 *
740 * @v tftp TFTP connection
741 * @v iobuf I/O buffer
742 * @ret rc Return status code
743 *
744 * Takes ownership of I/O buffer.
745 */
746 static int tftp_rx_data ( struct tftp_request *tftp,
747 struct io_buffer *iobuf ) {
748 struct tftp_data *data = iobuf->data;
749 struct xfer_metadata meta;
750 int block;
751 off_t offset;
752 size_t data_len;
753 int rc;
754
755 /* Sanity check */
756 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
757 DBGC ( tftp, "TFTP %p received underlength DATA packet "
758 "length %zd\n", tftp, iob_len ( iobuf ) );
759 rc = -EINVAL;
760 goto done;
761 }
762 if ( data->block == 0 ) {
763 DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
764 rc = -EINVAL;
765 goto done;
766 }
767
768 /* Extract data */
769 block = ( ntohs ( data->block ) - 1 );
770 offset = ( block * tftp->blksize );
771 iob_pull ( iobuf, sizeof ( *data ) );
772 data_len = iob_len ( iobuf );
773 if ( data_len > tftp->blksize ) {
774 DBGC ( tftp, "TFTP %p received overlength DATA packet "
775 "length %zd\n", tftp, data_len );
776 rc = -EINVAL;
777 goto done;
778 }
779
780 /* Deliver data */
781 memset ( &meta, 0, sizeof ( meta ) );
782 meta.whence = SEEK_SET;
783 meta.offset = offset;
784 if ( ( rc = xfer_deliver_iob_meta ( &tftp->xfer, iob_disown ( iobuf ),
785 &meta ) ) != 0 ) {
786 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
787 tftp, strerror ( rc ) );
788 goto done;
789 }
790
791 /* Ensure block bitmap is ready */
792 if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
793 goto done;
794
795 /* Mark block as received */
796 bitmap_set ( &tftp->bitmap, block );
797
798 /* Acknowledge block */
799 tftp_send_packet ( tftp );
800
801 /* If all blocks have been received, finish. */
802 if ( bitmap_full ( &tftp->bitmap ) )
803 tftp_done ( tftp, 0 );
804
805 done:
806 free_iob ( iobuf );
807 if ( rc )
808 tftp_done ( tftp, rc );
809 return rc;
810 }
811
812 /** Translation between TFTP errors and internal error numbers */
813 static const int tftp_errors[] = {
814 [TFTP_ERR_FILE_NOT_FOUND] = ENOENT,
815 [TFTP_ERR_ACCESS_DENIED] = EACCES,
816 [TFTP_ERR_ILLEGAL_OP] = ENOTSUP,
817 };
818
819 /**
820 * Receive ERROR
821 *
822 * @v tftp TFTP connection
823 * @v buf Temporary data buffer
824 * @v len Length of temporary data buffer
825 * @ret rc Return status code
826 */
827 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
828 struct tftp_error *error = buf;
829 unsigned int err;
830 int rc = 0;
831
832 /* Sanity check */
833 if ( len < sizeof ( *error ) ) {
834 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
835 "length %zd\n", tftp, len );
836 return -EINVAL;
837 }
838
839 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
840 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
841
842 /* Determine final operation result */
843 err = ntohs ( error->errcode );
844 if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
845 rc = -tftp_errors[err];
846 if ( ! rc )
847 rc = -ENOTSUP;
848
849 /* Close TFTP request */
850 tftp_done ( tftp, rc );
851
852 return 0;
853 }
854
855 /**
856 * Receive new data
857 *
858 * @v tftp TFTP connection
859 * @v iobuf I/O buffer
860 * @v meta Transfer metadata
861 * @ret rc Return status code
862 */
863 static int tftp_rx ( struct tftp_request *tftp,
864 struct io_buffer *iobuf,
865 struct xfer_metadata *meta ) {
866 struct sockaddr_tcpip *st_src;
867 struct tftp_common *common = iobuf->data;
868 size_t len = iob_len ( iobuf );
869 int rc = -EINVAL;
870
871 /* Sanity checks */
872 if ( len < sizeof ( *common ) ) {
873 DBGC ( tftp, "TFTP %p received underlength packet length "
874 "%zd\n", tftp, len );
875 goto done;
876 }
877 if ( ! meta->src ) {
878 DBGC ( tftp, "TFTP %p received packet without source port\n",
879 tftp );
880 goto done;
881 }
882
883 /* Filter by TID. Set TID on first response received */
884 st_src = ( struct sockaddr_tcpip * ) meta->src;
885 if ( ! tftp->peer.st_family ) {
886 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
887 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
888 ntohs ( tftp->peer.st_port ) );
889 } else if ( memcmp ( &tftp->peer, st_src,
890 sizeof ( tftp->peer ) ) != 0 ) {
891 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
892 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
893 ntohs ( tftp->peer.st_port ) );
894 goto done;
895 }
896
897 switch ( common->opcode ) {
898 case htons ( TFTP_OACK ):
899 rc = tftp_rx_oack ( tftp, iobuf->data, len );
900 break;
901 case htons ( TFTP_DATA ):
902 rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
903 break;
904 case htons ( TFTP_ERROR ):
905 rc = tftp_rx_error ( tftp, iobuf->data, len );
906 break;
907 default:
908 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
909 tftp, ntohs ( common->opcode ) );
910 break;
911 };
912
913 done:
914 free_iob ( iobuf );
915 return rc;
916 }
917
918 /**
919 * Receive new data via socket
920 *
921 * @v socket Transport layer interface
922 * @v iobuf I/O buffer
923 * @v meta Transfer metadata
924 * @ret rc Return status code
925 */
926 static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
927 struct io_buffer *iobuf,
928 struct xfer_metadata *meta ) {
929 struct tftp_request *tftp =
930 container_of ( socket, struct tftp_request, socket );
931
932 /* Enable sending ACKs when we receive a unicast packet. This
933 * covers three cases:
934 *
935 * 1. Standard TFTP; we should always send ACKs, and will
936 * always receive a unicast packet before we need to send the
937 * first ACK.
938 *
939 * 2. RFC2090 multicast TFTP; the only unicast packets we will
940 * receive are the OACKs; enable sending ACKs here (before
941 * processing the OACK) and disable it when processing the
942 * multicast option if we are not the master client.
943 *
944 * 3. MTFTP; receiving a unicast datagram indicates that we
945 * are the "master client" and should send ACKs.
946 */
947 tftp->flags |= TFTP_FL_SEND_ACK;
948
949 return tftp_rx ( tftp, iobuf, meta );
950 }
951
952 /** TFTP socket operations */
953 static struct xfer_interface_operations tftp_socket_operations = {
954 .close = ignore_xfer_close,
955 .vredirect = xfer_vreopen,
956 .window = unlimited_xfer_window,
957 .alloc_iob = default_xfer_alloc_iob,
958 .deliver_iob = tftp_socket_deliver_iob,
959 .deliver_raw = xfer_deliver_as_iob,
960 };
961
962 /**
963 * Receive new data via multicast socket
964 *
965 * @v mc_socket Multicast transport layer interface
966 * @v iobuf I/O buffer
967 * @v meta Transfer metadata
968 * @ret rc Return status code
969 */
970 static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
971 struct io_buffer *iobuf,
972 struct xfer_metadata *meta ) {
973 struct tftp_request *tftp =
974 container_of ( mc_socket, struct tftp_request, mc_socket );
975
976 return tftp_rx ( tftp, iobuf, meta );
977 }
978
979 /** TFTP multicast socket operations */
980 static struct xfer_interface_operations tftp_mc_socket_operations = {
981 .close = ignore_xfer_close,
982 .vredirect = xfer_vreopen,
983 .window = unlimited_xfer_window,
984 .alloc_iob = default_xfer_alloc_iob,
985 .deliver_iob = tftp_mc_socket_deliver_iob,
986 .deliver_raw = xfer_deliver_as_iob,
987 };
988
989 /**
990 * Close TFTP data transfer interface
991 *
992 * @v xfer Data transfer interface
993 * @v rc Reason for close
994 */
995 static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
996 struct tftp_request *tftp =
997 container_of ( xfer, struct tftp_request, xfer );
998
999 DBGC ( tftp, "TFTP %p interface closed: %s\n",
1000 tftp, strerror ( rc ) );
1001
1002 tftp_done ( tftp, rc );
1003 }
1004
1005 /**
1006 * Check flow control window
1007 *
1008 * @v xfer Data transfer interface
1009 * @ret len Length of window
1010 */
1011 static size_t tftp_xfer_window ( struct xfer_interface *xfer ) {
1012 struct tftp_request *tftp =
1013 container_of ( xfer, struct tftp_request, xfer );
1014
1015 /* We abuse this data-xfer method to convey the blocksize to
1016 * the caller. This really should be done using some kind of
1017 * stat() method, but we don't yet have the facility to do
1018 * that.
1019 */
1020 return tftp->blksize;
1021 }
1022
1023 /** TFTP data transfer interface operations */
1024 static struct xfer_interface_operations tftp_xfer_operations = {
1025 .close = tftp_xfer_close,
1026 .vredirect = ignore_xfer_vredirect,
1027 .window = tftp_xfer_window,
1028 .alloc_iob = default_xfer_alloc_iob,
1029 .deliver_iob = xfer_deliver_as_raw,
1030 .deliver_raw = ignore_xfer_deliver_raw,
1031 };
1032
1033 /**
1034 * Initiate TFTP/TFTM/MTFTP download
1035 *
1036 * @v xfer Data transfer interface
1037 * @v uri Uniform Resource Identifier
1038 * @ret rc Return status code
1039 */
1040 static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
1041 unsigned int default_port,
1042 struct sockaddr *multicast,
1043 unsigned int flags ) {
1044 struct tftp_request *tftp;
1045 int rc;
1046
1047 /* Sanity checks */
1048 if ( ! uri->host )
1049 return -EINVAL;
1050 if ( ! uri->path )
1051 return -EINVAL;
1052
1053 /* Allocate and populate TFTP structure */
1054 tftp = zalloc ( sizeof ( *tftp ) );
1055 if ( ! tftp )
1056 return -ENOMEM;
1057 tftp->refcnt.free = tftp_free;
1058 xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
1059 tftp->uri = uri_get ( uri );
1060 xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
1061 xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
1062 &tftp->refcnt );
1063 tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1064 tftp->flags = flags;
1065 tftp->timer.expired = tftp_timer_expired;
1066
1067 /* Open socket */
1068 tftp->port = uri_port ( tftp->uri, default_port );
1069 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1070 goto err;
1071
1072 /* Open multicast socket */
1073 if ( multicast ) {
1074 if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1075 goto err;
1076 }
1077
1078 /* Start timer to initiate RRQ */
1079 start_timer_nodelay ( &tftp->timer );
1080
1081 /* Attach to parent interface, mortalise self, and return */
1082 xfer_plug_plug ( &tftp->xfer, xfer );
1083 ref_put ( &tftp->refcnt );
1084 return 0;
1085
1086 err:
1087 DBGC ( tftp, "TFTP %p could not create request: %s\n",
1088 tftp, strerror ( rc ) );
1089 tftp_done ( tftp, rc );
1090 ref_put ( &tftp->refcnt );
1091 return rc;
1092 }
1093
1094 /**
1095 * Initiate TFTP download
1096 *
1097 * @v xfer Data transfer interface
1098 * @v uri Uniform Resource Identifier
1099 * @ret rc Return status code
1100 */
1101 static int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1102 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1103 TFTP_FL_RRQ_SIZES );
1104
1105 }
1106
1107 /** TFTP URI opener */
1108 struct uri_opener tftp_uri_opener __uri_opener = {
1109 .scheme = "tftp",
1110 .open = tftp_open,
1111 };
1112
1113 /**
1114 * Initiate TFTM download
1115 *
1116 * @v xfer Data transfer interface
1117 * @v uri Uniform Resource Identifier
1118 * @ret rc Return status code
1119 */
1120 static int tftm_open ( struct xfer_interface *xfer, struct uri *uri ) {
1121 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1122 ( TFTP_FL_RRQ_SIZES |
1123 TFTP_FL_RRQ_MULTICAST ) );
1124
1125 }
1126
1127 /** TFTM URI opener */
1128 struct uri_opener tftm_uri_opener __uri_opener = {
1129 .scheme = "tftm",
1130 .open = tftm_open,
1131 };
1132
1133 /**
1134 * Initiate MTFTP download
1135 *
1136 * @v xfer Data transfer interface
1137 * @v uri Uniform Resource Identifier
1138 * @ret rc Return status code
1139 */
1140 static int mtftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1141 return tftp_core_open ( xfer, uri, MTFTP_PORT,
1142 ( struct sockaddr * ) &tftp_mtftp_socket,
1143 TFTP_FL_MTFTP_RECOVERY );
1144 }
1145
1146 /** MTFTP URI opener */
1147 struct uri_opener mtftp_uri_opener __uri_opener = {
1148 .scheme = "mtftp",
1149 .open = mtftp_open,
1150 };
1151
1152 /******************************************************************************
1153 *
1154 * Settings
1155 *
1156 ******************************************************************************
1157 */
1158
1159 /** TFTP server setting */
1160 struct setting next_server_setting __setting = {
1161 .name = "next-server",
1162 .description = "TFTP server",
1163 .tag = DHCP_EB_SIADDR,
1164 .type = &setting_type_ipv4,
1165 };
1166
1167 /**
1168 * Apply TFTP configuration settings
1169 *
1170 * @ret rc Return status code
1171 */
1172 static int tftp_apply_settings ( void ) {
1173 static struct in_addr tftp_server = { 0 };
1174 struct in_addr last_tftp_server;
1175 char uri_string[32];
1176 struct uri *uri;
1177
1178 /* Retrieve TFTP server setting */
1179 last_tftp_server = tftp_server;
1180 fetch_ipv4_setting ( NULL, &next_server_setting, &tftp_server );
1181
1182 /* If TFTP server setting has changed, set the current working
1183 * URI to match. Do it only when the TFTP server has changed
1184 * to try to minimise surprises to the user, who probably
1185 * won't expect the CWURI to change just because they updated
1186 * an unrelated setting and triggered all the settings
1187 * applicators.
1188 */
1189 if ( tftp_server.s_addr != last_tftp_server.s_addr ) {
1190 snprintf ( uri_string, sizeof ( uri_string ),
1191 "tftp://%s/", inet_ntoa ( tftp_server ) );
1192 uri = parse_uri ( uri_string );
1193 if ( ! uri )
1194 return -ENOMEM;
1195 churi ( uri );
1196 uri_put ( uri );
1197 }
1198
1199 return 0;
1200 }
1201
1202 /** TFTP settings applicator */
1203 struct settings_applicator tftp_settings_applicator __settings_applicator = {
1204 .apply = tftp_apply_settings,
1205 };