You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tftp.c 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. if ( tftp->blksize == 0 )
  250. return -EINVAL;
  251. num_blocks = ( ( filesize / tftp->blksize ) + 1 );
  252. if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
  253. DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
  254. "%s\n", tftp, num_blocks, strerror ( rc ) );
  255. return rc;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * MTFTP multicast receive address
  261. *
  262. * This is treated as a global configuration parameter.
  263. */
  264. static struct sockaddr_in tftp_mtftp_socket = {
  265. .sin_family = AF_INET,
  266. .sin_addr.s_addr = htonl ( 0xefff0101 ),
  267. .sin_port = htons ( 3001 ),
  268. };
  269. /**
  270. * Set MTFTP multicast address
  271. *
  272. * @v address Multicast IPv4 address
  273. */
  274. void tftp_set_mtftp_address ( struct in_addr address ) {
  275. tftp_mtftp_socket.sin_addr = address;
  276. }
  277. /**
  278. * Set MTFTP multicast port
  279. *
  280. * @v port Multicast port
  281. */
  282. void tftp_set_mtftp_port ( unsigned int port ) {
  283. tftp_mtftp_socket.sin_port = htons ( port );
  284. }
  285. /**
  286. * Transmit RRQ
  287. *
  288. * @v tftp TFTP connection
  289. * @ret rc Return status code
  290. */
  291. static int tftp_send_rrq ( struct tftp_request *tftp ) {
  292. const char *path = ( tftp->uri->path + 1 /* skip '/' */ );
  293. struct tftp_rrq *rrq;
  294. size_t len;
  295. struct io_buffer *iobuf;
  296. size_t blksize;
  297. DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
  298. /* Allocate buffer */
  299. len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
  300. + 5 + 1 /* "octet" + NUL */
  301. + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
  302. + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
  303. + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
  304. iobuf = xfer_alloc_iob ( &tftp->socket, len );
  305. if ( ! iobuf )
  306. return -ENOMEM;
  307. /* Determine block size */
  308. blksize = xfer_window ( &tftp->xfer );
  309. if ( blksize > TFTP_MAX_BLKSIZE )
  310. blksize = TFTP_MAX_BLKSIZE;
  311. /* Build request */
  312. rrq = iob_put ( iobuf, sizeof ( *rrq ) );
  313. rrq->opcode = htons ( TFTP_RRQ );
  314. iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
  315. "%s%coctet", path, 0 ) + 1 );
  316. if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
  317. iob_put ( iobuf, snprintf ( iobuf->tail,
  318. iob_tailroom ( iobuf ),
  319. "blksize%c%zd%ctsize%c0",
  320. 0, blksize, 0, 0 ) + 1 );
  321. }
  322. if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
  323. iob_put ( iobuf, snprintf ( iobuf->tail,
  324. iob_tailroom ( iobuf ),
  325. "multicast%c", 0 ) + 1 );
  326. }
  327. /* RRQ always goes to the address specified in the initial
  328. * xfer_open() call
  329. */
  330. return xfer_deliver_iob ( &tftp->socket, iobuf );
  331. }
  332. /**
  333. * Transmit ACK
  334. *
  335. * @v tftp TFTP connection
  336. * @ret rc Return status code
  337. */
  338. static int tftp_send_ack ( struct tftp_request *tftp ) {
  339. struct tftp_ack *ack;
  340. struct io_buffer *iobuf;
  341. struct xfer_metadata meta = {
  342. .dest = ( struct sockaddr * ) &tftp->peer,
  343. };
  344. unsigned int block;
  345. /* Determine next required block number */
  346. block = bitmap_first_gap ( &tftp->bitmap );
  347. DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
  348. /* Allocate buffer */
  349. iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
  350. if ( ! iobuf )
  351. return -ENOMEM;
  352. /* Build ACK */
  353. ack = iob_put ( iobuf, sizeof ( *ack ) );
  354. ack->opcode = htons ( TFTP_ACK );
  355. ack->block = htons ( block );
  356. /* ACK always goes to the peer recorded from the RRQ response */
  357. return xfer_deliver ( &tftp->socket, iobuf, &meta );
  358. }
  359. /**
  360. * Transmit ERROR (Abort)
  361. *
  362. * @v tftp TFTP connection
  363. * @v errcode TFTP error code
  364. * @v errmsg Error message string
  365. * @ret rc Return status code
  366. */
  367. static int tftp_send_error ( struct tftp_request *tftp, int errcode,
  368. const char *errmsg ) {
  369. struct tftp_error *err;
  370. struct io_buffer *iobuf;
  371. struct xfer_metadata meta = {
  372. .dest = ( struct sockaddr * ) &tftp->peer,
  373. };
  374. size_t msglen;
  375. DBGC2 ( tftp, "TFTP %p sending ERROR %d: %s\n", tftp, errcode,
  376. errmsg );
  377. /* Allocate buffer */
  378. msglen = sizeof ( *err ) + strlen ( errmsg ) + 1 /* NUL */;
  379. iobuf = xfer_alloc_iob ( &tftp->socket, msglen );
  380. if ( ! iobuf )
  381. return -ENOMEM;
  382. /* Build ERROR */
  383. err = iob_put ( iobuf, msglen );
  384. err->opcode = htons ( TFTP_ERROR );
  385. err->errcode = htons ( errcode );
  386. strcpy ( err->errmsg, errmsg );
  387. /* ERR always goes to the peer recorded from the RRQ response */
  388. return xfer_deliver ( &tftp->socket, iobuf, &meta );
  389. }
  390. /**
  391. * Transmit next relevant packet
  392. *
  393. * @v tftp TFTP connection
  394. * @ret rc Return status code
  395. */
  396. static int tftp_send_packet ( struct tftp_request *tftp ) {
  397. /* Update retransmission timer. While name resolution takes place the
  398. * window is zero. Avoid unnecessary delay after name resolution
  399. * completes by retrying immediately.
  400. */
  401. stop_timer ( &tftp->timer );
  402. if ( xfer_window ( &tftp->socket ) ) {
  403. start_timer ( &tftp->timer );
  404. } else {
  405. start_timer_nodelay ( &tftp->timer );
  406. }
  407. /* Send RRQ or ACK as appropriate */
  408. if ( ! tftp->peer.st_family ) {
  409. return tftp_send_rrq ( tftp );
  410. } else {
  411. if ( tftp->flags & TFTP_FL_SEND_ACK ) {
  412. return tftp_send_ack ( tftp );
  413. } else {
  414. return 0;
  415. }
  416. }
  417. }
  418. /**
  419. * Handle TFTP retransmission timer expiry
  420. *
  421. * @v timer Retry timer
  422. * @v fail Failure indicator
  423. */
  424. static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
  425. struct tftp_request *tftp =
  426. container_of ( timer, struct tftp_request, timer );
  427. int rc;
  428. /* If we are doing MTFTP, attempt the various recovery strategies */
  429. if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
  430. if ( tftp->peer.st_family ) {
  431. /* If we have received any response from the server,
  432. * try resending the RRQ to restart the download.
  433. */
  434. DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
  435. if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
  436. goto err;
  437. } else {
  438. /* Fall back to plain TFTP after several attempts */
  439. tftp->mtftp_timeouts++;
  440. DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
  441. "open\n", tftp, tftp->mtftp_timeouts );
  442. if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
  443. DBGC ( tftp, "TFTP %p falling back to plain "
  444. "TFTP\n", tftp );
  445. tftp->flags = TFTP_FL_RRQ_SIZES;
  446. /* Close multicast socket */
  447. intf_restart ( &tftp->mc_socket, 0 );
  448. /* Reset retry timer */
  449. start_timer_nodelay ( &tftp->timer );
  450. /* The blocksize may change: discard
  451. * the block bitmap
  452. */
  453. bitmap_free ( &tftp->bitmap );
  454. memset ( &tftp->bitmap, 0,
  455. sizeof ( tftp->bitmap ) );
  456. /* Reopen on standard TFTP port */
  457. tftp->port = TFTP_PORT;
  458. if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
  459. goto err;
  460. }
  461. }
  462. } else {
  463. /* Not doing MTFTP (or have fallen back to plain
  464. * TFTP); fail as per normal.
  465. */
  466. if ( fail ) {
  467. rc = -ETIMEDOUT;
  468. goto err;
  469. }
  470. }
  471. tftp_send_packet ( tftp );
  472. return;
  473. err:
  474. tftp_done ( tftp, rc );
  475. }
  476. /**
  477. * Process TFTP "blksize" option
  478. *
  479. * @v tftp TFTP connection
  480. * @v value Option value
  481. * @ret rc Return status code
  482. */
  483. static int tftp_process_blksize ( struct tftp_request *tftp, char *value ) {
  484. char *end;
  485. tftp->blksize = strtoul ( value, &end, 10 );
  486. if ( *end ) {
  487. DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
  488. tftp, value );
  489. return -EINVAL_BLKSIZE;
  490. }
  491. DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
  492. return 0;
  493. }
  494. /**
  495. * Process TFTP "tsize" option
  496. *
  497. * @v tftp TFTP connection
  498. * @v value Option value
  499. * @ret rc Return status code
  500. */
  501. static int tftp_process_tsize ( struct tftp_request *tftp, 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, char *value ) {
  520. union {
  521. struct sockaddr sa;
  522. struct sockaddr_in sin;
  523. } socket;
  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. addr = value;
  532. port = strchr ( addr, ',' );
  533. if ( ! port ) {
  534. DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
  535. return -EINVAL_MC_NO_PORT;
  536. }
  537. *(port++) = '\0';
  538. mc = strchr ( port, ',' );
  539. if ( ! mc ) {
  540. DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
  541. return -EINVAL_MC_NO_MC;
  542. }
  543. *(mc++) = '\0';
  544. /* Parse parameters */
  545. if ( strtoul ( mc, &mc_end, 0 ) == 0 )
  546. tftp->flags &= ~TFTP_FL_SEND_ACK;
  547. if ( *mc_end ) {
  548. DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
  549. return -EINVAL_MC_INVALID_MC;
  550. }
  551. DBGC ( tftp, "TFTP %p is%s the master client\n",
  552. tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
  553. if ( *addr && *port ) {
  554. socket.sin.sin_family = AF_INET;
  555. if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
  556. DBGC ( tftp, "TFTP %p multicast invalid IP address "
  557. "%s\n", tftp, addr );
  558. return -EINVAL_MC_INVALID_IP;
  559. }
  560. DBGC ( tftp, "TFTP %p multicast IP address %s\n",
  561. tftp, inet_ntoa ( socket.sin.sin_addr ) );
  562. socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
  563. if ( *port_end ) {
  564. DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
  565. tftp, port );
  566. return -EINVAL_MC_INVALID_PORT;
  567. }
  568. DBGC ( tftp, "TFTP %p multicast port %d\n",
  569. tftp, ntohs ( socket.sin.sin_port ) );
  570. if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
  571. return rc;
  572. }
  573. return 0;
  574. }
  575. /** A TFTP option */
  576. struct tftp_option {
  577. /** Option name */
  578. const char *name;
  579. /** Option processor
  580. *
  581. * @v tftp TFTP connection
  582. * @v value Option value
  583. * @ret rc Return status code
  584. */
  585. int ( * process ) ( struct tftp_request *tftp, char *value );
  586. };
  587. /** Recognised TFTP options */
  588. static struct tftp_option tftp_options[] = {
  589. { "blksize", tftp_process_blksize },
  590. { "tsize", tftp_process_tsize },
  591. { "multicast", tftp_process_multicast },
  592. { NULL, NULL }
  593. };
  594. /**
  595. * Process TFTP option
  596. *
  597. * @v tftp TFTP connection
  598. * @v name Option name
  599. * @v value Option value
  600. * @ret rc Return status code
  601. */
  602. static int tftp_process_option ( struct tftp_request *tftp,
  603. const char *name, char *value ) {
  604. struct tftp_option *option;
  605. for ( option = tftp_options ; option->name ; option++ ) {
  606. if ( strcasecmp ( name, option->name ) == 0 )
  607. return option->process ( tftp, value );
  608. }
  609. DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
  610. tftp, name, value );
  611. /* Unknown options should be silently ignored */
  612. return 0;
  613. }
  614. /**
  615. * Receive OACK
  616. *
  617. * @v tftp TFTP connection
  618. * @v buf Temporary data buffer
  619. * @v len Length of temporary data buffer
  620. * @ret rc Return status code
  621. */
  622. static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
  623. struct tftp_oack *oack = buf;
  624. char *end = buf + len;
  625. char *name;
  626. char *value;
  627. char *next;
  628. int rc = 0;
  629. /* Sanity check */
  630. if ( len < sizeof ( *oack ) ) {
  631. DBGC ( tftp, "TFTP %p received underlength OACK packet "
  632. "length %zd\n", tftp, len );
  633. rc = -EINVAL;
  634. goto done;
  635. }
  636. /* Process each option in turn */
  637. for ( name = oack->data ; name < end ; name = next ) {
  638. /* Parse option name and value
  639. *
  640. * We treat parsing errors as non-fatal, because there
  641. * exists at least one TFTP server (IBM Tivoli PXE
  642. * Server 5.1.0.3) that has been observed to send
  643. * malformed OACKs containing trailing garbage bytes.
  644. */
  645. value = ( name + strnlen ( name, ( end - name ) ) + 1 );
  646. if ( value > end ) {
  647. DBGC ( tftp, "TFTP %p received OACK with malformed "
  648. "option name:\n", tftp );
  649. DBGC_HD ( tftp, oack, len );
  650. break;
  651. }
  652. if ( value == end ) {
  653. DBGC ( tftp, "TFTP %p received OACK missing value "
  654. "for option \"%s\"\n", tftp, name );
  655. DBGC_HD ( tftp, oack, len );
  656. break;
  657. }
  658. next = ( value + strnlen ( value, ( end - value ) ) + 1 );
  659. if ( next > end ) {
  660. DBGC ( tftp, "TFTP %p received OACK with malformed "
  661. "value for option \"%s\":\n", tftp, name );
  662. DBGC_HD ( tftp, oack, len );
  663. break;
  664. }
  665. /* Process option */
  666. if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
  667. goto done;
  668. }
  669. /* Process tsize information, if available */
  670. if ( tftp->tsize ) {
  671. if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
  672. goto done;
  673. }
  674. /* Request next data block */
  675. tftp_send_packet ( tftp );
  676. done:
  677. if ( rc )
  678. tftp_done ( tftp, rc );
  679. return rc;
  680. }
  681. /**
  682. * Receive DATA
  683. *
  684. * @v tftp TFTP connection
  685. * @v iobuf I/O buffer
  686. * @ret rc Return status code
  687. *
  688. * Takes ownership of I/O buffer.
  689. */
  690. static int tftp_rx_data ( struct tftp_request *tftp,
  691. struct io_buffer *iobuf ) {
  692. struct tftp_data *data = iobuf->data;
  693. struct xfer_metadata meta;
  694. unsigned int block;
  695. off_t offset;
  696. size_t data_len;
  697. int rc;
  698. /* Sanity check */
  699. if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
  700. DBGC ( tftp, "TFTP %p received underlength DATA packet "
  701. "length %zd\n", tftp, iob_len ( iobuf ) );
  702. rc = -EINVAL;
  703. goto done;
  704. }
  705. /* Calculate block number */
  706. block = ( ( bitmap_first_gap ( &tftp->bitmap ) + 1 ) & ~0xffff );
  707. if ( data->block == 0 && block == 0 ) {
  708. DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
  709. rc = -EINVAL;
  710. goto done;
  711. }
  712. block += ( ntohs ( data->block ) - 1 );
  713. /* Extract data */
  714. offset = ( block * tftp->blksize );
  715. iob_pull ( iobuf, sizeof ( *data ) );
  716. data_len = iob_len ( iobuf );
  717. if ( data_len > tftp->blksize ) {
  718. DBGC ( tftp, "TFTP %p received overlength DATA packet "
  719. "length %zd\n", tftp, data_len );
  720. rc = -EINVAL;
  721. goto done;
  722. }
  723. /* Deliver data */
  724. memset ( &meta, 0, sizeof ( meta ) );
  725. meta.flags = XFER_FL_ABS_OFFSET;
  726. meta.offset = offset;
  727. if ( ( rc = xfer_deliver ( &tftp->xfer, iob_disown ( iobuf ),
  728. &meta ) ) != 0 ) {
  729. DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
  730. tftp, strerror ( rc ) );
  731. goto done;
  732. }
  733. /* Ensure block bitmap is ready */
  734. if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
  735. goto done;
  736. /* Mark block as received */
  737. bitmap_set ( &tftp->bitmap, block );
  738. /* Acknowledge block */
  739. tftp_send_packet ( tftp );
  740. /* If all blocks have been received, finish. */
  741. if ( bitmap_full ( &tftp->bitmap ) )
  742. tftp_done ( tftp, 0 );
  743. done:
  744. free_iob ( iobuf );
  745. if ( rc )
  746. tftp_done ( tftp, rc );
  747. return rc;
  748. }
  749. /**
  750. * Convert TFTP error code to return status code
  751. *
  752. * @v errcode TFTP error code
  753. * @ret rc Return status code
  754. */
  755. static int tftp_errcode_to_rc ( unsigned int errcode ) {
  756. switch ( errcode ) {
  757. case TFTP_ERR_FILE_NOT_FOUND: return -ENOENT;
  758. case TFTP_ERR_ACCESS_DENIED: return -EACCES;
  759. case TFTP_ERR_ILLEGAL_OP: return -ENOTTY;
  760. default: return -ENOTSUP;
  761. }
  762. }
  763. /**
  764. * Receive ERROR
  765. *
  766. * @v tftp TFTP connection
  767. * @v buf Temporary data buffer
  768. * @v len Length of temporary data buffer
  769. * @ret rc Return status code
  770. */
  771. static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
  772. struct tftp_error *error = buf;
  773. int rc;
  774. /* Sanity check */
  775. if ( len < sizeof ( *error ) ) {
  776. DBGC ( tftp, "TFTP %p received underlength ERROR packet "
  777. "length %zd\n", tftp, len );
  778. return -EINVAL;
  779. }
  780. DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
  781. "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
  782. /* Determine final operation result */
  783. rc = tftp_errcode_to_rc ( ntohs ( error->errcode ) );
  784. /* Close TFTP request */
  785. tftp_done ( tftp, rc );
  786. return 0;
  787. }
  788. /**
  789. * Receive new data
  790. *
  791. * @v tftp TFTP connection
  792. * @v iobuf I/O buffer
  793. * @v meta Transfer metadata
  794. * @ret rc Return status code
  795. */
  796. static int tftp_rx ( struct tftp_request *tftp,
  797. struct io_buffer *iobuf,
  798. struct xfer_metadata *meta ) {
  799. struct sockaddr_tcpip *st_src;
  800. struct tftp_common *common = iobuf->data;
  801. size_t len = iob_len ( iobuf );
  802. int rc = -EINVAL;
  803. /* Sanity checks */
  804. if ( len < sizeof ( *common ) ) {
  805. DBGC ( tftp, "TFTP %p received underlength packet length "
  806. "%zd\n", tftp, len );
  807. goto done;
  808. }
  809. if ( ! meta->src ) {
  810. DBGC ( tftp, "TFTP %p received packet without source port\n",
  811. tftp );
  812. goto done;
  813. }
  814. /* Filter by TID. Set TID on first response received */
  815. st_src = ( struct sockaddr_tcpip * ) meta->src;
  816. if ( ! tftp->peer.st_family ) {
  817. memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
  818. DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
  819. ntohs ( tftp->peer.st_port ) );
  820. } else if ( memcmp ( &tftp->peer, st_src,
  821. sizeof ( tftp->peer ) ) != 0 ) {
  822. DBGC ( tftp, "TFTP %p received packet from wrong source (got "
  823. "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
  824. ntohs ( tftp->peer.st_port ) );
  825. goto done;
  826. }
  827. switch ( common->opcode ) {
  828. case htons ( TFTP_OACK ):
  829. rc = tftp_rx_oack ( tftp, iobuf->data, len );
  830. break;
  831. case htons ( TFTP_DATA ):
  832. rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
  833. break;
  834. case htons ( TFTP_ERROR ):
  835. rc = tftp_rx_error ( tftp, iobuf->data, len );
  836. break;
  837. default:
  838. DBGC ( tftp, "TFTP %p received strange packet type %d\n",
  839. tftp, ntohs ( common->opcode ) );
  840. break;
  841. };
  842. done:
  843. free_iob ( iobuf );
  844. return rc;
  845. }
  846. /**
  847. * Receive new data via socket
  848. *
  849. * @v tftp TFTP connection
  850. * @v iobuf I/O buffer
  851. * @v meta Transfer metadata
  852. * @ret rc Return status code
  853. */
  854. static int tftp_socket_deliver ( struct tftp_request *tftp,
  855. struct io_buffer *iobuf,
  856. struct xfer_metadata *meta ) {
  857. /* Enable sending ACKs when we receive a unicast packet. This
  858. * covers three cases:
  859. *
  860. * 1. Standard TFTP; we should always send ACKs, and will
  861. * always receive a unicast packet before we need to send the
  862. * first ACK.
  863. *
  864. * 2. RFC2090 multicast TFTP; the only unicast packets we will
  865. * receive are the OACKs; enable sending ACKs here (before
  866. * processing the OACK) and disable it when processing the
  867. * multicast option if we are not the master client.
  868. *
  869. * 3. MTFTP; receiving a unicast datagram indicates that we
  870. * are the "master client" and should send ACKs.
  871. */
  872. tftp->flags |= TFTP_FL_SEND_ACK;
  873. return tftp_rx ( tftp, iobuf, meta );
  874. }
  875. /** TFTP socket operations */
  876. static struct interface_operation tftp_socket_operations[] = {
  877. INTF_OP ( xfer_deliver, struct tftp_request *, tftp_socket_deliver ),
  878. };
  879. /** TFTP socket interface descriptor */
  880. static struct interface_descriptor tftp_socket_desc =
  881. INTF_DESC ( struct tftp_request, socket, tftp_socket_operations );
  882. /** TFTP multicast socket operations */
  883. static struct interface_operation tftp_mc_socket_operations[] = {
  884. INTF_OP ( xfer_deliver, struct tftp_request *, tftp_rx ),
  885. };
  886. /** TFTP multicast socket interface descriptor */
  887. static struct interface_descriptor tftp_mc_socket_desc =
  888. INTF_DESC ( struct tftp_request, mc_socket, tftp_mc_socket_operations );
  889. /**
  890. * Check flow control window
  891. *
  892. * @v tftp TFTP connection
  893. * @ret len Length of window
  894. */
  895. static size_t tftp_xfer_window ( struct tftp_request *tftp ) {
  896. /* We abuse this data-xfer method to convey the blocksize to
  897. * the caller. This really should be done using some kind of
  898. * stat() method, but we don't yet have the facility to do
  899. * that.
  900. */
  901. return tftp->blksize;
  902. }
  903. /**
  904. * Terminate download
  905. *
  906. * @v tftp TFTP connection
  907. * @v rc Reason for close
  908. */
  909. static void tftp_close ( struct tftp_request *tftp, int rc ) {
  910. /* Abort download */
  911. tftp_send_error ( tftp, 0, "TFTP Aborted" );
  912. /* Close TFTP request */
  913. tftp_done ( tftp, rc );
  914. }
  915. /** TFTP data transfer interface operations */
  916. static struct interface_operation tftp_xfer_operations[] = {
  917. INTF_OP ( xfer_window, struct tftp_request *, tftp_xfer_window ),
  918. INTF_OP ( intf_close, struct tftp_request *, tftp_close ),
  919. };
  920. /** TFTP data transfer interface descriptor */
  921. static struct interface_descriptor tftp_xfer_desc =
  922. INTF_DESC ( struct tftp_request, xfer, tftp_xfer_operations );
  923. /**
  924. * Initiate TFTP/TFTM/MTFTP download
  925. *
  926. * @v xfer Data transfer interface
  927. * @v uri Uniform Resource Identifier
  928. * @ret rc Return status code
  929. */
  930. static int tftp_core_open ( struct interface *xfer, struct uri *uri,
  931. unsigned int default_port,
  932. struct sockaddr *multicast,
  933. unsigned int flags ) {
  934. struct tftp_request *tftp;
  935. int rc;
  936. /* Sanity checks */
  937. if ( ! uri->host )
  938. return -EINVAL;
  939. if ( ! uri->path )
  940. return -EINVAL;
  941. if ( uri->path[0] != '/' )
  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 TFTM download
  995. *
  996. * @v xfer Data transfer interface
  997. * @v uri Uniform Resource Identifier
  998. * @ret rc Return status code
  999. */
  1000. static int tftm_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_RRQ_MULTICAST ) );
  1004. }
  1005. /** TFTM URI opener */
  1006. struct uri_opener tftm_uri_opener __uri_opener = {
  1007. .scheme = "tftm",
  1008. .open = tftm_open,
  1009. };
  1010. /**
  1011. * Initiate MTFTP download
  1012. *
  1013. * @v xfer Data transfer interface
  1014. * @v uri Uniform Resource Identifier
  1015. * @ret rc Return status code
  1016. */
  1017. static int mtftp_open ( struct interface *xfer, struct uri *uri ) {
  1018. return tftp_core_open ( xfer, uri, MTFTP_PORT,
  1019. ( struct sockaddr * ) &tftp_mtftp_socket,
  1020. TFTP_FL_MTFTP_RECOVERY );
  1021. }
  1022. /** MTFTP URI opener */
  1023. struct uri_opener mtftp_uri_opener __uri_opener = {
  1024. .scheme = "mtftp",
  1025. .open = mtftp_open,
  1026. };
  1027. /******************************************************************************
  1028. *
  1029. * Settings
  1030. *
  1031. ******************************************************************************
  1032. */
  1033. /**
  1034. * Apply TFTP configuration settings
  1035. *
  1036. * @ret rc Return status code
  1037. */
  1038. static int tftp_apply_settings ( void ) {
  1039. static struct in_addr tftp_server = { 0 };
  1040. struct in_addr new_tftp_server;
  1041. char uri_string[32];
  1042. struct uri *uri;
  1043. /* Retrieve TFTP server setting */
  1044. fetch_ipv4_setting ( NULL, &next_server_setting, &new_tftp_server );
  1045. /* If TFTP server setting has changed, set the current working
  1046. * URI to match. Do it only when the TFTP server has changed
  1047. * to try to minimise surprises to the user, who probably
  1048. * won't expect the CWURI to change just because they updated
  1049. * an unrelated setting and triggered all the settings
  1050. * applicators.
  1051. */
  1052. if ( new_tftp_server.s_addr &&
  1053. ( new_tftp_server.s_addr != tftp_server.s_addr ) ) {
  1054. DBGC ( &tftp_server, "TFTP server changed %s => ",
  1055. inet_ntoa ( tftp_server ) );
  1056. DBGC ( &tftp_server, "%s\n", inet_ntoa ( new_tftp_server ) );
  1057. snprintf ( uri_string, sizeof ( uri_string ),
  1058. "tftp://%s/", inet_ntoa ( new_tftp_server ) );
  1059. uri = parse_uri ( uri_string );
  1060. if ( ! uri )
  1061. return -ENOMEM;
  1062. churi ( uri );
  1063. uri_put ( uri );
  1064. tftp_server = new_tftp_server;
  1065. }
  1066. return 0;
  1067. }
  1068. /** TFTP settings applicator */
  1069. struct settings_applicator tftp_settings_applicator __settings_applicator = {
  1070. .apply = tftp_apply_settings,
  1071. };