1 #ifndef _GPXE_TCP_H 2 #define _GPXE_TCP_H 3 4 /** @file 5 * 6 * TCP protocol 7 * 8 * This file defines the gPXE TCP API. 9 * 10 */ 11 12 FILE_LICENCE ( GPL2_OR_LATER ); 13 14 #include <gpxe/tcpip.h> 15 16 /** 17 * A TCP header 18 */ 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 */ 29 }; 30 31 /** @defgroup tcpopts TCP options 32 * @{ 33 */ 34 35 /** End of TCP options list */ 36 #define TCP_OPTION_END 0 37 38 /** TCP option pad */ 39 #define TCP_OPTION_NOP 1 40 41 /** Generic TCP option */ 42 struct tcp_option { 43 uint8_t kind; 44 uint8_t length; 45 } __attribute__ (( packed )); 46 47 /** TCP MSS option */ 48 struct tcp_mss_option { 49 uint8_t kind; 50 uint8_t length; 51 uint16_t mss; 52 } __attribute__ (( packed )); 53 54 /** Code for the TCP MSS option */ 55 #define TCP_OPTION_MSS 2 56 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 )); 64 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 )); 70 71 /** Code for the TCP timestamp option */ 72 #define TCP_OPTION_TS 8 73 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; 80 }; 81 82 /** @} */ 83 84 /* 85 * TCP flags 86 */ 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 95 96 /** 97 * @defgroup tcpstates TCP states 98 * 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. 102 * 103 * @{ 104 */ 105 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 ) 109 110 /** TCP flags that have been acknowledged by the peer 111 * 112 * Note that this applies only to SYN and FIN. 113 */ 114 #define TCP_STATE_ACKED(flags) ( (flags) << 8 ) 115 #define TCP_FLAGS_ACKED(state) ( ( (state) >> 8 ) & 0xff ) 116 117 /** TCP flags that have been received from the peer 118 * 119 * Note that this applies only to SYN and FIN, and that once SYN has 120 * been received, we should always be sending ACK. 121 */ 122 #define TCP_STATE_RCVD(flags) ( (flags) << 16 ) 123 #define TCP_FLAGS_RCVD(state) ( ( (state) >> 16 ) & 0xff ) 124 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 ) ) 128 129 /** CLOSED 130 * 131 * The connection has not yet been used for anything. 132 */ 133 #define TCP_CLOSED TCP_RST 134 135 /** LISTEN 136 * 137 * Not currently used as a state; we have no support for listening 138 * connections. Given a unique value to avoid compiler warnings. 139 */ 140 #define TCP_LISTEN 0 141 142 /** SYN_SENT 143 * 144 * SYN has been sent, nothing has yet been received or acknowledged. 145 */ 146 #define TCP_SYN_SENT ( TCP_STATE_SENT ( TCP_SYN ) ) 147 148 /** SYN_RCVD 149 * 150 * SYN has been sent but not acknowledged, SYN has been received. 151 */ 152 #define TCP_SYN_RCVD ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \ 153 TCP_STATE_RCVD ( TCP_SYN ) ) 154 155 /** ESTABLISHED 156 * 157 * SYN has been sent and acknowledged, SYN has been received. 158 */ 159 #define TCP_ESTABLISHED ( TCP_STATE_SENT ( TCP_SYN | TCP_ACK ) | \ 160 TCP_STATE_ACKED ( TCP_SYN ) | \ 161 TCP_STATE_RCVD ( TCP_SYN ) ) 162 163 /** FIN_WAIT_1 164 * 165 * SYN has been sent and acknowledged, SYN has been received, FIN has 166 * been sent but not acknowledged, FIN has not been received. 167 * 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. 176 */ 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 ) ) 180 181 /** FIN_WAIT_2 182 * 183 * SYN has been sent and acknowledged, SYN has been received, FIN has 184 * been sent and acknowledged, FIN ha not been received. 185 */ 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 ) ) 189 190 /** CLOSING / LAST_ACK 191 * 192 * SYN has been sent and acknowledged, SYN has been received, FIN has 193 * been sent but not acknowledged, FIN has been received. 194 * 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. 198 */ 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 ) ) 203 204 /** TIME_WAIT 205 * 206 * SYN has been sent and acknowledged, SYN has been received, FIN has 207 * been sent and acknowledged, FIN has been received. 208 */ 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 ) ) 212 213 /** CLOSE_WAIT 214 * 215 * SYN has been sent and acknowledged, SYN has been received, FIN has 216 * been received. 217 */ 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 ) ) 221 222 /** Can send data in current state 223 * 224 * We can send data if and only if we have had our SYN acked and we 225 * have not yet sent our FIN. 226 */ 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 ) ) 231 232 /** Have ever been fully established 233 * 234 * We have been fully established if we have both received a SYN and 235 * had our own SYN acked. 236 */ 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 ) ) ) 241 242 /** Have closed gracefully 243 * 244 * We have closed gracefully if we have both received a FIN and had 245 * our own FIN acked. 246 */ 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 ) ) ) 251 252 /** @} */ 253 254 /** Mask for TCP header length field */ 255 #define TCP_MASK_HLEN 0xf0 256 257 /** Smallest port number on which a TCP connection can listen */ 258 #define TCP_MIN_PORT 1 259 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 */ 264 265 /** 266 * Maxmimum advertised TCP window size 267 * 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. 271 * 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 276 * 277 * max_bandwidth = ( tcp_window / round_trip_time ) 278 * 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. 284 * 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. 288 */ 289 #define TCP_MAX_WINDOW_SIZE ( 65536 - 4 ) 290 //#define TCP_MAX_WINDOW_SIZE 4096 291 292 /** 293 * Path MTU 294 * 295 * We really ought to implement Path MTU discovery. Until we do, 296 * anything with a path MTU greater than this may fail. 297 */ 298 #define TCP_PATH_MTU 1460 299 300 /** 301 * Advertised TCP MSS 302 * 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). 307 */ 308 #define TCP_MSS 1460 309 310 /** TCP maximum segment lifetime 311 * 312 * Currently set to 2 minutes, as per RFC 793. 313 */ 314 #define TCP_MSL ( 2 * 60 * TICKS_PER_SEC ) 315 316 extern struct tcpip_protocol tcp_protocol; 317 318 #endif /* _GPXE_TCP_H */ 319