TcpProto.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /** @file
  2. TCP protocol header file.
  3. Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _TCP_PROTO_H_
  7. #define _TCP_PROTO_H_
  8. ///
  9. /// Tcp states don't change their order. It is used as an
  10. /// index to mTcpOutFlag and other macros.
  11. ///
  12. #define TCP_CLOSED 0
  13. #define TCP_LISTEN 1
  14. #define TCP_SYN_SENT 2
  15. #define TCP_SYN_RCVD 3
  16. #define TCP_ESTABLISHED 4
  17. #define TCP_FIN_WAIT_1 5
  18. #define TCP_FIN_WAIT_2 6
  19. #define TCP_CLOSING 7
  20. #define TCP_TIME_WAIT 8
  21. #define TCP_CLOSE_WAIT 9
  22. #define TCP_LAST_ACK 10
  23. ///
  24. /// Flags in the TCP header
  25. ///
  26. #define TCP_FLG_FIN 0x01
  27. #define TCP_FLG_SYN 0x02
  28. #define TCP_FLG_RST 0x04
  29. #define TCP_FLG_PSH 0x08
  30. #define TCP_FLG_ACK 0x10
  31. #define TCP_FLG_URG 0x20
  32. //
  33. // mask for all the flags
  34. //
  35. #define TCP_FLG_FLAG 0x3F
  36. #define TCP_CONNECT_REFUSED (-1) ///< TCP error status
  37. #define TCP_CONNECT_RESET (-2) ///< TCP error status
  38. #define TCP_CONNECT_CLOSED (-3) ///< TCP error status
  39. //
  40. // Current congestion status as suggested by RFC3782.
  41. //
  42. #define TCP_CONGEST_RECOVER 1 ///< During the NewReno fast recovery.
  43. #define TCP_CONGEST_LOSS 2 ///< Retxmit because of retxmit time out.
  44. #define TCP_CONGEST_OPEN 3 ///< TCP is opening its congestion window.
  45. //
  46. // TCP control flags
  47. //
  48. #define TCP_CTRL_NO_NAGLE 0x0001 ///< Disable Nagle algorithm
  49. #define TCP_CTRL_NO_KEEPALIVE 0x0002 ///< Disable keepalive timer.
  50. #define TCP_CTRL_NO_WS 0x0004 ///< Disable window scale option.
  51. #define TCP_CTRL_RCVD_WS 0x0008 ///< Received a wnd scale option in syn.
  52. #define TCP_CTRL_NO_TS 0x0010 ///< Disable Timestamp option.
  53. #define TCP_CTRL_RCVD_TS 0x0020 ///< Received a Timestamp option in syn.
  54. #define TCP_CTRL_SND_TS 0x0040 ///< Send Timestamp option to remote.
  55. #define TCP_CTRL_SND_URG 0x0080 ///< In urgent send mode.
  56. #define TCP_CTRL_RCVD_URG 0x0100 ///< In urgent receive mode.
  57. #define TCP_CTRL_SND_PSH 0x0200 ///< In PUSH send mode.
  58. #define TCP_CTRL_FIN_SENT 0x0400 ///< FIN is sent.
  59. #define TCP_CTRL_FIN_ACKED 0x0800 ///< FIN is ACKed.
  60. #define TCP_CTRL_TIMER_ON 0x1000 ///< At least one of the timer is on.
  61. #define TCP_CTRL_RTT_ON 0x2000 ///< The RTT measurement is on.
  62. #define TCP_CTRL_ACK_NOW 0x4000 ///< Send the ACK now, don't delay.
  63. //
  64. // Timer related values
  65. //
  66. #define TCP_TIMER_CONNECT 0 ///< Connection establishment timer.
  67. #define TCP_TIMER_REXMIT 1 ///< Retransmit timer.
  68. #define TCP_TIMER_PROBE 2 ///< Window probe timer.
  69. #define TCP_TIMER_KEEPALIVE 3 ///< Keepalive timer.
  70. #define TCP_TIMER_FINWAIT2 4 ///< FIN_WAIT_2 timer.
  71. #define TCP_TIMER_2MSL 5 ///< TIME_WAIT timer.
  72. #define TCP_TIMER_NUMBER 6 ///< The total number of the TCP timer.
  73. #define TCP_TICK 200 ///< Every TCP tick is 200ms.
  74. #define TCP_TICK_HZ 5 ///< The frequence of TCP tick.
  75. #define TCP_RTT_SHIFT 3 ///< SRTT & RTTVAR scaled by 8.
  76. #define TCP_RTO_MIN TCP_TICK_HZ ///< The minimum value of RTO.
  77. #define TCP_RTO_MAX (TCP_TICK_HZ * 60) ///< The maximum value of RTO.
  78. #define TCP_FOLD_RTT 4 ///< Timeout threshold to fold RTT.
  79. //
  80. // Default values for some timers
  81. //
  82. #define TCP_MAX_LOSS 12 ///< Default max times to retxmit.
  83. #define TCP_KEEPALIVE_IDLE_MIN (TCP_TICK_HZ * 60 * 60 * 2) ///< First keepalive.
  84. #define TCP_KEEPALIVE_PERIOD (TCP_TICK_HZ * 60)
  85. #define TCP_MAX_KEEPALIVE 8
  86. #define TCP_FIN_WAIT2_TIME (2 * TCP_TICK_HZ)
  87. #define TCP_TIME_WAIT_TIME (2 * TCP_TICK_HZ)
  88. #define TCP_PAWS_24DAY (24 * 24 * 60 * 60 * TCP_TICK_HZ)
  89. #define TCP_CONNECT_TIME (75 * TCP_TICK_HZ)
  90. //
  91. // The header space to be reserved before TCP data to accommodate:
  92. // 60byte IP head + 60byte TCP head + link layer head
  93. //
  94. #define TCP_MAX_HEAD 192
  95. //
  96. // Value ranges for some control option
  97. //
  98. #define TCP_RCV_BUF_SIZE (2 * 1024 * 1024)
  99. #define TCP_RCV_BUF_SIZE_MIN (8 * 1024)
  100. #define TCP_SND_BUF_SIZE (2 * 1024 * 1024)
  101. #define TCP_SND_BUF_SIZE_MIN (8 * 1024)
  102. #define TCP_BACKLOG 10
  103. #define TCP_BACKLOG_MIN 5
  104. #define TCP_MAX_LOSS_MIN 6
  105. #define TCP_CONNECT_TIME_MIN (60 * TCP_TICK_HZ)
  106. #define TCP_MAX_KEEPALIVE_MIN 4
  107. #define TCP_KEEPALIVE_IDLE_MAX (TCP_TICK_HZ * 60 * 60 * 4)
  108. #define TCP_KEEPALIVE_PERIOD_MIN (TCP_TICK_HZ * 30)
  109. #define TCP_FIN_WAIT2_TIME_MAX (4 * TCP_TICK_HZ)
  110. #define TCP_TIME_WAIT_TIME_MAX (60 * TCP_TICK_HZ)
  111. ///
  112. /// TCP_CONNECTED: both ends have synchronized their ISN.
  113. ///
  114. #define TCP_CONNECTED(state) ((state) > TCP_SYN_RCVD)
  115. #define TCP_FIN_RCVD(State) \
  116. ( \
  117. ((State) == TCP_CLOSE_WAIT) || \
  118. ((State) == TCP_LAST_ACK) || \
  119. ((State) == TCP_CLOSING) || \
  120. ((State) == TCP_TIME_WAIT) \
  121. )
  122. #define TCP_LOCAL_CLOSED(State) \
  123. ( \
  124. ((State) == TCP_FIN_WAIT_1) || \
  125. ((State) == TCP_FIN_WAIT_2) || \
  126. ((State) == TCP_CLOSING) || \
  127. ((State) == TCP_TIME_WAIT) || \
  128. ((State) == TCP_LAST_ACK) \
  129. )
  130. //
  131. // Get the TCP_SEG point from a net buffer's ProtoData.
  132. //
  133. #define TCPSEG_NETBUF(NBuf) ((TCP_SEG *) ((NBuf)->ProtoData))
  134. //
  135. // Macros to compare sequence no
  136. //
  137. #define TCP_SEQ_LT(SeqA, SeqB) ((INT32) ((SeqA) - (SeqB)) < 0)
  138. #define TCP_SEQ_LEQ(SeqA, SeqB) ((INT32) ((SeqA) - (SeqB)) <= 0)
  139. #define TCP_SEQ_GT(SeqA, SeqB) ((INT32) ((SeqB) - (SeqA)) < 0)
  140. #define TCP_SEQ_GEQ(SeqA, SeqB) ((INT32) ((SeqB) - (SeqA)) <= 0)
  141. //
  142. // TCP_SEQ_BETWEEN return whether b <= m <= e
  143. //
  144. #define TCP_SEQ_BETWEEN(b, m, e) ((e) - (b) >= (m) - (b))
  145. //
  146. // TCP_SUB_SEQ returns Seq1 - Seq2. Make sure Seq1 >= Seq2
  147. //
  148. #define TCP_SUB_SEQ(Seq1, Seq2) ((UINT32) ((Seq1) - (Seq2)))
  149. //
  150. // Check whether Flag is on
  151. //
  152. #define TCP_FLG_ON(Value, Flag) ((BOOLEAN) (((Value) & (Flag)) != 0))
  153. //
  154. // Set and Clear operation on a Flag
  155. //
  156. #define TCP_SET_FLG(Value, Flag) ((Value) |= (Flag))
  157. #define TCP_CLEAR_FLG(Value, Flag) ((Value) &= ~(Flag))
  158. //
  159. // Test whether two peers are equal
  160. //
  161. #define TCP_PEER_EQUAL(Pa, Pb, Ver) \
  162. (((Pa)->Port == (Pb)->Port) && TcpIsIpEqual(&((Pa)->Ip), &((Pb)->Ip), Ver))
  163. //
  164. // Test whether Pa matches Pb, or Pa is more specific
  165. // than pb. Zero means wildcard.
  166. //
  167. #define TCP_PEER_MATCH(Pa, Pb, Ver) \
  168. ( \
  169. (((Pb)->Port == 0) || ((Pb)->Port == (Pa)->Port)) && \
  170. (TcpIsIpZero (&((Pb)->Ip), Ver) || TcpIsIpEqual (&((Pb)->Ip), &((Pa)->Ip), Ver)) \
  171. )
  172. #define TCP_TIMER_ON(Flag, Timer) ((Flag) & (1 << (Timer)))
  173. #define TCP_SET_TIMER(Flag, Timer) ((Flag) = (UINT16) ((Flag) | (1 << (Timer))))
  174. #define TCP_CLEAR_TIMER(Flag, Timer) ((Flag) = (UINT16) ((Flag) & (~(1 << (Timer)))))
  175. #define TCP_TIME_LT(Ta, Tb) ((INT32) ((Ta) - (Tb)) < 0)
  176. #define TCP_TIME_LEQ(Ta, Tb) ((INT32) ((Ta) - (Tb)) <= 0)
  177. #define TCP_SUB_TIME(Ta, Tb) ((UINT32) ((Ta) - (Tb)))
  178. #define TCP_MAX_WIN 0xFFFFU
  179. ///
  180. /// TCP segmentation data.
  181. ///
  182. typedef struct _TCP_SEG {
  183. TCP_SEQNO Seq; ///< Starting sequence number.
  184. TCP_SEQNO End; ///< The sequence of the last byte + 1, include SYN/FIN. End-Seq = SEG.LEN.
  185. TCP_SEQNO Ack; ///< ACK field in the segment.
  186. UINT8 Flag; ///< TCP header flags.
  187. UINT16 Urg; ///< Valid if URG flag is set.
  188. UINT32 Wnd; ///< TCP window size field.
  189. } TCP_SEG;
  190. ///
  191. /// Network endpoint, IP plus Port structure.
  192. ///
  193. typedef struct _TCP_PEER {
  194. EFI_IP_ADDRESS Ip; ///< IP address, in network byte order.
  195. TCP_PORTNO Port; ///< Port number, in network byte order.
  196. } TCP_PEER;
  197. typedef struct _TCP_CONTROL_BLOCK TCP_CB;
  198. ///
  199. /// TCP control block: it includes various states.
  200. ///
  201. struct _TCP_CONTROL_BLOCK {
  202. LIST_ENTRY List; ///< Back and forward link entry
  203. TCP_CB *Parent; ///< The parent TCP_CB structure
  204. SOCKET *Sk; ///< The socket it controlled.
  205. TCP_PEER LocalEnd; ///< Local endpoint.
  206. TCP_PEER RemoteEnd; ///< Remote endpoint.
  207. LIST_ENTRY SndQue; ///< Retxmission queue.
  208. LIST_ENTRY RcvQue; ///< Reassemble queue.
  209. UINT32 CtrlFlag; ///< Control flags, such as NO_NAGLE.
  210. INT32 Error; ///< Soft error status, such as TCP_CONNECT_RESET.
  211. //
  212. // RFC793 and RFC1122 defined variables
  213. //
  214. UINT8 State; ///< TCP state, such as SYN_SENT, LISTEN.
  215. UINT8 DelayedAck; ///< Number of delayed ACKs.
  216. UINT16 HeadSum; ///< Checksum of the fixed parts of pesudo
  217. ///< header: Src IP, Dst IP, 0, Protocol,
  218. ///< do not include the TCP length.
  219. TCP_SEQNO Iss; ///< Initial Sending Sequence.
  220. TCP_SEQNO SndUna; ///< First unacknowledged data.
  221. TCP_SEQNO SndNxt; ///< Next data sequence to send.
  222. TCP_SEQNO SndPsh; ///< Send PUSH point.
  223. TCP_SEQNO SndUp; ///< Send urgent point.
  224. UINT32 SndWnd; ///< Window advertised by the remote peer.
  225. UINT32 SndWndMax; ///< Max send window advertised by the peer.
  226. TCP_SEQNO SndWl1; ///< Seq number used for last window update.
  227. TCP_SEQNO SndWl2; ///< Ack no of last window update.
  228. UINT16 SndMss; ///< Max send segment size.
  229. TCP_SEQNO RcvNxt; ///< Next sequence no to receive.
  230. UINT32 RcvWnd; ///< Window advertised by the local peer.
  231. TCP_SEQNO RcvWl2; ///< The RcvNxt (or ACK) of last window update.
  232. ///< It is necessary because of delayed ACK.
  233. TCP_SEQNO RcvUp; ///< Urgent point;
  234. TCP_SEQNO Irs; ///< Initial Receiving Sequence.
  235. UINT16 RcvMss; ///< Max receive segment size.
  236. UINT16 EnabledTimer; ///< Which timer is currently enabled.
  237. UINT32 Timer[TCP_TIMER_NUMBER]; ///< When the timer will expire.
  238. INT32 NextExpire; ///< Countdown offset for the nearest timer.
  239. UINT32 Idle; ///< How long the connection is in idle.
  240. UINT32 ProbeTime; ///< The time out value for current window prober.
  241. BOOLEAN ProbeTimerOn; ///< If TRUE, the probe time is on.
  242. //
  243. // RFC1323 defined variables, about window scale,
  244. // timestamp and PAWS
  245. //
  246. UINT8 SndWndScale; ///< Wndscale received from the peer.
  247. UINT8 RcvWndScale; ///< Wndscale used to scale local buffer.
  248. UINT32 TsRecent; ///< TsRecent to echo to the remote peer.
  249. UINT32 TsRecentAge; ///< When this TsRecent is updated.
  250. //
  251. // RFC2988 defined variables. about RTT measurement
  252. //
  253. TCP_SEQNO RttSeq; ///< The seq of measured segment now.
  254. UINT32 RttMeasure; ///< Currently measured RTT in heartbeats.
  255. UINT32 SRtt; ///< Smoothed RTT, scaled by 8.
  256. UINT32 RttVar; ///< RTT variance, scaled by 8.
  257. UINT32 Rto; ///< Current RTO, not scaled.
  258. //
  259. // RFC2581, and 3782 variables.
  260. // Congestion control + NewReno fast recovery.
  261. //
  262. UINT32 CWnd; ///< Sender's congestion window.
  263. UINT32 Ssthresh; ///< Slow start threshold.
  264. TCP_SEQNO Recover; ///< Recover point for NewReno.
  265. UINT16 DupAck; ///< Number of duplicate ACKs.
  266. UINT8 CongestState; ///< The current congestion state(RFC3782).
  267. UINT8 LossTimes; ///< Number of retxmit timeouts in a row.
  268. TCP_SEQNO LossRecover; ///< Recover point for retxmit.
  269. //
  270. // RFC7323
  271. // Addressing Window Retraction for TCP Window Scale Option.
  272. //
  273. TCP_SEQNO RetxmitSeqMax; ///< Max Seq number in previous retransmission.
  274. //
  275. // configuration parameters, for EFI_TCP4_PROTOCOL specification
  276. //
  277. UINT32 KeepAliveIdle; ///< Idle time before sending first probe.
  278. UINT32 KeepAlivePeriod; ///< Interval for subsequent keep alive probe.
  279. UINT8 MaxKeepAlive; ///< Maximum keep alive probe times.
  280. UINT8 KeepAliveProbes; ///< The number of keep alive probe.
  281. UINT16 MaxRexmit; ///< The maximum number of retxmit before abort.
  282. UINT32 FinWait2Timeout; ///< The FIN_WAIT_2 timeout.
  283. UINT32 TimeWaitTimeout; ///< The TIME_WAIT timeout.
  284. UINT32 ConnectTimeout; ///< The connect establishment timeout.
  285. //
  286. // configuration for tcp provided by user
  287. //
  288. BOOLEAN UseDefaultAddr;
  289. UINT8 Tos;
  290. UINT8 Ttl;
  291. EFI_IPv4_ADDRESS SubnetMask;
  292. BOOLEAN RemoteIpZero; ///< RemoteEnd.Ip is ZERO when configured.
  293. IP_IO_IP_INFO *IpInfo; ///< Pointer reference to Ip used to send pkt
  294. UINT32 Tick; ///< 1 tick = 200ms
  295. };
  296. #endif