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 doesn't 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 * Alan Cox : Cleaned up unused code.
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/socket.h>
38 #include <linux/in.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <asm/system.h>
43 #include <linux/interrupt.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <net/ip.h>
47 #include <net/protocol.h>
48 #include <net/tcp.h>
49 #include <linux/skbuff.h>
50 #include <net/sock.h>
51 #include <net/arp.h>
52
53 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)
*/
54 {
55 unsigned long flags;
56
57 save_flags (flags);
58 cli();
59
60 t->timeout = 0;
61 del_timer (&t->timer);
62
63 restore_flags (flags);
64 }
65
66 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)
*/
67 {
68 delete_timer (t);
69 t->timeout = timeout;
70 #if 1
71 /* FIXME: ??? */
72 if ((int) len < 0) /* prevent close to infinite timers. THEY _DO_ */
73 len = 3; /* happen (negative values ?) - don't ask me why ! -FB */
74 #endif
75 t->timer.expires = jiffies+len;
76 add_timer (&t->timer);
77 }
78
79
80 /*
81 * Now we will only be called whenever we need to do
82 * something, but we must be sure to process all of the
83 * sockets that need it.
84 */
85
86 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)
*/
87 {
88 struct sock *sk = (struct sock*)data;
89 int why = sk->timeout;
90
91 /*
92 * only process if socket is not in use
93 */
94
95 cli();
96 if (sk->inuse || in_bh)
97 {
98 sk->timer.expires = jiffies+10;
99 add_timer(&sk->timer);
100 sti();
101 return;
102 }
103
104 sk->inuse = 1;
105 sti();
106
107 /* Always see if we need to send an ack. */
108
109 if (sk->ack_backlog && !sk->zapped)
110 {
111 sk->prot->read_wakeup (sk);
112 if (! sk->dead)
113 sk->data_ready(sk,0);
114 }
115
116 /* Now we need to figure out why the socket was on the timer. */
117
118 switch (why)
119 {
120 case TIME_DONE:
121 if (! sk->dead || sk->state != TCP_CLOSE)
122 {
123 printk ("non dead socket in time_done\n");
124 release_sock (sk);
125 break;
126 }
127 destroy_sock (sk);
128 break;
129
130 case TIME_DESTROY:
131 /*
132 * We've waited for a while for all the memory associated with
133 * the socket to be freed.
134 */
135 if(sk->wmem_alloc!=0 || sk->rmem_alloc!=0)
136 {
137 sk->wmem_alloc++; /* So it DOESN'T go away */
138 destroy_sock (sk);
139 sk->wmem_alloc--; /* Might now have hit 0 - fall through and do it again if so */
140 sk->inuse = 0; /* This will be ok, the destroy won't totally work */
141 }
142 if(sk->wmem_alloc==0 && sk->rmem_alloc==0)
143 destroy_sock(sk); /* Socket gone, DON'T update sk->inuse! */
144 break;
145
146 case TIME_CLOSE:
147 /* We've waited long enough, close the socket. */
148 sk->state = TCP_CLOSE;
149 delete_timer (sk);
150 if (!sk->dead)
151 sk->state_change(sk);
152 sk->shutdown = SHUTDOWN_MASK;
153 reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
154 release_sock (sk);
155 break;
156
157 default:
158 printk ("net_timer: timer expired - reason %d is unknown\n", why);
159 release_sock (sk);
160 break;
161 }
162 }
163