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.

tftp.c 30KB

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