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.

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