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

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