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