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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 flags
  28. */
  29. #define TCP_CWR 0x80
  30. #define TCP_ECE 0x40
  31. #define TCP_URG 0x20
  32. #define TCP_ACK 0x10
  33. #define TCP_PSH 0x08
  34. #define TCP_RST 0x04
  35. #define TCP_SYN 0x02
  36. #define TCP_FIN 0x01
  37. /**
  38. * @defgroup tcpstates TCP states
  39. *
  40. * The TCP state is defined by a combination of the flags that have
  41. * been sent to the peer, the flags that have been acknowledged by the
  42. * peer, and the flags that have been received from the peer.
  43. *
  44. * @{
  45. */
  46. /** TCP flags that have been sent in outgoing packets */
  47. #define TCP_STATE_SENT(flags) ( (flags) << 0 )
  48. #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
  49. /** TCP flags that have been acknowledged by the peer
  50. *
  51. * Note that this applies only to SYN and FIN.
  52. */
  53. #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
  54. #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
  55. /** TCP flags that have been received from the peer
  56. *
  57. * Note that this applies only to SYN and FIN, and that once SYN has
  58. * been received, we should always be sending ACK.
  59. */
  60. #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
  61. #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
  62. /** TCP flags that are currently being sent in outgoing packets */
  63. #define TCP_FLAGS_SENDING(state) \
  64. ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
  65. /** CLOSED
  66. *
  67. * The connection has not yet been used for anything.
  68. */
  69. #define TCP_CLOSED TCP_RST
  70. /** LISTEN
  71. *
  72. * Not currently used as a state; we have no support for listening
  73. * connections. Given a unique value to avoid compiler warnings.
  74. */
  75. #define TCP_LISTEN 0
  76. /** SYN_SENT
  77. *
  78. * SYN has been sent, nothing has yet been received or acknowledged.
  79. */
  80. #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
  81. /** SYN_RCVD
  82. *
  83. * SYN has been sent but not acknowledged, SYN has been received.
  84. */
  85. #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  86. TCP_STATE_RCVD ( TCP_SYN ) )
  87. /** ESTABLISHED
  88. *
  89. * SYN has been sent and acknowledged, SYN has been received.
  90. */
  91. #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  92. TCP_STATE_ACKED ( TCP_SYN ) | \
  93. TCP_STATE_RCVD ( TCP_SYN ) )
  94. /** FIN_WAIT_1
  95. *
  96. * SYN has been sent and acknowledged, SYN has been received, FIN has
  97. * been sent but not acknowledged, FIN has not been received.
  98. *
  99. * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
  100. * acknowledged, i.e. if the application closes the connection after
  101. * sending and receiving SYN, but before having had SYN acknowledged.
  102. * However, we have to *pretend* that SYN has been acknowledged
  103. * anyway, otherwise we end up sending SYN and FIN in the same
  104. * sequence number slot. Therefore, when we transition from SYN_RCVD
  105. * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
  106. * and increment our sequence number.
  107. */
  108. #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  109. TCP_STATE_ACKED ( TCP_SYN ) | \
  110. TCP_STATE_RCVD ( TCP_SYN ) )
  111. /** FIN_WAIT_2
  112. *
  113. * SYN has been sent and acknowledged, SYN has been received, FIN has
  114. * been sent and acknowledged, FIN ha not been received.
  115. */
  116. #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  117. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  118. TCP_STATE_RCVD ( TCP_SYN ) )
  119. /** CLOSING / LAST_ACK
  120. *
  121. * SYN has been sent and acknowledged, SYN has been received, FIN has
  122. * been sent but not acknowledged, FIN has been received.
  123. *
  124. * This state actually encompasses both CLOSING and LAST_ACK; they are
  125. * identical with the definition of state that we use. I don't
  126. * *believe* that they need to be distinguished.
  127. */
  128. #define TCP_CLOSING_OR_LAST_ACK \
  129. ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  130. TCP_STATE_ACKED ( TCP_SYN ) | \
  131. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  132. /** TIME_WAIT
  133. *
  134. * SYN has been sent and acknowledged, SYN has been received, FIN has
  135. * been sent and acknowledged, FIN has been received.
  136. */
  137. #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
  138. TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
  139. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  140. /** CLOSE_WAIT
  141. *
  142. * SYN has been sent and acknowledged, SYN has been received, FIN has
  143. * been received.
  144. */
  145. #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
  146. TCP_STATE_ACKED ( TCP_SYN ) | \
  147. TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
  148. /** Can send data in current state
  149. *
  150. * We can send data if and only if we have had our SYN acked and we
  151. * have not yet sent our FIN.
  152. */
  153. #define TCP_CAN_SEND_DATA(state) \
  154. ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
  155. TCP_STATE_SENT ( TCP_FIN ) ) ) \
  156. == TCP_STATE_ACKED ( TCP_SYN ) )
  157. /** Have closed gracefully
  158. *
  159. * We have closed gracefully if we have both received a FIN and had
  160. * our own FIN acked.
  161. */
  162. #define TCP_CLOSED_GRACEFULLY(state) \
  163. ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
  164. TCP_STATE_RCVD ( TCP_FIN ) ) ) \
  165. == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
  166. /** @} */
  167. /** Mask for TCP header length field */
  168. #define TCP_MASK_HLEN 0xf0
  169. /** Smallest port number on which a TCP connection can listen */
  170. #define TCP_MIN_PORT 1
  171. /* Some PKB constants */
  172. #define MAX_HDR_LEN 100
  173. #define MAX_PKB_LEN 1500
  174. #define MIN_PKB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
  175. /**
  176. * Advertised TCP window size
  177. *
  178. * Our TCP window is actually limited by the amount of space available
  179. * for RX packets in the NIC's RX ring; we tend to populate the rings
  180. * with far fewer descriptors than a typical driver. Since we have no
  181. * way of knowing how much of this RX ring space will be available for
  182. * received TCP packets (consider, for example, that they may all be
  183. * consumed by a series of unrelated ARP requests between other
  184. * machines on the network), it is actually not even theoretically
  185. * possible for us to specify an accurate window size. We therefore
  186. * guess an arbitrary number that is empirically as large as possible
  187. * while avoiding retransmissions due to dropped packets.
  188. */
  189. #define TCP_WINDOW_SIZE 2048
  190. /** TCP maximum segment lifetime
  191. *
  192. * Currently set to 2 minutes, as per RFC 793.
  193. */
  194. #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
  195. struct tcp_application;
  196. /**
  197. * TCP operations
  198. *
  199. */
  200. struct tcp_operations {
  201. /*
  202. * Connection closed
  203. *
  204. * @v app TCP application
  205. * @v status Error code, if any
  206. *
  207. * This is called when the connection is closed for any
  208. * reason, including timeouts or aborts. The status code
  209. * contains the negative error number, if the closure is due
  210. * to an error.
  211. *
  212. * When closed() is called, the application no longer has a
  213. * valid TCP connection. Note that connected() may not have
  214. * been called before closed(), if the close is due to an
  215. * error during connection setup.
  216. */
  217. void ( * closed ) ( struct tcp_application *app, int status );
  218. /**
  219. * Connection established
  220. *
  221. * @v app TCP application
  222. */
  223. void ( * connected ) ( struct tcp_application *app );
  224. /**
  225. * Data acknowledged
  226. *
  227. * @v app TCP application
  228. * @v len Length of acknowledged data
  229. *
  230. * @c len is guaranteed to not exceed the outstanding amount
  231. * of unacknowledged data.
  232. */
  233. void ( * acked ) ( struct tcp_application *app, size_t len );
  234. /**
  235. * New data received
  236. *
  237. * @v app TCP application
  238. * @v data Data
  239. * @v len Length of data
  240. */
  241. void ( * newdata ) ( struct tcp_application *app,
  242. void *data, size_t len );
  243. /**
  244. * Transmit data
  245. *
  246. * @v app TCP application
  247. * @v buf Temporary data buffer
  248. * @v len Length of temporary data buffer
  249. *
  250. * The application should transmit whatever it currently wants
  251. * to send using tcp_send(). If retransmissions are required,
  252. * senddata() will be called again and the application must
  253. * regenerate the data. The easiest way to implement this is
  254. * to ensure that senddata() never changes the application's
  255. * state.
  256. *
  257. * The application may use the temporary data buffer to
  258. * construct the data to be sent. Note that merely filling
  259. * the buffer will do nothing; the application must call
  260. * tcp_send() in order to actually transmit the data. Use of
  261. * the buffer is not compulsory; the application may call
  262. * tcp_send() on any block of data.
  263. */
  264. void ( * senddata ) ( struct tcp_application *app, void *buf,
  265. size_t len );
  266. };
  267. struct tcp_connection;
  268. /**
  269. * A TCP application
  270. *
  271. * This data structure represents an application with a TCP connection.
  272. */
  273. struct tcp_application {
  274. /** TCP connection data
  275. *
  276. * This is filled in by TCP calls that initiate a connection,
  277. * and reset to NULL when the connection is closed.
  278. */
  279. struct tcp_connection *conn;
  280. /** TCP connection operations table */
  281. struct tcp_operations *tcp_op;
  282. };
  283. extern int tcp_connect ( struct tcp_application *app,
  284. struct sockaddr_tcpip *peer,
  285. uint16_t local_port );
  286. extern void tcp_close ( struct tcp_application *app );
  287. extern int tcp_senddata ( struct tcp_application *app );
  288. extern int tcp_send ( struct tcp_application *app, const void *data,
  289. size_t len );
  290. extern struct tcpip_protocol tcp_protocol;
  291. #endif /* _GPXE_TCP_H */