8 * This file defines the gPXE TCP API.
13 #include <gpxe/tcpip.h>
14 #include <gpxe/stream.h>
20 uint16_t src; /* Source port */
21 uint16_t dest; /* Destination port */
22 uint32_t seq; /* Sequence number */
23 uint32_t ack; /* Acknowledgement number */
24 uint8_t hlen; /* Header length (4), Reserved (4) */
25 uint8_t flags; /* Reserved (2), Flags (6) */
26 uint16_t win; /* Advertised window */
27 uint16_t csum; /* Checksum */
28 uint16_t urg; /* Urgent pointer */
34 struct tcp_mss_option {
40 /** Code for the TCP MSS option */
41 #define TCP_OPTION_MSS 2
56 * @defgroup tcpstates TCP states
58 * The TCP state is defined by a combination of the flags that have
59 * been sent to the peer, the flags that have been acknowledged by the
60 * peer, and the flags that have been received from the peer.
65 /** TCP flags that have been sent in outgoing packets */
66 #define TCP_STATE_SENT(flags) ( (flags) << 0 )
67 #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
69 /** TCP flags that have been acknowledged by the peer
71 * Note that this applies only to SYN and FIN.
73 #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
74 #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
76 /** TCP flags that have been received from the peer
78 * Note that this applies only to SYN and FIN, and that once SYN has
79 * been received, we should always be sending ACK.
81 #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
82 #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
84 /** TCP flags that are currently being sent in outgoing packets */
85 #define TCP_FLAGS_SENDING(state) \
86 ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
90 * The connection has not yet been used for anything.
92 #define TCP_CLOSED TCP_RST
96 * Not currently used as a state; we have no support for listening
97 * connections. Given a unique value to avoid compiler warnings.
103 * SYN has been sent, nothing has yet been received or acknowledged.
105 #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
109 * SYN has been sent but not acknowledged, SYN has been received.
111 #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
112 TCP_STATE_RCVD ( TCP_SYN ) )
116 * SYN has been sent and acknowledged, SYN has been received.
118 #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
119 TCP_STATE_ACKED ( TCP_SYN ) | \
120 TCP_STATE_RCVD ( TCP_SYN ) )
124 * SYN has been sent and acknowledged, SYN has been received, FIN has
125 * been sent but not acknowledged, FIN has not been received.
127 * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
128 * acknowledged, i.e. if the application closes the connection after
129 * sending and receiving SYN, but before having had SYN acknowledged.
130 * However, we have to *pretend* that SYN has been acknowledged
131 * anyway, otherwise we end up sending SYN and FIN in the same
132 * sequence number slot. Therefore, when we transition from SYN_RCVD
133 * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
134 * and increment our sequence number.
136 #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
137 TCP_STATE_ACKED ( TCP_SYN ) | \
138 TCP_STATE_RCVD ( TCP_SYN ) )
142 * SYN has been sent and acknowledged, SYN has been received, FIN has
143 * been sent and acknowledged, FIN ha not been received.
145 #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
146 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
147 TCP_STATE_RCVD ( TCP_SYN ) )
149 /** CLOSING / LAST_ACK
151 * SYN has been sent and acknowledged, SYN has been received, FIN has
152 * been sent but not acknowledged, FIN has been received.
154 * This state actually encompasses both CLOSING and LAST_ACK; they are
155 * identical with the definition of state that we use. I don't
156 * *believe* that they need to be distinguished.
158 #define TCP_CLOSING_OR_LAST_ACK \
159 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
160 TCP_STATE_ACKED ( TCP_SYN ) | \
161 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
165 * SYN has been sent and acknowledged, SYN has been received, FIN has
166 * been sent and acknowledged, FIN has been received.
168 #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
169 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
170 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
174 * SYN has been sent and acknowledged, SYN has been received, FIN has
177 #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
178 TCP_STATE_ACKED ( TCP_SYN ) | \
179 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
181 /** Can send data in current state
183 * We can send data if and only if we have had our SYN acked and we
184 * have not yet sent our FIN.
186 #define TCP_CAN_SEND_DATA(state) \
187 ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
188 TCP_STATE_SENT ( TCP_FIN ) ) ) \
189 == TCP_STATE_ACKED ( TCP_SYN ) )
191 /** Have closed gracefully
193 * We have closed gracefully if we have both received a FIN and had
196 #define TCP_CLOSED_GRACEFULLY(state) \
197 ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
198 TCP_STATE_RCVD ( TCP_FIN ) ) ) \
199 == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
203 /** Mask for TCP header length field */
204 #define TCP_MASK_HLEN 0xf0
206 /** Smallest port number on which a TCP connection can listen */
207 #define TCP_MIN_PORT 1
209 /* Some IOB constants */
210 #define MAX_HDR_LEN 100
211 #define MAX_IOB_LEN 1500
212 #define MIN_IOB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
215 * Maxmimum advertised TCP window size
217 * We estimate the TCP window size as the amount of free memory we
218 * have. This is not strictly accurate (since it ignores any space
219 * already allocated as RX buffers), but it will do for now.
221 * Since we don't store out-of-order received packets, the
222 * retransmission penalty is that the whole window contents must be
223 * resent. This suggests keeping the window size small, but bear in
224 * mind that the maximum bandwidth on any link is limited to
226 * max_bandwidth = ( tcp_window / round_trip_time )
228 * With a 48kB window, which probably accurately reflects our amount
229 * of free memory, and a WAN RTT of say 200ms, this gives a maximum
230 * bandwidth of 240kB/s. This is sufficiently close to realistic that
231 * we will need to be careful that our advertised window doesn't end
232 * up limiting WAN download speeds.
234 * Finally, since the window goes into a 16-bit field and we cannot
235 * actually use 65536, we use a window size of (65536-4) to ensure
236 * that payloads remain dword-aligned.
238 #define TCP_MAX_WINDOW_SIZE ( 65536 - 4 )
243 * We currently hardcode this to a reasonable value and hope that the
244 * sender uses path MTU discovery. The alternative is breaking the
245 * abstraction layer so that we can find out the MTU from the IP layer
246 * (which would have to find out from the net device layer).
250 /** TCP maximum segment lifetime
252 * Currently set to 2 minutes, as per RFC 793.
254 #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
256 extern int tcp_open ( struct stream_application *app );
258 extern struct tcpip_protocol tcp_protocol;
260 #endif /* _GPXE_TCP_H */