1 /* 2 * INET An implementation of the TCP/IP protocol suite for the LINUX 3 * operating system. INET is implemented using the BSD Socket 4 * interface as the means of communication with the user level. 5 * 6 * Definitions for the TCP module. 7 * 8 * Version: @(#)tcp.h 1.0.5 05/23/93 9 * 10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu> 11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 12 * 13 * This program is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU General Public License 15 * as published by the Free Software Foundation; either version 16 * 2 of the License, or (at your option) any later version. 17 */ 18 #ifndef_TCP_H 19 #define_TCP_H 20
21 #include <linux/tcp.h>
22 #include <net/checksum.h>
23
24 #defineMAX_SYN_SIZE 44 + MAX_HEADER + 15
25 #define MAX_FIN_SIZE 40 + MAX_HEADER + 15
26 #defineMAX_ACK_SIZE 40 + MAX_HEADER + 15
27 #defineMAX_RESET_SIZE 40 + MAX_HEADER + 15
28 #defineMAX_WINDOW 32767 /* Never offer a window over 32767 without using 29 window scaling (not yet supported). Some poor 30 stacks do signed 16bit maths! */ 31 #defineMIN_WINDOW 2048
32 #define MAX_ACK_BACKLOG 2
33 #defineMAX_DUP_ACKS 2
34 #defineMIN_WRITE_SPACE 2048
35 #defineTCP_WINDOW_DIFF 2048
36
37 /* urg_data states */ 38 #defineURG_VALID 0x0100
39 #defineURG_NOTYET 0x0200
40 #defineURG_READ 0x0400
41
42 #defineTCP_RETR1 7 /* 43 * This is how many retries it does before it 44 * tries to figure out if the gateway is 45 * down. 46 */ 47
48 #defineTCP_RETR2 15 /* 49 * This should take at least 50 * 90 minutes to time out. 51 */ 52
53 #defineTCP_TIMEOUT_LEN (15*60*HZ) /* should be about 15 mins */ 54 #defineTCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to successfully 55 * close the socket, about 60 seconds */ 56 #defineTCP_FIN_TIMEOUT (3*60*HZ) /* BSD style FIN_WAIT2 deadlock breaker */ 57 #define TCP_ACK_TIME (3*HZ) /* time to delay before sending an ACK */ 58 #defineTCP_DONE_TIME (5*HZ/2)/* maximum time to wait before actually 59 * destroying a socket */ 60 #define TCP_WRITE_TIME (30*HZ) /* initial time to wait for an ACK, 61 * after last transmit */ 62 #defineTCP_TIMEOUT_INIT (3*HZ) /* RFC 1122 initial timeout value */ 63 #defineTCP_SYN_RETRIES 10 /* number of times to retry opening a 64 * connection (TCP_RETR2-....) */ 65 #define TCP_PROBEWAIT_LEN (1*HZ)/* time to wait between probes when 66 * I've got something to write and 67 * there is no window */ 68
69 #defineTCP_NO_CHECK 0 /* turn to one if you want the default 70 * to be no checksum */ 71
72
73 /* 74 * TCP option 75 */ 76
77 #defineTCPOPT_NOP 1 /* Padding */ 78 #defineTCPOPT_EOL 0 /* End of options */ 79 #defineTCPOPT_MSS 2 /* Segment size negotiating */ 80 /* 81 * We don't use these yet, but they are for PAWS and big windows 82 */ 83 #define TCPOPT_WINDOW 3 /* Window scaling */ 84 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ 85
86
87 /* 88 * The next routines deal with comparing 32 bit unsigned ints 89 * and worry about wraparound (automatic with unsigned arithmetic). 90 */ 91
92 extern__inlineintbefore(__u32seq1, __u32seq2)
/* */ 93 { 94 return (__s32)(seq1-seq2) < 0;
95 } 96
97 extern__inlineintafter(__u32seq1, __u32seq2)
/* */ 98 { 99 return (__s32)(seq2-seq1) < 0;
100 } 101
102
103 /* is s2<=s1<=s3 ? */ 104 extern__inlineint between(__u32seq1, __u32seq2, __u32seq3)
/* */ 105 { 106 return (after(seq1+1, seq2) && before(seq1, seq3+1));
107 } 108
109 static__inline__intmin(unsignedinta, unsignedintb)
/* */ 110 { 111 if (a > b)
112 a = b;
113 returna;
114 } 115
116 static__inline__intmax(unsignedinta, unsignedintb)
/* */ 117 { 118 if (a < b)
119 a = b;
120 returna;
121 } 122
123 externstructprototcp_prot;
124 externstructtcp_mibtcp_statistics;
125 externstructwait_queue *master_select_wakeup;
126
127 externvoidtcp_err(inttype, intcode, unsignedchar *header, __u32daddr,
128 __u32, structinet_protocol *protocol);
129 externvoidtcp_shutdown (structsock *sk, inthow);
130 externinttcp_rcv(structsk_buff *skb, structdevice *dev,
131 structoptions *opt, __u32daddr,
132 unsignedshortlen, __u32saddr, intredo,
133 structinet_protocol *protocol);
134
135 externinttcp_ioctl(structsock *sk, intcmd, unsignedlongarg);
136
137 externvoidtcp_read_wakeup(structsock *);
138 externvoidtcp_write_xmit(structsock *);
139 externvoidtcp_time_wait(structsock *);
140 externvoidtcp_retransmit(structsock *, int);
141 externvoidtcp_do_retransmit(structsock *, int);
142 externvoidtcp_send_check(structtcphdr *th, unsignedlongsaddr,
143 unsignedlongdaddr, intlen, structsk_buff *skb);
144
145 /* tcp_output.c */ 146
147 externvoidtcp_send_probe0(structsock *);
148 externvoidtcp_send_partial(structsock *);
149 externvoidtcp_write_wakeup(structsock *);
150 externvoidtcp_send_fin(structsock *sk);
151 externvoidtcp_send_synack(structsock *, structsock *, structsk_buff *);
152 externvoidtcp_send_skb(structsock *, structsk_buff *);
153 externvoidtcp_send_ack(structsock *sk);
154 externvoidtcp_send_delayed_ack(structsock *sk, inttimeout);
155 externvoidtcp_send_reset(unsignedlongsaddr, unsignedlongdaddr, structtcphdr *th,
156 structproto *prot, structoptions *opt, structdevice *dev, inttos, intttl);
157
158 externvoidtcp_enqueue_partial(structsk_buff *, structsock *);
159 externstructsk_buff * tcp_dequeue_partial(structsock *);
160
161 /* tcp_input.c */ 162 externvoidtcp_cache_zap(void);
163
164 /* tcp_timer.c */ 165 #definetcp_reset_msl_timer(x,y,z) reset_timer(x,y,z)
166 externvoidtcp_reset_xmit_timer(structsock *, int, unsignedlong);
167 externvoidtcp_delack_timer(unsignedlong);
168 externvoidtcp_retransmit_timer(unsignedlong);
169
170 /* 171 * Default sequence number picking algorithm. 172 * As close as possible to RFC 793, which 173 * suggests using a 250kHz clock. 174 * Further reading shows this assumes 2MB/s networks. 175 * For 10MB/s ethernet, a 1MHz clock is appropriate. 176 * That's funny, Linux has one built in! Use it! 177 */ 178
179 staticinlineu32tcp_init_seq(void)
/* */ 180 { 181 structtimevaltv;
182 do_gettimeofday(&tv);
183 returntv.tv_usec+tv.tv_sec*1000000;
184 } 185
186 static__inline__inttcp_old_window(structsock * sk)
/* */ 187 { 188 returnsk->window - (sk->acked_seq - sk->lastwin_seq);
189 } 190
191 static__inline__inttcp_new_window(structsock * sk)
/* */ 192 { 193 intwindow = sock_rspace(sk);
194
195 if (window > 1024)
196 window &= ~0x3FF; /* make free space a multiple of 1024 */ 197
198 if (sk->window_clamp && sk->window_clamp < window)
199 window = sk->window_clamp;
200
201 /* 202 * RFC 1122 says: 203 * 204 * "the suggested [SWS] avoidance algorithm for the receiver is to keep 205 * RECV.NEXT + RCV.WIN fixed until: 206 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)" 207 * 208 * Experiments against BSD and Solaris machines show that following 209 * these rules results in the BSD and Solaris machines making very 210 * bad guesses about how much data they can have in flight. 211 * 212 * Instead we follow the BSD lead and offer a window that gives 213 * the size of the current free space, truncated to a multiple 214 * of 1024 bytes. If the window is smaller than 215 * min(sk->mss, MAX_WINDOW/2) 216 * then we advertise the window as having size 0, unless this 217 * would shrink the window we offered last time. 218 * This results in as much as double the throughput as the original 219 * implementation. 220 */ 221
222 if (sk->mss == 0)
223 sk->mss = sk->mtu;
224
225 /* BSD style SWS avoidance 226 * Note that RFC1122 only says we must do silly window avoidance, 227 * it does not require that we use the suggested algorithm. 228 */ 229
230 if (window < min(sk->mss, MAX_WINDOW/2))
231 window = 0;
232
233 returnwindow;
234 } 235
236 /* 237 * Return true if we should raise the window when we 238 * have cleaned up the receive queue. We don't want to 239 * do this normally, only if it makes sense to avoid 240 * zero window probes.. 241 * 242 * We do this only if we can raise the window noticeably. 243 */ 244 static__inline__inttcp_raise_window(structsock * sk)
/* */ 245 { 246 returntcp_new_window(sk) >= 2*tcp_old_window(sk);
247 } 248
249 static__inline__unsignedshorttcp_select_window(structsock *sk)
/* */ 250 { 251 intwindow = tcp_new_window(sk);
252
253 /* Don't allow a shrinking window */ 254 if (window > tcp_old_window(sk)) { 255 sk->window = window;
256 sk->lastwin_seq = sk->acked_seq;
257 } 258 returnsk->window;
259 } 260
261 /* 262 * List all states of a TCP socket that can be viewed as a "connected" 263 * state. This now includes TCP_SYN_RECV, although I am not yet fully 264 * convinced that this is the solution for the 'getpeername(2)' 265 * problem. Thanks to Stephen A. Wood <saw@cebaf.gov> -FvK 266 */ 267
268 extern__inlineconstinttcp_connected(constintstate)
/* */ 269 { 270 return(state == TCP_ESTABLISHED || state == TCP_CLOSE_WAIT ||
271 state == TCP_FIN_WAIT1 || state == TCP_FIN_WAIT2 ||
272 state == TCP_SYN_RECV);
273 } 274
275 /* 276 * Calculate(/check) TCP checksum 277 */ 278 static__inline__u16tcp_check(structtcphdr *th, intlen,
/* */ 279 unsignedlongsaddr, unsignedlongdaddr, unsignedlongbase)
280 { 281 returncsum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
282 } 283
284 #undefSTATE_TRACE 285
286 #ifdefSTATE_TRACE 287 staticchar *statename[]={ 288 "Unused","Established","Syn Sent","Syn Recv",
289 "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
290 "Close Wait","Last ACK","Listen","Closing"
291 };
292 #endif 293
294 static__inline__voidtcp_set_state(structsock *sk, intstate)
/* */ 295 { 296 intoldstate = sk->state;
297
298 sk->state = state;
299
300 #ifdefSTATE_TRACE 301 if(sk->debug)
302 printk("TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
303 #endif 304
305 switch (state) { 306 caseTCP_ESTABLISHED:
307 if (oldstate != TCP_ESTABLISHED) { 308 tcp_statistics.TcpCurrEstab++;
309 /* This is a hack but it doesn't occur often and it's going to 310 be a real to fix nicely */ 311 if (oldstate == TCP_SYN_RECV)
312 wake_up_interruptible(&master_select_wakeup);
313 } 314 break;
315
316 caseTCP_CLOSE:
317 tcp_cache_zap();
318 /* Should be about 2 rtt's */ 319 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
320 /* fall through */ 321 default:
322 if (oldstate==TCP_ESTABLISHED)
323 tcp_statistics.TcpCurrEstab--;
324 } 325 } 326
327 #endif/* _TCP_H */