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.

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