GNU Linux-libre 4.14.290-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(unsigned long);
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.data     = (unsigned long)sk;
40         sk->sk_timer.function = &rose_heartbeat_expiry;
41         sk->sk_timer.expires  = jiffies + 5 * HZ;
42
43         sk_reset_timer(sk, &sk->sk_timer, sk->sk_timer.expires);
44 }
45
46 void rose_start_t1timer(struct sock *sk)
47 {
48         struct rose_sock *rose = rose_sk(sk);
49
50         sk_stop_timer(sk, &rose->timer);
51
52         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
53         rose->timer.expires  = jiffies + rose->t1;
54
55         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
56 }
57
58 void rose_start_t2timer(struct sock *sk)
59 {
60         struct rose_sock *rose = rose_sk(sk);
61
62         sk_stop_timer(sk, &rose->timer);
63
64         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
65         rose->timer.expires  = jiffies + rose->t2;
66
67         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
68 }
69
70 void rose_start_t3timer(struct sock *sk)
71 {
72         struct rose_sock *rose = rose_sk(sk);
73
74         sk_stop_timer(sk, &rose->timer);
75
76         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
77         rose->timer.expires  = jiffies + rose->t3;
78
79         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
80 }
81
82 void rose_start_hbtimer(struct sock *sk)
83 {
84         struct rose_sock *rose = rose_sk(sk);
85
86         sk_stop_timer(sk, &rose->timer);
87
88         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
89         rose->timer.expires  = jiffies + rose->hb;
90
91         sk_reset_timer(sk, &rose->timer, rose->timer.expires);
92 }
93
94 void rose_start_idletimer(struct sock *sk)
95 {
96         struct rose_sock *rose = rose_sk(sk);
97
98         sk_stop_timer(sk, &rose->timer);
99
100         if (rose->idle > 0) {
101                 rose->idletimer.function = (TIMER_FUNC_TYPE)rose_idletimer_expiry;
102                 rose->idletimer.expires  = jiffies + rose->idle;
103
104                 sk_reset_timer(sk, &rose->idletimer, rose->idletimer.expires);
105         }
106 }
107
108 void rose_stop_heartbeat(struct sock *sk)
109 {
110         sk_stop_timer(sk, &sk->sk_timer);
111 }
112
113 void rose_stop_timer(struct sock *sk)
114 {
115         sk_stop_timer(sk, &rose_sk(sk)->timer);
116 }
117
118 void rose_stop_idletimer(struct sock *sk)
119 {
120         sk_stop_timer(sk, &rose_sk(sk)->idletimer);
121 }
122
123 static void rose_heartbeat_expiry(unsigned long param)
124 {
125         struct sock *sk = (struct sock *)param;
126         struct rose_sock *rose = rose_sk(sk);
127
128         bh_lock_sock(sk);
129         switch (rose->state) {
130         case ROSE_STATE_0:
131                 /* Magic here: If we listen() and a new link dies before it
132                    is accepted() it isn't 'dead' so doesn't get removed. */
133                 if (sock_flag(sk, SOCK_DESTROY) ||
134                     (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
135                         bh_unlock_sock(sk);
136                         rose_destroy_socket(sk);
137                         sock_put(sk);
138                         return;
139                 }
140                 break;
141
142         case ROSE_STATE_3:
143                 /*
144                  * Check for the state of the receive buffer.
145                  */
146                 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
147                     (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
148                         rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
149                         rose->condition &= ~ROSE_COND_ACK_PENDING;
150                         rose->vl         = rose->vr;
151                         rose_write_internal(sk, ROSE_RR);
152                         rose_stop_timer(sk);    /* HB */
153                         break;
154                 }
155                 break;
156         }
157
158         rose_start_heartbeat(sk);
159         bh_unlock_sock(sk);
160         sock_put(sk);
161 }
162
163 static void rose_timer_expiry(struct timer_list *t)
164 {
165         struct rose_sock *rose = from_timer(rose, t, timer);
166         struct sock *sk = &rose->sock;
167
168         bh_lock_sock(sk);
169         switch (rose->state) {
170         case ROSE_STATE_1:      /* T1 */
171         case ROSE_STATE_4:      /* T2 */
172                 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
173                 rose->state = ROSE_STATE_2;
174                 rose_start_t3timer(sk);
175                 break;
176
177         case ROSE_STATE_2:      /* T3 */
178                 rose->neighbour->use--;
179                 rose_disconnect(sk, ETIMEDOUT, -1, -1);
180                 break;
181
182         case ROSE_STATE_3:      /* HB */
183                 if (rose->condition & ROSE_COND_ACK_PENDING) {
184                         rose->condition &= ~ROSE_COND_ACK_PENDING;
185                         rose_enquiry_response(sk);
186                 }
187                 break;
188         }
189         bh_unlock_sock(sk);
190         sock_put(sk);
191 }
192
193 static void rose_idletimer_expiry(struct timer_list *t)
194 {
195         struct rose_sock *rose = from_timer(rose, t, idletimer);
196         struct sock *sk = &rose->sock;
197
198         bh_lock_sock(sk);
199         rose_clear_queues(sk);
200
201         rose_write_internal(sk, ROSE_CLEAR_REQUEST);
202         rose_sk(sk)->state = ROSE_STATE_2;
203
204         rose_start_t3timer(sk);
205
206         sk->sk_state     = TCP_CLOSE;
207         sk->sk_err       = 0;
208         sk->sk_shutdown |= SEND_SHUTDOWN;
209
210         if (!sock_flag(sk, SOCK_DEAD)) {
211                 sk->sk_state_change(sk);
212                 sock_set_flag(sk, SOCK_DEAD);
213         }
214         bh_unlock_sock(sk);
215         sock_put(sk);
216 }