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 * We currently ignore the why/when parameters, and decide on 34 * our own how long we should wait on our own.. 35 */ 36
37 voidtcp_reset_xmit_timer(structsock *sk, intwhy, unsignedlongwhen)
/* */ 38 { 39 unsignedlongnow = jiffies;
40
41 when = ~0UL;
42 why = -1;
43
44 /* 45 * Was an old timer event active? 46 */ 47 if (del_timer(&sk->retransmit_timer)) { 48 why = sk->ip_xmit_timeout;
49 when = sk->retransmit_timer.expires;
50 } 51
52 /* 53 * Keepopen processing? 54 */ 55 if (sk->keepopen) { 56 unsignedlongnew_when = now + TCP_TIMEOUT_LEN;
57 if (new_when < when) { 58 when = new_when;
59 why = TIME_KEEPOPEN;
60 } 61 } 62
63 /* 64 * Retransmission? 65 */ 66 if (sk->send_head) { 67 structsk_buff * skb = sk->send_head;
68 unsignedlongnew_when = skb->when + sk->rto;
69 if (new_when < when) { 70 when = new_when;
71 why = TIME_WRITE;
72 } 73 }elseif (!skb_queue_empty(&sk->write_queue)) { 74 /* 75 * Zero window probe? 76 */ 77 structsk_buff * skb = sk->write_queue.next;
78 if (before(sk->window_seq, skb->end_seq)) { 79 unsignedlongnew_when = now + TIME_PROBE0;
80 if (new_when < when) { 81 when = new_when;
82 why = TIME_PROBE0;
83 } 84 } 85 } 86
87 if (why >= 0) { 88 sk->ip_xmit_timeout = why;
89 sk->retransmit_timer.expires = when;
90 add_timer(&sk->retransmit_timer);
91 } 92 } 93
94 /* 95 * POLICY: 96 * 97 * This is the normal code called for timeouts. It does the retransmission 98 * and then does backoff. tcp_do_retransmit is separated out because 99 * tcp_ack needs to send stuff from the retransmit queue without 100 * initiating a backoff. 101 */ 102
103
104 staticvoidtcp_retransmit_time(structsock *sk, intall)
/* */ 105 { 106 tcp_do_retransmit(sk, all);
107
108 /* 109 * Increase the timeout each time we retransmit. Note that 110 * we do not increase the rtt estimate. rto is initialized 111 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests 112 * that doubling rto each time is the least we can get away with. 113 * In KA9Q, Karn uses this for the first few times, and then 114 * goes to quadratic. netBSD doubles, but only goes up to *64, 115 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is 116 * defined in the protocol as the maximum possible RTT. I guess 117 * we'll have to use something other than TCP to talk to the 118 * University of Mars. 119 * 120 * PAWS allows us longer timeouts and large windows, so once 121 * implemented ftp to mars will work nicely. We will have to fix 122 * the 120 second clamps though! 123 */ 124
125 sk->backoff++;
126 sk->rto = min(sk->rto << 1, 120*HZ);
127 tcp_reset_xmit_timer(sk, TIME_WRITE, sk->rto);
128 } 129
130 /* 131 * POLICY: 132 * Congestion control. 133 * 134 * A timer event has trigger a tcp retransmit timeout. The 135 * socket xmit queue is ready and set up to send. Because 136 * the ack receive code keeps the queue straight we do 137 * nothing clever here. 138 */ 139
140 voidtcp_retransmit(structsock *sk, intall)
/* */ 141 { 142 if (all)
143 { 144 tcp_retransmit_time(sk, all);
145 return;
146 } 147
148 sk->ssthresh = sk->cong_window >> 1; /* remember window where we lost */ 149 /* sk->ssthresh in theory can be zero. I guess that's OK */ 150 sk->cong_count = 0;
151
152 sk->cong_window = 1;
153
154 /* Do the actual retransmit. */ 155 tcp_retransmit_time(sk, all);
156 } 157
158 /* 159 * A write timeout has occurred. Process the after effects. BROKEN (badly) 160 */ 161
162 staticinttcp_write_timeout(structsock *sk)
/* */ 163 { 164 /* 165 * Look for a 'soft' timeout. 166 */ 167 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
168 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1))
169 { 170 /* 171 * Attempt to recover if arp has changed (unlikely!) or 172 * a route has shifted (not supported prior to 1.3). 173 */ 174 ip_rt_advice(&sk->ip_route_cache, 0);
175 } 176
177 /* 178 * Have we tried to SYN too many times (repent repent 8)) 179 */ 180
181 if(sk->retransmits > TCP_SYN_RETRIES && sk->state==TCP_SYN_SENT)
182 { 183 if(sk->err_soft)
184 sk->err=sk->err_soft;
185 else 186 sk->err=ETIMEDOUT;
187 sk->error_report(sk);
188 del_timer(&sk->retransmit_timer);
189 tcp_statistics.TcpAttemptFails++; /* Is this right ??? - FIXME - */ 190 tcp_set_state(sk,TCP_CLOSE);
191 /* Don't FIN, we got nothing back */ 192 return 0;
193 } 194 /* 195 * Has it gone just too far ? 196 */ 197 if (sk->retransmits > TCP_RETR2)
198 { 199 if(sk->err_soft)
200 sk->err = sk->err_soft;
201 else 202 sk->err = ETIMEDOUT;
203 sk->error_report(sk);
204 del_timer(&sk->retransmit_timer);
205 /* 206 * Time wait the socket 207 */ 208 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2 || sk->state == TCP_CLOSING )
209 { 210 tcp_set_state(sk,TCP_TIME_WAIT);
211 tcp_reset_msl_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
212 } 213 else 214 { 215 /* 216 * Clean up time. 217 */ 218 tcp_set_state(sk, TCP_CLOSE);
219 return 0;
220 } 221 } 222 return 1;
223 } 224
225
226 /* 227 * The TCP retransmit timer. This lacks a few small details. 228 * 229 * 1. An initial rtt timeout on the probe0 should cause what we can 230 * of the first write queue buffer to be split and sent. 231 * 2. On a 'major timeout' as defined by RFC1122 we shouldn't report 232 * ETIMEDOUT if we know an additional 'soft' error caused this. 233 * tcp_err should save a 'soft error' for us. 234 */ 235
236 voidtcp_retransmit_timer(unsignedlongdata)
/* */ 237 { 238 structsock *sk = (structsock*)data;
239 intwhy = sk->ip_xmit_timeout;
240
241 /* 242 * We are reset. We will send no more retransmits. 243 */ 244
245 if(sk->zapped)
246 return;
247
248 /* 249 * Only process if socket is not in use 250 */ 251
252 if (sk->users)
253 { 254 /* Try again in 1 second */ 255 sk->retransmit_timer.expires = jiffies+HZ;
256 add_timer(&sk->retransmit_timer);
257 return;
258 } 259
260 if (sk->ack_backlog && !sk->dead)
261 sk->data_ready(sk,0);
262
263 /* Now we need to figure out why the socket was on the timer. */ 264
265 switch (why)
266 { 267 /* Window probing */ 268 caseTIME_PROBE0:
269 tcp_send_probe0(sk);
270 tcp_reset_xmit_timer(sk, 0, 0); /* get us going again */ 271 break;
272
273 /* Retransmitting */ 274 caseTIME_WRITE:
275 sk->prot->retransmit (sk, 0);
276 tcp_write_timeout(sk);
277 tcp_reset_xmit_timer(sk, 0, 0); /* get us going again */ 278 break;
279
280 /* Sending Keepalives */ 281 caseTIME_KEEPOPEN:
282 /* Send something to keep the connection open. */ 283 if (sk->prot->write_wakeup)
284 sk->prot->write_wakeup (sk);
285 sk->retransmits++;
286 sk->prot->retransmits++;
287 tcp_write_timeout(sk);
288 tcp_reset_xmit_timer (sk, 0, 0);
289 break;
290
291 default:
292 printk ("rexmit_timer: timer expired - reason unknown\n");
293 break;
294 } 295 }