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