8
* This file defines the gPXE TCP API.
12
FILE_LICENCE ( GPL2_OR_LATER );
14
#include <gpxe/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 timestamp option */
58
struct tcp_timestamp_option {
63
} __attribute__ (( packed ));
65
/** Padded TCP timestamp option (used for sending) */
66
struct tcp_timestamp_padded_option {
68
struct tcp_timestamp_option tsopt;
69
} __attribute__ (( packed ));
71
/** Code for the TCP timestamp option */
72
#define TCP_OPTION_TS 8
74
/** Parsed TCP options */
76
/** MSS option, if present */
77
const struct tcp_mss_option *mssopt;
78
/** Timestampe option, if present */
79
const struct tcp_timestamp_option *tsopt;
97
* @defgroup tcpstates TCP states
99
* The TCP state is defined by a combination of the flags that have
100
* been sent to the peer, the flags that have been acknowledged by the
101
* peer, and the flags that have been received from the peer.
106
/** TCP flags that have been sent in outgoing packets */
107
#define TCP_STATE_SENT(flags) ( (flags) << 0 )
108
#define TCP_FLAGS_SENT(state) ( ( (state) >> 0 ) & 0xff )
110
/** TCP flags that have been acknowledged by the peer
112
* Note that this applies only to SYN and FIN.
114
#define TCP_STATE_ACKED(flags) ( (flags) << 8 )
115
#define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff )
117
/** TCP flags that have been received from the peer
119
* Note that this applies only to SYN and FIN, and that once SYN has
120
* been received, we should always be sending ACK.
122
#define TCP_STATE_RCVD(flags) ( (flags) << 16 )
123
#define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff )
125
/** TCP flags that are currently being sent in outgoing packets */
126
#define TCP_FLAGS_SENDING(state) \
127
( TCP_FLAGS_SENT ( state ) & ~TCP_FLAGS_ACKED ( state ) )
131
* The connection has not yet been used for anything.
133
#define TCP_CLOSED TCP_RST
137
* Not currently used as a state; we have no support for listening
138
* connections. Given a unique value to avoid compiler warnings.
144
* SYN has been sent, nothing has yet been received or acknowledged.
146
#define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
150
* SYN has been sent but not acknowledged, SYN has been received.
152
#define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
153
TCP_STATE_RCVD ( TCP_SYN ) )
157
* SYN has been sent and acknowledged, SYN has been received.
159
#define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
160
TCP_STATE_ACKED ( TCP_SYN ) | \
161
TCP_STATE_RCVD ( TCP_SYN ) )
165
* SYN has been sent and acknowledged, SYN has been received, FIN has
166
* been sent but not acknowledged, FIN has not been received.
168
* RFC 793 shows that we can enter FIN_WAIT_1 without have had SYN
169
* acknowledged, i.e. if the application closes the connection after
170
* sending and receiving SYN, but before having had SYN acknowledged.
171
* However, we have to *pretend* that SYN has been acknowledged
172
* anyway, otherwise we end up sending SYN and FIN in the same
173
* sequence number slot. Therefore, when we transition from SYN_RCVD
174
* to FIN_WAIT_1, we have to remember to set TCP_STATE_ACKED(TCP_SYN)
175
* and increment our sequence number.
177
#define TCP_FIN_WAIT_1 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
178
TCP_STATE_ACKED ( TCP_SYN ) | \
179
TCP_STATE_RCVD ( TCP_SYN ) )
183
* SYN has been sent and acknowledged, SYN has been received, FIN has
184
* been sent and acknowledged, FIN ha not been received.
186
#define TCP_FIN_WAIT_2 ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
187
TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
188
TCP_STATE_RCVD ( TCP_SYN ) )
190
/** CLOSING / LAST_ACK
192
* SYN has been sent and acknowledged, SYN has been received, FIN has
193
* been sent but not acknowledged, FIN has been received.
195
* This state actually encompasses both CLOSING and LAST_ACK; they are
196
* identical with the definition of state that we use. I don't
197
* *believe* that they need to be distinguished.
199
#define TCP_CLOSING_OR_LAST_ACK \
200
( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
201
TCP_STATE_ACKED ( TCP_SYN ) | \
202
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
206
* SYN has been sent and acknowledged, SYN has been received, FIN has
207
* been sent and acknowledged, FIN has been received.
209
#define TCP_TIME_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK | TCP_FIN ) | \
210
TCP_STATE_ACKED ( TCP_SYN | TCP_FIN ) | \
211
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
215
* SYN has been sent and acknowledged, SYN has been received, FIN has
218
#define TCP_CLOSE_WAIT ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \
219
TCP_STATE_ACKED ( TCP_SYN ) | \
220
TCP_STATE_RCVD ( TCP_SYN | TCP_FIN ) )
222
/** Can send data in current state
224
* We can send data if and only if we have had our SYN acked and we
225
* have not yet sent our FIN.
227
#define TCP_CAN_SEND_DATA(state) \
228
( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
229
TCP_STATE_SENT ( TCP_FIN ) ) ) \
230
== TCP_STATE_ACKED ( TCP_SYN ) )
232
/** Have ever been fully established
234
* We have been fully established if we have both received a SYN and
235
* had our own SYN acked.
237
#define TCP_HAS_BEEN_ESTABLISHED(state) \
238
( ( (state) & ( TCP_STATE_ACKED ( TCP_SYN ) | \
239
TCP_STATE_RCVD ( TCP_SYN ) ) ) \
240
== ( TCP_STATE_ACKED ( TCP_SYN ) | TCP_STATE_RCVD ( TCP_SYN ) ) )
242
/** Have closed gracefully
244
* We have closed gracefully if we have both received a FIN and had
247
#define TCP_CLOSED_GRACEFULLY(state) \
248
( ( (state) & ( TCP_STATE_ACKED ( TCP_FIN ) | \
249
TCP_STATE_RCVD ( TCP_FIN ) ) ) \
250
== ( TCP_STATE_ACKED ( TCP_FIN ) | TCP_STATE_RCVD ( TCP_FIN ) ) )
254
/** Mask for TCP header length field */
255
#define TCP_MASK_HLEN 0xf0
257
/** Smallest port number on which a TCP connection can listen */
258
#define TCP_MIN_PORT 1
260
/* Some IOB constants */
261
#define MAX_HDR_LEN 100
262
#define MAX_IOB_LEN 1500
263
#define MIN_IOB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
266
* Maxmimum advertised TCP window size
268
* We estimate the TCP window size as the amount of free memory we
269
* have. This is not strictly accurate (since it ignores any space
270
* already allocated as RX buffers), but it will do for now.
272
* Since we don't store out-of-order received packets, the
273
* retransmission penalty is that the whole window contents must be
274
* resent. This suggests keeping the window size small, but bear in
275
* mind that the maximum bandwidth on any link is limited to
277
* max_bandwidth = ( tcp_window / round_trip_time )
279
* With a 48kB window, which probably accurately reflects our amount
280
* of free memory, and a WAN RTT of say 200ms, this gives a maximum
281
* bandwidth of 240kB/s. This is sufficiently close to realistic that
282
* we will need to be careful that our advertised window doesn't end
283
* up limiting WAN download speeds.
285
* Finally, since the window goes into a 16-bit field and we cannot
286
* actually use 65536, we use a window size of (65536-4) to ensure
287
* that payloads remain dword-aligned.
289
//#define TCP_MAX_WINDOW_SIZE ( 65536 - 4 )
290
#define TCP_MAX_WINDOW_SIZE 4096
295
* We really ought to implement Path MTU discovery. Until we do,
296
* anything with a path MTU greater than this may fail.
298
#define TCP_PATH_MTU 1460
303
* We currently hardcode this to a reasonable value and hope that the
304
* sender uses path MTU discovery. The alternative is breaking the
305
* abstraction layer so that we can find out the MTU from the IP layer
306
* (which would have to find out from the net device layer).
310
/** TCP maximum segment lifetime
312
* Currently set to 2 minutes, as per RFC 793.
314
#define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
316
extern struct tcpip_protocol tcp_protocol;
318
#endif /* _GPXE_TCP_H */