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

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