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 28KB

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