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