8 * This file defines the iPXE TCP API.
12 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
14 #include <ipxe/tcpip.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 */
31 /** @defgroup tcpopts TCP options
35 /** End of TCP options list */
36 #define TCP_OPTION_END 0
39 #define TCP_OPTION_NOP 1
41 /** Generic TCP option */
45 } __attribute__ (( packed ));
48 struct tcp_mss_option {
52 } __attribute__ (( packed ));
54 /** Code for the TCP MSS option */
55 #define TCP_OPTION_MSS 2
57 /** TCP window scale option */
58 struct tcp_window_scale_option {
62 } __attribute__ (( packed ));
64 /** Padded TCP window scale option (used for sending) */
65 struct tcp_window_scale_padded_option {
67 struct tcp_window_scale_option wsopt;
68 } __attribute (( packed ));
70 /** Code for the TCP window scale option */
71 #define TCP_OPTION_WS 3
73 /** Advertised TCP window scale
75 * Using a scale factor of 2**9 provides for a maximum window of 32MB,
76 * which is sufficient to allow Gigabit-speed transfers with a 200ms
77 * RTT. The minimum advertised window is 512 bytes, which is still
78 * less than a single packet.
80 #define TCP_RX_WINDOW_SCALE 9
82 /** TCP selective acknowledgement permitted option */
83 struct tcp_sack_permitted_option {
86 } __attribute__ (( packed ));
88 /** Padded TCP selective acknowledgement permitted option (used for sending) */
89 struct tcp_sack_permitted_padded_option {
91 struct tcp_sack_permitted_option spopt;
92 } __attribute__ (( packed ));
94 /** Code for the TCP selective acknowledgement permitted option */
95 #define TCP_OPTION_SACK_PERMITTED 4
97 /** TCP selective acknowledgement option */
98 struct tcp_sack_option {
101 } __attribute__ (( packed ));
103 /** TCP selective acknowledgement block */
104 struct tcp_sack_block {
107 } __attribute__ (( packed ));
109 /** Maximum number of selective acknowledgement blocks
111 * This allows for the presence of the TCP timestamp option.
113 #define TCP_SACK_MAX 3
115 /** Padded TCP selective acknowledgement option (used for sending) */
116 struct tcp_sack_padded_option {
118 struct tcp_sack_option sackopt;
119 } __attribute__ (( packed ));
121 /** Code for the TCP selective acknowledgement option */
122 #define TCP_OPTION_SACK 5
124 /** TCP timestamp option */
125 struct tcp_timestamp_option {
130 } __attribute__ (( packed ));
132 /** Padded TCP timestamp option (used for sending) */
133 struct tcp_timestamp_padded_option {
135 struct tcp_timestamp_option tsopt;
136 } __attribute__ (( packed ));
138 /** Code for the TCP timestamp option */
139 #define TCP_OPTION_TS 8
141 /** Parsed TCP options */
143 /** MSS option, if present */
144 const struct tcp_mss_option *mssopt;
145 /** Window scale option, if present */
146 const struct tcp_window_scale_option *wsopt;
147 /** SACK permitted option, if present */
148 const struct tcp_sack_permitted_option *spopt;
149 /** Timestamp option, if present */
150 const struct tcp_timestamp_option *tsopt;
168 * @defgroup tcpstates TCP states
170 * The TCP state is defined by a combination of the flags that have
171 * been sent to the peer, the flags that have been acknowledged by the
172 * peer, and the flags that have been received from the peer.
177 /** TCP flags that have been sent in outgoing packets */
178 #define TCP_STATE_SENT(flags) ( (flags) << 0 )
179 #define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
181 /** TCP flags that have been acknowledged by the peer
183 * Note that this applies only to SYN and FIN.
185 #define TCP_STATE_ACKED(flags) ( (flags) << 8 )
186 #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
188 /** TCP flags that have been received from the peer
190 * Note that this applies only to SYN and FIN, and that once SYN has
191 * been received, we should always be sending ACK.
193 #define TCP_STATE_RCVD(flags) ( (flags) << 16 )
194 #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
196 /** TCP flags that are currently being sent in outgoing packets */
197 #define TCP_FLAGS_SENDING(state) \
198 ( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
202 * The connection has not yet been used for anything.
204 #define TCP_CLOSED TCP_RST
208 * Not currently used as a state; we have no support for listening
209 * connections. Given a unique value to avoid compiler warnings.
215 * SYN has been sent, nothing has yet been received or acknowledged.
217 #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
221 * SYN has been sent but not acknowledged, SYN has been received.
223 #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
224 TCP_STATE_RCVD ( TCP_SYN ) )
228 * SYN has been sent and acknowledged, SYN has been received.
230 #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
231 TCP_STATE_ACKED ( TCP_SYN ) | \
232 TCP_STATE_RCVD ( TCP_SYN ) )
236 * SYN has been sent and acknowledged, SYN has been received, FIN has
237 * been sent but not acknowledged, FIN has not been received.
239 * RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
240 * acknowledged, i.e. if the application closes the connection after
241 * sending and receiving SYN, but before having had SYN acknowledged.
242 * However, we have to *pretend* that SYN has been acknowledged
243 * anyway, otherwise we end up sending SYN and FIN in the same
244 * sequence number slot. Therefore, when we transition from SYN_RCVD
245 * to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
246 * and increment our sequence number.
248 #define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
249 TCP_STATE_ACKED ( TCP_SYN ) | \
250 TCP_STATE_RCVD ( TCP_SYN ) )
254 * SYN has been sent and acknowledged, SYN has been received, FIN has
255 * been sent and acknowledged, FIN ha not been received.
257 #define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
258 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
259 TCP_STATE_RCVD ( TCP_SYN ) )
261 /** CLOSING / LAST_ACK
263 * SYN has been sent and acknowledged, SYN has been received, FIN has
264 * been sent but not acknowledged, FIN has been received.
266 * This state actually encompasses both CLOSING and LAST_ACK; they are
267 * identical with the definition of state that we use. I don't
268 * *believe* that they need to be distinguished.
270 #define TCP_CLOSING_OR_LAST_ACK \
271 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
272 TCP_STATE_ACKED ( TCP_SYN ) | \
273 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
277 * SYN has been sent and acknowledged, SYN has been received, FIN has
278 * been sent and acknowledged, FIN has been received.
280 #define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
281 TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
282 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
286 * SYN has been sent and acknowledged, SYN has been received, FIN has
289 #define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
290 TCP_STATE_ACKED ( TCP_SYN ) | \
291 TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
293 /** Can send data in current state
295 * We can send data if and only if we have had our SYN acked and we
296 * have not yet sent our FIN.
298 #define TCP_CAN_SEND_DATA(state) \
299 ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
300 TCP_STATE_SENT ( TCP_FIN ) ) ) \
301 == TCP_STATE_ACKED ( TCP_SYN ) )
303 /** Have ever been fully established
305 * We have been fully established if we have both received a SYN and
306 * had our own SYN acked.
308 #define TCP_HAS_BEEN_ESTABLISHED(state) \
309 ( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
310 TCP_STATE_RCVD ( TCP_SYN ) ) ) \
311 == ( TCP_STATE_ACKED ( TCP_SYN ) | TCP_STATE_RCVD ( TCP_SYN ) ) )
313 /** Have closed gracefully
315 * We have closed gracefully if we have both received a FIN and had
318 #define TCP_CLOSED_GRACEFULLY(state) \
319 ( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
320 TCP_STATE_RCVD ( TCP_FIN ) ) ) \
321 == ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
325 /** Mask for TCP header length field */
326 #define TCP_MASK_HLEN 0xf0
328 /** Smallest port number on which a TCP connection can listen */
329 #define TCP_MIN_PORT 1
332 * Maxmimum advertised TCP window size
334 * The maximum bandwidth on any link is limited by
336 * max_bandwidth * round_trip_time = tcp_window
338 * Some rough expectations for achievable bandwidths over various
341 * a) Gigabit LAN: expected bandwidth 125MB/s, typical RTT 0.5ms,
342 * minimum required window 64kB
344 * b) Home Internet connection: expected bandwidth 10MB/s, typical
345 * RTT 25ms, minimum required window 256kB
347 * c) WAN: expected bandwidth 2MB/s, typical RTT 100ms, minimum
348 * required window 200kB.
350 * The maximum possible value for the TCP window size is 1GB (using
351 * the maximum window scale of 2**14). However, it is advisable to
352 * keep the window size as small as possible (without limiting
353 * bandwidth), since in the event of a lost packet the window size
354 * represents the maximum amount that will need to be retransmitted.
356 * We therefore choose a maximum window size of 256kB.
358 #define TCP_MAX_WINDOW_SIZE ( 256 * 1024 )
363 * IPv6 requires all data link layers to support a datagram size of
364 * 1280 bytes. We choose to use this as our maximum transmitted
365 * datagram size, on the assumption that any practical link layer we
366 * encounter will allow this size. This is a very conservative
367 * assumption in practice, but the impact of making such a
368 * conservative assumption is insignificant since the amount of data
369 * that we transmit (rather than receive) is negligible.
371 * We allow space within this 1280 bytes for an IPv6 header, a TCP
372 * header, and a (padded) TCP timestamp option.
374 #define TCP_PATH_MTU \
375 ( 1280 - 40 /* IPv6 */ - 20 /* TCP */ - 12 /* TCP timestamp */ )
377 /** TCP maximum segment lifetime
379 * Currently set to 2 minutes, as per RFC 793.
381 #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
384 * TCP maximum header length
387 #define TCP_MAX_HEADER_LEN \
388 ( MAX_LL_NET_HEADER_LEN + \
389 sizeof ( struct tcp_header ) + \
390 sizeof ( struct tcp_mss_option ) + \
391 sizeof ( struct tcp_window_scale_padded_option ) + \
392 sizeof ( struct tcp_timestamp_padded_option ) )
395 * Compare TCP sequence numbers
397 * @v seq1 Sequence number 1
398 * @v seq2 Sequence number 2
399 * @ret diff Sequence difference
401 * Analogous to memcmp(), returns an integer less than, equal to, or
402 * greater than zero if @c seq1 is found, respectively, to be before,
403 * equal to, or after @c seq2.
405 static inline __attribute__ (( always_inline )) int32_t
406 tcp_cmp ( uint32_t seq1, uint32_t seq2 ) {
407 return ( ( int32_t ) ( seq1 - seq2 ) );
411 * Check if TCP sequence number lies within window
413 * @v seq Sequence number
414 * @v start Start of window
415 * @v len Length of window
416 * @ret in_window Sequence number is within window
418 static inline int tcp_in_window ( uint32_t seq, uint32_t start,
420 return ( ( seq - start ) < len );
423 /** TCP finish wait time
425 * Currently set to one second, since we should not allow a slowly
426 * responding server to substantially delay a call to shutdown().
428 #define TCP_FINISH_TIMEOUT ( 1 * TICKS_PER_SEC )
430 extern struct tcpip_protocol tcp_protocol __tcpip_protocol;
432 #endif /* _IPXE_TCP_H */