您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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/tftp.h>
  35. /** @file
  36. *
  37. * TFTP protocol
  38. *
  39. */
  40. FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
  41. /**
  42. * A TFTP request
  43. *
  44. * This data structure holds the state for an ongoing TFTP transfer.
  45. */
  46. struct tftp_request {
  47. /** Reference count */
  48. struct refcnt refcnt;
  49. /** Data transfer interface */
  50. struct xfer_interface xfer;
  51. /** URI being fetched */
  52. struct uri *uri;
  53. /** Transport layer interface */
  54. struct xfer_interface socket;
  55. /** Multicast transport layer interface */
  56. struct xfer_interface mc_socket;
  57. /** Data block size
  58. *
  59. * This is the "blksize" option negotiated with the TFTP
  60. * server. (If the TFTP server does not support TFTP options,
  61. * this will default to 512).
  62. */
  63. unsigned int blksize;
  64. /** File size
  65. *
  66. * This is the value returned in the "tsize" option from the
  67. * TFTP server. If the TFTP server does not support the
  68. * "tsize" option, this value will be zero.
  69. */
  70. unsigned long tsize;
  71. /** Multicast address
  72. *
  73. * This is the destination address for multicast data
  74. * transmissions.
  75. */
  76. struct sockaddr_tcpip multicast;
  77. /** Master client
  78. *
  79. * True if this is the client responsible for sending ACKs.
  80. */
  81. int master;
  82. /** Peer address
  83. *
  84. * The peer address is determined by the first response
  85. * received to the TFTP RRQ.
  86. */
  87. struct sockaddr_tcpip peer;
  88. /** Block bitmap */
  89. struct bitmap bitmap;
  90. /** Maximum known length
  91. *
  92. * We don't always know the file length in advance. In
  93. * particular, if the TFTP server doesn't support the tsize
  94. * option, or we are using MTFTP, then we don't know the file
  95. * length until we see the end-of-file block (which, in the
  96. * case of MTFTP, may not be the last block we see).
  97. *
  98. * This value is updated whenever we obtain information about
  99. * the file length.
  100. */
  101. size_t filesize;
  102. /** Retransmission timer */
  103. struct retry_timer timer;
  104. };
  105. /**
  106. * Free TFTP request
  107. *
  108. * @v refcnt Reference counter
  109. */
  110. static void tftp_free ( struct refcnt *refcnt ) {
  111. struct tftp_request *tftp =
  112. container_of ( refcnt, struct tftp_request, refcnt );
  113. uri_put ( tftp->uri );
  114. bitmap_free ( &tftp->bitmap );
  115. free ( tftp );
  116. }
  117. /**
  118. * Mark TFTP request as complete
  119. *
  120. * @v tftp TFTP connection
  121. * @v rc Return status code
  122. */
  123. static void tftp_done ( struct tftp_request *tftp, int rc ) {
  124. DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
  125. tftp, rc, strerror ( rc ) );
  126. /* Stop the retry timer */
  127. stop_timer ( &tftp->timer );
  128. /* Close all data transfer interfaces */
  129. xfer_nullify ( &tftp->socket );
  130. xfer_close ( &tftp->socket, rc );
  131. xfer_nullify ( &tftp->mc_socket );
  132. xfer_close ( &tftp->mc_socket, rc );
  133. xfer_nullify ( &tftp->xfer );
  134. xfer_close ( &tftp->xfer, rc );
  135. }
  136. /**
  137. * Presize TFTP receive buffers and block bitmap
  138. *
  139. * @v tftp TFTP connection
  140. * @v filesize Known minimum file size
  141. * @ret rc Return status code
  142. */
  143. static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
  144. unsigned int num_blocks;
  145. int rc;
  146. /* Do nothing if we are already large enough */
  147. if ( filesize <= tftp->filesize )
  148. return 0;
  149. /* Record filesize */
  150. tftp->filesize = filesize;
  151. /* Notify recipient of file size */
  152. xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
  153. xfer_seek ( &tftp->xfer, 0, SEEK_SET );
  154. /* Calculate expected number of blocks. Note that files whose
  155. * length is an exact multiple of the blocksize will have a
  156. * trailing zero-length block, which must be included.
  157. */
  158. num_blocks = ( ( filesize / tftp->blksize ) + 1 );
  159. if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
  160. DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
  161. "%s\n", tftp, num_blocks, strerror ( rc ) );
  162. return rc;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * TFTP requested blocksize
  168. *
  169. * This is treated as a global configuration parameter.
  170. */
  171. static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
  172. /**
  173. * Set TFTP request blocksize
  174. *
  175. * @v blksize Requested block size
  176. */
  177. void tftp_set_request_blksize ( unsigned int blksize ) {
  178. if ( blksize < TFTP_DEFAULT_BLKSIZE )
  179. blksize = TFTP_DEFAULT_BLKSIZE;
  180. tftp_request_blksize = blksize;
  181. }
  182. /**
  183. * Transmit RRQ
  184. *
  185. * @v tftp TFTP connection
  186. * @ret rc Return status code
  187. */
  188. static int tftp_send_rrq ( struct tftp_request *tftp ) {
  189. struct tftp_rrq *rrq;
  190. const char *path = tftp->uri->path;
  191. size_t len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
  192. + 5 + 1 /* "octet" + NUL */
  193. + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
  194. + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
  195. + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
  196. struct io_buffer *iobuf;
  197. DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
  198. /* Allocate buffer */
  199. iobuf = xfer_alloc_iob ( &tftp->socket, len );
  200. if ( ! iobuf )
  201. return -ENOMEM;
  202. /* Build request */
  203. rrq = iob_put ( iobuf, sizeof ( *rrq ) );
  204. rrq->opcode = htons ( TFTP_RRQ );
  205. iob_put ( iobuf,
  206. snprintf ( rrq->data, iob_tailroom ( iobuf ),
  207. "%s%coctet%cblksize%c%d%ctsize%c0%cmulticast%c",
  208. path, 0, 0, 0, tftp_request_blksize, 0,
  209. 0, 0, 0 ) + 1 );
  210. /* RRQ always goes to the address specified in the initial
  211. * xfer_open() call
  212. */
  213. return xfer_deliver_iob ( &tftp->socket, iobuf );
  214. }
  215. /**
  216. * Transmit ACK
  217. *
  218. * @v tftp TFTP connection
  219. * @ret rc Return status code
  220. */
  221. static int tftp_send_ack ( struct tftp_request *tftp ) {
  222. struct tftp_ack *ack;
  223. struct io_buffer *iobuf;
  224. struct xfer_metadata meta = {
  225. .dest = ( struct sockaddr * ) &tftp->peer,
  226. };
  227. unsigned int block;
  228. /* Determine next required block number */
  229. block = bitmap_first_gap ( &tftp->bitmap );
  230. DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
  231. /* Allocate buffer */
  232. iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
  233. if ( ! iobuf )
  234. return -ENOMEM;
  235. /* Build ACK */
  236. ack = iob_put ( iobuf, sizeof ( *ack ) );
  237. ack->opcode = htons ( TFTP_ACK );
  238. ack->block = htons ( block );
  239. /* ACK always goes to the peer recorded from the RRQ response */
  240. return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
  241. }
  242. /**
  243. * Transmit next relevant packet
  244. *
  245. * @v tftp TFTP connection
  246. * @ret rc Return status code
  247. */
  248. static int tftp_send_packet ( struct tftp_request *tftp ) {
  249. /* Update retransmission timer */
  250. stop_timer ( &tftp->timer );
  251. start_timer ( &tftp->timer );
  252. /* If we are the master client, send RRQ or ACK as appropriate */
  253. if ( tftp->master ) {
  254. if ( ! tftp->peer.st_family ) {
  255. return tftp_send_rrq ( tftp );
  256. } else {
  257. return tftp_send_ack ( tftp );
  258. }
  259. } else {
  260. /* Do nothing when not the master client */
  261. return 0;
  262. }
  263. }
  264. /**
  265. * Handle TFTP retransmission timer expiry
  266. *
  267. * @v timer Retry timer
  268. * @v fail Failure indicator
  269. */
  270. static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
  271. struct tftp_request *tftp =
  272. container_of ( timer, struct tftp_request, timer );
  273. if ( fail ) {
  274. tftp_done ( tftp, -ETIMEDOUT );
  275. } else {
  276. tftp_send_packet ( tftp );
  277. }
  278. }
  279. /**
  280. * Process TFTP "blksize" option
  281. *
  282. * @v tftp TFTP connection
  283. * @v value Option value
  284. * @ret rc Return status code
  285. */
  286. static int tftp_process_blksize ( struct tftp_request *tftp,
  287. const char *value ) {
  288. char *end;
  289. tftp->blksize = strtoul ( value, &end, 10 );
  290. if ( *end ) {
  291. DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
  292. tftp, value );
  293. return -EINVAL;
  294. }
  295. DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
  296. return 0;
  297. }
  298. /**
  299. * Process TFTP "tsize" option
  300. *
  301. * @v tftp TFTP connection
  302. * @v value Option value
  303. * @ret rc Return status code
  304. */
  305. static int tftp_process_tsize ( struct tftp_request *tftp,
  306. const char *value ) {
  307. char *end;
  308. tftp->tsize = strtoul ( value, &end, 10 );
  309. if ( *end ) {
  310. DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
  311. tftp, value );
  312. return -EINVAL;
  313. }
  314. DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
  315. return 0;
  316. }
  317. /**
  318. * Process TFTP "multicast" option
  319. *
  320. * @v tftp TFTP connection
  321. * @v value Option value
  322. * @ret rc Return status code
  323. */
  324. static int tftp_process_multicast ( struct tftp_request *tftp,
  325. const char *value ) {
  326. struct sockaddr_in *sin = ( struct sockaddr_in * ) &tftp->multicast;
  327. char buf[ strlen ( value ) + 1 ];
  328. char *addr;
  329. char *port;
  330. char *port_end;
  331. char *mc;
  332. char *mc_end;
  333. struct sockaddr *mc_peer;
  334. struct sockaddr *mc_local;
  335. int rc;
  336. /* Split value into "addr,port,mc" fields */
  337. memcpy ( buf, value, sizeof ( buf ) );
  338. addr = buf;
  339. port = strchr ( addr, ',' );
  340. if ( ! port ) {
  341. DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
  342. return -EINVAL;
  343. }
  344. *(port++) = '\0';
  345. mc = strchr ( port, ',' );
  346. if ( ! mc ) {
  347. DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
  348. return -EINVAL;
  349. }
  350. *(mc++) = '\0';
  351. /* Parse parameters */
  352. if ( *addr ) {
  353. if ( inet_aton ( addr, &sin->sin_addr ) == 0 ) {
  354. DBGC ( tftp, "TFTP %p multicast invalid IP address "
  355. "%s\n", tftp, addr );
  356. return -EINVAL;
  357. }
  358. DBGC ( tftp, "TFTP %p multicast IP address %s\n",
  359. tftp, inet_ntoa ( sin->sin_addr ) );
  360. }
  361. if ( *port ) {
  362. sin->sin_port = htons ( strtoul ( port, &port_end, 0 ) );
  363. if ( *port_end ) {
  364. DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
  365. tftp, port );
  366. return -EINVAL;
  367. }
  368. DBGC ( tftp, "TFTP %p multicast port %d\n",
  369. tftp, ntohs ( sin->sin_port ) );
  370. }
  371. tftp->master = strtoul ( mc, &mc_end, 0 );
  372. if ( *mc_end ) {
  373. DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
  374. return -EINVAL;
  375. }
  376. DBGC ( tftp, "TFTP %p is%s the master client\n",
  377. tftp, ( tftp->master ? "" : " not" ) );
  378. /* Open multicast socket, if new address specified */
  379. if ( *addr || *port ) {
  380. xfer_close ( &tftp->mc_socket, 0 );
  381. mc_peer = ( ( struct sockaddr * ) &tftp->peer );
  382. mc_local = ( ( struct sockaddr * ) &tftp->multicast );
  383. mc_local->sa_family = mc_peer->sa_family;
  384. if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
  385. mc_peer, mc_local ) ) != 0 ) {
  386. DBGC ( tftp, "TFTP %p could not open multicast "
  387. "socket: %s\n", tftp, strerror ( rc ) );
  388. return rc;
  389. }
  390. }
  391. return 0;
  392. }
  393. /** A TFTP option */
  394. struct tftp_option {
  395. /** Option name */
  396. const char *name;
  397. /** Option processor
  398. *
  399. * @v tftp TFTP connection
  400. * @v value Option value
  401. * @ret rc Return status code
  402. */
  403. int ( * process ) ( struct tftp_request *tftp, const char *value );
  404. };
  405. /** Recognised TFTP options */
  406. static struct tftp_option tftp_options[] = {
  407. { "blksize", tftp_process_blksize },
  408. { "tsize", tftp_process_tsize },
  409. { "multicast", tftp_process_multicast },
  410. { NULL, NULL }
  411. };
  412. /**
  413. * Process TFTP option
  414. *
  415. * @v tftp TFTP connection
  416. * @v name Option name
  417. * @v value Option value
  418. * @ret rc Return status code
  419. */
  420. static int tftp_process_option ( struct tftp_request *tftp,
  421. const char *name, const char *value ) {
  422. struct tftp_option *option;
  423. for ( option = tftp_options ; option->name ; option++ ) {
  424. if ( strcasecmp ( name, option->name ) == 0 )
  425. return option->process ( tftp, value );
  426. }
  427. DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
  428. tftp, name, value );
  429. /* Unknown options should be silently ignored */
  430. return 0;
  431. }
  432. /**
  433. * Receive OACK
  434. *
  435. * @v tftp TFTP connection
  436. * @v buf Temporary data buffer
  437. * @v len Length of temporary data buffer
  438. * @ret rc Return status code
  439. */
  440. static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
  441. struct tftp_oack *oack = buf;
  442. char *end = buf + len;
  443. char *name;
  444. char *value;
  445. int rc = 0;
  446. /* Sanity check */
  447. if ( len < sizeof ( *oack ) ) {
  448. DBGC ( tftp, "TFTP %p received underlength OACK packet "
  449. "length %zd\n", tftp, len );
  450. rc = -EINVAL;
  451. goto done;
  452. }
  453. if ( end[-1] != '\0' ) {
  454. DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
  455. tftp );
  456. rc = -EINVAL;
  457. goto done;
  458. }
  459. /* Process each option in turn */
  460. name = oack->data;
  461. while ( name < end ) {
  462. value = ( name + strlen ( name ) + 1 );
  463. if ( value == end ) {
  464. DBGC ( tftp, "TFTP %p received OACK missing value "
  465. "for option \"%s\"\n", tftp, name );
  466. rc = -EINVAL;
  467. goto done;
  468. }
  469. if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
  470. goto done;
  471. name = ( value + strlen ( value ) + 1 );
  472. }
  473. /* Process tsize information, if available */
  474. if ( tftp->tsize ) {
  475. if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
  476. goto done;
  477. }
  478. /* Request next data block */
  479. tftp_send_packet ( tftp );
  480. done:
  481. if ( rc )
  482. tftp_done ( tftp, rc );
  483. return rc;
  484. }
  485. /**
  486. * Receive DATA
  487. *
  488. * @v tftp TFTP connection
  489. * @v iobuf I/O buffer
  490. * @ret rc Return status code
  491. *
  492. * Takes ownership of I/O buffer.
  493. */
  494. static int tftp_rx_data ( struct tftp_request *tftp,
  495. struct io_buffer *iobuf ) {
  496. struct tftp_data *data = iobuf->data;
  497. int block;
  498. off_t offset;
  499. size_t data_len;
  500. int rc;
  501. /* Sanity check */
  502. if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
  503. DBGC ( tftp, "TFTP %p received underlength DATA packet "
  504. "length %zd\n", tftp, iob_len ( iobuf ) );
  505. rc = -EINVAL;
  506. goto done;
  507. }
  508. /* Extract data */
  509. block = ( ntohs ( data->block ) - 1 );
  510. offset = ( block * tftp->blksize );
  511. iob_pull ( iobuf, sizeof ( *data ) );
  512. data_len = iob_len ( iobuf );
  513. if ( data_len > tftp->blksize ) {
  514. DBGC ( tftp, "TFTP %p received overlength DATA packet "
  515. "length %zd\n", tftp, data_len );
  516. rc = -EINVAL;
  517. goto done;
  518. }
  519. /* Deliver data */
  520. xfer_seek ( &tftp->xfer, offset, SEEK_SET );
  521. rc = xfer_deliver_iob ( &tftp->xfer, iobuf );
  522. iobuf = NULL;
  523. if ( rc != 0 ) {
  524. DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
  525. tftp, strerror ( rc ) );
  526. goto done;
  527. }
  528. /* Ensure block bitmap is ready */
  529. if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
  530. goto done;
  531. /* Mark block as received */
  532. bitmap_set ( &tftp->bitmap, block );
  533. /* Acknowledge block */
  534. tftp_send_packet ( tftp );
  535. /* If all blocks have been received, finish. */
  536. if ( bitmap_full ( &tftp->bitmap ) )
  537. tftp_done ( tftp, 0 );
  538. done:
  539. free_iob ( iobuf );
  540. if ( rc )
  541. tftp_done ( tftp, rc );
  542. return rc;
  543. }
  544. /** Translation between TFTP errors and internal error numbers */
  545. static const uint8_t tftp_errors[] = {
  546. [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
  547. [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
  548. [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
  549. };
  550. /**
  551. * Receive ERROR
  552. *
  553. * @v tftp TFTP connection
  554. * @v buf Temporary data buffer
  555. * @v len Length of temporary data buffer
  556. * @ret rc Return status code
  557. */
  558. static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
  559. struct tftp_error *error = buf;
  560. unsigned int err;
  561. int rc = 0;
  562. /* Sanity check */
  563. if ( len < sizeof ( *error ) ) {
  564. DBGC ( tftp, "TFTP %p received underlength ERROR packet "
  565. "length %zd\n", tftp, len );
  566. return -EINVAL;
  567. }
  568. DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
  569. "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
  570. /* Determine final operation result */
  571. err = ntohs ( error->errcode );
  572. if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
  573. rc = -tftp_errors[err];
  574. if ( ! rc )
  575. rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
  576. /* Close TFTP request */
  577. tftp_done ( tftp, rc );
  578. return 0;
  579. }
  580. /**
  581. * Receive new data
  582. *
  583. * @v tftp TFTP connection
  584. * @v iobuf I/O buffer
  585. * @v meta Transfer metadata, or NULL
  586. * @ret rc Return status code
  587. */
  588. static int tftp_rx ( struct tftp_request *tftp,
  589. struct io_buffer *iobuf,
  590. struct xfer_metadata *meta ) {
  591. struct sockaddr_tcpip *st_src;
  592. struct tftp_common *common = iobuf->data;
  593. size_t len = iob_len ( iobuf );
  594. int rc = -EINVAL;
  595. /* Sanity checks */
  596. if ( len < sizeof ( *common ) ) {
  597. DBGC ( tftp, "TFTP %p received underlength packet length "
  598. "%zd\n", tftp, len );
  599. goto done;
  600. }
  601. if ( ! meta ) {
  602. DBGC ( tftp, "TFTP %p received packet without metadata\n",
  603. tftp );
  604. goto done;
  605. }
  606. if ( ! meta->src ) {
  607. DBGC ( tftp, "TFTP %p received packet without source port\n",
  608. tftp );
  609. goto done;
  610. }
  611. /* Filter by TID. Set TID on first response received */
  612. st_src = ( struct sockaddr_tcpip * ) meta->src;
  613. if ( ! tftp->peer.st_family ) {
  614. memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
  615. DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
  616. ntohs ( tftp->peer.st_port ) );
  617. } else if ( memcmp ( &tftp->peer, st_src,
  618. sizeof ( tftp->peer ) ) != 0 ) {
  619. DBGC ( tftp, "TFTP %p received packet from wrong source (got "
  620. "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
  621. ntohs ( tftp->peer.st_port ) );
  622. goto done;
  623. }
  624. switch ( common->opcode ) {
  625. case htons ( TFTP_OACK ):
  626. rc = tftp_rx_oack ( tftp, iobuf->data, len );
  627. break;
  628. case htons ( TFTP_DATA ):
  629. rc = tftp_rx_data ( tftp, iobuf );
  630. iobuf = NULL;
  631. break;
  632. case htons ( TFTP_ERROR ):
  633. rc = tftp_rx_error ( tftp, iobuf->data, len );
  634. break;
  635. default:
  636. DBGC ( tftp, "TFTP %p received strange packet type %d\n",
  637. tftp, ntohs ( common->opcode ) );
  638. break;
  639. };
  640. done:
  641. free_iob ( iobuf );
  642. return rc;
  643. }
  644. /**
  645. * Receive new data via socket
  646. *
  647. * @v socket Transport layer interface
  648. * @v iobuf I/O buffer
  649. * @v meta Transfer metadata, or NULL
  650. * @ret rc Return status code
  651. */
  652. static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
  653. struct io_buffer *iobuf,
  654. struct xfer_metadata *meta ) {
  655. struct tftp_request *tftp =
  656. container_of ( socket, struct tftp_request, socket );
  657. return tftp_rx ( tftp, iobuf, meta );
  658. }
  659. /**
  660. * TFTP connection closed by network stack
  661. *
  662. * @v socket Transport layer interface
  663. * @v rc Reason for close
  664. */
  665. static void tftp_socket_close ( struct xfer_interface *socket, int rc ) {
  666. struct tftp_request *tftp =
  667. container_of ( socket, struct tftp_request, socket );
  668. DBGC ( tftp, "TFTP %p socket closed: %s\n",
  669. tftp, strerror ( rc ) );
  670. /* Any close counts as an error */
  671. if ( ! rc )
  672. rc = -ECONNRESET;
  673. tftp_done ( tftp, rc );
  674. }
  675. /** TFTP socket operations */
  676. static struct xfer_interface_operations tftp_socket_operations = {
  677. .close = tftp_socket_close,
  678. .vredirect = xfer_vopen,
  679. .seek = ignore_xfer_seek,
  680. .window = unlimited_xfer_window,
  681. .alloc_iob = default_xfer_alloc_iob,
  682. .deliver_iob = tftp_socket_deliver_iob,
  683. .deliver_raw = xfer_deliver_as_iob,
  684. };
  685. /**
  686. * Receive new data via multicast socket
  687. *
  688. * @v mc_socket Multicast transport layer interface
  689. * @v iobuf I/O buffer
  690. * @v meta Transfer metadata, or NULL
  691. * @ret rc Return status code
  692. */
  693. static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
  694. struct io_buffer *iobuf,
  695. struct xfer_metadata *meta ) {
  696. struct tftp_request *tftp =
  697. container_of ( mc_socket, struct tftp_request, mc_socket );
  698. return tftp_rx ( tftp, iobuf, meta );
  699. }
  700. /**
  701. * TFTP multicast connection closed by network stack
  702. *
  703. * @v socket Multicast transport layer interface
  704. * @v rc Reason for close
  705. */
  706. static void tftp_mc_socket_close ( struct xfer_interface *mc_socket,
  707. int rc ) {
  708. struct tftp_request *tftp =
  709. container_of ( mc_socket, struct tftp_request, mc_socket );
  710. DBGC ( tftp, "TFTP %p multicast socket closed: %s\n",
  711. tftp, strerror ( rc ) );
  712. /* The multicast socket may be closed when we receive a new
  713. * OACK and open/reopen the socket; we should not call
  714. * tftp_done() at this point.
  715. */
  716. }
  717. /** TFTP multicast socket operations */
  718. static struct xfer_interface_operations tftp_mc_socket_operations = {
  719. .close = tftp_mc_socket_close,
  720. .vredirect = xfer_vopen,
  721. .seek = ignore_xfer_seek,
  722. .window = unlimited_xfer_window,
  723. .alloc_iob = default_xfer_alloc_iob,
  724. .deliver_iob = tftp_mc_socket_deliver_iob,
  725. .deliver_raw = xfer_deliver_as_iob,
  726. };
  727. /**
  728. * Close TFTP data transfer interface
  729. *
  730. * @v xfer Data transfer interface
  731. * @v rc Reason for close
  732. */
  733. static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
  734. struct tftp_request *tftp =
  735. container_of ( xfer, struct tftp_request, xfer );
  736. DBGC ( tftp, "TFTP %p interface closed: %s\n",
  737. tftp, strerror ( rc ) );
  738. tftp_done ( tftp, rc );
  739. }
  740. /** TFTP data transfer interface operations */
  741. static struct xfer_interface_operations tftp_xfer_operations = {
  742. .close = tftp_xfer_close,
  743. .vredirect = ignore_xfer_vredirect,
  744. .seek = ignore_xfer_seek,
  745. .window = unlimited_xfer_window,
  746. .alloc_iob = default_xfer_alloc_iob,
  747. .deliver_iob = xfer_deliver_as_raw,
  748. .deliver_raw = ignore_xfer_deliver_raw,
  749. };
  750. /**
  751. * Initiate TFTP download
  752. *
  753. * @v xfer Data transfer interface
  754. * @v uri Uniform Resource Identifier
  755. * @ret rc Return status code
  756. */
  757. int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
  758. struct tftp_request *tftp;
  759. struct sockaddr_tcpip server;
  760. int rc;
  761. /* Sanity checks */
  762. if ( ! uri->host )
  763. return -EINVAL;
  764. if ( ! uri->path )
  765. return -EINVAL;
  766. /* Allocate and populate TFTP structure */
  767. tftp = zalloc ( sizeof ( *tftp ) );
  768. if ( ! tftp )
  769. return -ENOMEM;
  770. tftp->refcnt.free = tftp_free;
  771. xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
  772. tftp->uri = uri_get ( uri );
  773. xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
  774. xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
  775. &tftp->refcnt );
  776. tftp->blksize = TFTP_DEFAULT_BLKSIZE;
  777. tftp->master = 1;
  778. tftp->timer.expired = tftp_timer_expired;
  779. /* Open socket */
  780. memset ( &server, 0, sizeof ( server ) );
  781. server.st_port = htons ( uri_port ( tftp->uri, TFTP_PORT ) );
  782. if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
  783. ( struct sockaddr * ) &server,
  784. uri->host, NULL ) ) != 0 )
  785. goto err;
  786. /* Start timer to initiate RRQ */
  787. start_timer_nodelay ( &tftp->timer );
  788. /* Attach to parent interface, mortalise self, and return */
  789. xfer_plug_plug ( &tftp->xfer, xfer );
  790. ref_put ( &tftp->refcnt );
  791. return 0;
  792. err:
  793. DBGC ( tftp, "TFTP %p could not create request: %s\n",
  794. tftp, strerror ( rc ) );
  795. tftp_done ( tftp, rc );
  796. ref_put ( &tftp->refcnt );
  797. return rc;
  798. }
  799. /** TFTP URI opener */
  800. struct uri_opener tftp_uri_opener __uri_opener = {
  801. .scheme = "tftp",
  802. .open = tftp_open,
  803. };