GNU Linux-libre 4.19.281-gnu1
[releases.git] / net / netrom / nr_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/uaccess.h>
27 #include <linux/fcntl.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <net/netrom.h>
31
32 static void nr_heartbeat_expiry(struct timer_list *);
33 static void nr_t1timer_expiry(struct timer_list *);
34 static void nr_t2timer_expiry(struct timer_list *);
35 static void nr_t4timer_expiry(struct timer_list *);
36 static void nr_idletimer_expiry(struct timer_list *);
37
38 void nr_init_timers(struct sock *sk)
39 {
40         struct nr_sock *nr = nr_sk(sk);
41
42         timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
43         timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
44         timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
45         timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
46
47         /* initialized by sock_init_data */
48         sk->sk_timer.function = nr_heartbeat_expiry;
49 }
50
51 void nr_start_t1timer(struct sock *sk)
52 {
53         struct nr_sock *nr = nr_sk(sk);
54
55         sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
56 }
57
58 void nr_start_t2timer(struct sock *sk)
59 {
60         struct nr_sock *nr = nr_sk(sk);
61
62         sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
63 }
64
65 void nr_start_t4timer(struct sock *sk)
66 {
67         struct nr_sock *nr = nr_sk(sk);
68
69         sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
70 }
71
72 void nr_start_idletimer(struct sock *sk)
73 {
74         struct nr_sock *nr = nr_sk(sk);
75
76         if (nr->idle > 0)
77                 sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
78 }
79
80 void nr_start_heartbeat(struct sock *sk)
81 {
82         sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
83 }
84
85 void nr_stop_t1timer(struct sock *sk)
86 {
87         sk_stop_timer(sk, &nr_sk(sk)->t1timer);
88 }
89
90 void nr_stop_t2timer(struct sock *sk)
91 {
92         sk_stop_timer(sk, &nr_sk(sk)->t2timer);
93 }
94
95 void nr_stop_t4timer(struct sock *sk)
96 {
97         sk_stop_timer(sk, &nr_sk(sk)->t4timer);
98 }
99
100 void nr_stop_idletimer(struct sock *sk)
101 {
102         sk_stop_timer(sk, &nr_sk(sk)->idletimer);
103 }
104
105 void nr_stop_heartbeat(struct sock *sk)
106 {
107         sk_stop_timer(sk, &sk->sk_timer);
108 }
109
110 int nr_t1timer_running(struct sock *sk)
111 {
112         return timer_pending(&nr_sk(sk)->t1timer);
113 }
114
115 static void nr_heartbeat_expiry(struct timer_list *t)
116 {
117         struct sock *sk = from_timer(sk, t, sk_timer);
118         struct nr_sock *nr = nr_sk(sk);
119
120         bh_lock_sock(sk);
121         switch (nr->state) {
122         case NR_STATE_0:
123                 /* Magic here: If we listen() and a new link dies before it
124                    is accepted() it isn't 'dead' so doesn't get removed. */
125                 if (sock_flag(sk, SOCK_DESTROY) ||
126                     (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
127                         sock_hold(sk);
128                         bh_unlock_sock(sk);
129                         nr_destroy_socket(sk);
130                         goto out;
131                 }
132                 break;
133
134         case NR_STATE_3:
135                 /*
136                  * Check for the state of the receive buffer.
137                  */
138                 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
139                     (nr->condition & NR_COND_OWN_RX_BUSY)) {
140                         nr->condition &= ~NR_COND_OWN_RX_BUSY;
141                         nr->condition &= ~NR_COND_ACK_PENDING;
142                         nr->vl         = nr->vr;
143                         nr_write_internal(sk, NR_INFOACK);
144                         break;
145                 }
146                 break;
147         }
148
149         nr_start_heartbeat(sk);
150         bh_unlock_sock(sk);
151 out:
152         sock_put(sk);
153 }
154
155 static void nr_t2timer_expiry(struct timer_list *t)
156 {
157         struct nr_sock *nr = from_timer(nr, t, t2timer);
158         struct sock *sk = &nr->sock;
159
160         bh_lock_sock(sk);
161         if (nr->condition & NR_COND_ACK_PENDING) {
162                 nr->condition &= ~NR_COND_ACK_PENDING;
163                 nr_enquiry_response(sk);
164         }
165         bh_unlock_sock(sk);
166         sock_put(sk);
167 }
168
169 static void nr_t4timer_expiry(struct timer_list *t)
170 {
171         struct nr_sock *nr = from_timer(nr, t, t4timer);
172         struct sock *sk = &nr->sock;
173
174         bh_lock_sock(sk);
175         nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
176         bh_unlock_sock(sk);
177         sock_put(sk);
178 }
179
180 static void nr_idletimer_expiry(struct timer_list *t)
181 {
182         struct nr_sock *nr = from_timer(nr, t, idletimer);
183         struct sock *sk = &nr->sock;
184
185         bh_lock_sock(sk);
186
187         nr_clear_queues(sk);
188
189         nr->n2count = 0;
190         nr_write_internal(sk, NR_DISCREQ);
191         nr->state = NR_STATE_2;
192
193         nr_start_t1timer(sk);
194         nr_stop_t2timer(sk);
195         nr_stop_t4timer(sk);
196
197         sk->sk_state     = TCP_CLOSE;
198         sk->sk_err       = 0;
199         sk->sk_shutdown |= SEND_SHUTDOWN;
200
201         if (!sock_flag(sk, SOCK_DEAD)) {
202                 sk->sk_state_change(sk);
203                 sock_set_flag(sk, SOCK_DEAD);
204         }
205         bh_unlock_sock(sk);
206         sock_put(sk);
207 }
208
209 static void nr_t1timer_expiry(struct timer_list *t)
210 {
211         struct nr_sock *nr = from_timer(nr, t, t1timer);
212         struct sock *sk = &nr->sock;
213
214         bh_lock_sock(sk);
215         switch (nr->state) {
216         case NR_STATE_1:
217                 if (nr->n2count == nr->n2) {
218                         nr_disconnect(sk, ETIMEDOUT);
219                         goto out;
220                 } else {
221                         nr->n2count++;
222                         nr_write_internal(sk, NR_CONNREQ);
223                 }
224                 break;
225
226         case NR_STATE_2:
227                 if (nr->n2count == nr->n2) {
228                         nr_disconnect(sk, ETIMEDOUT);
229                         goto out;
230                 } else {
231                         nr->n2count++;
232                         nr_write_internal(sk, NR_DISCREQ);
233                 }
234                 break;
235
236         case NR_STATE_3:
237                 if (nr->n2count == nr->n2) {
238                         nr_disconnect(sk, ETIMEDOUT);
239                         goto out;
240                 } else {
241                         nr->n2count++;
242                         nr_requeue_frames(sk);
243                 }
244                 break;
245         }
246
247         nr_start_t1timer(sk);
248 out:
249         bh_unlock_sock(sk);
250         sock_put(sk);
251 }