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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 + 1 /* skip '/' */ );
  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. if ( uri->path[0] != '/' )
  945. return -EINVAL;
  946. /* Allocate and populate TFTP structure */
  947. tftp = zalloc ( sizeof ( *tftp ) );
  948. if ( ! tftp )
  949. return -ENOMEM;
  950. ref_init ( &tftp->refcnt, tftp_free );
  951. intf_init ( &tftp->xfer, &tftp_xfer_desc, &tftp->refcnt );
  952. intf_init ( &tftp->socket, &tftp_socket_desc, &tftp->refcnt );
  953. intf_init ( &tftp->mc_socket, &tftp_mc_socket_desc, &tftp->refcnt );
  954. timer_init ( &tftp->timer, tftp_timer_expired, &tftp->refcnt );
  955. tftp->uri = uri_get ( uri );
  956. tftp->blksize = TFTP_DEFAULT_BLKSIZE;
  957. tftp->flags = flags;
  958. /* Open socket */
  959. tftp->port = uri_port ( tftp->uri, default_port );
  960. if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
  961. goto err;
  962. /* Open multicast socket */
  963. if ( multicast ) {
  964. if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
  965. goto err;
  966. }
  967. /* Start timer to initiate RRQ */
  968. start_timer_nodelay ( &tftp->timer );
  969. /* Attach to parent interface, mortalise self, and return */
  970. intf_plug_plug ( &tftp->xfer, xfer );
  971. ref_put ( &tftp->refcnt );
  972. return 0;
  973. err:
  974. DBGC ( tftp, "TFTP %p could not create request: %s\n",
  975. tftp, strerror ( rc ) );
  976. tftp_done ( tftp, rc );
  977. ref_put ( &tftp->refcnt );
  978. return rc;
  979. }
  980. /**
  981. * Initiate TFTP download
  982. *
  983. * @v xfer Data transfer interface
  984. * @v uri Uniform Resource Identifier
  985. * @ret rc Return status code
  986. */
  987. static int tftp_open ( struct interface *xfer, struct uri *uri ) {
  988. return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
  989. TFTP_FL_RRQ_SIZES );
  990. }
  991. /** TFTP URI opener */
  992. struct uri_opener tftp_uri_opener __uri_opener = {
  993. .scheme = "tftp",
  994. .open = tftp_open,
  995. };
  996. /**
  997. * Initiate TFTM download
  998. *
  999. * @v xfer Data transfer interface
  1000. * @v uri Uniform Resource Identifier
  1001. * @ret rc Return status code
  1002. */
  1003. static int tftm_open ( struct interface *xfer, struct uri *uri ) {
  1004. return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
  1005. ( TFTP_FL_RRQ_SIZES |
  1006. TFTP_FL_RRQ_MULTICAST ) );
  1007. }
  1008. /** TFTM URI opener */
  1009. struct uri_opener tftm_uri_opener __uri_opener = {
  1010. .scheme = "tftm",
  1011. .open = tftm_open,
  1012. };
  1013. /**
  1014. * Initiate MTFTP download
  1015. *
  1016. * @v xfer Data transfer interface
  1017. * @v uri Uniform Resource Identifier
  1018. * @ret rc Return status code
  1019. */
  1020. static int mtftp_open ( struct interface *xfer, struct uri *uri ) {
  1021. return tftp_core_open ( xfer, uri, MTFTP_PORT,
  1022. ( struct sockaddr * ) &tftp_mtftp_socket,
  1023. TFTP_FL_MTFTP_RECOVERY );
  1024. }
  1025. /** MTFTP URI opener */
  1026. struct uri_opener mtftp_uri_opener __uri_opener = {
  1027. .scheme = "mtftp",
  1028. .open = mtftp_open,
  1029. };
  1030. /******************************************************************************
  1031. *
  1032. * Settings
  1033. *
  1034. ******************************************************************************
  1035. */
  1036. /**
  1037. * Apply TFTP configuration settings
  1038. *
  1039. * @ret rc Return status code
  1040. */
  1041. static int tftp_apply_settings ( void ) {
  1042. static struct in_addr tftp_server = { 0 };
  1043. struct in_addr new_tftp_server;
  1044. char uri_string[32];
  1045. struct uri *uri;
  1046. /* Retrieve TFTP server setting */
  1047. fetch_ipv4_setting ( NULL, &next_server_setting, &new_tftp_server );
  1048. /* If TFTP server setting has changed, set the current working
  1049. * URI to match. Do it only when the TFTP server has changed
  1050. * to try to minimise surprises to the user, who probably
  1051. * won't expect the CWURI to change just because they updated
  1052. * an unrelated setting and triggered all the settings
  1053. * applicators.
  1054. */
  1055. if ( new_tftp_server.s_addr &&
  1056. ( new_tftp_server.s_addr != tftp_server.s_addr ) ) {
  1057. DBGC ( &tftp_server, "TFTP server changed %s => ",
  1058. inet_ntoa ( tftp_server ) );
  1059. DBGC ( &tftp_server, "%s\n", inet_ntoa ( new_tftp_server ) );
  1060. snprintf ( uri_string, sizeof ( uri_string ),
  1061. "tftp://%s/", inet_ntoa ( new_tftp_server ) );
  1062. uri = parse_uri ( uri_string );
  1063. if ( ! uri )
  1064. return -ENOMEM;
  1065. churi ( uri );
  1066. uri_put ( uri );
  1067. tftp_server = new_tftp_server;
  1068. }
  1069. return 0;
  1070. }
  1071. /** TFTP settings applicator */
  1072. struct settings_applicator tftp_settings_applicator __settings_applicator = {
  1073. .apply = tftp_apply_settings,
  1074. };