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.

tcp.h 9.0KB

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