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

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