iPXE
Macros
TCP states

The TCP state is defined by a combination of the flags that have been sent to the peer, the flags that have been acknowledged by the peer, and the flags that have been received from the peer. More...

Macros

#define TCP_STATE_SENT(flags)   ( (flags) << 0 )
 TCP flags that have been sent in outgoing packets. More...
 
#define TCP_FLAGS_SENT(state)   ( ( (state) >> 0 ) & 0xff )
 
#define TCP_STATE_ACKED(flags)   ( (flags) << 8 )
 TCP flags that have been acknowledged by the peer. More...
 
#define TCP_FLAGS_ACKED(state)   ( ( (state) >> 8 ) & 0xff )
 
#define TCP_STATE_RCVD(flags)   ( (flags) << 16 )
 TCP flags that have been received from the peer. More...
 
#define TCP_FLAGS_RCVD(state)   ( ( (state) >> 16 ) & 0xff )
 
#define TCP_FLAGS_SENDING(state)   ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
 TCP flags that are currently being sent in outgoing packets. More...
 
#define TCP_CLOSED   TCP_RST
 CLOSED. More...
 
#define TCP_LISTEN   0
 LISTEN. More...
 
#define TCP_SYN_SENT   ( TCP_STATE_SENT ( TCP_SYN ) )
 SYN_SENT. More...
 
#define TCP_SYN_RCVD
 SYN_RCVD. More...
 
#define TCP_ESTABLISHED
 ESTABLISHED. More...
 
#define TCP_FIN_WAIT_1
 FIN_WAIT_1. More...
 
#define TCP_FIN_WAIT_2
 FIN_WAIT_2. More...
 
#define TCP_CLOSING_OR_LAST_ACK
 CLOSING / LAST_ACK. More...
 
#define TCP_TIME_WAIT
 TIME_WAIT. More...
 
#define TCP_CLOSE_WAIT
 CLOSE_WAIT. More...
 
#define TCP_CAN_SEND_DATA(state)
 Can send data in current state. More...
 
#define TCP_HAS_BEEN_ESTABLISHED(state)
 Have ever been fully established. More...
 
#define TCP_CLOSED_GRACEFULLY(state)
 Have closed gracefully. More...
 

Detailed Description

The TCP state is defined by a combination of the flags that have been sent to the peer, the flags that have been acknowledged by the peer, and the flags that have been received from the peer.

Macro Definition Documentation

◆ TCP_STATE_SENT

#define TCP_STATE_SENT (   flags)    ( (flags) << 0 )

TCP flags that have been sent in outgoing packets.

Definition at line 176 of file tcp.h.

◆ TCP_FLAGS_SENT

#define TCP_FLAGS_SENT (   state)    ( ( (state) >> 0 ) & 0xff )

Definition at line 177 of file tcp.h.

◆ TCP_STATE_ACKED

#define TCP_STATE_ACKED (   flags)    ( (flags) << 8 )

TCP flags that have been acknowledged by the peer.

Note that this applies only to SYN and FIN.

Definition at line 183 of file tcp.h.

◆ TCP_FLAGS_ACKED

#define TCP_FLAGS_ACKED (   state)    ( ( (state) >> 8 ) & 0xff )

Definition at line 184 of file tcp.h.

◆ TCP_STATE_RCVD

#define TCP_STATE_RCVD (   flags)    ( (flags) << 16 )

TCP flags that have been received from the peer.

Note that this applies only to SYN and FIN, and that once SYN has been received, we should always be sending ACK.

Definition at line 191 of file tcp.h.

◆ TCP_FLAGS_RCVD

#define TCP_FLAGS_RCVD (   state)    ( ( (state) >> 16 ) & 0xff )

Definition at line 192 of file tcp.h.

◆ TCP_FLAGS_SENDING

#define TCP_FLAGS_SENDING (   state)    ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )

TCP flags that are currently being sent in outgoing packets.

Definition at line 195 of file tcp.h.

◆ TCP_CLOSED

#define TCP_CLOSED   TCP_RST

CLOSED.

The connection has not yet been used for anything.

Definition at line 202 of file tcp.h.

◆ TCP_LISTEN

#define TCP_LISTEN   0

LISTEN.

Not currently used as a state; we have no support for listening connections. Given a unique value to avoid compiler warnings.

Definition at line 209 of file tcp.h.

◆ TCP_SYN_SENT

#define TCP_SYN_SENT   ( TCP_STATE_SENT ( TCP_SYN ) )

SYN_SENT.

SYN has been sent, nothing has yet been received or acknowledged.

Definition at line 215 of file tcp.h.

◆ TCP_SYN_RCVD

#define TCP_SYN_RCVD
Value:
TCP_STATE_RCVD ( TCP_SYN ) )
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

SYN_RCVD.

SYN has been sent but not acknowledged, SYN has been received.

Definition at line 221 of file tcp.h.

◆ TCP_ESTABLISHED

#define TCP_ESTABLISHED
Value:
TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_RCVD ( TCP_SYN ) )
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

ESTABLISHED.

SYN has been sent and acknowledged, SYN has been received.

Definition at line 228 of file tcp.h.

◆ TCP_FIN_WAIT_1

#define TCP_FIN_WAIT_1
Value:
TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_RCVD ( TCP_SYN ) )
#define TCP_FIN
Definition: tcp.h:163
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

FIN_WAIT_1.

SYN has been sent and acknowledged, SYN has been received, FIN has been sent but not acknowledged, FIN has not been received.

RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN acknowledged, i.e. if the application closes the connection after sending and receiving SYN, but before having had SYN acknowledged. However, we have to pretend that SYN has been acknowledged anyway, otherwise we end up sending SYN and FIN in the same sequence number slot. Therefore, when we transition from SYN_RCVD to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN) and increment our sequence number.

Definition at line 246 of file tcp.h.

◆ TCP_FIN_WAIT_2

#define TCP_FIN_WAIT_2
Value:
TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
TCP_STATE_RCVD ( TCP_SYN ) )
#define TCP_FIN
Definition: tcp.h:163
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

FIN_WAIT_2.

SYN has been sent and acknowledged, SYN has been received, FIN has been sent and acknowledged, FIN ha not been received.

Definition at line 255 of file tcp.h.

◆ TCP_CLOSING_OR_LAST_ACK

#define TCP_CLOSING_OR_LAST_ACK
Value:
TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
#define TCP_FIN
Definition: tcp.h:163
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

CLOSING / LAST_ACK.

SYN has been sent and acknowledged, SYN has been received, FIN has been sent but not acknowledged, FIN has been received.

This state actually encompasses both CLOSING and LAST_ACK; they are identical with the definition of state that we use. I don't believe that they need to be distinguished.

Definition at line 268 of file tcp.h.

◆ TCP_TIME_WAIT

#define TCP_TIME_WAIT
Value:
TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
#define TCP_FIN
Definition: tcp.h:163
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

TIME_WAIT.

SYN has been sent and acknowledged, SYN has been received, FIN has been sent and acknowledged, FIN has been received.

Definition at line 278 of file tcp.h.

◆ TCP_CLOSE_WAIT

#define TCP_CLOSE_WAIT
Value:
TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
#define TCP_FIN
Definition: tcp.h:163
#define TCP_ACK
Definition: tcp.h:159
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_SENT(flags)
TCP flags that have been sent in outgoing packets.
Definition: tcp.h:176

CLOSE_WAIT.

SYN has been sent and acknowledged, SYN has been received, FIN has been received.

Definition at line 287 of file tcp.h.

◆ TCP_CAN_SEND_DATA

#define TCP_CAN_SEND_DATA (   state)
Value:
( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_SENT ( TCP_FIN ) ) ) \
uint8_t state
State.
Definition: eth_slow.h:47
#define TCP_FIN
Definition: tcp.h:163
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_ACKED(flags)
TCP flags that have been acknowledged by the peer.
Definition: tcp.h:183

Can send data in current state.

We can send data if and only if we have had our SYN acked and we have not yet sent our FIN.

Definition at line 296 of file tcp.h.

◆ TCP_HAS_BEEN_ESTABLISHED

#define TCP_HAS_BEEN_ESTABLISHED (   state)
Value:
( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
TCP_STATE_RCVD ( TCP_SYN ) ) ) \
uint8_t state
State.
Definition: eth_slow.h:47
#define TCP_SYN
Definition: tcp.h:162
#define TCP_STATE_RCVD(flags)
TCP flags that have been received from the peer.
Definition: tcp.h:191
#define TCP_STATE_ACKED(flags)
TCP flags that have been acknowledged by the peer.
Definition: tcp.h:183

Have ever been fully established.

We have been fully established if we have both received a SYN and had our own SYN acked.

Definition at line 306 of file tcp.h.

◆ TCP_CLOSED_GRACEFULLY

#define TCP_CLOSED_GRACEFULLY (   state)
Value:
( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
TCP_STATE_RCVD ( TCP_FIN ) ) ) \
uint8_t state
State.
Definition: eth_slow.h:47
#define TCP_FIN
Definition: tcp.h:163
#define TCP_STATE_RCVD(flags)
TCP flags that have been received from the peer.
Definition: tcp.h:191
#define TCP_STATE_ACKED(flags)
TCP flags that have been acknowledged by the peer.
Definition: tcp.h:183

Have closed gracefully.

We have closed gracefully if we have both received a FIN and had our own FIN acked.

Definition at line 316 of file tcp.h.