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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #ifndef _IPXE_TCP_H
  2. #define _IPXE_TCP_H
  3. /** @file
  4. *
  5. * TCP protocol
  6. *
  7. * This file defines the iPXE TCP API.
  8. *
  9. */
  10. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  11. #include <ipxe/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 window scale option */
  47. struct tcp_window_scale_option {
  48. uint8_t kind;
  49. uint8_t length;
  50. uint8_t scale;
  51. } __attribute__ (( packed ));
  52. /** Padded TCP window scale option (used for sending) */
  53. struct tcp_window_scale_padded_option {
  54. uint8_t nop;
  55. struct tcp_window_scale_option wsopt;
  56. } __attribute (( packed ));
  57. /** Code for the TCP window scale option */
  58. #define TCP_OPTION_WS 3
  59. /** Advertised TCP window scale
  60. *
  61. * Using a scale factor of 2**9 provides for a maximum window of 32MB,
  62. * which is sufficient to allow Gigabit-speed transfers with a 200ms
  63. * RTT. The minimum advertised window is 512 bytes, which is still
  64. * less than a single packet.
  65. */
  66. #define TCP_RX_WINDOW_SCALE 9
  67. /** TCP timestamp option */
  68. struct tcp_timestamp_option {
  69. uint8_t kind;
  70. uint8_t length;
  71. uint32_t tsval;
  72. uint32_t tsecr;
  73. } __attribute__ (( packed ));
  74. /** Padded TCP timestamp option (used for sending) */
  75. struct tcp_timestamp_padded_option {
  76. uint8_t nop[2];
  77. struct tcp_timestamp_option tsopt;
  78. } __attribute__ (( packed ));
  79. /** Code for the TCP timestamp option */
  80. #define TCP_OPTION_TS 8
  81. /** Parsed TCP options */
  82. struct tcp_options {
  83. /** MSS option, if present */
  84. const struct tcp_mss_option *mssopt;
  85. /** Window scale option, if present */
  86. const struct tcp_window_scale_option *wsopt;
  87. /** Timestamp option, if present */
  88. const struct tcp_timestamp_option *tsopt;
  89. };
  90. /** @} */
  91. /*
  92. * TCP flags
  93. */
  94. #define TCP_CWR 0x80
  95. #define TCP_ECE 0x40
  96. #define TCP_URG 0x20
  97. #define TCP_ACK 0x10
  98. #define TCP_PSH 0x08
  99. #define TCP_RST 0x04
  100. #define TCP_SYN 0x02
  101. #define TCP_FIN 0x01
  102. /**
  103. * @defgroup tcpstates TCP states
  104. *
  105. * The TCP state is defined by a combination of the flags that have
  106. * been sent to the peer, the flags that have been acknowledged by the
  107. * peer, and the flags that have been received from the peer.
  108. *
  109. * @{
  110. */
  111. /** TCP flags that have been sent in outgoing packets */
  112. #define TCP_STATE_SENT(flags) ( (flags) << 0 )
  113. #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
  114. /** TCP flags that have been acknowledged by the peer
  115. *
  116. * Note that this applies only to SYN and FIN.
  117. */
  118. #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
  119. #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
  120. /** TCP flags that have been received from the peer
  121. *
  122. * Note that this applies only to SYN and FIN, and that once SYN has
  123. * been received, we should always be sending ACK.
  124. */
  125. #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
  126. #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
  127. /** TCP flags that are currently being sent in outgoing packets */
  128. #define TCP_FLAGS_SENDING(state) \
  129. ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
  130. /** CLOSED
  131. *
  132. * The connection has not yet been used for anything.
  133. */
  134. #define TCP_CLOSED TCP_RST
  135. /** LISTEN
  136. *
  137. * Not currently used as a state; we have no support for listening
  138. * connections. Given a unique value to avoid compiler warnings.
  139. */
  140. #define TCP_LISTEN 0
  141. /** SYN_SENT
  142. *
  143. * SYN has been sent, nothing has yet been received or acknowledged.
  144. */
  145. #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
  146. /** SYN_RCVD
  147. *
  148. * SYN has been sent but not acknowledged, SYN has been received.
  149. */
  150. #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  151. TCP_STATE_RCVD ( TCP_SYN ) )
  152. /** ESTABLISHED
  153. *
  154. * SYN has been sent and acknowledged, SYN has been received.
  155. */
  156. #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  157. TCP_STATE_ACKED ( TCP_SYN ) | \
  158. TCP_STATE_RCVD ( TCP_SYN ) )
  159. /** FIN_WAIT_1
  160. *
  161. * SYN has been sent and acknowledged, SYN has been received, FIN has
  162. * been sent but not acknowledged, FIN has not been received.
  163. *
  164. * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
  165. * acknowledged, i.e. if the application closes the connection after
  166. * sending and receiving SYN, but before having had SYN acknowledged.
  167. * However, we have to *pretend* that SYN has been acknowledged
  168. * anyway, otherwise we end up sending SYN and FIN in the same
  169. * sequence number slot. Therefore, when we transition from SYN_RCVD
  170. * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
  171. * and increment our sequence number.
  172. */
  173. #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  174. TCP_STATE_ACKED ( TCP_SYN ) | \
  175. TCP_STATE_RCVD ( TCP_SYN ) )
  176. /** FIN_WAIT_2
  177. *
  178. * SYN has been sent and acknowledged, SYN has been received, FIN has
  179. * been sent and acknowledged, FIN ha not been received.
  180. */
  181. #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  182. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  183. TCP_STATE_RCVD ( TCP_SYN ) )
  184. /** CLOSING / LAST_ACK
  185. *
  186. * SYN has been sent and acknowledged, SYN has been received, FIN has
  187. * been sent but not acknowledged, FIN has been received.
  188. *
  189. * This state actually encompasses both CLOSING and LAST_ACK; they are
  190. * identical with the definition of state that we use. I don't
  191. * *believe* that they need to be distinguished.
  192. */
  193. #define TCP_CLOSING_OR_LAST_ACK \
  194. ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  195. TCP_STATE_ACKED ( TCP_SYN ) | \
  196. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  197. /** TIME_WAIT
  198. *
  199. * SYN has been sent and acknowledged, SYN has been received, FIN has
  200. * been sent and acknowledged, FIN has been received.
  201. */
  202. #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  203. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  204. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  205. /** CLOSE_WAIT
  206. *
  207. * SYN has been sent and acknowledged, SYN has been received, FIN has
  208. * been received.
  209. */
  210. #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  211. TCP_STATE_ACKED ( TCP_SYN ) | \
  212. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  213. /** Can send data in current state
  214. *
  215. * We can send data if and only if we have had our SYN acked and we
  216. * have not yet sent our FIN.
  217. */
  218. #define TCP_CAN_SEND_DATA(state) \
  219. ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
  220. TCP_STATE_SENT ( TCP_FIN ) ) ) \
  221. == TCP_STATE_ACKED ( TCP_SYN ) )
  222. /** Have ever been fully established
  223. *
  224. * We have been fully established if we have both received a SYN and
  225. * had our own SYN acked.
  226. */
  227. #define TCP_HAS_BEEN_ESTABLISHED(state) \
  228. ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
  229. TCP_STATE_RCVD ( TCP_SYN ) ) ) \
  230. == ( TCP_STATE_ACKED ( TCP_SYN ) | TCP_STATE_RCVD ( TCP_SYN ) ) )
  231. /** Have closed gracefully
  232. *
  233. * We have closed gracefully if we have both received a FIN and had
  234. * our own FIN acked.
  235. */
  236. #define TCP_CLOSED_GRACEFULLY(state) \
  237. ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
  238. TCP_STATE_RCVD ( TCP_FIN ) ) ) \
  239. == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
  240. /** @} */
  241. /** Mask for TCP header length field */
  242. #define TCP_MASK_HLEN 0xf0
  243. /** Smallest port number on which a TCP connection can listen */
  244. #define TCP_MIN_PORT 1
  245. /**
  246. * Maxmimum advertised TCP window size
  247. *
  248. * The maximum bandwidth on any link is limited by
  249. *
  250. * max_bandwidth * round_trip_time = tcp_window
  251. *
  252. * Some rough expectations for achievable bandwidths over various
  253. * links are:
  254. *
  255. * a) Gigabit LAN: expected bandwidth 125MB/s, typical RTT 0.5ms,
  256. * minimum required window 64kB
  257. *
  258. * b) Home Internet connection: expected bandwidth 10MB/s, typical
  259. * RTT 25ms, minimum required window 256kB
  260. *
  261. * c) WAN: expected bandwidth 2MB/s, typical RTT 100ms, minimum
  262. * required window 200kB.
  263. *
  264. * The maximum possible value for the TCP window size is 1GB (using
  265. * the maximum window scale of 2**14). However, it is advisable to
  266. * keep the window size as small as possible (without limiting
  267. * bandwidth), since in the event of a lost packet the window size
  268. * represents the maximum amount that will need to be retransmitted.
  269. *
  270. * We therefore choose a maximum window size of 256kB.
  271. */
  272. #define TCP_MAX_WINDOW_SIZE ( 256 * 1024 )
  273. /**
  274. * Path MTU
  275. *
  276. * IPv6 requires all data link layers to support a datagram size of
  277. * 1280 bytes. We choose to use this as our maximum transmitted
  278. * datagram size, on the assumption that any practical link layer we
  279. * encounter will allow this size. This is a very conservative
  280. * assumption in practice, but the impact of making such a
  281. * conservative assumption is insignificant since the amount of data
  282. * that we transmit (rather than receive) is negligible.
  283. *
  284. * We allow space within this 1280 bytes for an IPv6 header, a TCP
  285. * header, and a (padded) TCP timestamp option.
  286. */
  287. #define TCP_PATH_MTU \
  288. ( 1280 - 40 /* IPv6 */ - 20 /* TCP */ - 12 /* TCP timestamp */ )
  289. /** TCP maximum segment lifetime
  290. *
  291. * Currently set to 2 minutes, as per RFC 793.
  292. */
  293. #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
  294. /**
  295. * TCP maximum header length
  296. *
  297. */
  298. #define TCP_MAX_HEADER_LEN \
  299. ( MAX_LL_NET_HEADER_LEN + \
  300. sizeof ( struct tcp_header ) + \
  301. sizeof ( struct tcp_mss_option ) + \
  302. sizeof ( struct tcp_window_scale_padded_option ) + \
  303. sizeof ( struct tcp_timestamp_padded_option ) )
  304. /**
  305. * Compare TCP sequence numbers
  306. *
  307. * @v seq1 Sequence number 1
  308. * @v seq2 Sequence number 2
  309. * @ret diff Sequence difference
  310. *
  311. * Analogous to memcmp(), returns an integer less than, equal to, or
  312. * greater than zero if @c seq1 is found, respectively, to be before,
  313. * equal to, or after @c seq2.
  314. */
  315. static inline __attribute__ (( always_inline )) int32_t
  316. tcp_cmp ( uint32_t seq1, uint32_t seq2 ) {
  317. return ( ( int32_t ) ( seq1 - seq2 ) );
  318. }
  319. /**
  320. * Check if TCP sequence number lies within window
  321. *
  322. * @v seq Sequence number
  323. * @v start Start of window
  324. * @v len Length of window
  325. * @ret in_window Sequence number is within window
  326. */
  327. static inline int tcp_in_window ( uint32_t seq, uint32_t start,
  328. uint32_t len ) {
  329. return ( ( seq - start ) < len );
  330. }
  331. extern struct tcpip_protocol tcp_protocol __tcpip_protocol;
  332. #endif /* _IPXE_TCP_H */