Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

tcp.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef _GPXE_TCP_H
  2. #define _GPXE_TCP_H
  3. /** @file
  4. *
  5. * TCP protocol
  6. *
  7. * This file defines the gPXE TCP API.
  8. *
  9. */
  10. #include "latch.h"
  11. #include <gpxe/tcpip.h>
  12. #include <gpxe/stream.h>
  13. /**
  14. * A TCP header
  15. */
  16. struct tcp_header {
  17. uint16_t src; /* Source port */
  18. uint16_t dest; /* Destination port */
  19. uint32_t seq; /* Sequence number */
  20. uint32_t ack; /* Acknowledgement number */
  21. uint8_t hlen; /* Header length (4), Reserved (4) */
  22. uint8_t flags; /* Reserved (2), Flags (6) */
  23. uint16_t win; /* Advertised window */
  24. uint16_t csum; /* Checksum */
  25. uint16_t urg; /* Urgent pointer */
  26. };
  27. /**
  28. * TCP MSS option
  29. */
  30. struct tcp_mss_option {
  31. uint8_t kind;
  32. uint8_t length;
  33. uint16_t mss;
  34. };
  35. /** Code for the TCP MSS option */
  36. #define TCP_OPTION_MSS 2
  37. /*
  38. * TCP flags
  39. */
  40. #define TCP_CWR 0x80
  41. #define TCP_ECE 0x40
  42. #define TCP_URG 0x20
  43. #define TCP_ACK 0x10
  44. #define TCP_PSH 0x08
  45. #define TCP_RST 0x04
  46. #define TCP_SYN 0x02
  47. #define TCP_FIN 0x01
  48. /**
  49. * @defgroup tcpstates TCP states
  50. *
  51. * The TCP state is defined by a combination of the flags that have
  52. * been sent to the peer, the flags that have been acknowledged by the
  53. * peer, and the flags that have been received from the peer.
  54. *
  55. * @{
  56. */
  57. /** TCP flags that have been sent in outgoing packets */
  58. #define TCP_STATE_SENT(flags) ( (flags) << 0 )
  59. #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
  60. /** TCP flags that have been acknowledged by the peer
  61. *
  62. * Note that this applies only to SYN and FIN.
  63. */
  64. #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
  65. #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
  66. /** TCP flags that have been received from the peer
  67. *
  68. * Note that this applies only to SYN and FIN, and that once SYN has
  69. * been received, we should always be sending ACK.
  70. */
  71. #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
  72. #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
  73. /** TCP flags that are currently being sent in outgoing packets */
  74. #define TCP_FLAGS_SENDING(state) \
  75. ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
  76. /** CLOSED
  77. *
  78. * The connection has not yet been used for anything.
  79. */
  80. #define TCP_CLOSED TCP_RST
  81. /** LISTEN
  82. *
  83. * Not currently used as a state; we have no support for listening
  84. * connections. Given a unique value to avoid compiler warnings.
  85. */
  86. #define TCP_LISTEN 0
  87. /** SYN_SENT
  88. *
  89. * SYN has been sent, nothing has yet been received or acknowledged.
  90. */
  91. #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
  92. /** SYN_RCVD
  93. *
  94. * SYN has been sent but not acknowledged, SYN has been received.
  95. */
  96. #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  97. TCP_STATE_RCVD ( TCP_SYN ) )
  98. /** ESTABLISHED
  99. *
  100. * SYN has been sent and acknowledged, SYN has been received.
  101. */
  102. #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  103. TCP_STATE_ACKED ( TCP_SYN ) | \
  104. TCP_STATE_RCVD ( TCP_SYN ) )
  105. /** FIN_WAIT_1
  106. *
  107. * SYN has been sent and acknowledged, SYN has been received, FIN has
  108. * been sent but not acknowledged, FIN has not been received.
  109. *
  110. * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
  111. * acknowledged, i.e. if the application closes the connection after
  112. * sending and receiving SYN, but before having had SYN acknowledged.
  113. * However, we have to *pretend* that SYN has been acknowledged
  114. * anyway, otherwise we end up sending SYN and FIN in the same
  115. * sequence number slot. Therefore, when we transition from SYN_RCVD
  116. * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
  117. * and increment our sequence number.
  118. */
  119. #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  120. TCP_STATE_ACKED ( TCP_SYN ) | \
  121. TCP_STATE_RCVD ( TCP_SYN ) )
  122. /** FIN_WAIT_2
  123. *
  124. * SYN has been sent and acknowledged, SYN has been received, FIN has
  125. * been sent and acknowledged, FIN ha not been received.
  126. */
  127. #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  128. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  129. TCP_STATE_RCVD ( TCP_SYN ) )
  130. /** CLOSING / LAST_ACK
  131. *
  132. * SYN has been sent and acknowledged, SYN has been received, FIN has
  133. * been sent but not acknowledged, FIN has been received.
  134. *
  135. * This state actually encompasses both CLOSING and LAST_ACK; they are
  136. * identical with the definition of state that we use. I don't
  137. * *believe* that they need to be distinguished.
  138. */
  139. #define TCP_CLOSING_OR_LAST_ACK \
  140. ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  141. TCP_STATE_ACKED ( TCP_SYN ) | \
  142. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  143. /** TIME_WAIT
  144. *
  145. * SYN has been sent and acknowledged, SYN has been received, FIN has
  146. * been sent and acknowledged, FIN has been received.
  147. */
  148. #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  149. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  150. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  151. /** CLOSE_WAIT
  152. *
  153. * SYN has been sent and acknowledged, SYN has been received, FIN has
  154. * been received.
  155. */
  156. #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  157. TCP_STATE_ACKED ( TCP_SYN ) | \
  158. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  159. /** Can send data in current state
  160. *
  161. * We can send data if and only if we have had our SYN acked and we
  162. * have not yet sent our FIN.
  163. */
  164. #define TCP_CAN_SEND_DATA(state) \
  165. ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
  166. TCP_STATE_SENT ( TCP_FIN ) ) ) \
  167. == TCP_STATE_ACKED ( TCP_SYN ) )
  168. /** Have closed gracefully
  169. *
  170. * We have closed gracefully if we have both received a FIN and had
  171. * our own FIN acked.
  172. */
  173. #define TCP_CLOSED_GRACEFULLY(state) \
  174. ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
  175. TCP_STATE_RCVD ( TCP_FIN ) ) ) \
  176. == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
  177. /** @} */
  178. /** Mask for TCP header length field */
  179. #define TCP_MASK_HLEN 0xf0
  180. /** Smallest port number on which a TCP connection can listen */
  181. #define TCP_MIN_PORT 1
  182. /* Some IOB constants */
  183. #define MAX_HDR_LEN 100
  184. #define MAX_IOB_LEN 1500
  185. #define MIN_IOB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
  186. /**
  187. * Maxmimum advertised TCP window size
  188. *
  189. * We estimate the TCP window size as the amount of free memory we
  190. * have. This is not strictly accurate (since it ignores any space
  191. * already allocated as RX buffers), but it will do for now.
  192. *
  193. * Since we don't store out-of-order received packets, the
  194. * retransmission penalty is that the whole window contents must be
  195. * resent. This suggests keeping the window size small, but bear in
  196. * mind that the maximum bandwidth on any link is limited to
  197. *
  198. * max_bandwidth = ( tcp_window / round_trip_time )
  199. *
  200. * With a 48kB window, which probably accurately reflects our amount
  201. * of free memory, and a WAN RTT of say 200ms, this gives a maximum
  202. * bandwidth of 240kB/s. This is sufficiently close to realistic that
  203. * we will need to be careful that our advertised window doesn't end
  204. * up limiting WAN download speeds.
  205. *
  206. * Finally, since the window goes into a 16-bit field and we cannot
  207. * actually use 65536, we use a window size of (65536-4) to ensure
  208. * that payloads remain dword-aligned.
  209. */
  210. #define TCP_MAX_WINDOW_SIZE ( 65536 - 4 )
  211. /**
  212. * Advertised TCP MSS
  213. *
  214. * We currently hardcode this to a reasonable value and hope that the
  215. * sender uses path MTU discovery. The alternative is breaking the
  216. * abstraction layer so that we can find out the MTU from the IP layer
  217. * (which would have to find out from the net device layer).
  218. */
  219. #define TCP_MSS 1460
  220. /** TCP maximum segment lifetime
  221. *
  222. * Currently set to 2 minutes, as per RFC 793.
  223. */
  224. #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
  225. extern struct tcpip_protocol tcp_protocol;
  226. #endif /* _GPXE_TCP_H */