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 * TIMER - implementation of software timers for IP.
7 *
8 * Version: @(#)timer.c 1.0.7 05/25/93
9 *
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Corey Minyard <wf-rch!minyard@relay.EU.net>
13 * Fred Baumgarten, <dc6iq@insu1.etec.uni-karlsruhe.de>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 *
16 * Fixes:
17 * Alan Cox : To avoid destroying a wait queue as we use it
18 * we defer destruction until the destroy timer goes
19 * off.
20 * Alan Cox : Destroy socket doesnt write a status value to the
21 * socket buffer _AFTER_ freeing it! Also sock ensures
22 * the socket will get removed BEFORE this is called
23 * otherwise if the timer TIME_DESTROY occurs inside
24 * of inet_bh() with this socket being handled it goes
25 * BOOM! Have to stop timer going off if net_bh is
26 * active or the destroy causes crashes.
27 *
28 * This program is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU General Public License
30 * as published by the Free Software Foundation; either version
31 * 2 of the License, or (at your option) any later version.
32 */
33
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/socket.h>
37 #include <linux/in.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <asm/system.h>
42 #include <linux/interrupt.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include "ip.h"
46 #include "protocol.h"
47 #include "tcp.h"
48 #include <linux/skbuff.h>
49 #include "sock.h"
50 #include "arp.h"
51
52 void delete_timer (struct sock *t)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
53 {
54 unsigned long flags;
55
56 save_flags (flags);
57 cli();
58
59 t->timeout = 0;
60 del_timer (&t->timer);
61
62 restore_flags (flags);
63 }
64
65 void reset_timer (struct sock *t, int timeout, unsigned long len)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
66 {
67 delete_timer (t);
68 t->timeout = timeout;
69 #if 1
70 /* FIXME: ??? */
71 if ((int) len < 0) /* prevent close to infinite timers. THEY _DO_ */
72 len = 3; /* happen (negative values ?) - don't ask me why ! -FB */
73 #endif
74 t->timer.expires = len;
75 add_timer (&t->timer);
76 }
77
78
79 /*
80 * Now we will only be called whenever we need to do
81 * something, but we must be sure to process all of the
82 * sockets that need it.
83 */
84
85 void net_timer (unsigned long data)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
86 {
87 struct sock *sk = (struct sock*)data;
88 int why = sk->timeout;
89 /* timeout is overwritten by 'delete_timer' and 'reset_timer' */
90
91 cli();
92 if (sk->inuse || in_bh)
93 {
94 sk->timer.expires = 10;
95 add_timer(&sk->timer);
96 sti();
97 return;
98 }
99
100 sk->inuse = 1;
101 sti();
102
103 if (skb_peek(&sk->write_queue) &&
104 before(sk->window_seq, sk->write_queue.next->h.seq) &&
105 sk->send_head == NULL &&
106 sk->ack_backlog == 0 &&
107 sk->state != TCP_TIME_WAIT)
108 reset_timer(sk, TIME_PROBE0, sk->rto);
109 else if (sk->keepopen)
110 reset_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
111
112 /* Always see if we need to send an ack. */
113
114 if (sk->ack_backlog)
115 {
116 sk->prot->read_wakeup (sk);
117 if (! sk->dead)
118 sk->data_ready(sk,0);
119 }
120
121 /* Now we need to figure out why the socket was on the timer. */
122
123 switch (why)
124 {
125 case TIME_DONE:
126 if (! sk->dead || sk->state != TCP_CLOSE)
127 {
128 printk ("non dead socket in time_done\n");
129 release_sock (sk);
130 break;
131 }
132 destroy_sock (sk);
133 break;
134
135 case TIME_DESTROY:
136 /*
137 * We've waited for a while for all the memory associated with
138 * the socket to be freed.
139 */
140 if(sk->wmem_alloc!=0 || sk->rmem_alloc!=0)
141 {
142 sk->wmem_alloc++; /* So it DOESNT go away */
143 destroy_sock (sk);
144 sk->wmem_alloc--; /* Might now have hit 0 - fall through and do it again if so */
145 sk->inuse = 0; /* This will be ok, the destroy won't totally work */
146 }
147 if(sk->wmem_alloc==0 && sk->rmem_alloc==0)
148 destroy_sock(sk); /* Socket gone, DONT update sk->inuse! */
149 break;
150 case TIME_CLOSE:
151 /* We've waited long enough, close the socket. */
152 sk->state = TCP_CLOSE;
153 delete_timer (sk);
154 /* Kill the ARP entry in case the hardware has changed. */
155 arp_destroy (sk->daddr, 0);
156 if (!sk->dead)
157 sk->state_change(sk);
158 sk->shutdown = SHUTDOWN_MASK;
159 reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
160 release_sock (sk);
161 break;
162 case TIME_PROBE0:
163 tcp_send_probe0(sk);
164 release_sock (sk);
165 break;
166 case TIME_WRITE: /* try to retransmit. */
167 /* It could be we got here because we needed to send an ack.
168 * So we need to check for that.
169 */
170 {
171 struct sk_buff *skb;
172 unsigned long flags;
173
174 save_flags(flags);
175 cli();
176 skb = sk->send_head;
177 if (!skb)
178 {
179 restore_flags(flags);
180 }
181 else
182 {
183 if (jiffies < skb->when + sk->rto)
184 {
185 reset_timer (sk, TIME_WRITE, skb->when + sk->rto - jiffies);
186 restore_flags(flags);
187 release_sock (sk);
188 break;
189 }
190 restore_flags(flags);
191 /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
192 sk->retransmits, sk->packets_out, sk->cong_window); */
193 sk->prot->retransmit (sk, 0);
194 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
195 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1))
196 {
197 arp_destroy (sk->daddr, 0);
198 ip_route_check (sk->daddr);
199 }
200 if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2)
201 {
202 sk->err = ETIMEDOUT;
203 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2 || sk->state == TCP_CLOSING)
204 {
205 sk->state = TCP_TIME_WAIT;
206 reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
207 }
208 else
209 {
210 sk->prot->close (sk, 1);
211 break;
212 }
213 }
214 }
215 release_sock (sk);
216 break;
217 }
218 case TIME_KEEPOPEN:
219 /* Send something to keep the connection open. */
220 if (sk->prot->write_wakeup)
221 sk->prot->write_wakeup (sk);
222 sk->retransmits++;
223 if (sk->shutdown == SHUTDOWN_MASK)
224 {
225 sk->prot->close (sk, 1);
226 sk->state = TCP_CLOSE;
227 }
228 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
229 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1))
230 {
231 arp_destroy (sk->daddr, 0);
232 ip_route_check (sk->daddr);
233 release_sock (sk);
234 break;
235 }
236 if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2)
237 {
238 arp_destroy (sk->daddr, 0);
239 sk->err = ETIMEDOUT;
240 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2)
241 {
242 sk->state = TCP_TIME_WAIT;
243 if (!sk->dead)
244 sk->state_change(sk);
245 release_sock (sk);
246 }
247 else
248 {
249 sk->prot->close (sk, 1);
250 }
251 break;
252 }
253 release_sock (sk);
254 break;
255 default:
256 printk ("net_timer: timer expired - reason unknown\n");
257 release_sock (sk);
258 break;
259 }
260 }
261