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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "etherboot.h"
  2. #include "proto.h"
  3. #include "errno.h"
  4. #include "tftp.h"
  5. #include "tftpcore.h"
  6. /** @file
  7. *
  8. * TFTM protocol
  9. *
  10. * TFTM is a protocol defined in RFC2090 as a multicast extension to
  11. * TFTP.
  12. */
  13. static inline int tftm_process_opts ( struct tftp_state *state,
  14. struct tftp_oack *oack ) {
  15. struct in_addr old_mcast_addr = state->multicast.sin_addr;
  16. if ( ! tftp_process_opts ( state, oack ) )
  17. return 0;
  18. if ( old_mcast_addr.s_addr != state->multicast.sin_addr.s_addr ) {
  19. if ( old_mcast_addr.s_addr ) {
  20. DBG ( "TFTM: Leaving multicast group %@\n",
  21. old_mcast_addr.s_addr );
  22. leave_group ( IGMP_SERVER );
  23. }
  24. DBG ( "TFTM: Joining multicast group %@\n",
  25. state->multicast.sin_addr.s_addr );
  26. join_group ( IGMP_SERVER, state->multicast.sin_addr.s_addr );
  27. }
  28. DBG ( "TFTM: I am a %s client\n",
  29. ( state->master ? "master" : "slave" ) );
  30. return 1;
  31. }
  32. static inline int tftm_process_data ( struct tftp_state *state,
  33. struct tftp_data *data,
  34. struct buffer *buffer ) {
  35. unsigned int blksize;
  36. off_t offset;
  37. /* Calculate block size and offset within file */
  38. blksize = ( ntohs ( data->udp.len )
  39. + offsetof ( typeof ( *data ), udp )
  40. - offsetof ( typeof ( *data ), data ) );
  41. offset = ( ntohs ( data->block ) - 1 ) * state->blksize;
  42. /* Check for oversized block */
  43. if ( blksize > state->blksize ) {
  44. DBG ( "TFTM: oversized block size %d (max %d)\n",
  45. blksize, state->blksize );
  46. errno = PXENV_STATUS_TFTP_INVALID_PACKET_SIZE;
  47. return 0;
  48. }
  49. /* Place block in the buffer */
  50. if ( ! fill_buffer ( buffer, data->data, offset, blksize ) ) {
  51. DBG ( "TFTM: could not place data in buffer: %m\n" );
  52. return 0;
  53. }
  54. /* If this is the last block, record the filesize (in case the
  55. * server didn't supply a tsize option.
  56. */
  57. if ( blksize < state->blksize ) {
  58. state->tsize = offset + blksize;
  59. }
  60. /* Record the last received block */
  61. state->block = ntohs ( data->block );
  62. return 1;
  63. }
  64. static inline int tftm_next ( struct tftp_state *state,
  65. union tftp_any **reply,
  66. struct buffer *buffer ) {
  67. long listen_timeout;
  68. listen_timeout = rfc2131_sleep_interval ( TIMEOUT, MAX_TFTP_RETRIES );
  69. /* If we are not the master client, just listen for the next
  70. * packet
  71. */
  72. if ( ! state->master ) {
  73. if ( tftp_get ( state, listen_timeout, reply ) ) {
  74. /* Heard a non-error packet */
  75. return 1;
  76. }
  77. if ( *reply ) {
  78. /* Received an error packet */
  79. return 0;
  80. }
  81. /* Didn't hear anything; try prodding the server */
  82. state->master = 1;
  83. }
  84. /* We are the master client; trigger the next packet
  85. * that we want
  86. */
  87. state->block = buffer->fill / state->blksize;
  88. return tftp_ack ( state, reply );
  89. }
  90. /**
  91. * Download a file via TFTM
  92. *
  93. * @v server TFTP server
  94. * @v file File name
  95. * @v buffer Buffer into which to load file
  96. * @ret True File was downloaded successfully
  97. * @ret False File was not downloaded successfully
  98. * @err #PXENV_STATUS_TFTP_UNKNOWN_OPCODE Unknown type of TFTP block received
  99. * @err other As returned by tftp_open()
  100. * @err other As returned by tftp_process_opts()
  101. * @err other As returned by tftp_ack()
  102. * @err other As returned by tftp_process_data()
  103. *
  104. * Download a file from a TFTP server into the specified buffer using
  105. * the TFTM protocol.
  106. */
  107. static int tftm ( char *url __unused, struct sockaddr_in *server, char *file,
  108. struct buffer *buffer ) {
  109. struct tftp_state state;
  110. union tftp_any *reply;
  111. int rc = 0;
  112. /* Initialise TFTP state */
  113. memset ( &state, 0, sizeof ( state ) );
  114. state.server = *server;
  115. /* Start as the master. This means that if the TFTP server
  116. * doesn't actually support multicast, we'll still ACK the
  117. * packets and it should all proceed as for a normal TFTP
  118. * connection.
  119. */
  120. state.master = 1;
  121. /* Open the file */
  122. if ( ! tftp_open ( &state, file, &reply, 1 ) ) {
  123. DBG ( "TFTM: could not open %@:%d/%s : %m\n",
  124. server->sin_addr.s_addr, server->sin_port, file );
  125. return 0;
  126. }
  127. /* Fetch file, a block at a time */
  128. while ( 1 ) {
  129. twiddle();
  130. /* Process the current packet */
  131. switch ( ntohs ( reply->common.opcode ) ) {
  132. case TFTP_OACK:
  133. /* Options can be received at any time */
  134. if ( ! tftm_process_opts ( &state, &reply->oack ) ) {
  135. DBG ( "TFTM: failed to process OACK: %m\n" );
  136. tftp_error ( &state, TFTP_ERR_BAD_OPTS, NULL );
  137. goto out;
  138. }
  139. break;
  140. case TFTP_DATA:
  141. if ( ! tftm_process_data ( &state, &reply->data,
  142. buffer ) ) {
  143. DBG ( "TFTM: failed to process DATA: %m\n" );
  144. tftp_error ( &state, TFTP_ERR_ILLEGAL_OP,
  145. NULL );
  146. goto out;
  147. }
  148. break;
  149. default:
  150. DBG ( "TFTM: unexpected packet type %d\n",
  151. ntohs ( reply->common.opcode ) );
  152. errno = PXENV_STATUS_TFTP_UNKNOWN_OPCODE;
  153. tftp_error ( &state, TFTP_ERR_ILLEGAL_OP, NULL );
  154. goto out;
  155. }
  156. /* If we know the filesize, and we have all the data, stop */
  157. if ( state.tsize && ( buffer->fill == state.tsize ) )
  158. break;
  159. /* Fetch the next packet */
  160. if ( ! tftm_next ( &state, &reply, buffer ) ) {
  161. DBG ( "TFTM: could not get next block: %m\n" );
  162. if ( ! reply ) {
  163. tftp_error ( &state, TFTP_ERR_ILLEGAL_OP,
  164. NULL );
  165. }
  166. goto out;
  167. }
  168. }
  169. /* ACK the final packet, as a courtesy to the server */
  170. tftp_ack_nowait ( &state );
  171. rc = 1;
  172. out:
  173. if ( state.multicast.sin_addr.s_addr ) {
  174. leave_group ( IGMP_SERVER );
  175. }
  176. return rc;
  177. }
  178. static struct protocol tftm_protocol __protocol = {
  179. .name = "x-tftm",
  180. .default_port = TFTP_PORT,
  181. .load = tftm,
  182. };