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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /**
  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. /**
  27. * TCP MSS option
  28. */
  29. struct tcp_mss_option {
  30. uint8_t kind;
  31. uint8_t length;
  32. uint16_t mss;
  33. };
  34. /** Code for the TCP MSS option */
  35. #define TCP_OPTION_MSS 2
  36. /*
  37. * TCP flags
  38. */
  39. #define TCP_CWR 0x80
  40. #define TCP_ECE 0x40
  41. #define TCP_URG 0x20
  42. #define TCP_ACK 0x10
  43. #define TCP_PSH 0x08
  44. #define TCP_RST 0x04
  45. #define TCP_SYN 0x02
  46. #define TCP_FIN 0x01
  47. /**
  48. * @defgroup tcpstates TCP states
  49. *
  50. * The TCP state is defined by a combination of the flags that have
  51. * been sent to the peer, the flags that have been acknowledged by the
  52. * peer, and the flags that have been received from the peer.
  53. *
  54. * @{
  55. */
  56. /** TCP flags that have been sent in outgoing packets */
  57. #define TCP_STATE_SENT(flags) ( (flags) << 0 )
  58. #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
  59. /** TCP flags that have been acknowledged by the peer
  60. *
  61. * Note that this applies only to SYN and FIN.
  62. */
  63. #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
  64. #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
  65. /** TCP flags that have been received from the peer
  66. *
  67. * Note that this applies only to SYN and FIN, and that once SYN has
  68. * been received, we should always be sending ACK.
  69. */
  70. #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
  71. #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
  72. /** TCP flags that are currently being sent in outgoing packets */
  73. #define TCP_FLAGS_SENDING(state) \
  74. ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
  75. /** CLOSED
  76. *
  77. * The connection has not yet been used for anything.
  78. */
  79. #define TCP_CLOSED TCP_RST
  80. /** LISTEN
  81. *
  82. * Not currently used as a state; we have no support for listening
  83. * connections. Given a unique value to avoid compiler warnings.
  84. */
  85. #define TCP_LISTEN 0
  86. /** SYN_SENT
  87. *
  88. * SYN has been sent, nothing has yet been received or acknowledged.
  89. */
  90. #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
  91. /** SYN_RCVD
  92. *
  93. * SYN has been sent but not acknowledged, SYN has been received.
  94. */
  95. #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  96. TCP_STATE_RCVD ( TCP_SYN ) )
  97. /** ESTABLISHED
  98. *
  99. * SYN has been sent and acknowledged, SYN has been received.
  100. */
  101. #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  102. TCP_STATE_ACKED ( TCP_SYN ) | \
  103. TCP_STATE_RCVD ( TCP_SYN ) )
  104. /** FIN_WAIT_1
  105. *
  106. * SYN has been sent and acknowledged, SYN has been received, FIN has
  107. * been sent but not acknowledged, FIN has not been received.
  108. *
  109. * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
  110. * acknowledged, i.e. if the application closes the connection after
  111. * sending and receiving SYN, but before having had SYN acknowledged.
  112. * However, we have to *pretend* that SYN has been acknowledged
  113. * anyway, otherwise we end up sending SYN and FIN in the same
  114. * sequence number slot. Therefore, when we transition from SYN_RCVD
  115. * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
  116. * and increment our sequence number.
  117. */
  118. #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  119. TCP_STATE_ACKED ( TCP_SYN ) | \
  120. TCP_STATE_RCVD ( TCP_SYN ) )
  121. /** FIN_WAIT_2
  122. *
  123. * SYN has been sent and acknowledged, SYN has been received, FIN has
  124. * been sent and acknowledged, FIN ha not been received.
  125. */
  126. #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  127. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  128. TCP_STATE_RCVD ( TCP_SYN ) )
  129. /** CLOSING / LAST_ACK
  130. *
  131. * SYN has been sent and acknowledged, SYN has been received, FIN has
  132. * been sent but not acknowledged, FIN has been received.
  133. *
  134. * This state actually encompasses both CLOSING and LAST_ACK; they are
  135. * identical with the definition of state that we use. I don't
  136. * *believe* that they need to be distinguished.
  137. */
  138. #define TCP_CLOSING_OR_LAST_ACK \
  139. ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  140. TCP_STATE_ACKED ( TCP_SYN ) | \
  141. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  142. /** TIME_WAIT
  143. *
  144. * SYN has been sent and acknowledged, SYN has been received, FIN has
  145. * been sent and acknowledged, FIN has been received.
  146. */
  147. #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  148. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  149. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  150. /** CLOSE_WAIT
  151. *
  152. * SYN has been sent and acknowledged, SYN has been received, FIN has
  153. * been received.
  154. */
  155. #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  156. TCP_STATE_ACKED ( TCP_SYN ) | \
  157. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  158. /** Can send data in current state
  159. *
  160. * We can send data if and only if we have had our SYN acked and we
  161. * have not yet sent our FIN.
  162. */
  163. #define TCP_CAN_SEND_DATA(state) \
  164. ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
  165. TCP_STATE_SENT ( TCP_FIN ) ) ) \
  166. == TCP_STATE_ACKED ( TCP_SYN ) )
  167. /** Have closed gracefully
  168. *
  169. * We have closed gracefully if we have both received a FIN and had
  170. * our own FIN acked.
  171. */
  172. #define TCP_CLOSED_GRACEFULLY(state) \
  173. ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
  174. TCP_STATE_RCVD ( TCP_FIN ) ) ) \
  175. == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
  176. /** @} */
  177. /** Mask for TCP header length field */
  178. #define TCP_MASK_HLEN 0xf0
  179. /** Smallest port number on which a TCP connection can listen */
  180. #define TCP_MIN_PORT 1
  181. /* Some PKB constants */
  182. #define MAX_HDR_LEN 100
  183. #define MAX_PKB_LEN 1500
  184. #define MIN_PKB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
  185. /**
  186. * Advertised TCP window size
  187. *
  188. *
  189. * Our TCP window is actually limited by the amount of space available
  190. * for RX packets in the NIC's RX ring; we tend to populate the rings
  191. * with far fewer descriptors than a typical driver. This would
  192. * result in a desperately small window size, which kills WAN download
  193. * performance; the maximum bandwidth on any link is limited to
  194. *
  195. * max_bandwidth = ( tcp_window / round_trip_time )
  196. *
  197. * With a 4kB window, which probably accurately reflects our amount of
  198. * buffer space, and a WAN RTT of say 200ms, this gives a maximum
  199. * achievable bandwidth of 20kB/s, which is not acceptable.
  200. *
  201. * We therefore aim to process packets as fast as they arrive, and
  202. * advertise an "infinite" window. If we don't process packets as
  203. * fast as they arrive, then we will drop packets and have to incur
  204. * the retransmission penalty.
  205. *
  206. * Since we don't store out-of-order received packets, the
  207. * retransmission penalty is that the whole window contents must be
  208. * resent.
  209. *
  210. * We choose to compromise on a window size of 64kB (which is the
  211. * maximum that can be represented without using TCP options). This
  212. * gives a maximum bandwidth of 320kB/s at 200ms RTT, which is
  213. * probably faster than the actual link bandwidth. It also limits
  214. * retransmissions to 64kB, which is reasonable.
  215. *
  216. * Finally, since the window goes into a 16-bit field and we cannot
  217. * actually use 65536, we use a window size of (65536-4) to ensure
  218. * that payloads remain dword-aligned.
  219. */
  220. #define TCP_WINDOW_SIZE ( 65536 - 4 )
  221. /**
  222. * Advertised TCP MSS
  223. *
  224. * We currently hardcode this to a reasonable value and hope that the
  225. * sender uses path MTU discovery. The alternative is breaking the
  226. * abstraction layer so that we can find out the MTU from the IP layer
  227. * (which would have to find out from the net device layer).
  228. */
  229. #define TCP_MSS 1460
  230. /** TCP maximum segment lifetime
  231. *
  232. * Currently set to 2 minutes, as per RFC 793.
  233. */
  234. #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
  235. struct tcp_application;
  236. /**
  237. * TCP operations
  238. *
  239. */
  240. struct tcp_operations {
  241. /*
  242. * Connection closed
  243. *
  244. * @v app TCP application
  245. * @v status Error code, if any
  246. *
  247. * This is called when the connection is closed for any
  248. * reason, including timeouts or aborts. The status code
  249. * contains the negative error number, if the closure is due
  250. * to an error.
  251. *
  252. * When closed() is called, the application no longer has a
  253. * valid TCP connection. Note that connected() may not have
  254. * been called before closed(), if the close is due to an
  255. * error during connection setup.
  256. */
  257. void ( * closed ) ( struct tcp_application *app, int status );
  258. /**
  259. * Connection established
  260. *
  261. * @v app TCP application
  262. */
  263. void ( * connected ) ( struct tcp_application *app );
  264. /**
  265. * Data acknowledged
  266. *
  267. * @v app TCP application
  268. * @v len Length of acknowledged data
  269. *
  270. * @c len is guaranteed to not exceed the outstanding amount
  271. * of unacknowledged data.
  272. */
  273. void ( * acked ) ( struct tcp_application *app, size_t len );
  274. /**
  275. * New data received
  276. *
  277. * @v app TCP application
  278. * @v data Data
  279. * @v len Length of data
  280. */
  281. void ( * newdata ) ( struct tcp_application *app,
  282. void *data, size_t len );
  283. /**
  284. * Transmit data
  285. *
  286. * @v app TCP application
  287. * @v buf Temporary data buffer
  288. * @v len Length of temporary data buffer
  289. *
  290. * The application should transmit whatever it currently wants
  291. * to send using tcp_send(). If retransmissions are required,
  292. * senddata() will be called again and the application must
  293. * regenerate the data. The easiest way to implement this is
  294. * to ensure that senddata() never changes the application's
  295. * state.
  296. *
  297. * The application may use the temporary data buffer to
  298. * construct the data to be sent. Note that merely filling
  299. * the buffer will do nothing; the application must call
  300. * tcp_send() in order to actually transmit the data. Use of
  301. * the buffer is not compulsory; the application may call
  302. * tcp_send() on any block of data.
  303. */
  304. void ( * senddata ) ( struct tcp_application *app, void *buf,
  305. size_t len );
  306. };
  307. struct tcp_connection;
  308. /**
  309. * A TCP application
  310. *
  311. * This data structure represents an application with a TCP connection.
  312. */
  313. struct tcp_application {
  314. /** TCP connection data
  315. *
  316. * This is filled in by TCP calls that initiate a connection,
  317. * and reset to NULL when the connection is closed.
  318. */
  319. struct tcp_connection *conn;
  320. /** TCP connection operations table */
  321. struct tcp_operations *tcp_op;
  322. };
  323. extern int tcp_connect ( struct tcp_application *app,
  324. struct sockaddr_tcpip *peer,
  325. uint16_t local_port );
  326. extern void tcp_close ( struct tcp_application *app );
  327. extern int tcp_senddata ( struct tcp_application *app );
  328. extern int tcp_send ( struct tcp_application *app, const void *data,
  329. size_t len );
  330. extern struct tcpip_protocol tcp_protocol;
  331. #endif /* _GPXE_TCP_H */