Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tftp.c 31KB

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