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 * Implementation of the Transmission Control Protocol(TCP). 7 * 8 * Version: @(#)tcp.c 1.0.16 05/25/93 9 * 10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu> 11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 12 * Mark Evans, <evansmp@uhura.aston.ac.uk> 13 * Corey Minyard <wf-rch!minyard@relay.EU.net> 14 * Florian La Roche, <flla@stud.uni-sb.de> 15 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 16 * Linus Torvalds, <torvalds@cs.helsinki.fi> 17 * Alan Cox, <gw4pts@gw4pts.ampr.org> 18 * Matthew Dillon, <dillon@apollo.west.oic.com> 19 * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 20 * Jorge Cwik, <jorge@laser.satlink.net> 21 */ 22
23 #include <net/tcp.h>
24
25 voidtcp_delack_timer(unsignedlongdata)
/* */ 26 { 27 tcp_send_ack((structsock *) data);
28 } 29
30 /* 31 * Reset the retransmission timer 32 */ 33
34 voidtcp_reset_xmit_timer(structsock *sk, intwhy, unsignedlongwhen)
/* */ 35 { 36 del_timer(&sk->retransmit_timer);
37 sk->ip_xmit_timeout = why;
38 if((long)when < 0)
39 { 40 when=3;
41 printk("Error: Negative timer in xmit_timer\n");
42 } 43 sk->retransmit_timer.expires=jiffies+when;
44 add_timer(&sk->retransmit_timer);
45 } 46
47 /* 48 * POLICY: 49 * 50 * This is the normal code called for timeouts. It does the retransmission 51 * and then does backoff. tcp_do_retransmit is separated out because 52 * tcp_ack needs to send stuff from the retransmit queue without 53 * initiating a backoff. 54 */ 55
56
57 staticvoidtcp_retransmit_time(structsock *sk, intall)
/* */ 58 { 59 tcp_do_retransmit(sk, all);
60
61 /* 62 * Increase the timeout each time we retransmit. Note that 63 * we do not increase the rtt estimate. rto is initialized 64 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests 65 * that doubling rto each time is the least we can get away with. 66 * In KA9Q, Karn uses this for the first few times, and then 67 * goes to quadratic. netBSD doubles, but only goes up to *64, 68 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is 69 * defined in the protocol as the maximum possible RTT. I guess 70 * we'll have to use something other than TCP to talk to the 71 * University of Mars. 72 * 73 * PAWS allows us longer timeouts and large windows, so once 74 * implemented ftp to mars will work nicely. We will have to fix 75 * the 120 second clamps though! 76 */ 77
78 sk->backoff++;
79 sk->rto = min(sk->rto << 1, 120*HZ);
80 tcp_reset_xmit_timer(sk, TIME_WRITE, sk->rto);
81 } 82
83 /* 84 * POLICY: 85 * Congestion control. 86 * 87 * A timer event has trigger a tcp retransmit timeout. The 88 * socket xmit queue is ready and set up to send. Because 89 * the ack receive code keeps the queue straight we do 90 * nothing clever here. 91 */ 92
93 voidtcp_retransmit(structsock *sk, intall)
/* */ 94 { 95 if (all)
96 { 97 tcp_retransmit_time(sk, all);
98 return;
99 } 100
101 sk->ssthresh = sk->cong_window >> 1; /* remember window where we lost */ 102 /* sk->ssthresh in theory can be zero. I guess that's OK */ 103 sk->cong_count = 0;
104
105 sk->cong_window = 1;
106
107 /* Do the actual retransmit. */ 108 tcp_retransmit_time(sk, all);
109 } 110
111 /* 112 * A write timeout has occurred. Process the after effects. BROKEN (badly) 113 */ 114
115 staticinttcp_write_timeout(structsock *sk)
/* */ 116 { 117 /* 118 * Look for a 'soft' timeout. 119 */ 120 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
121 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1))
122 { 123 /* 124 * Attempt to recover if arp has changed (unlikely!) or 125 * a route has shifted (not supported prior to 1.3). 126 */ 127 ip_rt_advice(&sk->ip_route_cache, 0);
128 } 129
130 /* 131 * Have we tried to SYN too many times (repent repent 8)) 132 */ 133
134 if(sk->retransmits > TCP_SYN_RETRIES && sk->state==TCP_SYN_SENT)
135 { 136 if(sk->err_soft)
137 sk->err=sk->err_soft;
138 else 139 sk->err=ETIMEDOUT;
140 sk->error_report(sk);
141 del_timer(&sk->retransmit_timer);
142 tcp_statistics.TcpAttemptFails++; /* Is this right ??? - FIXME - */ 143 tcp_set_state(sk,TCP_CLOSE);
144 /* Don't FIN, we got nothing back */ 145 return 0;
146 } 147 /* 148 * Has it gone just too far ? 149 */ 150 if (sk->retransmits > TCP_RETR2)
151 { 152 if(sk->err_soft)
153 sk->err = sk->err_soft;
154 else 155 sk->err = ETIMEDOUT;
156 sk->error_report(sk);
157 del_timer(&sk->retransmit_timer);
158 /* 159 * Time wait the socket 160 */ 161 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2 || sk->state == TCP_CLOSING )
162 { 163 tcp_set_state(sk,TCP_TIME_WAIT);
164 tcp_reset_msl_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
165 } 166 else 167 { 168 /* 169 * Clean up time. 170 */ 171 tcp_set_state(sk, TCP_CLOSE);
172 return 0;
173 } 174 } 175 return 1;
176 } 177
178 /* 179 * It could be we got here because we needed to send an ack, 180 * so we need to check for that and not just normal retransmit. 181 */ 182 staticvoidtcp_time_write_timeout(structsock * sk)
/* */ 183 { 184 /* 185 * Retransmission 186 */ 187 sk->prot->retransmit (sk, 0);
188 tcp_write_timeout(sk);
189 } 190
191
192 /* 193 * The TCP retransmit timer. This lacks a few small details. 194 * 195 * 1. An initial rtt timeout on the probe0 should cause what we can 196 * of the first write queue buffer to be split and sent. 197 * 2. On a 'major timeout' as defined by RFC1122 we shouldn't report 198 * ETIMEDOUT if we know an additional 'soft' error caused this. 199 * tcp_err should save a 'soft error' for us. 200 */ 201
202 voidtcp_retransmit_timer(unsignedlongdata)
/* */ 203 { 204 structsock *sk = (structsock*)data;
205 intwhy = sk->ip_xmit_timeout;
206
207 /* 208 * We are reset. We will send no more retransmits. 209 */ 210
211 if(sk->zapped)
212 return;
213
214 /* 215 * Only process if socket is not in use 216 */ 217
218 if (sk->users)
219 { 220 /* Try again in 1 second */ 221 sk->retransmit_timer.expires = jiffies+HZ;
222 add_timer(&sk->retransmit_timer);
223 return;
224 } 225
226 if (sk->ack_backlog && !sk->dead)
227 sk->data_ready(sk,0);
228
229 /* Now we need to figure out why the socket was on the timer. */ 230
231 switch (why)
232 { 233 /* Window probing */ 234 caseTIME_PROBE0:
235 tcp_send_probe0(sk);
236 tcp_write_timeout(sk);
237 break;
238
239 /* Retransmitting */ 240 caseTIME_WRITE:
241 tcp_time_write_timeout(sk);
242 break;
243
244 /* Sending Keepalives */ 245 caseTIME_KEEPOPEN:
246 /* 247 * this reset_timer() call is a hack, this is not 248 * how KEEPOPEN is supposed to work. 249 */ 250 tcp_reset_xmit_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
251 /* Send something to keep the connection open. */ 252 if (sk->prot->write_wakeup)
253 sk->prot->write_wakeup (sk);
254 sk->retransmits++;
255 sk->prot->retransmits++;
256 tcp_write_timeout(sk);
257 break;
258
259 default:
260 printk ("rexmit_timer: timer expired - reason unknown\n");
261 break;
262 } 263 }