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.

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