GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / rose / rose_timer.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8  * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
9  */
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/in.h>
14 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
20 #include <net/ax25.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <net/sock.h>
25 #include <net/tcp_states.h>
26 #include <linux/fcntl.h>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
29 #include <net/rose.h>
30
31 static void rose_heartbeat_expiry(struct timer_list *t);
32 static void rose_timer_expiry(struct timer_list *);
33 static void rose_idletimer_expiry(struct timer_list *);
34
35 void rose_start_heartbeat(struct sock *sk)
36 {
37         sk_stop_timer(sk, &sk->sk_timer);
38
39         sk->sk_timer.function = rose_heartbeat_expiry;
40         sk->sk_timer.expires  = jiffies + 5 * HZ;
41
42         sk_reset_timer(sk, &sk->sk_timer, sk->sk_timer.expires);
43 }
44
45 void rose_start_t1timer(struct sock *sk)
46 {
47         struct rose_sock *rose = rose_sk(sk);
48
49         sk_stop_timer(sk, &rose->timer);
50
51         rose->timer.function = rose_timer_expiry;
52         rose->timer.expires  = jiffies + rose->t1;
53
54         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
55 }
56
57 void rose_start_t2timer(struct sock *sk)
58 {
59         struct rose_sock *rose = rose_sk(sk);
60
61         sk_stop_timer(sk, &rose->timer);
62
63         rose->timer.function = rose_timer_expiry;
64         rose->timer.expires  = jiffies + rose->t2;
65
66         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
67 }
68
69 void rose_start_t3timer(struct sock *sk)
70 {
71         struct rose_sock *rose = rose_sk(sk);
72
73         sk_stop_timer(sk, &rose->timer);
74
75         rose->timer.function = rose_timer_expiry;
76         rose->timer.expires  = jiffies + rose->t3;
77
78         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
79 }
80
81 void rose_start_hbtimer(struct sock *sk)
82 {
83         struct rose_sock *rose = rose_sk(sk);
84
85         sk_stop_timer(sk, &rose->timer);
86
87         rose->timer.function = rose_timer_expiry;
88         rose->timer.expires  = jiffies + rose->hb;
89
90         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
91 }
92
93 void rose_start_idletimer(struct sock *sk)
94 {
95         struct rose_sock *rose = rose_sk(sk);
96
97         sk_stop_timer(sk, &rose->idletimer);
98
99         if (rose->idle > 0) {
100                 rose->idletimer.function = rose_idletimer_expiry;
101                 rose->idletimer.expires  = jiffies + rose->idle;
102
103                 sk_reset_timer(sk, &rose->idletimer, rose->idletimer.expires);
104         }
105 }
106
107 void rose_stop_heartbeat(struct sock *sk)
108 {
109         sk_stop_timer(sk, &sk->sk_timer);
110 }
111
112 void rose_stop_timer(struct sock *sk)
113 {
114         sk_stop_timer(sk, &rose_sk(sk)->timer);
115 }
116
117 void rose_stop_idletimer(struct sock *sk)
118 {
119         sk_stop_timer(sk, &rose_sk(sk)->idletimer);
120 }
121
122 static void rose_heartbeat_expiry(struct timer_list *t)
123 {
124         struct sock *sk = from_timer(sk, t, sk_timer);
125         struct rose_sock *rose = rose_sk(sk);
126
127         bh_lock_sock(sk);
128         switch (rose->state) {
129         case ROSE_STATE_0:
130                 /* Magic here: If we listen() and a new link dies before it
131                    is accepted() it isn't 'dead' so doesn't get removed. */
132                 if (sock_flag(sk, SOCK_DESTROY) ||
133                     (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
134                         bh_unlock_sock(sk);
135                         rose_destroy_socket(sk);
136                         sock_put(sk);
137                         return;
138                 }
139                 break;
140
141         case ROSE_STATE_3:
142                 /*
143                  * Check for the state of the receive buffer.
144                  */
145                 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
146                     (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
147                         rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
148                         rose->condition &= ~ROSE_COND_ACK_PENDING;
149                         rose->vl         = rose->vr;
150                         rose_write_internal(sk, ROSE_RR);
151                         rose_stop_timer(sk);    /* HB */
152                         break;
153                 }
154                 break;
155         }
156
157         rose_start_heartbeat(sk);
158         bh_unlock_sock(sk);
159         sock_put(sk);
160 }
161
162 static void rose_timer_expiry(struct timer_list *t)
163 {
164         struct rose_sock *rose = from_timer(rose, t, timer);
165         struct sock *sk = &rose->sock;
166
167         bh_lock_sock(sk);
168         switch (rose->state) {
169         case ROSE_STATE_1:      /* T1 */
170         case ROSE_STATE_4:      /* T2 */
171                 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
172                 rose->state = ROSE_STATE_2;
173                 rose_start_t3timer(sk);
174                 break;
175
176         case ROSE_STATE_2:      /* T3 */
177                 rose->neighbour->use--;
178                 rose_disconnect(sk, ETIMEDOUT, -1, -1);
179                 break;
180
181         case ROSE_STATE_3:      /* HB */
182                 if (rose->condition & ROSE_COND_ACK_PENDING) {
183                         rose->condition &= ~ROSE_COND_ACK_PENDING;
184                         rose_enquiry_response(sk);
185                 }
186                 break;
187         }
188         bh_unlock_sock(sk);
189         sock_put(sk);
190 }
191
192 static void rose_idletimer_expiry(struct timer_list *t)
193 {
194         struct rose_sock *rose = from_timer(rose, t, idletimer);
195         struct sock *sk = &rose->sock;
196
197         bh_lock_sock(sk);
198         rose_clear_queues(sk);
199
200         rose_write_internal(sk, ROSE_CLEAR_REQUEST);
201         rose_sk(sk)->state = ROSE_STATE_2;
202
203         rose_start_t3timer(sk);
204
205         sk->sk_state     = TCP_CLOSE;
206         sk->sk_err       = 0;
207         sk->sk_shutdown |= SEND_SHUTDOWN;
208
209         if (!sock_flag(sk, SOCK_DEAD)) {
210                 sk->sk_state_change(sk);
211                 sock_set_flag(sk, SOCK_DEAD);
212         }
213         bh_unlock_sock(sk);
214         sock_put(sk);
215 }