[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / include / gpxe / tcp.h
blobbe761725545defd52e5f13b87f5be0997b5ef90e
1 #ifndef _GPXE_TCP_H
2 #define _GPXE_TCP_H
4 /** @file
6 * TCP protocol
8 * This file defines the gPXE TCP API.
12 FILE_LICENCE ( GPL2_OR_LATER );
14 #include <gpxe/tcpip.h>
16 /**
17 * A TCP header
19 struct tcp_header {
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
32 * @{
35 /** End of TCP options list */
36 #define TCP_OPTION_END 0
38 /** TCP option pad */
39 #define TCP_OPTION_NOP 1
41 /** Generic TCP option */
42 struct tcp_option {
43 uint8_t kind;
44 uint8_t length;
45 } __attribute__ (( packed ));
47 /** TCP MSS option */
48 struct tcp_mss_option {
49 uint8_t kind;
50 uint8_t length;
51 uint16_t mss;
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 {
59 uint8_t kind;
60 uint8_t length;
61 uint32_t tsval;
62 uint32_t tsecr;
63 } __attribute__ (( packed ));
65 /** Padded TCP timestamp option (used for sending) */
66 struct tcp_timestamp_padded_option {
67 uint8_t nop[2];
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 */
75 struct 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;
82 /** @} */
85 * TCP flags
87 #define TCP_CWR 0x80
88 #define TCP_ECE 0x40
89 #define TCP_URG 0x20
90 #define TCP_ACK 0x10
91 #define TCP_PSH 0x08
92 #define TCP_RST 0x04
93 #define TCP_SYN 0x02
94 #define TCP_FIN 0x01
96 /**
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.
103 * @{
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 ) )
129 /** CLOSED
131 * The connection has not yet been used for anything.
133 #define TCP_CLOSED TCP_RST
135 /** LISTEN
137 * Not currently used as a state; we have no support for listening
138 * connections. Given a unique value to avoid compiler warnings.
140 #define TCP_LISTEN 0
142 /** SYN_SENT
144 * SYN has been sent, nothing has yet been received or acknowledged.
146 #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) )
148 /** SYN_RCVD
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 ) )
155 /** ESTABLISHED
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 ) )
163 /** FIN_WAIT_1
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 ) )
181 /** FIN_WAIT_2
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 ) )
204 /** TIME_WAIT
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 ) )
213 /** CLOSE_WAIT
215 * SYN has been sent and acknowledged, SYN has been received, FIN has
216 * been received.
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
245 * our own FIN acked.
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 ) ) )
252 /** @} */
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 8192
293 * Path MTU
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
301 * Advertised TCP MSS
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).
308 #define TCP_MSS 1460
310 /** TCP maximum segment lifetime
312 * Currently set to 2 minutes, as per RFC 793.
314 #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
317 * Compare TCP sequence numbers
319 * @v seq1 Sequence number 1
320 * @v seq2 Sequence number 2
321 * @ret diff Sequence difference
323 * Analogous to memcmp(), returns an integer less than, equal to, or
324 * greater than zero if @c seq1 is found, respectively, to be before,
325 * equal to, or after @c seq2.
327 static inline __attribute__ (( always_inline )) int32_t
328 tcp_cmp ( uint32_t seq1, uint32_t seq2 ) {
329 return ( ( int32_t ) ( seq1 - seq2 ) );
333 * Check if TCP sequence number lies within window
335 * @v seq Sequence number
336 * @v start Start of window
337 * @v len Length of window
338 * @ret in_window Sequence number is within window
340 static inline int tcp_in_window ( uint32_t seq, uint32_t start,
341 uint32_t len ) {
342 return ( ( seq - start ) < len );
345 extern struct tcpip_protocol tcp_protocol;
347 #endif /* _GPXE_TCP_H */