Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tftp.c 30KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. 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. if ( data->block == 0 ) {
  676. DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
  677. rc = -EINVAL;
  678. goto done;
  679. }
  680. /* Extract data */
  681. block = ( ntohs ( data->block ) - 1 );
  682. offset = ( block * tftp->blksize );
  683. iob_pull ( iobuf, sizeof ( *data ) );
  684. data_len = iob_len ( iobuf );
  685. if ( data_len > tftp->blksize ) {
  686. DBGC ( tftp, "TFTP %p received overlength DATA packet "
  687. "length %zd\n", tftp, data_len );
  688. rc = -EINVAL;
  689. goto done;
  690. }
  691. /* Deliver data */
  692. memset ( &meta, 0, sizeof ( meta ) );
  693. meta.whence = SEEK_SET;
  694. meta.offset = offset;
  695. if ( ( rc = xfer_deliver_iob_meta ( &tftp->xfer, iob_disown ( iobuf ),
  696. &meta ) ) != 0 ) {
  697. DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
  698. tftp, strerror ( rc ) );
  699. goto done;
  700. }
  701. /* Ensure block bitmap is ready */
  702. if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
  703. goto done;
  704. /* Mark block as received */
  705. bitmap_set ( &tftp->bitmap, block );
  706. /* Acknowledge block */
  707. tftp_send_packet ( tftp );
  708. /* If all blocks have been received, finish. */
  709. if ( bitmap_full ( &tftp->bitmap ) )
  710. tftp_done ( tftp, 0 );
  711. done:
  712. free_iob ( iobuf );
  713. if ( rc )
  714. tftp_done ( tftp, rc );
  715. return rc;
  716. }
  717. /** Translation between TFTP errors and internal error numbers */
  718. static const int tftp_errors[] = {
  719. [TFTP_ERR_FILE_NOT_FOUND] = ENOENT,
  720. [TFTP_ERR_ACCESS_DENIED] = EACCES,
  721. [TFTP_ERR_ILLEGAL_OP] = ENOTSUP,
  722. };
  723. /**
  724. * Receive ERROR
  725. *
  726. * @v tftp TFTP connection
  727. * @v buf Temporary data buffer
  728. * @v len Length of temporary data buffer
  729. * @ret rc Return status code
  730. */
  731. static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
  732. struct tftp_error *error = buf;
  733. unsigned int err;
  734. int rc = 0;
  735. /* Sanity check */
  736. if ( len < sizeof ( *error ) ) {
  737. DBGC ( tftp, "TFTP %p received underlength ERROR packet "
  738. "length %zd\n", tftp, len );
  739. return -EINVAL;
  740. }
  741. DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
  742. "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
  743. /* Determine final operation result */
  744. err = ntohs ( error->errcode );
  745. if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
  746. rc = -tftp_errors[err];
  747. if ( ! rc )
  748. rc = -ENOTSUP;
  749. /* Close TFTP request */
  750. tftp_done ( tftp, rc );
  751. return 0;
  752. }
  753. /**
  754. * Receive new data
  755. *
  756. * @v tftp TFTP connection
  757. * @v iobuf I/O buffer
  758. * @v meta Transfer metadata
  759. * @ret rc Return status code
  760. */
  761. static int tftp_rx ( struct tftp_request *tftp,
  762. struct io_buffer *iobuf,
  763. struct xfer_metadata *meta ) {
  764. struct sockaddr_tcpip *st_src;
  765. struct tftp_common *common = iobuf->data;
  766. size_t len = iob_len ( iobuf );
  767. int rc = -EINVAL;
  768. /* Sanity checks */
  769. if ( len < sizeof ( *common ) ) {
  770. DBGC ( tftp, "TFTP %p received underlength packet length "
  771. "%zd\n", tftp, len );
  772. goto done;
  773. }
  774. if ( ! meta->src ) {
  775. DBGC ( tftp, "TFTP %p received packet without source port\n",
  776. tftp );
  777. goto done;
  778. }
  779. /* Filter by TID. Set TID on first response received */
  780. st_src = ( struct sockaddr_tcpip * ) meta->src;
  781. if ( ! tftp->peer.st_family ) {
  782. memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
  783. DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
  784. ntohs ( tftp->peer.st_port ) );
  785. } else if ( memcmp ( &tftp->peer, st_src,
  786. sizeof ( tftp->peer ) ) != 0 ) {
  787. DBGC ( tftp, "TFTP %p received packet from wrong source (got "
  788. "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
  789. ntohs ( tftp->peer.st_port ) );
  790. goto done;
  791. }
  792. switch ( common->opcode ) {
  793. case htons ( TFTP_OACK ):
  794. rc = tftp_rx_oack ( tftp, iobuf->data, len );
  795. break;
  796. case htons ( TFTP_DATA ):
  797. rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
  798. break;
  799. case htons ( TFTP_ERROR ):
  800. rc = tftp_rx_error ( tftp, iobuf->data, len );
  801. break;
  802. default:
  803. DBGC ( tftp, "TFTP %p received strange packet type %d\n",
  804. tftp, ntohs ( common->opcode ) );
  805. break;
  806. };
  807. done:
  808. free_iob ( iobuf );
  809. return rc;
  810. }
  811. /**
  812. * Receive new data via socket
  813. *
  814. * @v socket Transport layer interface
  815. * @v iobuf I/O buffer
  816. * @v meta Transfer metadata
  817. * @ret rc Return status code
  818. */
  819. static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
  820. struct io_buffer *iobuf,
  821. struct xfer_metadata *meta ) {
  822. struct tftp_request *tftp =
  823. container_of ( socket, struct tftp_request, socket );
  824. /* Enable sending ACKs when we receive a unicast packet. This
  825. * covers three cases:
  826. *
  827. * 1. Standard TFTP; we should always send ACKs, and will
  828. * always receive a unicast packet before we need to send the
  829. * first ACK.
  830. *
  831. * 2. RFC2090 multicast TFTP; the only unicast packets we will
  832. * receive are the OACKs; enable sending ACKs here (before
  833. * processing the OACK) and disable it when processing the
  834. * multicast option if we are not the master client.
  835. *
  836. * 3. MTFTP; receiving a unicast datagram indicates that we
  837. * are the "master client" and should send ACKs.
  838. */
  839. tftp->flags |= TFTP_FL_SEND_ACK;
  840. return tftp_rx ( tftp, iobuf, meta );
  841. }
  842. /** TFTP socket operations */
  843. static struct xfer_interface_operations tftp_socket_operations = {
  844. .close = ignore_xfer_close,
  845. .vredirect = xfer_vreopen,
  846. .window = unlimited_xfer_window,
  847. .alloc_iob = default_xfer_alloc_iob,
  848. .deliver_iob = tftp_socket_deliver_iob,
  849. .deliver_raw = xfer_deliver_as_iob,
  850. };
  851. /**
  852. * Receive new data via multicast socket
  853. *
  854. * @v mc_socket Multicast transport layer interface
  855. * @v iobuf I/O buffer
  856. * @v meta Transfer metadata
  857. * @ret rc Return status code
  858. */
  859. static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
  860. struct io_buffer *iobuf,
  861. struct xfer_metadata *meta ) {
  862. struct tftp_request *tftp =
  863. container_of ( mc_socket, struct tftp_request, mc_socket );
  864. return tftp_rx ( tftp, iobuf, meta );
  865. }
  866. /** TFTP multicast socket operations */
  867. static struct xfer_interface_operations tftp_mc_socket_operations = {
  868. .close = ignore_xfer_close,
  869. .vredirect = xfer_vreopen,
  870. .window = unlimited_xfer_window,
  871. .alloc_iob = default_xfer_alloc_iob,
  872. .deliver_iob = tftp_mc_socket_deliver_iob,
  873. .deliver_raw = xfer_deliver_as_iob,
  874. };
  875. /**
  876. * Close TFTP data transfer interface
  877. *
  878. * @v xfer Data transfer interface
  879. * @v rc Reason for close
  880. */
  881. static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
  882. struct tftp_request *tftp =
  883. container_of ( xfer, struct tftp_request, xfer );
  884. DBGC ( tftp, "TFTP %p interface closed: %s\n",
  885. tftp, strerror ( rc ) );
  886. tftp_done ( tftp, rc );
  887. }
  888. /**
  889. * Check flow control window
  890. *
  891. * @v xfer Data transfer interface
  892. * @ret len Length of window
  893. */
  894. static size_t tftp_xfer_window ( struct xfer_interface *xfer ) {
  895. struct tftp_request *tftp =
  896. container_of ( xfer, struct tftp_request, xfer );
  897. /* We abuse this data-xfer method to convey the blocksize to
  898. * the caller. This really should be done using some kind of
  899. * stat() method, but we don't yet have the facility to do
  900. * that.
  901. */
  902. return tftp->blksize;
  903. }
  904. /** TFTP data transfer interface operations */
  905. static struct xfer_interface_operations tftp_xfer_operations = {
  906. .close = tftp_xfer_close,
  907. .vredirect = ignore_xfer_vredirect,
  908. .window = tftp_xfer_window,
  909. .alloc_iob = default_xfer_alloc_iob,
  910. .deliver_iob = xfer_deliver_as_raw,
  911. .deliver_raw = ignore_xfer_deliver_raw,
  912. };
  913. /**
  914. * Initiate TFTP/TFTM/MTFTP download
  915. *
  916. * @v xfer Data transfer interface
  917. * @v uri Uniform Resource Identifier
  918. * @ret rc Return status code
  919. */
  920. static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
  921. unsigned int default_port,
  922. struct sockaddr *multicast,
  923. unsigned int flags ) {
  924. struct tftp_request *tftp;
  925. int rc;
  926. /* Sanity checks */
  927. if ( ! uri->host )
  928. return -EINVAL;
  929. if ( ! uri->path )
  930. return -EINVAL;
  931. /* Allocate and populate TFTP structure */
  932. tftp = zalloc ( sizeof ( *tftp ) );
  933. if ( ! tftp )
  934. return -ENOMEM;
  935. tftp->refcnt.free = tftp_free;
  936. xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
  937. tftp->uri = uri_get ( uri );
  938. xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
  939. xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
  940. &tftp->refcnt );
  941. tftp->blksize = TFTP_DEFAULT_BLKSIZE;
  942. tftp->flags = flags;
  943. tftp->timer.expired = tftp_timer_expired;
  944. /* Open socket */
  945. tftp->port = uri_port ( tftp->uri, default_port );
  946. if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
  947. goto err;
  948. /* Open multicast socket */
  949. if ( multicast ) {
  950. if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
  951. goto err;
  952. }
  953. /* Start timer to initiate RRQ */
  954. start_timer_nodelay ( &tftp->timer );
  955. /* Attach to parent interface, mortalise self, and return */
  956. xfer_plug_plug ( &tftp->xfer, xfer );
  957. ref_put ( &tftp->refcnt );
  958. return 0;
  959. err:
  960. DBGC ( tftp, "TFTP %p could not create request: %s\n",
  961. tftp, strerror ( rc ) );
  962. tftp_done ( tftp, rc );
  963. ref_put ( &tftp->refcnt );
  964. return rc;
  965. }
  966. /**
  967. * Initiate TFTP download
  968. *
  969. * @v xfer Data transfer interface
  970. * @v uri Uniform Resource Identifier
  971. * @ret rc Return status code
  972. */
  973. static int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
  974. return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
  975. TFTP_FL_RRQ_SIZES );
  976. }
  977. /** TFTP URI opener */
  978. struct uri_opener tftp_uri_opener __uri_opener = {
  979. .scheme = "tftp",
  980. .open = tftp_open,
  981. };
  982. /**
  983. * Initiate TFTM download
  984. *
  985. * @v xfer Data transfer interface
  986. * @v uri Uniform Resource Identifier
  987. * @ret rc Return status code
  988. */
  989. static int tftm_open ( struct xfer_interface *xfer, struct uri *uri ) {
  990. return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
  991. ( TFTP_FL_RRQ_SIZES |
  992. TFTP_FL_RRQ_MULTICAST ) );
  993. }
  994. /** TFTM URI opener */
  995. struct uri_opener tftm_uri_opener __uri_opener = {
  996. .scheme = "tftm",
  997. .open = tftm_open,
  998. };
  999. /**
  1000. * Initiate MTFTP download
  1001. *
  1002. * @v xfer Data transfer interface
  1003. * @v uri Uniform Resource Identifier
  1004. * @ret rc Return status code
  1005. */
  1006. static int mtftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
  1007. return tftp_core_open ( xfer, uri, MTFTP_PORT,
  1008. ( struct sockaddr * ) &tftp_mtftp_socket,
  1009. TFTP_FL_MTFTP_RECOVERY );
  1010. }
  1011. /** MTFTP URI opener */
  1012. struct uri_opener mtftp_uri_opener __uri_opener = {
  1013. .scheme = "mtftp",
  1014. .open = mtftp_open,
  1015. };
  1016. /******************************************************************************
  1017. *
  1018. * Settings
  1019. *
  1020. ******************************************************************************
  1021. */
  1022. /** TFTP server setting */
  1023. struct setting next_server_setting __setting = {
  1024. .name = "next-server",
  1025. .description = "TFTP server",
  1026. .tag = DHCP_EB_SIADDR,
  1027. .type = &setting_type_ipv4,
  1028. };
  1029. /**
  1030. * Apply TFTP configuration settings
  1031. *
  1032. * @ret rc Return status code
  1033. */
  1034. static int tftp_apply_settings ( void ) {
  1035. static struct in_addr tftp_server = { 0 };
  1036. struct in_addr last_tftp_server;
  1037. char uri_string[32];
  1038. struct uri *uri;
  1039. /* Retrieve TFTP server setting */
  1040. last_tftp_server = tftp_server;
  1041. fetch_ipv4_setting ( NULL, &next_server_setting, &tftp_server );
  1042. /* If TFTP server setting has changed, set the current working
  1043. * URI to match. Do it only when the TFTP server has changed
  1044. * to try to minimise surprises to the user, who probably
  1045. * won't expect the CWURI to change just because they updated
  1046. * an unrelated setting and triggered all the settings
  1047. * applicators.
  1048. */
  1049. if ( tftp_server.s_addr != last_tftp_server.s_addr ) {
  1050. snprintf ( uri_string, sizeof ( uri_string ),
  1051. "tftp://%s/", inet_ntoa ( tftp_server ) );
  1052. uri = parse_uri ( uri_string );
  1053. if ( ! uri )
  1054. return -ENOMEM;
  1055. churi ( uri );
  1056. uri_put ( uri );
  1057. }
  1058. return 0;
  1059. }
  1060. /** TFTP settings applicator */
  1061. struct settings_applicator tftp_settings_applicator __settings_applicator = {
  1062. .apply = tftp_apply_settings,
  1063. };