您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <string.h>
  21. #include <strings.h>
  22. #include <byteswap.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <vsprintf.h>
  26. #include <gpxe/async.h>
  27. #include <gpxe/tftp.h>
  28. /** @file
  29. *
  30. * TFTP protocol
  31. *
  32. */
  33. /** A TFTP option */
  34. struct tftp_option {
  35. /** Option name */
  36. const char *name;
  37. /** Option processor
  38. *
  39. * @v tftp TFTP connection
  40. * @v value Option value
  41. * @ret rc Return status code
  42. */
  43. int ( * process ) ( struct tftp_session *tftp, const char *value );
  44. };
  45. /**
  46. * Process TFTP "blksize" option
  47. *
  48. * @v tftp TFTP connection
  49. * @v value Option value
  50. * @ret rc Return status code
  51. */
  52. static int tftp_process_blksize ( struct tftp_session *tftp,
  53. const char *value ) {
  54. char *end;
  55. tftp->blksize = strtoul ( value, &end, 10 );
  56. if ( *end ) {
  57. DBG ( "TFTP %p got invalid blksize \"%s\"\n", tftp, value );
  58. return -EINVAL;
  59. }
  60. DBG ( "TFTP %p blksize=%d\n", tftp, tftp->blksize );
  61. return 0;
  62. }
  63. /**
  64. * Process TFTP "tsize" option
  65. *
  66. * @v tftp TFTP connection
  67. * @v value Option value
  68. * @ret rc Return status code
  69. */
  70. static int tftp_process_tsize ( struct tftp_session *tftp,
  71. const char *value ) {
  72. char *end;
  73. tftp->tsize = strtoul ( value, &end, 10 );
  74. if ( *end ) {
  75. DBG ( "TFTP %p got invalid tsize \"%s\"\n", tftp, value );
  76. return -EINVAL;
  77. }
  78. DBG ( "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
  79. return 0;
  80. }
  81. /** Recognised TFTP options */
  82. static struct tftp_option tftp_options[] = {
  83. { "blksize", tftp_process_blksize },
  84. { "tsize", tftp_process_tsize },
  85. { NULL, NULL }
  86. };
  87. /**
  88. * Process TFTP option
  89. *
  90. * @v tftp TFTP connection
  91. * @v name Option name
  92. * @v value Option value
  93. * @ret rc Return status code
  94. */
  95. static int tftp_process_option ( struct tftp_session *tftp,
  96. const char *name, const char *value ) {
  97. struct tftp_option *option;
  98. for ( option = tftp_options ; option->name ; option++ ) {
  99. if ( strcasecmp ( name, option->name ) == 0 )
  100. return option->process ( tftp, value );
  101. }
  102. DBG ( "TFTP %p received unknown option \"%s\" = \"%s\"\n",
  103. tftp, name, value );
  104. return -EINVAL;
  105. }
  106. /** Translation between TFTP errors and internal error numbers */
  107. static const uint8_t tftp_errors[] = {
  108. [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
  109. [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
  110. [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
  111. };
  112. /**
  113. * Mark TFTP session as complete
  114. *
  115. * @v tftp TFTP connection
  116. * @v rc Return status code
  117. */
  118. static void tftp_done ( struct tftp_session *tftp, int rc ) {
  119. /* Stop the retry timer */
  120. stop_timer ( &tftp->timer );
  121. /* Close UDP connection */
  122. udp_close ( &tftp->udp );
  123. /* Mark async operation as complete */
  124. async_done ( &tftp->aop, rc );
  125. }
  126. /**
  127. * Send next packet in TFTP session
  128. *
  129. * @v tftp TFTP connection
  130. */
  131. static void tftp_send_packet ( struct tftp_session *tftp ) {
  132. start_timer ( &tftp->timer );
  133. udp_senddata ( &tftp->udp );
  134. }
  135. /**
  136. * Handle TFTP retransmission timer expiry
  137. *
  138. * @v timer Retry timer
  139. * @v fail Failure indicator
  140. */
  141. static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
  142. struct tftp_session *tftp =
  143. container_of ( timer, struct tftp_session, timer );
  144. if ( fail ) {
  145. tftp_done ( tftp, -ETIMEDOUT );
  146. } else {
  147. tftp_send_packet ( tftp );
  148. }
  149. }
  150. /**
  151. * Mark TFTP block as received
  152. *
  153. * @v tftp TFTP connection
  154. * @v block Block number
  155. */
  156. static void tftp_received ( struct tftp_session *tftp, unsigned int block ) {
  157. /* Stop the retry timer */
  158. stop_timer ( &tftp->timer );
  159. /* Update state to indicate which block we're now waiting for */
  160. tftp->state = block;
  161. /* Send next packet */
  162. tftp_send_packet ( tftp );
  163. }
  164. /**
  165. * Transmit RRQ
  166. *
  167. * @v tftp TFTP connection
  168. * @v buf Temporary data buffer
  169. * @v len Length of temporary data buffer
  170. * @ret rc Return status code
  171. */
  172. static int tftp_send_rrq ( struct tftp_session *tftp, void *buf, size_t len ) {
  173. struct tftp_rrq *rrq = buf;
  174. void *data;
  175. void *end;
  176. DBG ( "TFTP %p requesting \"%s\"\n", tftp, tftp->filename );
  177. data = rrq->data;
  178. end = ( buf + len );
  179. if ( data > end )
  180. goto overflow;
  181. data += snprintf ( data, ( end - data ),
  182. "%s%coctet%cblksize%c%d%ctsize%c0",
  183. tftp->filename, 0, 0, 0, tftp->blksize, 0, 0 ) + 1;
  184. if ( data > end )
  185. goto overflow;
  186. rrq->opcode = htons ( TFTP_RRQ );
  187. return udp_send ( &tftp->udp, buf, ( data - buf ) );
  188. overflow:
  189. DBG ( "TFTP %p RRQ out of space\n", tftp );
  190. return -ENOBUFS;
  191. }
  192. /**
  193. * Receive OACK
  194. *
  195. * @v tftp TFTP connection
  196. * @v buf Temporary data buffer
  197. * @v len Length of temporary data buffer
  198. * @ret rc Return status code
  199. */
  200. static int tftp_rx_oack ( struct tftp_session *tftp, void *buf, size_t len ) {
  201. struct tftp_oack *oack = buf;
  202. char *end = buf + len;
  203. char *name;
  204. char *value;
  205. int rc;
  206. /* Sanity check */
  207. if ( len < sizeof ( *oack ) ) {
  208. DBG ( "TFTP %p received underlength OACK packet length %d\n",
  209. tftp, len );
  210. return -EINVAL;
  211. }
  212. if ( end[-1] != '\0' ) {
  213. DBG ( "TFTP %p received OACK missing final NUL\n", tftp );
  214. return -EINVAL;
  215. }
  216. /* Process each option in turn */
  217. name = oack->data;
  218. while ( name < end ) {
  219. value = ( name + strlen ( name ) + 1 );
  220. if ( value == end ) {
  221. DBG ( "TFTP %p received OACK missing value for option "
  222. "\"%s\"\n", tftp, name );
  223. return -EINVAL;
  224. }
  225. if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
  226. return rc;
  227. name = ( value + strlen ( value ) + 1 );
  228. }
  229. /* Mark as received block 0 (the OACK) */
  230. tftp_received ( tftp, 0 );
  231. return 0;
  232. }
  233. /**
  234. * Receive DATA
  235. *
  236. * @v tftp TFTP connection
  237. * @v buf Temporary data buffer
  238. * @v len Length of temporary data buffer
  239. * @ret rc Return status code
  240. */
  241. static int tftp_rx_data ( struct tftp_session *tftp, void *buf, size_t len ) {
  242. struct tftp_data *data = buf;
  243. unsigned int block;
  244. size_t data_len;
  245. /* Sanity check */
  246. if ( len < sizeof ( *data ) ) {
  247. DBG ( "TFTP %p received underlength DATA packet length %d\n",
  248. tftp, len );
  249. return -EINVAL;
  250. }
  251. /* Pass to callback */
  252. block = ntohs ( data->block );
  253. data_len = ( len - offsetof ( typeof ( *data ), data ) );
  254. tftp->callback ( tftp, block, data->data, data_len );
  255. /* Mark block as received */
  256. tftp_received ( tftp, block );
  257. /* Finish when final block received */
  258. if ( data_len < tftp->blksize )
  259. tftp_done ( tftp, 0 );
  260. return 0;
  261. }
  262. /**
  263. * Transmit ACK
  264. *
  265. * @v tftp TFTP connection
  266. * @v buf Temporary data buffer
  267. * @v len Length of temporary data buffer
  268. * @ret rc Return status code
  269. */
  270. static int tftp_send_ack ( struct tftp_session *tftp ) {
  271. struct tftp_ack ack;
  272. ack.opcode = htons ( TFTP_ACK );
  273. ack.block = htons ( tftp->state );
  274. return udp_send ( &tftp->udp, &ack, sizeof ( ack ) );
  275. }
  276. /**
  277. * Receive ERROR
  278. *
  279. * @v tftp TFTP connection
  280. * @v buf Temporary data buffer
  281. * @v len Length of temporary data buffer
  282. * @ret rc Return status code
  283. */
  284. static int tftp_rx_error ( struct tftp_session *tftp, void *buf, size_t len ) {
  285. struct tftp_error *error = buf;
  286. unsigned int err;
  287. int rc = 0;
  288. /* Sanity check */
  289. if ( len < sizeof ( *error ) ) {
  290. DBG ( "TFTP %p received underlength ERROR packet length %d\n",
  291. tftp, len );
  292. return -EINVAL;
  293. }
  294. DBG ( "TFTP %p received ERROR packet with code %d, message \"%s\"\n",
  295. tftp, ntohs ( error->errcode ), error->errmsg );
  296. /* Determine final operation result */
  297. err = ntohs ( error->errcode );
  298. if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
  299. rc = -tftp_errors[err];
  300. if ( ! rc )
  301. rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
  302. /* Close TFTP session */
  303. tftp_done ( tftp, rc );
  304. return 0;
  305. }
  306. /**
  307. * Transmit data
  308. *
  309. * @v conn UDP connection
  310. * @v buf Temporary data buffer
  311. * @v len Length of temporary data buffer
  312. * @ret rc Return status code
  313. */
  314. static int tftp_senddata ( struct udp_connection *conn,
  315. void *buf, size_t len ) {
  316. struct tftp_session *tftp =
  317. container_of ( conn, struct tftp_session, udp );
  318. if ( tftp->state < 0 ) {
  319. return tftp_send_rrq ( tftp, buf, len );
  320. } else {
  321. return tftp_send_ack ( tftp );
  322. }
  323. }
  324. /**
  325. * Receive new data
  326. *
  327. * @v udp UDP connection
  328. * @v data Received data
  329. * @v len Length of received data
  330. * @v st_src Partially-filled source address
  331. * @v st_dest Partially-filled destination address
  332. */
  333. static int tftp_newdata ( struct udp_connection *conn, void *data, size_t len,
  334. struct sockaddr_tcpip *st_src __unused,
  335. struct sockaddr_tcpip *st_dest __unused ) {
  336. struct tftp_session *tftp =
  337. container_of ( conn, struct tftp_session, udp );
  338. struct tftp_common *common = data;
  339. if ( len < sizeof ( *common ) ) {
  340. DBG ( "TFTP %p received underlength packet length %d\n",
  341. tftp, len );
  342. return -EINVAL;
  343. }
  344. /* Filter by TID. Set TID on first response received */
  345. if ( tftp->tid ) {
  346. if ( tftp->tid != st_src->st_port ) {
  347. DBG ( "TFTP %p received packet from wrong port "
  348. "(got %d, wanted %d)\n", tftp,
  349. ntohs ( st_src->st_port ), ntohs ( tftp->tid ) );
  350. return -EINVAL;
  351. }
  352. } else {
  353. tftp->tid = st_src->st_port;
  354. DBG ( "TFTP %p using remote port %d\n", tftp,
  355. ntohs ( tftp->tid ) );
  356. udp_connect_port ( &tftp->udp, tftp->tid );
  357. }
  358. /* Filter by source address */
  359. if ( memcmp ( st_src, udp_peer ( &tftp->udp ),
  360. sizeof ( *st_src ) ) != 0 ) {
  361. DBG ( "TFTP %p received packet from foreign source\n", tftp );
  362. return -EINVAL;
  363. }
  364. switch ( common->opcode ) {
  365. case htons ( TFTP_OACK ):
  366. return tftp_rx_oack ( tftp, data, len );
  367. case htons ( TFTP_DATA ):
  368. return tftp_rx_data ( tftp, data, len );
  369. case htons ( TFTP_ERROR ):
  370. return tftp_rx_error ( tftp, data, len );
  371. default:
  372. DBG ( "TFTP %p received strange packet type %d\n", tftp,
  373. ntohs ( common->opcode ) );
  374. return -EINVAL;
  375. };
  376. }
  377. /** TFTP UDP operations */
  378. static struct udp_operations tftp_udp_operations = {
  379. .senddata = tftp_senddata,
  380. .newdata = tftp_newdata,
  381. };
  382. /**
  383. * Initiate TFTP download
  384. *
  385. * @v tftp TFTP session
  386. * @ret aop Asynchronous operation
  387. */
  388. struct async_operation * tftp_get ( struct tftp_session *tftp ) {
  389. int rc;
  390. assert ( tftp->filename != NULL );
  391. assert ( tftp->callback != NULL );
  392. assert ( tftp->udp.peer.st_family != 0 );
  393. /* Initialise TFTP session */
  394. tftp->udp.udp_op = &tftp_udp_operations;
  395. tftp->timer.expired = tftp_timer_expired;
  396. tftp->state = -1;
  397. tftp->blksize = TFTP_DEFAULT_BLKSIZE;
  398. /* Open UDP connection */
  399. if ( ( rc = udp_open ( &tftp->udp, 0 ) ) != 0 ) {
  400. async_done ( &tftp->aop, rc );
  401. goto out;
  402. }
  403. /* Transmit initial RRQ */
  404. tftp_send_packet ( tftp );
  405. out:
  406. return &tftp->aop;
  407. }