Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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