You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tftp.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef TFTP_H
  2. #define TFTP_H
  3. /** @file */
  4. #include <gpxe/in.h>
  5. #include <gpxe/buffer.h>
  6. #include "nic.h"
  7. #include "ip.h"
  8. #include "udp.h"
  9. #define TFTP_PORT 69 /**< Default TFTP server port */
  10. #define TFTP_DEFAULT_BLKSIZE 512
  11. #define TFTP_MAX_BLKSIZE 1432 /* 512 */
  12. #define TFTP_RRQ 1
  13. #define TFTP_WRQ 2
  14. #define TFTP_DATA 3
  15. #define TFTP_ACK 4
  16. #define TFTP_ERROR 5
  17. #define TFTP_OACK 6
  18. #define TFTP_ERR_FILE_NOT_FOUND 1 /**< File not found */
  19. #define TFTP_ERR_ACCESS_DENIED 2 /**< Access violation */
  20. #define TFTP_ERR_DISK_FULL 3 /**< Disk full or allocation exceeded */
  21. #define TFTP_ERR_ILLEGAL_OP 4 /**< Illegal TFTP operation */
  22. #define TFTP_ERR_UNKNOWN_TID 5 /**< Unknown transfer ID */
  23. #define TFTP_ERR_FILE_EXISTS 6 /**< File already exists */
  24. #define TFTP_ERR_UNKNOWN_USER 7 /**< No such user */
  25. #define TFTP_ERR_BAD_OPTS 8 /**< Option negotiation failed */
  26. /** A TFTP request (RRQ) packet */
  27. struct tftp_rrq {
  28. struct iphdr ip;
  29. struct udphdr udp;
  30. uint16_t opcode;
  31. char data[TFTP_DEFAULT_BLKSIZE];
  32. } PACKED;
  33. /** A TFTP data (DATA) packet */
  34. struct tftp_data {
  35. struct iphdr ip;
  36. struct udphdr udp;
  37. uint16_t opcode;
  38. uint16_t block;
  39. uint8_t data[TFTP_MAX_BLKSIZE];
  40. } PACKED;
  41. /** A TFTP acknowledgement (ACK) packet */
  42. struct tftp_ack {
  43. struct iphdr ip;
  44. struct udphdr udp;
  45. uint16_t opcode;
  46. uint16_t block;
  47. } PACKED;
  48. /** A TFTP error (ERROR) packet */
  49. struct tftp_error {
  50. struct iphdr ip;
  51. struct udphdr udp;
  52. uint16_t opcode;
  53. uint16_t errcode;
  54. char errmsg[TFTP_DEFAULT_BLKSIZE];
  55. } PACKED;
  56. /** A TFTP options acknowledgement (OACK) packet */
  57. struct tftp_oack {
  58. struct iphdr ip;
  59. struct udphdr udp;
  60. uint16_t opcode;
  61. uint8_t data[TFTP_DEFAULT_BLKSIZE];
  62. } PACKED;
  63. /** The common header of all TFTP packets */
  64. struct tftp_common {
  65. struct iphdr ip;
  66. struct udphdr udp;
  67. uint16_t opcode;
  68. } PACKED;
  69. /** A union encapsulating all TFTP packet types */
  70. union tftp_any {
  71. struct tftp_common common;
  72. struct tftp_rrq rrq;
  73. struct tftp_data data;
  74. struct tftp_ack ack;
  75. struct tftp_error error;
  76. struct tftp_oack oack;
  77. };
  78. /**
  79. * TFTP state
  80. *
  81. * This data structure holds the state for an ongoing TFTP transfer.
  82. */
  83. struct tftp_state {
  84. /** TFTP server address
  85. *
  86. * This is the IP address and UDP port from which data packets
  87. * will be sent, and to which ACK packets should be sent.
  88. */
  89. struct sockaddr_in server;
  90. /** TFTP client port
  91. *
  92. * This is the UDP port from which the open request will be
  93. * sent, and to which any unicast data packets will be sent.
  94. */
  95. uint16_t lport;
  96. /** TFTP multicast address
  97. *
  98. * This is the IP address and UDP port to which multicast data
  99. * packets, if any, will be sent.
  100. */
  101. struct sockaddr_in multicast;
  102. /** Master client
  103. *
  104. * This will be true if the client is the master client for a
  105. * multicast protocol (i.e. MTFTP or TFTM). (It will always
  106. * be true for a non-multicast protocol, i.e. plain old TFTP).
  107. */
  108. int master;
  109. /** Data block size
  110. *
  111. * This is the "blksize" option negotiated with the TFTP
  112. * server. (If the TFTP server does not support TFTP options,
  113. * this will default to 512).
  114. */
  115. unsigned int blksize;
  116. /** File size
  117. *
  118. * This is the value returned in the "tsize" option from the
  119. * TFTP server. If the TFTP server does not support the
  120. * "tsize" option, this value will be zero.
  121. */
  122. off_t tsize;
  123. /** Last received block
  124. *
  125. * The block number of the most recent block received from the
  126. * TFTP server. Note that the first data block is block 1; a
  127. * value of 0 indicates that no data blocks have yet been
  128. * received.
  129. *
  130. * For multicast TFTP protocols, where the blocks may not be
  131. * received in strict order, the meaning of this field changes
  132. * slightly, to "first missing block minus one". For example,
  133. * suppose that we have received blocks 1, 2, 4 and 5; this
  134. * field would then have the value 2, since the first missing
  135. * block is block 3. If the blocks do arrive in strict order,
  136. * this definition is exactly equivalent to "most recently
  137. * received block".
  138. */
  139. unsigned int block;
  140. };
  141. struct tftpreq_info_t {
  142. struct sockaddr_in *server;
  143. const char *name;
  144. unsigned short blksize;
  145. } PACKED;
  146. struct tftpblk_info_t {
  147. char *data;
  148. unsigned int block;
  149. unsigned int len;
  150. int eof;
  151. } PACKED;
  152. #define TFTP_MIN_PACKET (sizeof(struct iphdr) + sizeof(struct udphdr) + 4)
  153. #endif /* TFTP_H */