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

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