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