8 * This file defines the gPXE TCP API.
13 #include <gpxe/tcpip.h>
19 uint16_t src; /* Source port */
20 uint16_t dest; /* Destination port */
21 uint32_t seq; /* Sequence number */
22 uint32_t ack; /* Acknowledgement number */
23 uint8_t hlen; /* Header length (4), Reserved (4) */
24 uint8_t flags; /* Reserved (2), Flags (6) */
25 uint16_t win; /* Advertised window */
26 uint16_t csum; /* Checksum */
27 uint16_t urg; /* Urgent pointer */
33 struct tcp_mss_option {
39 /** Code for the TCP MSS option */
40 #define TCP_OPTION_MSS 2
55 * @defgroup tcpstates TCP states
57 * The TCP state is defined by a combination of the flags that have
58 * been sent to the peer, the flags that have been acknowledged by the
59 * peer, and the flags that have been received from the peer.
64 /** TCP flags that have been sent in outgoing packets */
65 #define TCP_STATE_SENT(flags) ( (flags) << 0 )
66 #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
68 /** TCP flags that have been acknowledged by the peer
70 * Note that this applies only to SYN and FIN.
72 #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
73 #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
75 /** TCP flags that have been received from the peer
77 * Note that this applies only to SYN and FIN, and that once SYN has
78 * been received, we should always be sending ACK.
80 #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
81 #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
83 /** TCP flags that are currently being sent in outgoing packets */
84 #define TCP_FLAGS_SENDING(state) \
85 ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
89 * The connection has not yet been used for anything.
91 #define TCP_CLOSED TCP_RST
95 * Not currently used as a state; we have no support for listening
96 * connections. Given a unique value to avoid compiler warnings.
102 * SYN has been sent, nothing has yet been received or acknowledged.
104 #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
108 * SYN has been sent but not acknowledged, SYN has been received.
110 #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
111 TCP_STATE_RCVD ( TCP_SYN ) )
115 * SYN has been sent and acknowledged, SYN has been received.
117 #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
118 TCP_STATE_ACKED ( TCP_SYN ) | \
119 TCP_STATE_RCVD ( TCP_SYN ) )
123 * SYN has been sent and acknowledged, SYN has been received, FIN has
124 * been sent but not acknowledged, FIN has not been received.
126 * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
127 * acknowledged, i.e. if the application closes the connection after
128 * sending and receiving SYN, but before having had SYN acknowledged.
129 * However, we have to *pretend* that SYN has been acknowledged
130 * anyway, otherwise we end up sending SYN and FIN in the same
131 * sequence number slot. Therefore, when we transition from SYN_RCVD
132 * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
133 * and increment our sequence number.
135 #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
136 TCP_STATE_ACKED ( TCP_SYN ) | \
137 TCP_STATE_RCVD ( TCP_SYN ) )
141 * SYN has been sent and acknowledged, SYN has been received, FIN has
142 * been sent and acknowledged, FIN ha not been received.
144 #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
145 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
146 TCP_STATE_RCVD ( TCP_SYN ) )
148 /** CLOSING / LAST_ACK
150 * SYN has been sent and acknowledged, SYN has been received, FIN has
151 * been sent but not acknowledged, FIN has been received.
153 * This state actually encompasses both CLOSING and LAST_ACK; they are
154 * identical with the definition of state that we use. I don't
155 * *believe* that they need to be distinguished.
157 #define TCP_CLOSING_OR_LAST_ACK \
158 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
159 TCP_STATE_ACKED ( TCP_SYN ) | \
160 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
164 * SYN has been sent and acknowledged, SYN has been received, FIN has
165 * been sent and acknowledged, FIN has been received.
167 #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
168 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
169 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
173 * SYN has been sent and acknowledged, SYN has been received, FIN has
176 #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
177 TCP_STATE_ACKED ( TCP_SYN ) | \
178 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
180 /** Can send data in current state
182 * We can send data if and only if we have had our SYN acked and we
183 * have not yet sent our FIN.
185 #define TCP_CAN_SEND_DATA(state) \
186 ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
187 TCP_STATE_SENT ( TCP_FIN ) ) ) \
188 == TCP_STATE_ACKED ( TCP_SYN ) )
190 /** Have closed gracefully
192 * We have closed gracefully if we have both received a FIN and had
195 #define TCP_CLOSED_GRACEFULLY(state) \
196 ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
197 TCP_STATE_RCVD ( TCP_FIN ) ) ) \
198 == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
202 /** Mask for TCP header length field */
203 #define TCP_MASK_HLEN 0xf0
205 /** Smallest port number on which a TCP connection can listen */
206 #define TCP_MIN_PORT 1
208 /* Some IOB constants */
209 #define MAX_HDR_LEN 100
210 #define MAX_IOB_LEN 1500
211 #define MIN_IOB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
214 * Maxmimum advertised TCP window size
216 * We estimate the TCP window size as the amount of free memory we
217 * have. This is not strictly accurate (since it ignores any space
218 * already allocated as RX buffers), but it will do for now.
220 * Since we don't store out-of-order received packets, the
221 * retransmission penalty is that the whole window contents must be
222 * resent. This suggests keeping the window size small, but bear in
223 * mind that the maximum bandwidth on any link is limited to
225 * max_bandwidth = ( tcp_window / round_trip_time )
227 * With a 48kB window, which probably accurately reflects our amount
228 * of free memory, and a WAN RTT of say 200ms, this gives a maximum
229 * bandwidth of 240kB/s. This is sufficiently close to realistic that
230 * we will need to be careful that our advertised window doesn't end
231 * up limiting WAN download speeds.
233 * Finally, since the window goes into a 16-bit field and we cannot
234 * actually use 65536, we use a window size of (65536-4) to ensure
235 * that payloads remain dword-aligned.
237 #define TCP_MAX_WINDOW_SIZE ( 65536 - 4 )
242 * We really ought to implement Path MTU discovery. Until we do,
243 * anything with a path MTU greater than this may fail.
245 #define TCP_PATH_MTU 1460
250 * We currently hardcode this to a reasonable value and hope that the
251 * sender uses path MTU discovery. The alternative is breaking the
252 * abstraction layer so that we can find out the MTU from the IP layer
253 * (which would have to find out from the net device layer).
257 /** TCP maximum segment lifetime
259 * Currently set to 2 minutes, as per RFC 793.
261 #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
263 extern struct tcpip_protocol tcp_protocol;
265 #endif /* _GPXE_TCP_H */